blob: e2d93663a73bf55fcd25ec68a866ba1fe550c7c7 [file] [log] [blame]
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include <string>
#include "base/bind.h"
#include "base/memory/ptr_util.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/google/google_url_tracker_factory.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/chrome_template_url_service_client.h"
#include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
#include "chrome/browser/web_data_service_factory.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"
#include "components/search_engines/default_search_manager.h"
#include "components/search_engines/search_engines_pref_names.h"
#include "components/search_engines/template_url_service.h"
#include "rlz/features/features.h"
#if BUILDFLAG(ENABLE_RLZ)
#include "components/rlz/rlz_tracker.h"
#endif
// static
TemplateURLService* TemplateURLServiceFactory::GetForProfile(Profile* profile) {
return static_cast<TemplateURLService*>(
GetInstance()->GetServiceForBrowserContext(profile, true));
}
// static
TemplateURLServiceFactory* TemplateURLServiceFactory::GetInstance() {
return base::Singleton<TemplateURLServiceFactory>::get();
}
// static
std::unique_ptr<KeyedService> TemplateURLServiceFactory::BuildInstanceFor(
content::BrowserContext* context) {
base::Closure dsp_change_callback;
#if BUILDFLAG(ENABLE_RLZ)
dsp_change_callback = base::Bind(
base::IgnoreResult(&rlz::RLZTracker::RecordProductEvent), rlz_lib::CHROME,
rlz::RLZTracker::ChromeOmnibox(), rlz_lib::SET_TO_GOOGLE);
#endif
Profile* profile = static_cast<Profile*>(context);
return base::MakeUnique<TemplateURLService>(
profile->GetPrefs(),
std::unique_ptr<SearchTermsData>(new UIThreadSearchTermsData(profile)),
WebDataServiceFactory::GetKeywordWebDataForProfile(
profile, ServiceAccessType::EXPLICIT_ACCESS),
std::unique_ptr<TemplateURLServiceClient>(
new ChromeTemplateURLServiceClient(
HistoryServiceFactory::GetForProfile(
profile, ServiceAccessType::EXPLICIT_ACCESS))),
GoogleURLTrackerFactory::GetForProfile(profile),
g_browser_process->rappor_service(), dsp_change_callback);
}
TemplateURLServiceFactory::TemplateURLServiceFactory()
: BrowserContextKeyedServiceFactory(
"TemplateURLServiceFactory",
BrowserContextDependencyManager::GetInstance()) {
DependsOn(GoogleURLTrackerFactory::GetInstance());
DependsOn(HistoryServiceFactory::GetInstance());
DependsOn(WebDataServiceFactory::GetInstance());
}
TemplateURLServiceFactory::~TemplateURLServiceFactory() {}
KeyedService* TemplateURLServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* profile) const {
return BuildInstanceFor(static_cast<Profile*>(profile)).release();
}
void TemplateURLServiceFactory::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
DefaultSearchManager::RegisterProfilePrefs(registry);
TemplateURLService::RegisterProfilePrefs(registry);
}
content::BrowserContext* TemplateURLServiceFactory::GetBrowserContextToUse(
content::BrowserContext* context) const {
return chrome::GetBrowserContextRedirectedInIncognito(context);
}
bool TemplateURLServiceFactory::ServiceIsNULLWhileTesting() const {
return true;
}