[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] | 67c7e0a | 2013-05-04 13:56:41 | [diff] [blame] | 15 | #include "chrome/browser/sync/glue/sync_start_util.h" |
[email protected] | 486295a | 2013-03-20 23:35:49 | [diff] [blame] | 16 | #include "chrome/browser/ui/profile_error_dialog.h" |
sdefresne | f8cf5be | 2014-12-16 20:08:09 | [diff] [blame] | 17 | #include "components/autofill/core/browser/webdata/autofill_webdata_service.h" |
[email protected] | 540380fc | 2014-03-14 10:10:34 | [diff] [blame] | 18 | #include "components/keyed_service/content/browser_context_dependency_manager.h" |
Rouslan Solomakhin | de01253 | 2017-09-20 15:18:34 | [diff] [blame] | 19 | #include "components/payments/content/payment_manifest_web_data_service.h" |
[email protected] | bf5c532d | 2014-07-05 00:29:53 | [diff] [blame] | 20 | #include "components/search_engines/keyword_web_data_service.h" |
[email protected] | 7274ef0 | 2014-03-24 22:43:40 | [diff] [blame] | 21 | #include "components/signin/core/browser/webdata/token_web_data.h" |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 22 | #include "components/webdata_services/web_data_service_wrapper.h" |
[email protected] | 8550f06 | 2013-03-20 23:51:05 | [diff] [blame] | 23 | #include "content/public/browser/browser_thread.h" |
[email protected] | 486295a | 2013-03-20 23:35:49 | [diff] [blame] | 24 | |
[email protected] | bd386e6 | 2014-07-09 22:17:06 | [diff] [blame] | 25 | #if defined(OS_WIN) |
[email protected] | b646c85 | 2014-07-17 06:19:47 | [diff] [blame] | 26 | #include "components/password_manager/core/browser/webdata/password_web_data_service_win.h" |
[email protected] | bd386e6 | 2014-07-09 22:17:06 | [diff] [blame] | 27 | #endif |
| 28 | |
[email protected] | 8550f06 | 2013-03-20 23:51:05 | [diff] [blame] | 29 | using content::BrowserThread; |
| 30 | |
[email protected] | 486295a | 2013-03-20 23:35:49 | [diff] [blame] | 31 | namespace { |
| 32 | |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 33 | // Converts a WebDataServiceWrapper::ErrorType to ProfileErrorType. |
| 34 | ProfileErrorType ProfileErrorFromWebDataServiceWrapperError( |
| 35 | WebDataServiceWrapper::ErrorType error_type) { |
| 36 | switch (error_type) { |
| 37 | case WebDataServiceWrapper::ERROR_LOADING_AUTOFILL: |
tfarina | 87b7a3f | 2016-10-21 01:35:24 | [diff] [blame] | 38 | return ProfileErrorType::DB_AUTOFILL_WEB_DATA; |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 39 | |
| 40 | case WebDataServiceWrapper::ERROR_LOADING_KEYWORD: |
tfarina | 87b7a3f | 2016-10-21 01:35:24 | [diff] [blame] | 41 | return ProfileErrorType::DB_KEYWORD_WEB_DATA; |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 42 | |
| 43 | case WebDataServiceWrapper::ERROR_LOADING_TOKEN: |
tfarina | 87b7a3f | 2016-10-21 01:35:24 | [diff] [blame] | 44 | return ProfileErrorType::DB_TOKEN_WEB_DATA; |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 45 | |
| 46 | case WebDataServiceWrapper::ERROR_LOADING_PASSWORD: |
tfarina | 87b7a3f | 2016-10-21 01:35:24 | [diff] [blame] | 47 | return ProfileErrorType::DB_WEB_DATA; |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 48 | |
gogerald | 7947209 | 2017-04-27 15:38:16 | [diff] [blame] | 49 | case WebDataServiceWrapper::ERROR_LOADING_PAYMENT_MANIFEST: |
| 50 | return ProfileErrorType::DB_PAYMENT_MANIFEST_WEB_DATA; |
| 51 | |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 52 | default: |
Rouslan Solomakhin | de01253 | 2017-09-20 15:18:34 | [diff] [blame] | 53 | NOTREACHED() << "Unknown WebDataServiceWrapper::ErrorType: " |
| 54 | << error_type; |
tfarina | 87b7a3f | 2016-10-21 01:35:24 | [diff] [blame] | 55 | return ProfileErrorType::DB_WEB_DATA; |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
[email protected] | 486295a | 2013-03-20 23:35:49 | [diff] [blame] | 59 | // Callback to show error dialog on profile load error. |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 60 | void ProfileErrorCallback(WebDataServiceWrapper::ErrorType error_type, |
afakhry | 7c9abe7 | 2016-08-05 17:33:19 | [diff] [blame] | 61 | sql::InitStatus status, |
| 62 | const std::string& diagnostics) { |
| 63 | ShowProfileErrorDialog(ProfileErrorFromWebDataServiceWrapperError(error_type), |
afakhry | a592d29c | 2016-08-22 19:06:57 | [diff] [blame] | 64 | SqlInitStatusToMessageId(status), diagnostics); |
[email protected] | 486295a | 2013-03-20 23:35:49 | [diff] [blame] | 65 | } |
| 66 | |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 67 | } // namespace |
| 68 | |
[email protected] | d07edd4 | 2012-05-14 23:49:46 | [diff] [blame] | 69 | WebDataServiceFactory::WebDataServiceFactory() |
[email protected] | f1484c5 | 2013-05-22 23:25:44 | [diff] [blame] | 70 | : BrowserContextKeyedServiceFactory( |
[email protected] | bd386e6 | 2014-07-09 22:17:06 | [diff] [blame] | 71 | "WebDataService", |
| 72 | BrowserContextDependencyManager::GetInstance()) { |
[email protected] | dd3c53f | 2013-03-14 02:59:41 | [diff] [blame] | 73 | // WebDataServiceFactory has no dependecies. |
[email protected] | d07edd4 | 2012-05-14 23:49:46 | [diff] [blame] | 74 | } |
| 75 | |
Rouslan Solomakhin | de01253 | 2017-09-20 15:18:34 | [diff] [blame] | 76 | WebDataServiceFactory::~WebDataServiceFactory() {} |
[email protected] | d07edd4 | 2012-05-14 23:49:46 | [diff] [blame] | 77 | |
| 78 | // static |
[email protected] | e2b3105 | 2013-03-25 01:49:37 | [diff] [blame] | 79 | WebDataServiceWrapper* WebDataServiceFactory::GetForProfile( |
[email protected] | 34b5946 | 2013-12-03 14:08:32 | [diff] [blame] | 80 | Profile* profile, |
sdefresne | e9ea3c2 | 2015-01-10 10:10:04 | [diff] [blame] | 81 | ServiceAccessType access_type) { |
[email protected] | 66769db | 2012-09-13 17:06:19 | [diff] [blame] | 82 | // If |access_type| starts being used for anything other than this |
| 83 | // DCHECK, we need to start taking it as a parameter to |
[email protected] | 34b5946 | 2013-12-03 14:08:32 | [diff] [blame] | 84 | // the *WebDataService::FromBrowserContext() functions (see above). |
sdefresne | e9ea3c2 | 2015-01-10 10:10:04 | [diff] [blame] | 85 | DCHECK(access_type != ServiceAccessType::IMPLICIT_ACCESS || |
| 86 | !profile->IsOffTheRecord()); |
[email protected] | e2b3105 | 2013-03-25 01:49:37 | [diff] [blame] | 87 | return static_cast<WebDataServiceWrapper*>( |
[email protected] | bd386e6 | 2014-07-09 22:17:06 | [diff] [blame] | 88 | GetInstance()->GetServiceForBrowserContext(profile, true)); |
[email protected] | d07edd4 | 2012-05-14 23:49:46 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // static |
[email protected] | e2b3105 | 2013-03-25 01:49:37 | [diff] [blame] | 92 | WebDataServiceWrapper* WebDataServiceFactory::GetForProfileIfExists( |
[email protected] | 34b5946 | 2013-12-03 14:08:32 | [diff] [blame] | 93 | Profile* profile, |
sdefresne | e9ea3c2 | 2015-01-10 10:10:04 | [diff] [blame] | 94 | ServiceAccessType access_type) { |
[email protected] | dd3c53f | 2013-03-14 02:59:41 | [diff] [blame] | 95 | // If |access_type| starts being used for anything other than this |
| 96 | // DCHECK, we need to start taking it as a parameter to |
[email protected] | 34b5946 | 2013-12-03 14:08:32 | [diff] [blame] | 97 | // the *WebDataService::FromBrowserContext() functions (see above). |
sdefresne | e9ea3c2 | 2015-01-10 10:10:04 | [diff] [blame] | 98 | DCHECK(access_type != ServiceAccessType::IMPLICIT_ACCESS || |
| 99 | !profile->IsOffTheRecord()); |
[email protected] | e2b3105 | 2013-03-25 01:49:37 | [diff] [blame] | 100 | return static_cast<WebDataServiceWrapper*>( |
[email protected] | bd386e6 | 2014-07-09 22:17:06 | [diff] [blame] | 101 | GetInstance()->GetServiceForBrowserContext(profile, false)); |
[email protected] | dd3c53f | 2013-03-14 02:59:41 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // static |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 105 | scoped_refptr<autofill::AutofillWebDataService> |
[email protected] | 34b5946 | 2013-12-03 14:08:32 | [diff] [blame] | 106 | WebDataServiceFactory::GetAutofillWebDataForProfile( |
| 107 | Profile* profile, |
sdefresne | e9ea3c2 | 2015-01-10 10:10:04 | [diff] [blame] | 108 | ServiceAccessType access_type) { |
[email protected] | 34b5946 | 2013-12-03 14:08:32 | [diff] [blame] | 109 | WebDataServiceWrapper* wrapper = |
| 110 | WebDataServiceFactory::GetForProfile(profile, access_type); |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 111 | // |wrapper| can be null in Incognito mode. |
Rouslan Solomakhin | de01253 | 2017-09-20 15:18:34 | [diff] [blame] | 112 | return wrapper ? wrapper->GetAutofillWebData() |
| 113 | : scoped_refptr<autofill::AutofillWebDataService>(nullptr); |
[email protected] | 34b5946 | 2013-12-03 14:08:32 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // static |
[email protected] | 37b32460 | 2014-07-02 07:30:49 | [diff] [blame] | 117 | scoped_refptr<KeywordWebDataService> |
| 118 | WebDataServiceFactory::GetKeywordWebDataForProfile( |
| 119 | Profile* profile, |
sdefresne | e9ea3c2 | 2015-01-10 10:10:04 | [diff] [blame] | 120 | ServiceAccessType access_type) { |
[email protected] | 37b32460 | 2014-07-02 07:30:49 | [diff] [blame] | 121 | WebDataServiceWrapper* wrapper = |
| 122 | WebDataServiceFactory::GetForProfile(profile, access_type); |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 123 | // |wrapper| can be null in Incognito mode. |
Rouslan Solomakhin | de01253 | 2017-09-20 15:18:34 | [diff] [blame] | 124 | return wrapper ? wrapper->GetKeywordWebData() |
| 125 | : scoped_refptr<KeywordWebDataService>(nullptr); |
[email protected] | 37b32460 | 2014-07-02 07:30:49 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | // static |
[email protected] | bd386e6 | 2014-07-09 22:17:06 | [diff] [blame] | 129 | scoped_refptr<TokenWebData> WebDataServiceFactory::GetTokenWebDataForProfile( |
[email protected] | 1be4efd | 2014-01-09 12:43:44 | [diff] [blame] | 130 | Profile* profile, |
sdefresne | e9ea3c2 | 2015-01-10 10:10:04 | [diff] [blame] | 131 | ServiceAccessType access_type) { |
[email protected] | 1be4efd | 2014-01-09 12:43:44 | [diff] [blame] | 132 | WebDataServiceWrapper* wrapper = |
| 133 | WebDataServiceFactory::GetForProfile(profile, access_type); |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 134 | // |wrapper| can be null in Incognito mode. |
Rouslan Solomakhin | de01253 | 2017-09-20 15:18:34 | [diff] [blame] | 135 | return wrapper ? wrapper->GetTokenWebData() |
| 136 | : scoped_refptr<TokenWebData>(nullptr); |
[email protected] | 1be4efd | 2014-01-09 12:43:44 | [diff] [blame] | 137 | } |
| 138 | |
[email protected] | bd386e6 | 2014-07-09 22:17:06 | [diff] [blame] | 139 | #if defined(OS_WIN) |
| 140 | // static |
| 141 | scoped_refptr<PasswordWebDataService> |
| 142 | WebDataServiceFactory::GetPasswordWebDataForProfile( |
| 143 | Profile* profile, |
sdefresne | e9ea3c2 | 2015-01-10 10:10:04 | [diff] [blame] | 144 | ServiceAccessType access_type) { |
[email protected] | bd386e6 | 2014-07-09 22:17:06 | [diff] [blame] | 145 | WebDataServiceWrapper* wrapper = |
| 146 | WebDataServiceFactory::GetForProfile(profile, access_type); |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 147 | // |wrapper| can be null in Incognito mode. |
Rouslan Solomakhin | de01253 | 2017-09-20 15:18:34 | [diff] [blame] | 148 | return wrapper ? wrapper->GetPasswordWebData() |
| 149 | : scoped_refptr<PasswordWebDataService>(nullptr); |
[email protected] | bd386e6 | 2014-07-09 22:17:06 | [diff] [blame] | 150 | } |
| 151 | #endif |
| 152 | |
gogerald | 7947209 | 2017-04-27 15:38:16 | [diff] [blame] | 153 | // static |
| 154 | scoped_refptr<payments::PaymentManifestWebDataService> |
| 155 | WebDataServiceFactory::GetPaymentManifestWebDataForProfile( |
| 156 | Profile* profile, |
| 157 | ServiceAccessType access_type) { |
| 158 | WebDataServiceWrapper* wrapper = |
| 159 | WebDataServiceFactory::GetForProfile(profile, access_type); |
| 160 | // |wrapper| can be null in Incognito mode. |
| 161 | return wrapper |
| 162 | ? wrapper->GetPaymentManifestWebData() |
| 163 | : scoped_refptr<payments::PaymentManifestWebDataService>(nullptr); |
| 164 | } |
gogerald | 7947209 | 2017-04-27 15:38:16 | [diff] [blame] | 165 | |
[email protected] | 1be4efd | 2014-01-09 12:43:44 | [diff] [blame] | 166 | // static |
[email protected] | d07edd4 | 2012-05-14 23:49:46 | [diff] [blame] | 167 | WebDataServiceFactory* WebDataServiceFactory::GetInstance() { |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 168 | return base::Singleton<WebDataServiceFactory>::get(); |
[email protected] | d07edd4 | 2012-05-14 23:49:46 | [diff] [blame] | 169 | } |
| 170 | |
[email protected] | 018bf65 | 2013-05-03 23:18:34 | [diff] [blame] | 171 | content::BrowserContext* WebDataServiceFactory::GetBrowserContextToUse( |
| 172 | content::BrowserContext* context) const { |
| 173 | return chrome::GetBrowserContextRedirectedInIncognito(context); |
[email protected] | d07edd4 | 2012-05-14 23:49:46 | [diff] [blame] | 174 | } |
| 175 | |
[email protected] | 540380fc | 2014-03-14 10:10:34 | [diff] [blame] | 176 | KeyedService* WebDataServiceFactory::BuildServiceInstanceFor( |
sdefresne | f8cf5be | 2014-12-16 20:08:09 | [diff] [blame] | 177 | content::BrowserContext* context) const { |
| 178 | const base::FilePath& profile_path = context->GetPath(); |
| 179 | return new WebDataServiceWrapper( |
| 180 | profile_path, g_browser_process->GetApplicationLocale(), |
thestig | 529ad8a | 2016-07-08 20:30:12 | [diff] [blame] | 181 | BrowserThread::GetTaskRunnerForThread(BrowserThread::UI), |
sdefresne | f8cf5be | 2014-12-16 20:08:09 | [diff] [blame] | 182 | sync_start_util::GetFlareForSyncableService(profile_path), |
sdefresne | cb955cd | 2014-12-15 23:21:56 | [diff] [blame] | 183 | &ProfileErrorCallback); |
[email protected] | d07edd4 | 2012-05-14 23:49:46 | [diff] [blame] | 184 | } |
| 185 | |
[email protected] | bb05cae1 | 2012-09-06 00:37:52 | [diff] [blame] | 186 | bool WebDataServiceFactory::ServiceIsNULLWhileTesting() const { |
[email protected] | d07edd4 | 2012-05-14 23:49:46 | [diff] [blame] | 187 | return true; |
| 188 | } |