| 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 "content/browser/dom_storage/dom_storage_context_impl.h" | 5 #include "content/browser/dom_storage/dom_storage_context_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> |
| 7 #include <stdlib.h> | 8 #include <stdlib.h> |
| 8 | 9 |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 11 #include "base/files/file_enumerator.h" | 12 #include "base/files/file_enumerator.h" |
| 12 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 13 #include "base/guid.h" | 14 #include "base/guid.h" |
| 14 #include "base/location.h" | 15 #include "base/location.h" |
| 15 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 16 #include "content/browser/dom_storage/dom_storage_area.h" | 17 #include "content/browser/dom_storage/dom_storage_area.h" |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 session_storage_database_ = NULL; | 62 session_storage_database_ = NULL; |
| 62 task_runner_->PostShutdownBlockingTask( | 63 task_runner_->PostShutdownBlockingTask( |
| 63 FROM_HERE, | 64 FROM_HERE, |
| 64 DOMStorageTaskRunner::COMMIT_SEQUENCE, | 65 DOMStorageTaskRunner::COMMIT_SEQUENCE, |
| 65 base::Bind(&SessionStorageDatabase::Release, | 66 base::Bind(&SessionStorageDatabase::Release, |
| 66 base::Unretained(to_release))); | 67 base::Unretained(to_release))); |
| 67 } | 68 } |
| 68 } | 69 } |
| 69 | 70 |
| 70 DOMStorageNamespace* DOMStorageContextImpl::GetStorageNamespace( | 71 DOMStorageNamespace* DOMStorageContextImpl::GetStorageNamespace( |
| 71 int64 namespace_id) { | 72 int64_t namespace_id) { |
| 72 if (is_shutdown_) | 73 if (is_shutdown_) |
| 73 return NULL; | 74 return NULL; |
| 74 StorageNamespaceMap::iterator found = namespaces_.find(namespace_id); | 75 StorageNamespaceMap::iterator found = namespaces_.find(namespace_id); |
| 75 if (found == namespaces_.end()) { | 76 if (found == namespaces_.end()) { |
| 76 if (namespace_id == kLocalStorageNamespaceId) { | 77 if (namespace_id == kLocalStorageNamespaceId) { |
| 77 if (!localstorage_directory_.empty()) { | 78 if (!localstorage_directory_.empty()) { |
| 78 if (!base::CreateDirectory(localstorage_directory_)) { | 79 if (!base::CreateDirectory(localstorage_directory_)) { |
| 79 LOG(ERROR) << "Failed to create 'Local Storage' directory," | 80 LOG(ERROR) << "Failed to create 'Local Storage' directory," |
| 80 " falling back to in-memory only."; | 81 " falling back to in-memory only."; |
| 81 localstorage_directory_ = base::FilePath(); | 82 localstorage_directory_ = base::FilePath(); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 // renderers get emptied out too. | 142 // renderers get emptied out too. |
| 142 DOMStorageArea* area = local->GetOpenStorageArea(origin); | 143 DOMStorageArea* area = local->GetOpenStorageArea(origin); |
| 143 if (area) | 144 if (area) |
| 144 NotifyAreaCleared(area, origin); | 145 NotifyAreaCleared(area, origin); |
| 145 } | 146 } |
| 146 | 147 |
| 147 void DOMStorageContextImpl::DeleteSessionStorage( | 148 void DOMStorageContextImpl::DeleteSessionStorage( |
| 148 const SessionStorageUsageInfo& usage_info) { | 149 const SessionStorageUsageInfo& usage_info) { |
| 149 DCHECK(!is_shutdown_); | 150 DCHECK(!is_shutdown_); |
| 150 DOMStorageNamespace* dom_storage_namespace = NULL; | 151 DOMStorageNamespace* dom_storage_namespace = NULL; |
| 151 std::map<std::string, int64>::const_iterator it = | 152 std::map<std::string, int64_t>::const_iterator it = |
| 152 persistent_namespace_id_to_namespace_id_.find( | 153 persistent_namespace_id_to_namespace_id_.find( |
| 153 usage_info.persistent_namespace_id); | 154 usage_info.persistent_namespace_id); |
| 154 if (it != persistent_namespace_id_to_namespace_id_.end()) { | 155 if (it != persistent_namespace_id_to_namespace_id_.end()) { |
| 155 dom_storage_namespace = GetStorageNamespace(it->second); | 156 dom_storage_namespace = GetStorageNamespace(it->second); |
| 156 } else { | 157 } else { |
| 157 int64 namespace_id = AllocateSessionId(); | 158 int64_t namespace_id = AllocateSessionId(); |
| 158 CreateSessionNamespace(namespace_id, usage_info.persistent_namespace_id); | 159 CreateSessionNamespace(namespace_id, usage_info.persistent_namespace_id); |
| 159 dom_storage_namespace = GetStorageNamespace(namespace_id); | 160 dom_storage_namespace = GetStorageNamespace(namespace_id); |
| 160 } | 161 } |
| 161 dom_storage_namespace->DeleteSessionStorageOrigin(usage_info.origin); | 162 dom_storage_namespace->DeleteSessionStorageOrigin(usage_info.origin); |
| 162 // Synthesize a 'cleared' event if the area is open so CachedAreas in | 163 // Synthesize a 'cleared' event if the area is open so CachedAreas in |
| 163 // renderers get emptied out too. | 164 // renderers get emptied out too. |
| 164 DOMStorageArea* area = | 165 DOMStorageArea* area = |
| 165 dom_storage_namespace->GetOpenStorageArea(usage_info.origin); | 166 dom_storage_namespace->GetOpenStorageArea(usage_info.origin); |
| 166 if (area) | 167 if (area) |
| 167 NotifyAreaCleared(area, usage_info.origin); | 168 NotifyAreaCleared(area, usage_info.origin); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 } | 233 } |
| 233 | 234 |
| 234 void DOMStorageContextImpl::NotifyAreaCleared( | 235 void DOMStorageContextImpl::NotifyAreaCleared( |
| 235 const DOMStorageArea* area, | 236 const DOMStorageArea* area, |
| 236 const GURL& page_url) { | 237 const GURL& page_url) { |
| 237 FOR_EACH_OBSERVER( | 238 FOR_EACH_OBSERVER( |
| 238 EventObserver, event_observers_, | 239 EventObserver, event_observers_, |
| 239 OnDOMStorageAreaCleared(area, page_url)); | 240 OnDOMStorageAreaCleared(area, page_url)); |
| 240 } | 241 } |
| 241 | 242 |
| 242 int64 DOMStorageContextImpl::AllocateSessionId() { | 243 int64_t DOMStorageContextImpl::AllocateSessionId() { |
| 243 return session_id_sequence_.GetNext() + session_id_offset_; | 244 return session_id_sequence_.GetNext() + session_id_offset_; |
| 244 } | 245 } |
| 245 | 246 |
| 246 std::string DOMStorageContextImpl::AllocatePersistentSessionId() { | 247 std::string DOMStorageContextImpl::AllocatePersistentSessionId() { |
| 247 std::string guid = base::GenerateGUID(); | 248 std::string guid = base::GenerateGUID(); |
| 248 std::replace(guid.begin(), guid.end(), '-', '_'); | 249 std::replace(guid.begin(), guid.end(), '-', '_'); |
| 249 return guid; | 250 return guid; |
| 250 } | 251 } |
| 251 | 252 |
| 252 void DOMStorageContextImpl::CreateSessionNamespace( | 253 void DOMStorageContextImpl::CreateSessionNamespace( |
| 253 int64 namespace_id, | 254 int64_t namespace_id, |
| 254 const std::string& persistent_namespace_id) { | 255 const std::string& persistent_namespace_id) { |
| 255 if (is_shutdown_) | 256 if (is_shutdown_) |
| 256 return; | 257 return; |
| 257 DCHECK(namespace_id != kLocalStorageNamespaceId); | 258 DCHECK(namespace_id != kLocalStorageNamespaceId); |
| 258 DCHECK(namespaces_.find(namespace_id) == namespaces_.end()); | 259 DCHECK(namespaces_.find(namespace_id) == namespaces_.end()); |
| 259 namespaces_[namespace_id] = new DOMStorageNamespace( | 260 namespaces_[namespace_id] = new DOMStorageNamespace( |
| 260 namespace_id, persistent_namespace_id, session_storage_database_.get(), | 261 namespace_id, persistent_namespace_id, session_storage_database_.get(), |
| 261 task_runner_.get()); | 262 task_runner_.get()); |
| 262 persistent_namespace_id_to_namespace_id_[persistent_namespace_id] = | 263 persistent_namespace_id_to_namespace_id_[persistent_namespace_id] = |
| 263 namespace_id; | 264 namespace_id; |
| 264 } | 265 } |
| 265 | 266 |
| 266 void DOMStorageContextImpl::DeleteSessionNamespace( | 267 void DOMStorageContextImpl::DeleteSessionNamespace(int64_t namespace_id, |
| 267 int64 namespace_id, bool should_persist_data) { | 268 bool should_persist_data) { |
| 268 DCHECK_NE(kLocalStorageNamespaceId, namespace_id); | 269 DCHECK_NE(kLocalStorageNamespaceId, namespace_id); |
| 269 StorageNamespaceMap::const_iterator it = namespaces_.find(namespace_id); | 270 StorageNamespaceMap::const_iterator it = namespaces_.find(namespace_id); |
| 270 if (it == namespaces_.end()) | 271 if (it == namespaces_.end()) |
| 271 return; | 272 return; |
| 272 std::string persistent_namespace_id = it->second->persistent_namespace_id(); | 273 std::string persistent_namespace_id = it->second->persistent_namespace_id(); |
| 273 if (session_storage_database_.get()) { | 274 if (session_storage_database_.get()) { |
| 274 if (!should_persist_data) { | 275 if (!should_persist_data) { |
| 275 task_runner_->PostShutdownBlockingTask( | 276 task_runner_->PostShutdownBlockingTask( |
| 276 FROM_HERE, | 277 FROM_HERE, |
| 277 DOMStorageTaskRunner::COMMIT_SEQUENCE, | 278 DOMStorageTaskRunner::COMMIT_SEQUENCE, |
| 278 base::Bind( | 279 base::Bind( |
| 279 base::IgnoreResult(&SessionStorageDatabase::DeleteNamespace), | 280 base::IgnoreResult(&SessionStorageDatabase::DeleteNamespace), |
| 280 session_storage_database_, | 281 session_storage_database_, |
| 281 persistent_namespace_id)); | 282 persistent_namespace_id)); |
| 282 } else { | 283 } else { |
| 283 // Ensure that the data gets committed before we shut down. | 284 // Ensure that the data gets committed before we shut down. |
| 284 it->second->Shutdown(); | 285 it->second->Shutdown(); |
| 285 if (!scavenging_started_) { | 286 if (!scavenging_started_) { |
| 286 // Protect the persistent namespace ID from scavenging. | 287 // Protect the persistent namespace ID from scavenging. |
| 287 protected_persistent_session_ids_.insert(persistent_namespace_id); | 288 protected_persistent_session_ids_.insert(persistent_namespace_id); |
| 288 } | 289 } |
| 289 } | 290 } |
| 290 } | 291 } |
| 291 persistent_namespace_id_to_namespace_id_.erase(persistent_namespace_id); | 292 persistent_namespace_id_to_namespace_id_.erase(persistent_namespace_id); |
| 292 namespaces_.erase(namespace_id); | 293 namespaces_.erase(namespace_id); |
| 293 } | 294 } |
| 294 | 295 |
| 295 void DOMStorageContextImpl::CloneSessionNamespace( | 296 void DOMStorageContextImpl::CloneSessionNamespace( |
| 296 int64 existing_id, int64 new_id, | 297 int64_t existing_id, |
| 298 int64_t new_id, |
| 297 const std::string& new_persistent_id) { | 299 const std::string& new_persistent_id) { |
| 298 if (is_shutdown_) | 300 if (is_shutdown_) |
| 299 return; | 301 return; |
| 300 DCHECK_NE(kLocalStorageNamespaceId, existing_id); | 302 DCHECK_NE(kLocalStorageNamespaceId, existing_id); |
| 301 DCHECK_NE(kLocalStorageNamespaceId, new_id); | 303 DCHECK_NE(kLocalStorageNamespaceId, new_id); |
| 302 StorageNamespaceMap::iterator found = namespaces_.find(existing_id); | 304 StorageNamespaceMap::iterator found = namespaces_.find(existing_id); |
| 303 if (found != namespaces_.end()) | 305 if (found != namespaces_.end()) |
| 304 namespaces_[new_id] = found->second->Clone(new_id, new_persistent_id); | 306 namespaces_[new_id] = found->second->Clone(new_id, new_persistent_id); |
| 305 else | 307 else |
| 306 CreateSessionNamespace(new_id, new_persistent_id); | 308 CreateSessionNamespace(new_id, new_persistent_id); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 if (!deletable_persistent_namespace_ids_.empty()) { | 420 if (!deletable_persistent_namespace_ids_.empty()) { |
| 419 task_runner_->PostDelayedTask( | 421 task_runner_->PostDelayedTask( |
| 420 FROM_HERE, base::Bind( | 422 FROM_HERE, base::Bind( |
| 421 &DOMStorageContextImpl::DeleteNextUnusedNamespace, | 423 &DOMStorageContextImpl::DeleteNextUnusedNamespace, |
| 422 this), | 424 this), |
| 423 base::TimeDelta::FromSeconds(kSessionStoraceScavengingSeconds)); | 425 base::TimeDelta::FromSeconds(kSessionStoraceScavengingSeconds)); |
| 424 } | 426 } |
| 425 } | 427 } |
| 426 | 428 |
| 427 } // namespace content | 429 } // namespace content |
| OLD | NEW |