blob: 8c24d1f28f173117757668af6d3b8085a78659f7 [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]67c7e0a2013-05-04 13:56:4115#include "chrome/browser/sync/glue/sync_start_util.h"
[email protected]486295a2013-03-20 23:35:4916#include "chrome/browser/ui/profile_error_dialog.h"
sdefresnef8cf5be2014-12-16 20:08:0917#include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
[email protected]540380fc2014-03-14 10:10:3418#include "components/keyed_service/content/browser_context_dependency_manager.h"
Rouslan Solomakhinde012532017-09-20 15:18:3419#include "components/payments/content/payment_manifest_web_data_service.h"
[email protected]bf5c532d2014-07-05 00:29:5320#include "components/search_engines/keyword_web_data_service.h"
[email protected]7274ef02014-03-24 22:43:4021#include "components/signin/core/browser/webdata/token_web_data.h"
sdefresnecb955cd2014-12-15 23:21:5622#include "components/webdata_services/web_data_service_wrapper.h"
[email protected]8550f062013-03-20 23:51:0523#include "content/public/browser/browser_thread.h"
[email protected]486295a2013-03-20 23:35:4924
[email protected]bd386e62014-07-09 22:17:0625#if defined(OS_WIN)
[email protected]b646c852014-07-17 06:19:4726#include "components/password_manager/core/browser/webdata/password_web_data_service_win.h"
[email protected]bd386e62014-07-09 22:17:0627#endif
28
[email protected]8550f062013-03-20 23:51:0529using content::BrowserThread;
30
[email protected]486295a2013-03-20 23:35:4931namespace {
32
sdefresnecb955cd2014-12-15 23:21:5633// Converts a WebDataServiceWrapper::ErrorType to ProfileErrorType.
34ProfileErrorType ProfileErrorFromWebDataServiceWrapperError(
35 WebDataServiceWrapper::ErrorType error_type) {
36 switch (error_type) {
37 case WebDataServiceWrapper::ERROR_LOADING_AUTOFILL:
tfarina87b7a3f2016-10-21 01:35:2438 return ProfileErrorType::DB_AUTOFILL_WEB_DATA;
sdefresnecb955cd2014-12-15 23:21:5639
40 case WebDataServiceWrapper::ERROR_LOADING_KEYWORD:
tfarina87b7a3f2016-10-21 01:35:2441 return ProfileErrorType::DB_KEYWORD_WEB_DATA;
sdefresnecb955cd2014-12-15 23:21:5642
43 case WebDataServiceWrapper::ERROR_LOADING_TOKEN:
tfarina87b7a3f2016-10-21 01:35:2444 return ProfileErrorType::DB_TOKEN_WEB_DATA;
sdefresnecb955cd2014-12-15 23:21:5645
46 case WebDataServiceWrapper::ERROR_LOADING_PASSWORD:
tfarina87b7a3f2016-10-21 01:35:2447 return ProfileErrorType::DB_WEB_DATA;
sdefresnecb955cd2014-12-15 23:21:5648
gogerald79472092017-04-27 15:38:1649 case WebDataServiceWrapper::ERROR_LOADING_PAYMENT_MANIFEST:
50 return ProfileErrorType::DB_PAYMENT_MANIFEST_WEB_DATA;
51
sdefresnecb955cd2014-12-15 23:21:5652 default:
Rouslan Solomakhinde012532017-09-20 15:18:3453 NOTREACHED() << "Unknown WebDataServiceWrapper::ErrorType: "
54 << error_type;
tfarina87b7a3f2016-10-21 01:35:2455 return ProfileErrorType::DB_WEB_DATA;
sdefresnecb955cd2014-12-15 23:21:5656 }
57}
58
[email protected]486295a2013-03-20 23:35:4959// Callback to show error dialog on profile load error.
sdefresnecb955cd2014-12-15 23:21:5660void ProfileErrorCallback(WebDataServiceWrapper::ErrorType error_type,
afakhry7c9abe72016-08-05 17:33:1961 sql::InitStatus status,
62 const std::string& diagnostics) {
63 ShowProfileErrorDialog(ProfileErrorFromWebDataServiceWrapperError(error_type),
afakhrya592d29c2016-08-22 19:06:5764 SqlInitStatusToMessageId(status), diagnostics);
[email protected]486295a2013-03-20 23:35:4965}
66
sdefresnecb955cd2014-12-15 23:21:5667} // namespace
68
[email protected]d07edd42012-05-14 23:49:4669WebDataServiceFactory::WebDataServiceFactory()
[email protected]f1484c52013-05-22 23:25:4470 : BrowserContextKeyedServiceFactory(
[email protected]bd386e62014-07-09 22:17:0671 "WebDataService",
72 BrowserContextDependencyManager::GetInstance()) {
[email protected]dd3c53f2013-03-14 02:59:4173 // WebDataServiceFactory has no dependecies.
[email protected]d07edd42012-05-14 23:49:4674}
75
Rouslan Solomakhinde012532017-09-20 15:18:3476WebDataServiceFactory::~WebDataServiceFactory() {}
[email protected]d07edd42012-05-14 23:49:4677
78// static
[email protected]e2b31052013-03-25 01:49:3779WebDataServiceWrapper* WebDataServiceFactory::GetForProfile(
[email protected]34b59462013-12-03 14:08:3280 Profile* profile,
sdefresnee9ea3c22015-01-10 10:10:0481 ServiceAccessType access_type) {
[email protected]66769db2012-09-13 17:06:1982 // 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]34b59462013-12-03 14:08:3284 // the *WebDataService::FromBrowserContext() functions (see above).
sdefresnee9ea3c22015-01-10 10:10:0485 DCHECK(access_type != ServiceAccessType::IMPLICIT_ACCESS ||
86 !profile->IsOffTheRecord());
[email protected]e2b31052013-03-25 01:49:3787 return static_cast<WebDataServiceWrapper*>(
[email protected]bd386e62014-07-09 22:17:0688 GetInstance()->GetServiceForBrowserContext(profile, true));
[email protected]d07edd42012-05-14 23:49:4689}
90
91// static
[email protected]e2b31052013-03-25 01:49:3792WebDataServiceWrapper* WebDataServiceFactory::GetForProfileIfExists(
[email protected]34b59462013-12-03 14:08:3293 Profile* profile,
sdefresnee9ea3c22015-01-10 10:10:0494 ServiceAccessType access_type) {
[email protected]dd3c53f2013-03-14 02:59:4195 // 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]34b59462013-12-03 14:08:3297 // the *WebDataService::FromBrowserContext() functions (see above).
sdefresnee9ea3c22015-01-10 10:10:0498 DCHECK(access_type != ServiceAccessType::IMPLICIT_ACCESS ||
99 !profile->IsOffTheRecord());
[email protected]e2b31052013-03-25 01:49:37100 return static_cast<WebDataServiceWrapper*>(
[email protected]bd386e62014-07-09 22:17:06101 GetInstance()->GetServiceForBrowserContext(profile, false));
[email protected]dd3c53f2013-03-14 02:59:41102}
103
104// static
sdefresnecb955cd2014-12-15 23:21:56105scoped_refptr<autofill::AutofillWebDataService>
[email protected]34b59462013-12-03 14:08:32106WebDataServiceFactory::GetAutofillWebDataForProfile(
107 Profile* profile,
sdefresnee9ea3c22015-01-10 10:10:04108 ServiceAccessType access_type) {
[email protected]34b59462013-12-03 14:08:32109 WebDataServiceWrapper* wrapper =
110 WebDataServiceFactory::GetForProfile(profile, access_type);
sdefresnecb955cd2014-12-15 23:21:56111 // |wrapper| can be null in Incognito mode.
Rouslan Solomakhinde012532017-09-20 15:18:34112 return wrapper ? wrapper->GetAutofillWebData()
113 : scoped_refptr<autofill::AutofillWebDataService>(nullptr);
[email protected]34b59462013-12-03 14:08:32114}
115
116// static
[email protected]37b324602014-07-02 07:30:49117scoped_refptr<KeywordWebDataService>
118WebDataServiceFactory::GetKeywordWebDataForProfile(
119 Profile* profile,
sdefresnee9ea3c22015-01-10 10:10:04120 ServiceAccessType access_type) {
[email protected]37b324602014-07-02 07:30:49121 WebDataServiceWrapper* wrapper =
122 WebDataServiceFactory::GetForProfile(profile, access_type);
sdefresnecb955cd2014-12-15 23:21:56123 // |wrapper| can be null in Incognito mode.
Rouslan Solomakhinde012532017-09-20 15:18:34124 return wrapper ? wrapper->GetKeywordWebData()
125 : scoped_refptr<KeywordWebDataService>(nullptr);
[email protected]37b324602014-07-02 07:30:49126}
127
128// static
[email protected]bd386e62014-07-09 22:17:06129scoped_refptr<TokenWebData> WebDataServiceFactory::GetTokenWebDataForProfile(
[email protected]1be4efd2014-01-09 12:43:44130 Profile* profile,
sdefresnee9ea3c22015-01-10 10:10:04131 ServiceAccessType access_type) {
[email protected]1be4efd2014-01-09 12:43:44132 WebDataServiceWrapper* wrapper =
133 WebDataServiceFactory::GetForProfile(profile, access_type);
sdefresnecb955cd2014-12-15 23:21:56134 // |wrapper| can be null in Incognito mode.
Rouslan Solomakhinde012532017-09-20 15:18:34135 return wrapper ? wrapper->GetTokenWebData()
136 : scoped_refptr<TokenWebData>(nullptr);
[email protected]1be4efd2014-01-09 12:43:44137}
138
[email protected]bd386e62014-07-09 22:17:06139#if defined(OS_WIN)
140// static
141scoped_refptr<PasswordWebDataService>
142WebDataServiceFactory::GetPasswordWebDataForProfile(
143 Profile* profile,
sdefresnee9ea3c22015-01-10 10:10:04144 ServiceAccessType access_type) {
[email protected]bd386e62014-07-09 22:17:06145 WebDataServiceWrapper* wrapper =
146 WebDataServiceFactory::GetForProfile(profile, access_type);
sdefresnecb955cd2014-12-15 23:21:56147 // |wrapper| can be null in Incognito mode.
Rouslan Solomakhinde012532017-09-20 15:18:34148 return wrapper ? wrapper->GetPasswordWebData()
149 : scoped_refptr<PasswordWebDataService>(nullptr);
[email protected]bd386e62014-07-09 22:17:06150}
151#endif
152
gogerald79472092017-04-27 15:38:16153// static
154scoped_refptr<payments::PaymentManifestWebDataService>
155WebDataServiceFactory::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}
gogerald79472092017-04-27 15:38:16165
[email protected]1be4efd2014-01-09 12:43:44166// static
[email protected]d07edd42012-05-14 23:49:46167WebDataServiceFactory* WebDataServiceFactory::GetInstance() {
olli.raula36aa8be2015-09-10 11:14:22168 return base::Singleton<WebDataServiceFactory>::get();
[email protected]d07edd42012-05-14 23:49:46169}
170
[email protected]018bf652013-05-03 23:18:34171content::BrowserContext* WebDataServiceFactory::GetBrowserContextToUse(
172 content::BrowserContext* context) const {
173 return chrome::GetBrowserContextRedirectedInIncognito(context);
[email protected]d07edd42012-05-14 23:49:46174}
175
[email protected]540380fc2014-03-14 10:10:34176KeyedService* WebDataServiceFactory::BuildServiceInstanceFor(
sdefresnef8cf5be2014-12-16 20:08:09177 content::BrowserContext* context) const {
178 const base::FilePath& profile_path = context->GetPath();
179 return new WebDataServiceWrapper(
180 profile_path, g_browser_process->GetApplicationLocale(),
thestig529ad8a2016-07-08 20:30:12181 BrowserThread::GetTaskRunnerForThread(BrowserThread::UI),
sdefresnef8cf5be2014-12-16 20:08:09182 sync_start_util::GetFlareForSyncableService(profile_path),
sdefresnecb955cd2014-12-15 23:21:56183 &ProfileErrorCallback);
[email protected]d07edd42012-05-14 23:49:46184}
185
[email protected]bb05cae12012-09-06 00:37:52186bool WebDataServiceFactory::ServiceIsNULLWhileTesting() const {
[email protected]d07edd42012-05-14 23:49:46187 return true;
188}