sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "components/webdata_services/web_data_service_wrapper.h" |
| 6 | |
Gyuyoung Kim | 8b084c0 | 2018-01-23 13:34:37 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 9 | #include "base/bind.h" |
| 10 | #include "base/callback.h" |
pnoland | aa02c72 | 2016-11-29 00:21:08 | [diff] [blame] | 11 | #include "base/feature_list.h" |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 12 | #include "base/files/file_path.h" |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 13 | #include "base/single_thread_task_runner.h" |
Gabriel Charette | 44db142 | 2018-08-06 11:19:33 | [diff] [blame] | 14 | #include "base/task/post_task.h" |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 15 | #include "build/build_config.h" |
pnoland | aa02c72 | 2016-11-29 00:21:08 | [diff] [blame] | 16 | #include "components/autofill/core/browser/webdata/autocomplete_sync_bridge.h" |
Jan Krcal | 7d2b56d | 2018-07-04 20:48:24 | [diff] [blame] | 17 | #include "components/autofill/core/browser/webdata/autofill_profile_sync_bridge.h" |
sdefresne | f8cf5be | 2014-12-16 20:08:09 | [diff] [blame] | 18 | #include "components/autofill/core/browser/webdata/autofill_profile_syncable_service.h" |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 19 | #include "components/autofill/core/browser/webdata/autofill_table.h" |
Jan Krcal | 7873f47 | 2018-07-25 14:13:44 | [diff] [blame] | 20 | #include "components/autofill/core/browser/webdata/autofill_wallet_metadata_sync_bridge.h" |
rouslan | fd7f464 | 2015-06-23 22:10:15 | [diff] [blame] | 21 | #include "components/autofill/core/browser/webdata/autofill_wallet_metadata_syncable_service.h" |
Mikel Astiz | f7ea338 | 2018-07-24 16:19:17 | [diff] [blame] | 22 | #include "components/autofill/core/browser/webdata/autofill_wallet_sync_bridge.h" |
brettw | 43eadb63 | 2015-02-12 18:44:39 | [diff] [blame] | 23 | #include "components/autofill/core/browser/webdata/autofill_wallet_syncable_service.h" |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 24 | #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" |
Florian Uunk | bb7ccd8 | 2018-07-20 15:53:36 | [diff] [blame] | 25 | #include "components/autofill/core/common/autofill_features.h" |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 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" |
pnoland | aa02c72 | 2016-11-29 00:21:08 | [diff] [blame] | 30 | #include "components/sync/driver/sync_driver_switches.h" |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 31 | #include "components/webdata/common/web_database_service.h" |
| 32 | #include "components/webdata/common/webdata_constants.h" |
| 33 | |
Rouslan Solomakhin | de01253 | 2017-09-20 15:18:34 | [diff] [blame] | 34 | #if !defined(OS_IOS) |
| 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" |
gogerald | 753494c7 | 2017-04-12 17:15:34 | [diff] [blame] | 38 | #endif |
| 39 | |
sdefresne | f8cf5be | 2014-12-16 20:08:09 | [diff] [blame] | 40 | namespace { |
| 41 | |
Jan Krcal | 7d2b56d | 2018-07-04 20:48:24 | [diff] [blame] | 42 | // TODO(jkrcal): Rename this function when the last webdata sync type get |
| 43 | // converted to USS, e.g. to InitSyncBridgesOnDBSequence(). Check also other |
| 44 | // related functions. |
Florian Uunk | bb7ccd8 | 2018-07-20 15:53:36 | [diff] [blame] | 45 | void InitSyncableProfileServicesOnDBSequence( |
Peter Kasting | 1975067 | 2017-08-04 05:24:09 | [diff] [blame] | 46 | scoped_refptr<base::SingleThreadTaskRunner> db_task_runner, |
sdefresne | f8cf5be | 2014-12-16 20:08:09 | [diff] [blame] | 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) { |
Peter Kasting | 1975067 | 2017-08-04 05:24:09 | [diff] [blame] | 52 | DCHECK(db_task_runner->RunsTasksInCurrentSequence()); |
sdefresne | f8cf5be | 2014-12-16 20:08:09 | [diff] [blame] | 53 | |
| 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. |
Jan Krcal | 9915270 | 2018-02-23 09:21:42 | [diff] [blame] | 56 | autofill::AutocompleteSyncBridge::CreateForWebDataServiceAndBackend( |
| 57 | autofill_web_data.get(), autofill_backend); |
brettw | 43eadb63 | 2015-02-12 18:44:39 | [diff] [blame] | 58 | |
Jan Krcal | 7d2b56d | 2018-07-04 20:48:24 | [diff] [blame] | 59 | if (base::FeatureList::IsEnabled(switches::kSyncUSSAutofillProfile)) { |
| 60 | autofill::AutofillProfileSyncBridge::CreateForWebDataServiceAndBackend( |
| 61 | app_locale, autofill_backend, autofill_web_data.get()); |
| 62 | } else { |
| 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); |
| 68 | } |
Florian Uunk | bb7ccd8 | 2018-07-20 15:53:36 | [diff] [blame] | 69 | } |
Jan Krcal | 7d2b56d | 2018-07-04 20:48:24 | [diff] [blame] | 70 | |
Mikel Astiz | f7ea338 | 2018-07-24 16:19:17 | [diff] [blame] | 71 | // TODO(jkrcal): Rename this function when the last webdata sync type get |
| 72 | // converted to USS, e.g. to InitSyncBridgesOnDBSequence(). Check also other |
| 73 | // related functions. |
Florian Uunk | bb7ccd8 | 2018-07-20 15:53:36 | [diff] [blame] | 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, |
Jan Krcal | bebe60c | 2018-08-30 09:16:01 | [diff] [blame] | 80 | bool is_full_sync, |
Florian Uunk | bb7ccd8 | 2018-07-20 15:53:36 | [diff] [blame] | 81 | autofill::AutofillWebDataBackend* autofill_backend) { |
| 82 | DCHECK(db_task_runner->RunsTasksInCurrentSequence()); |
Mikel Astiz | f7ea338 | 2018-07-24 16:19:17 | [diff] [blame] | 83 | |
Florian Uunk | 8bb9f4d | 2018-10-22 15:52:58 | [diff] [blame] | 84 | base::RepeatingCallback<void(bool)> wallet_active_callback; |
| 85 | if (base::FeatureList::IsEnabled(switches::kSyncUSSAutofillWalletMetadata)) { |
| 86 | autofill::AutofillWalletMetadataSyncBridge:: |
| 87 | CreateForWebDataServiceAndBackend(app_locale, autofill_backend, |
| 88 | autofill_web_data.get()); |
| 89 | wallet_active_callback = base::BindRepeating( |
| 90 | &autofill::AutofillWalletMetadataSyncBridge:: |
| 91 | OnWalletDataTrackingStateChanged, |
| 92 | autofill::AutofillWalletMetadataSyncBridge::FromWebDataService( |
| 93 | autofill_web_data.get()) |
| 94 | ->GetWeakPtr()); |
| 95 | } else { |
| 96 | autofill::AutofillWalletMetadataSyncableService:: |
| 97 | CreateForWebDataServiceAndBackend(autofill_web_data.get(), |
| 98 | autofill_backend, app_locale); |
| 99 | wallet_active_callback = base::BindRepeating( |
| 100 | &autofill::AutofillWalletMetadataSyncableService:: |
| 101 | OnWalletDataTrackingStateChanged, |
| 102 | autofill::AutofillWalletMetadataSyncableService::FromWebDataService( |
| 103 | autofill_web_data.get()) |
| 104 | ->GetWeakPtr()); |
| 105 | } |
| 106 | |
Mikel Astiz | f7ea338 | 2018-07-24 16:19:17 | [diff] [blame] | 107 | if (base::FeatureList::IsEnabled(switches::kSyncUSSAutofillWalletData)) { |
| 108 | autofill::AutofillWalletSyncBridge::CreateForWebDataServiceAndBackend( |
Florian Uunk | 8bb9f4d | 2018-10-22 15:52:58 | [diff] [blame] | 109 | app_locale, wallet_active_callback, is_full_sync, autofill_backend, |
| 110 | autofill_web_data.get()); |
Mikel Astiz | f7ea338 | 2018-07-24 16:19:17 | [diff] [blame] | 111 | } else { |
| 112 | autofill::AutofillWalletSyncableService::CreateForWebDataServiceAndBackend( |
| 113 | autofill_web_data.get(), autofill_backend, app_locale); |
| 114 | autofill::AutofillWalletSyncableService::FromWebDataService( |
| 115 | autofill_web_data.get()) |
| 116 | ->InjectStartSyncFlare(sync_flare); |
Florian Uunk | 8bb9f4d | 2018-10-22 15:52:58 | [diff] [blame] | 117 | // For non-USS wallet, the metadata is always checking the existence of |
| 118 | // wallet data to add/remove metadata entries. |
| 119 | wallet_active_callback.Run(true); |
Jan Krcal | 7873f47 | 2018-07-25 14:13:44 | [diff] [blame] | 120 | } |
sdefresne | f8cf5be | 2014-12-16 20:08:09 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | } // namespace |
| 124 | |
Rouslan Solomakhin | de01253 | 2017-09-20 15:18:34 | [diff] [blame] | 125 | WebDataServiceWrapper::WebDataServiceWrapper() {} |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 126 | |
| 127 | WebDataServiceWrapper::WebDataServiceWrapper( |
sdefresne | f8cf5be | 2014-12-16 20:08:09 | [diff] [blame] | 128 | const base::FilePath& context_path, |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 129 | const std::string& application_locale, |
Peter Kasting | 1975067 | 2017-08-04 05:24:09 | [diff] [blame] | 130 | const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner, |
sdefresne | f8cf5be | 2014-12-16 20:08:09 | [diff] [blame] | 131 | const syncer::SyncableService::StartSyncFlare& flare, |
thestig | 6f04675 | 2015-08-21 01:23:47 | [diff] [blame] | 132 | const ShowErrorCallback& show_error_callback) { |
sdefresne | f8cf5be | 2014-12-16 20:08:09 | [diff] [blame] | 133 | base::FilePath path = context_path.Append(kWebDataFilename); |
Peter Kasting | d3e6933a | 2017-07-28 07:42:28 | [diff] [blame] | 134 | // TODO(pkasting): http://crbug.com/740773 This should likely be sequenced, |
| 135 | // not single-threaded; it's also possible the various uses of this below |
| 136 | // should each use their own sequences instead of sharing this one. |
Peter Kasting | 1975067 | 2017-08-04 05:24:09 | [diff] [blame] | 137 | auto db_task_runner = base::CreateSingleThreadTaskRunnerWithTraits( |
Peter Kasting | d3e6933a | 2017-07-28 07:42:28 | [diff] [blame] | 138 | {base::MayBlock(), base::TaskPriority::USER_VISIBLE, |
| 139 | base::TaskShutdownBehavior::BLOCK_SHUTDOWN}); |
Florian Uunk | 75c22c9 | 2018-06-25 12:44:10 | [diff] [blame] | 140 | profile_database_ = |
| 141 | new WebDatabaseService(path, ui_task_runner, db_task_runner); |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 142 | |
| 143 | // All tables objects that participate in managing the database must |
| 144 | // be added here. |
Florian Uunk | 75c22c9 | 2018-06-25 12:44:10 | [diff] [blame] | 145 | profile_database_->AddTable(std::make_unique<autofill::AutofillTable>()); |
| 146 | profile_database_->AddTable(std::make_unique<KeywordTable>()); |
Florian Uunk | 75c22c9 | 2018-06-25 12:44:10 | [diff] [blame] | 147 | profile_database_->AddTable(std::make_unique<TokenServiceTable>()); |
Rouslan Solomakhin | de01253 | 2017-09-20 15:18:34 | [diff] [blame] | 148 | #if !defined(OS_IOS) |
Florian Uunk | 75c22c9 | 2018-06-25 12:44:10 | [diff] [blame] | 149 | profile_database_->AddTable( |
Gyuyoung Kim | 8b084c0 | 2018-01-23 13:34:37 | [diff] [blame] | 150 | std::make_unique<payments::PaymentMethodManifestTable>()); |
Florian Uunk | 75c22c9 | 2018-06-25 12:44:10 | [diff] [blame] | 151 | profile_database_->AddTable( |
Gyuyoung Kim | 8b084c0 | 2018-01-23 13:34:37 | [diff] [blame] | 152 | std::make_unique<payments::WebAppManifestSectionTable>()); |
gogerald | 753494c7 | 2017-04-12 17:15:34 | [diff] [blame] | 153 | #endif |
Florian Uunk | 75c22c9 | 2018-06-25 12:44:10 | [diff] [blame] | 154 | profile_database_->LoadDatabase(); |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 155 | |
Florian Uunk | 75c22c9 | 2018-06-25 12:44:10 | [diff] [blame] | 156 | profile_autofill_web_data_ = new autofill::AutofillWebDataService( |
| 157 | profile_database_, ui_task_runner, db_task_runner, |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 158 | base::Bind(show_error_callback, ERROR_LOADING_AUTOFILL)); |
Florian Uunk | 75c22c9 | 2018-06-25 12:44:10 | [diff] [blame] | 159 | profile_autofill_web_data_->Init(); |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 160 | |
| 161 | keyword_web_data_ = new KeywordWebDataService( |
Florian Uunk | 75c22c9 | 2018-06-25 12:44:10 | [diff] [blame] | 162 | profile_database_, ui_task_runner, |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 163 | base::Bind(show_error_callback, ERROR_LOADING_KEYWORD)); |
| 164 | keyword_web_data_->Init(); |
| 165 | |
Peter Kasting | 1975067 | 2017-08-04 05:24:09 | [diff] [blame] | 166 | token_web_data_ = |
Florian Uunk | 75c22c9 | 2018-06-25 12:44:10 | [diff] [blame] | 167 | new TokenWebData(profile_database_, ui_task_runner, db_task_runner, |
Peter Kasting | 1975067 | 2017-08-04 05:24:09 | [diff] [blame] | 168 | base::Bind(show_error_callback, ERROR_LOADING_TOKEN)); |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 169 | token_web_data_->Init(); |
| 170 | |
Rouslan Solomakhin | de01253 | 2017-09-20 15:18:34 | [diff] [blame] | 171 | #if !defined(OS_IOS) |
gogerald | 7947209 | 2017-04-27 15:38:16 | [diff] [blame] | 172 | payment_manifest_web_data_ = new payments::PaymentManifestWebDataService( |
Florian Uunk | 75c22c9 | 2018-06-25 12:44:10 | [diff] [blame] | 173 | profile_database_, |
gogerald | 7947209 | 2017-04-27 15:38:16 | [diff] [blame] | 174 | base::Bind(show_error_callback, ERROR_LOADING_PAYMENT_MANIFEST), |
Peter Kasting | 1975067 | 2017-08-04 05:24:09 | [diff] [blame] | 175 | ui_task_runner); |
gogerald | 7947209 | 2017-04-27 15:38:16 | [diff] [blame] | 176 | #endif |
| 177 | |
Florian Uunk | bb7ccd8 | 2018-07-20 15:53:36 | [diff] [blame] | 178 | profile_autofill_web_data_->GetAutofillBackend(base::Bind( |
| 179 | &InitSyncableProfileServicesOnDBSequence, db_task_runner, flare, |
| 180 | profile_autofill_web_data_, context_path, application_locale)); |
Jan Krcal | bebe60c | 2018-08-30 09:16:01 | [diff] [blame] | 181 | profile_autofill_web_data_->GetAutofillBackend( |
| 182 | base::Bind(&InitSyncableAccountServicesOnDBSequence, db_task_runner, |
| 183 | flare, profile_autofill_web_data_, context_path, |
| 184 | application_locale, /*is_full_sync=*/true)); |
Florian Uunk | 75c22c9 | 2018-06-25 12:44:10 | [diff] [blame] | 185 | |
Florian Uunk | bb7ccd8 | 2018-07-20 15:53:36 | [diff] [blame] | 186 | if (base::FeatureList::IsEnabled( |
| 187 | autofill::features::kAutofillEnableAccountWalletStorage)) { |
| 188 | account_database_ = |
| 189 | new WebDatabaseService(base::FilePath(WebDatabase::kInMemoryPath), |
| 190 | ui_task_runner, db_task_runner); |
| 191 | account_database_->AddTable(std::make_unique<autofill::AutofillTable>()); |
| 192 | account_database_->LoadDatabase(); |
Florian Uunk | 75c22c9 | 2018-06-25 12:44:10 | [diff] [blame] | 193 | |
Florian Uunk | bb7ccd8 | 2018-07-20 15:53:36 | [diff] [blame] | 194 | account_autofill_web_data_ = new autofill::AutofillWebDataService( |
| 195 | account_database_, ui_task_runner, db_task_runner, |
| 196 | base::Bind(show_error_callback, ERROR_LOADING_ACCOUNT_AUTOFILL)); |
| 197 | account_autofill_web_data_->Init(); |
Jan Krcal | bebe60c | 2018-08-30 09:16:01 | [diff] [blame] | 198 | account_autofill_web_data_->GetAutofillBackend( |
| 199 | base::Bind(&InitSyncableAccountServicesOnDBSequence, db_task_runner, |
| 200 | flare, account_autofill_web_data_, context_path, |
| 201 | application_locale, /*is_full_sync=*/false)); |
Florian Uunk | bb7ccd8 | 2018-07-20 15:53:36 | [diff] [blame] | 202 | } |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 203 | } |
| 204 | |
Rouslan Solomakhin | de01253 | 2017-09-20 15:18:34 | [diff] [blame] | 205 | WebDataServiceWrapper::~WebDataServiceWrapper() {} |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 206 | |
| 207 | void WebDataServiceWrapper::Shutdown() { |
Florian Uunk | 75c22c9 | 2018-06-25 12:44:10 | [diff] [blame] | 208 | profile_autofill_web_data_->ShutdownOnUISequence(); |
Florian Uunk | bb7ccd8 | 2018-07-20 15:53:36 | [diff] [blame] | 209 | if (account_autofill_web_data_) |
| 210 | account_autofill_web_data_->ShutdownOnUISequence(); |
Peter Kasting | 1975067 | 2017-08-04 05:24:09 | [diff] [blame] | 211 | keyword_web_data_->ShutdownOnUISequence(); |
| 212 | token_web_data_->ShutdownOnUISequence(); |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 213 | |
Rouslan Solomakhin | de01253 | 2017-09-20 15:18:34 | [diff] [blame] | 214 | #if !defined(OS_IOS) |
Peter Kasting | 1975067 | 2017-08-04 05:24:09 | [diff] [blame] | 215 | payment_manifest_web_data_->ShutdownOnUISequence(); |
gogerald | 7947209 | 2017-04-27 15:38:16 | [diff] [blame] | 216 | #endif |
| 217 | |
Florian Uunk | 75c22c9 | 2018-06-25 12:44:10 | [diff] [blame] | 218 | profile_database_->ShutdownDatabase(); |
Florian Uunk | bb7ccd8 | 2018-07-20 15:53:36 | [diff] [blame] | 219 | if (account_database_) |
| 220 | account_database_->ShutdownDatabase(); |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | scoped_refptr<autofill::AutofillWebDataService> |
Florian Uunk | 75c22c9 | 2018-06-25 12:44:10 | [diff] [blame] | 224 | WebDataServiceWrapper::GetProfileAutofillWebData() { |
| 225 | return profile_autofill_web_data_.get(); |
| 226 | } |
| 227 | |
| 228 | scoped_refptr<autofill::AutofillWebDataService> |
| 229 | WebDataServiceWrapper::GetAccountAutofillWebData() { |
| 230 | return account_autofill_web_data_.get(); |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | scoped_refptr<KeywordWebDataService> |
| 234 | WebDataServiceWrapper::GetKeywordWebData() { |
| 235 | return keyword_web_data_.get(); |
| 236 | } |
| 237 | |
| 238 | scoped_refptr<TokenWebData> WebDataServiceWrapper::GetTokenWebData() { |
| 239 | return token_web_data_.get(); |
| 240 | } |
| 241 | |
Rouslan Solomakhin | de01253 | 2017-09-20 15:18:34 | [diff] [blame] | 242 | #if !defined(OS_IOS) |
gogerald | 7947209 | 2017-04-27 15:38:16 | [diff] [blame] | 243 | scoped_refptr<payments::PaymentManifestWebDataService> |
| 244 | WebDataServiceWrapper::GetPaymentManifestWebData() { |
| 245 | return payment_manifest_web_data_.get(); |
| 246 | } |
| 247 | #endif |