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/search_engines/template_url_service_factory.h"
6
7#include <string>
8
9#include "base/bind.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/rlz/rlz.h"
16#include "chrome/browser/search_engines/chrome_template_url_service_client.h"
17#include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
18#include "chrome/browser/webdata/web_data_service_factory.h"
19#include "components/keyed_service/content/browser_context_dependency_manager.h"
20#include "components/pref_registry/pref_registry_syncable.h"
21#include "components/search_engines/default_search_manager.h"
22#include "components/search_engines/search_engines_pref_names.h"
23#include "components/search_engines/template_url_service.h"
24
25// static
26TemplateURLService* TemplateURLServiceFactory::GetForProfile(Profile* profile) {
27  return static_cast<TemplateURLService*>(
28      GetInstance()->GetServiceForBrowserContext(profile, true));
29}
30
31// static
32TemplateURLServiceFactory* TemplateURLServiceFactory::GetInstance() {
33  return Singleton<TemplateURLServiceFactory>::get();
34}
35
36// static
37KeyedService* TemplateURLServiceFactory::BuildInstanceFor(
38    content::BrowserContext* context) {
39  base::Closure dsp_change_callback;
40#if defined(ENABLE_RLZ)
41  dsp_change_callback =
42      base::Bind(base::IgnoreResult(&RLZTracker::RecordProductEvent),
43                 rlz_lib::CHROME,
44                 RLZTracker::ChromeOmnibox(),
45                 rlz_lib::SET_TO_GOOGLE);
46#endif
47  Profile* profile = static_cast<Profile*>(context);
48  return new TemplateURLService(
49      profile->GetPrefs(),
50      scoped_ptr<SearchTermsData>(new UIThreadSearchTermsData(profile)),
51      WebDataServiceFactory::GetKeywordWebDataForProfile(
52          profile, Profile::EXPLICIT_ACCESS),
53      scoped_ptr<TemplateURLServiceClient>(
54          new ChromeTemplateURLServiceClient(profile)),
55      GoogleURLTrackerFactory::GetForProfile(profile),
56      g_browser_process->rappor_service(),
57      dsp_change_callback);
58}
59
60TemplateURLServiceFactory::TemplateURLServiceFactory()
61    : BrowserContextKeyedServiceFactory(
62        "TemplateURLServiceFactory",
63        BrowserContextDependencyManager::GetInstance()) {
64  DependsOn(GoogleURLTrackerFactory::GetInstance());
65  DependsOn(HistoryServiceFactory::GetInstance());
66  DependsOn(WebDataServiceFactory::GetInstance());
67}
68
69TemplateURLServiceFactory::~TemplateURLServiceFactory() {}
70
71KeyedService* TemplateURLServiceFactory::BuildServiceInstanceFor(
72    content::BrowserContext* profile) const {
73  return BuildInstanceFor(static_cast<Profile*>(profile));
74}
75
76void TemplateURLServiceFactory::RegisterProfilePrefs(
77    user_prefs::PrefRegistrySyncable* registry) {
78  DefaultSearchManager::RegisterProfilePrefs(registry);
79  registry->RegisterStringPref(prefs::kSyncedDefaultSearchProviderGUID,
80                               std::string(),
81                               user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
82  registry->RegisterBooleanPref(
83      prefs::kDefaultSearchProviderEnabled,
84      true,
85      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
86  registry->RegisterStringPref(
87      prefs::kDefaultSearchProviderName,
88      std::string(),
89      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
90  registry->RegisterStringPref(
91      prefs::kDefaultSearchProviderID,
92      std::string(),
93      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
94  registry->RegisterStringPref(
95      prefs::kDefaultSearchProviderPrepopulateID,
96      std::string(),
97      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
98  registry->RegisterStringPref(
99      prefs::kDefaultSearchProviderSuggestURL,
100      std::string(),
101      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
102  registry->RegisterStringPref(
103      prefs::kDefaultSearchProviderSearchURL,
104      std::string(),
105      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
106  registry->RegisterStringPref(
107      prefs::kDefaultSearchProviderInstantURL,
108      std::string(),
109      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
110  registry->RegisterStringPref(
111      prefs::kDefaultSearchProviderImageURL,
112      std::string(),
113      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
114  registry->RegisterStringPref(
115      prefs::kDefaultSearchProviderNewTabURL,
116      std::string(),
117      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
118  registry->RegisterStringPref(
119      prefs::kDefaultSearchProviderSearchURLPostParams,
120      std::string(),
121      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
122  registry->RegisterStringPref(
123      prefs::kDefaultSearchProviderSuggestURLPostParams,
124      std::string(),
125      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
126  registry->RegisterStringPref(
127      prefs::kDefaultSearchProviderInstantURLPostParams,
128      std::string(),
129      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
130  registry->RegisterStringPref(
131      prefs::kDefaultSearchProviderImageURLPostParams,
132      std::string(),
133      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
134  registry->RegisterStringPref(
135      prefs::kDefaultSearchProviderKeyword,
136      std::string(),
137      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
138  registry->RegisterStringPref(
139      prefs::kDefaultSearchProviderIconURL,
140      std::string(),
141      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
142  registry->RegisterStringPref(
143      prefs::kDefaultSearchProviderEncodings,
144      std::string(),
145      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
146  registry->RegisterListPref(prefs::kDefaultSearchProviderAlternateURLs,
147                             user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
148  registry->RegisterStringPref(
149      prefs::kDefaultSearchProviderSearchTermsReplacementKey,
150      std::string(),
151      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
152}
153
154content::BrowserContext* TemplateURLServiceFactory::GetBrowserContextToUse(
155    content::BrowserContext* context) const {
156  return chrome::GetBrowserContextRedirectedInIncognito(context);
157}
158
159bool TemplateURLServiceFactory::ServiceIsNULLWhileTesting() const {
160  return true;
161}
162