Index: content/browser/dom_storage/dom_storage_context_impl_new.cc |
=================================================================== |
--- content/browser/dom_storage/dom_storage_context_impl_new.cc (revision 127025) |
+++ content/browser/dom_storage/dom_storage_context_impl_new.cc (working copy) |
@@ -2,318 +2,173 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "content/browser/in_process_webkit/dom_storage_context_impl.h" |
+#include "content/browser/dom_storage/dom_storage_context_impl_new.h" |
-#include <algorithm> |
+#ifdef ENABLE_NEW_DOM_STORAGE_BACKEND |
#include "base/bind.h" |
-#include "base/file_path.h" |
-#include "base/file_util.h" |
-#include "base/string_util.h" |
-#include "content/browser/in_process_webkit/dom_storage_area.h" |
-#include "content/browser/in_process_webkit/dom_storage_namespace.h" |
-#include "content/common/dom_storage_common.h" |
+#include "base/bind_helpers.h" |
+#include "base/message_loop_proxy.h" |
#include "content/public/browser/browser_thread.h" |
-#include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" |
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
+#include "webkit/database/database_util.h" |
+#include "webkit/dom_storage/dom_storage_area.h" |
+#include "webkit/dom_storage/dom_storage_context.h" |
+#include "webkit/dom_storage/dom_storage_task_runner.h" |
#include "webkit/glue/webkit_glue.h" |
-#include "webkit/quota/special_storage_policy.h" |
using content::BrowserThread; |
using content::DOMStorageContext; |
-using WebKit::WebSecurityOrigin; |
+using dom_storage::DomStorageArea; |
+using dom_storage::DomStorageContext; |
+using dom_storage::DomStorageWorkerPoolTaskRunner; |
+using webkit_database::DatabaseUtil; |
-const FilePath::CharType DOMStorageContextImpl::kLocalStorageDirectory[] = |
- FILE_PATH_LITERAL("Local Storage"); |
- |
-const FilePath::CharType DOMStorageContextImpl::kLocalStorageExtension[] = |
- FILE_PATH_LITERAL(".localstorage"); |
- |
namespace { |
-void ClearLocalState(const FilePath& domstorage_path, |
- quota::SpecialStoragePolicy* special_storage_policy, |
- bool clear_all_databases) { |
- file_util::FileEnumerator file_enumerator( |
- domstorage_path, false, file_util::FileEnumerator::FILES); |
- for (FilePath file_path = file_enumerator.Next(); !file_path.empty(); |
- file_path = file_enumerator.Next()) { |
- if (file_path.Extension() == |
- DOMStorageContextImpl::kLocalStorageExtension) { |
- GURL origin(WebSecurityOrigin::createFromDatabaseIdentifier( |
- webkit_glue::FilePathToWebString(file_path.BaseName())).toString()); |
- if (special_storage_policy && |
- special_storage_policy->IsStorageProtected(origin)) |
- continue; |
- if (!clear_all_databases && |
- !special_storage_policy->IsStorageSessionOnly(origin)) { |
- continue; |
- } |
- file_util::Delete(file_path, false); |
- } |
- } |
-} |
+const char kLocalStorageDirectory[] = "Local Storage"; |
-} // namespace |
+// TODO(michaeln): Fix the content layer api, FilePaths and |
+// string16 origin_ids are just wrong. Then get rid of |
+// this conversion non-sense. Most of the includes are just |
+// to support that non-sense. |
-DOMStorageContextImpl::DOMStorageContextImpl( |
- const FilePath& data_path, |
- quota::SpecialStoragePolicy* special_storage_policy) |
- : last_storage_area_id_(0), |
- last_session_storage_namespace_id_on_ui_thread_(kLocalStorageNamespaceId), |
- last_session_storage_namespace_id_on_io_thread_(kLocalStorageNamespaceId), |
- clear_local_state_on_exit_(false), |
- save_session_state_(false), |
- special_storage_policy_(special_storage_policy), |
- webkit_message_loop_( |
- BrowserThread::GetMessageLoopProxyForThread( |
- BrowserThread::WEBKIT_DEPRECATED)) { |
- data_path_ = data_path; |
+GURL OriginIdToGURL(const string16& origin_id) { |
+ return DatabaseUtil::GetOriginFromIdentifier(origin_id); |
} |
-DOMStorageContextImpl::~DOMStorageContextImpl() { |
- // This should not go away until all DOM Storage message filters have gone |
- // away. And they remove themselves from this list. |
- DCHECK(message_filter_set_.empty()); |
- |
- for (StorageNamespaceMap::iterator iter(storage_namespace_map_.begin()); |
- iter != storage_namespace_map_.end(); ++iter) { |
- delete iter->second; |
- } |
- |
- if (save_session_state_) |
- return; |
- |
- bool has_session_only_databases = |
- special_storage_policy_.get() && |
- special_storage_policy_->HasSessionOnlyOrigins(); |
- |
- // Clearning only session-only databases, and there are none. |
- if (!clear_local_state_on_exit_ && !has_session_only_databases) |
- return; |
- |
- // Not being on the WEBKIT thread here means we are running in a unit test |
- // where no clean up is needed. |
- if (BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)) { |
- ClearLocalState(data_path_.Append(kLocalStorageDirectory), |
- special_storage_policy_, |
- clear_local_state_on_exit_); |
- } |
+FilePath OriginToFullFilePath(const FilePath& directory, |
+ const GURL& origin) { |
+ return directory.Append(DomStorageArea::DatabaseFileNameFromOrigin(origin)); |
} |
-int64 DOMStorageContextImpl::AllocateStorageAreaId() { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); |
- return ++last_storage_area_id_; |
+GURL FilePathToOrigin(const FilePath& path) { |
+ DCHECK(path.MatchesExtension(DomStorageArea::kDatabaseFileExtension)); |
+ return OriginIdToGURL( |
+ webkit_glue::FilePathToWebString(path.BaseName().RemoveExtension())); |
} |
-int64 DOMStorageContextImpl::AllocateSessionStorageNamespaceId() { |
- if (BrowserThread::CurrentlyOn(BrowserThread::UI)) |
- return ++last_session_storage_namespace_id_on_ui_thread_; |
- return --last_session_storage_namespace_id_on_io_thread_; |
+void InvokeAllStorageFilesCallbackHelper( |
+ const DOMStorageContext::GetAllStorageFilesCallback& callback, |
+ const std::vector<FilePath>& file_paths) { |
+ callback.Run(file_paths); |
} |
-int64 DOMStorageContextImpl::CloneSessionStorage(int64 original_id) { |
- DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); |
- int64 clone_id = AllocateSessionStorageNamespaceId(); |
- BrowserThread::PostTask( |
- BrowserThread::WEBKIT_DEPRECATED, FROM_HERE, |
- base::Bind(&DOMStorageContextImpl::CompleteCloningSessionStorage, |
- this, original_id, clone_id)); |
- return clone_id; |
-} |
+void GetAllStorageFilesHelper( |
+ base::MessageLoopProxy* reply_loop, |
+ DomStorageContext* context, |
+ const DOMStorageContext::GetAllStorageFilesCallback& callback) { |
+ std::vector<DomStorageContext::UsageInfo> infos; |
+ context->GetUsageInfo(&infos); |
-void DOMStorageContextImpl::RegisterStorageArea(DOMStorageArea* storage_area) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); |
- int64 id = storage_area->id(); |
- DCHECK(!GetStorageArea(id)); |
- storage_area_map_[id] = storage_area; |
-} |
+ std::vector<FilePath> paths; |
+ for (size_t i = 0; i < infos.size(); ++i) { |
+ paths.push_back( |
+ OriginToFullFilePath(context->directory(), infos[i].origin)); |
+ } |
-void DOMStorageContextImpl::UnregisterStorageArea( |
- DOMStorageArea* storage_area) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); |
- int64 id = storage_area->id(); |
- DCHECK(GetStorageArea(id)); |
- storage_area_map_.erase(id); |
+ reply_loop->PostTask( |
+ FROM_HERE, |
+ base::Bind(&InvokeAllStorageFilesCallbackHelper, |
+ callback, paths)); |
} |
-DOMStorageArea* DOMStorageContextImpl::GetStorageArea(int64 id) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); |
- StorageAreaMap::iterator iter = storage_area_map_.find(id); |
- if (iter == storage_area_map_.end()) |
- return NULL; |
- return iter->second; |
} |
-void DOMStorageContextImpl::DeleteSessionStorageNamespace(int64 namespace_id) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED) || |
- !BrowserThread::IsMessageLoopValid(BrowserThread::WEBKIT_DEPRECATED)); |
- StorageNamespaceMap::iterator iter = |
- storage_namespace_map_.find(namespace_id); |
- if (iter == storage_namespace_map_.end()) |
- return; |
- DCHECK(iter->second->dom_storage_type() == DOM_STORAGE_SESSION); |
- delete iter->second; |
- storage_namespace_map_.erase(iter); |
+DOMStorageContextImpl::DOMStorageContextImpl( |
+ const FilePath& data_path, |
+ quota::SpecialStoragePolicy* special_storage_policy) { |
+ base::SequencedWorkerPool* worker_pool = BrowserThread::GetBlockingPool(); |
+ context_ = new dom_storage::DomStorageContext( |
+ data_path.empty() ? |
+ data_path : data_path.AppendASCII(kLocalStorageDirectory), |
+ special_storage_policy, |
+ new DomStorageWorkerPoolTaskRunner( |
+ worker_pool, |
+ worker_pool->GetNamedSequenceToken("dom_storage"), |
+ BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))); |
} |
-DOMStorageNamespace* DOMStorageContextImpl::GetStorageNamespace( |
- int64 id, bool allocation_allowed) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); |
- StorageNamespaceMap::iterator iter = storage_namespace_map_.find(id); |
- if (iter != storage_namespace_map_.end()) |
- return iter->second; |
- if (!allocation_allowed) |
- return NULL; |
- if (id == kLocalStorageNamespaceId) |
- return CreateLocalStorage(); |
- return CreateSessionStorage(id); |
+DOMStorageContextImpl::~DOMStorageContextImpl() { |
} |
-void DOMStorageContextImpl::RegisterMessageFilter( |
- DOMStorageMessageFilter* message_filter) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
- DCHECK(message_filter_set_.find(message_filter) == |
- message_filter_set_.end()); |
- message_filter_set_.insert(message_filter); |
+void DOMStorageContextImpl::GetAllStorageFiles( |
+ const GetAllStorageFilesCallback& callback) { |
+ DCHECK(context_); |
+ context_->task_runner()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&GetAllStorageFilesHelper, |
+ base::MessageLoopProxy::current(), |
+ context_, callback)); |
} |
-void DOMStorageContextImpl::UnregisterMessageFilter( |
- DOMStorageMessageFilter* message_filter) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
- DCHECK(message_filter_set_.find(message_filter) != |
- message_filter_set_.end()); |
- message_filter_set_.erase(message_filter); |
+FilePath DOMStorageContextImpl::GetFilePath(const string16& origin_id) const { |
+ DCHECK(context_); |
+ return OriginToFullFilePath(context_->directory(), OriginIdToGURL(origin_id)); |
} |
-const DOMStorageContextImpl::MessageFilterSet* |
-DOMStorageContextImpl::GetMessageFilterSet() const { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
- return &message_filter_set_; |
+void DOMStorageContextImpl::DeleteForOrigin(const string16& origin_id) { |
+ DCHECK(context_); |
+ context_->task_runner()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&DomStorageContext::DeleteOrigin, context_, |
+ OriginIdToGURL(origin_id))); |
} |
-void DOMStorageContextImpl::PurgeMemory() { |
- // It is only safe to purge the memory from the LocalStorage namespace, |
- // because it is backed by disk and can be reloaded later. If we purge a |
- // SessionStorage namespace, its data will be gone forever, because it isn't |
- // currently backed by disk. |
- DOMStorageNamespace* local_storage = |
- GetStorageNamespace(kLocalStorageNamespaceId, false); |
- if (local_storage) |
- local_storage->PurgeMemory(); |
+void DOMStorageContextImpl::DeleteLocalStorageFile(const FilePath& file_path) { |
+ DCHECK(context_); |
+ context_->task_runner()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&DomStorageContext::DeleteOrigin, context_, |
+ FilePathToOrigin(file_path))); |
} |
void DOMStorageContextImpl::DeleteDataModifiedSince(const base::Time& cutoff) { |
- // Make sure that we don't delete a database that's currently being accessed |
- // by unloading all of the databases temporarily. |
- PurgeMemory(); |
- |
- file_util::FileEnumerator file_enumerator( |
- data_path_.Append(kLocalStorageDirectory), false, |
- file_util::FileEnumerator::FILES); |
- for (FilePath path = file_enumerator.Next(); !path.value().empty(); |
- path = file_enumerator.Next()) { |
- GURL origin(WebSecurityOrigin::createFromDatabaseIdentifier( |
- webkit_glue::FilePathToWebString(path.BaseName())).toString()); |
- if (special_storage_policy_->IsStorageProtected(origin)) |
- continue; |
- |
- file_util::FileEnumerator::FindInfo find_info; |
- file_enumerator.GetFindInfo(&find_info); |
- if (file_util::HasFileBeenModifiedSince(find_info, cutoff)) |
- file_util::Delete(path, false); |
- } |
+ DCHECK(context_); |
+ context_->task_runner()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&DomStorageContext::DeleteDataModifiedSince, context_, |
+ cutoff)); |
} |
-void DOMStorageContextImpl::DeleteLocalStorageFile(const FilePath& file_path) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); |
- |
- // Make sure that we don't delete a database that's currently being accessed |
- // by unloading all of the databases temporarily. |
- // TODO(bulach): both this method and DeleteDataModifiedSince could purge |
- // only the memory used by the specific file instead of all memory at once. |
- // See http://crbug.com/32000 |
- PurgeMemory(); |
- file_util::Delete(file_path, false); |
+void DOMStorageContextImpl::PurgeMemory() { |
+ DCHECK(context_); |
+ context_->task_runner()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&DomStorageContext::PurgeMemory, context_)); |
} |
-void DOMStorageContextImpl::DeleteForOrigin(const string16& origin_id) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); |
- DeleteLocalStorageFile(GetFilePath(origin_id)); |
+void DOMStorageContextImpl::SetClearLocalState(bool clear_local_state) { |
+ DCHECK(context_); |
+ context_->task_runner()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&DomStorageContext::SetClearLocalState, context_, |
+ clear_local_state)); |
} |
-void DOMStorageContextImpl::DeleteAllLocalStorageFiles() { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); |
- |
- // Make sure that we don't delete a database that's currently being accessed |
- // by unloading all of the databases temporarily. |
- PurgeMemory(); |
- |
- file_util::FileEnumerator file_enumerator( |
- data_path_.Append(kLocalStorageDirectory), false, |
- file_util::FileEnumerator::FILES); |
- for (FilePath file_path = file_enumerator.Next(); !file_path.empty(); |
- file_path = file_enumerator.Next()) { |
- if (file_path.Extension() == kLocalStorageExtension) |
- file_util::Delete(file_path, false); |
- } |
+void DOMStorageContextImpl::SaveSessionState() { |
+ DCHECK(context_); |
+ context_->task_runner()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&DomStorageContext::SaveSessionState, context_)); |
} |
-DOMStorageNamespace* DOMStorageContextImpl::CreateLocalStorage() { |
- FilePath dir_path; |
- if (!data_path_.empty()) |
- dir_path = data_path_.Append(kLocalStorageDirectory); |
- DOMStorageNamespace* new_namespace = |
- DOMStorageNamespace::CreateLocalStorageNamespace(this, dir_path); |
- RegisterStorageNamespace(new_namespace); |
- return new_namespace; |
+void DOMStorageContextImpl::Shutdown() { |
+ DCHECK(context_); |
+ context_->task_runner()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&DomStorageContext::Shutdown, context_)); |
} |
-DOMStorageNamespace* DOMStorageContextImpl::CreateSessionStorage( |
- int64 namespace_id) { |
- DOMStorageNamespace* new_namespace = |
- DOMStorageNamespace::CreateSessionStorageNamespace(this, namespace_id); |
- RegisterStorageNamespace(new_namespace); |
- return new_namespace; |
+int64 DOMStorageContextImpl::LeakyCloneSessionStorage( |
+ int64 existing_namespace_id) { |
+ DCHECK(context_); |
+ int64 clone_id = context_->AllocateSessionId(); |
+ context_->task_runner()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&DomStorageContext::CloneSessionNamespace, context_, |
+ existing_namespace_id, clone_id)); |
+ return clone_id; |
} |
-void DOMStorageContextImpl::RegisterStorageNamespace( |
- DOMStorageNamespace* storage_namespace) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); |
- int64 id = storage_namespace->id(); |
- DCHECK(!GetStorageNamespace(id, false)); |
- storage_namespace_map_[id] = storage_namespace; |
-} |
- |
-void DOMStorageContextImpl::CompleteCloningSessionStorage( |
- int64 existing_id, int64 clone_id) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); |
- DOMStorageNamespace* existing_namespace = |
- GetStorageNamespace(existing_id, false); |
- // If nothing exists, then there's nothing to clone. |
- if (existing_namespace) |
- RegisterStorageNamespace(existing_namespace->Copy(clone_id)); |
-} |
- |
-base::SequencedTaskRunner* DOMStorageContextImpl::task_runner() const { |
- return webkit_message_loop_; |
-} |
- |
-std::vector<FilePath> DOMStorageContextImpl::GetAllStorageFiles() { |
- std::vector<FilePath> files; |
- file_util::FileEnumerator file_enumerator( |
- data_path_.Append(kLocalStorageDirectory), false, |
- file_util::FileEnumerator::FILES); |
- for (FilePath file_path = file_enumerator.Next(); !file_path.empty(); |
- file_path = file_enumerator.Next()) { |
- if (file_path.Extension() == kLocalStorageExtension) |
- files.push_back(file_path); |
- } |
- return files; |
-} |
- |
-FilePath DOMStorageContextImpl::GetFilePath(const string16& origin_id) const { |
- FilePath storage_dir = data_path_.Append(kLocalStorageDirectory); |
- FilePath::StringType id = webkit_glue::WebStringToFilePathString(origin_id); |
- return storage_dir.Append(id.append(kLocalStorageExtension)); |
-} |
+#endif // ENABLE_NEW_DOM_STORAGE_BACKEND |