Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / policy / cloud / user_policy_signin_service_factory.cc
blob40e831e95f7fa8a9a17769ce8e76135459094c89
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.
5 #include "chrome/browser/policy/cloud/user_policy_signin_service_factory.h"
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"
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
26 namespace policy {
28 namespace {
30 // Used only for testing.
31 DeviceManagementService* g_device_management_service = NULL;
33 } // namespace
35 UserPolicySigninServiceFactory::UserPolicySigninServiceFactory()
36 : BrowserContextKeyedServiceFactory(
37 "UserPolicySigninService",
38 BrowserContextDependencyManager::GetInstance()) {
39 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
40 DependsOn(SigninManagerFactory::GetInstance());
41 DependsOn(UserCloudPolicyManagerFactory::GetInstance());
44 UserPolicySigninServiceFactory::~UserPolicySigninServiceFactory() {}
46 // static
47 UserPolicySigninService* UserPolicySigninServiceFactory::GetForProfile(
48 Profile* profile) {
49 return static_cast<UserPolicySigninService*>(
50 GetInstance()->GetServiceForBrowserContext(profile, true));
53 // static
54 UserPolicySigninServiceFactory* UserPolicySigninServiceFactory::GetInstance() {
55 return base::Singleton<UserPolicySigninServiceFactory>::get();
58 // static
59 void UserPolicySigninServiceFactory::SetDeviceManagementServiceForTesting(
60 DeviceManagementService* device_management_service) {
61 g_device_management_service = device_management_service;
64 KeyedService* 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;
83 bool
84 UserPolicySigninServiceFactory::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
98 void UserPolicySigninServiceFactory::RegisterProfilePrefs(
99 user_prefs::PrefRegistrySyncable* user_prefs) {
100 #if defined(OS_ANDROID) || defined(OS_IOS)
101 user_prefs->RegisterInt64Pref(prefs::kLastPolicyCheckTime, 0);
102 #endif
105 } // namespace policy