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/history/history_service_factory.h"
7 #include "base/prefs/pref_service.h"
8 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
9 #include "chrome/browser/history/history_service.h"
10 #include "chrome/browser/profiles/incognito_helpers.h"
11 #include "chrome/common/pref_names.h"
12 #include "components/bookmarks/core/browser/bookmark_model.h"
13 #include "components/keyed_service/content/browser_context_dependency_manager.h"
16 HistoryService* HistoryServiceFactory::GetForProfile(
17 Profile* profile, Profile::ServiceAccessType sat) {
18 // If saving history is disabled, only allow explicit access.
19 if (profile->GetPrefs()->GetBoolean(prefs::kSavingBrowserHistoryDisabled) &&
20 sat != Profile::EXPLICIT_ACCESS)
23 return static_cast<HistoryService*>(
24 GetInstance()->GetServiceForBrowserContext(profile, true));
29 HistoryServiceFactory::GetForProfileIfExists(
30 Profile* profile, Profile::ServiceAccessType sat) {
31 // If saving history is disabled, only allow explicit access.
32 if (profile->GetPrefs()->GetBoolean(prefs::kSavingBrowserHistoryDisabled) &&
33 sat != Profile::EXPLICIT_ACCESS)
36 return static_cast<HistoryService*>(
37 GetInstance()->GetServiceForBrowserContext(profile, false));
42 HistoryServiceFactory::GetForProfileWithoutCreating(Profile* profile) {
43 return static_cast<HistoryService*>(
44 GetInstance()->GetServiceForBrowserContext(profile, false));
48 HistoryServiceFactory* HistoryServiceFactory::GetInstance() {
49 return Singleton<HistoryServiceFactory>::get();
53 void HistoryServiceFactory::ShutdownForProfile(Profile* profile) {
54 HistoryServiceFactory* factory = GetInstance();
55 factory->BrowserContextDestroyed(profile);
58 HistoryServiceFactory::HistoryServiceFactory()
59 : BrowserContextKeyedServiceFactory(
60 "HistoryService", BrowserContextDependencyManager::GetInstance()) {
61 DependsOn(BookmarkModelFactory::GetInstance());
64 HistoryServiceFactory::~HistoryServiceFactory() {
67 KeyedService* HistoryServiceFactory::BuildServiceInstanceFor(
68 content::BrowserContext* context) const {
69 Profile* profile = static_cast<Profile*>(context);
70 HistoryService* history_service = new HistoryService(profile);
71 if (!history_service->Init(profile->GetPath(),
72 BookmarkModelFactory::GetForProfile(profile))) {
75 return history_service;
78 content::BrowserContext* HistoryServiceFactory::GetBrowserContextToUse(
79 content::BrowserContext* context) const {
80 return chrome::GetBrowserContextRedirectedInIncognito(context);
83 bool HistoryServiceFactory::ServiceIsNULLWhileTesting() const {