blob: 5975365b48b8c1517937522c6afa4b9a7a12117e [file] [log] [blame]
[email protected]d07edd42012-05-14 23:49:461// 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
tfarinabf215a6f2015-06-15 22:24:055#include "chrome/browser/web_data_service_factory.h"
[email protected]d07edd42012-05-14 23:49:466
[email protected]486295a2013-03-20 23:35:497#include "base/bind.h"
[email protected]57999812013-02-24 05:40:528#include "base/files/file_path.h"
sdefresnecb955cd2014-12-15 23:21:569#include "base/memory/singleton.h"
avi664c07b2015-12-26 02:18:3110#include "build/build_config.h"
[email protected]ed005f6f2013-04-05 17:03:5611#include "chrome/browser/browser_process.h"
[email protected]018bf652013-05-03 23:18:3412#include "chrome/browser/profiles/incognito_helpers.h"
sdefresnee9ea3c22015-01-10 10:10:0413#include "chrome/browser/profiles/profile.h"
afakhrya592d29c2016-08-22 19:06:5714#include "chrome/browser/profiles/sql_init_error_message_ids.h"
[email protected]486295a2013-03-20 23:35:4915#include "chrome/browser/ui/profile_error_dialog.h"
sdefresnef8cf5be2014-12-16 20:08:0916#include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
[email protected]bf5c532d2014-07-05 00:29:5317#include "components/search_engines/keyword_web_data_service.h"
Colin Blundell2cdc91a2019-07-12 15:24:2718#include "components/signin/public/webdata/token_web_data.h"
sdefresnecb955cd2014-12-15 23:21:5619#include "components/webdata_services/web_data_service_wrapper.h"
Eric Seckler8652dcd52018-09-20 10:42:2820#include "content/public/browser/browser_task_traits.h"
[email protected]8550f062013-03-20 23:51:0521#include "content/public/browser/browser_thread.h"
[email protected]486295a2013-03-20 23:35:4922
23namespace {
24
sdefresnecb955cd2014-12-15 23:21:5625// Converts a WebDataServiceWrapper::ErrorType to ProfileErrorType.
26ProfileErrorType ProfileErrorFromWebDataServiceWrapperError(
27 WebDataServiceWrapper::ErrorType error_type) {
28 switch (error_type) {
29 case WebDataServiceWrapper::ERROR_LOADING_AUTOFILL:
tfarina87b7a3f2016-10-21 01:35:2430 return ProfileErrorType::DB_AUTOFILL_WEB_DATA;
sdefresnecb955cd2014-12-15 23:21:5631
Florian Uunk75c22c92018-06-25 12:44:1032 case WebDataServiceWrapper::ERROR_LOADING_ACCOUNT_AUTOFILL:
33 return ProfileErrorType::DB_ACCOUNT_AUTOFILL_WEB_DATA;
34
sdefresnecb955cd2014-12-15 23:21:5635 case WebDataServiceWrapper::ERROR_LOADING_KEYWORD:
tfarina87b7a3f2016-10-21 01:35:2436 return ProfileErrorType::DB_KEYWORD_WEB_DATA;
sdefresnecb955cd2014-12-15 23:21:5637
38 case WebDataServiceWrapper::ERROR_LOADING_TOKEN:
tfarina87b7a3f2016-10-21 01:35:2439 return ProfileErrorType::DB_TOKEN_WEB_DATA;
sdefresnecb955cd2014-12-15 23:21:5640
41 case WebDataServiceWrapper::ERROR_LOADING_PASSWORD:
tfarina87b7a3f2016-10-21 01:35:2442 return ProfileErrorType::DB_WEB_DATA;
sdefresnecb955cd2014-12-15 23:21:5643
gogerald79472092017-04-27 15:38:1644 case WebDataServiceWrapper::ERROR_LOADING_PAYMENT_MANIFEST:
45 return ProfileErrorType::DB_PAYMENT_MANIFEST_WEB_DATA;
46
sdefresnecb955cd2014-12-15 23:21:5647 default:
Rouslan Solomakhinde012532017-09-20 15:18:3448 NOTREACHED() << "Unknown WebDataServiceWrapper::ErrorType: "
49 << error_type;
tfarina87b7a3f2016-10-21 01:35:2450 return ProfileErrorType::DB_WEB_DATA;
sdefresnecb955cd2014-12-15 23:21:5651 }
52}
53
[email protected]486295a2013-03-20 23:35:4954// Callback to show error dialog on profile load error.
sdefresnecb955cd2014-12-15 23:21:5655void ProfileErrorCallback(WebDataServiceWrapper::ErrorType error_type,
afakhry7c9abe72016-08-05 17:33:1956 sql::InitStatus status,
57 const std::string& diagnostics) {
58 ShowProfileErrorDialog(ProfileErrorFromWebDataServiceWrapperError(error_type),
afakhrya592d29c2016-08-22 19:06:5759 SqlInitStatusToMessageId(status), diagnostics);
[email protected]486295a2013-03-20 23:35:4960}
61
sdefresnecb955cd2014-12-15 23:21:5662} // namespace
63
Richard Knolldb1fa8a2021-04-23 08:08:2864WebDataServiceFactory::WebDataServiceFactory() = default;
[email protected]d07edd42012-05-14 23:49:4665
Richard Knolldb1fa8a2021-04-23 08:08:2866WebDataServiceFactory::~WebDataServiceFactory() = default;
[email protected]d07edd42012-05-14 23:49:4667
68// static
[email protected]e2b31052013-03-25 01:49:3769WebDataServiceWrapper* WebDataServiceFactory::GetForProfile(
[email protected]34b59462013-12-03 14:08:3270 Profile* profile,
sdefresnee9ea3c22015-01-10 10:10:0471 ServiceAccessType access_type) {
Richard Knolldb1fa8a2021-04-23 08:08:2872 return GetForBrowserContext(profile, access_type);
[email protected]d07edd42012-05-14 23:49:4673}
74
75// static
[email protected]e2b31052013-03-25 01:49:3776WebDataServiceWrapper* WebDataServiceFactory::GetForProfileIfExists(
[email protected]34b59462013-12-03 14:08:3277 Profile* profile,
sdefresnee9ea3c22015-01-10 10:10:0478 ServiceAccessType access_type) {
Richard Knolldb1fa8a2021-04-23 08:08:2879 return GetForBrowserContextIfExists(profile, access_type);
[email protected]dd3c53f2013-03-14 02:59:4180}
81
82// static
sdefresnecb955cd2014-12-15 23:21:5683scoped_refptr<autofill::AutofillWebDataService>
[email protected]34b59462013-12-03 14:08:3284WebDataServiceFactory::GetAutofillWebDataForProfile(
85 Profile* profile,
sdefresnee9ea3c22015-01-10 10:10:0486 ServiceAccessType access_type) {
[email protected]34b59462013-12-03 14:08:3287 WebDataServiceWrapper* wrapper =
88 WebDataServiceFactory::GetForProfile(profile, access_type);
sdefresnecb955cd2014-12-15 23:21:5689 // |wrapper| can be null in Incognito mode.
Florian Uunk75c22c92018-06-25 12:44:1090 return wrapper ? wrapper->GetProfileAutofillWebData()
91 : scoped_refptr<autofill::AutofillWebDataService>(nullptr);
92}
93
94// static
95scoped_refptr<autofill::AutofillWebDataService>
96WebDataServiceFactory::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 Solomakhinde012532017-09-20 15:18:34103 : scoped_refptr<autofill::AutofillWebDataService>(nullptr);
[email protected]34b59462013-12-03 14:08:32104}
105
106// static
[email protected]37b324602014-07-02 07:30:49107scoped_refptr<KeywordWebDataService>
108WebDataServiceFactory::GetKeywordWebDataForProfile(
109 Profile* profile,
sdefresnee9ea3c22015-01-10 10:10:04110 ServiceAccessType access_type) {
[email protected]37b324602014-07-02 07:30:49111 WebDataServiceWrapper* wrapper =
112 WebDataServiceFactory::GetForProfile(profile, access_type);
sdefresnecb955cd2014-12-15 23:21:56113 // |wrapper| can be null in Incognito mode.
Rouslan Solomakhinde012532017-09-20 15:18:34114 return wrapper ? wrapper->GetKeywordWebData()
115 : scoped_refptr<KeywordWebDataService>(nullptr);
[email protected]37b324602014-07-02 07:30:49116}
117
118// static
[email protected]bd386e62014-07-09 22:17:06119scoped_refptr<TokenWebData> WebDataServiceFactory::GetTokenWebDataForProfile(
[email protected]1be4efd2014-01-09 12:43:44120 Profile* profile,
sdefresnee9ea3c22015-01-10 10:10:04121 ServiceAccessType access_type) {
[email protected]1be4efd2014-01-09 12:43:44122 WebDataServiceWrapper* wrapper =
123 WebDataServiceFactory::GetForProfile(profile, access_type);
sdefresnecb955cd2014-12-15 23:21:56124 // |wrapper| can be null in Incognito mode.
Rouslan Solomakhinde012532017-09-20 15:18:34125 return wrapper ? wrapper->GetTokenWebData()
126 : scoped_refptr<TokenWebData>(nullptr);
[email protected]1be4efd2014-01-09 12:43:44127}
128
gogerald79472092017-04-27 15:38:16129// static
[email protected]d07edd42012-05-14 23:49:46130WebDataServiceFactory* WebDataServiceFactory::GetInstance() {
olli.raula36aa8be2015-09-10 11:14:22131 return base::Singleton<WebDataServiceFactory>::get();
[email protected]d07edd42012-05-14 23:49:46132}
133
[email protected]018bf652013-05-03 23:18:34134content::BrowserContext* WebDataServiceFactory::GetBrowserContextToUse(
135 content::BrowserContext* context) const {
136 return chrome::GetBrowserContextRedirectedInIncognito(context);
[email protected]d07edd42012-05-14 23:49:46137}
138
[email protected]540380fc2014-03-14 10:10:34139KeyedService* WebDataServiceFactory::BuildServiceInstanceFor(
sdefresnef8cf5be2014-12-16 20:08:09140 content::BrowserContext* context) const {
141 const base::FilePath& profile_path = context->GetPath();
Gabriel Charettee7cdc5cd2020-05-27 23:35:05142 return new WebDataServiceWrapper(profile_path,
143 g_browser_process->GetApplicationLocale(),
144 content::GetUIThreadTaskRunner({}),
145 base::BindRepeating(&ProfileErrorCallback));
[email protected]d07edd42012-05-14 23:49:46146}
147
[email protected]bb05cae12012-09-06 00:37:52148bool WebDataServiceFactory::ServiceIsNULLWhileTesting() const {
[email protected]d07edd42012-05-14 23:49:46149 return true;
150}