Commit 89e4a67b authored by Victor Costan's avatar Victor Costan Committed by Chromium LUCI CQ

WebSQL: Replace off_the_record with incognito.

The majority of the storage code uses incognito instead of
off_the_record. This CL gets WebSQL in line, to reduce cognitive
overhead for readers.

Change-Id: I8fcce58f82da24e22aa7b14b201dc7ed935b1114
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2634551
Auto-Submit: Victor Costan <pwnall@chromium.org>
Reviewed-by: default avatarJoshua Bell <jsbell@chromium.org>
Commit-Queue: Joshua Bell <jsbell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#844845}
parent 0c1b242b
......@@ -115,8 +115,8 @@ void WebDatabaseHostImpl::OpenFileValidated(const base::string16& vfs_file_name,
std::string origin_identifier;
base::string16 database_name;
// When in OffTheRecord mode, we want to make sure that all DB files are
// removed when the OffTheRecord browser context goes away, so we add the
// When in Incognito mode, we want to make sure that all DB files are
// removed when the Incognito browser context goes away, so we add the
// SQLITE_OPEN_DELETEONCLOSE flag when opening all files, and keep
// open handles to them in the database tracker to make sure they're
// around for as long as needed.
......@@ -130,14 +130,14 @@ void WebDatabaseHostImpl::OpenFileValidated(const base::string16& vfs_file_name,
base::FilePath db_file = DatabaseUtil::GetFullFilePathForVfsFile(
db_tracker_.get(), vfs_file_name);
if (!db_file.empty()) {
if (db_tracker_->IsOffTheRecordProfile()) {
tracked_file = db_tracker_->GetOffTheRecordFile(vfs_file_name);
if (db_tracker_->IsIncognitoProfile()) {
tracked_file = db_tracker_->GetIncognitoFile(vfs_file_name);
if (!tracked_file) {
file = VfsBackend::OpenFile(
db_file, desired_flags | SQLITE_OPEN_DELETEONCLOSE);
if (!(desired_flags & SQLITE_OPEN_DELETEONCLOSE)) {
tracked_file = db_tracker_->SaveOffTheRecordFile(vfs_file_name,
std::move(file));
tracked_file =
db_tracker_->SaveIncognitoFile(vfs_file_name, std::move(file));
}
}
} else {
......@@ -287,20 +287,20 @@ void WebDatabaseHostImpl::DatabaseDeleteFile(
base::FilePath db_file =
DatabaseUtil::GetFullFilePathForVfsFile(db_tracker_.get(), vfs_file_name);
if (!db_file.empty()) {
// In order to delete a journal file in OffTheRecord mode, we only need to
// In order to delete a journal file in Incognito mode, we only need to
// close the open handle to it that's stored in the database tracker.
if (db_tracker_->IsOffTheRecordProfile()) {
if (db_tracker_->IsIncognitoProfile()) {
const base::string16 wal_suffix(base::ASCIIToUTF16("-wal"));
base::string16 sqlite_suffix;
// WAL files can be deleted without having previously been opened.
if (!db_tracker_->HasSavedOffTheRecordFileHandle(vfs_file_name) &&
if (!db_tracker_->HasSavedIncognitoFileHandle(vfs_file_name) &&
DatabaseUtil::CrackVfsFileName(vfs_file_name, nullptr, nullptr,
&sqlite_suffix) &&
sqlite_suffix == wal_suffix) {
error_code = SQLITE_OK;
} else {
db_tracker_->CloseOffTheRecordFileHandle(vfs_file_name);
db_tracker_->CloseIncognitoFileHandle(vfs_file_name);
error_code = SQLITE_OK;
}
} else {
......
......@@ -34,7 +34,7 @@ namespace storage {
const base::FilePath::CharType kDatabaseDirectoryName[] =
FILE_PATH_LITERAL("databases");
const base::FilePath::CharType kOffTheRecordDatabaseDirectoryName[] =
const base::FilePath::CharType kIncognitoDatabaseDirectoryName[] =
FILE_PATH_LITERAL("databases-off-the-record");
const base::FilePath::CharType kTrackerDatabaseFileName[] =
FILE_PATH_LITERAL("Databases.db");
......@@ -86,13 +86,13 @@ OriginInfo::OriginInfo(const std::string& origin_identifier, int64_t total_size)
: origin_identifier_(origin_identifier), total_size_(total_size) {}
DatabaseTracker::DatabaseTracker(const base::FilePath& profile_path,
bool is_off_the_record,
bool is_incognito,
SpecialStoragePolicy* special_storage_policy,
QuotaManagerProxy* quota_manager_proxy)
: is_off_the_record_(is_off_the_record),
: is_incognito_(is_incognito),
profile_path_(profile_path),
db_dir_(is_off_the_record_
? profile_path_.Append(kOffTheRecordDatabaseDirectoryName)
db_dir_(is_incognito_
? profile_path_.Append(kIncognitoDatabaseDirectoryName)
: profile_path_.Append(kDatabaseDirectoryName)),
db_(new sql::Database()),
special_storage_policy_(special_storage_policy),
......@@ -260,7 +260,7 @@ void DatabaseTracker::CloseTrackerDatabaseAndClearCaches() {
DCHECK(task_runner_->RunsTasksInCurrentSequence());
ClearAllCachedOriginInfo();
if (!is_off_the_record_) {
if (!is_incognito_) {
meta_table_.reset(nullptr);
databases_table_.reset(nullptr);
db_->Close();
......@@ -274,16 +274,16 @@ base::FilePath DatabaseTracker::GetOriginDirectory(
base::string16 origin_directory;
if (!is_off_the_record_) {
if (!is_incognito_) {
origin_directory = base::UTF8ToUTF16(origin_identifier);
} else {
auto it = off_the_record_origin_directories_.find(origin_identifier);
if (it != off_the_record_origin_directories_.end()) {
auto it = incognito_origin_directories_.find(origin_identifier);
if (it != incognito_origin_directories_.end()) {
origin_directory = it->second;
} else {
origin_directory = base::NumberToString16(
off_the_record_origin_directories_generator_++);
off_the_record_origin_directories_[origin_identifier] = origin_directory;
origin_directory =
base::NumberToString16(incognito_origin_directories_generator_++);
incognito_origin_directories_[origin_identifier] = origin_directory;
}
}
......@@ -428,18 +428,18 @@ bool DatabaseTracker::DeleteOrigin(const std::string& origin_identifier,
base::DeletePathRecursively(origin_dir);
base::DeletePathRecursively(new_origin_dir); // Might fail on windows.
if (is_off_the_record_) {
off_the_record_origin_directories_.erase(origin_identifier);
if (is_incognito_) {
incognito_origin_directories_.erase(origin_identifier);
// TODO(jsbell): Consider alternate data structures to avoid this
// linear scan.
for (auto it = off_the_record_file_handles_.begin();
it != off_the_record_file_handles_.end();) {
for (auto it = incognito_file_handles_.begin();
it != incognito_file_handles_.end();) {
std::string id;
if (DatabaseUtil::CrackVfsFileName(it->first, &id, nullptr, nullptr) &&
id == origin_identifier) {
delete it->second;
it = off_the_record_file_handles_.erase(it);
it = incognito_file_handles_.erase(it);
} else {
++it;
}
......@@ -508,10 +508,9 @@ bool DatabaseTracker::LazyInit() {
databases_table_.reset(new DatabasesTable(db_.get()));
meta_table_.reset(new sql::MetaTable());
is_initialized_ =
base::CreateDirectory(db_dir_) &&
is_initialized_ = base::CreateDirectory(db_dir_) &&
(db_->is_open() ||
(is_off_the_record_ ? db_->OpenInMemory()
(is_incognito_ ? db_->OpenInMemory()
: db_->Open(kTrackerDatabaseFullPath))) &&
UpgradeToCurrentVersion();
if (!is_initialized_) {
......@@ -789,64 +788,64 @@ int DatabaseTracker::DeleteDataForOrigin(const url::Origin& origin,
return net::OK;
}
const base::File* DatabaseTracker::GetOffTheRecordFile(
const base::File* DatabaseTracker::GetIncognitoFile(
const base::string16& vfs_file_name) const {
DCHECK(task_runner_->RunsTasksInCurrentSequence());
DCHECK(is_off_the_record_);
auto it = off_the_record_file_handles_.find(vfs_file_name);
if (it != off_the_record_file_handles_.end())
DCHECK(is_incognito_);
auto it = incognito_file_handles_.find(vfs_file_name);
if (it != incognito_file_handles_.end())
return it->second;
return nullptr;
}
const base::File* DatabaseTracker::SaveOffTheRecordFile(
const base::File* DatabaseTracker::SaveIncognitoFile(
const base::string16& vfs_file_name,
base::File file) {
DCHECK(task_runner_->RunsTasksInCurrentSequence());
DCHECK(is_off_the_record_);
DCHECK(is_incognito_);
if (!file.IsValid())
return nullptr;
base::File* to_insert = new base::File(std::move(file));
auto rv = off_the_record_file_handles_.insert(
std::make_pair(vfs_file_name, to_insert));
auto rv =
incognito_file_handles_.insert(std::make_pair(vfs_file_name, to_insert));
DCHECK(rv.second);
return rv.first->second;
}
void DatabaseTracker::CloseOffTheRecordFileHandle(
void DatabaseTracker::CloseIncognitoFileHandle(
const base::string16& vfs_file_name) {
DCHECK(task_runner_->RunsTasksInCurrentSequence());
DCHECK(is_off_the_record_);
DCHECK(off_the_record_file_handles_.find(vfs_file_name) !=
off_the_record_file_handles_.end());
DCHECK(is_incognito_);
DCHECK(incognito_file_handles_.find(vfs_file_name) !=
incognito_file_handles_.end());
auto it = off_the_record_file_handles_.find(vfs_file_name);
if (it != off_the_record_file_handles_.end()) {
auto it = incognito_file_handles_.find(vfs_file_name);
if (it != incognito_file_handles_.end()) {
delete it->second;
off_the_record_file_handles_.erase(it);
incognito_file_handles_.erase(it);
}
}
bool DatabaseTracker::HasSavedOffTheRecordFileHandle(
bool DatabaseTracker::HasSavedIncognitoFileHandle(
const base::string16& vfs_file_name) const {
DCHECK(task_runner_->RunsTasksInCurrentSequence());
return (off_the_record_file_handles_.find(vfs_file_name) !=
off_the_record_file_handles_.end());
return (incognito_file_handles_.find(vfs_file_name) !=
incognito_file_handles_.end());
}
void DatabaseTracker::DeleteOffTheRecordDBDirectory() {
void DatabaseTracker::DeleteIncognitoDBDirectory() {
DCHECK(task_runner_->RunsTasksInCurrentSequence());
is_initialized_ = false;
for (auto& pair : off_the_record_file_handles_)
for (auto& pair : incognito_file_handles_)
delete pair.second;
base::FilePath off_the_record_db_dir =
profile_path_.Append(kOffTheRecordDatabaseDirectoryName);
if (base::DirectoryExists(off_the_record_db_dir))
base::DeletePathRecursively(off_the_record_db_dir);
base::FilePath incognito_db_dir =
profile_path_.Append(kIncognitoDatabaseDirectoryName);
if (base::DirectoryExists(incognito_db_dir))
base::DeletePathRecursively(incognito_db_dir);
}
void DatabaseTracker::ClearSessionOnlyOrigins() {
......@@ -894,8 +893,8 @@ void DatabaseTracker::Shutdown() {
return;
}
shutting_down_ = true;
if (is_off_the_record_)
DeleteOffTheRecordDBDirectory();
if (is_incognito_)
DeleteIncognitoDBDirectory();
else if (!force_keep_session_state_)
ClearSessionOnlyOrigins();
CloseTrackerDatabaseAndClearCaches();
......
......@@ -98,7 +98,7 @@ class COMPONENT_EXPORT(STORAGE_BROWSER) DatabaseTracker
};
DatabaseTracker(const base::FilePath& profile_path,
bool is_off_the_record,
bool is_incognito,
SpecialStoragePolicy* special_storage_policy,
QuotaManagerProxy* quota_manager_proxy);
......@@ -164,18 +164,16 @@ class COMPONENT_EXPORT(STORAGE_BROWSER) DatabaseTracker
virtual int DeleteDataForOrigin(const url::Origin& origin,
net::CompletionOnceCallback callback);
bool IsOffTheRecordProfile() const { return is_off_the_record_; }
bool IsIncognitoProfile() const { return is_incognito_; }
const base::File* GetOffTheRecordFile(
const base::string16& vfs_file_path) const;
const base::File* SaveOffTheRecordFile(const base::string16& vfs_file_path,
const base::File* GetIncognitoFile(const base::string16& vfs_file_path) const;
const base::File* SaveIncognitoFile(const base::string16& vfs_file_path,
base::File file);
void CloseOffTheRecordFileHandle(const base::string16& vfs_file_path);
bool HasSavedOffTheRecordFileHandle(
const base::string16& vfs_file_path) const;
void CloseIncognitoFileHandle(const base::string16& vfs_file_path);
bool HasSavedIncognitoFileHandle(const base::string16& vfs_file_path) const;
// Shutdown the database tracker, deleting database files if the tracker is
// used for an OffTheRecord profile.
// used for an Incognito profile.
void Shutdown();
// Disables the exit-time deletion of session-only data.
void SetForceKeepSessionState();
......@@ -219,9 +217,9 @@ class COMPONENT_EXPORT(STORAGE_BROWSER) DatabaseTracker
// virtual for unit-testing only.
virtual ~DatabaseTracker();
// Deletes the directory that stores all DBs in OffTheRecord mode, if it
// Deletes the directory that stores all DBs in Incognito mode, if it
// exists.
void DeleteOffTheRecordDBDirectory();
void DeleteIncognitoDBDirectory();
// Deletes session-only databases. Blocks databases from being created/opened.
void ClearSessionOnlyOrigins();
......@@ -279,7 +277,7 @@ class COMPONENT_EXPORT(STORAGE_BROWSER) DatabaseTracker
base::FilePath GetOriginDirectory(const std::string& origin_identifier);
bool is_initialized_ = false;
const bool is_off_the_record_;
const bool is_incognito_;
bool force_keep_session_state_ = false;
bool shutting_down_ = false;
const base::FilePath profile_path_;
......@@ -312,20 +310,20 @@ class COMPONENT_EXPORT(STORAGE_BROWSER) DatabaseTracker
// The database tracker thread we're supposed to run file IO on.
scoped_refptr<base::SequencedTaskRunner> task_runner_;
// When in OffTheRecord mode, store a DELETE_ON_CLOSE handle to each
// main DB and journal file that was accessed. When the OffTheRecord profile
// When in Incognito mode, store a DELETE_ON_CLOSE handle to each
// main DB and journal file that was accessed. When the Incognito profile
// goes away (or when the browser crashes), all these handles will be
// closed, and the files will be deleted.
std::map<base::string16, base::File*> off_the_record_file_handles_;
std::map<base::string16, base::File*> incognito_file_handles_;
// In a non-OffTheRecord profile, all DBs in an origin are stored in a
// directory named after the origin. In an OffTheRecord profile though, we do
// In a non-Incognito profile, all DBs in an origin are stored in a
// directory named after the origin. In an Incognito profile though, we do
// not want the directory structure to reveal the origins visited by the user
// (in case the browser process crashes and those directories are not
// deleted). So we use this map to assign directory names that do not reveal
// this information.
std::map<std::string, base::string16> off_the_record_origin_directories_;
int off_the_record_origin_directories_generator_ = 0;
std::map<std::string, base::string16> incognito_origin_directories_;
int incognito_origin_directories_generator_ = 0;
FRIEND_TEST_ALL_PREFIXES(DatabaseTracker, TestHelper);
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment