Commit 2ada406e authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Misc. cleanup for components/webdata/.

The biggest change here is removing the idea of load success callbacks, which
are never used.

Bug: none
Change-Id: I31c1790e2c7e3bb1c3275327893ddee32533c808
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1962610
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Reviewed-by: default avatarCait Phillips <caitkp@chromium.org>
Cr-Commit-Position: refs/heads/master@{#727016}
parent 6dd69717
...@@ -4,21 +4,10 @@ ...@@ -4,21 +4,10 @@
#include "components/webdata/common/web_data_service_base.h" #include "components/webdata/common/web_data_service_base.h"
#include "base/bind.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/stl_util.h"
#include "base/threading/thread.h" #include "base/threading/thread.h"
#include "components/webdata/common/web_database_service.h" #include "components/webdata/common/web_database_service.h"
////////////////////////////////////////////////////////////////////////////////
//
// WebDataServiceBase implementation.
//
////////////////////////////////////////////////////////////////////////////////
using base::Bind;
using base::Time;
WebDataServiceBase::WebDataServiceBase( WebDataServiceBase::WebDataServiceBase(
scoped_refptr<WebDatabaseService> wdbs, scoped_refptr<WebDatabaseService> wdbs,
const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner) const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner)
...@@ -34,34 +23,17 @@ void WebDataServiceBase::Init(ProfileErrorCallback callback) { ...@@ -34,34 +23,17 @@ void WebDataServiceBase::Init(ProfileErrorCallback callback) {
} }
void WebDataServiceBase::ShutdownDatabase() { void WebDataServiceBase::ShutdownDatabase() {
if (!wdbs_) if (wdbs_)
return; wdbs_->ShutdownDatabase();
wdbs_->ShutdownDatabase();
} }
void WebDataServiceBase::CancelRequest(Handle h) { void WebDataServiceBase::CancelRequest(Handle h) {
if (!wdbs_) if (wdbs_)
return; wdbs_->CancelRequest(h);
wdbs_->CancelRequest(h);
}
bool WebDataServiceBase::IsDatabaseLoaded() {
if (!wdbs_)
return false;
return wdbs_->db_loaded();
}
void WebDataServiceBase::RegisterDBLoadedCallback(DBLoadedCallback callback) {
if (!wdbs_)
return;
wdbs_->RegisterDBLoadedCallback(std::move(callback));
} }
WebDatabase* WebDataServiceBase::GetDatabase() { WebDatabase* WebDataServiceBase::GetDatabase() {
if (!wdbs_) return wdbs_ ? wdbs_->GetDatabaseOnDB() : nullptr;
return nullptr;
return wdbs_->GetDatabaseOnDB();
} }
WebDataServiceBase::~WebDataServiceBase() { WebDataServiceBase::~WebDataServiceBase() = default;
}
...@@ -36,8 +36,6 @@ class WEBDATA_EXPORT WebDataServiceBase ...@@ -36,8 +36,6 @@ class WEBDATA_EXPORT WebDataServiceBase
using ProfileErrorCallback = using ProfileErrorCallback =
base::OnceCallback<void(sql::InitStatus, const std::string&)>; base::OnceCallback<void(sql::InitStatus, const std::string&)>;
using DBLoadedCallback = base::OnceClosure;
// |callback| will only be invoked on error, and only if // |callback| will only be invoked on error, and only if
// |callback.is_null()| evaluates to false. // |callback.is_null()| evaluates to false.
// //
...@@ -65,20 +63,8 @@ class WEBDATA_EXPORT WebDataServiceBase ...@@ -65,20 +63,8 @@ class WEBDATA_EXPORT WebDataServiceBase
// Unloads the database and shuts down service. // Unloads the database and shuts down service.
void ShutdownDatabase(); void ShutdownDatabase();
// Register a callback to be notified that the database has loaded. Multiple
// callbacks may be registered, and each will be called at most once
// (following a successful database load), then cleared.
// Note: if the database load is already complete, then the callback will NOT
// be stored or called.
virtual void RegisterDBLoadedCallback(DBLoadedCallback callback);
// Returns true if the database load has completetd successfully, and
// ShutdownOnUISequence() has not yet been called.
virtual bool IsDatabaseLoaded();
// Returns a pointer to the DB (used by SyncableServices). May return NULL if // Returns a pointer to the DB (used by SyncableServices). May return NULL if
// the database is not loaded or otherwise unavailable. Must be called on DB // the database is unavailable. Must be called on DB sequence.
// sequence.
virtual WebDatabase* GetDatabase(); virtual WebDatabase* GetDatabase();
protected: protected:
......
...@@ -14,20 +14,13 @@ ...@@ -14,20 +14,13 @@
#include "components/webdata/common/web_database_table.h" #include "components/webdata/common/web_database_table.h"
#include "sql/error_delegate_util.h" #include "sql/error_delegate_util.h"
using base::Bind;
using base::FilePath;
WebDatabaseBackend::WebDatabaseBackend( WebDatabaseBackend::WebDatabaseBackend(
const FilePath& path, const base::FilePath& path,
Delegate* delegate, std::unique_ptr<Delegate> delegate,
const scoped_refptr<base::SingleThreadTaskRunner>& db_thread) const scoped_refptr<base::SingleThreadTaskRunner>& db_thread)
: base::RefCountedDeleteOnSequence<WebDatabaseBackend>(db_thread), : base::RefCountedDeleteOnSequence<WebDatabaseBackend>(db_thread),
db_path_(path), db_path_(path),
request_manager_(new WebDataRequestManager()), delegate_(std::move(delegate)) {}
init_status_(sql::INIT_FAILURE),
init_complete_(false),
catastrophic_error_occurred_(false),
delegate_(delegate) {}
void WebDatabaseBackend::AddTable(std::unique_ptr<WebDatabaseTable> table) { void WebDatabaseBackend::AddTable(std::unique_ptr<WebDatabaseTable> table) {
DCHECK(!db_); DCHECK(!db_);
...@@ -36,15 +29,14 @@ void WebDatabaseBackend::AddTable(std::unique_ptr<WebDatabaseTable> table) { ...@@ -36,15 +29,14 @@ void WebDatabaseBackend::AddTable(std::unique_ptr<WebDatabaseTable> table) {
void WebDatabaseBackend::InitDatabase() { void WebDatabaseBackend::InitDatabase() {
LoadDatabaseIfNecessary(); LoadDatabaseIfNecessary();
if (delegate_) { if (delegate_)
delegate_->DBLoaded(init_status_, diagnostics_); delegate_->DBLoaded(init_status_, diagnostics_);
}
} }
void WebDatabaseBackend::ShutdownDatabase() { void WebDatabaseBackend::ShutdownDatabase() {
if (db_ && init_status_ == sql::INIT_OK) if (db_ && init_status_ == sql::INIT_OK)
db_->CommitTransaction(); db_->CommitTransaction();
db_.reset(nullptr); db_.reset();
init_complete_ = true; // Ensures the init sequence is not re-run. init_complete_ = true; // Ensures the init sequence is not re-run.
init_status_ = sql::INIT_FAILURE; init_status_ = sql::INIT_FAILURE;
} }
...@@ -81,10 +73,8 @@ void WebDatabaseBackend::DBReadTaskWrapper( ...@@ -81,10 +73,8 @@ void WebDatabaseBackend::DBReadTaskWrapper(
std::unique_ptr<WDTypedResult> WebDatabaseBackend::ExecuteReadTask( std::unique_ptr<WDTypedResult> WebDatabaseBackend::ExecuteReadTask(
WebDatabaseService::ReadTask task) { WebDatabaseService::ReadTask task) {
LoadDatabaseIfNecessary(); LoadDatabaseIfNecessary();
if (db_ && init_status_ == sql::INIT_OK) { return (db_ && init_status_ == sql::INIT_OK) ? std::move(task).Run(db_.get())
return std::move(task).Run(db_.get()); : nullptr;
}
return nullptr;
} }
WebDatabaseBackend::~WebDatabaseBackend() { WebDatabaseBackend::~WebDatabaseBackend() {
...@@ -96,7 +86,7 @@ void WebDatabaseBackend::LoadDatabaseIfNecessary() { ...@@ -96,7 +86,7 @@ void WebDatabaseBackend::LoadDatabaseIfNecessary() {
return; return;
init_complete_ = true; init_complete_ = true;
db_.reset(new WebDatabase()); db_ = std::make_unique<WebDatabase>();
for (const auto& table : tables_) for (const auto& table : tables_)
db_->AddTable(table.get()); db_->AddTable(table.get());
...@@ -109,17 +99,13 @@ void WebDatabaseBackend::LoadDatabaseIfNecessary() { ...@@ -109,17 +99,13 @@ void WebDatabaseBackend::LoadDatabaseIfNecessary() {
init_status_ = db_->Init(db_path_); init_status_ = db_->Init(db_path_);
if (init_status_ != sql::INIT_OK) { if (init_status_ != sql::INIT_OK) {
LOG(ERROR) << "Cannot initialize the web database: " << init_status_;
db_.reset(); db_.reset();
return; return;
} }
// A catastrophic error might have happened and recovered. // A catastrophic error might have happened and recovered.
if (catastrophic_error_occurred_) { if (catastrophic_error_occurred_)
init_status_ = sql::INIT_OK_WITH_DATA_LOSS; init_status_ = sql::INIT_OK_WITH_DATA_LOSS;
LOG(WARNING)
<< "Webdata recovered from a catastrophic error. Data loss possible.";
}
db_->BeginTransaction(); db_->BeginTransaction();
} }
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/memory/ref_counted_delete_on_sequence.h" #include "base/memory/ref_counted_delete_on_sequence.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "components/webdata/common/web_data_request_manager.h"
#include "components/webdata/common/web_database_service.h" #include "components/webdata/common/web_database_service.h"
#include "components/webdata/common/webdata_export.h" #include "components/webdata/common/webdata_export.h"
...@@ -44,7 +45,7 @@ class WEBDATA_EXPORT WebDatabaseBackend ...@@ -44,7 +45,7 @@ class WEBDATA_EXPORT WebDatabaseBackend
WebDatabaseBackend( WebDatabaseBackend(
const base::FilePath& path, const base::FilePath& path,
Delegate* delegate, std::unique_ptr<Delegate> delegate,
const scoped_refptr<base::SingleThreadTaskRunner>& db_thread); const scoped_refptr<base::SingleThreadTaskRunner>& db_thread);
// Must call only before InitDatabaseWithCallback. // Must call only before InitDatabaseWithCallback.
...@@ -111,19 +112,20 @@ class WEBDATA_EXPORT WebDatabaseBackend ...@@ -111,19 +112,20 @@ class WEBDATA_EXPORT WebDatabaseBackend
std::unique_ptr<WebDatabase> db_; std::unique_ptr<WebDatabase> db_;
// Keeps track of all pending requests made to the db. // Keeps track of all pending requests made to the db.
scoped_refptr<WebDataRequestManager> request_manager_; scoped_refptr<WebDataRequestManager> request_manager_ =
base::MakeRefCounted<WebDataRequestManager>();
// State of database initialization. Used to prevent the executing of tasks // State of database initialization. Used to prevent the executing of tasks
// before the db is ready. // before the db is ready.
sql::InitStatus init_status_; sql::InitStatus init_status_ = sql::INIT_FAILURE;
// True if an attempt has been made to load the database (even if the attempt // True if an attempt has been made to load the database (even if the attempt
// fails), used to avoid continually trying to reinit if the db init fails. // fails), used to avoid continually trying to reinit if the db init fails.
bool init_complete_; bool init_complete_ = false;
// True if a catastrophic database error occurs and further error callbacks // True if a catastrophic database error occurs and further error callbacks
// from the database should be ignored. // from the database should be ignored.
bool catastrophic_error_occurred_; bool catastrophic_error_occurred_ = false;
// If a catastrophic database error has occurred, this contains any available // If a catastrophic database error has occurred, this contains any available
// diagnostic information. // diagnostic information.
......
...@@ -15,9 +15,6 @@ ...@@ -15,9 +15,6 @@
#include "components/webdata/common/web_data_service_consumer.h" #include "components/webdata/common/web_data_service_consumer.h"
#include "components/webdata/common/web_database_backend.h" #include "components/webdata/common/web_database_backend.h"
using base::Bind;
using base::FilePath;
// Receives messages from the backend on the DB sequence, posts them to // Receives messages from the backend on the DB sequence, posts them to
// WebDatabaseService on the UI sequence. // WebDatabaseService on the UI sequence.
class WebDatabaseService::BackendDelegate class WebDatabaseService::BackendDelegate
...@@ -44,19 +41,18 @@ WebDatabaseService::WebDatabaseService( ...@@ -44,19 +41,18 @@ WebDatabaseService::WebDatabaseService(
scoped_refptr<base::SingleThreadTaskRunner> db_task_runner) scoped_refptr<base::SingleThreadTaskRunner> db_task_runner)
: base::RefCountedDeleteOnSequence<WebDatabaseService>(ui_task_runner), : base::RefCountedDeleteOnSequence<WebDatabaseService>(ui_task_runner),
path_(path), path_(path),
db_loaded_(false),
db_task_runner_(db_task_runner) { db_task_runner_(db_task_runner) {
DCHECK(ui_task_runner->RunsTasksInCurrentSequence()); DCHECK(ui_task_runner->RunsTasksInCurrentSequence());
DCHECK(db_task_runner_); DCHECK(db_task_runner_);
} }
WebDatabaseService::~WebDatabaseService() { WebDatabaseService::~WebDatabaseService() = default;
}
void WebDatabaseService::AddTable(std::unique_ptr<WebDatabaseTable> table) { void WebDatabaseService::AddTable(std::unique_ptr<WebDatabaseTable> table) {
if (!web_db_backend_) { if (!web_db_backend_) {
web_db_backend_ = new WebDatabaseBackend( web_db_backend_ = base::MakeRefCounted<WebDatabaseBackend>(
path_, new BackendDelegate(weak_ptr_factory_.GetWeakPtr()), path_,
std::make_unique<BackendDelegate>(weak_ptr_factory_.GetWeakPtr()),
db_task_runner_); db_task_runner_);
} }
web_db_backend_->AddTable(std::move(table)); web_db_backend_->AddTable(std::move(table));
...@@ -69,8 +65,6 @@ void WebDatabaseService::LoadDatabase() { ...@@ -69,8 +65,6 @@ void WebDatabaseService::LoadDatabase() {
} }
void WebDatabaseService::ShutdownDatabase() { void WebDatabaseService::ShutdownDatabase() {
db_loaded_ = false;
loaded_callbacks_.clear();
error_callbacks_.clear(); error_callbacks_.clear();
weak_ptr_factory_.InvalidateWeakPtrs(); weak_ptr_factory_.InvalidateWeakPtrs();
if (!web_db_backend_) if (!web_db_backend_)
...@@ -117,13 +111,8 @@ WebDataServiceBase::Handle WebDatabaseService::ScheduleDBTaskWithResult( ...@@ -117,13 +111,8 @@ WebDataServiceBase::Handle WebDatabaseService::ScheduleDBTaskWithResult(
} }
void WebDatabaseService::CancelRequest(WebDataServiceBase::Handle h) { void WebDatabaseService::CancelRequest(WebDataServiceBase::Handle h) {
if (!web_db_backend_) if (web_db_backend_)
return; web_db_backend_->request_manager()->CancelRequest(h);
web_db_backend_->request_manager()->CancelRequest(h);
}
void WebDatabaseService::RegisterDBLoadedCallback(DBLoadedCallback callback) {
loaded_callbacks_.push_back(std::move(callback));
} }
void WebDatabaseService::RegisterDBErrorCallback(DBLoadErrorCallback callback) { void WebDatabaseService::RegisterDBErrorCallback(DBLoadErrorCallback callback) {
...@@ -134,29 +123,19 @@ void WebDatabaseService::OnDatabaseLoadDone(sql::InitStatus status, ...@@ -134,29 +123,19 @@ void WebDatabaseService::OnDatabaseLoadDone(sql::InitStatus status,
const std::string& diagnostics) { const std::string& diagnostics) {
// The INIT_OK_WITH_DATA_LOSS status is an initialization success but with // The INIT_OK_WITH_DATA_LOSS status is an initialization success but with
// suspected data loss, so we also run the error callbacks. // suspected data loss, so we also run the error callbacks.
if (status != sql::INIT_OK) { if (status == sql::INIT_OK)
// Notify that the database load failed. return;
while (!error_callbacks_.empty()) {
// The profile error callback is a message box that runs in a nested run
// loop. While it's being displayed, other OnDatabaseLoadDone() will run
// (posted from WebDatabaseBackend::Delegate::DBLoaded()). We need to make
// sure that after the callback running the message box returns, it checks
// |error_callbacks_| before it accesses it.
DBLoadErrorCallback error_callback = std::move(error_callbacks_.back());
error_callbacks_.pop_back();
if (error_callback)
std::move(error_callback).Run(status, diagnostics);
}
}
if (status == sql::INIT_OK || status == sql::INIT_OK_WITH_DATA_LOSS) {
db_loaded_ = true;
while (!loaded_callbacks_.empty()) { // Notify that the database load failed.
DBLoadedCallback loaded_callback = std::move(loaded_callbacks_.back()); while (!error_callbacks_.empty()) {
loaded_callbacks_.pop_back(); // The profile error callback is a message box that runs in a nested run
if (loaded_callback) // loop. While it's being displayed, other OnDatabaseLoadDone() will run
std::move(loaded_callback).Run(); // (posted from WebDatabaseBackend::Delegate::DBLoaded()). We need to make
} // sure that after the callback running the message box returns, it checks
// |error_callbacks_| before it accesses it.
DBLoadErrorCallback error_callback = std::move(error_callbacks_.back());
error_callbacks_.pop_back();
if (!error_callback.is_null())
std::move(error_callback).Run(status, diagnostics);
} }
} }
...@@ -50,7 +50,6 @@ class WEBDATA_EXPORT WebDatabaseService ...@@ -50,7 +50,6 @@ class WEBDATA_EXPORT WebDatabaseService
using WriteTask = base::OnceCallback<WebDatabase::State(WebDatabase*)>; using WriteTask = base::OnceCallback<WebDatabase::State(WebDatabase*)>;
// Types for managing DB loading callbacks. // Types for managing DB loading callbacks.
using DBLoadedCallback = base::OnceClosure;
using DBLoadErrorCallback = using DBLoadErrorCallback =
base::OnceCallback<void(sql::InitStatus, const std::string&)>; base::OnceCallback<void(sql::InitStatus, const std::string&)>;
...@@ -93,13 +92,6 @@ class WEBDATA_EXPORT WebDatabaseService ...@@ -93,13 +92,6 @@ class WEBDATA_EXPORT WebDatabaseService
// somewhere else. // somewhere else.
virtual void CancelRequest(WebDataServiceBase::Handle h); virtual void CancelRequest(WebDataServiceBase::Handle h);
// Register a callback to be notified that the database has loaded. Multiple
// callbacks may be registered, and each will be called at most once
// (following a successful database load), then cleared.
// Note: if the database load is already complete, then the callback will NOT
// be stored or called.
void RegisterDBLoadedCallback(DBLoadedCallback callback);
// Register a callback to be notified that the database has failed to load. // Register a callback to be notified that the database has failed to load.
// Multiple callbacks may be registered, and each will be called at most once // Multiple callbacks may be registered, and each will be called at most once
// (following a database load failure), then cleared. // (following a database load failure), then cleared.
...@@ -107,15 +99,12 @@ class WEBDATA_EXPORT WebDatabaseService ...@@ -107,15 +99,12 @@ class WEBDATA_EXPORT WebDatabaseService
// be stored or called. // be stored or called.
void RegisterDBErrorCallback(DBLoadErrorCallback callback); void RegisterDBErrorCallback(DBLoadErrorCallback callback);
bool db_loaded() const { return db_loaded_; }
private: private:
class BackendDelegate; class BackendDelegate;
friend class BackendDelegate; friend class BackendDelegate;
friend class base::RefCountedDeleteOnSequence<WebDatabaseService>; friend class base::RefCountedDeleteOnSequence<WebDatabaseService>;
friend class base::DeleteHelper<WebDatabaseService>; friend class base::DeleteHelper<WebDatabaseService>;
using LoadedCallbacks = std::vector<DBLoadedCallback>;
using ErrorCallbacks = std::vector<DBLoadErrorCallback>; using ErrorCallbacks = std::vector<DBLoadErrorCallback>;
virtual ~WebDatabaseService(); virtual ~WebDatabaseService();
...@@ -129,15 +118,9 @@ class WEBDATA_EXPORT WebDatabaseService ...@@ -129,15 +118,9 @@ class WEBDATA_EXPORT WebDatabaseService
// PostTask on DB sequence may outlive us. // PostTask on DB sequence may outlive us.
scoped_refptr<WebDatabaseBackend> web_db_backend_; scoped_refptr<WebDatabaseBackend> web_db_backend_;
// Callbacks to be called once the DB has loaded.
LoadedCallbacks loaded_callbacks_;
// Callbacks to be called if the DB has failed to load. // Callbacks to be called if the DB has failed to load.
ErrorCallbacks error_callbacks_; ErrorCallbacks error_callbacks_;
// True if the WebDatabase has loaded.
bool db_loaded_;
scoped_refptr<base::SingleThreadTaskRunner> db_task_runner_; scoped_refptr<base::SingleThreadTaskRunner> db_task_runner_;
// All vended weak pointers are invalidated in ShutdownDatabase(). // All vended weak pointers are invalidated in ShutdownDatabase().
......
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