Home | History | Annotate | Download | only in history
      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.h"
      9 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
     10 #include "chrome/browser/history/history_service.h"
     11 #include "chrome/browser/profiles/incognito_helpers.h"
     12 #include "chrome/common/pref_names.h"
     13 #include "components/browser_context_keyed_service/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 BrowserContextKeyedService*
     68 HistoryServiceFactory::BuildServiceInstanceFor(
     69     content::BrowserContext* context) const {
     70   Profile* profile = static_cast<Profile*>(context);
     71   HistoryService* history_service = new HistoryService(profile);
     72   if (!history_service->Init(profile->GetPath(),
     73                              BookmarkModelFactory::GetForProfile(profile))) {
     74     return NULL;
     75   }
     76   return history_service;
     77 }
     78 
     79 content::BrowserContext* HistoryServiceFactory::GetBrowserContextToUse(
     80     content::BrowserContext* context) const {
     81   return chrome::GetBrowserContextRedirectedInIncognito(context);
     82 }
     83 
     84 bool HistoryServiceFactory::ServiceIsNULLWhileTesting() const {
     85   return true;
     86 }
     87