1 // Copyright 2012 The Chromium Authors
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"
7 #include "base/files/file_path.h"
8 #include "base/functional/bind.h"
9 #include "base/no_destructor.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/profiles/profile_error_dialog.h"
16 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
17 #include "components/search_engines/keyword_web_data_service.h"
18 #include "components/signin/public/webdata/token_web_data.h"
19 #include "components/webdata_services/web_data_service_wrapper.h"
20 #include "content/public/browser/browser_task_traits.h"
21 #include "content/public/browser/browser_thread.h"
25 // Converts a WebDataServiceWrapper::ErrorType to ProfileErrorType.
26 ProfileErrorType ProfileErrorFromWebDataServiceWrapperError(
27 WebDataServiceWrapper::ErrorType error_type) {
29 case WebDataServiceWrapper::ERROR_LOADING_AUTOFILL:
30 return ProfileErrorType::DB_AUTOFILL_WEB_DATA;
32 case WebDataServiceWrapper::ERROR_LOADING_ACCOUNT_AUTOFILL:
33 return ProfileErrorType::DB_ACCOUNT_AUTOFILL_WEB_DATA;
35 case WebDataServiceWrapper::ERROR_LOADING_KEYWORD:
36 return ProfileErrorType::DB_KEYWORD_WEB_DATA;
38 case WebDataServiceWrapper::ERROR_LOADING_TOKEN:
39 return ProfileErrorType::DB_TOKEN_WEB_DATA;
41 case WebDataServiceWrapper::ERROR_LOADING_PASSWORD:
42 return ProfileErrorType::DB_WEB_DATA;
44 case WebDataServiceWrapper::ERROR_LOADING_PAYMENT_MANIFEST:
45 return ProfileErrorType::DB_PAYMENT_MANIFEST_WEB_DATA;
48 NOTREACHED() << "Unknown WebDataServiceWrapper::ErrorType: "
50 return ProfileErrorType::DB_WEB_DATA;
54 // Callback to show error dialog on profile load error.
55 void ProfileErrorCallback(WebDataServiceWrapper::ErrorType error_type,
56 sql::InitStatus status,
57 const std::string& diagnostics) {
58 ShowProfileErrorDialog(ProfileErrorFromWebDataServiceWrapperError(error_type),
59 SqlInitStatusToMessageId(status), diagnostics);
62 std::unique_ptr<KeyedService> BuildWebDataService(
63 content::BrowserContext* context) {
64 const base::FilePath& profile_path = context->GetPath();
65 return std::make_unique<WebDataServiceWrapper>(
66 profile_path, g_browser_process->GetApplicationLocale(),
67 content::GetUIThreadTaskRunner({}),
68 base::BindRepeating(&ProfileErrorCallback));
73 WebDataServiceFactory::WebDataServiceFactory() = default;
75 WebDataServiceFactory::~WebDataServiceFactory() = default;
78 WebDataServiceWrapper* WebDataServiceFactory::GetForProfile(
80 ServiceAccessType access_type) {
81 return GetForBrowserContext(profile, access_type);
85 WebDataServiceWrapper* WebDataServiceFactory::GetForProfileIfExists(
87 ServiceAccessType access_type) {
88 return GetForBrowserContextIfExists(profile, access_type);
92 scoped_refptr<autofill::AutofillWebDataService>
93 WebDataServiceFactory::GetAutofillWebDataForProfile(
95 ServiceAccessType access_type) {
96 WebDataServiceWrapper* wrapper =
97 WebDataServiceFactory::GetForProfile(profile, access_type);
98 // |wrapper| can be null in Incognito mode.
99 return wrapper ? wrapper->GetProfileAutofillWebData()
100 : scoped_refptr<autofill::AutofillWebDataService>(nullptr);
104 scoped_refptr<autofill::AutofillWebDataService>
105 WebDataServiceFactory::GetAutofillWebDataForAccount(
107 ServiceAccessType access_type) {
108 WebDataServiceWrapper* wrapper =
109 WebDataServiceFactory::GetForProfile(profile, access_type);
110 // |wrapper| can be null in Incognito mode.
111 return wrapper ? wrapper->GetAccountAutofillWebData()
112 : scoped_refptr<autofill::AutofillWebDataService>(nullptr);
116 scoped_refptr<KeywordWebDataService>
117 WebDataServiceFactory::GetKeywordWebDataForProfile(
119 ServiceAccessType access_type) {
120 WebDataServiceWrapper* wrapper =
121 WebDataServiceFactory::GetForProfile(profile, access_type);
122 // |wrapper| can be null in Incognito mode.
123 return wrapper ? wrapper->GetKeywordWebData()
124 : scoped_refptr<KeywordWebDataService>(nullptr);
128 scoped_refptr<TokenWebData> WebDataServiceFactory::GetTokenWebDataForProfile(
130 ServiceAccessType access_type) {
131 WebDataServiceWrapper* wrapper =
132 WebDataServiceFactory::GetForProfile(profile, access_type);
133 // |wrapper| can be null in Incognito mode.
134 return wrapper ? wrapper->GetTokenWebData()
135 : scoped_refptr<TokenWebData>(nullptr);
139 WebDataServiceFactory* WebDataServiceFactory::GetInstance() {
140 static base::NoDestructor<WebDataServiceFactory> instance;
141 return instance.get();
145 BrowserContextKeyedServiceFactory::TestingFactory
146 WebDataServiceFactory::GetDefaultFactory() {
147 return base::BindRepeating(&BuildWebDataService);
150 content::BrowserContext* WebDataServiceFactory::GetBrowserContextToUse(
151 content::BrowserContext* context) const {
152 return chrome::GetBrowserContextRedirectedInIncognito(context);
155 std::unique_ptr<KeyedService>
156 WebDataServiceFactory::BuildServiceInstanceForBrowserContext(
157 content::BrowserContext* context) const {
158 return BuildWebDataService(context);
161 bool WebDataServiceFactory::ServiceIsNULLWhileTesting() const {