1 /*
2  * Copyright (C) 2008 Apple 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
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "config.h"
27 #include "JSStorageCustom.h"
28 
29 #if ENABLE(DOM_STORAGE)
30 
31 #include "PlatformString.h"
32 #include <runtime/PropertyNameArray.h>
33 #include "Storage.h"
34 
35 using namespace JSC;
36 
37 namespace WebCore {
38 
canGetItemsForName(ExecState *,Storage * impl,const Identifier & propertyName)39 bool JSStorage::canGetItemsForName(ExecState*, Storage* impl, const Identifier& propertyName)
40 {
41     return impl->contains(identifierToString(propertyName));
42 }
43 
nameGetter(ExecState * exec,JSValue slotBase,const Identifier & propertyName)44 JSValue JSStorage::nameGetter(ExecState* exec, JSValue slotBase, const Identifier& propertyName)
45 {
46     JSStorage* thisObj = static_cast<JSStorage*>(asObject(slotBase));
47     return jsStringOrNull(exec, thisObj->impl()->getItem(identifierToString(propertyName)));
48 }
49 
deleteProperty(ExecState * exec,const Identifier & propertyName)50 bool JSStorage::deleteProperty(ExecState* exec, const Identifier& propertyName)
51 {
52     // Only perform the custom delete if the object doesn't have a native property by this name.
53     // Since hasProperty() would end up calling canGetItemsForName() and be fooled, we need to check
54     // the native property slots manually.
55     PropertySlot slot;
56     if (getStaticValueSlot<JSStorage, Base>(exec, s_info.propHashTable(exec), this, propertyName, slot))
57         return false;
58 
59     JSValue prototype = this->prototype();
60     if (prototype.isObject() && asObject(prototype)->hasProperty(exec, propertyName))
61         return false;
62 
63     m_impl->removeItem(identifierToString(propertyName));
64     return true;
65 }
66 
getOwnPropertyNames(ExecState * exec,PropertyNameArray & propertyNames,EnumerationMode mode)67 void JSStorage::getOwnPropertyNames(ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode)
68 {
69     unsigned length = m_impl->length();
70     for (unsigned i = 0; i < length; ++i)
71         propertyNames.add(Identifier(exec, stringToUString(m_impl->key(i))));
72 
73     Base::getOwnPropertyNames(exec, propertyNames, mode);
74 }
75 
putDelegate(ExecState * exec,const Identifier & propertyName,JSValue value,PutPropertySlot &)76 bool JSStorage::putDelegate(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot&)
77 {
78     // Only perform the custom put if the object doesn't have a native property by this name.
79     // Since hasProperty() would end up calling canGetItemsForName() and be fooled, we need to check
80     // the native property slots manually.
81     PropertySlot slot;
82     if (getStaticValueSlot<JSStorage, Base>(exec, s_info.propHashTable(exec), this, propertyName, slot))
83         return false;
84 
85     JSValue prototype = this->prototype();
86     if (prototype.isObject() && asObject(prototype)->hasProperty(exec, propertyName))
87         return false;
88 
89     String stringValue = ustringToString(value.toString(exec));
90     if (exec->hadException())
91         return true;
92 
93     ExceptionCode ec = 0;
94     impl()->setItem(identifierToString(propertyName), stringValue, ec);
95     setDOMException(exec, ec);
96 
97     return true;
98 }
99 
100 } // namespace WebCore
101 
102 #endif // ENABLE(DOM_STORAGE)
103