1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/webdata_services/web_data_service_wrapper.h"
10 #include "base/callback.h"
11 #include "base/feature_list.h"
12 #include "base/files/file_path.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/task/post_task.h"
15 #include "build/build_config.h"
16 #include "components/autofill/core/browser/webdata/autocomplete_sync_bridge.h"
17 #include "components/autofill/core/browser/webdata/autofill_profile_sync_bridge.h"
18 #include "components/autofill/core/browser/webdata/autofill_profile_syncable_service.h"
19 #include "components/autofill/core/browser/webdata/autofill_table.h"
20 #include "components/autofill/core/browser/webdata/autofill_wallet_metadata_sync_bridge.h"
21 #include "components/autofill/core/browser/webdata/autofill_wallet_metadata_syncable_service.h"
22 #include "components/autofill/core/browser/webdata/autofill_wallet_sync_bridge.h"
23 #include "components/autofill/core/browser/webdata/autofill_wallet_syncable_service.h"
24 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
25 #include "components/autofill/core/common/autofill_features.h"
26 #include "components/search_engines/keyword_table.h"
27 #include "components/search_engines/keyword_web_data_service.h"
28 #include "components/signin/core/browser/webdata/token_service_table.h"
29 #include "components/signin/core/browser/webdata/token_web_data.h"
30 #include "components/sync/driver/sync_driver_switches.h"
31 #include "components/webdata/common/web_database_service.h"
32 #include "components/webdata/common/webdata_constants.h"
35 #include "components/payments/content/payment_manifest_web_data_service.h"
36 #include "components/payments/content/payment_method_manifest_table.h"
37 #include "components/payments/content/web_app_manifest_section_table.h"
42 // TODO(jkrcal): Rename this function when the last webdata sync type get
43 // converted to USS, e.g. to InitSyncBridgesOnDBSequence(). Check also other
45 void InitSyncableProfileServicesOnDBSequence(
46 scoped_refptr<base::SingleThreadTaskRunner> db_task_runner,
47 const syncer::SyncableService::StartSyncFlare& sync_flare,
48 const scoped_refptr<autofill::AutofillWebDataService>& autofill_web_data,
49 const base::FilePath& context_path,
50 const std::string& app_locale,
51 autofill::AutofillWebDataBackend* autofill_backend) {
52 DCHECK(db_task_runner->RunsTasksInCurrentSequence());
54 // Currently only Autocomplete and Autofill profiles use the new Sync API, but
55 // all the database data should migrate to this API over time.
56 autofill::AutocompleteSyncBridge::CreateForWebDataServiceAndBackend(
57 autofill_web_data.get(), autofill_backend);
59 if (base::FeatureList::IsEnabled(switches::kSyncUSSAutofillProfile)) {
60 autofill::AutofillProfileSyncBridge::CreateForWebDataServiceAndBackend(
61 app_locale, autofill_backend, autofill_web_data.get());
63 autofill::AutofillProfileSyncableService::CreateForWebDataServiceAndBackend(
64 autofill_web_data.get(), autofill_backend, app_locale);
65 autofill::AutofillProfileSyncableService::FromWebDataService(
66 autofill_web_data.get())
67 ->InjectStartSyncFlare(sync_flare);
71 // TODO(jkrcal): Rename this function when the last webdata sync type get
72 // converted to USS, e.g. to InitSyncBridgesOnDBSequence(). Check also other
74 void InitSyncableAccountServicesOnDBSequence(
75 scoped_refptr<base::SingleThreadTaskRunner> db_task_runner,
76 const syncer::SyncableService::StartSyncFlare& sync_flare,
77 const scoped_refptr<autofill::AutofillWebDataService>& autofill_web_data,
78 const base::FilePath& context_path,
79 const std::string& app_locale,
80 autofill::AutofillWebDataBackend* autofill_backend) {
81 DCHECK(db_task_runner->RunsTasksInCurrentSequence());
83 base::RepeatingCallback<void(bool)> wallet_active_callback;
84 if (base::FeatureList::IsEnabled(switches::kSyncUSSAutofillWalletMetadata)) {
85 autofill::AutofillWalletMetadataSyncBridge::
86 CreateForWebDataServiceAndBackend(app_locale, autofill_backend,
87 autofill_web_data.get());
88 wallet_active_callback = base::BindRepeating(
89 &autofill::AutofillWalletMetadataSyncBridge::
90 OnWalletDataTrackingStateChanged,
91 autofill::AutofillWalletMetadataSyncBridge::FromWebDataService(
92 autofill_web_data.get())
95 autofill::AutofillWalletMetadataSyncableService::
96 CreateForWebDataServiceAndBackend(autofill_web_data.get(),
97 autofill_backend, app_locale);
98 wallet_active_callback = base::BindRepeating(
99 &autofill::AutofillWalletMetadataSyncableService::
100 OnWalletDataTrackingStateChanged,
101 autofill::AutofillWalletMetadataSyncableService::FromWebDataService(
102 autofill_web_data.get())
106 if (base::FeatureList::IsEnabled(switches::kSyncUSSAutofillWalletData)) {
107 autofill::AutofillWalletSyncBridge::CreateForWebDataServiceAndBackend(
108 app_locale, wallet_active_callback, autofill_backend,
109 autofill_web_data.get());
111 autofill::AutofillWalletSyncableService::CreateForWebDataServiceAndBackend(
112 autofill_web_data.get(), autofill_backend, app_locale);
113 autofill::AutofillWalletSyncableService::FromWebDataService(
114 autofill_web_data.get())
115 ->InjectStartSyncFlare(sync_flare);
116 // For non-USS wallet, the metadata is always checking the existence of
117 // wallet data to add/remove metadata entries.
118 wallet_active_callback.Run(true);
124 WebDataServiceWrapper::WebDataServiceWrapper() {}
126 WebDataServiceWrapper::WebDataServiceWrapper(
127 const base::FilePath& context_path,
128 const std::string& application_locale,
129 const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner,
130 const syncer::SyncableService::StartSyncFlare& flare,
131 const ShowErrorCallback& show_error_callback) {
132 base::FilePath path = context_path.Append(kWebDataFilename);
133 // TODO(pkasting): http://crbug.com/740773 This should likely be sequenced,
134 // not single-threaded; it's also possible the various uses of this below
135 // should each use their own sequences instead of sharing this one.
136 auto db_task_runner = base::CreateSingleThreadTaskRunnerWithTraits(
137 {base::MayBlock(), base::TaskPriority::USER_VISIBLE,
138 base::TaskShutdownBehavior::BLOCK_SHUTDOWN});
140 new WebDatabaseService(path, ui_task_runner, db_task_runner);
142 // All tables objects that participate in managing the database must
144 profile_database_->AddTable(std::make_unique<autofill::AutofillTable>());
145 profile_database_->AddTable(std::make_unique<KeywordTable>());
146 profile_database_->AddTable(std::make_unique<TokenServiceTable>());
148 profile_database_->AddTable(
149 std::make_unique<payments::PaymentMethodManifestTable>());
150 profile_database_->AddTable(
151 std::make_unique<payments::WebAppManifestSectionTable>());
153 profile_database_->LoadDatabase();
155 profile_autofill_web_data_ = new autofill::AutofillWebDataService(
156 profile_database_, ui_task_runner, db_task_runner,
157 base::Bind(show_error_callback, ERROR_LOADING_AUTOFILL));
158 profile_autofill_web_data_->Init();
160 keyword_web_data_ = new KeywordWebDataService(
161 profile_database_, ui_task_runner,
162 base::Bind(show_error_callback, ERROR_LOADING_KEYWORD));
163 keyword_web_data_->Init();
166 new TokenWebData(profile_database_, ui_task_runner, db_task_runner,
167 base::Bind(show_error_callback, ERROR_LOADING_TOKEN));
168 token_web_data_->Init();
171 payment_manifest_web_data_ = new payments::PaymentManifestWebDataService(
173 base::Bind(show_error_callback, ERROR_LOADING_PAYMENT_MANIFEST),
177 profile_autofill_web_data_->GetAutofillBackend(base::Bind(
178 &InitSyncableProfileServicesOnDBSequence, db_task_runner, flare,
179 profile_autofill_web_data_, context_path, application_locale));
180 profile_autofill_web_data_->GetAutofillBackend(base::Bind(
181 &InitSyncableAccountServicesOnDBSequence, db_task_runner, flare,
182 profile_autofill_web_data_, context_path, application_locale));
184 if (base::FeatureList::IsEnabled(
185 autofill::features::kAutofillEnableAccountWalletStorage)) {
187 new WebDatabaseService(base::FilePath(WebDatabase::kInMemoryPath),
188 ui_task_runner, db_task_runner);
189 account_database_->AddTable(std::make_unique<autofill::AutofillTable>());
190 account_database_->LoadDatabase();
192 account_autofill_web_data_ = new autofill::AutofillWebDataService(
193 account_database_, ui_task_runner, db_task_runner,
194 base::Bind(show_error_callback, ERROR_LOADING_ACCOUNT_AUTOFILL));
195 account_autofill_web_data_->Init();
196 account_autofill_web_data_->GetAutofillBackend(base::Bind(
197 &InitSyncableAccountServicesOnDBSequence, db_task_runner, flare,
198 account_autofill_web_data_, context_path, application_locale));
202 WebDataServiceWrapper::~WebDataServiceWrapper() {}
204 void WebDataServiceWrapper::Shutdown() {
205 profile_autofill_web_data_->ShutdownOnUISequence();
206 if (account_autofill_web_data_)
207 account_autofill_web_data_->ShutdownOnUISequence();
208 keyword_web_data_->ShutdownOnUISequence();
209 token_web_data_->ShutdownOnUISequence();
212 payment_manifest_web_data_->ShutdownOnUISequence();
215 profile_database_->ShutdownDatabase();
216 if (account_database_)
217 account_database_->ShutdownDatabase();
220 scoped_refptr<autofill::AutofillWebDataService>
221 WebDataServiceWrapper::GetProfileAutofillWebData() {
222 return profile_autofill_web_data_.get();
225 scoped_refptr<autofill::AutofillWebDataService>
226 WebDataServiceWrapper::GetAccountAutofillWebData() {
227 return account_autofill_web_data_.get();
230 scoped_refptr<KeywordWebDataService>
231 WebDataServiceWrapper::GetKeywordWebData() {
232 return keyword_web_data_.get();
235 scoped_refptr<TokenWebData> WebDataServiceWrapper::GetTokenWebData() {
236 return token_web_data_.get();
240 scoped_refptr<payments::PaymentManifestWebDataService>
241 WebDataServiceWrapper::GetPaymentManifestWebData() {
242 return payment_manifest_web_data_.get();