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