Use the AutofillWebDataService's task runner instead of assuming the DB thread.
This is a precursor to moving the WebDataService off the DB thread entirely.
This reduces the number of direct dependencies on the DB thread.
Lots of stuff is still dependent on the DB thread, including some of the tests I
am touching here. This just breaks the later change into smaller,
more-reviewable pieces.
Bug: 689520
Change-Id: I1b6af84ae941e7223458995d60858154f129cb5d
Reviewed-on: https://chromium-review.googlesource.com/580708
Commit-Queue: Peter Kasting <[email protected]>
Reviewed-by: Vaclav Brozek <[email protected]>
Reviewed-by: Martin Šrámek <[email protected]>
Reviewed-by: mahmadi <[email protected]>
Reviewed-by: Nicolas Zea <[email protected]>
Cr-Commit-Position: refs/heads/master@{#488867}
diff --git a/chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.cc b/chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.cc
index bb36ce9c..75b00af 100644
--- a/chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.cc
+++ b/chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.cc
@@ -605,10 +605,9 @@
clear_autofill_origin_urls_.Start();
web_data_service->RemoveOriginURLsModifiedBetween(
delete_begin_, delete_end_);
- // The above calls are done on the UI thread but do their work on the DB
- // thread. So wait for it.
- BrowserThread::PostTaskAndReply(
- BrowserThread::DB, FROM_HERE, base::BindOnce(&base::DoNothing),
+ // Ask for a call back when the above call is finished.
+ web_data_service->GetDBTaskRunner()->PostTaskAndReply(
+ FROM_HERE, base::BindOnce(&base::DoNothing),
clear_autofill_origin_urls_.GetCompletionCallback());
autofill::PersonalDataManager* data_manager =
@@ -852,11 +851,10 @@
delete_end_);
web_data_service->RemoveAutofillDataModifiedBetween(
delete_begin_, delete_end_);
- // The above calls are done on the UI thread but do their work on the DB
- // thread. So wait for it.
- BrowserThread::PostTaskAndReply(BrowserThread::DB, FROM_HERE,
- base::BindOnce(&base::DoNothing),
- clear_form_.GetCompletionCallback());
+ // Ask for a call back when the above calls are finished.
+ web_data_service->GetDBTaskRunner()->PostTaskAndReply(
+ FROM_HERE, base::BindOnce(&base::DoNothing),
+ clear_form_.GetCompletionCallback());
autofill::PersonalDataManager* data_manager =
autofill::PersonalDataManagerFactory::GetForProfile(profile_);
diff --git a/chrome/browser/sync/chrome_sync_client.cc b/chrome/browser/sync/chrome_sync_client.cc
index 75e3f7a5..0372c458 100644
--- a/chrome/browser/sync/chrome_sync_client.cc
+++ b/chrome/browser/sync/chrome_sync_client.cc
@@ -219,6 +219,8 @@
web_data_service_ = WebDataServiceFactory::GetAutofillWebDataForProfile(
profile_, ServiceAccessType::IMPLICIT_ACCESS);
+ db_thread_ =
+ web_data_service_ ? web_data_service_->GetDBTaskRunner() : nullptr;
password_store_ = PasswordStoreFactory::GetForProfile(
profile_, ServiceAccessType::IMPLICIT_ACCESS);
@@ -238,10 +240,8 @@
prefs::kSavingBrowserHistoryDisabled, sync_service_url,
content::BrowserThread::GetTaskRunnerForThread(
content::BrowserThread::UI),
- content::BrowserThread::GetTaskRunnerForThread(
- content::BrowserThread::DB),
- token_service, url_request_context_getter, web_data_service_,
- password_store_);
+ db_thread_, token_service, url_request_context_getter,
+ web_data_service_, password_store_);
}
}
@@ -531,9 +531,7 @@
DCHECK_CURRENTLY_ON(BrowserThread::UI);
switch (group) {
case syncer::GROUP_DB:
- return new syncer::SequencedModelWorker(
- BrowserThread::GetTaskRunnerForThread(BrowserThread::DB),
- syncer::GROUP_DB);
+ return new syncer::SequencedModelWorker(db_thread_, syncer::GROUP_DB);
// TODO(stanisc): crbug.com/731903: Rename GROUP_FILE to reflect that it is
// used only for app and extension settings.
case syncer::GROUP_FILE:
diff --git a/chrome/browser/sync/chrome_sync_client.h b/chrome/browser/sync/chrome_sync_client.h
index 7ad51302..2c6283a1 100644
--- a/chrome/browser/sync/chrome_sync_client.h
+++ b/chrome/browser/sync/chrome_sync_client.h
@@ -9,6 +9,7 @@
#include <vector>
#include "base/macros.h"
+#include "base/single_thread_task_runner.h"
#include "chrome/browser/sync/glue/extensions_activity_monitor.h"
#include "components/sync/driver/sync_client.h"
@@ -95,6 +96,9 @@
scoped_refptr<autofill::AutofillWebDataService> web_data_service_;
scoped_refptr<password_manager::PasswordStore> password_store_;
+ // The task runner for the |web_data_service_|, if any.
+ scoped_refptr<base::SingleThreadTaskRunner> db_thread_;
+
std::unique_ptr<sync_sessions::SyncSessionsClient> sync_sessions_client_;
// Generates and monitors the ExtensionsActivity object used by sync.
diff --git a/chrome/browser/sync/test/integration/autofill_helper.cc b/chrome/browser/sync/test/integration/autofill_helper.cc
index 7a331510..0a9f668 100644
--- a/chrome/browser/sync/test/integration/autofill_helper.cc
+++ b/chrome/browser/sync/test/integration/autofill_helper.cc
@@ -67,11 +67,12 @@
done_event->Signal();
}
-void RunOnDBThreadAndBlock(base::Closure task) {
+void RunOnDBThreadAndBlock(scoped_refptr<AutofillWebDataService> wds,
+ base::Closure task) {
WaitableEvent done_event(base::WaitableEvent::ResetPolicy::AUTOMATIC,
base::WaitableEvent::InitialState::NOT_SIGNALED);
- BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
- BindOnce(&RunOnDBThreadAndSignal, task, &done_event));
+ wds->GetDBTaskRunner()->PostTask(
+ FROM_HERE, BindOnce(&RunOnDBThreadAndSignal, task, &done_event));
done_event.Wait();
}
@@ -89,7 +90,7 @@
void(AutofillWebDataService::*add_observer_func)(
AutofillWebDataServiceObserverOnDBThread*) =
&AutofillWebDataService::AddObserver;
- RunOnDBThreadAndBlock(Bind(add_observer_func, wds, &mock_observer));
+ RunOnDBThreadAndBlock(wds, Bind(add_observer_func, wds, &mock_observer));
wds->RemoveFormValueForElementName(key.name(), key.value());
done_event.Wait();
@@ -97,34 +98,34 @@
void(AutofillWebDataService::*remove_observer_func)(
AutofillWebDataServiceObserverOnDBThread*) =
&AutofillWebDataService::RemoveObserver;
- RunOnDBThreadAndBlock(Bind(remove_observer_func, wds, &mock_observer));
+ RunOnDBThreadAndBlock(wds, Bind(remove_observer_func, wds, &mock_observer));
}
void GetAllAutofillEntriesOnDBThread(AutofillWebDataService* wds,
std::vector<AutofillEntry>* entries) {
- DCHECK_CURRENTLY_ON(BrowserThread::DB);
+ DCHECK(wds->GetDBTaskRunner()->RunsTasksInCurrentSequence());
AutofillTable::FromWebDatabase(
wds->GetDatabase())->GetAllAutofillEntries(entries);
}
std::vector<AutofillEntry> GetAllAutofillEntries(AutofillWebDataService* wds) {
std::vector<AutofillEntry> entries;
- DCHECK_CURRENTLY_ON(BrowserThread::UI);
- RunOnDBThreadAndBlock(Bind(&GetAllAutofillEntriesOnDBThread,
- Unretained(wds),
- &entries));
+ RunOnDBThreadAndBlock(
+ wds, Bind(&GetAllAutofillEntriesOnDBThread, Unretained(wds), &entries));
return entries;
}
// UI thread returns from the update operations on the DB thread and schedules
// the sync. This function blocks until after this scheduled sync is complete by
// scheduling additional empty task on DB Thread. Call after AddKeys/RemoveKey.
-void BlockForPendingDBThreadTasks() {
+void BlockForPendingDBThreadTasks(int profile) {
// The order of the notifications is undefined, so sync change sometimes is
// posted after the notification for observer_helper. Post new task to db
// thread that guaranteed to be after sync and would be blocking until
// completion.
- RunOnDBThreadAndBlock(base::Closure());
+ scoped_refptr<AutofillWebDataService> wds =
+ autofill_helper::GetWebDataService(profile);
+ RunOnDBThreadAndBlock(wds, base::Closure());
}
bool ProfilesMatchImpl(
@@ -249,21 +250,21 @@
void(AutofillWebDataService::*add_observer_func)(
AutofillWebDataServiceObserverOnDBThread*) =
&AutofillWebDataService::AddObserver;
- RunOnDBThreadAndBlock(Bind(add_observer_func, wds, &mock_observer));
+ RunOnDBThreadAndBlock(wds, Bind(add_observer_func, wds, &mock_observer));
wds->AddFormFields(form_fields);
done_event.Wait();
- BlockForPendingDBThreadTasks();
+ BlockForPendingDBThreadTasks(profile);
void(AutofillWebDataService::*remove_observer_func)(
AutofillWebDataServiceObserverOnDBThread*) =
&AutofillWebDataService::RemoveObserver;
- RunOnDBThreadAndBlock(Bind(remove_observer_func, wds, &mock_observer));
+ RunOnDBThreadAndBlock(wds, Bind(remove_observer_func, wds, &mock_observer));
}
void RemoveKey(int profile, const AutofillKey& key) {
RemoveKeyDontBlockForSync(profile, key);
- BlockForPendingDBThreadTasks();
+ BlockForPendingDBThreadTasks(profile);
}
void RemoveKeys(int profile) {
@@ -271,7 +272,7 @@
for (const AutofillEntry& entry : keys) {
RemoveKeyDontBlockForSync(profile, entry.key());
}
- BlockForPendingDBThreadTasks();
+ BlockForPendingDBThreadTasks(profile);
}
std::set<AutofillEntry> GetAllKeys(int profile) {
@@ -336,15 +337,14 @@
// asynchronous and there's no way to pass a callback that's run when our
// Refresh() call finishes. A PersonalDataManagerObserver won't completely fix
// the problem either since there could be multiple outstanding modifications
- // scheduled, and we cannot ensure that we have the latest view. Instead post
- // a task to the DB thread and wait for it run. This will ensure that our
- // Refresh() was executed first, as long as the DB thread is sequenced. It is
- // possible for another write to sneak in between our Refresh() and the task
- // that is blocked for, causing the web_profiles() read to return even more
- // current data, but this shouldn't cause problems. While PersonalDataManager
- // will cancel outstanding queries, this is only instigated on the UI thread,
- // which we are about to block, which means we are safe.
- BlockForPendingDBThreadTasks();
+ // scheduled, and we cannot ensure that we have the latest view. Instead
+ // explicitly wait for our Refresh to have executed. It is possible for
+ // another write to sneak in between our Refresh() and the task that is
+ // blocked for, causing the web_profiles() read to return even more current
+ // data, but this shouldn't cause problems. While PersonalDataManager will
+ // cancel outstanding queries, this is only instigated on the UI thread, which
+ // we are about to block, which means we are safe.
+ BlockForPendingDBThreadTasks(profile);
return pdm->web_profiles();
}
@@ -399,7 +399,7 @@
// Similar to GetAllAutoFillProfiles() we need to make sure we are not reading
// before any locally instigated async writes. This is run exactly one time
// before the first IsExitConditionSatisfied() is called.
- BlockForPendingDBThreadTasks();
+ BlockForPendingDBThreadTasks(profile_a_);
return StatusChangeChecker::Wait();
}