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 "core/storage/Storage.h" 28 29 #include "bindings/core/v8/ExceptionState.h" 30 #include "wtf/PassOwnPtr.h" 31 #include "wtf/PassRefPtr.h" 32 #include "wtf/text/WTFString.h" 33 34 namespace blink { 35 36 PassRefPtrWillBeRawPtr<Storage> Storage::create(LocalFrame* frame, PassOwnPtrWillBeRawPtr<StorageArea> storageArea) 37 { 38 return adoptRefWillBeNoop(new Storage(frame, storageArea)); 39 } 40 41 Storage::Storage(LocalFrame* frame, PassOwnPtrWillBeRawPtr<StorageArea> storageArea) 42 : DOMWindowProperty(frame) 43 , m_storageArea(storageArea) 44 { 45 ASSERT(m_frame); 46 ASSERT(m_storageArea); 47 } 48 49 DEFINE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(Storage); 50 51 String Storage::anonymousIndexedGetter(unsigned index, ExceptionState& exceptionState) 52 { 53 return anonymousNamedGetter(AtomicString::number(index), exceptionState); 54 } 55 56 String Storage::anonymousNamedGetter(const AtomicString& name, ExceptionState& exceptionState) 57 { 58 bool found = contains(name, exceptionState); 59 if (exceptionState.hadException() || !found) 60 return String(); 61 String result = getItem(name, exceptionState); 62 if (exceptionState.hadException()) 63 return String(); 64 return result; 65 } 66 67 bool Storage::anonymousNamedSetter(const AtomicString& name, const AtomicString& value, ExceptionState& exceptionState) 68 { 69 setItem(name, value, exceptionState); 70 return true; 71 } 72 73 bool Storage::anonymousIndexedSetter(unsigned index, const AtomicString& value, ExceptionState& exceptionState) 74 { 75 return anonymousNamedSetter(AtomicString::number(index), value, exceptionState); 76 } 77 78 DeleteResult Storage::anonymousNamedDeleter(const AtomicString& name, ExceptionState& exceptionState) 79 { 80 bool found = contains(name, exceptionState); 81 if (!found) 82 return DeleteUnknownProperty; 83 if (exceptionState.hadException()) 84 return DeleteReject; 85 removeItem(name, exceptionState); 86 if (exceptionState.hadException()) 87 return DeleteReject; 88 return DeleteSuccess; 89 } 90 91 DeleteResult Storage::anonymousIndexedDeleter(unsigned index, ExceptionState& exceptionState) 92 { 93 DeleteResult result = anonymousNamedDeleter(AtomicString::number(index), exceptionState); 94 return result == DeleteUnknownProperty ? DeleteSuccess : result; 95 } 96 97 void Storage::namedPropertyEnumerator(Vector<String>& names, ExceptionState& exceptionState) 98 { 99 unsigned length = this->length(exceptionState); 100 if (exceptionState.hadException()) 101 return; 102 names.resize(length); 103 for (unsigned i = 0; i < length; ++i) { 104 String key = this->key(i, exceptionState); 105 if (exceptionState.hadException()) 106 return; 107 ASSERT(!key.isNull()); 108 String val = getItem(key, exceptionState); 109 if (exceptionState.hadException()) 110 return; 111 names[i] = key; 112 } 113 } 114 115 bool Storage::namedPropertyQuery(const AtomicString& name, ExceptionState& exceptionState) 116 { 117 if (name == "length") 118 return false; 119 bool found = contains(name, exceptionState); 120 if (exceptionState.hadException() || !found) 121 return false; 122 return true; 123 } 124 125 void Storage::trace(Visitor* visitor) 126 { 127 visitor->trace(m_storageArea); 128 DOMWindowProperty::trace(visitor); 129 } 130 131 } 132