Home | History | Annotate | Download | only in search_engines
      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/prefs/pref_service.h"
     10 #include "chrome/browser/google/google_url_tracker_factory.h"
     11 #include "chrome/browser/history/history_service_factory.h"
     12 #include "chrome/browser/profiles/incognito_helpers.h"
     13 #include "chrome/browser/search_engines/template_url_service.h"
     14 #include "chrome/browser/webdata/web_data_service_factory.h"
     15 #include "chrome/common/pref_names.h"
     16 #include "components/browser_context_keyed_service/browser_context_dependency_manager.h"
     17 #include "components/user_prefs/pref_registry_syncable.h"
     18 
     19 // static
     20 TemplateURLService* TemplateURLServiceFactory::GetForProfile(Profile* profile) {
     21   return static_cast<TemplateURLService*>(
     22       GetInstance()->GetServiceForBrowserContext(profile, true));
     23 }
     24 
     25 // static
     26 TemplateURLServiceFactory* TemplateURLServiceFactory::GetInstance() {
     27   return Singleton<TemplateURLServiceFactory>::get();
     28 }
     29 
     30 // static
     31 BrowserContextKeyedService* TemplateURLServiceFactory::BuildInstanceFor(
     32     content::BrowserContext* profile) {
     33   return new TemplateURLService(static_cast<Profile*>(profile));
     34 }
     35 
     36 TemplateURLServiceFactory::TemplateURLServiceFactory()
     37     : BrowserContextKeyedServiceFactory(
     38         "TemplateURLServiceFactory",
     39         BrowserContextDependencyManager::GetInstance()) {
     40   DependsOn(GoogleURLTrackerFactory::GetInstance());
     41   DependsOn(HistoryServiceFactory::GetInstance());
     42   DependsOn(WebDataServiceFactory::GetInstance());
     43 }
     44 
     45 TemplateURLServiceFactory::~TemplateURLServiceFactory() {}
     46 
     47 BrowserContextKeyedService* TemplateURLServiceFactory::BuildServiceInstanceFor(
     48     content::BrowserContext* profile) const {
     49   return BuildInstanceFor(static_cast<Profile*>(profile));
     50 }
     51 
     52 void TemplateURLServiceFactory::RegisterProfilePrefs(
     53     user_prefs::PrefRegistrySyncable* registry) {
     54   registry->RegisterStringPref(prefs::kSyncedDefaultSearchProviderGUID,
     55                                std::string(),
     56                                user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
     57   registry->RegisterBooleanPref(
     58       prefs::kDefaultSearchProviderEnabled,
     59       true,
     60       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     61   registry->RegisterStringPref(
     62       prefs::kDefaultSearchProviderName,
     63       std::string(),
     64       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     65   registry->RegisterStringPref(
     66       prefs::kDefaultSearchProviderID,
     67       std::string(),
     68       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     69   registry->RegisterStringPref(
     70       prefs::kDefaultSearchProviderPrepopulateID,
     71       std::string(),
     72       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     73   registry->RegisterStringPref(
     74       prefs::kDefaultSearchProviderSuggestURL,
     75       std::string(),
     76       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     77   registry->RegisterStringPref(
     78       prefs::kDefaultSearchProviderSearchURL,
     79       std::string(),
     80       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     81   registry->RegisterStringPref(
     82       prefs::kDefaultSearchProviderInstantURL,
     83       std::string(),
     84       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     85   registry->RegisterStringPref(
     86       prefs::kDefaultSearchProviderImageURL,
     87       std::string(),
     88       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     89   registry->RegisterStringPref(
     90       prefs::kDefaultSearchProviderSearchURLPostParams,
     91       std::string(),
     92       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     93   registry->RegisterStringPref(
     94       prefs::kDefaultSearchProviderSuggestURLPostParams,
     95       std::string(),
     96       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
     97   registry->RegisterStringPref(
     98       prefs::kDefaultSearchProviderInstantURLPostParams,
     99       std::string(),
    100       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
    101   registry->RegisterStringPref(
    102       prefs::kDefaultSearchProviderImageURLPostParams,
    103       std::string(),
    104       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
    105   registry->RegisterStringPref(
    106       prefs::kDefaultSearchProviderKeyword,
    107       std::string(),
    108       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
    109   registry->RegisterStringPref(
    110       prefs::kDefaultSearchProviderIconURL,
    111       std::string(),
    112       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
    113   registry->RegisterStringPref(
    114       prefs::kDefaultSearchProviderEncodings,
    115       std::string(),
    116       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
    117   registry->RegisterListPref(prefs::kDefaultSearchProviderAlternateURLs,
    118                              user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
    119   registry->RegisterStringPref(
    120       prefs::kDefaultSearchProviderSearchTermsReplacementKey,
    121       std::string(),
    122       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
    123 }
    124 
    125 content::BrowserContext* TemplateURLServiceFactory::GetBrowserContextToUse(
    126     content::BrowserContext* context) const {
    127   return chrome::GetBrowserContextRedirectedInIncognito(context);
    128 }
    129 
    130 bool TemplateURLServiceFactory::ServiceIsNULLWhileTesting() const {
    131   return true;
    132 }
    133 
    134 void TemplateURLServiceFactory::BrowserContextShutdown(
    135     content::BrowserContext* profile) {
    136   // We shutdown AND destroy the TemplateURLService during this pass.
    137   // TemplateURLService schedules a task on the WebDataService from its
    138   // destructor. Delete it first to ensure the task gets scheduled before we
    139   // shut down the database.
    140   BrowserContextKeyedServiceFactory::BrowserContextShutdown(profile);
    141   BrowserContextKeyedServiceFactory::BrowserContextDestroyed(profile);
    142 }
    143 
    144 void TemplateURLServiceFactory::BrowserContextDestroyed(
    145     content::BrowserContext* profile) {
    146   // Don't double delete.
    147 }
    148