[M85 Dev][EFL] Fix errors to generate ninja files
[platform/framework/web/chromium-efl.git] / chrome / browser / web_data_service_factory.cc
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.
4
5 #include "chrome/browser/web_data_service_factory.h"
6
7 #include "base/bind.h"
8 #include "base/files/file_path.h"
9 #include "base/memory/singleton.h"
10 #include "build/build_config.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/profiles/incognito_helpers.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/profiles/sql_init_error_message_ids.h"
15 #include "chrome/browser/ui/profile_error_dialog.h"
16 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
17 #include "components/keyed_service/content/browser_context_dependency_manager.h"
18 #include "components/payments/content/payment_manifest_web_data_service.h"
19 #include "components/search_engines/keyword_web_data_service.h"
20 #include "components/signin/public/webdata/token_web_data.h"
21 #include "components/webdata_services/web_data_service_wrapper.h"
22 #include "content/public/browser/browser_task_traits.h"
23 #include "content/public/browser/browser_thread.h"
24
25 namespace {
26
27 // Converts a WebDataServiceWrapper::ErrorType to ProfileErrorType.
28 ProfileErrorType ProfileErrorFromWebDataServiceWrapperError(
29     WebDataServiceWrapper::ErrorType error_type) {
30   switch (error_type) {
31     case WebDataServiceWrapper::ERROR_LOADING_AUTOFILL:
32       return ProfileErrorType::DB_AUTOFILL_WEB_DATA;
33
34     case WebDataServiceWrapper::ERROR_LOADING_ACCOUNT_AUTOFILL:
35       return ProfileErrorType::DB_ACCOUNT_AUTOFILL_WEB_DATA;
36
37     case WebDataServiceWrapper::ERROR_LOADING_KEYWORD:
38       return ProfileErrorType::DB_KEYWORD_WEB_DATA;
39
40     case WebDataServiceWrapper::ERROR_LOADING_TOKEN:
41       return ProfileErrorType::DB_TOKEN_WEB_DATA;
42
43     case WebDataServiceWrapper::ERROR_LOADING_PASSWORD:
44       return ProfileErrorType::DB_WEB_DATA;
45
46     case WebDataServiceWrapper::ERROR_LOADING_PAYMENT_MANIFEST:
47       return ProfileErrorType::DB_PAYMENT_MANIFEST_WEB_DATA;
48
49     default:
50       NOTREACHED() << "Unknown WebDataServiceWrapper::ErrorType: "
51                    << error_type;
52       return ProfileErrorType::DB_WEB_DATA;
53   }
54 }
55
56 // Callback to show error dialog on profile load error.
57 void ProfileErrorCallback(WebDataServiceWrapper::ErrorType error_type,
58                           sql::InitStatus status,
59                           const std::string& diagnostics) {
60   ShowProfileErrorDialog(ProfileErrorFromWebDataServiceWrapperError(error_type),
61                          SqlInitStatusToMessageId(status), diagnostics);
62 }
63
64 }  // namespace
65
66 WebDataServiceFactory::WebDataServiceFactory()
67     : BrowserContextKeyedServiceFactory(
68           "WebDataService",
69           BrowserContextDependencyManager::GetInstance()) {
70   // WebDataServiceFactory has no dependecies.
71 }
72
73 WebDataServiceFactory::~WebDataServiceFactory() {}
74
75 // static
76 WebDataServiceWrapper* WebDataServiceFactory::GetForProfile(
77     Profile* profile,
78     ServiceAccessType access_type) {
79   // If |access_type| starts being used for anything other than this
80   // DCHECK, we need to start taking it as a parameter to
81   // the *WebDataService::FromBrowserContext() functions (see above).
82   DCHECK(access_type != ServiceAccessType::IMPLICIT_ACCESS ||
83          !profile->IsOffTheRecord());
84   return static_cast<WebDataServiceWrapper*>(
85       GetInstance()->GetServiceForBrowserContext(profile, true));
86 }
87
88 // static
89 WebDataServiceWrapper* WebDataServiceFactory::GetForProfileIfExists(
90     Profile* profile,
91     ServiceAccessType access_type) {
92   // If |access_type| starts being used for anything other than this
93   // DCHECK, we need to start taking it as a parameter to
94   // the *WebDataService::FromBrowserContext() functions (see above).
95   DCHECK(access_type != ServiceAccessType::IMPLICIT_ACCESS ||
96          !profile->IsOffTheRecord());
97   return static_cast<WebDataServiceWrapper*>(
98       GetInstance()->GetServiceForBrowserContext(profile, false));
99 }
100
101 // static
102 scoped_refptr<autofill::AutofillWebDataService>
103 WebDataServiceFactory::GetAutofillWebDataForProfile(
104     Profile* profile,
105     ServiceAccessType access_type) {
106   WebDataServiceWrapper* wrapper =
107       WebDataServiceFactory::GetForProfile(profile, access_type);
108   // |wrapper| can be null in Incognito mode.
109   return wrapper ? wrapper->GetProfileAutofillWebData()
110                  : scoped_refptr<autofill::AutofillWebDataService>(nullptr);
111 }
112
113 // static
114 scoped_refptr<autofill::AutofillWebDataService>
115 WebDataServiceFactory::GetAutofillWebDataForAccount(
116     Profile* profile,
117     ServiceAccessType access_type) {
118   WebDataServiceWrapper* wrapper =
119       WebDataServiceFactory::GetForProfile(profile, access_type);
120   // |wrapper| can be null in Incognito mode.
121   return wrapper ? wrapper->GetAccountAutofillWebData()
122                  : scoped_refptr<autofill::AutofillWebDataService>(nullptr);
123 }
124
125 // static
126 scoped_refptr<KeywordWebDataService>
127 WebDataServiceFactory::GetKeywordWebDataForProfile(
128     Profile* profile,
129     ServiceAccessType access_type) {
130   WebDataServiceWrapper* wrapper =
131       WebDataServiceFactory::GetForProfile(profile, access_type);
132   // |wrapper| can be null in Incognito mode.
133   return wrapper ? wrapper->GetKeywordWebData()
134                  : scoped_refptr<KeywordWebDataService>(nullptr);
135 }
136
137 // static
138 scoped_refptr<TokenWebData> WebDataServiceFactory::GetTokenWebDataForProfile(
139     Profile* profile,
140     ServiceAccessType access_type) {
141   WebDataServiceWrapper* wrapper =
142       WebDataServiceFactory::GetForProfile(profile, access_type);
143   // |wrapper| can be null in Incognito mode.
144   return wrapper ? wrapper->GetTokenWebData()
145                  : scoped_refptr<TokenWebData>(nullptr);
146 }
147
148 // static
149 scoped_refptr<payments::PaymentManifestWebDataService>
150 WebDataServiceFactory::GetPaymentManifestWebDataForProfile(
151     Profile* profile,
152     ServiceAccessType access_type) {
153   WebDataServiceWrapper* wrapper =
154       WebDataServiceFactory::GetForProfile(profile, access_type);
155   // |wrapper| can be null in Incognito mode.
156   return wrapper
157              ? wrapper->GetPaymentManifestWebData()
158              : scoped_refptr<payments::PaymentManifestWebDataService>(nullptr);
159 }
160
161 // static
162 WebDataServiceFactory* WebDataServiceFactory::GetInstance() {
163   return base::Singleton<WebDataServiceFactory>::get();
164 }
165
166 content::BrowserContext* WebDataServiceFactory::GetBrowserContextToUse(
167     content::BrowserContext* context) const {
168   return chrome::GetBrowserContextRedirectedInIncognito(context);
169 }
170
171 KeyedService* WebDataServiceFactory::BuildServiceInstanceFor(
172     content::BrowserContext* context) const {
173   const base::FilePath& profile_path = context->GetPath();
174   return new WebDataServiceWrapper(profile_path,
175                                    g_browser_process->GetApplicationLocale(),
176                                    content::GetUIThreadTaskRunner({}),
177                                    base::BindRepeating(&ProfileErrorCallback));
178 }
179
180 bool WebDataServiceFactory::ServiceIsNULLWhileTesting() const {
181   return true;
182 }