[M120 Migration]Fix for crash during chrome exit
[platform/framework/web/chromium-efl.git] / chrome / browser / web_data_service_factory.cc
1 // Copyright 2012 The Chromium Authors
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/files/file_path.h"
8 #include "base/functional/bind.h"
9 #include "base/no_destructor.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/profiles/profile_error_dialog.h"
16 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
17 #include "components/search_engines/keyword_web_data_service.h"
18 #include "components/signin/public/webdata/token_web_data.h"
19 #include "components/webdata_services/web_data_service_wrapper.h"
20 #include "content/public/browser/browser_task_traits.h"
21 #include "content/public/browser/browser_thread.h"
22
23 namespace {
24
25 // Converts a WebDataServiceWrapper::ErrorType to ProfileErrorType.
26 ProfileErrorType ProfileErrorFromWebDataServiceWrapperError(
27     WebDataServiceWrapper::ErrorType error_type) {
28   switch (error_type) {
29     case WebDataServiceWrapper::ERROR_LOADING_AUTOFILL:
30       return ProfileErrorType::DB_AUTOFILL_WEB_DATA;
31
32     case WebDataServiceWrapper::ERROR_LOADING_ACCOUNT_AUTOFILL:
33       return ProfileErrorType::DB_ACCOUNT_AUTOFILL_WEB_DATA;
34
35     case WebDataServiceWrapper::ERROR_LOADING_KEYWORD:
36       return ProfileErrorType::DB_KEYWORD_WEB_DATA;
37
38     case WebDataServiceWrapper::ERROR_LOADING_TOKEN:
39       return ProfileErrorType::DB_TOKEN_WEB_DATA;
40
41     case WebDataServiceWrapper::ERROR_LOADING_PASSWORD:
42       return ProfileErrorType::DB_WEB_DATA;
43
44     case WebDataServiceWrapper::ERROR_LOADING_PAYMENT_MANIFEST:
45       return ProfileErrorType::DB_PAYMENT_MANIFEST_WEB_DATA;
46
47     default:
48       NOTREACHED() << "Unknown WebDataServiceWrapper::ErrorType: "
49                    << error_type;
50       return ProfileErrorType::DB_WEB_DATA;
51   }
52 }
53
54 // Callback to show error dialog on profile load error.
55 void ProfileErrorCallback(WebDataServiceWrapper::ErrorType error_type,
56                           sql::InitStatus status,
57                           const std::string& diagnostics) {
58   ShowProfileErrorDialog(ProfileErrorFromWebDataServiceWrapperError(error_type),
59                          SqlInitStatusToMessageId(status), diagnostics);
60 }
61
62 std::unique_ptr<KeyedService> BuildWebDataService(
63     content::BrowserContext* context) {
64   const base::FilePath& profile_path = context->GetPath();
65   return std::make_unique<WebDataServiceWrapper>(
66       profile_path, g_browser_process->GetApplicationLocale(),
67       content::GetUIThreadTaskRunner({}),
68       base::BindRepeating(&ProfileErrorCallback));
69 }
70
71 }  // namespace
72
73 WebDataServiceFactory::WebDataServiceFactory() = default;
74
75 WebDataServiceFactory::~WebDataServiceFactory() = default;
76
77 // static
78 WebDataServiceWrapper* WebDataServiceFactory::GetForProfile(
79     Profile* profile,
80     ServiceAccessType access_type) {
81   return GetForBrowserContext(profile, access_type);
82 }
83
84 // static
85 WebDataServiceWrapper* WebDataServiceFactory::GetForProfileIfExists(
86     Profile* profile,
87     ServiceAccessType access_type) {
88   return GetForBrowserContextIfExists(profile, access_type);
89 }
90
91 // static
92 scoped_refptr<autofill::AutofillWebDataService>
93 WebDataServiceFactory::GetAutofillWebDataForProfile(
94     Profile* profile,
95     ServiceAccessType access_type) {
96   WebDataServiceWrapper* wrapper =
97       WebDataServiceFactory::GetForProfile(profile, access_type);
98   // |wrapper| can be null in Incognito mode.
99   return wrapper ? wrapper->GetProfileAutofillWebData()
100                  : scoped_refptr<autofill::AutofillWebDataService>(nullptr);
101 }
102
103 // static
104 scoped_refptr<autofill::AutofillWebDataService>
105 WebDataServiceFactory::GetAutofillWebDataForAccount(
106     Profile* profile,
107     ServiceAccessType access_type) {
108   WebDataServiceWrapper* wrapper =
109       WebDataServiceFactory::GetForProfile(profile, access_type);
110   // |wrapper| can be null in Incognito mode.
111   return wrapper ? wrapper->GetAccountAutofillWebData()
112                  : scoped_refptr<autofill::AutofillWebDataService>(nullptr);
113 }
114
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 ? wrapper->GetKeywordWebData()
124                  : scoped_refptr<KeywordWebDataService>(nullptr);
125 }
126
127 // static
128 scoped_refptr<TokenWebData> WebDataServiceFactory::GetTokenWebDataForProfile(
129     Profile* profile,
130     ServiceAccessType access_type) {
131   WebDataServiceWrapper* wrapper =
132       WebDataServiceFactory::GetForProfile(profile, access_type);
133   // |wrapper| can be null in Incognito mode.
134   return wrapper ? wrapper->GetTokenWebData()
135                  : scoped_refptr<TokenWebData>(nullptr);
136 }
137
138 // static
139 WebDataServiceFactory* WebDataServiceFactory::GetInstance() {
140   static base::NoDestructor<WebDataServiceFactory> instance;
141   return instance.get();
142 }
143
144 // static
145 BrowserContextKeyedServiceFactory::TestingFactory
146 WebDataServiceFactory::GetDefaultFactory() {
147   return base::BindRepeating(&BuildWebDataService);
148 }
149
150 content::BrowserContext* WebDataServiceFactory::GetBrowserContextToUse(
151     content::BrowserContext* context) const {
152   return chrome::GetBrowserContextRedirectedInIncognito(context);
153 }
154
155 std::unique_ptr<KeyedService>
156 WebDataServiceFactory::BuildServiceInstanceForBrowserContext(
157     content::BrowserContext* context) const {
158   return BuildWebDataService(context);
159 }
160
161 bool WebDataServiceFactory::ServiceIsNULLWhileTesting() const {
162   return true;
163 }