This source file includes following definitions.
- DatabaseFileNameFromOrigin
- OriginFromDatabaseFileName
- commit_batches_in_flight_
- commit_batches_in_flight_
- ExtractValues
- Length
- Key
- GetItem
- SetItem
- RemoveItem
- Clear
- FastClear
- ShallowCopy
- HasUncommittedChanges
- DeleteOrigin
- PurgeMemory
- Shutdown
- InitialImportIfNeeded
- CreateCommitBatchIfNeeded
- OnCommitTimer
- CommitChanges
- OnCommitComplete
- ShutdownInCommitSequence
#include "content/browser/dom_storage/dom_storage_area.h"
#include "base/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "content/browser/dom_storage/dom_storage_namespace.h"
#include "content/browser/dom_storage/dom_storage_task_runner.h"
#include "content/browser/dom_storage/local_storage_database_adapter.h"
#include "content/browser/dom_storage/session_storage_database.h"
#include "content/browser/dom_storage/session_storage_database_adapter.h"
#include "content/common/dom_storage/dom_storage_map.h"
#include "content/common/dom_storage/dom_storage_types.h"
#include "webkit/browser/database/database_util.h"
#include "webkit/common/database/database_identifier.h"
#include "webkit/common/fileapi/file_system_util.h"
using webkit_database::DatabaseUtil;
namespace content {
static const int kCommitTimerSeconds = 1;
DOMStorageArea::CommitBatch::CommitBatch()
: clear_all_first(false) {
}
DOMStorageArea::CommitBatch::~CommitBatch() {}
const base::FilePath::CharType DOMStorageArea::kDatabaseFileExtension[] =
FILE_PATH_LITERAL(".localstorage");
base::FilePath DOMStorageArea::DatabaseFileNameFromOrigin(const GURL& origin) {
std::string filename = webkit_database::GetIdentifierFromOrigin(origin);
return base::FilePath().Append(kDatabaseFileExtension).
InsertBeforeExtensionASCII(filename);
}
GURL DOMStorageArea::OriginFromDatabaseFileName(const base::FilePath& name) {
DCHECK(name.MatchesExtension(kDatabaseFileExtension));
std::string origin_id =
name.BaseName().RemoveExtension().MaybeAsASCII();
return webkit_database::GetOriginFromIdentifier(origin_id);
}
DOMStorageArea::DOMStorageArea(
const GURL& origin, const base::FilePath& directory,
DOMStorageTaskRunner* task_runner)
: namespace_id_(kLocalStorageNamespaceId), origin_(origin),
directory_(directory),
task_runner_(task_runner),
map_(new DOMStorageMap(kPerStorageAreaQuota +
kPerStorageAreaOverQuotaAllowance)),
is_initial_import_done_(true),
is_shutdown_(false),
commit_batches_in_flight_(0) {
if (!directory.empty()) {
base::FilePath path = directory.Append(DatabaseFileNameFromOrigin(origin_));
backing_.reset(new LocalStorageDatabaseAdapter(path));
is_initial_import_done_ = false;
}
}
DOMStorageArea::DOMStorageArea(
int64 namespace_id,
const std::string& persistent_namespace_id,
const GURL& origin,
SessionStorageDatabase* session_storage_backing,
DOMStorageTaskRunner* task_runner)
: namespace_id_(namespace_id),
persistent_namespace_id_(persistent_namespace_id),
origin_(origin),
task_runner_(task_runner),
map_(new DOMStorageMap(kPerStorageAreaQuota +
kPerStorageAreaOverQuotaAllowance)),
session_storage_backing_(session_storage_backing),
is_initial_import_done_(true),
is_shutdown_(false),
commit_batches_in_flight_(0) {
DCHECK(namespace_id != kLocalStorageNamespaceId);
if (session_storage_backing) {
backing_.reset(new SessionStorageDatabaseAdapter(
session_storage_backing, persistent_namespace_id, origin));
is_initial_import_done_ = false;
}
}
DOMStorageArea::~DOMStorageArea() {
}
void DOMStorageArea::ExtractValues(DOMStorageValuesMap* map) {
if (is_shutdown_)
return;
InitialImportIfNeeded();
map_->ExtractValues(map);
}
unsigned DOMStorageArea::Length() {
if (is_shutdown_)
return 0;
InitialImportIfNeeded();
return map_->Length();
}
base::NullableString16 DOMStorageArea::Key(unsigned index) {
if (is_shutdown_)
return base::NullableString16();
InitialImportIfNeeded();
return map_->Key(index);
}
base::NullableString16 DOMStorageArea::GetItem(const base::string16& key) {
if (is_shutdown_)
return base::NullableString16();
InitialImportIfNeeded();
return map_->GetItem(key);
}
bool DOMStorageArea::SetItem(const base::string16& key,
const base::string16& value,
base::NullableString16* old_value) {
if (is_shutdown_)
return false;
InitialImportIfNeeded();
if (!map_->HasOneRef())
map_ = map_->DeepCopy();
bool success = map_->SetItem(key, value, old_value);
if (success && backing_) {
CommitBatch* commit_batch = CreateCommitBatchIfNeeded();
commit_batch->changed_values[key] = base::NullableString16(value, false);
}
return success;
}
bool DOMStorageArea::RemoveItem(const base::string16& key,
base::string16* old_value) {
if (is_shutdown_)
return false;
InitialImportIfNeeded();
if (!map_->HasOneRef())
map_ = map_->DeepCopy();
bool success = map_->RemoveItem(key, old_value);
if (success && backing_) {
CommitBatch* commit_batch = CreateCommitBatchIfNeeded();
commit_batch->changed_values[key] = base::NullableString16();
}
return success;
}
bool DOMStorageArea::Clear() {
if (is_shutdown_)
return false;
InitialImportIfNeeded();
if (map_->Length() == 0)
return false;
map_ = new DOMStorageMap(kPerStorageAreaQuota +
kPerStorageAreaOverQuotaAllowance);
if (backing_) {
CommitBatch* commit_batch = CreateCommitBatchIfNeeded();
commit_batch->clear_all_first = true;
commit_batch->changed_values.clear();
}
return true;
}
void DOMStorageArea::FastClear() {
if (is_shutdown_)
return;
map_ = new DOMStorageMap(kPerStorageAreaQuota +
kPerStorageAreaOverQuotaAllowance);
is_initial_import_done_ = true;
if (backing_) {
CommitBatch* commit_batch = CreateCommitBatchIfNeeded();
commit_batch->clear_all_first = true;
commit_batch->changed_values.clear();
}
}
DOMStorageArea* DOMStorageArea::ShallowCopy(
int64 destination_namespace_id,
const std::string& destination_persistent_namespace_id) {
DCHECK_NE(kLocalStorageNamespaceId, namespace_id_);
DCHECK_NE(kLocalStorageNamespaceId, destination_namespace_id);
DOMStorageArea* copy = new DOMStorageArea(
destination_namespace_id, destination_persistent_namespace_id, origin_,
session_storage_backing_.get(), task_runner_.get());
copy->map_ = map_;
copy->is_shutdown_ = is_shutdown_;
copy->is_initial_import_done_ = true;
if (commit_batch_)
OnCommitTimer();
return copy;
}
bool DOMStorageArea::HasUncommittedChanges() const {
DCHECK(!is_shutdown_);
return commit_batch_.get() || commit_batches_in_flight_;
}
void DOMStorageArea::DeleteOrigin() {
DCHECK(!is_shutdown_);
DCHECK(!session_storage_backing_.get());
if (HasUncommittedChanges()) {
Clear();
return;
}
map_ = new DOMStorageMap(kPerStorageAreaQuota +
kPerStorageAreaOverQuotaAllowance);
if (backing_) {
is_initial_import_done_ = false;
backing_->Reset();
backing_->DeleteFiles();
}
}
void DOMStorageArea::PurgeMemory() {
DCHECK(!is_shutdown_);
DCHECK(!session_storage_backing_.get());
if (!is_initial_import_done_ ||
!backing_.get() ||
HasUncommittedChanges())
return;
is_initial_import_done_ = false;
map_ = new DOMStorageMap(kPerStorageAreaQuota +
kPerStorageAreaOverQuotaAllowance);
backing_->Reset();
}
void DOMStorageArea::Shutdown() {
DCHECK(!is_shutdown_);
is_shutdown_ = true;
map_ = NULL;
if (!backing_)
return;
bool success = task_runner_->PostShutdownBlockingTask(
FROM_HERE,
DOMStorageTaskRunner::COMMIT_SEQUENCE,
base::Bind(&DOMStorageArea::ShutdownInCommitSequence, this));
DCHECK(success);
}
void DOMStorageArea::InitialImportIfNeeded() {
if (is_initial_import_done_)
return;
DCHECK(backing_.get());
base::TimeTicks before = base::TimeTicks::Now();
DOMStorageValuesMap initial_values;
backing_->ReadAllValues(&initial_values);
map_->SwapValues(&initial_values);
is_initial_import_done_ = true;
base::TimeDelta time_to_import = base::TimeTicks::Now() - before;
UMA_HISTOGRAM_TIMES("LocalStorage.BrowserTimeToPrimeLocalStorage",
time_to_import);
size_t local_storage_size_kb = map_->bytes_used() / 1024;
UMA_HISTOGRAM_CUSTOM_COUNTS("LocalStorage.BrowserLocalStorageSizeInKB",
local_storage_size_kb,
0, 6 * 1024, 50);
if (local_storage_size_kb < 100) {
UMA_HISTOGRAM_TIMES(
"LocalStorage.BrowserTimeToPrimeLocalStorageUnder100KB",
time_to_import);
} else if (local_storage_size_kb < 1000) {
UMA_HISTOGRAM_TIMES(
"LocalStorage.BrowserTimeToPrimeLocalStorage100KBTo1MB",
time_to_import);
} else {
UMA_HISTOGRAM_TIMES(
"LocalStorage.BrowserTimeToPrimeLocalStorage1MBTo5MB",
time_to_import);
}
}
DOMStorageArea::CommitBatch* DOMStorageArea::CreateCommitBatchIfNeeded() {
DCHECK(!is_shutdown_);
if (!commit_batch_) {
commit_batch_.reset(new CommitBatch());
if (!commit_batches_in_flight_) {
task_runner_->PostDelayedTask(
FROM_HERE,
base::Bind(&DOMStorageArea::OnCommitTimer, this),
base::TimeDelta::FromSeconds(kCommitTimerSeconds));
}
}
return commit_batch_.get();
}
void DOMStorageArea::OnCommitTimer() {
if (is_shutdown_)
return;
DCHECK(backing_.get());
if (!commit_batch_)
return;
DCHECK(task_runner_->IsRunningOnPrimarySequence());
bool success = task_runner_->PostShutdownBlockingTask(
FROM_HERE,
DOMStorageTaskRunner::COMMIT_SEQUENCE,
base::Bind(&DOMStorageArea::CommitChanges, this,
base::Owned(commit_batch_.release())));
++commit_batches_in_flight_;
DCHECK(success);
}
void DOMStorageArea::CommitChanges(const CommitBatch* commit_batch) {
DCHECK(task_runner_->IsRunningOnCommitSequence());
backing_->CommitChanges(commit_batch->clear_all_first,
commit_batch->changed_values);
task_runner_->PostTask(
FROM_HERE,
base::Bind(&DOMStorageArea::OnCommitComplete, this));
}
void DOMStorageArea::OnCommitComplete() {
DCHECK(task_runner_->IsRunningOnPrimarySequence());
--commit_batches_in_flight_;
if (is_shutdown_)
return;
if (commit_batch_.get() && !commit_batches_in_flight_) {
task_runner_->PostDelayedTask(
FROM_HERE,
base::Bind(&DOMStorageArea::OnCommitTimer, this),
base::TimeDelta::FromSeconds(kCommitTimerSeconds));
}
}
void DOMStorageArea::ShutdownInCommitSequence() {
DCHECK(task_runner_->IsRunningOnCommitSequence());
DCHECK(backing_.get());
if (commit_batch_) {
bool success = backing_->CommitChanges(
commit_batch_->clear_all_first,
commit_batch_->changed_values);
DCHECK(success);
}
commit_batch_.reset();
backing_.reset();
session_storage_backing_ = NULL;
}
}