2010-11-17 18:28:32 +01:00
|
|
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#include "libcef/dom_storage_context.h"
|
2010-11-17 18:28:32 +01:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#include "libcef/cef_thread.h"
|
|
|
|
#include "libcef/dom_storage_area.h"
|
|
|
|
#include "libcef/dom_storage_namespace.h"
|
|
|
|
|
2011-10-20 22:34:13 +02:00
|
|
|
#include "base/bind.h"
|
2010-11-17 18:28:32 +01:00
|
|
|
#include "base/file_path.h"
|
|
|
|
#include "base/file_util.h"
|
|
|
|
#include "base/string_util.h"
|
2011-02-15 19:07:24 +01:00
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h"
|
2011-10-06 15:34:47 +02:00
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebStorageNamespace.h"
|
2011-12-16 15:51:10 +01:00
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
|
2011-10-06 15:34:47 +02:00
|
|
|
#include "webkit/database/database_util.h"
|
2010-11-17 18:28:32 +01:00
|
|
|
#include "webkit/glue/webkit_glue.h"
|
|
|
|
|
|
|
|
const FilePath::CharType DOMStorageContext::kLocalStorageDirectory[] =
|
|
|
|
FILE_PATH_LITERAL("Local Storage");
|
|
|
|
|
|
|
|
const FilePath::CharType DOMStorageContext::kLocalStorageExtension[] =
|
|
|
|
FILE_PATH_LITERAL(".localstorage");
|
|
|
|
|
2011-10-06 15:34:47 +02:00
|
|
|
// Use WebStorageNamespace quota sizes as the default.
|
|
|
|
unsigned int DOMStorageContext::local_storage_quota_ =
|
|
|
|
WebKit::WebStorageNamespace::m_localStorageQuota;
|
|
|
|
unsigned int DOMStorageContext::session_storage_quota_ =
|
|
|
|
WebKit::WebStorageNamespace::m_sessionStorageQuota;
|
|
|
|
|
|
|
|
DOMStorageContext::DOMStorageContext(const FilePath& local_storage_path)
|
|
|
|
: local_storage_path_(local_storage_path),
|
|
|
|
last_storage_area_id_(0),
|
2010-11-17 18:28:32 +01:00
|
|
|
last_session_storage_namespace_id_on_ui_thread_(kLocalStorageNamespaceId),
|
2012-01-10 00:46:23 +01:00
|
|
|
last_session_storage_namespace_id_on_io_thread_(
|
|
|
|
kLocalStorageNamespaceId) {
|
2010-11-17 18:28:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DOMStorageContext::~DOMStorageContext() {
|
|
|
|
for (StorageNamespaceMap::iterator iter(storage_namespace_map_.begin());
|
|
|
|
iter != storage_namespace_map_.end(); ++iter) {
|
|
|
|
delete iter->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int64 DOMStorageContext::AllocateStorageAreaId() {
|
|
|
|
DCHECK(CefThread::CurrentlyOn(CefThread::UI));
|
|
|
|
return ++last_storage_area_id_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64 DOMStorageContext::AllocateSessionStorageNamespaceId() {
|
|
|
|
if (CefThread::CurrentlyOn(CefThread::UI))
|
|
|
|
return ++last_session_storage_namespace_id_on_ui_thread_;
|
|
|
|
return --last_session_storage_namespace_id_on_io_thread_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64 DOMStorageContext::CloneSessionStorage(int64 original_id) {
|
|
|
|
DCHECK(!CefThread::CurrentlyOn(CefThread::UI));
|
|
|
|
int64 clone_id = AllocateSessionStorageNamespaceId();
|
|
|
|
CefThread::PostTask(
|
2011-10-20 22:34:13 +02:00
|
|
|
CefThread::UI, FROM_HERE,
|
|
|
|
base::Bind(&DOMStorageContext::CompleteCloningSessionStorage, this,
|
|
|
|
original_id, clone_id));
|
2010-11-17 18:28:32 +01:00
|
|
|
return clone_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DOMStorageContext::RegisterStorageArea(DOMStorageArea* storage_area) {
|
|
|
|
DCHECK(CefThread::CurrentlyOn(CefThread::UI));
|
|
|
|
int64 id = storage_area->id();
|
|
|
|
DCHECK(!GetStorageArea(id));
|
|
|
|
storage_area_map_[id] = storage_area;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DOMStorageContext::UnregisterStorageArea(DOMStorageArea* storage_area) {
|
|
|
|
DCHECK(CefThread::CurrentlyOn(CefThread::UI));
|
|
|
|
int64 id = storage_area->id();
|
|
|
|
DCHECK(GetStorageArea(id));
|
|
|
|
storage_area_map_.erase(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
DOMStorageArea* DOMStorageContext::GetStorageArea(int64 id) {
|
|
|
|
DCHECK(CefThread::CurrentlyOn(CefThread::UI));
|
|
|
|
StorageAreaMap::iterator iter = storage_area_map_.find(id);
|
|
|
|
if (iter == storage_area_map_.end())
|
|
|
|
return NULL;
|
|
|
|
return iter->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DOMStorageContext::DeleteSessionStorageNamespace(int64 namespace_id) {
|
|
|
|
DCHECK(CefThread::CurrentlyOn(CefThread::UI));
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-11-02 20:50:00 +01:00
|
|
|
void DOMStorageContext::DeleteLocalStorageNamespace() {
|
|
|
|
DCHECK(CefThread::CurrentlyOn(CefThread::UI));
|
|
|
|
StorageNamespaceMap::iterator iter =
|
|
|
|
storage_namespace_map_.find(kLocalStorageNamespaceId);
|
|
|
|
if (iter == storage_namespace_map_.end())
|
|
|
|
return;
|
|
|
|
DCHECK(iter->second->dom_storage_type() == DOM_STORAGE_LOCAL);
|
|
|
|
delete iter->second;
|
|
|
|
storage_namespace_map_.erase(iter);
|
|
|
|
}
|
|
|
|
|
2010-11-17 18:28:32 +01:00
|
|
|
DOMStorageNamespace* DOMStorageContext::GetStorageNamespace(
|
|
|
|
int64 id, bool allocation_allowed) {
|
|
|
|
DCHECK(CefThread::CurrentlyOn(CefThread::UI));
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-10-06 15:34:47 +02:00
|
|
|
DOMStorageArea* DOMStorageContext::GetStorageArea(int64 namespace_id,
|
|
|
|
const string16& origin, bool allocation_allowed) {
|
|
|
|
DCHECK(CefThread::CurrentlyOn(CefThread::UI));
|
|
|
|
DOMStorageNamespace* ns =
|
|
|
|
GetStorageNamespace(namespace_id, allocation_allowed);
|
|
|
|
if (ns)
|
|
|
|
return ns->GetStorageArea(origin, allocation_allowed);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DOMStorageContext::PurgeMemory(int64 namespace_id) {
|
|
|
|
DOMStorageNamespace* ns = GetStorageNamespace(namespace_id, false);
|
|
|
|
if (ns)
|
|
|
|
ns->PurgeMemory();
|
2010-11-17 18:28:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DOMStorageContext::DeleteDataModifiedSince(
|
|
|
|
const base::Time& cutoff,
|
|
|
|
const char* url_scheme_to_be_skipped,
|
|
|
|
const std::vector<string16>& protected_origins) {
|
|
|
|
// Make sure that we don't delete a database that's currently being accessed
|
|
|
|
// by unloading all of the databases temporarily.
|
2011-10-06 15:34:47 +02:00
|
|
|
PurgeMemory(kLocalStorageNamespaceId);
|
|
|
|
|
|
|
|
if (local_storage_path_.empty())
|
|
|
|
return;
|
2010-11-17 18:28:32 +01:00
|
|
|
|
|
|
|
file_util::FileEnumerator file_enumerator(
|
2011-10-06 15:34:47 +02:00
|
|
|
local_storage_path_.Append(kLocalStorageDirectory), false,
|
2010-11-17 18:28:32 +01:00
|
|
|
file_util::FileEnumerator::FILES);
|
|
|
|
for (FilePath path = file_enumerator.Next(); !path.value().empty();
|
|
|
|
path = file_enumerator.Next()) {
|
|
|
|
WebKit::WebSecurityOrigin web_security_origin =
|
|
|
|
WebKit::WebSecurityOrigin::createFromDatabaseIdentifier(
|
|
|
|
webkit_glue::FilePathToWebString(path.BaseName()));
|
|
|
|
if (EqualsASCII(web_security_origin.protocol(), url_scheme_to_be_skipped))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::vector<string16>::const_iterator find_iter =
|
|
|
|
std::find(protected_origins.begin(), protected_origins.end(),
|
|
|
|
web_security_origin.databaseIdentifier());
|
|
|
|
if (find_iter != protected_origins.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
file_util::FileEnumerator::FindInfo find_info;
|
|
|
|
file_enumerator.GetFindInfo(&find_info);
|
|
|
|
if (file_util::HasFileBeenModifiedSince(find_info, cutoff))
|
|
|
|
file_util::Delete(path, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-06 15:34:47 +02:00
|
|
|
void DOMStorageContext::DeleteLocalStorageForOrigin(const string16& origin) {
|
2010-11-17 18:28:32 +01:00
|
|
|
DCHECK(CefThread::CurrentlyOn(CefThread::UI));
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-10-06 15:34:47 +02:00
|
|
|
DOMStorageArea* area =
|
|
|
|
GetStorageArea(kLocalStorageNamespaceId, origin, false);
|
|
|
|
if (!area)
|
|
|
|
return;
|
2010-11-17 18:28:32 +01:00
|
|
|
|
2011-10-06 15:34:47 +02:00
|
|
|
// Calling Clear() is necessary to remove the data from the namespace.
|
|
|
|
area->Clear();
|
|
|
|
area->PurgeMemory();
|
2010-11-17 18:28:32 +01:00
|
|
|
|
2011-10-06 15:34:47 +02:00
|
|
|
if (local_storage_path_.empty())
|
|
|
|
return;
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-10-06 15:34:47 +02:00
|
|
|
FilePath file_path = GetLocalStorageFilePath(origin);
|
|
|
|
if (!file_path.empty())
|
|
|
|
file_util::Delete(file_path, false);
|
2010-11-17 18:28:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DOMStorageContext::DeleteAllLocalStorageFiles() {
|
|
|
|
DCHECK(CefThread::CurrentlyOn(CefThread::UI));
|
|
|
|
|
|
|
|
// Make sure that we don't delete a database that's currently being accessed
|
|
|
|
// by unloading all of the databases temporarily.
|
2011-10-06 15:34:47 +02:00
|
|
|
PurgeMemory(kLocalStorageNamespaceId);
|
|
|
|
|
|
|
|
if (local_storage_path_.empty())
|
|
|
|
return;
|
2010-11-17 18:28:32 +01:00
|
|
|
|
|
|
|
file_util::FileEnumerator file_enumerator(
|
2011-10-06 15:34:47 +02:00
|
|
|
local_storage_path_.Append(kLocalStorageDirectory), false,
|
2010-11-17 18:28:32 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-02 20:50:00 +01:00
|
|
|
void DOMStorageContext::SetLocalStoragePath(
|
|
|
|
const FilePath& local_storage_path) {
|
|
|
|
DCHECK(CefThread::CurrentlyOn(CefThread::UI));
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-11-02 20:50:00 +01:00
|
|
|
if ((local_storage_path.empty() && local_storage_path_.empty()) ||
|
|
|
|
local_storage_path == local_storage_path_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Make sure that we don't swap out a database that's currently being accessed
|
|
|
|
// by unloading all of the databases temporarily.
|
|
|
|
PurgeMemory(kLocalStorageNamespaceId);
|
|
|
|
|
|
|
|
// Delete the current namespace, if any. It will be recreated using the new
|
|
|
|
// path when needed.
|
|
|
|
DeleteLocalStorageNamespace();
|
|
|
|
|
|
|
|
local_storage_path_ = local_storage_path;
|
|
|
|
}
|
|
|
|
|
2010-11-17 18:28:32 +01:00
|
|
|
DOMStorageNamespace* DOMStorageContext::CreateLocalStorage() {
|
|
|
|
FilePath dir_path;
|
2011-10-06 15:34:47 +02:00
|
|
|
if (!local_storage_path_.empty())
|
|
|
|
dir_path = local_storage_path_.Append(kLocalStorageDirectory);
|
2010-11-17 18:28:32 +01:00
|
|
|
DOMStorageNamespace* new_namespace =
|
|
|
|
DOMStorageNamespace::CreateLocalStorageNamespace(this, dir_path);
|
|
|
|
RegisterStorageNamespace(new_namespace);
|
|
|
|
return new_namespace;
|
|
|
|
}
|
|
|
|
|
|
|
|
DOMStorageNamespace* DOMStorageContext::CreateSessionStorage(
|
|
|
|
int64 namespace_id) {
|
|
|
|
DOMStorageNamespace* new_namespace =
|
|
|
|
DOMStorageNamespace::CreateSessionStorageNamespace(this, namespace_id);
|
|
|
|
RegisterStorageNamespace(new_namespace);
|
|
|
|
return new_namespace;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DOMStorageContext::RegisterStorageNamespace(
|
|
|
|
DOMStorageNamespace* storage_namespace) {
|
|
|
|
DCHECK(CefThread::CurrentlyOn(CefThread::UI));
|
|
|
|
int64 id = storage_namespace->id();
|
|
|
|
DCHECK(!GetStorageNamespace(id, false));
|
|
|
|
storage_namespace_map_[id] = storage_namespace;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
void DOMStorageContext::CompleteCloningSessionStorage(
|
|
|
|
DOMStorageContext* context, int64 existing_id, int64 clone_id) {
|
|
|
|
DCHECK(CefThread::CurrentlyOn(CefThread::UI));
|
|
|
|
DOMStorageNamespace* existing_namespace =
|
|
|
|
context->GetStorageNamespace(existing_id, false);
|
|
|
|
// If nothing exists, then there's nothing to clone.
|
|
|
|
if (existing_namespace)
|
|
|
|
context->RegisterStorageNamespace(existing_namespace->Copy(clone_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void DOMStorageContext::ClearLocalState(const FilePath& profile_path,
|
|
|
|
const char* url_scheme_to_be_skipped) {
|
|
|
|
file_util::FileEnumerator file_enumerator(profile_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) {
|
|
|
|
WebKit::WebSecurityOrigin web_security_origin =
|
|
|
|
WebKit::WebSecurityOrigin::createFromDatabaseIdentifier(
|
|
|
|
webkit_glue::FilePathToWebString(file_path.BaseName()));
|
|
|
|
if (!EqualsASCII(web_security_origin.protocol(),
|
|
|
|
url_scheme_to_be_skipped))
|
|
|
|
file_util::Delete(file_path, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FilePath DOMStorageContext::GetLocalStorageFilePath(
|
2011-10-06 15:34:47 +02:00
|
|
|
const string16& origin) const {
|
|
|
|
DCHECK(!local_storage_path_.empty());
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-10-06 15:34:47 +02:00
|
|
|
string16 origin_id =
|
|
|
|
webkit_database::DatabaseUtil::GetOriginIdentifier(GURL(origin));
|
|
|
|
|
|
|
|
FilePath storageDir = local_storage_path_.Append(
|
2010-11-17 18:28:32 +01:00
|
|
|
DOMStorageContext::kLocalStorageDirectory);
|
|
|
|
FilePath::StringType id =
|
|
|
|
webkit_glue::WebStringToFilePathString(origin_id);
|
|
|
|
return storageDir.Append(id.append(kLocalStorageExtension));
|
|
|
|
}
|