1 // Copyright 2015 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 "ios/chrome/browser/web_data_service_factory.h"
8 #include "base/files/file_path.h"
9 #include "base/logging.h"
10 #include "base/memory/singleton.h"
11 #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
12 #include "components/keyed_service/core/service_access_type.h"
13 #include "components/keyed_service/ios/browser_state_dependency_manager.h"
14 #include "components/search_engines/keyword_web_data_service.h"
15 #include "components/signin/core/browser/webdata/token_web_data.h"
16 #include "components/webdata_services/web_data_service_wrapper.h"
17 #include "ios/chrome/browser/application_context.h"
18 #include "ios/chrome/browser/browser_state/browser_state_otr_helper.h"
19 #include "ios/chrome/browser/sync/glue/sync_start_util.h"
20 #include "ios/public/provider/chrome/browser/browser_state/chrome_browser_state.h"
21 #include "ios/web/public/web_thread.h"
26 void DoNothingOnErrorCallback(WebDataServiceWrapper::ErrorType error_type
,
27 sql::InitStatus status
) {
33 WebDataServiceWrapper
* WebDataServiceFactory::GetForBrowserState(
34 ios::ChromeBrowserState
* browser_state
,
35 ServiceAccessType access_type
) {
36 DCHECK(access_type
== ServiceAccessType::EXPLICIT_ACCESS
||
37 !browser_state
->IsOffTheRecord());
38 return static_cast<WebDataServiceWrapper
*>(
39 GetInstance()->GetServiceForBrowserState(browser_state
, true));
43 WebDataServiceWrapper
* WebDataServiceFactory::GetForBrowserStateIfExists(
44 ios::ChromeBrowserState
* browser_state
,
45 ServiceAccessType access_type
) {
46 DCHECK(access_type
== ServiceAccessType::EXPLICIT_ACCESS
||
47 !browser_state
->IsOffTheRecord());
48 return static_cast<WebDataServiceWrapper
*>(
49 GetInstance()->GetServiceForBrowserState(browser_state
, false));
53 scoped_refptr
<autofill::AutofillWebDataService
>
54 WebDataServiceFactory::GetAutofillWebDataForBrowserState(
55 ios::ChromeBrowserState
* browser_state
,
56 ServiceAccessType access_type
) {
57 WebDataServiceWrapper
* wrapper
=
58 GetForBrowserState(browser_state
, access_type
);
59 return wrapper
? wrapper
->GetAutofillWebData() : nullptr;
63 scoped_refptr
<KeywordWebDataService
>
64 WebDataServiceFactory::GetKeywordWebDataForBrowserState(
65 ios::ChromeBrowserState
* browser_state
,
66 ServiceAccessType access_type
) {
67 WebDataServiceWrapper
* wrapper
=
68 GetForBrowserState(browser_state
, access_type
);
69 return wrapper
? wrapper
->GetKeywordWebData() : nullptr;
73 scoped_refptr
<TokenWebData
>
74 WebDataServiceFactory::GetTokenWebDataForBrowserState(
75 ios::ChromeBrowserState
* browser_state
,
76 ServiceAccessType access_type
) {
77 WebDataServiceWrapper
* wrapper
=
78 GetForBrowserState(browser_state
, access_type
);
79 return wrapper
? wrapper
->GetTokenWebData() : nullptr;
83 WebDataServiceFactory
* WebDataServiceFactory::GetInstance() {
84 return base::Singleton
<WebDataServiceFactory
>::get();
87 WebDataServiceFactory::WebDataServiceFactory()
88 : BrowserStateKeyedServiceFactory(
90 BrowserStateDependencyManager::GetInstance()) {
93 WebDataServiceFactory::~WebDataServiceFactory() {
96 scoped_ptr
<KeyedService
> WebDataServiceFactory::BuildServiceInstanceFor(
97 web::BrowserState
* context
) const {
98 const base::FilePath
& browser_state_path
= context
->GetStatePath();
99 return make_scoped_ptr(new WebDataServiceWrapper(
100 browser_state_path
, GetApplicationContext()->GetApplicationLocale(),
101 web::WebThread::GetTaskRunnerForThread(web::WebThread::UI
),
102 web::WebThread::GetTaskRunnerForThread(web::WebThread::DB
),
103 ios::sync_start_util::GetFlareForSyncableService(browser_state_path
),
104 &DoNothingOnErrorCallback
));
107 web::BrowserState
* WebDataServiceFactory::GetBrowserStateToUse(
108 web::BrowserState
* context
) const {
109 return GetBrowserStateRedirectedInIncognito(context
);
112 bool WebDataServiceFactory::ServiceIsNULLWhileTesting() const {