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.
5 #include "chrome/browser/web_data_service_factory.h"
8 #include "base/files/file_path.h"
9 #include "base/memory/singleton.h"
10 #include "build/build_config.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/profiles/incognito_helpers.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/profiles/sql_init_error_message_ids.h"
15 #include "chrome/browser/ui/profile_error_dialog.h"
16 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
17 #include "components/keyed_service/content/browser_context_dependency_manager.h"
18 #include "components/payments/content/payment_manifest_web_data_service.h"
19 #include "components/search_engines/keyword_web_data_service.h"
20 #include "components/signin/public/webdata/token_web_data.h"
21 #include "components/webdata_services/web_data_service_wrapper.h"
22 #include "content/public/browser/browser_task_traits.h"
23 #include "content/public/browser/browser_thread.h"
27 // Converts a WebDataServiceWrapper::ErrorType to ProfileErrorType.
28 ProfileErrorType ProfileErrorFromWebDataServiceWrapperError(
29 WebDataServiceWrapper::ErrorType error_type) {
31 case WebDataServiceWrapper::ERROR_LOADING_AUTOFILL:
32 return ProfileErrorType::DB_AUTOFILL_WEB_DATA;
34 case WebDataServiceWrapper::ERROR_LOADING_ACCOUNT_AUTOFILL:
35 return ProfileErrorType::DB_ACCOUNT_AUTOFILL_WEB_DATA;
37 case WebDataServiceWrapper::ERROR_LOADING_KEYWORD:
38 return ProfileErrorType::DB_KEYWORD_WEB_DATA;
40 case WebDataServiceWrapper::ERROR_LOADING_TOKEN:
41 return ProfileErrorType::DB_TOKEN_WEB_DATA;
43 case WebDataServiceWrapper::ERROR_LOADING_PASSWORD:
44 return ProfileErrorType::DB_WEB_DATA;
46 case WebDataServiceWrapper::ERROR_LOADING_PAYMENT_MANIFEST:
47 return ProfileErrorType::DB_PAYMENT_MANIFEST_WEB_DATA;
50 NOTREACHED() << "Unknown WebDataServiceWrapper::ErrorType: "
52 return ProfileErrorType::DB_WEB_DATA;
56 // Callback to show error dialog on profile load error.
57 void ProfileErrorCallback(WebDataServiceWrapper::ErrorType error_type,
58 sql::InitStatus status,
59 const std::string& diagnostics) {
60 ShowProfileErrorDialog(ProfileErrorFromWebDataServiceWrapperError(error_type),
61 SqlInitStatusToMessageId(status), diagnostics);
66 WebDataServiceFactory::WebDataServiceFactory()
67 : BrowserContextKeyedServiceFactory(
69 BrowserContextDependencyManager::GetInstance()) {
70 // WebDataServiceFactory has no dependecies.
73 WebDataServiceFactory::~WebDataServiceFactory() {}
76 WebDataServiceWrapper* WebDataServiceFactory::GetForProfile(
78 ServiceAccessType access_type) {
79 // If |access_type| starts being used for anything other than this
80 // DCHECK, we need to start taking it as a parameter to
81 // the *WebDataService::FromBrowserContext() functions (see above).
82 DCHECK(access_type != ServiceAccessType::IMPLICIT_ACCESS ||
83 !profile->IsOffTheRecord());
84 return static_cast<WebDataServiceWrapper*>(
85 GetInstance()->GetServiceForBrowserContext(profile, true));
89 WebDataServiceWrapper* WebDataServiceFactory::GetForProfileIfExists(
91 ServiceAccessType access_type) {
92 // If |access_type| starts being used for anything other than this
93 // DCHECK, we need to start taking it as a parameter to
94 // the *WebDataService::FromBrowserContext() functions (see above).
95 DCHECK(access_type != ServiceAccessType::IMPLICIT_ACCESS ||
96 !profile->IsOffTheRecord());
97 return static_cast<WebDataServiceWrapper*>(
98 GetInstance()->GetServiceForBrowserContext(profile, false));
102 scoped_refptr<autofill::AutofillWebDataService>
103 WebDataServiceFactory::GetAutofillWebDataForProfile(
105 ServiceAccessType access_type) {
106 WebDataServiceWrapper* wrapper =
107 WebDataServiceFactory::GetForProfile(profile, access_type);
108 // |wrapper| can be null in Incognito mode.
109 return wrapper ? wrapper->GetProfileAutofillWebData()
110 : scoped_refptr<autofill::AutofillWebDataService>(nullptr);
114 scoped_refptr<autofill::AutofillWebDataService>
115 WebDataServiceFactory::GetAutofillWebDataForAccount(
117 ServiceAccessType access_type) {
118 WebDataServiceWrapper* wrapper =
119 WebDataServiceFactory::GetForProfile(profile, access_type);
120 // |wrapper| can be null in Incognito mode.
121 return wrapper ? wrapper->GetAccountAutofillWebData()
122 : scoped_refptr<autofill::AutofillWebDataService>(nullptr);
126 scoped_refptr<KeywordWebDataService>
127 WebDataServiceFactory::GetKeywordWebDataForProfile(
129 ServiceAccessType access_type) {
130 WebDataServiceWrapper* wrapper =
131 WebDataServiceFactory::GetForProfile(profile, access_type);
132 // |wrapper| can be null in Incognito mode.
133 return wrapper ? wrapper->GetKeywordWebData()
134 : scoped_refptr<KeywordWebDataService>(nullptr);
138 scoped_refptr<TokenWebData> WebDataServiceFactory::GetTokenWebDataForProfile(
140 ServiceAccessType access_type) {
141 WebDataServiceWrapper* wrapper =
142 WebDataServiceFactory::GetForProfile(profile, access_type);
143 // |wrapper| can be null in Incognito mode.
144 return wrapper ? wrapper->GetTokenWebData()
145 : scoped_refptr<TokenWebData>(nullptr);
149 scoped_refptr<payments::PaymentManifestWebDataService>
150 WebDataServiceFactory::GetPaymentManifestWebDataForProfile(
152 ServiceAccessType access_type) {
153 WebDataServiceWrapper* wrapper =
154 WebDataServiceFactory::GetForProfile(profile, access_type);
155 // |wrapper| can be null in Incognito mode.
157 ? wrapper->GetPaymentManifestWebData()
158 : scoped_refptr<payments::PaymentManifestWebDataService>(nullptr);
162 WebDataServiceFactory* WebDataServiceFactory::GetInstance() {
163 return base::Singleton<WebDataServiceFactory>::get();
166 content::BrowserContext* WebDataServiceFactory::GetBrowserContextToUse(
167 content::BrowserContext* context) const {
168 return chrome::GetBrowserContextRedirectedInIncognito(context);
171 KeyedService* WebDataServiceFactory::BuildServiceInstanceFor(
172 content::BrowserContext* context) const {
173 const base::FilePath& profile_path = context->GetPath();
174 return new WebDataServiceWrapper(profile_path,
175 g_browser_process->GetApplicationLocale(),
176 content::GetUIThreadTaskRunner({}),
177 base::BindRepeating(&ProfileErrorCallback));
180 bool WebDataServiceFactory::ServiceIsNULLWhileTesting() const {