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