This source file includes following definitions.
- DBOperation
- DBOperation
- operation_count_
- ReadAreaValues
- CommitAreaChanges
- CloneNamespace
- DeleteArea
- DeleteNamespace
- ReadNamespacesAndOrigins
- LazyOpen
- TryToOpen
- IsOpen
- CallerErrorCheck
- ConsistencyCheck
- DatabaseErrorCheck
- CreateNamespace
- GetAreasInNamespace
- AddAreaToNamespace
- DeleteAreaHelper
- GetMapForArea
- CreateMapForArea
- ReadMap
- WriteValuesToMap
- GetMapRefCount
- IncreaseMapRefCount
- DecreaseMapRefCount
- ClearMap
- DeepCopyArea
- NamespaceStartKey
- NamespaceKey
- NamespacePrefix
- MapRefCountKey
- MapKey
- NextMapIdKey
#include "content/browser/dom_storage/session_storage_database.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "third_party/leveldatabase/src/include/leveldb/db.h"
#include "third_party/leveldatabase/src/include/leveldb/iterator.h"
#include "third_party/leveldatabase/src/include/leveldb/options.h"
#include "third_party/leveldatabase/src/include/leveldb/status.h"
#include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
#include "url/gurl.h"
namespace {
const char session_storage_uma_name[] = "SessionStorageDatabase.Open";
enum SessionStorageUMA {
SESSION_STORAGE_UMA_SUCCESS,
SESSION_STORAGE_UMA_RECREATED,
SESSION_STORAGE_UMA_FAIL,
SESSION_STORAGE_UMA_MAX
};
}
namespace content {
class SessionStorageDatabase::DBOperation {
public:
DBOperation(SessionStorageDatabase* session_storage_database)
: session_storage_database_(session_storage_database) {
base::AutoLock auto_lock(session_storage_database_->db_lock_);
++session_storage_database_->operation_count_;
}
~DBOperation() {
base::AutoLock auto_lock(session_storage_database_->db_lock_);
--session_storage_database_->operation_count_;
if ((session_storage_database_->is_inconsistent_ ||
session_storage_database_->db_error_) &&
session_storage_database_->operation_count_ == 0 &&
!session_storage_database_->invalid_db_deleted_) {
session_storage_database_->db_.reset();
#if defined(OS_WIN)
leveldb::DestroyDB(
base::WideToUTF8(session_storage_database_->file_path_.value()),
leveldb::Options());
#else
leveldb::DestroyDB(session_storage_database_->file_path_.value(),
leveldb::Options());
#endif
session_storage_database_->invalid_db_deleted_ = true;
}
}
private:
SessionStorageDatabase* session_storage_database_;
};
SessionStorageDatabase::SessionStorageDatabase(const base::FilePath& file_path)
: file_path_(file_path),
db_error_(false),
is_inconsistent_(false),
invalid_db_deleted_(false),
operation_count_(0) {
}
SessionStorageDatabase::~SessionStorageDatabase() {
}
void SessionStorageDatabase::ReadAreaValues(const std::string& namespace_id,
const GURL& origin,
DOMStorageValuesMap* result) {
if (!LazyOpen(false))
return;
DBOperation operation(this);
leveldb::ReadOptions options;
options.snapshot = db_->GetSnapshot();
std::string map_id;
bool exists;
if (GetMapForArea(namespace_id, origin.spec(), options, &exists, &map_id) &&
exists)
ReadMap(map_id, options, result, false);
db_->ReleaseSnapshot(options.snapshot);
}
bool SessionStorageDatabase::CommitAreaChanges(
const std::string& namespace_id,
const GURL& origin,
bool clear_all_first,
const DOMStorageValuesMap& changes) {
if (!LazyOpen(true))
return false;
DBOperation operation(this);
leveldb::WriteBatch batch;
const bool kOkIfExists = true;
if (!CreateNamespace(namespace_id, kOkIfExists, &batch))
return false;
std::string map_id;
bool exists;
if (!GetMapForArea(namespace_id, origin.spec(), leveldb::ReadOptions(),
&exists, &map_id))
return false;
if (exists) {
int64 ref_count;
if (!GetMapRefCount(map_id, &ref_count))
return false;
if (ref_count > 1) {
if (!DeepCopyArea(namespace_id, origin, !clear_all_first,
&map_id, &batch))
return false;
}
else if (clear_all_first) {
if (!ClearMap(map_id, &batch))
return false;
}
} else {
if (!changes.empty()) {
if (!CreateMapForArea(namespace_id, origin, &map_id, &batch))
return false;
}
}
WriteValuesToMap(map_id, changes, &batch);
leveldb::Status s = db_->Write(leveldb::WriteOptions(), &batch);
return DatabaseErrorCheck(s.ok());
}
bool SessionStorageDatabase::CloneNamespace(
const std::string& namespace_id, const std::string& new_namespace_id) {
if (!LazyOpen(true))
return false;
DBOperation operation(this);
leveldb::WriteBatch batch;
const bool kOkIfExists = false;
if (!CreateNamespace(new_namespace_id, kOkIfExists, &batch))
return false;
std::map<std::string, std::string> areas;
if (!GetAreasInNamespace(namespace_id, &areas))
return false;
for (std::map<std::string, std::string>::const_iterator it = areas.begin();
it != areas.end(); ++it) {
const std::string& origin = it->first;
const std::string& map_id = it->second;
if (!IncreaseMapRefCount(map_id, &batch))
return false;
AddAreaToNamespace(new_namespace_id, origin, map_id, &batch);
}
leveldb::Status s = db_->Write(leveldb::WriteOptions(), &batch);
return DatabaseErrorCheck(s.ok());
}
bool SessionStorageDatabase::DeleteArea(const std::string& namespace_id,
const GURL& origin) {
if (!LazyOpen(false)) {
return true;
}
DBOperation operation(this);
leveldb::WriteBatch batch;
if (!DeleteAreaHelper(namespace_id, origin.spec(), &batch))
return false;
leveldb::Status s = db_->Write(leveldb::WriteOptions(), &batch);
return DatabaseErrorCheck(s.ok());
}
bool SessionStorageDatabase::DeleteNamespace(const std::string& namespace_id) {
if (!LazyOpen(false)) {
return true;
}
DBOperation operation(this);
leveldb::WriteBatch batch;
std::map<std::string, std::string> areas;
if (!GetAreasInNamespace(namespace_id, &areas))
return false;
for (std::map<std::string, std::string>::const_iterator it = areas.begin();
it != areas.end(); ++it) {
const std::string& origin = it->first;
if (!DeleteAreaHelper(namespace_id, origin, &batch))
return false;
}
batch.Delete(NamespaceStartKey(namespace_id));
leveldb::Status s = db_->Write(leveldb::WriteOptions(), &batch);
return DatabaseErrorCheck(s.ok());
}
bool SessionStorageDatabase::ReadNamespacesAndOrigins(
std::map<std::string, std::vector<GURL> >* namespaces_and_origins) {
if (!LazyOpen(true))
return false;
DBOperation operation(this);
leveldb::ReadOptions options;
options.snapshot = db_->GetSnapshot();
std::string namespace_prefix = NamespacePrefix();
scoped_ptr<leveldb::Iterator> it(db_->NewIterator(options));
it->Seek(namespace_prefix);
if (!it->Valid()) {
db_->ReleaseSnapshot(options.snapshot);
return true;
}
if (!DatabaseErrorCheck(it->status().ok())) {
db_->ReleaseSnapshot(options.snapshot);
return false;
}
std::string current_namespace_start_key;
std::string current_namespace_id;
for (it->Next(); it->Valid(); it->Next()) {
std::string key = it->key().ToString();
if (key.find(namespace_prefix) != 0) {
break;
}
if (current_namespace_start_key.empty() ||
key.substr(0, current_namespace_start_key.length()) !=
current_namespace_start_key) {
current_namespace_start_key = key;
current_namespace_id =
key.substr(namespace_prefix.length(),
key.length() - namespace_prefix.length() - 1);
namespaces_and_origins->insert(
std::make_pair(current_namespace_id, std::vector<GURL>()));
} else {
std::string origin = key.substr(current_namespace_start_key.length());
(*namespaces_and_origins)[current_namespace_id].push_back(GURL(origin));
}
}
db_->ReleaseSnapshot(options.snapshot);
return true;
}
bool SessionStorageDatabase::LazyOpen(bool create_if_needed) {
base::AutoLock auto_lock(db_lock_);
if (db_error_ || is_inconsistent_) {
return false;
}
if (IsOpen())
return true;
if (!create_if_needed &&
(!base::PathExists(file_path_) || base::IsDirectoryEmpty(file_path_))) {
return false;
}
leveldb::DB* db;
leveldb::Status s = TryToOpen(&db);
if (!s.ok()) {
LOG(WARNING) << "Failed to open leveldb in " << file_path_.value()
<< ", error: " << s.ToString();
DCHECK(db == NULL);
base::DeleteFile(file_path_, true);
s = TryToOpen(&db);
if (!s.ok()) {
LOG(WARNING) << "Failed to open leveldb in " << file_path_.value()
<< ", error: " << s.ToString();
UMA_HISTOGRAM_ENUMERATION(session_storage_uma_name,
SESSION_STORAGE_UMA_FAIL,
SESSION_STORAGE_UMA_MAX);
DCHECK(db == NULL);
db_error_ = true;
return false;
}
UMA_HISTOGRAM_ENUMERATION(session_storage_uma_name,
SESSION_STORAGE_UMA_RECREATED,
SESSION_STORAGE_UMA_MAX);
} else {
UMA_HISTOGRAM_ENUMERATION(session_storage_uma_name,
SESSION_STORAGE_UMA_SUCCESS,
SESSION_STORAGE_UMA_MAX);
}
db_.reset(db);
return true;
}
leveldb::Status SessionStorageDatabase::TryToOpen(leveldb::DB** db) {
leveldb::Options options;
options.max_open_files = 0;
options.create_if_missing = true;
#if defined(OS_WIN)
return leveldb::DB::Open(options, base::WideToUTF8(file_path_.value()), db);
#elif defined(OS_POSIX)
return leveldb::DB::Open(options, file_path_.value(), db);
#endif
}
bool SessionStorageDatabase::IsOpen() const {
return db_.get() != NULL;
}
bool SessionStorageDatabase::CallerErrorCheck(bool ok) const {
DCHECK(ok);
return ok;
}
bool SessionStorageDatabase::ConsistencyCheck(bool ok) {
if (ok)
return true;
base::AutoLock auto_lock(db_lock_);
is_inconsistent_ = true;
return false;
}
bool SessionStorageDatabase::DatabaseErrorCheck(bool ok) {
if (ok)
return true;
base::AutoLock auto_lock(db_lock_);
db_error_ = true;
return false;
}
bool SessionStorageDatabase::CreateNamespace(const std::string& namespace_id,
bool ok_if_exists,
leveldb::WriteBatch* batch) {
leveldb::Slice namespace_prefix = NamespacePrefix();
std::string dummy;
leveldb::Status s = db_->Get(leveldb::ReadOptions(), namespace_prefix,
&dummy);
if (!DatabaseErrorCheck(s.ok() || s.IsNotFound()))
return false;
if (s.IsNotFound())
batch->Put(namespace_prefix, "");
std::string namespace_start_key = NamespaceStartKey(namespace_id);
s = db_->Get(leveldb::ReadOptions(), namespace_start_key, &dummy);
if (!DatabaseErrorCheck(s.ok() || s.IsNotFound()))
return false;
if (s.IsNotFound()) {
batch->Put(namespace_start_key, "");
return true;
}
return CallerErrorCheck(ok_if_exists);
}
bool SessionStorageDatabase::GetAreasInNamespace(
const std::string& namespace_id,
std::map<std::string, std::string>* areas) {
std::string namespace_start_key = NamespaceStartKey(namespace_id);
scoped_ptr<leveldb::Iterator> it(db_->NewIterator(leveldb::ReadOptions()));
it->Seek(namespace_start_key);
if (!it->Valid()) {
return true;
}
if (!DatabaseErrorCheck(it->status().ok()))
return false;
for (it->Next(); it->Valid(); it->Next()) {
std::string key = it->key().ToString();
if (key.find(namespace_start_key) != 0) {
break;
}
std::string origin = key.substr(namespace_start_key.length());
std::string map_id = it->value().ToString();
(*areas)[origin] = map_id;
}
return true;
}
void SessionStorageDatabase::AddAreaToNamespace(const std::string& namespace_id,
const std::string& origin,
const std::string& map_id,
leveldb::WriteBatch* batch) {
std::string namespace_key = NamespaceKey(namespace_id, origin);
batch->Put(namespace_key, map_id);
}
bool SessionStorageDatabase::DeleteAreaHelper(
const std::string& namespace_id,
const std::string& origin,
leveldb::WriteBatch* batch) {
std::string map_id;
bool exists;
if (!GetMapForArea(namespace_id, origin, leveldb::ReadOptions(), &exists,
&map_id))
return false;
if (!exists)
return true;
if (!DecreaseMapRefCount(map_id, 1, batch))
return false;
std::string namespace_key = NamespaceKey(namespace_id, origin);
batch->Delete(namespace_key);
std::string namespace_start_key = NamespaceStartKey(namespace_id);
scoped_ptr<leveldb::Iterator> it(db_->NewIterator(leveldb::ReadOptions()));
it->Seek(namespace_start_key);
if (!ConsistencyCheck(it->Valid()))
return false;
it->Next();
if (!ConsistencyCheck(it->Valid()))
return false;
it->Next();
if (!it->Valid())
return true;
std::string key = it->key().ToString();
if (key.find(namespace_start_key) != 0)
batch->Delete(namespace_start_key);
return true;
}
bool SessionStorageDatabase::GetMapForArea(const std::string& namespace_id,
const std::string& origin,
const leveldb::ReadOptions& options,
bool* exists, std::string* map_id) {
std::string namespace_key = NamespaceKey(namespace_id, origin);
leveldb::Status s = db_->Get(options, namespace_key, map_id);
if (s.IsNotFound()) {
*exists = false;
return true;
}
*exists = true;
return DatabaseErrorCheck(s.ok());
}
bool SessionStorageDatabase::CreateMapForArea(const std::string& namespace_id,
const GURL& origin,
std::string* map_id,
leveldb::WriteBatch* batch) {
leveldb::Slice next_map_id_key = NextMapIdKey();
leveldb::Status s = db_->Get(leveldb::ReadOptions(), next_map_id_key, map_id);
if (!DatabaseErrorCheck(s.ok() || s.IsNotFound()))
return false;
int64 next_map_id = 0;
if (s.IsNotFound()) {
*map_id = "0";
} else {
bool conversion_ok = base::StringToInt64(*map_id, &next_map_id);
if (!ConsistencyCheck(conversion_ok))
return false;
}
batch->Put(next_map_id_key, base::Int64ToString(++next_map_id));
std::string namespace_key = NamespaceKey(namespace_id, origin.spec());
batch->Put(namespace_key, *map_id);
batch->Put(MapRefCountKey(*map_id), "1");
return true;
}
bool SessionStorageDatabase::ReadMap(const std::string& map_id,
const leveldb::ReadOptions& options,
DOMStorageValuesMap* result,
bool only_keys) {
scoped_ptr<leveldb::Iterator> it(db_->NewIterator(options));
std::string map_start_key = MapRefCountKey(map_id);
it->Seek(map_start_key);
if (!ConsistencyCheck(it->Valid()))
return false;
if (!DatabaseErrorCheck(it->status().ok()))
return false;
for (it->Next(); it->Valid(); it->Next()) {
std::string key = it->key().ToString();
if (key.find(map_start_key) != 0) {
break;
}
base::string16 key16 =
base::UTF8ToUTF16(key.substr(map_start_key.length()));
if (only_keys) {
(*result)[key16] = base::NullableString16();
} else {
size_t len = it->value().size() / sizeof(base::char16);
const base::char16* data_ptr =
reinterpret_cast<const base::char16*>(it->value().data());
(*result)[key16] =
base::NullableString16(base::string16(data_ptr, len), false);
}
}
return true;
}
void SessionStorageDatabase::WriteValuesToMap(const std::string& map_id,
const DOMStorageValuesMap& values,
leveldb::WriteBatch* batch) {
for (DOMStorageValuesMap::const_iterator it = values.begin();
it != values.end();
++it) {
base::NullableString16 value = it->second;
std::string key = MapKey(map_id, base::UTF16ToUTF8(it->first));
if (value.is_null()) {
batch->Delete(key);
} else {
const char* data = reinterpret_cast<const char*>(value.string().data());
size_t size = value.string().size() * 2;
batch->Put(key, leveldb::Slice(data, size));
}
}
}
bool SessionStorageDatabase::GetMapRefCount(const std::string& map_id,
int64* ref_count) {
std::string ref_count_string;
leveldb::Status s = db_->Get(leveldb::ReadOptions(),
MapRefCountKey(map_id), &ref_count_string);
if (!ConsistencyCheck(s.ok()))
return false;
bool conversion_ok = base::StringToInt64(ref_count_string, ref_count);
return ConsistencyCheck(conversion_ok);
}
bool SessionStorageDatabase::IncreaseMapRefCount(const std::string& map_id,
leveldb::WriteBatch* batch) {
int64 old_ref_count;
if (!GetMapRefCount(map_id, &old_ref_count))
return false;
batch->Put(MapRefCountKey(map_id), base::Int64ToString(++old_ref_count));
return true;
}
bool SessionStorageDatabase::DecreaseMapRefCount(const std::string& map_id,
int decrease,
leveldb::WriteBatch* batch) {
int64 ref_count;
if (!GetMapRefCount(map_id, &ref_count))
return false;
if (!ConsistencyCheck(decrease <= ref_count))
return false;
ref_count -= decrease;
if (ref_count > 0) {
batch->Put(MapRefCountKey(map_id), base::Int64ToString(ref_count));
} else {
if (!ClearMap(map_id, batch))
return false;
batch->Delete(MapRefCountKey(map_id));
}
return true;
}
bool SessionStorageDatabase::ClearMap(const std::string& map_id,
leveldb::WriteBatch* batch) {
DOMStorageValuesMap values;
if (!ReadMap(map_id, leveldb::ReadOptions(), &values, true))
return false;
for (DOMStorageValuesMap::const_iterator it = values.begin();
it != values.end(); ++it)
batch->Delete(MapKey(map_id, base::UTF16ToUTF8(it->first)));
return true;
}
bool SessionStorageDatabase::DeepCopyArea(
const std::string& namespace_id, const GURL& origin, bool copy_data,
std::string* map_id, leveldb::WriteBatch* batch) {
DOMStorageValuesMap values;
if (copy_data && !ReadMap(*map_id, leveldb::ReadOptions(), &values, false))
return false;
if (!DecreaseMapRefCount(*map_id, 1, batch))
return false;
if (!CreateMapForArea(namespace_id, origin, map_id, batch))
return false;
WriteValuesToMap(*map_id, values, batch);
return true;
}
std::string SessionStorageDatabase::NamespaceStartKey(
const std::string& namespace_id) {
return base::StringPrintf("namespace-%s-", namespace_id.c_str());
}
std::string SessionStorageDatabase::NamespaceKey(
const std::string& namespace_id, const std::string& origin) {
return base::StringPrintf("namespace-%s-%s", namespace_id.c_str(),
origin.c_str());
}
const char* SessionStorageDatabase::NamespacePrefix() {
return "namespace-";
}
std::string SessionStorageDatabase::MapRefCountKey(const std::string& map_id) {
return base::StringPrintf("map-%s-", map_id.c_str());
}
std::string SessionStorageDatabase::MapKey(const std::string& map_id,
const std::string& key) {
return base::StringPrintf("map-%s-%s", map_id.c_str(), key.c_str());
}
const char* SessionStorageDatabase::NextMapIdKey() {
return "next-map-id";
}
}