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 "Storage.h" |
28 | |
29 | #include "Document.h" |
30 | #include "Frame.h" |
31 | #include "Page.h" |
32 | #include "SchemeRegistry.h" |
33 | #include "SecurityOrigin.h" |
34 | #include "StorageArea.h" |
35 | #include "StorageType.h" |
36 | #include <wtf/IsoMallocInlines.h> |
37 | #include <wtf/text/WTFString.h> |
38 | |
39 | namespace WebCore { |
40 | |
41 | WTF_MAKE_ISO_ALLOCATED_IMPL(Storage); |
42 | |
43 | Ref<Storage> Storage::create(DOMWindow& window, Ref<StorageArea>&& storageArea) |
44 | { |
45 | return adoptRef(*new Storage(window, WTFMove(storageArea))); |
46 | } |
47 | |
48 | Storage::Storage(DOMWindow& window, Ref<StorageArea>&& storageArea) |
49 | : DOMWindowProperty(&window) |
50 | , m_storageArea(WTFMove(storageArea)) |
51 | { |
52 | ASSERT(frame()); |
53 | |
54 | m_storageArea->incrementAccessCount(); |
55 | } |
56 | |
57 | Storage::~Storage() |
58 | { |
59 | m_storageArea->decrementAccessCount(); |
60 | } |
61 | |
62 | unsigned Storage::length() const |
63 | { |
64 | return m_storageArea->length(); |
65 | } |
66 | |
67 | String Storage::key(unsigned index) const |
68 | { |
69 | return m_storageArea->key(index); |
70 | } |
71 | |
72 | String Storage::getItem(const String& key) const |
73 | { |
74 | return m_storageArea->item(key); |
75 | } |
76 | |
77 | ExceptionOr<void> Storage::setItem(const String& key, const String& value) |
78 | { |
79 | auto* frame = this->frame(); |
80 | if (!frame) |
81 | return Exception { InvalidAccessError }; |
82 | |
83 | bool quotaException = false; |
84 | m_storageArea->setItem(frame, key, value, quotaException); |
85 | if (quotaException) |
86 | return Exception { QuotaExceededError }; |
87 | return { }; |
88 | } |
89 | |
90 | ExceptionOr<void> Storage::removeItem(const String& key) |
91 | { |
92 | auto* frame = this->frame(); |
93 | if (!frame) |
94 | return Exception { InvalidAccessError }; |
95 | |
96 | m_storageArea->removeItem(frame, key); |
97 | return { }; |
98 | } |
99 | |
100 | ExceptionOr<void> Storage::clear() |
101 | { |
102 | auto* frame = this->frame(); |
103 | if (!frame) |
104 | return Exception { InvalidAccessError }; |
105 | |
106 | m_storageArea->clear(frame); |
107 | return { }; |
108 | } |
109 | |
110 | bool Storage::contains(const String& key) const |
111 | { |
112 | return m_storageArea->contains(key); |
113 | } |
114 | |
115 | bool Storage::isSupportedPropertyName(const String& propertyName) const |
116 | { |
117 | return m_storageArea->contains(propertyName); |
118 | } |
119 | |
120 | Vector<AtomicString> Storage::supportedPropertyNames() const |
121 | { |
122 | unsigned length = m_storageArea->length(); |
123 | |
124 | Vector<AtomicString> result; |
125 | result.reserveInitialCapacity(length); |
126 | |
127 | for (unsigned i = 0; i < length; ++i) |
128 | result.uncheckedAppend(m_storageArea->key(i)); |
129 | |
130 | return result; |
131 | } |
132 | |
133 | } // namespace WebCore |
134 | |