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/search_engines/template_url_service_factory.h"
8 #include "base/callback.h"
9 #include "base/memory/singleton.h"
10 #include "components/keyed_service/core/service_access_type.h"
11 #include "components/keyed_service/ios/browser_state_dependency_manager.h"
12 #include "components/search_engines/default_search_manager.h"
13 #include "components/search_engines/template_url_service.h"
14 #include "ios/chrome/browser/application_context.h"
15 #include "ios/chrome/browser/browser_state/browser_state_otr_helper.h"
16 #include "ios/chrome/browser/google/google_url_tracker_factory.h"
17 #include "ios/chrome/browser/history/history_service_factory.h"
18 #include "ios/chrome/browser/search_engines/template_url_service_client_impl.h"
19 #include "ios/chrome/browser/search_engines/ui_thread_search_terms_data.h"
20 #include "ios/chrome/browser/web_data_service_factory.h"
21 #include "ios/public/provider/chrome/browser/browser_state/chrome_browser_state.h"
23 #if defined(ENABLE_RLZ)
24 #include "components/rlz/rlz_tracker.h"
30 base::Closure
GetDefaultSearchProviderChangedCallback() {
31 #if defined(ENABLE_RLZ)
32 return base::Bind(base::IgnoreResult(&rlz::RLZTracker::RecordProductEvent
),
33 rlz_lib::CHROME
, rlz::RLZTracker::ChromeOmnibox(),
34 rlz_lib::SET_TO_GOOGLE
);
36 return base::Closure();
40 scoped_ptr
<KeyedService
> BuildTemplateURLService(web::BrowserState
* context
) {
41 ios::ChromeBrowserState
* browser_state
=
42 ios::ChromeBrowserState::FromBrowserState(context
);
43 return make_scoped_ptr(new TemplateURLService(
44 browser_state
->GetPrefs(),
45 make_scoped_ptr(new ios::UIThreadSearchTermsData(browser_state
)),
46 ios::WebDataServiceFactory::GetKeywordWebDataForBrowserState(
47 browser_state
, ServiceAccessType::EXPLICIT_ACCESS
),
48 make_scoped_ptr(new ios::TemplateURLServiceClientImpl(
49 ios::HistoryServiceFactory::GetForBrowserState(
50 browser_state
, ServiceAccessType::EXPLICIT_ACCESS
))),
51 ios::GoogleURLTrackerFactory::GetForBrowserState(browser_state
),
52 GetApplicationContext()->GetRapporService(),
53 GetDefaultSearchProviderChangedCallback()));
59 TemplateURLService
* TemplateURLServiceFactory::GetForBrowserState(
60 ios::ChromeBrowserState
* browser_state
) {
61 return static_cast<TemplateURLService
*>(
62 GetInstance()->GetServiceForBrowserState(browser_state
, true));
66 TemplateURLServiceFactory
* TemplateURLServiceFactory::GetInstance() {
67 return base::Singleton
<TemplateURLServiceFactory
>::get();
71 BrowserStateKeyedServiceFactory::TestingFactoryFunction
72 TemplateURLServiceFactory::GetDefaultFactory() {
73 return &BuildTemplateURLService
;
76 TemplateURLServiceFactory::TemplateURLServiceFactory()
77 : BrowserStateKeyedServiceFactory(
79 BrowserStateDependencyManager::GetInstance()) {
80 DependsOn(ios::GoogleURLTrackerFactory::GetInstance());
81 DependsOn(ios::HistoryServiceFactory::GetInstance());
82 DependsOn(ios::WebDataServiceFactory::GetInstance());
85 TemplateURLServiceFactory::~TemplateURLServiceFactory() {}
87 void TemplateURLServiceFactory::RegisterBrowserStatePrefs(
88 user_prefs::PrefRegistrySyncable
* registry
) {
89 DefaultSearchManager::RegisterProfilePrefs(registry
);
90 TemplateURLService::RegisterProfilePrefs(registry
);
93 scoped_ptr
<KeyedService
> TemplateURLServiceFactory::BuildServiceInstanceFor(
94 web::BrowserState
* context
) const {
95 return BuildTemplateURLService(context
);
98 web::BrowserState
* TemplateURLServiceFactory::GetBrowserStateToUse(
99 web::BrowserState
* context
) const {
100 return GetBrowserStateRedirectedInIncognito(context
);
103 bool TemplateURLServiceFactory::ServiceIsNULLWhileTesting() const {