blob: 638eae62aedee4787691c0266dc8394712c89594 [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"
[email protected]bf5c532d2014-07-05 00:29:5319#include "components/search_engines/keyword_web_data_service.h"
[email protected]7274ef02014-03-24 22:43:4020#include "components/signin/core/browser/webdata/token_web_data.h"
sdefresnecb955cd2014-12-15 23:21:5621#include "components/webdata_services/web_data_service_wrapper.h"
[email protected]8550f062013-03-20 23:51:0522#include "content/public/browser/browser_thread.h"
[email protected]486295a2013-03-20 23:35:4923
[email protected]bd386e62014-07-09 22:17:0624#if defined(OS_WIN)
[email protected]b646c852014-07-17 06:19:4725#include "components/password_manager/core/browser/webdata/password_web_data_service_win.h"
[email protected]bd386e62014-07-09 22:17:0626#endif
27
[email protected]8550f062013-03-20 23:51:0528using content::BrowserThread;
29
[email protected]486295a2013-03-20 23:35:4930namespace {
31
sdefresnecb955cd2014-12-15 23:21:5632// Converts a WebDataServiceWrapper::ErrorType to ProfileErrorType.
33ProfileErrorType 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]486295a2013-03-20 23:35:4955// Callback to show error dialog on profile load error.
sdefresnecb955cd2014-12-15 23:21:5656void ProfileErrorCallback(WebDataServiceWrapper::ErrorType error_type,
afakhry7c9abe72016-08-05 17:33:1957 sql::InitStatus status,
58 const std::string& diagnostics) {
59 ShowProfileErrorDialog(ProfileErrorFromWebDataServiceWrapperError(error_type),
afakhrya592d29c2016-08-22 19:06:5760 SqlInitStatusToMessageId(status), diagnostics);
[email protected]486295a2013-03-20 23:35:4961}
62
sdefresnecb955cd2014-12-15 23:21:5663} // namespace
64
[email protected]d07edd42012-05-14 23:49:4665WebDataServiceFactory::WebDataServiceFactory()
[email protected]f1484c52013-05-22 23:25:4466 : BrowserContextKeyedServiceFactory(
[email protected]bd386e62014-07-09 22:17:0667 "WebDataService",
68 BrowserContextDependencyManager::GetInstance()) {
[email protected]dd3c53f2013-03-14 02:59:4169 // WebDataServiceFactory has no dependecies.
[email protected]d07edd42012-05-14 23:49:4670}
71
[email protected]bd386e62014-07-09 22:17:0672WebDataServiceFactory::~WebDataServiceFactory() {
73}
[email protected]d07edd42012-05-14 23:49:4674
75// static
[email protected]e2b31052013-03-25 01:49:3776WebDataServiceWrapper* WebDataServiceFactory::GetForProfile(
[email protected]34b59462013-12-03 14:08:3277 Profile* profile,
sdefresnee9ea3c22015-01-10 10:10:0478 ServiceAccessType access_type) {
[email protected]66769db2012-09-13 17:06:1979 // 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]34b59462013-12-03 14:08:3281 // the *WebDataService::FromBrowserContext() functions (see above).
sdefresnee9ea3c22015-01-10 10:10:0482 DCHECK(access_type != ServiceAccessType::IMPLICIT_ACCESS ||
83 !profile->IsOffTheRecord());
[email protected]e2b31052013-03-25 01:49:3784 return static_cast<WebDataServiceWrapper*>(
[email protected]bd386e62014-07-09 22:17:0685 GetInstance()->GetServiceForBrowserContext(profile, true));
[email protected]d07edd42012-05-14 23:49:4686}
87
88// static
[email protected]e2b31052013-03-25 01:49:3789WebDataServiceWrapper* WebDataServiceFactory::GetForProfileIfExists(
[email protected]34b59462013-12-03 14:08:3290 Profile* profile,
sdefresnee9ea3c22015-01-10 10:10:0491 ServiceAccessType access_type) {
[email protected]dd3c53f2013-03-14 02:59:4192 // 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]34b59462013-12-03 14:08:3294 // the *WebDataService::FromBrowserContext() functions (see above).
sdefresnee9ea3c22015-01-10 10:10:0495 DCHECK(access_type != ServiceAccessType::IMPLICIT_ACCESS ||
96 !profile->IsOffTheRecord());
[email protected]e2b31052013-03-25 01:49:3797 return static_cast<WebDataServiceWrapper*>(
[email protected]bd386e62014-07-09 22:17:0698 GetInstance()->GetServiceForBrowserContext(profile, false));
[email protected]dd3c53f2013-03-14 02:59:4199}
100
101// static
sdefresnecb955cd2014-12-15 23:21:56102scoped_refptr<autofill::AutofillWebDataService>
[email protected]34b59462013-12-03 14:08:32103WebDataServiceFactory::GetAutofillWebDataForProfile(
104 Profile* profile,
sdefresnee9ea3c22015-01-10 10:10:04105 ServiceAccessType access_type) {
[email protected]34b59462013-12-03 14:08:32106 WebDataServiceWrapper* wrapper =
107 WebDataServiceFactory::GetForProfile(profile, access_type);
sdefresnecb955cd2014-12-15 23:21:56108 // |wrapper| can be null in Incognito mode.
[email protected]34b59462013-12-03 14:08:32109 return wrapper ?
110 wrapper->GetAutofillWebData() :
sdefresnecb955cd2014-12-15 23:21:56111 scoped_refptr<autofill::AutofillWebDataService>(nullptr);
[email protected]34b59462013-12-03 14:08:32112}
113
114// static
[email protected]37b324602014-07-02 07:30:49115scoped_refptr<KeywordWebDataService>
116WebDataServiceFactory::GetKeywordWebDataForProfile(
117 Profile* profile,
sdefresnee9ea3c22015-01-10 10:10:04118 ServiceAccessType access_type) {
[email protected]37b324602014-07-02 07:30:49119 WebDataServiceWrapper* wrapper =
120 WebDataServiceFactory::GetForProfile(profile, access_type);
sdefresnecb955cd2014-12-15 23:21:56121 // |wrapper| can be null in Incognito mode.
[email protected]37b324602014-07-02 07:30:49122 return wrapper ?
sdefresnecb955cd2014-12-15 23:21:56123 wrapper->GetKeywordWebData() :
124 scoped_refptr<KeywordWebDataService>(nullptr);
[email protected]37b324602014-07-02 07:30:49125}
126
127// static
[email protected]bd386e62014-07-09 22:17:06128scoped_refptr<TokenWebData> WebDataServiceFactory::GetTokenWebDataForProfile(
[email protected]1be4efd2014-01-09 12:43:44129 Profile* profile,
sdefresnee9ea3c22015-01-10 10:10:04130 ServiceAccessType access_type) {
[email protected]1be4efd2014-01-09 12:43:44131 WebDataServiceWrapper* wrapper =
132 WebDataServiceFactory::GetForProfile(profile, access_type);
sdefresnecb955cd2014-12-15 23:21:56133 // |wrapper| can be null in Incognito mode.
[email protected]bd386e62014-07-09 22:17:06134 return wrapper ?
sdefresnecb955cd2014-12-15 23:21:56135 wrapper->GetTokenWebData() : scoped_refptr<TokenWebData>(nullptr);
[email protected]1be4efd2014-01-09 12:43:44136}
137
[email protected]bd386e62014-07-09 22:17:06138#if defined(OS_WIN)
139// static
140scoped_refptr<PasswordWebDataService>
141WebDataServiceFactory::GetPasswordWebDataForProfile(
142 Profile* profile,
sdefresnee9ea3c22015-01-10 10:10:04143 ServiceAccessType access_type) {
[email protected]bd386e62014-07-09 22:17:06144 WebDataServiceWrapper* wrapper =
145 WebDataServiceFactory::GetForProfile(profile, access_type);
sdefresnecb955cd2014-12-15 23:21:56146 // |wrapper| can be null in Incognito mode.
[email protected]bd386e62014-07-09 22:17:06147 return wrapper ?
148 wrapper->GetPasswordWebData() :
sdefresnecb955cd2014-12-15 23:21:56149 scoped_refptr<PasswordWebDataService>(nullptr);
[email protected]bd386e62014-07-09 22:17:06150}
151#endif
152
[email protected]1be4efd2014-01-09 12:43:44153// static
[email protected]d07edd42012-05-14 23:49:46154WebDataServiceFactory* WebDataServiceFactory::GetInstance() {
olli.raula36aa8be2015-09-10 11:14:22155 return base::Singleton<WebDataServiceFactory>::get();
[email protected]d07edd42012-05-14 23:49:46156}
157
[email protected]018bf652013-05-03 23:18:34158content::BrowserContext* WebDataServiceFactory::GetBrowserContextToUse(
159 content::BrowserContext* context) const {
160 return chrome::GetBrowserContextRedirectedInIncognito(context);
[email protected]d07edd42012-05-14 23:49:46161}
162
[email protected]540380fc2014-03-14 10:10:34163KeyedService* WebDataServiceFactory::BuildServiceInstanceFor(
sdefresnef8cf5be2014-12-16 20:08:09164 content::BrowserContext* context) const {
165 const base::FilePath& profile_path = context->GetPath();
166 return new WebDataServiceWrapper(
167 profile_path, g_browser_process->GetApplicationLocale(),
thestig529ad8a2016-07-08 20:30:12168 BrowserThread::GetTaskRunnerForThread(BrowserThread::UI),
169 BrowserThread::GetTaskRunnerForThread(BrowserThread::DB),
sdefresnef8cf5be2014-12-16 20:08:09170 sync_start_util::GetFlareForSyncableService(profile_path),
sdefresnecb955cd2014-12-15 23:21:56171 &ProfileErrorCallback);
[email protected]d07edd42012-05-14 23:49:46172}
173
[email protected]bb05cae12012-09-06 00:37:52174bool WebDataServiceFactory::ServiceIsNULLWhileTesting() const {
[email protected]d07edd42012-05-14 23:49:46175 return true;
176}