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/search_engines/template_url_service_factory.h"
10 #include "base/prefs/pref_service.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/google/google_url_tracker_factory.h"
13 #include "chrome/browser/history/history_service_factory.h"
14 #include "chrome/browser/profiles/incognito_helpers.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/rlz/rlz.h"
17 #include "chrome/browser/search_engines/chrome_template_url_service_client.h"
18 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
19 #include "chrome/browser/webdata/web_data_service_factory.h"
20 #include "components/keyed_service/content/browser_context_dependency_manager.h"
21 #include "components/pref_registry/pref_registry_syncable.h"
22 #include "components/search_engines/default_search_manager.h"
23 #include "components/search_engines/search_engines_pref_names.h"
24 #include "components/search_engines/template_url_service.h"
27 TemplateURLService
* TemplateURLServiceFactory::GetForProfile(Profile
* profile
) {
28 return static_cast<TemplateURLService
*>(
29 GetInstance()->GetServiceForBrowserContext(profile
, true));
33 TemplateURLServiceFactory
* TemplateURLServiceFactory::GetInstance() {
34 return Singleton
<TemplateURLServiceFactory
>::get();
38 scoped_ptr
<KeyedService
> TemplateURLServiceFactory::BuildInstanceFor(
39 content::BrowserContext
* context
) {
40 base::Closure dsp_change_callback
;
41 #if defined(ENABLE_RLZ)
43 base::Bind(base::IgnoreResult(&RLZTracker::RecordProductEvent
),
45 RLZTracker::ChromeOmnibox(),
46 rlz_lib::SET_TO_GOOGLE
);
48 Profile
* profile
= static_cast<Profile
*>(context
);
49 return make_scoped_ptr(new TemplateURLService(
51 scoped_ptr
<SearchTermsData
>(new UIThreadSearchTermsData(profile
)),
52 WebDataServiceFactory::GetKeywordWebDataForProfile(
53 profile
, ServiceAccessType::EXPLICIT_ACCESS
),
54 scoped_ptr
<TemplateURLServiceClient
>(new ChromeTemplateURLServiceClient(
55 HistoryServiceFactory::GetForProfile(
56 profile
, ServiceAccessType::EXPLICIT_ACCESS
))),
57 GoogleURLTrackerFactory::GetForProfile(profile
),
58 g_browser_process
->rappor_service(), dsp_change_callback
));
61 TemplateURLServiceFactory::TemplateURLServiceFactory()
62 : BrowserContextKeyedServiceFactory(
63 "TemplateURLServiceFactory",
64 BrowserContextDependencyManager::GetInstance()) {
65 DependsOn(GoogleURLTrackerFactory::GetInstance());
66 DependsOn(HistoryServiceFactory::GetInstance());
67 DependsOn(WebDataServiceFactory::GetInstance());
70 TemplateURLServiceFactory::~TemplateURLServiceFactory() {}
72 KeyedService
* TemplateURLServiceFactory::BuildServiceInstanceFor(
73 content::BrowserContext
* profile
) const {
74 return BuildInstanceFor(static_cast<Profile
*>(profile
)).release();
77 void TemplateURLServiceFactory::RegisterProfilePrefs(
78 user_prefs::PrefRegistrySyncable
* registry
) {
79 DefaultSearchManager::RegisterProfilePrefs(registry
);
80 registry
->RegisterStringPref(prefs::kSyncedDefaultSearchProviderGUID
,
82 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
83 registry
->RegisterBooleanPref(prefs::kDefaultSearchProviderEnabled
, true);
84 registry
->RegisterStringPref(prefs::kDefaultSearchProviderName
,
86 registry
->RegisterStringPref(prefs::kDefaultSearchProviderID
, std::string());
87 registry
->RegisterStringPref(prefs::kDefaultSearchProviderPrepopulateID
,
89 registry
->RegisterStringPref(prefs::kDefaultSearchProviderSuggestURL
,
91 registry
->RegisterStringPref(prefs::kDefaultSearchProviderSearchURL
,
93 registry
->RegisterStringPref(prefs::kDefaultSearchProviderInstantURL
,
95 registry
->RegisterStringPref(prefs::kDefaultSearchProviderImageURL
,
97 registry
->RegisterStringPref(prefs::kDefaultSearchProviderNewTabURL
,
99 registry
->RegisterStringPref(prefs::kDefaultSearchProviderSearchURLPostParams
,
101 registry
->RegisterStringPref(
102 prefs::kDefaultSearchProviderSuggestURLPostParams
, std::string());
103 registry
->RegisterStringPref(
104 prefs::kDefaultSearchProviderInstantURLPostParams
, std::string());
105 registry
->RegisterStringPref(prefs::kDefaultSearchProviderImageURLPostParams
,
107 registry
->RegisterStringPref(prefs::kDefaultSearchProviderKeyword
,
109 registry
->RegisterStringPref(prefs::kDefaultSearchProviderIconURL
,
111 registry
->RegisterStringPref(prefs::kDefaultSearchProviderEncodings
,
113 registry
->RegisterListPref(prefs::kDefaultSearchProviderAlternateURLs
);
114 registry
->RegisterStringPref(
115 prefs::kDefaultSearchProviderSearchTermsReplacementKey
, std::string());
118 content::BrowserContext
* TemplateURLServiceFactory::GetBrowserContextToUse(
119 content::BrowserContext
* context
) const {
120 return chrome::GetBrowserContextRedirectedInIncognito(context
);
123 bool TemplateURLServiceFactory::ServiceIsNULLWhileTesting() const {