OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "webkit/tools/test_shell/simple_dom_storage_system.h" | 5 #include "webkit/tools/test_shell/simple_dom_storage_system.h" |
6 | 6 |
| 7 #include "base/auto_reset.h" |
7 #include "googleurl/src/gurl.h" | 8 #include "googleurl/src/gurl.h" |
8 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h" | 9 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h" |
9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebStorageArea.h" | 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebStorageArea.h" |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebStorageEventDispat
cher.h" |
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebStorageNamespace.h
" | 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebStorageNamespace.h
" |
11 #include "webkit/database/database_util.h" | 13 #include "webkit/database/database_util.h" |
12 #include "webkit/dom_storage/dom_storage_context.h" | 14 #include "webkit/dom_storage/dom_storage_area.h" |
13 #include "webkit/dom_storage/dom_storage_host.h" | 15 #include "webkit/dom_storage/dom_storage_host.h" |
14 #include "webkit/dom_storage/dom_storage_session.h" | |
15 | 16 |
16 using dom_storage::DomStorageContext; | 17 using dom_storage::DomStorageContext; |
17 using dom_storage::DomStorageHost; | 18 using dom_storage::DomStorageHost; |
18 using dom_storage::DomStorageSession; | 19 using dom_storage::DomStorageSession; |
19 using webkit_database::DatabaseUtil; | 20 using webkit_database::DatabaseUtil; |
| 21 using WebKit::WebStorageArea; |
20 using WebKit::WebStorageNamespace; | 22 using WebKit::WebStorageNamespace; |
21 using WebKit::WebStorageArea; | 23 using WebKit::WebStorageEventDispatcher; |
22 using WebKit::WebString; | 24 using WebKit::WebString; |
23 using WebKit::WebURL; | 25 using WebKit::WebURL; |
24 | 26 |
25 namespace { | 27 namespace { |
26 const int kInvalidNamespaceId = -1; | 28 const int kInvalidNamespaceId = -1; |
27 } | 29 } |
28 | 30 |
29 class SimpleDomStorageSystem::NamespaceImpl : public WebStorageNamespace { | 31 class SimpleDomStorageSystem::NamespaceImpl : public WebStorageNamespace { |
30 public: | 32 public: |
31 explicit NamespaceImpl(const base::WeakPtr<SimpleDomStorageSystem>& parent); | 33 explicit NamespaceImpl(const base::WeakPtr<SimpleDomStorageSystem>& parent); |
32 NamespaceImpl(const base::WeakPtr<SimpleDomStorageSystem>& parent, | 34 NamespaceImpl(const base::WeakPtr<SimpleDomStorageSystem>& parent, |
33 int session_namespace_id); | 35 int session_namespace_id); |
34 virtual ~NamespaceImpl(); | 36 virtual ~NamespaceImpl(); |
35 virtual WebStorageArea* createStorageArea(const WebString& origin) OVERRIDE; | 37 virtual WebStorageArea* createStorageArea(const WebString& origin) OVERRIDE; |
36 virtual WebStorageNamespace* copy() OVERRIDE; | 38 virtual WebStorageNamespace* copy() OVERRIDE; |
37 virtual void close() OVERRIDE; | 39 virtual bool isSameNamespace(const WebStorageNamespace&) const OVERRIDE; |
38 | 40 |
39 private: | 41 private: |
40 DomStorageContext* Context() { | 42 DomStorageContext* Context() { |
41 if (!parent_.get()) | 43 if (!parent_.get()) |
42 return NULL; | 44 return NULL; |
43 return parent_->context_.get(); | 45 return parent_->context_.get(); |
44 } | 46 } |
45 | 47 |
46 base::WeakPtr<SimpleDomStorageSystem> parent_; | 48 base::WeakPtr<SimpleDomStorageSystem> parent_; |
47 int namespace_id_; | 49 int namespace_id_; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 WebStorageNamespace* SimpleDomStorageSystem::NamespaceImpl::copy() { | 105 WebStorageNamespace* SimpleDomStorageSystem::NamespaceImpl::copy() { |
104 DCHECK_NE(dom_storage::kLocalStorageNamespaceId, namespace_id_); | 106 DCHECK_NE(dom_storage::kLocalStorageNamespaceId, namespace_id_); |
105 int new_id = kInvalidNamespaceId; | 107 int new_id = kInvalidNamespaceId; |
106 if (Context()) { | 108 if (Context()) { |
107 new_id = Context()->AllocateSessionId(); | 109 new_id = Context()->AllocateSessionId(); |
108 Context()->CloneSessionNamespace(namespace_id_, new_id); | 110 Context()->CloneSessionNamespace(namespace_id_, new_id); |
109 } | 111 } |
110 return new NamespaceImpl(parent_, new_id); | 112 return new NamespaceImpl(parent_, new_id); |
111 } | 113 } |
112 | 114 |
113 void SimpleDomStorageSystem::NamespaceImpl::close() { | 115 bool SimpleDomStorageSystem::NamespaceImpl::isSameNamespace( |
114 // TODO(michaeln): remove this deprecated method. | 116 const WebStorageNamespace& other) const { |
| 117 const NamespaceImpl* other_impl = static_cast<const NamespaceImpl*>(&other); |
| 118 return namespace_id_ == other_impl->namespace_id_; |
115 } | 119 } |
116 | 120 |
117 // AreaImpl ----------------------------- | 121 // AreaImpl ----------------------------- |
118 | 122 |
119 SimpleDomStorageSystem::AreaImpl::AreaImpl( | 123 SimpleDomStorageSystem::AreaImpl::AreaImpl( |
120 const base::WeakPtr<SimpleDomStorageSystem>& parent, | 124 const base::WeakPtr<SimpleDomStorageSystem>& parent, |
121 int namespace_id, const GURL& origin) | 125 int namespace_id, const GURL& origin) |
122 : parent_(parent), | 126 : parent_(parent), |
123 connection_id_(0) { | 127 connection_id_(0) { |
124 if (Host()) { | 128 if (Host()) { |
(...skipping 26 matching lines...) Expand all Loading... |
151 } | 155 } |
152 | 156 |
153 void SimpleDomStorageSystem::AreaImpl::setItem( | 157 void SimpleDomStorageSystem::AreaImpl::setItem( |
154 const WebString& key, const WebString& newValue, | 158 const WebString& key, const WebString& newValue, |
155 const WebURL& pageUrl, Result& result, WebString& oldValue) { | 159 const WebURL& pageUrl, Result& result, WebString& oldValue) { |
156 result = ResultBlockedByQuota; | 160 result = ResultBlockedByQuota; |
157 oldValue = NullableString16(true); | 161 oldValue = NullableString16(true); |
158 if (!Host()) | 162 if (!Host()) |
159 return; | 163 return; |
160 | 164 |
| 165 AutoReset<AreaImpl*> auto_reset(&parent_->area_being_processed_, this); |
161 NullableString16 old_value; | 166 NullableString16 old_value; |
162 if (!Host()->SetAreaItem(connection_id_, key, newValue, pageUrl, | 167 if (!Host()->SetAreaItem(connection_id_, key, newValue, pageUrl, |
163 &old_value)) | 168 &old_value)) |
164 return; | 169 return; |
165 | 170 |
166 result = ResultOK; | 171 result = ResultOK; |
167 oldValue = old_value; | 172 oldValue = old_value; |
168 } | 173 } |
169 | 174 |
170 void SimpleDomStorageSystem::AreaImpl::removeItem( | 175 void SimpleDomStorageSystem::AreaImpl::removeItem( |
171 const WebString& key, const WebURL& pageUrl, WebString& oldValue) { | 176 const WebString& key, const WebURL& pageUrl, WebString& oldValue) { |
172 oldValue = NullableString16(true); | 177 oldValue = NullableString16(true); |
173 if (!Host()) | 178 if (!Host()) |
174 return; | 179 return; |
175 | 180 |
| 181 AutoReset<AreaImpl*> auto_reset(&parent_->area_being_processed_, this); |
176 string16 old_value; | 182 string16 old_value; |
177 if (!Host()->RemoveAreaItem(connection_id_, key, pageUrl, &old_value)) | 183 if (!Host()->RemoveAreaItem(connection_id_, key, pageUrl, &old_value)) |
178 return; | 184 return; |
179 | 185 |
180 oldValue = old_value; | 186 oldValue = old_value; |
181 } | 187 } |
182 | 188 |
183 void SimpleDomStorageSystem::AreaImpl::clear( | 189 void SimpleDomStorageSystem::AreaImpl::clear( |
184 const WebURL& pageUrl, bool& somethingCleared) { | 190 const WebURL& pageUrl, bool& somethingCleared) { |
185 if (Host()) | 191 if (Host()) { |
| 192 AutoReset<AreaImpl*> auto_reset(&parent_->area_being_processed_, this); |
186 somethingCleared = Host()->ClearArea(connection_id_, pageUrl); | 193 somethingCleared = Host()->ClearArea(connection_id_, pageUrl); |
187 else | 194 return; |
188 somethingCleared = false; | 195 } |
| 196 somethingCleared = false; |
189 } | 197 } |
190 | 198 |
191 // SimpleDomStorageSystem ----------------------------- | 199 // SimpleDomStorageSystem ----------------------------- |
192 | 200 |
193 SimpleDomStorageSystem* SimpleDomStorageSystem::g_instance_; | 201 SimpleDomStorageSystem* SimpleDomStorageSystem::g_instance_; |
194 | 202 |
195 SimpleDomStorageSystem::SimpleDomStorageSystem() | 203 SimpleDomStorageSystem::SimpleDomStorageSystem() |
196 : weak_factory_(this), | 204 : weak_factory_(this), |
197 context_(new DomStorageContext(FilePath(), FilePath(), NULL, NULL)), | 205 context_(new DomStorageContext(FilePath(), FilePath(), NULL, NULL)), |
198 host_(new DomStorageHost(context_)), | 206 host_(new DomStorageHost(context_)), |
| 207 area_being_processed_(NULL), |
199 next_connection_id_(1) { | 208 next_connection_id_(1) { |
200 DCHECK(!g_instance_); | 209 DCHECK(!g_instance_); |
201 g_instance_ = this; | 210 g_instance_ = this; |
| 211 context_->AddEventObserver(this); |
202 } | 212 } |
203 | 213 |
204 SimpleDomStorageSystem::~SimpleDomStorageSystem() { | 214 SimpleDomStorageSystem::~SimpleDomStorageSystem() { |
205 g_instance_ = NULL; | 215 g_instance_ = NULL; |
206 host_.reset(); | 216 host_.reset(); |
| 217 context_->RemoveEventObserver(this); |
207 } | 218 } |
208 | 219 |
209 WebStorageNamespace* SimpleDomStorageSystem::CreateLocalStorageNamespace() { | 220 WebStorageNamespace* SimpleDomStorageSystem::CreateLocalStorageNamespace() { |
210 return new NamespaceImpl(weak_factory_.GetWeakPtr()); | 221 return new NamespaceImpl(weak_factory_.GetWeakPtr()); |
211 } | 222 } |
212 | 223 |
213 WebStorageNamespace* SimpleDomStorageSystem::CreateSessionStorageNamespace() { | 224 WebStorageNamespace* SimpleDomStorageSystem::CreateSessionStorageNamespace() { |
214 int id = context_->AllocateSessionId(); | 225 int id = context_->AllocateSessionId(); |
215 context_->CreateSessionNamespace(id); | 226 context_->CreateSessionNamespace(id); |
216 return new NamespaceImpl(weak_factory_.GetWeakPtr(), id); | 227 return new NamespaceImpl(weak_factory_.GetWeakPtr(), id); |
217 } | 228 } |
| 229 |
| 230 void SimpleDomStorageSystem::OnDomStorageItemSet( |
| 231 const dom_storage::DomStorageArea* area, |
| 232 const string16& key, |
| 233 const string16& new_value, |
| 234 const NullableString16& old_value, |
| 235 const GURL& page_url) { |
| 236 DispatchDomStorageEvent(area, page_url, |
| 237 NullableString16(key, false), |
| 238 NullableString16(new_value, false), |
| 239 old_value); |
| 240 } |
| 241 |
| 242 void SimpleDomStorageSystem::OnDomStorageItemRemoved( |
| 243 const dom_storage::DomStorageArea* area, |
| 244 const string16& key, |
| 245 const string16& old_value, |
| 246 const GURL& page_url) { |
| 247 DispatchDomStorageEvent(area, page_url, |
| 248 NullableString16(key, false), |
| 249 NullableString16(true), |
| 250 NullableString16(old_value, false)); |
| 251 } |
| 252 |
| 253 void SimpleDomStorageSystem::OnDomStorageAreaCleared( |
| 254 const dom_storage::DomStorageArea* area, |
| 255 const GURL& page_url) { |
| 256 DispatchDomStorageEvent(area, page_url, |
| 257 NullableString16(true), |
| 258 NullableString16(true), |
| 259 NullableString16(true)); |
| 260 } |
| 261 |
| 262 void SimpleDomStorageSystem::DispatchDomStorageEvent( |
| 263 const dom_storage::DomStorageArea* area, |
| 264 const GURL& page_url, |
| 265 const NullableString16& key, |
| 266 const NullableString16& new_value, |
| 267 const NullableString16& old_value) { |
| 268 DCHECK(area_being_processed_); |
| 269 if (area->namespace_id() == dom_storage::kLocalStorageNamespaceId) { |
| 270 WebStorageEventDispatcher::dispatchLocalStorageEvent( |
| 271 key, |
| 272 old_value, |
| 273 new_value, |
| 274 area->origin(), |
| 275 page_url, |
| 276 area_being_processed_, |
| 277 true /* originatedInProcess */); |
| 278 } else { |
| 279 NamespaceImpl session_namespace_for_event_dispatch( |
| 280 base::WeakPtr<SimpleDomStorageSystem>(), area->namespace_id()); |
| 281 WebStorageEventDispatcher::dispatchSessionStorageEvent( |
| 282 key, |
| 283 old_value, |
| 284 new_value, |
| 285 area->origin(), |
| 286 page_url, |
| 287 session_namespace_for_event_dispatch, |
| 288 area_being_processed_, |
| 289 true /* originatedInProcess */); |
| 290 } |
| 291 } |
OLD | NEW |