f3f4687ed4ff12c094774a3c474b95018f7bef21
[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
35 #include "Storage.h"
36 #include "V8Binding.h"
37 #include "V8CustomBinding.h"
38 #include "V8Proxy.h"
39
40 namespace WebCore {
41
42 // Get an array containing the names of indexed properties in a collection.
43 v8::Handle<v8::Array> V8Custom::v8StorageNamedPropertyEnumerator(const v8::AccessorInfo& info)
44 {
45     Storage* storage = V8DOMWrapper::convertToNativeObject<Storage>(V8ClassIndex::STORAGE, info.Holder());
46     unsigned int length = storage->length();
47     v8::Handle<v8::Array> properties = v8::Array::New(length);
48     for (unsigned int i = 0; i < length; ++i) {
49         String key = storage->key(i);
50         ASSERT(!key.isNull());
51         String val = storage->getItem(key);
52         properties->Set(v8::Integer::New(i), v8String(key));
53     }
54
55     return properties;
56 }
57
58 static v8::Handle<v8::Value> storageGetter(v8::Local<v8::String> v8Name, const v8::AccessorInfo& info)
59 {
60     Storage* storage = V8DOMWrapper::convertToNativeObject<Storage>(V8ClassIndex::STORAGE, info.Holder());
61     String name = toWebCoreString(v8Name);
62
63     if (storage->contains(name) && name != "length")
64         return v8String(storage->getItem(name));
65
66     return notHandledByInterceptor();
67 }
68
69 INDEXED_PROPERTY_GETTER(Storage)
70 {
71     INC_STATS("DOM.Storage.IndexedPropertyGetter");
72     v8::Local<v8::Integer> indexV8 = v8::Integer::New(index);
73     return storageGetter(indexV8->ToString(), info);
74 }
75
76 NAMED_PROPERTY_GETTER(Storage)
77 {
78     INC_STATS("DOM.Storage.NamedPropertyGetter");
79     return storageGetter(name, info);
80 }
81
82 static v8::Handle<v8::Value> storageSetter(v8::Local<v8::String> v8Name, v8::Local<v8::Value> v8Value, const v8::AccessorInfo& info)
83 {
84     Storage* storage = V8DOMWrapper::convertToNativeObject<Storage>(V8ClassIndex::STORAGE, info.Holder());
85     String name = toWebCoreString(v8Name);
86     String value = toWebCoreString(v8Value);
87
88     // Silently ignore length (rather than letting the bindings raise an exception).
89     if (name == "length")
90         return v8Value;
91
92     v8::Handle<v8::Value> prototypeValue = info.Holder()->GetRealNamedPropertyInPrototypeChain(v8Name);
93     if (!prototypeValue.IsEmpty())
94         return notHandledByInterceptor();
95
96     ExceptionCode ec = 0;
97     storage->setItem(name, value, ec);
98     if (ec)
99         return throwError(ec);
100
101     return v8Value;
102 }
103
104 INDEXED_PROPERTY_SETTER(Storage)
105 {
106     INC_STATS("DOM.Storage.NamedPropertyGetter");
107     v8::Local<v8::Integer> indexV8 = v8::Integer::New(index);
108     return storageSetter(indexV8->ToString(), value, info);
109 }
110
111 NAMED_PROPERTY_SETTER(Storage)
112 {
113     INC_STATS("DOM.Storage.NamedPropertySetter");
114     return storageSetter(name, value, info);
115 }
116
117 static v8::Handle<v8::Boolean> storageDeleter(v8::Local<v8::String> v8Name, const v8::AccessorInfo& info)
118 {
119     Storage* storage = V8DOMWrapper::convertToNativeObject<Storage>(V8ClassIndex::STORAGE, info.Holder());
120     String name = toWebCoreString(v8Name);
121     
122     if (storage->contains(name)) {
123         storage->removeItem(name);
124         return v8::True();
125     }
126
127     return deletionNotHandledByInterceptor();
128 }
129
130 INDEXED_PROPERTY_DELETER(Storage)
131 {
132     INC_STATS("DOM.Storage.IndexedPropertyDeleter");
133     v8::Local<v8::Integer> indexV8 = v8::Integer::New(index);
134     return storageDeleter(indexV8->ToString(), info);
135 }
136
137 NAMED_PROPERTY_DELETER(Storage)
138 {
139     INC_STATS("DOM.Storage.NamedPropertyDeleter");
140     return storageDeleter(name, info);
141 }
142
143 } // namespace WebCore
144
145 #endif