| 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/dom_storage/dom_storage_context.h" | 5 #include "webkit/dom_storage/dom_storage_context.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/time.h" | 11 #include "base/time.h" |
| 12 #include "webkit/dom_storage/dom_storage_area.h" | 12 #include "webkit/dom_storage/dom_storage_area.h" |
| 13 #include "webkit/dom_storage/dom_storage_namespace.h" | 13 #include "webkit/dom_storage/dom_storage_namespace.h" |
| 14 #include "webkit/dom_storage/dom_storage_task_runner.h" | 14 #include "webkit/dom_storage/dom_storage_task_runner.h" |
| 15 #include "webkit/dom_storage/dom_storage_types.h" | 15 #include "webkit/dom_storage/dom_storage_types.h" |
| 16 #include "webkit/quota/special_storage_policy.h" | 16 #include "webkit/quota/special_storage_policy.h" |
| 17 | 17 |
| 18 using file_util::FileEnumerator; | 18 using file_util::FileEnumerator; |
| 19 | 19 |
| 20 namespace dom_storage { | 20 namespace dom_storage { |
| 21 | 21 |
| 22 DomStorageContext::UsageInfo::UsageInfo() : data_size(0) {} | 22 DomStorageContext::UsageInfo::UsageInfo() : data_size(0) {} |
| 23 DomStorageContext::UsageInfo::~UsageInfo() {} | 23 DomStorageContext::UsageInfo::~UsageInfo() {} |
| 24 | 24 |
| 25 DomStorageContext::DomStorageContext( | 25 DomStorageContext::DomStorageContext( |
| 26 const FilePath& directory, | 26 const FilePath& localstorage_directory, |
| 27 const FilePath& sessionstorage_directory, |
| 27 quota::SpecialStoragePolicy* special_storage_policy, | 28 quota::SpecialStoragePolicy* special_storage_policy, |
| 28 DomStorageTaskRunner* task_runner) | 29 DomStorageTaskRunner* task_runner) |
| 29 : directory_(directory), | 30 : localstorage_directory_(localstorage_directory), |
| 31 sessionstorage_directory_(sessionstorage_directory), |
| 30 task_runner_(task_runner), | 32 task_runner_(task_runner), |
| 31 is_shutdown_(false), | 33 is_shutdown_(false), |
| 32 clear_local_state_(false), | 34 clear_local_state_(false), |
| 33 save_session_state_(false), | 35 save_session_state_(false), |
| 34 special_storage_policy_(special_storage_policy) { | 36 special_storage_policy_(special_storage_policy) { |
| 35 // AtomicSequenceNum starts at 0 but we want to start session | 37 // AtomicSequenceNum starts at 0 but we want to start session |
| 36 // namespace ids at one since zero is reserved for the | 38 // namespace ids at one since zero is reserved for the |
| 37 // kLocalStorageNamespaceId. | 39 // kLocalStorageNamespaceId. |
| 38 session_id_sequence_.GetNext(); | 40 session_id_sequence_.GetNext(); |
| 39 } | 41 } |
| 40 | 42 |
| 41 DomStorageContext::~DomStorageContext() { | 43 DomStorageContext::~DomStorageContext() { |
| 42 } | 44 } |
| 43 | 45 |
| 44 DomStorageNamespace* DomStorageContext::GetStorageNamespace( | 46 DomStorageNamespace* DomStorageContext::GetStorageNamespace( |
| 45 int64 namespace_id) { | 47 int64 namespace_id) { |
| 46 if (is_shutdown_) | 48 if (is_shutdown_) |
| 47 return NULL; | 49 return NULL; |
| 48 StorageNamespaceMap::iterator found = namespaces_.find(namespace_id); | 50 StorageNamespaceMap::iterator found = namespaces_.find(namespace_id); |
| 49 if (found == namespaces_.end()) { | 51 if (found == namespaces_.end()) { |
| 50 if (namespace_id == kLocalStorageNamespaceId) { | 52 if (namespace_id == kLocalStorageNamespaceId) { |
| 51 if (!directory_.empty()) { | 53 if (!localstorage_directory_.empty()) { |
| 52 if (!file_util::CreateDirectory(directory_)) { | 54 if (!file_util::CreateDirectory(localstorage_directory_)) { |
| 53 LOG(ERROR) << "Failed to create 'Local Storage' directory," | 55 LOG(ERROR) << "Failed to create 'Local Storage' directory," |
| 54 " falling back to in-memory only."; | 56 " falling back to in-memory only."; |
| 55 directory_ = FilePath(); | 57 localstorage_directory_ = FilePath(); |
| 56 } | 58 } |
| 57 } | 59 } |
| 58 DomStorageNamespace* local = | 60 DomStorageNamespace* local = |
| 59 new DomStorageNamespace(directory_, task_runner_); | 61 new DomStorageNamespace(localstorage_directory_, task_runner_); |
| 60 namespaces_[kLocalStorageNamespaceId] = local; | 62 namespaces_[kLocalStorageNamespaceId] = local; |
| 61 return local; | 63 return local; |
| 62 } | 64 } |
| 63 return NULL; | 65 return NULL; |
| 64 } | 66 } |
| 65 return found->second; | 67 return found->second; |
| 66 } | 68 } |
| 67 | 69 |
| 68 void DomStorageContext::GetUsageInfo(std::vector<UsageInfo>* infos, | 70 void DomStorageContext::GetUsageInfo(std::vector<UsageInfo>* infos, |
| 69 bool include_file_info) { | 71 bool include_file_info) { |
| 70 if (directory_.empty()) | 72 if (localstorage_directory_.empty()) |
| 71 return; | 73 return; |
| 72 FileEnumerator enumerator(directory_, false, FileEnumerator::FILES); | 74 FileEnumerator enumerator(localstorage_directory_, false, |
| 75 FileEnumerator::FILES); |
| 73 for (FilePath path = enumerator.Next(); !path.empty(); | 76 for (FilePath path = enumerator.Next(); !path.empty(); |
| 74 path = enumerator.Next()) { | 77 path = enumerator.Next()) { |
| 75 if (path.MatchesExtension(DomStorageArea::kDatabaseFileExtension)) { | 78 if (path.MatchesExtension(DomStorageArea::kDatabaseFileExtension)) { |
| 76 UsageInfo info; | 79 UsageInfo info; |
| 77 info.origin = DomStorageArea::OriginFromDatabaseFileName(path); | 80 info.origin = DomStorageArea::OriginFromDatabaseFileName(path); |
| 78 if (include_file_info) { | 81 if (include_file_info) { |
| 79 FileEnumerator::FindInfo find_info; | 82 FileEnumerator::FindInfo find_info; |
| 80 enumerator.GetFindInfo(&find_info); | 83 enumerator.GetFindInfo(&find_info); |
| 81 info.data_size = FileEnumerator::GetFilesize(find_info); | 84 info.data_size = FileEnumerator::GetFilesize(find_info); |
| 82 info.last_modified = FileEnumerator::GetLastModifiedTime(find_info); | 85 info.last_modified = FileEnumerator::GetLastModifiedTime(find_info); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 if (found != namespaces_.end()) | 117 if (found != namespaces_.end()) |
| 115 found->second->PurgeMemory(); | 118 found->second->PurgeMemory(); |
| 116 } | 119 } |
| 117 | 120 |
| 118 void DomStorageContext::Shutdown() { | 121 void DomStorageContext::Shutdown() { |
| 119 is_shutdown_ = true; | 122 is_shutdown_ = true; |
| 120 StorageNamespaceMap::const_iterator it = namespaces_.begin(); | 123 StorageNamespaceMap::const_iterator it = namespaces_.begin(); |
| 121 for (; it != namespaces_.end(); ++it) | 124 for (; it != namespaces_.end(); ++it) |
| 122 it->second->Shutdown(); | 125 it->second->Shutdown(); |
| 123 | 126 |
| 124 if (directory_.empty()) | 127 if (localstorage_directory_.empty()) |
| 125 return; | 128 return; |
| 126 | 129 |
| 127 // Respect the content policy settings about what to | 130 // Respect the content policy settings about what to |
| 128 // keep and what to discard. | 131 // keep and what to discard. |
| 129 if (save_session_state_) | 132 if (save_session_state_) |
| 130 return; // Keep everything. | 133 return; // Keep everything. |
| 131 | 134 |
| 132 bool has_session_only_origins = | 135 bool has_session_only_origins = |
| 133 special_storage_policy_.get() && | 136 special_storage_policy_.get() && |
| 134 special_storage_policy_->HasSessionOnlyOrigins(); | 137 special_storage_policy_->HasSessionOnlyOrigins(); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 for (size_t i = 0; i < infos.size(); ++i) { | 221 for (size_t i = 0; i < infos.size(); ++i) { |
| 219 const GURL& origin = infos[i].origin; | 222 const GURL& origin = infos[i].origin; |
| 220 if (special_storage_policy_ && | 223 if (special_storage_policy_ && |
| 221 special_storage_policy_->IsStorageProtected(origin)) | 224 special_storage_policy_->IsStorageProtected(origin)) |
| 222 continue; | 225 continue; |
| 223 if (!clear_local_state_ && | 226 if (!clear_local_state_ && |
| 224 !special_storage_policy_->IsStorageSessionOnly(origin)) | 227 !special_storage_policy_->IsStorageSessionOnly(origin)) |
| 225 continue; | 228 continue; |
| 226 | 229 |
| 227 const bool kNotRecursive = false; | 230 const bool kNotRecursive = false; |
| 228 FilePath database_file_path = directory_.Append( | 231 FilePath database_file_path = localstorage_directory_.Append( |
| 229 DomStorageArea::DatabaseFileNameFromOrigin(origin)); | 232 DomStorageArea::DatabaseFileNameFromOrigin(origin)); |
| 230 file_util::Delete(database_file_path, kNotRecursive); | 233 file_util::Delete(database_file_path, kNotRecursive); |
| 231 file_util::Delete( | 234 file_util::Delete( |
| 232 DomStorageDatabase::GetJournalFilePath(database_file_path), | 235 DomStorageDatabase::GetJournalFilePath(database_file_path), |
| 233 kNotRecursive); | 236 kNotRecursive); |
| 234 } | 237 } |
| 235 } | 238 } |
| 236 | 239 |
| 237 } // namespace dom_storage | 240 } // namespace dom_storage |
| OLD | NEW |