2010-01-07 Nate Chapin <japhet@chromium.org>
[WebKit.git] / WebCore / bindings / v8 / custom / V8StorageCustom.cpp
1 /*
2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32
33 #if ENABLE(DOM_STORAGE)
34 #include "V8Storage.h"
35
36 #include "Storage.h"
37 #include "V8Binding.h"
38 #include "V8CustomBinding.h"
39 #include "V8Proxy.h"
40
41 namespace WebCore {
42
43 // Get an array containing the names of indexed properties in a collection.
44 v8::Handle<v8::Array> V8Storage::namedPropertyEnumerator(const v8::AccessorInfo& info)
45 {
46     Storage* storage = V8DOMWrapper::convertToNativeObject<Storage>(V8ClassIndex::STORAGE, info.Holder());
47     unsigned int length = storage->length();
48     v8::Handle<v8::Array> properties = v8::Array::New(length);
49     for (unsigned int i = 0; i < length; ++i) {
50         String key = storage->key(i);
51         ASSERT(!key.isNull());
52         String val = storage->getItem(key);
53         properties->Set(v8::Integer::New(i), v8String(key));
54     }
55
56     return properties;
57 }
58
59 static v8::Handle<v8::Value> storageGetter(v8::Local<v8::String> v8Name, const v8::AccessorInfo& info)
60 {
61     Storage* storage = V8DOMWrapper::convertToNativeObject<Storage>(V8ClassIndex::STORAGE, info.Holder());
62     String name = toWebCoreString(v8Name);
63
64     if (storage->contains(name) && name != "length")
65         return v8String(storage->getItem(name));
66
67     return notHandledByInterceptor();
68 }
69
70 v8::Handle<v8::Value> V8Storage::indexedPropertyGetter(uint32_t index, const v8::AccessorInfo& info)
71 {
72     INC_STATS("DOM.Storage.IndexedPropertyGetter");
73     v8::Local<v8::Integer> indexV8 = v8::Integer::New(index);
74     return storageGetter(indexV8->ToString(), info);
75 }
76
77 v8::Handle<v8::Value> V8Storage::namedPropertyGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
78 {
79     INC_STATS("DOM.Storage.NamedPropertyGetter");
80     return storageGetter(name, info);
81 }
82
83 static v8::Handle<v8::Value> storageSetter(v8::Local<v8::String> v8Name, v8::Local<v8::Value> v8Value, const v8::AccessorInfo& info)
84 {
85     Storage* storage = V8DOMWrapper::convertToNativeObject<Storage>(V8ClassIndex::STORAGE, info.Holder());
86     String name = toWebCoreString(v8Name);
87     String value = toWebCoreString(v8Value);
88
89     // Silently ignore length (rather than letting the bindings raise an exception).
90     if (name == "length")
91         return v8Value;
92
93     v8::Handle<v8::Value> prototypeValue = info.Holder()->GetRealNamedPropertyInPrototypeChain(v8Name);
94     if (!prototypeValue.IsEmpty())
95         return notHandledByInterceptor();
96
97     ExceptionCode ec = 0;
98     storage->setItem(name, value, ec);
99     if (ec)
100         return throwError(ec);
101
102     return v8Value;
103 }
104
105 v8::Handle<v8::Value> V8Storage::indexedPropertySetter(uint32_t index, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
106 {
107     INC_STATS("DOM.Storage.NamedPropertyGetter");
108     v8::Local<v8::Integer> indexV8 = v8::Integer::New(index);
109     return storageSetter(indexV8->ToString(), value, info);
110 }
111
112 v8::Handle<v8::Value> V8Storage::namedPropertySetter(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
113 {
114     INC_STATS("DOM.Storage.NamedPropertySetter");
115     return storageSetter(name, value, info);
116 }
117
118 static v8::Handle<v8::Boolean> storageDeleter(v8::Local<v8::String> v8Name, const v8::AccessorInfo& info)
119 {
120     Storage* storage = V8DOMWrapper::convertToNativeObject<Storage>(V8ClassIndex::STORAGE, info.Holder());
121     String name = toWebCoreString(v8Name);
122     
123     if (storage->contains(name)) {
124         storage->removeItem(name);
125         return v8::True();
126     }
127
128     return deletionNotHandledByInterceptor();
129 }
130
131 v8::Handle<v8::Boolean> V8Storage::indexedPropertyDeleter(uint32_t index, const v8::AccessorInfo& info)
132 {
133     INC_STATS("DOM.Storage.IndexedPropertyDeleter");
134     v8::Local<v8::Integer> indexV8 = v8::Integer::New(index);
135     return storageDeleter(indexV8->ToString(), info);
136 }
137
138 v8::Handle<v8::Boolean> V8Storage::namedPropertyDeleter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
139 {
140     INC_STATS("DOM.Storage.NamedPropertyDeleter");
141     return storageDeleter(name, info);
142 }
143
144 } // namespace WebCore
145
146 #endif