2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
33 #if ENABLE(DOM_STORAGE)
34 #include "V8Storage.h"
37 #include "V8Binding.h"
42 // Get an array containing the names of indexed properties in a collection.
43 v8::Handle<v8::Array> V8Storage::namedPropertyEnumerator(const v8::AccessorInfo& info)
45 Storage* storage = V8Storage::toNative(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));
58 static v8::Handle<v8::Value> storageGetter(v8::Local<v8::String> v8Name, const v8::AccessorInfo& info)
60 Storage* storage = V8Storage::toNative(info.Holder());
61 String name = toWebCoreString(v8Name);
63 if (storage->contains(name) && name != "length")
64 return v8String(storage->getItem(name));
66 return notHandledByInterceptor();
69 v8::Handle<v8::Value> V8Storage::indexedPropertyGetter(uint32_t index, const v8::AccessorInfo& info)
71 INC_STATS("DOM.Storage.IndexedPropertyGetter");
72 v8::Local<v8::Integer> indexV8 = v8::Integer::New(index);
73 return storageGetter(indexV8->ToString(), info);
76 v8::Handle<v8::Value> V8Storage::namedPropertyGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
78 INC_STATS("DOM.Storage.NamedPropertyGetter");
79 return storageGetter(name, info);
82 v8::Handle<v8::Integer> V8Storage::namedPropertyQuery(v8::Local<v8::String> v8Name, const v8::AccessorInfo& info)
84 INC_STATS("DOM.Storage.NamedPropertyQuery");
86 Storage* storage = V8Storage::toNative(info.Holder());
87 String name = toWebCoreString(v8Name);
89 if (storage->contains(name) && name != "length")
90 return v8::Integer::New(v8::None);
92 return v8::Handle<v8::Integer>();
95 static v8::Handle<v8::Value> storageSetter(v8::Local<v8::String> v8Name, v8::Local<v8::Value> v8Value, const v8::AccessorInfo& info)
97 Storage* storage = V8Storage::toNative(info.Holder());
98 String name = toWebCoreString(v8Name);
99 String value = toWebCoreString(v8Value);
101 // Silently ignore length (rather than letting the bindings raise an exception).
102 if (name == "length")
105 if (!info.Holder()->GetRealNamedPropertyInPrototypeChain(v8Name).IsEmpty())
106 return notHandledByInterceptor();
108 ExceptionCode ec = 0;
109 storage->setItem(name, value, ec);
111 return throwError(ec);
116 v8::Handle<v8::Value> V8Storage::indexedPropertySetter(uint32_t index, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
118 INC_STATS("DOM.Storage.NamedPropertyGetter");
119 v8::Local<v8::Integer> indexV8 = v8::Integer::New(index);
120 return storageSetter(indexV8->ToString(), value, info);
123 v8::Handle<v8::Value> V8Storage::namedPropertySetter(v8::Local<v8::String> name, v8::Local<v8::Value> value, const v8::AccessorInfo& info)
125 INC_STATS("DOM.Storage.NamedPropertySetter");
126 return storageSetter(name, value, info);
129 static v8::Handle<v8::Boolean> storageDeleter(v8::Local<v8::String> v8Name, const v8::AccessorInfo& info)
131 Storage* storage = V8Storage::toNative(info.Holder());
132 String name = toWebCoreString(v8Name);
134 if (storage->contains(name)) {
135 storage->removeItem(name);
139 return deletionNotHandledByInterceptor();
142 v8::Handle<v8::Boolean> V8Storage::indexedPropertyDeleter(uint32_t index, const v8::AccessorInfo& info)
144 INC_STATS("DOM.Storage.IndexedPropertyDeleter");
145 v8::Local<v8::Integer> indexV8 = v8::Integer::New(index);
146 return storageDeleter(indexV8->ToString(), info);
149 v8::Handle<v8::Boolean> V8Storage::namedPropertyDeleter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
151 INC_STATS("DOM.Storage.NamedPropertyDeleter");
152 return storageDeleter(name, info);
155 } // namespace WebCore