Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 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/browser/dom_storage/dom_storage_context.h" |
| 6 | |
| 7 | #include "base/bind.h" |
| 8 | #include "base/bind_helpers.h" |
| 9 | #include "base/file_util.h" |
| 10 | #include "base/files/file_enumerator.h" |
| 11 | #include "base/guid.h" |
| 12 | #include "base/location.h" |
| 13 | #include "base/time.h" |
| 14 | #include "webkit/browser/dom_storage/dom_storage_area.h" |
| 15 | #include "webkit/browser/dom_storage/dom_storage_database.h" |
| 16 | #include "webkit/browser/dom_storage/dom_storage_namespace.h" |
| 17 | #include "webkit/browser/dom_storage/dom_storage_task_runner.h" |
| 18 | #include "webkit/browser/dom_storage/session_storage_database.h" |
| 19 | #include "webkit/browser/quota/special_storage_policy.h" |
| 20 | #include "webkit/common/dom_storage/dom_storage_types.h" |
| 21 | |
| 22 | namespace dom_storage { |
| 23 | |
| 24 | static const int kSessionStoraceScavengingSeconds = 60; |
| 25 | |
| 26 | DomStorageContext::DomStorageContext( |
| 27 | const base::FilePath& localstorage_directory, |
| 28 | const base::FilePath& sessionstorage_directory, |
| 29 | quota::SpecialStoragePolicy* special_storage_policy, |
| 30 | DomStorageTaskRunner* task_runner) |
| 31 | : localstorage_directory_(localstorage_directory), |
| 32 | sessionstorage_directory_(sessionstorage_directory), |
| 33 | task_runner_(task_runner), |
| 34 | is_shutdown_(false), |
| 35 | force_keep_session_state_(false), |
| 36 | special_storage_policy_(special_storage_policy), |
| 37 | scavenging_started_(false) { |
| 38 | // AtomicSequenceNum starts at 0 but we want to start session |
| 39 | // namespace ids at one since zero is reserved for the |
| 40 | // kLocalStorageNamespaceId. |
| 41 | session_id_sequence_.GetNext(); |
| 42 | } |
| 43 | |
| 44 | DomStorageContext::~DomStorageContext() { |
| 45 | if (session_storage_database_.get()) { |
| 46 | // SessionStorageDatabase shouldn't be deleted right away: deleting it will |
| 47 | // potentially involve waiting in leveldb::DBImpl::~DBImpl, and waiting |
| 48 | // shouldn't happen on this thread. |
| 49 | SessionStorageDatabase* to_release = session_storage_database_.get(); |
| 50 | to_release->AddRef(); |
| 51 | session_storage_database_ = NULL; |
| 52 | task_runner_->PostShutdownBlockingTask( |
| 53 | FROM_HERE, |
| 54 | DomStorageTaskRunner::COMMIT_SEQUENCE, |
| 55 | base::Bind(&SessionStorageDatabase::Release, |
| 56 | base::Unretained(to_release))); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | DomStorageNamespace* DomStorageContext::GetStorageNamespace( |
| 61 | int64 namespace_id) { |
| 62 | if (is_shutdown_) |
| 63 | return NULL; |
| 64 | StorageNamespaceMap::iterator found = namespaces_.find(namespace_id); |
| 65 | if (found == namespaces_.end()) { |
| 66 | if (namespace_id == kLocalStorageNamespaceId) { |
| 67 | if (!localstorage_directory_.empty()) { |
| 68 | if (!file_util::CreateDirectory(localstorage_directory_)) { |
| 69 | LOG(ERROR) << "Failed to create 'Local Storage' directory," |
| 70 | " falling back to in-memory only."; |
| 71 | localstorage_directory_ = base::FilePath(); |
| 72 | } |
| 73 | } |
| 74 | DomStorageNamespace* local = |
| 75 | new DomStorageNamespace(localstorage_directory_, task_runner_.get()); |
| 76 | namespaces_[kLocalStorageNamespaceId] = local; |
| 77 | return local; |
| 78 | } |
| 79 | return NULL; |
| 80 | } |
| 81 | return found->second.get(); |
| 82 | } |
| 83 | |
| 84 | void DomStorageContext::GetLocalStorageUsage( |
| 85 | std::vector<LocalStorageUsageInfo>* infos, |
| 86 | bool include_file_info) { |
| 87 | if (localstorage_directory_.empty()) |
| 88 | return; |
| 89 | base::FileEnumerator enumerator(localstorage_directory_, false, |
| 90 | base::FileEnumerator::FILES); |
| 91 | for (base::FilePath path = enumerator.Next(); !path.empty(); |
| 92 | path = enumerator.Next()) { |
| 93 | if (path.MatchesExtension(DomStorageArea::kDatabaseFileExtension)) { |
| 94 | LocalStorageUsageInfo info; |
| 95 | info.origin = DomStorageArea::OriginFromDatabaseFileName(path); |
| 96 | if (include_file_info) { |
| 97 | base::FileEnumerator::FileInfo find_info = enumerator.GetInfo(); |
| 98 | info.data_size = find_info.GetSize(); |
| 99 | info.last_modified = find_info.GetLastModifiedTime(); |
| 100 | } |
| 101 | infos->push_back(info); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void DomStorageContext::GetSessionStorageUsage( |
| 107 | std::vector<SessionStorageUsageInfo>* infos) { |
| 108 | if (!session_storage_database_.get()) |
| 109 | return; |
| 110 | std::map<std::string, std::vector<GURL> > namespaces_and_origins; |
| 111 | session_storage_database_->ReadNamespacesAndOrigins( |
| 112 | &namespaces_and_origins); |
| 113 | for (std::map<std::string, std::vector<GURL> >::const_iterator it = |
| 114 | namespaces_and_origins.begin(); |
| 115 | it != namespaces_and_origins.end(); ++it) { |
| 116 | for (std::vector<GURL>::const_iterator origin_it = it->second.begin(); |
| 117 | origin_it != it->second.end(); ++origin_it) { |
| 118 | SessionStorageUsageInfo info; |
| 119 | info.persistent_namespace_id = it->first; |
| 120 | info.origin = *origin_it; |
| 121 | infos->push_back(info); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | void DomStorageContext::DeleteLocalStorage(const GURL& origin) { |
| 127 | DCHECK(!is_shutdown_); |
| 128 | DomStorageNamespace* local = GetStorageNamespace(kLocalStorageNamespaceId); |
| 129 | local->DeleteLocalStorageOrigin(origin); |
| 130 | // Synthesize a 'cleared' event if the area is open so CachedAreas in |
| 131 | // renderers get emptied out too. |
| 132 | DomStorageArea* area = local->GetOpenStorageArea(origin); |
| 133 | if (area) |
| 134 | NotifyAreaCleared(area, origin); |
| 135 | } |
| 136 | |
| 137 | void DomStorageContext::DeleteSessionStorage( |
| 138 | const SessionStorageUsageInfo& usage_info) { |
| 139 | DCHECK(!is_shutdown_); |
| 140 | DomStorageNamespace* dom_storage_namespace = NULL; |
| 141 | std::map<std::string, int64>::const_iterator it = |
| 142 | persistent_namespace_id_to_namespace_id_.find( |
| 143 | usage_info.persistent_namespace_id); |
| 144 | if (it != persistent_namespace_id_to_namespace_id_.end()) { |
| 145 | dom_storage_namespace = GetStorageNamespace(it->second); |
| 146 | } else { |
| 147 | int64 namespace_id = AllocateSessionId(); |
| 148 | CreateSessionNamespace(namespace_id, usage_info.persistent_namespace_id); |
| 149 | dom_storage_namespace = GetStorageNamespace(namespace_id); |
| 150 | } |
| 151 | dom_storage_namespace->DeleteSessionStorageOrigin(usage_info.origin); |
| 152 | // Synthesize a 'cleared' event if the area is open so CachedAreas in |
| 153 | // renderers get emptied out too. |
| 154 | DomStorageArea* area = |
| 155 | dom_storage_namespace->GetOpenStorageArea(usage_info.origin); |
| 156 | if (area) |
| 157 | NotifyAreaCleared(area, usage_info.origin); |
| 158 | } |
| 159 | |
| 160 | void DomStorageContext::PurgeMemory() { |
| 161 | // We can only purge memory from the local storage namespace |
| 162 | // which is backed by disk. |
| 163 | // TODO(marja): Purge sessionStorage, too. (Requires changes to the FastClear |
| 164 | // functionality.) |
| 165 | StorageNamespaceMap::iterator found = |
| 166 | namespaces_.find(kLocalStorageNamespaceId); |
| 167 | if (found != namespaces_.end()) |
| 168 | found->second->PurgeMemory(DomStorageNamespace::PURGE_AGGRESSIVE); |
| 169 | } |
| 170 | |
| 171 | void DomStorageContext::Shutdown() { |
| 172 | is_shutdown_ = true; |
| 173 | StorageNamespaceMap::const_iterator it = namespaces_.begin(); |
| 174 | for (; it != namespaces_.end(); ++it) |
| 175 | it->second->Shutdown(); |
| 176 | |
| 177 | if (localstorage_directory_.empty() && !session_storage_database_.get()) |
| 178 | return; |
| 179 | |
| 180 | // Respect the content policy settings about what to |
| 181 | // keep and what to discard. |
| 182 | if (force_keep_session_state_) |
| 183 | return; // Keep everything. |
| 184 | |
| 185 | bool has_session_only_origins = |
| 186 | special_storage_policy_.get() && |
| 187 | special_storage_policy_->HasSessionOnlyOrigins(); |
| 188 | |
| 189 | if (has_session_only_origins) { |
| 190 | // We may have to delete something. We continue on the |
| 191 | // commit sequence after area shutdown tasks have cycled |
| 192 | // thru that sequence (and closed their database files). |
| 193 | bool success = task_runner_->PostShutdownBlockingTask( |
| 194 | FROM_HERE, |
| 195 | DomStorageTaskRunner::COMMIT_SEQUENCE, |
| 196 | base::Bind(&DomStorageContext::ClearSessionOnlyOrigins, this)); |
| 197 | DCHECK(success); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | void DomStorageContext::AddEventObserver(EventObserver* observer) { |
| 202 | event_observers_.AddObserver(observer); |
| 203 | } |
| 204 | |
| 205 | void DomStorageContext::RemoveEventObserver(EventObserver* observer) { |
| 206 | event_observers_.RemoveObserver(observer); |
| 207 | } |
| 208 | |
| 209 | void DomStorageContext::NotifyItemSet( |
| 210 | const DomStorageArea* area, |
| 211 | const base::string16& key, |
| 212 | const base::string16& new_value, |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame^] | 213 | const base::NullableString16& old_value, |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 214 | const GURL& page_url) { |
| 215 | FOR_EACH_OBSERVER( |
| 216 | EventObserver, event_observers_, |
| 217 | OnDomStorageItemSet(area, key, new_value, old_value, page_url)); |
| 218 | } |
| 219 | |
| 220 | void DomStorageContext::NotifyItemRemoved( |
| 221 | const DomStorageArea* area, |
| 222 | const base::string16& key, |
| 223 | const base::string16& old_value, |
| 224 | const GURL& page_url) { |
| 225 | FOR_EACH_OBSERVER( |
| 226 | EventObserver, event_observers_, |
| 227 | OnDomStorageItemRemoved(area, key, old_value, page_url)); |
| 228 | } |
| 229 | |
| 230 | void DomStorageContext::NotifyAreaCleared( |
| 231 | const DomStorageArea* area, |
| 232 | const GURL& page_url) { |
| 233 | FOR_EACH_OBSERVER( |
| 234 | EventObserver, event_observers_, |
| 235 | OnDomStorageAreaCleared(area, page_url)); |
| 236 | } |
| 237 | |
| 238 | std::string DomStorageContext::AllocatePersistentSessionId() { |
| 239 | std::string guid = base::GenerateGUID(); |
| 240 | std::replace(guid.begin(), guid.end(), '-', '_'); |
| 241 | return guid; |
| 242 | } |
| 243 | |
| 244 | void DomStorageContext::CreateSessionNamespace( |
| 245 | int64 namespace_id, |
| 246 | const std::string& persistent_namespace_id) { |
| 247 | if (is_shutdown_) |
| 248 | return; |
| 249 | DCHECK(namespace_id != kLocalStorageNamespaceId); |
| 250 | DCHECK(namespaces_.find(namespace_id) == namespaces_.end()); |
| 251 | namespaces_[namespace_id] = new DomStorageNamespace( |
| 252 | namespace_id, persistent_namespace_id, session_storage_database_.get(), |
| 253 | task_runner_.get()); |
| 254 | persistent_namespace_id_to_namespace_id_[persistent_namespace_id] = |
| 255 | namespace_id; |
| 256 | } |
| 257 | |
| 258 | void DomStorageContext::DeleteSessionNamespace( |
| 259 | int64 namespace_id, bool should_persist_data) { |
| 260 | DCHECK_NE(kLocalStorageNamespaceId, namespace_id); |
| 261 | StorageNamespaceMap::const_iterator it = namespaces_.find(namespace_id); |
| 262 | if (it == namespaces_.end()) |
| 263 | return; |
| 264 | std::string persistent_namespace_id = it->second->persistent_namespace_id(); |
| 265 | if (session_storage_database_.get()) { |
| 266 | if (!should_persist_data) { |
| 267 | task_runner_->PostShutdownBlockingTask( |
| 268 | FROM_HERE, |
| 269 | DomStorageTaskRunner::COMMIT_SEQUENCE, |
| 270 | base::Bind( |
| 271 | base::IgnoreResult(&SessionStorageDatabase::DeleteNamespace), |
| 272 | session_storage_database_, |
| 273 | persistent_namespace_id)); |
| 274 | } else { |
| 275 | // Ensure that the data gets committed before we shut down. |
| 276 | it->second->Shutdown(); |
| 277 | if (!scavenging_started_) { |
| 278 | // Protect the persistent namespace ID from scavenging. |
| 279 | protected_persistent_session_ids_.insert(persistent_namespace_id); |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | persistent_namespace_id_to_namespace_id_.erase(persistent_namespace_id); |
| 284 | namespaces_.erase(namespace_id); |
| 285 | } |
| 286 | |
| 287 | void DomStorageContext::CloneSessionNamespace( |
| 288 | int64 existing_id, int64 new_id, |
| 289 | const std::string& new_persistent_id) { |
| 290 | if (is_shutdown_) |
| 291 | return; |
| 292 | DCHECK_NE(kLocalStorageNamespaceId, existing_id); |
| 293 | DCHECK_NE(kLocalStorageNamespaceId, new_id); |
| 294 | StorageNamespaceMap::iterator found = namespaces_.find(existing_id); |
| 295 | if (found != namespaces_.end()) |
| 296 | namespaces_[new_id] = found->second->Clone(new_id, new_persistent_id); |
| 297 | else |
| 298 | CreateSessionNamespace(new_id, new_persistent_id); |
| 299 | } |
| 300 | |
| 301 | void DomStorageContext::ClearSessionOnlyOrigins() { |
| 302 | if (!localstorage_directory_.empty()) { |
| 303 | std::vector<LocalStorageUsageInfo> infos; |
| 304 | const bool kDontIncludeFileInfo = false; |
| 305 | GetLocalStorageUsage(&infos, kDontIncludeFileInfo); |
| 306 | for (size_t i = 0; i < infos.size(); ++i) { |
| 307 | const GURL& origin = infos[i].origin; |
| 308 | if (special_storage_policy_->IsStorageProtected(origin)) |
| 309 | continue; |
| 310 | if (!special_storage_policy_->IsStorageSessionOnly(origin)) |
| 311 | continue; |
| 312 | |
| 313 | const bool kNotRecursive = false; |
| 314 | base::FilePath database_file_path = localstorage_directory_.Append( |
| 315 | DomStorageArea::DatabaseFileNameFromOrigin(origin)); |
| 316 | file_util::Delete(database_file_path, kNotRecursive); |
| 317 | file_util::Delete( |
| 318 | DomStorageDatabase::GetJournalFilePath(database_file_path), |
| 319 | kNotRecursive); |
| 320 | } |
| 321 | } |
| 322 | if (session_storage_database_.get()) { |
| 323 | std::vector<SessionStorageUsageInfo> infos; |
| 324 | GetSessionStorageUsage(&infos); |
| 325 | for (size_t i = 0; i < infos.size(); ++i) { |
| 326 | const GURL& origin = infos[i].origin; |
| 327 | if (special_storage_policy_->IsStorageProtected(origin)) |
| 328 | continue; |
| 329 | if (!special_storage_policy_->IsStorageSessionOnly(origin)) |
| 330 | continue; |
| 331 | session_storage_database_->DeleteArea(infos[i].persistent_namespace_id, |
| 332 | origin); |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | void DomStorageContext::SetSaveSessionStorageOnDisk() { |
| 338 | DCHECK(namespaces_.empty()); |
| 339 | if (!sessionstorage_directory_.empty()) { |
| 340 | session_storage_database_ = new SessionStorageDatabase( |
| 341 | sessionstorage_directory_); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | void DomStorageContext::StartScavengingUnusedSessionStorage() { |
| 346 | if (session_storage_database_.get()) { |
| 347 | task_runner_->PostDelayedTask( |
| 348 | FROM_HERE, base::Bind(&DomStorageContext::FindUnusedNamespaces, this), |
| 349 | base::TimeDelta::FromSeconds(kSessionStoraceScavengingSeconds)); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | void DomStorageContext::FindUnusedNamespaces() { |
| 354 | DCHECK(session_storage_database_.get()); |
| 355 | if (scavenging_started_) |
| 356 | return; |
| 357 | scavenging_started_ = true; |
| 358 | std::set<std::string> namespace_ids_in_use; |
| 359 | for (StorageNamespaceMap::const_iterator it = namespaces_.begin(); |
| 360 | it != namespaces_.end(); ++it) |
| 361 | namespace_ids_in_use.insert(it->second->persistent_namespace_id()); |
| 362 | std::set<std::string> protected_persistent_session_ids; |
| 363 | protected_persistent_session_ids.swap(protected_persistent_session_ids_); |
| 364 | task_runner_->PostShutdownBlockingTask( |
| 365 | FROM_HERE, DomStorageTaskRunner::COMMIT_SEQUENCE, |
| 366 | base::Bind( |
| 367 | &DomStorageContext::FindUnusedNamespacesInCommitSequence, |
| 368 | this, namespace_ids_in_use, protected_persistent_session_ids)); |
| 369 | } |
| 370 | |
| 371 | void DomStorageContext::FindUnusedNamespacesInCommitSequence( |
| 372 | const std::set<std::string>& namespace_ids_in_use, |
| 373 | const std::set<std::string>& protected_persistent_session_ids) { |
| 374 | DCHECK(session_storage_database_.get()); |
| 375 | // Delete all namespaces which don't have an associated DomStorageNamespace |
| 376 | // alive. |
| 377 | std::map<std::string, std::vector<GURL> > namespaces_and_origins; |
| 378 | session_storage_database_->ReadNamespacesAndOrigins(&namespaces_and_origins); |
| 379 | for (std::map<std::string, std::vector<GURL> >::const_iterator it = |
| 380 | namespaces_and_origins.begin(); |
| 381 | it != namespaces_and_origins.end(); ++it) { |
| 382 | if (namespace_ids_in_use.find(it->first) == namespace_ids_in_use.end() && |
| 383 | protected_persistent_session_ids.find(it->first) == |
| 384 | protected_persistent_session_ids.end()) { |
| 385 | deletable_persistent_namespace_ids_.push_back(it->first); |
| 386 | } |
| 387 | } |
| 388 | if (!deletable_persistent_namespace_ids_.empty()) { |
| 389 | task_runner_->PostDelayedTask( |
| 390 | FROM_HERE, base::Bind( |
| 391 | &DomStorageContext::DeleteNextUnusedNamespace, |
| 392 | this), |
| 393 | base::TimeDelta::FromSeconds(kSessionStoraceScavengingSeconds)); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | void DomStorageContext::DeleteNextUnusedNamespace() { |
| 398 | if (is_shutdown_) |
| 399 | return; |
| 400 | task_runner_->PostShutdownBlockingTask( |
| 401 | FROM_HERE, DomStorageTaskRunner::COMMIT_SEQUENCE, |
| 402 | base::Bind( |
| 403 | &DomStorageContext::DeleteNextUnusedNamespaceInCommitSequence, |
| 404 | this)); |
| 405 | } |
| 406 | |
| 407 | void DomStorageContext::DeleteNextUnusedNamespaceInCommitSequence() { |
| 408 | if (deletable_persistent_namespace_ids_.empty()) |
| 409 | return; |
| 410 | const std::string& persistent_id = deletable_persistent_namespace_ids_.back(); |
| 411 | session_storage_database_->DeleteNamespace(persistent_id); |
| 412 | deletable_persistent_namespace_ids_.pop_back(); |
| 413 | if (!deletable_persistent_namespace_ids_.empty()) { |
| 414 | task_runner_->PostDelayedTask( |
| 415 | FROM_HERE, base::Bind( |
| 416 | &DomStorageContext::DeleteNextUnusedNamespace, |
| 417 | this), |
| 418 | base::TimeDelta::FromSeconds(kSessionStoraceScavengingSeconds)); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | } // namespace dom_storage |