Move AppMenuTest from ChromeShellTest to ChromePublicTest
[chromium-blink-merge.git] / chrome / browser / web_data_service_factory.cc
blob3dd86a19a032b62d57907af3160b52da07b16621
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"
7 #include "base/bind.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"
24 #if defined(OS_WIN)
25 #include "components/password_manager/core/browser/webdata/password_web_data_service_win.h"
26 #endif
28 using content::BrowserThread;
30 namespace {
32 // Converts a WebDataServiceWrapper::ErrorType to ProfileErrorType.
33 ProfileErrorType ProfileErrorFromWebDataServiceWrapperError(
34 WebDataServiceWrapper::ErrorType error_type) {
35 switch (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;
48 default:
49 NOTREACHED()
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);
64 } // namespace
66 WebDataServiceFactory::WebDataServiceFactory()
67 : BrowserContextKeyedServiceFactory(
68 "WebDataService",
69 BrowserContextDependencyManager::GetInstance()) {
70 // WebDataServiceFactory has no dependecies.
73 WebDataServiceFactory::~WebDataServiceFactory() {
76 // static
77 WebDataServiceWrapper* WebDataServiceFactory::GetForProfile(
78 Profile* profile,
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));
89 // static
90 WebDataServiceWrapper* WebDataServiceFactory::GetForProfileIfExists(
91 Profile* profile,
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));
102 // static
103 scoped_refptr<autofill::AutofillWebDataService>
104 WebDataServiceFactory::GetAutofillWebDataForProfile(
105 Profile* profile,
106 ServiceAccessType access_type) {
107 WebDataServiceWrapper* wrapper =
108 WebDataServiceFactory::GetForProfile(profile, access_type);
109 // |wrapper| can be null in Incognito mode.
110 return wrapper ?
111 wrapper->GetAutofillWebData() :
112 scoped_refptr<autofill::AutofillWebDataService>(nullptr);
115 // static
116 scoped_refptr<KeywordWebDataService>
117 WebDataServiceFactory::GetKeywordWebDataForProfile(
118 Profile* profile,
119 ServiceAccessType access_type) {
120 WebDataServiceWrapper* wrapper =
121 WebDataServiceFactory::GetForProfile(profile, access_type);
122 // |wrapper| can be null in Incognito mode.
123 return wrapper ?
124 wrapper->GetKeywordWebData() :
125 scoped_refptr<KeywordWebDataService>(nullptr);
128 // static
129 scoped_refptr<TokenWebData> WebDataServiceFactory::GetTokenWebDataForProfile(
130 Profile* profile,
131 ServiceAccessType access_type) {
132 WebDataServiceWrapper* wrapper =
133 WebDataServiceFactory::GetForProfile(profile, access_type);
134 // |wrapper| can be null in Incognito mode.
135 return wrapper ?
136 wrapper->GetTokenWebData() : scoped_refptr<TokenWebData>(nullptr);
139 #if defined(OS_WIN)
140 // static
141 scoped_refptr<PasswordWebDataService>
142 WebDataServiceFactory::GetPasswordWebDataForProfile(
143 Profile* profile,
144 ServiceAccessType access_type) {
145 WebDataServiceWrapper* wrapper =
146 WebDataServiceFactory::GetForProfile(profile, access_type);
147 // |wrapper| can be null in Incognito mode.
148 return wrapper ?
149 wrapper->GetPasswordWebData() :
150 scoped_refptr<PasswordWebDataService>(nullptr);
152 #endif
154 // static
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 {
176 return true;