|
1 /* |
|
2 * Copyright (C) 2009 Google Inc. All Rights Reserved. |
|
3 * (C) 2008 Apple Inc. |
|
4 * |
|
5 * Redistribution and use in source and binary forms, with or without |
|
6 * modification, are permitted provided that the following conditions |
|
7 * are met: |
|
8 * 1. Redistributions of source code must retain the above copyright |
|
9 * notice, this list of conditions and the following disclaimer. |
|
10 * 2. Redistributions in binary form must reproduce the above copyright |
|
11 * notice, this list of conditions and the following disclaimer in the |
|
12 * documentation and/or other materials provided with the distribution. |
|
13 * |
|
14 * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. ``AS IS'' AND ANY |
|
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC. OR |
|
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
25 */ |
|
26 |
|
27 #include "config.h" |
|
28 #include "StorageAreaProxy.h" |
|
29 |
|
30 #if ENABLE(DOM_STORAGE) |
|
31 |
|
32 #include "DOMWindow.h" |
|
33 #include "Document.h" |
|
34 #include "EventNames.h" |
|
35 #include "ExceptionCode.h" |
|
36 #include "Frame.h" |
|
37 #include "Page.h" |
|
38 #include "PageGroup.h" |
|
39 #include "SecurityOrigin.h" |
|
40 #include "StorageAreaImpl.h" |
|
41 #include "StorageEvent.h" |
|
42 |
|
43 #include "WebFrameImpl.h" |
|
44 #include "WebStorageArea.h" |
|
45 #include "WebString.h" |
|
46 #include "WebURL.h" |
|
47 |
|
48 namespace WebCore { |
|
49 |
|
50 StorageAreaProxy::StorageAreaProxy(WebKit::WebStorageArea* storageArea, StorageType storageType) |
|
51 : m_storageArea(storageArea) |
|
52 , m_storageType(storageType) |
|
53 { |
|
54 } |
|
55 |
|
56 StorageAreaProxy::~StorageAreaProxy() |
|
57 { |
|
58 } |
|
59 |
|
60 unsigned StorageAreaProxy::length() const |
|
61 { |
|
62 return m_storageArea->length(); |
|
63 } |
|
64 |
|
65 String StorageAreaProxy::key(unsigned index) const |
|
66 { |
|
67 return m_storageArea->key(index); |
|
68 } |
|
69 |
|
70 String StorageAreaProxy::getItem(const String& key) const |
|
71 { |
|
72 return m_storageArea->getItem(key); |
|
73 } |
|
74 |
|
75 String StorageAreaProxy::setItem(const String& key, const String& value, ExceptionCode& ec, Frame* frame) |
|
76 { |
|
77 WebKit::WebStorageArea::Result result = WebKit::WebStorageArea::ResultOK; |
|
78 WebKit::WebString oldValue; |
|
79 WebKit::WebFrame* webFrame = WebKit::WebFrameImpl::fromFrame(frame); |
|
80 m_storageArea->setItem(key, value, frame->document()->url(), result, oldValue, webFrame); |
|
81 ec = (result == WebKit::WebStorageArea::ResultOK) ? 0 : QUOTA_EXCEEDED_ERR; |
|
82 String oldValueString = oldValue; |
|
83 if (oldValueString != value && result == WebKit::WebStorageArea::ResultOK) |
|
84 storageEvent(key, oldValue, value, m_storageType, frame->document()->securityOrigin(), frame); |
|
85 return oldValue; |
|
86 } |
|
87 |
|
88 String StorageAreaProxy::removeItem(const String& key, Frame* frame) |
|
89 { |
|
90 WebKit::WebString oldValue; |
|
91 m_storageArea->removeItem(key, frame->document()->url(), oldValue); |
|
92 if (!oldValue.isNull()) |
|
93 storageEvent(key, oldValue, String(), m_storageType, frame->document()->securityOrigin(), frame); |
|
94 return oldValue; |
|
95 } |
|
96 |
|
97 bool StorageAreaProxy::clear(Frame* frame) |
|
98 { |
|
99 bool clearedSomething; |
|
100 m_storageArea->clear(frame->document()->url(), clearedSomething); |
|
101 if (clearedSomething) |
|
102 storageEvent(String(), String(), String(), m_storageType, frame->document()->securityOrigin(), frame); |
|
103 return clearedSomething; |
|
104 } |
|
105 |
|
106 bool StorageAreaProxy::contains(const String& key) const |
|
107 { |
|
108 return !getItem(key).isNull(); |
|
109 } |
|
110 |
|
111 // Copied from WebCore/storage/StorageEventDispatcher.cpp out of necessity. It's probably best to keep it current. |
|
112 void StorageAreaProxy::storageEvent(const String& key, const String& oldValue, const String& newValue, StorageType storageType, SecurityOrigin* securityOrigin, Frame* sourceFrame) |
|
113 { |
|
114 Page* page = sourceFrame->page(); |
|
115 if (!page) |
|
116 return; |
|
117 |
|
118 // We need to copy all relevant frames from every page to a vector since sending the event to one frame might mutate the frame tree |
|
119 // of any given page in the group or mutate the page group itself. |
|
120 Vector<RefPtr<Frame> > frames; |
|
121 if (storageType == SessionStorage) { |
|
122 // Send events only to our page. |
|
123 for (Frame* frame = page->mainFrame(); frame; frame = frame->tree()->traverseNext()) { |
|
124 if (sourceFrame != frame && frame->document()->securityOrigin()->equal(securityOrigin)) |
|
125 frames.append(frame); |
|
126 } |
|
127 |
|
128 for (unsigned i = 0; i < frames.size(); ++i) { |
|
129 ExceptionCode ec = 0; |
|
130 Storage* storage = frames[i]->domWindow()->sessionStorage(ec); |
|
131 if (!ec) |
|
132 frames[i]->document()->enqueueEvent(StorageEvent::create(eventNames().storageEvent, key, oldValue, newValue, sourceFrame->document()->url(), storage)); |
|
133 } |
|
134 } else { |
|
135 // Send events to every page. |
|
136 const HashSet<Page*>& pages = page->group().pages(); |
|
137 HashSet<Page*>::const_iterator end = pages.end(); |
|
138 for (HashSet<Page*>::const_iterator it = pages.begin(); it != end; ++it) { |
|
139 for (Frame* frame = (*it)->mainFrame(); frame; frame = frame->tree()->traverseNext()) { |
|
140 if (sourceFrame != frame && frame->document()->securityOrigin()->equal(securityOrigin)) |
|
141 frames.append(frame); |
|
142 } |
|
143 } |
|
144 |
|
145 for (unsigned i = 0; i < frames.size(); ++i) { |
|
146 ExceptionCode ec = 0; |
|
147 Storage* storage = frames[i]->domWindow()->localStorage(ec); |
|
148 if (!ec) |
|
149 frames[i]->document()->enqueueEvent(StorageEvent::create(eventNames().storageEvent, key, oldValue, newValue, sourceFrame->document()->url(), storage)); |
|
150 } |
|
151 } |
|
152 } |
|
153 |
|
154 } // namespace WebCore |
|
155 |
|
156 #endif // ENABLE(DOM_STORAGE) |