| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/in_process_webkit/dom_storage_context.h" | 5 #include "content/browser/in_process_webkit/dom_storage_context.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "content/browser/in_process_webkit/dom_storage_area.h" | 13 #include "content/browser/in_process_webkit/dom_storage_area.h" |
| 14 #include "content/browser/in_process_webkit/dom_storage_namespace.h" | 14 #include "content/browser/in_process_webkit/dom_storage_namespace.h" |
| 15 #include "content/browser/in_process_webkit/webkit_context.h" | 15 #include "content/browser/in_process_webkit/webkit_context.h" |
| 16 #include "content/common/dom_storage_common.h" | 16 #include "content/common/dom_storage_common.h" |
| 17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" |
| 19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
| 20 #include "webkit/glue/webkit_glue.h" | 20 #include "webkit/glue/webkit_glue.h" |
| 21 #include "webkit/quota/special_storage_policy.h" | 21 #include "webkit/quota/special_storage_policy.h" |
| 22 | 22 |
| 23 using content::BrowserThread; | 23 using content::BrowserThread; |
| 24 | 24 |
| 25 using WebKit::WebSecurityOrigin; | 25 using WebKit::WebSecurityOrigin; |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 void ClearLocalState(const FilePath& domstorage_path, | 29 void ClearLocalState(const FilePath& domstorage_path, |
| 30 quota::SpecialStoragePolicy* special_storage_policy) { | 30 quota::SpecialStoragePolicy* special_storage_policy, |
| 31 bool clear_all_databases) { |
| 31 file_util::FileEnumerator file_enumerator( | 32 file_util::FileEnumerator file_enumerator( |
| 32 domstorage_path, false, file_util::FileEnumerator::FILES); | 33 domstorage_path, false, file_util::FileEnumerator::FILES); |
| 33 for (FilePath file_path = file_enumerator.Next(); !file_path.empty(); | 34 for (FilePath file_path = file_enumerator.Next(); !file_path.empty(); |
| 34 file_path = file_enumerator.Next()) { | 35 file_path = file_enumerator.Next()) { |
| 35 if (file_path.Extension() == DOMStorageContext::kLocalStorageExtension) { | 36 if (file_path.Extension() == DOMStorageContext::kLocalStorageExtension) { |
| 36 GURL origin(WebSecurityOrigin::createFromDatabaseIdentifier( | 37 GURL origin(WebSecurityOrigin::createFromDatabaseIdentifier( |
| 37 webkit_glue::FilePathToWebString(file_path.BaseName())).toString()); | 38 webkit_glue::FilePathToWebString(file_path.BaseName())).toString()); |
| 38 if (!special_storage_policy->IsStorageProtected(origin)) | 39 if (special_storage_policy->IsStorageProtected(origin)) |
| 39 file_util::Delete(file_path, false); | 40 continue; |
| 41 if (!clear_all_databases && |
| 42 !special_storage_policy->IsStorageSessionOnly(origin)) { |
| 43 continue; |
| 44 } |
| 45 file_util::Delete(file_path, false); |
| 40 } | 46 } |
| 41 } | 47 } |
| 42 } | 48 } |
| 43 | 49 |
| 44 } // namespace | 50 } // namespace |
| 45 | 51 |
| 46 const FilePath::CharType DOMStorageContext::kLocalStorageDirectory[] = | 52 const FilePath::CharType DOMStorageContext::kLocalStorageDirectory[] = |
| 47 FILE_PATH_LITERAL("Local Storage"); | 53 FILE_PATH_LITERAL("Local Storage"); |
| 48 | 54 |
| 49 const FilePath::CharType DOMStorageContext::kLocalStorageExtension[] = | 55 const FilePath::CharType DOMStorageContext::kLocalStorageExtension[] = |
| 50 FILE_PATH_LITERAL(".localstorage"); | 56 FILE_PATH_LITERAL(".localstorage"); |
| 51 | 57 |
| 52 DOMStorageContext::DOMStorageContext( | 58 DOMStorageContext::DOMStorageContext( |
| 53 WebKitContext* webkit_context, | 59 WebKitContext* webkit_context, |
| 54 quota::SpecialStoragePolicy* special_storage_policy) | 60 quota::SpecialStoragePolicy* special_storage_policy) |
| 55 : last_storage_area_id_(0), | 61 : last_storage_area_id_(0), |
| 56 last_session_storage_namespace_id_on_ui_thread_(kLocalStorageNamespaceId), | 62 last_session_storage_namespace_id_on_ui_thread_(kLocalStorageNamespaceId), |
| 57 last_session_storage_namespace_id_on_io_thread_(kLocalStorageNamespaceId), | 63 last_session_storage_namespace_id_on_io_thread_(kLocalStorageNamespaceId), |
| 58 clear_local_state_on_exit_(false), | 64 clear_local_state_on_exit_(false), |
| 65 save_session_state_(false), |
| 59 special_storage_policy_(special_storage_policy) { | 66 special_storage_policy_(special_storage_policy) { |
| 60 data_path_ = webkit_context->data_path(); | 67 data_path_ = webkit_context->data_path(); |
| 61 } | 68 } |
| 62 | 69 |
| 63 DOMStorageContext::~DOMStorageContext() { | 70 DOMStorageContext::~DOMStorageContext() { |
| 64 // This should not go away until all DOM Storage message filters have gone | 71 // This should not go away until all DOM Storage message filters have gone |
| 65 // away. And they remove themselves from this list. | 72 // away. And they remove themselves from this list. |
| 66 DCHECK(message_filter_set_.empty()); | 73 DCHECK(message_filter_set_.empty()); |
| 67 | 74 |
| 68 for (StorageNamespaceMap::iterator iter(storage_namespace_map_.begin()); | 75 for (StorageNamespaceMap::iterator iter(storage_namespace_map_.begin()); |
| 69 iter != storage_namespace_map_.end(); ++iter) { | 76 iter != storage_namespace_map_.end(); ++iter) { |
| 70 delete iter->second; | 77 delete iter->second; |
| 71 } | 78 } |
| 72 | 79 |
| 80 if (save_session_state_) |
| 81 return; |
| 82 |
| 83 bool has_session_only_databases = |
| 84 special_storage_policy_.get() && |
| 85 special_storage_policy_->HasSessionOnlyOrigins(); |
| 86 |
| 87 // Clearning only session-only databases, and there are none. |
| 88 if (!clear_local_state_on_exit_ && !has_session_only_databases) |
| 89 return; |
| 90 |
| 73 // Not being on the WEBKIT thread here means we are running in a unit test | 91 // Not being on the WEBKIT thread here means we are running in a unit test |
| 74 // where no clean up is needed. | 92 // where no clean up is needed. |
| 75 if (clear_local_state_on_exit_ && | 93 if (BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)) { |
| 76 BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)) { | |
| 77 ClearLocalState(data_path_.Append(kLocalStorageDirectory), | 94 ClearLocalState(data_path_.Append(kLocalStorageDirectory), |
| 78 special_storage_policy_); | 95 special_storage_policy_, |
| 96 clear_local_state_on_exit_); |
| 79 } | 97 } |
| 80 } | 98 } |
| 81 | 99 |
| 82 int64 DOMStorageContext::AllocateStorageAreaId() { | 100 int64 DOMStorageContext::AllocateStorageAreaId() { |
| 83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); | 101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); |
| 84 return ++last_storage_area_id_; | 102 return ++last_storage_area_id_; |
| 85 } | 103 } |
| 86 | 104 |
| 87 int64 DOMStorageContext::AllocateSessionStorageNamespaceId() { | 105 int64 DOMStorageContext::AllocateSessionStorageNamespaceId() { |
| 88 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) | 106 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 if (special_storage_policy_->IsStorageProtected(origin)) | 212 if (special_storage_policy_->IsStorageProtected(origin)) |
| 195 continue; | 213 continue; |
| 196 | 214 |
| 197 file_util::FileEnumerator::FindInfo find_info; | 215 file_util::FileEnumerator::FindInfo find_info; |
| 198 file_enumerator.GetFindInfo(&find_info); | 216 file_enumerator.GetFindInfo(&find_info); |
| 199 if (file_util::HasFileBeenModifiedSince(find_info, cutoff)) | 217 if (file_util::HasFileBeenModifiedSince(find_info, cutoff)) |
| 200 file_util::Delete(path, false); | 218 file_util::Delete(path, false); |
| 201 } | 219 } |
| 202 } | 220 } |
| 203 | 221 |
| 204 void DOMStorageContext::DeleteSessionOnlyData() { | |
| 205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); | |
| 206 | |
| 207 // Make sure that we don't delete a database that's currently being accessed | |
| 208 // by unloading all of the databases temporarily. | |
| 209 PurgeMemory(); | |
| 210 | |
| 211 file_util::FileEnumerator file_enumerator( | |
| 212 data_path_.Append(kLocalStorageDirectory), false, | |
| 213 file_util::FileEnumerator::FILES); | |
| 214 for (FilePath path = file_enumerator.Next(); !path.value().empty(); | |
| 215 path = file_enumerator.Next()) { | |
| 216 GURL origin(WebSecurityOrigin::createFromDatabaseIdentifier( | |
| 217 webkit_glue::FilePathToWebString(path.BaseName())).toString()); | |
| 218 if (!special_storage_policy_->IsStorageSessionOnly(origin)) | |
| 219 continue; | |
| 220 if (special_storage_policy_->IsStorageProtected(origin)) | |
| 221 continue; | |
| 222 | |
| 223 file_util::Delete(path, false); | |
| 224 } | |
| 225 } | |
| 226 | |
| 227 void DOMStorageContext::DeleteLocalStorageFile(const FilePath& file_path) { | 222 void DOMStorageContext::DeleteLocalStorageFile(const FilePath& file_path) { |
| 228 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); | 223 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); |
| 229 | 224 |
| 230 // Make sure that we don't delete a database that's currently being accessed | 225 // Make sure that we don't delete a database that's currently being accessed |
| 231 // by unloading all of the databases temporarily. | 226 // by unloading all of the databases temporarily. |
| 232 // TODO(bulach): both this method and DeleteDataModifiedSince could purge | 227 // TODO(bulach): both this method and DeleteDataModifiedSince could purge |
| 233 // only the memory used by the specific file instead of all memory at once. | 228 // only the memory used by the specific file instead of all memory at once. |
| 234 // See http://crbug.com/32000 | 229 // See http://crbug.com/32000 |
| 235 PurgeMemory(); | 230 PurgeMemory(); |
| 236 file_util::Delete(file_path, false); | 231 file_util::Delete(file_path, false); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 } | 291 } |
| 297 | 292 |
| 298 FilePath DOMStorageContext::GetLocalStorageFilePath( | 293 FilePath DOMStorageContext::GetLocalStorageFilePath( |
| 299 const string16& origin_id) const { | 294 const string16& origin_id) const { |
| 300 FilePath storageDir = data_path_.Append( | 295 FilePath storageDir = data_path_.Append( |
| 301 DOMStorageContext::kLocalStorageDirectory); | 296 DOMStorageContext::kLocalStorageDirectory); |
| 302 FilePath::StringType id = | 297 FilePath::StringType id = |
| 303 webkit_glue::WebStringToFilePathString(origin_id); | 298 webkit_glue::WebStringToFilePathString(origin_id); |
| 304 return storageDir.Append(id.append(kLocalStorageExtension)); | 299 return storageDir.Append(id.append(kLocalStorageExtension)); |
| 305 } | 300 } |
| OLD | NEW |