Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / history / history_service_factory.cc
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/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"
14
15 // static
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)
21     return NULL;
22
23   return static_cast<HistoryService*>(
24       GetInstance()->GetServiceForBrowserContext(profile, true));
25 }
26
27 // static
28 HistoryService*
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)
34     return NULL;
35
36   return static_cast<HistoryService*>(
37       GetInstance()->GetServiceForBrowserContext(profile, false));
38 }
39
40 // static
41 HistoryService*
42 HistoryServiceFactory::GetForProfileWithoutCreating(Profile* profile) {
43   return static_cast<HistoryService*>(
44       GetInstance()->GetServiceForBrowserContext(profile, false));
45 }
46
47 // static
48 HistoryServiceFactory* HistoryServiceFactory::GetInstance() {
49   return Singleton<HistoryServiceFactory>::get();
50 }
51
52 // static
53 void HistoryServiceFactory::ShutdownForProfile(Profile* profile) {
54   HistoryServiceFactory* factory = GetInstance();
55   factory->BrowserContextDestroyed(profile);
56 }
57
58 HistoryServiceFactory::HistoryServiceFactory()
59     : BrowserContextKeyedServiceFactory(
60           "HistoryService", BrowserContextDependencyManager::GetInstance()) {
61   DependsOn(BookmarkModelFactory::GetInstance());
62 }
63
64 HistoryServiceFactory::~HistoryServiceFactory() {
65 }
66
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))) {
73     return NULL;
74   }
75   return history_service;
76 }
77
78 content::BrowserContext* HistoryServiceFactory::GetBrowserContextToUse(
79     content::BrowserContext* context) const {
80   return chrome::GetBrowserContextRedirectedInIncognito(context);
81 }
82
83 bool HistoryServiceFactory::ServiceIsNULLWhileTesting() const {
84   return true;
85 }