[email protected] | d07edd4 | 2012-05-14 23:49:46 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
tfarina | bf215a6f | 2015-06-15 22:24:05 | [diff] [blame] | 5 | #include "chrome/browser/web_data_service_factory.h" |
[email protected] | d07edd4 | 2012-05-14 23:49:46 | [diff] [blame] | 6 | |
[email protected] | 486295a | 2013-03-20 23:35:49 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 8 | #include "base/files/file_path.h" |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 9 | #include "base/memory/singleton.h" |
avi | 664c07b | 2015-12-26 02:18:31 | [diff] [blame] | 10 | #include "build/build_config.h" |
[email protected] | ed005f6f | 2013-04-05 17:03:56 | [diff] [blame] | 11 | #include "chrome/browser/browser_process.h" |
[email protected] | 018bf65 | 2013-05-03 23:18:34 | [diff] [blame] | 12 | #include "chrome/browser/profiles/incognito_helpers.h" |
sdefresne | e9ea3c2 | 2015-01-10 10:10:04 | [diff] [blame] | 13 | #include "chrome/browser/profiles/profile.h" |
afakhry | a592d29c | 2016-08-22 19:06:57 | [diff] [blame] | 14 | #include "chrome/browser/profiles/sql_init_error_message_ids.h" |
[email protected] | 486295a | 2013-03-20 23:35:49 | [diff] [blame] | 15 | #include "chrome/browser/ui/profile_error_dialog.h" |
sdefresne | f8cf5be | 2014-12-16 20:08:09 | [diff] [blame] | 16 | #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" |
[email protected] | bf5c532d | 2014-07-05 00:29:53 | [diff] [blame] | 17 | #include "components/search_engines/keyword_web_data_service.h" |
Colin Blundell | 2cdc91a | 2019-07-12 15:24:27 | [diff] [blame] | 18 | #include "components/signin/public/webdata/token_web_data.h" |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 19 | #include "components/webdata_services/web_data_service_wrapper.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 20 | #include "content/public/browser/browser_task_traits.h" |
[email protected] | 8550f06 | 2013-03-20 23:51:05 | [diff] [blame] | 21 | #include "content/public/browser/browser_thread.h" |
[email protected] | 486295a | 2013-03-20 23:35:49 | [diff] [blame] | 22 | |
| 23 | namespace { |
| 24 | |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 25 | // Converts a WebDataServiceWrapper::ErrorType to ProfileErrorType. |
| 26 | ProfileErrorType ProfileErrorFromWebDataServiceWrapperError( |
| 27 | WebDataServiceWrapper::ErrorType error_type) { |
| 28 | switch (error_type) { |
| 29 | case WebDataServiceWrapper::ERROR_LOADING_AUTOFILL: |
tfarina | 87b7a3f | 2016-10-21 01:35:24 | [diff] [blame] | 30 | return ProfileErrorType::DB_AUTOFILL_WEB_DATA; |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 31 | |
Florian Uunk | 75c22c9 | 2018-06-25 12:44:10 | [diff] [blame] | 32 | case WebDataServiceWrapper::ERROR_LOADING_ACCOUNT_AUTOFILL: |
| 33 | return ProfileErrorType::DB_ACCOUNT_AUTOFILL_WEB_DATA; |
| 34 | |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 35 | case WebDataServiceWrapper::ERROR_LOADING_KEYWORD: |
tfarina | 87b7a3f | 2016-10-21 01:35:24 | [diff] [blame] | 36 | return ProfileErrorType::DB_KEYWORD_WEB_DATA; |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 37 | |
| 38 | case WebDataServiceWrapper::ERROR_LOADING_TOKEN: |
tfarina | 87b7a3f | 2016-10-21 01:35:24 | [diff] [blame] | 39 | return ProfileErrorType::DB_TOKEN_WEB_DATA; |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 40 | |
| 41 | case WebDataServiceWrapper::ERROR_LOADING_PASSWORD: |
tfarina | 87b7a3f | 2016-10-21 01:35:24 | [diff] [blame] | 42 | return ProfileErrorType::DB_WEB_DATA; |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 43 | |
gogerald | 7947209 | 2017-04-27 15:38:16 | [diff] [blame] | 44 | case WebDataServiceWrapper::ERROR_LOADING_PAYMENT_MANIFEST: |
| 45 | return ProfileErrorType::DB_PAYMENT_MANIFEST_WEB_DATA; |
| 46 | |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 47 | default: |
Rouslan Solomakhin | de01253 | 2017-09-20 15:18:34 | [diff] [blame] | 48 | NOTREACHED() << "Unknown WebDataServiceWrapper::ErrorType: " |
| 49 | << error_type; |
tfarina | 87b7a3f | 2016-10-21 01:35:24 | [diff] [blame] | 50 | return ProfileErrorType::DB_WEB_DATA; |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
[email protected] | 486295a | 2013-03-20 23:35:49 | [diff] [blame] | 54 | // Callback to show error dialog on profile load error. |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 55 | void ProfileErrorCallback(WebDataServiceWrapper::ErrorType error_type, |
afakhry | 7c9abe7 | 2016-08-05 17:33:19 | [diff] [blame] | 56 | sql::InitStatus status, |
| 57 | const std::string& diagnostics) { |
| 58 | ShowProfileErrorDialog(ProfileErrorFromWebDataServiceWrapperError(error_type), |
afakhry | a592d29c | 2016-08-22 19:06:57 | [diff] [blame] | 59 | SqlInitStatusToMessageId(status), diagnostics); |
[email protected] | 486295a | 2013-03-20 23:35:49 | [diff] [blame] | 60 | } |
| 61 | |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 62 | } // namespace |
| 63 | |
Richard Knoll | db1fa8a | 2021-04-23 08:08:28 | [diff] [blame] | 64 | WebDataServiceFactory::WebDataServiceFactory() = default; |
[email protected] | d07edd4 | 2012-05-14 23:49:46 | [diff] [blame] | 65 | |
Richard Knoll | db1fa8a | 2021-04-23 08:08:28 | [diff] [blame] | 66 | WebDataServiceFactory::~WebDataServiceFactory() = default; |
[email protected] | d07edd4 | 2012-05-14 23:49:46 | [diff] [blame] | 67 | |
| 68 | // static |
[email protected] | e2b3105 | 2013-03-25 01:49:37 | [diff] [blame] | 69 | WebDataServiceWrapper* WebDataServiceFactory::GetForProfile( |
[email protected] | 34b5946 | 2013-12-03 14:08:32 | [diff] [blame] | 70 | Profile* profile, |
sdefresne | e9ea3c2 | 2015-01-10 10:10:04 | [diff] [blame] | 71 | ServiceAccessType access_type) { |
Richard Knoll | db1fa8a | 2021-04-23 08:08:28 | [diff] [blame] | 72 | return GetForBrowserContext(profile, access_type); |
[email protected] | d07edd4 | 2012-05-14 23:49:46 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // static |
[email protected] | e2b3105 | 2013-03-25 01:49:37 | [diff] [blame] | 76 | WebDataServiceWrapper* WebDataServiceFactory::GetForProfileIfExists( |
[email protected] | 34b5946 | 2013-12-03 14:08:32 | [diff] [blame] | 77 | Profile* profile, |
sdefresne | e9ea3c2 | 2015-01-10 10:10:04 | [diff] [blame] | 78 | ServiceAccessType access_type) { |
Richard Knoll | db1fa8a | 2021-04-23 08:08:28 | [diff] [blame] | 79 | return GetForBrowserContextIfExists(profile, access_type); |
[email protected] | dd3c53f | 2013-03-14 02:59:41 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | // static |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 83 | scoped_refptr<autofill::AutofillWebDataService> |
[email protected] | 34b5946 | 2013-12-03 14:08:32 | [diff] [blame] | 84 | WebDataServiceFactory::GetAutofillWebDataForProfile( |
| 85 | Profile* profile, |
sdefresne | e9ea3c2 | 2015-01-10 10:10:04 | [diff] [blame] | 86 | ServiceAccessType access_type) { |
[email protected] | 34b5946 | 2013-12-03 14:08:32 | [diff] [blame] | 87 | WebDataServiceWrapper* wrapper = |
| 88 | WebDataServiceFactory::GetForProfile(profile, access_type); |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 89 | // |wrapper| can be null in Incognito mode. |
Florian Uunk | 75c22c9 | 2018-06-25 12:44:10 | [diff] [blame] | 90 | return wrapper ? wrapper->GetProfileAutofillWebData() |
| 91 | : scoped_refptr<autofill::AutofillWebDataService>(nullptr); |
| 92 | } |
| 93 | |
| 94 | // static |
| 95 | scoped_refptr<autofill::AutofillWebDataService> |
| 96 | WebDataServiceFactory::GetAutofillWebDataForAccount( |
| 97 | Profile* profile, |
| 98 | ServiceAccessType access_type) { |
| 99 | WebDataServiceWrapper* wrapper = |
| 100 | WebDataServiceFactory::GetForProfile(profile, access_type); |
| 101 | // |wrapper| can be null in Incognito mode. |
| 102 | return wrapper ? wrapper->GetAccountAutofillWebData() |
Rouslan Solomakhin | de01253 | 2017-09-20 15:18:34 | [diff] [blame] | 103 | : scoped_refptr<autofill::AutofillWebDataService>(nullptr); |
[email protected] | 34b5946 | 2013-12-03 14:08:32 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // static |
[email protected] | 37b32460 | 2014-07-02 07:30:49 | [diff] [blame] | 107 | scoped_refptr<KeywordWebDataService> |
| 108 | WebDataServiceFactory::GetKeywordWebDataForProfile( |
| 109 | Profile* profile, |
sdefresne | e9ea3c2 | 2015-01-10 10:10:04 | [diff] [blame] | 110 | ServiceAccessType access_type) { |
[email protected] | 37b32460 | 2014-07-02 07:30:49 | [diff] [blame] | 111 | WebDataServiceWrapper* wrapper = |
| 112 | WebDataServiceFactory::GetForProfile(profile, access_type); |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 113 | // |wrapper| can be null in Incognito mode. |
Rouslan Solomakhin | de01253 | 2017-09-20 15:18:34 | [diff] [blame] | 114 | return wrapper ? wrapper->GetKeywordWebData() |
| 115 | : scoped_refptr<KeywordWebDataService>(nullptr); |
[email protected] | 37b32460 | 2014-07-02 07:30:49 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // static |
[email protected] | bd386e6 | 2014-07-09 22:17:06 | [diff] [blame] | 119 | scoped_refptr<TokenWebData> WebDataServiceFactory::GetTokenWebDataForProfile( |
[email protected] | 1be4efd | 2014-01-09 12:43:44 | [diff] [blame] | 120 | Profile* profile, |
sdefresne | e9ea3c2 | 2015-01-10 10:10:04 | [diff] [blame] | 121 | ServiceAccessType access_type) { |
[email protected] | 1be4efd | 2014-01-09 12:43:44 | [diff] [blame] | 122 | WebDataServiceWrapper* wrapper = |
| 123 | WebDataServiceFactory::GetForProfile(profile, access_type); |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 124 | // |wrapper| can be null in Incognito mode. |
Rouslan Solomakhin | de01253 | 2017-09-20 15:18:34 | [diff] [blame] | 125 | return wrapper ? wrapper->GetTokenWebData() |
| 126 | : scoped_refptr<TokenWebData>(nullptr); |
[email protected] | 1be4efd | 2014-01-09 12:43:44 | [diff] [blame] | 127 | } |
| 128 | |
gogerald | 7947209 | 2017-04-27 15:38:16 | [diff] [blame] | 129 | // static |
[email protected] | d07edd4 | 2012-05-14 23:49:46 | [diff] [blame] | 130 | WebDataServiceFactory* WebDataServiceFactory::GetInstance() { |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 131 | return base::Singleton<WebDataServiceFactory>::get(); |
[email protected] | d07edd4 | 2012-05-14 23:49:46 | [diff] [blame] | 132 | } |
| 133 | |
[email protected] | 018bf65 | 2013-05-03 23:18:34 | [diff] [blame] | 134 | content::BrowserContext* WebDataServiceFactory::GetBrowserContextToUse( |
| 135 | content::BrowserContext* context) const { |
| 136 | return chrome::GetBrowserContextRedirectedInIncognito(context); |
[email protected] | d07edd4 | 2012-05-14 23:49:46 | [diff] [blame] | 137 | } |
| 138 | |
[email protected] | 540380fc | 2014-03-14 10:10:34 | [diff] [blame] | 139 | KeyedService* WebDataServiceFactory::BuildServiceInstanceFor( |
sdefresne | f8cf5be | 2014-12-16 20:08:09 | [diff] [blame] | 140 | content::BrowserContext* context) const { |
| 141 | const base::FilePath& profile_path = context->GetPath(); |
Gabriel Charette | e7cdc5cd | 2020-05-27 23:35:05 | [diff] [blame] | 142 | return new WebDataServiceWrapper(profile_path, |
| 143 | g_browser_process->GetApplicationLocale(), |
| 144 | content::GetUIThreadTaskRunner({}), |
| 145 | base::BindRepeating(&ProfileErrorCallback)); |
[email protected] | d07edd4 | 2012-05-14 23:49:46 | [diff] [blame] | 146 | } |
| 147 | |
[email protected] | bb05cae1 | 2012-09-06 00:37:52 | [diff] [blame] | 148 | bool WebDataServiceFactory::ServiceIsNULLWhileTesting() const { |
[email protected] | d07edd4 | 2012-05-14 23:49:46 | [diff] [blame] | 149 | return true; |
| 150 | } |