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/web_history_service_factory.h" 6 7 #include "base/command_line.h" 8 #include "chrome/browser/content_settings/cookie_settings.h" 9 #include "chrome/browser/history/web_history_service.h" 10 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" 11 #include "chrome/browser/sync/profile_sync_service.h" 12 #include "chrome/browser/sync/profile_sync_service_factory.h" 13 #include "components/keyed_service/content/browser_context_dependency_manager.h" 14 15 namespace { 16 // Returns true if the user is signed in and full history sync is enabled, 17 // and false otherwise. 18 bool IsHistorySyncEnabled(Profile* profile) { 19 ProfileSyncService* sync = 20 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile); 21 return sync && 22 sync->sync_initialized() && 23 sync->GetActiveDataTypes().Has(syncer::HISTORY_DELETE_DIRECTIVES); 24 } 25 26 } // namespace 27 28 // static 29 WebHistoryServiceFactory* WebHistoryServiceFactory::GetInstance() { 30 return Singleton<WebHistoryServiceFactory>::get(); 31 } 32 33 // static 34 history::WebHistoryService* WebHistoryServiceFactory::GetForProfile( 35 Profile* profile) { 36 if (IsHistorySyncEnabled(profile)) { 37 return static_cast<history::WebHistoryService*>( 38 GetInstance()->GetServiceForBrowserContext(profile, true)); 39 } 40 return NULL; 41 } 42 43 KeyedService* WebHistoryServiceFactory::BuildServiceInstanceFor( 44 content::BrowserContext* context) const { 45 Profile* profile = static_cast<Profile*>(context); 46 47 // Ensure that the service is not instantiated or used if the user is not 48 // signed into sync, or if web history is not enabled. 49 return IsHistorySyncEnabled(profile) ? 50 new history::WebHistoryService(profile) : NULL; 51 } 52 53 WebHistoryServiceFactory::WebHistoryServiceFactory() 54 : BrowserContextKeyedServiceFactory( 55 "WebHistoryServiceFactory", 56 BrowserContextDependencyManager::GetInstance()) { 57 DependsOn(CookieSettings::Factory::GetInstance()); 58 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance()); 59 } 60 61 WebHistoryServiceFactory::~WebHistoryServiceFactory() { 62 } 63