| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "webkit/support/simple_dom_storage_system.h" | |
| 6 | |
| 7 #include "base/auto_reset.h" | |
| 8 #include "googleurl/src/gurl.h" | |
| 9 #include "third_party/WebKit/public/platform/WebStorageArea.h" | |
| 10 #include "third_party/WebKit/public/platform/WebStorageNamespace.h" | |
| 11 #include "third_party/WebKit/public/platform/WebURL.h" | |
| 12 #include "third_party/WebKit/public/web/WebStorageEventDispatcher.h" | |
| 13 #include "webkit/browser/database/database_util.h" | |
| 14 #include "webkit/browser/dom_storage/dom_storage_area.h" | |
| 15 #include "webkit/browser/dom_storage/dom_storage_host.h" | |
| 16 | |
| 17 using dom_storage::DomStorageContext; | |
| 18 using dom_storage::DomStorageHost; | |
| 19 using dom_storage::DomStorageSession; | |
| 20 using webkit_database::DatabaseUtil; | |
| 21 using WebKit::WebStorageArea; | |
| 22 using WebKit::WebStorageNamespace; | |
| 23 using WebKit::WebStorageEventDispatcher; | |
| 24 using WebKit::WebString; | |
| 25 using WebKit::WebURL; | |
| 26 | |
| 27 namespace { | |
| 28 const int kInvalidNamespaceId = -1; | |
| 29 } | |
| 30 | |
| 31 class SimpleDomStorageSystem::NamespaceImpl : public WebStorageNamespace { | |
| 32 public: | |
| 33 explicit NamespaceImpl(const base::WeakPtr<SimpleDomStorageSystem>& parent); | |
| 34 NamespaceImpl(const base::WeakPtr<SimpleDomStorageSystem>& parent, | |
| 35 int session_namespace_id); | |
| 36 virtual ~NamespaceImpl(); | |
| 37 virtual WebStorageArea* createStorageArea(const WebString& origin) OVERRIDE; | |
| 38 virtual WebStorageNamespace* copy() OVERRIDE; | |
| 39 virtual bool isSameNamespace(const WebStorageNamespace&) const OVERRIDE; | |
| 40 | |
| 41 private: | |
| 42 DomStorageContext* Context() { | |
| 43 if (!parent_.get()) | |
| 44 return NULL; | |
| 45 return parent_->context_.get(); | |
| 46 } | |
| 47 | |
| 48 base::WeakPtr<SimpleDomStorageSystem> parent_; | |
| 49 int namespace_id_; | |
| 50 }; | |
| 51 | |
| 52 class SimpleDomStorageSystem::AreaImpl : public WebStorageArea { | |
| 53 public: | |
| 54 AreaImpl(const base::WeakPtr<SimpleDomStorageSystem>& parent, | |
| 55 int namespace_id, const GURL& origin); | |
| 56 virtual ~AreaImpl(); | |
| 57 virtual unsigned length() OVERRIDE; | |
| 58 virtual WebString key(unsigned index) OVERRIDE; | |
| 59 virtual WebString getItem(const WebString& key) OVERRIDE; | |
| 60 virtual void setItem(const WebString& key, const WebString& newValue, | |
| 61 const WebURL& pageUrl, Result&) OVERRIDE; | |
| 62 virtual void removeItem(const WebString& key, | |
| 63 const WebURL& pageUrl) OVERRIDE; | |
| 64 virtual void clear(const WebURL& pageUrl) OVERRIDE; | |
| 65 | |
| 66 private: | |
| 67 DomStorageHost* Host() { | |
| 68 if (!parent_.get()) | |
| 69 return NULL; | |
| 70 return parent_->host_.get(); | |
| 71 } | |
| 72 | |
| 73 base::WeakPtr<SimpleDomStorageSystem> parent_; | |
| 74 int connection_id_; | |
| 75 }; | |
| 76 | |
| 77 // NamespaceImpl ----------------------------- | |
| 78 | |
| 79 SimpleDomStorageSystem::NamespaceImpl::NamespaceImpl( | |
| 80 const base::WeakPtr<SimpleDomStorageSystem>& parent) | |
| 81 : parent_(parent), | |
| 82 namespace_id_(dom_storage::kLocalStorageNamespaceId) { | |
| 83 } | |
| 84 | |
| 85 SimpleDomStorageSystem::NamespaceImpl::NamespaceImpl( | |
| 86 const base::WeakPtr<SimpleDomStorageSystem>& parent, | |
| 87 int session_namespace_id) | |
| 88 : parent_(parent), | |
| 89 namespace_id_(session_namespace_id) { | |
| 90 } | |
| 91 | |
| 92 SimpleDomStorageSystem::NamespaceImpl::~NamespaceImpl() { | |
| 93 if (namespace_id_ == dom_storage::kLocalStorageNamespaceId || | |
| 94 namespace_id_ == kInvalidNamespaceId || !Context()) { | |
| 95 return; | |
| 96 } | |
| 97 Context()->DeleteSessionNamespace(namespace_id_, false); | |
| 98 } | |
| 99 | |
| 100 WebStorageArea* SimpleDomStorageSystem::NamespaceImpl::createStorageArea( | |
| 101 const WebString& origin) { | |
| 102 return new AreaImpl(parent_, namespace_id_, GURL(origin)); | |
| 103 } | |
| 104 | |
| 105 WebStorageNamespace* SimpleDomStorageSystem::NamespaceImpl::copy() { | |
| 106 DCHECK_NE(dom_storage::kLocalStorageNamespaceId, namespace_id_); | |
| 107 int new_id = kInvalidNamespaceId; | |
| 108 if (Context()) { | |
| 109 new_id = Context()->AllocateSessionId(); | |
| 110 Context()->CloneSessionNamespace(namespace_id_, new_id, std::string()); | |
| 111 } | |
| 112 return new NamespaceImpl(parent_, new_id); | |
| 113 } | |
| 114 | |
| 115 bool SimpleDomStorageSystem::NamespaceImpl::isSameNamespace( | |
| 116 const WebStorageNamespace& other) const { | |
| 117 const NamespaceImpl* other_impl = static_cast<const NamespaceImpl*>(&other); | |
| 118 return namespace_id_ == other_impl->namespace_id_; | |
| 119 } | |
| 120 | |
| 121 // AreaImpl ----------------------------- | |
| 122 | |
| 123 SimpleDomStorageSystem::AreaImpl::AreaImpl( | |
| 124 const base::WeakPtr<SimpleDomStorageSystem>& parent, | |
| 125 int namespace_id, const GURL& origin) | |
| 126 : parent_(parent), | |
| 127 connection_id_(0) { | |
| 128 if (Host()) { | |
| 129 connection_id_ = (parent_->next_connection_id_)++; | |
| 130 Host()->OpenStorageArea(connection_id_, namespace_id, origin); | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 SimpleDomStorageSystem::AreaImpl::~AreaImpl() { | |
| 135 if (Host()) | |
| 136 Host()->CloseStorageArea(connection_id_); | |
| 137 } | |
| 138 | |
| 139 unsigned SimpleDomStorageSystem::AreaImpl::length() { | |
| 140 if (Host()) | |
| 141 return Host()->GetAreaLength(connection_id_); | |
| 142 return 0; | |
| 143 } | |
| 144 | |
| 145 WebString SimpleDomStorageSystem::AreaImpl::key(unsigned index) { | |
| 146 if (Host()) | |
| 147 return Host()->GetAreaKey(connection_id_, index); | |
| 148 return base::NullableString16(); | |
| 149 } | |
| 150 | |
| 151 WebString SimpleDomStorageSystem::AreaImpl::getItem(const WebString& key) { | |
| 152 if (Host()) | |
| 153 return Host()->GetAreaItem(connection_id_, key); | |
| 154 return base::NullableString16(); | |
| 155 } | |
| 156 | |
| 157 void SimpleDomStorageSystem::AreaImpl::setItem( | |
| 158 const WebString& key, const WebString& newValue, | |
| 159 const WebURL& pageUrl, Result& result) { | |
| 160 result = ResultBlockedByQuota; | |
| 161 if (!Host()) | |
| 162 return; | |
| 163 | |
| 164 base::AutoReset<AreaImpl*> auto_reset(&parent_->area_being_processed_, this); | |
| 165 base::NullableString16 unused; | |
| 166 if (!Host()->SetAreaItem(connection_id_, key, newValue, pageUrl, | |
| 167 &unused)) | |
| 168 return; | |
| 169 | |
| 170 result = ResultOK; | |
| 171 } | |
| 172 | |
| 173 void SimpleDomStorageSystem::AreaImpl::removeItem( | |
| 174 const WebString& key, const WebURL& pageUrl) { | |
| 175 if (!Host()) | |
| 176 return; | |
| 177 | |
| 178 base::AutoReset<AreaImpl*> auto_reset(&parent_->area_being_processed_, this); | |
| 179 base::string16 notused; | |
| 180 Host()->RemoveAreaItem(connection_id_, key, pageUrl, ¬used); | |
| 181 } | |
| 182 | |
| 183 void SimpleDomStorageSystem::AreaImpl::clear(const WebURL& pageUrl) { | |
| 184 if (!Host()) | |
| 185 return; | |
| 186 | |
| 187 base::AutoReset<AreaImpl*> auto_reset(&parent_->area_being_processed_, this); | |
| 188 Host()->ClearArea(connection_id_, pageUrl); | |
| 189 } | |
| 190 | |
| 191 // SimpleDomStorageSystem ----------------------------- | |
| 192 | |
| 193 SimpleDomStorageSystem* SimpleDomStorageSystem::g_instance_; | |
| 194 | |
| 195 SimpleDomStorageSystem::SimpleDomStorageSystem() | |
| 196 : weak_factory_(this), | |
| 197 context_(new DomStorageContext(base::FilePath(), | |
| 198 base::FilePath(), | |
| 199 NULL, | |
| 200 NULL)), | |
| 201 host_(new DomStorageHost(context_.get())), | |
| 202 area_being_processed_(NULL), | |
| 203 next_connection_id_(1) { | |
| 204 DCHECK(!g_instance_); | |
| 205 g_instance_ = this; | |
| 206 context_->AddEventObserver(this); | |
| 207 } | |
| 208 | |
| 209 SimpleDomStorageSystem::~SimpleDomStorageSystem() { | |
| 210 g_instance_ = NULL; | |
| 211 host_.reset(); | |
| 212 context_->RemoveEventObserver(this); | |
| 213 } | |
| 214 | |
| 215 WebStorageNamespace* SimpleDomStorageSystem::CreateLocalStorageNamespace() { | |
| 216 return new NamespaceImpl(weak_factory_.GetWeakPtr()); | |
| 217 } | |
| 218 | |
| 219 WebStorageNamespace* SimpleDomStorageSystem::CreateSessionStorageNamespace() { | |
| 220 int id = context_->AllocateSessionId(); | |
| 221 context_->CreateSessionNamespace(id, std::string()); | |
| 222 return new NamespaceImpl(weak_factory_.GetWeakPtr(), id); | |
| 223 } | |
| 224 | |
| 225 void SimpleDomStorageSystem::OnDomStorageItemSet( | |
| 226 const dom_storage::DomStorageArea* area, | |
| 227 const base::string16& key, | |
| 228 const base::string16& new_value, | |
| 229 const base::NullableString16& old_value, | |
| 230 const GURL& page_url) { | |
| 231 DispatchDomStorageEvent(area, page_url, | |
| 232 base::NullableString16(key, false), | |
| 233 base::NullableString16(new_value, false), | |
| 234 old_value); | |
| 235 } | |
| 236 | |
| 237 void SimpleDomStorageSystem::OnDomStorageItemRemoved( | |
| 238 const dom_storage::DomStorageArea* area, | |
| 239 const base::string16& key, | |
| 240 const base::string16& old_value, | |
| 241 const GURL& page_url) { | |
| 242 DispatchDomStorageEvent(area, page_url, | |
| 243 base::NullableString16(key, false), | |
| 244 base::NullableString16(), | |
| 245 base::NullableString16(old_value, false)); | |
| 246 } | |
| 247 | |
| 248 void SimpleDomStorageSystem::OnDomStorageAreaCleared( | |
| 249 const dom_storage::DomStorageArea* area, | |
| 250 const GURL& page_url) { | |
| 251 DispatchDomStorageEvent(area, page_url, | |
| 252 base::NullableString16(), | |
| 253 base::NullableString16(), | |
| 254 base::NullableString16()); | |
| 255 } | |
| 256 | |
| 257 void SimpleDomStorageSystem::DispatchDomStorageEvent( | |
| 258 const dom_storage::DomStorageArea* area, | |
| 259 const GURL& page_url, | |
| 260 const base::NullableString16& key, | |
| 261 const base::NullableString16& new_value, | |
| 262 const base::NullableString16& old_value) { | |
| 263 DCHECK(area_being_processed_); | |
| 264 if (area->namespace_id() == dom_storage::kLocalStorageNamespaceId) { | |
| 265 WebStorageEventDispatcher::dispatchLocalStorageEvent( | |
| 266 key, | |
| 267 old_value, | |
| 268 new_value, | |
| 269 area->origin(), | |
| 270 page_url, | |
| 271 area_being_processed_, | |
| 272 true /* originatedInProcess */); | |
| 273 } else { | |
| 274 NamespaceImpl session_namespace_for_event_dispatch( | |
| 275 base::WeakPtr<SimpleDomStorageSystem>(), area->namespace_id()); | |
| 276 WebStorageEventDispatcher::dispatchSessionStorageEvent( | |
| 277 key, | |
| 278 old_value, | |
| 279 new_value, | |
| 280 area->origin(), | |
| 281 page_url, | |
| 282 session_namespace_for_event_dispatch, | |
| 283 area_being_processed_, | |
| 284 true /* originatedInProcess */); | |
| 285 } | |
| 286 } | |
| OLD | NEW |