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/history/history_service_factory.h"
6
7#include "base/prefs/pref_service.h"
8#include "chrome/browser/bookmarks/bookmark_model_factory.h"
9#include "chrome/browser/bookmarks/chrome_bookmark_client.h"
10#include "chrome/browser/bookmarks/chrome_bookmark_client_factory.h"
11#include "chrome/browser/history/chrome_history_client.h"
12#include "chrome/browser/history/chrome_history_client_factory.h"
13#include "chrome/browser/history/history_service.h"
14#include "chrome/browser/profiles/incognito_helpers.h"
15#include "chrome/common/pref_names.h"
16#include "components/bookmarks/browser/bookmark_model.h"
17#include "components/keyed_service/content/browser_context_dependency_manager.h"
18
19// static
20HistoryService* HistoryServiceFactory::GetForProfile(
21    Profile* profile, Profile::ServiceAccessType sat) {
22  // If saving history is disabled, only allow explicit access.
23  if (profile->GetPrefs()->GetBoolean(prefs::kSavingBrowserHistoryDisabled) &&
24      sat != Profile::EXPLICIT_ACCESS)
25    return NULL;
26
27  return static_cast<HistoryService*>(
28      GetInstance()->GetServiceForBrowserContext(profile, true));
29}
30
31// static
32HistoryService*
33HistoryServiceFactory::GetForProfileIfExists(
34    Profile* profile, Profile::ServiceAccessType sat) {
35  // If saving history is disabled, only allow explicit access.
36  if (profile->GetPrefs()->GetBoolean(prefs::kSavingBrowserHistoryDisabled) &&
37      sat != Profile::EXPLICIT_ACCESS)
38    return NULL;
39
40  return static_cast<HistoryService*>(
41      GetInstance()->GetServiceForBrowserContext(profile, false));
42}
43
44// static
45HistoryService*
46HistoryServiceFactory::GetForProfileWithoutCreating(Profile* profile) {
47  return static_cast<HistoryService*>(
48      GetInstance()->GetServiceForBrowserContext(profile, false));
49}
50
51// static
52HistoryServiceFactory* HistoryServiceFactory::GetInstance() {
53  return Singleton<HistoryServiceFactory>::get();
54}
55
56// static
57void HistoryServiceFactory::ShutdownForProfile(Profile* profile) {
58  HistoryServiceFactory* factory = GetInstance();
59  factory->BrowserContextDestroyed(profile);
60}
61
62HistoryServiceFactory::HistoryServiceFactory()
63    : BrowserContextKeyedServiceFactory(
64          "HistoryService", BrowserContextDependencyManager::GetInstance()) {
65  DependsOn(ChromeHistoryClientFactory::GetInstance());
66  DependsOn(ChromeBookmarkClientFactory::GetInstance());
67}
68
69HistoryServiceFactory::~HistoryServiceFactory() {
70}
71
72KeyedService* HistoryServiceFactory::BuildServiceInstanceFor(
73    content::BrowserContext* context) const {
74  Profile* profile = static_cast<Profile*>(context);
75  scoped_ptr<HistoryService> history_service(new HistoryService(
76      ChromeHistoryClientFactory::GetForProfile(profile), profile));
77  if (!history_service->Init(profile->GetPath()))
78    return NULL;
79  ChromeBookmarkClientFactory::GetForProfile(profile)
80      ->SetHistoryService(history_service.get());
81  return history_service.release();
82}
83
84content::BrowserContext* HistoryServiceFactory::GetBrowserContextToUse(
85    content::BrowserContext* context) const {
86  return chrome::GetBrowserContextRedirectedInIncognito(context);
87}
88
89bool HistoryServiceFactory::ServiceIsNULLWhileTesting() const {
90  return true;
91}
92