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