summaryrefslogtreecommitdiffstats
path: root/ios/chrome/browser/history/web_history_service_factory.cc
blob: 9211fc6b3e74c2e135facf58806461e72da3ba01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ios/chrome/browser/history/web_history_service_factory.h"

#include "base/memory/singleton.h"
#include "components/browser_sync/browser/profile_sync_service.h"
#include "components/history/core/browser/web_history_service.h"
#include "components/keyed_service/ios/browser_state_dependency_manager.h"
#include "components/prefs/pref_service.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h"
#include "components/signin/core/browser/signin_manager.h"
#include "components/sync_driver/sync_service.h"
#include "ios/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/chrome/browser/signin/oauth2_token_service_factory.h"
#include "ios/chrome/browser/signin/signin_manager_factory.h"
#include "ios/chrome/browser/sync/ios_chrome_profile_sync_service_factory.h"
#include "net/url_request/url_request_context_getter.h"

namespace ios {
namespace {

// Returns true if the user is signed-in and full history sync is enabled,
// false otherwise.
bool IsHistorySyncEnabled(ios::ChromeBrowserState* browser_state) {
  sync_driver::SyncService* sync_service =
      IOSChromeProfileSyncServiceFactory::GetForBrowserState(browser_state);
  return sync_service && sync_service->IsSyncActive() &&
         sync_service->GetActiveDataTypes().Has(
             syncer::HISTORY_DELETE_DIRECTIVES);
}

}  // namespace

// static
history::WebHistoryService* WebHistoryServiceFactory::GetForBrowserState(
    ios::ChromeBrowserState* browser_state) {
  // Ensure that the service is not instantiated or used if the user is not
  // signed into sync, or if web history is not enabled.
  if (!IsHistorySyncEnabled(browser_state))
    return nullptr;

  return static_cast<history::WebHistoryService*>(
      GetInstance()->GetServiceForBrowserState(browser_state, true));
}

// static
WebHistoryServiceFactory* WebHistoryServiceFactory::GetInstance() {
  return base::Singleton<WebHistoryServiceFactory>::get();
}

WebHistoryServiceFactory::WebHistoryServiceFactory()
    : BrowserStateKeyedServiceFactory(
          "WebHistoryService",
          BrowserStateDependencyManager::GetInstance()) {
  DependsOn(IOSChromeProfileSyncServiceFactory::GetInstance());
  DependsOn(OAuth2TokenServiceFactory::GetInstance());
  DependsOn(ios::SigninManagerFactory::GetInstance());
}

WebHistoryServiceFactory::~WebHistoryServiceFactory() {
}

scoped_ptr<KeyedService> WebHistoryServiceFactory::BuildServiceInstanceFor(
    web::BrowserState* context) const {
  ios::ChromeBrowserState* browser_state =
      ios::ChromeBrowserState::FromBrowserState(context);
  return make_scoped_ptr(new history::WebHistoryService(
      OAuth2TokenServiceFactory::GetForBrowserState(browser_state),
      ios::SigninManagerFactory::GetForBrowserState(browser_state),
      browser_state->GetRequestContext()));
}

}  // namespace ios