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/policy/cloud/user_policy_signin_service_factory.h"
6
7#include "base/memory/ref_counted.h"
8#include "base/prefs/pref_service.h"
9#include "chrome/browser/browser_process.h"
10#include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h"
11#include "chrome/browser/profiles/profile.h"
12#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
13#include "chrome/browser/signin/signin_manager_factory.h"
14#include "chrome/common/pref_names.h"
15#include "components/keyed_service/content/browser_context_dependency_manager.h"
16#include "components/policy/core/browser/browser_policy_connector.h"
17#include "components/pref_registry/pref_registry_syncable.h"
18#include "net/url_request/url_request_context_getter.h"
19
20#if defined(OS_ANDROID) || defined(OS_IOS)
21#include "chrome/browser/policy/cloud/user_policy_signin_service_mobile.h"
22#else
23#include "chrome/browser/policy/cloud/user_policy_signin_service.h"
24#endif
25
26namespace policy {
27
28namespace {
29
30// Used only for testing.
31DeviceManagementService* g_device_management_service = NULL;
32
33}  // namespace
34
35UserPolicySigninServiceFactory::UserPolicySigninServiceFactory()
36    : BrowserContextKeyedServiceFactory(
37        "UserPolicySigninService",
38        BrowserContextDependencyManager::GetInstance()) {
39  DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
40  DependsOn(SigninManagerFactory::GetInstance());
41  DependsOn(UserCloudPolicyManagerFactory::GetInstance());
42}
43
44UserPolicySigninServiceFactory::~UserPolicySigninServiceFactory() {}
45
46// static
47UserPolicySigninService* UserPolicySigninServiceFactory::GetForProfile(
48    Profile* profile) {
49  return static_cast<UserPolicySigninService*>(
50      GetInstance()->GetServiceForBrowserContext(profile, true));
51}
52
53// static
54UserPolicySigninServiceFactory* UserPolicySigninServiceFactory::GetInstance() {
55  return Singleton<UserPolicySigninServiceFactory>::get();
56}
57
58// static
59void UserPolicySigninServiceFactory::SetDeviceManagementServiceForTesting(
60    DeviceManagementService* device_management_service) {
61  g_device_management_service = device_management_service;
62}
63
64KeyedService* UserPolicySigninServiceFactory::BuildServiceInstanceFor(
65    content::BrowserContext* context) const {
66  Profile* profile = static_cast<Profile*>(context);
67  BrowserPolicyConnector* connector =
68      g_browser_process->browser_policy_connector();
69  DeviceManagementService* device_management_service =
70      g_device_management_service ? g_device_management_service
71                                  : connector->device_management_service();
72  UserPolicySigninService* service = new UserPolicySigninService(
73      profile,
74      g_browser_process->local_state(),
75      device_management_service,
76      UserCloudPolicyManagerFactory::GetForBrowserContext(context),
77      SigninManagerFactory::GetForProfile(profile),
78      g_browser_process->system_request_context(),
79      ProfileOAuth2TokenServiceFactory::GetForProfile(profile));
80  return service;
81}
82
83bool
84UserPolicySigninServiceFactory::ServiceIsCreatedWithBrowserContext() const {
85#if defined(OS_IOS)
86  // This service isn't required at Profile creation time on iOS.
87  // Creating it at that time also leads to a crash, because the SigninManager
88  // trigger a token fetch too early (this isn't a problem on other platforms,
89  // because the refresh token isn't available that early).
90  return false;
91#else
92  // Create this object when the profile is created so it can track any
93  // user signin activity.
94  return true;
95#endif
96}
97
98void UserPolicySigninServiceFactory::RegisterProfilePrefs(
99    user_prefs::PrefRegistrySyncable* user_prefs) {
100#if defined(OS_ANDROID) || defined(OS_IOS)
101  user_prefs->RegisterInt64Pref(
102      prefs::kLastPolicyCheckTime,
103      0,
104      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
105#endif
106}
107
108}  // namespace policy
109