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 "chrome/browser/browser_process.h"
11 #include "chrome/browser/profiles/incognito_helpers.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/sync/glue/sync_start_util.h"
14 #include "chrome/browser/ui/profile_error_dialog.h"
15 #include "chrome/grit/chromium_strings.h"
16 #include "chrome/grit/generated_resources.h"
17 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
18 #include "components/keyed_service/content/browser_context_dependency_manager.h"
19 #include "components/search_engines/keyword_web_data_service.h"
20 #include "components/signin/core/browser/webdata/token_web_data.h"
21 #include "components/webdata_services/web_data_service_wrapper.h"
22 #include "content/public/browser/browser_thread.h"
25 #include "components/password_manager/core/browser/webdata/password_web_data_service_win.h"
28 using content::BrowserThread
;
32 // Converts a WebDataServiceWrapper::ErrorType to ProfileErrorType.
33 ProfileErrorType
ProfileErrorFromWebDataServiceWrapperError(
34 WebDataServiceWrapper::ErrorType error_type
) {
36 case WebDataServiceWrapper::ERROR_LOADING_AUTOFILL
:
37 return PROFILE_ERROR_DB_AUTOFILL_WEB_DATA
;
39 case WebDataServiceWrapper::ERROR_LOADING_KEYWORD
:
40 return PROFILE_ERROR_DB_KEYWORD_WEB_DATA
;
42 case WebDataServiceWrapper::ERROR_LOADING_TOKEN
:
43 return PROFILE_ERROR_DB_TOKEN_WEB_DATA
;
45 case WebDataServiceWrapper::ERROR_LOADING_PASSWORD
:
46 return PROFILE_ERROR_DB_WEB_DATA
;
50 << "Unknown WebDataServiceWrapper::ErrorType: " << error_type
;
51 return PROFILE_ERROR_DB_WEB_DATA
;
55 // Callback to show error dialog on profile load error.
56 void ProfileErrorCallback(WebDataServiceWrapper::ErrorType error_type
,
57 sql::InitStatus status
) {
58 ShowProfileErrorDialog(
59 ProfileErrorFromWebDataServiceWrapperError(error_type
),
60 (status
== sql::INIT_FAILURE
) ?
61 IDS_COULDNT_OPEN_PROFILE_ERROR
: IDS_PROFILE_TOO_NEW_ERROR
);
66 WebDataServiceFactory::WebDataServiceFactory()
67 : BrowserContextKeyedServiceFactory(
69 BrowserContextDependencyManager::GetInstance()) {
70 // WebDataServiceFactory has no dependecies.
73 WebDataServiceFactory::~WebDataServiceFactory() {
77 WebDataServiceWrapper
* WebDataServiceFactory::GetForProfile(
79 ServiceAccessType access_type
) {
80 // If |access_type| starts being used for anything other than this
81 // DCHECK, we need to start taking it as a parameter to
82 // the *WebDataService::FromBrowserContext() functions (see above).
83 DCHECK(access_type
!= ServiceAccessType::IMPLICIT_ACCESS
||
84 !profile
->IsOffTheRecord());
85 return static_cast<WebDataServiceWrapper
*>(
86 GetInstance()->GetServiceForBrowserContext(profile
, true));
90 WebDataServiceWrapper
* WebDataServiceFactory::GetForProfileIfExists(
92 ServiceAccessType access_type
) {
93 // If |access_type| starts being used for anything other than this
94 // DCHECK, we need to start taking it as a parameter to
95 // the *WebDataService::FromBrowserContext() functions (see above).
96 DCHECK(access_type
!= ServiceAccessType::IMPLICIT_ACCESS
||
97 !profile
->IsOffTheRecord());
98 return static_cast<WebDataServiceWrapper
*>(
99 GetInstance()->GetServiceForBrowserContext(profile
, false));
103 scoped_refptr
<autofill::AutofillWebDataService
>
104 WebDataServiceFactory::GetAutofillWebDataForProfile(
106 ServiceAccessType access_type
) {
107 WebDataServiceWrapper
* wrapper
=
108 WebDataServiceFactory::GetForProfile(profile
, access_type
);
109 // |wrapper| can be null in Incognito mode.
111 wrapper
->GetAutofillWebData() :
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.
124 wrapper
->GetKeywordWebData() :
125 scoped_refptr
<KeywordWebDataService
>(nullptr);
129 scoped_refptr
<TokenWebData
> WebDataServiceFactory::GetTokenWebDataForProfile(
131 ServiceAccessType access_type
) {
132 WebDataServiceWrapper
* wrapper
=
133 WebDataServiceFactory::GetForProfile(profile
, access_type
);
134 // |wrapper| can be null in Incognito mode.
136 wrapper
->GetTokenWebData() : scoped_refptr
<TokenWebData
>(nullptr);
141 scoped_refptr
<PasswordWebDataService
>
142 WebDataServiceFactory::GetPasswordWebDataForProfile(
144 ServiceAccessType access_type
) {
145 WebDataServiceWrapper
* wrapper
=
146 WebDataServiceFactory::GetForProfile(profile
, access_type
);
147 // |wrapper| can be null in Incognito mode.
149 wrapper
->GetPasswordWebData() :
150 scoped_refptr
<PasswordWebDataService
>(nullptr);
155 WebDataServiceFactory
* WebDataServiceFactory::GetInstance() {
156 return Singleton
<WebDataServiceFactory
>::get();
159 content::BrowserContext
* WebDataServiceFactory::GetBrowserContextToUse(
160 content::BrowserContext
* context
) const {
161 return chrome::GetBrowserContextRedirectedInIncognito(context
);
164 KeyedService
* WebDataServiceFactory::BuildServiceInstanceFor(
165 content::BrowserContext
* context
) const {
166 const base::FilePath
& profile_path
= context
->GetPath();
167 return new WebDataServiceWrapper(
168 profile_path
, g_browser_process
->GetApplicationLocale(),
169 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI
),
170 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB
),
171 sync_start_util::GetFlareForSyncableService(profile_path
),
172 &ProfileErrorCallback
);
175 bool WebDataServiceFactory::ServiceIsNULLWhileTesting() const {