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/browser_context_keyed_service/browser_context_dependency_manager.h"
16 #include "components/policy/core/browser/browser_policy_connector.h"
17 #include "components/user_prefs/pref_registry_syncable.h"
18 #include "net/url_request/url_request_context_getter.h"
20 #if defined(OS_ANDROID)
21 #include "chrome/browser/policy/cloud/user_policy_signin_service_android.h"
23 #include "chrome/browser/policy/cloud/user_policy_signin_service_ios.h"
25 #include "chrome/browser/policy/cloud/user_policy_signin_service.h"
32 // Used only for testing.
33 DeviceManagementService* g_device_management_service = NULL;
37 UserPolicySigninServiceFactory::UserPolicySigninServiceFactory()
38 : BrowserContextKeyedServiceFactory(
39 "UserPolicySigninService",
40 BrowserContextDependencyManager::GetInstance()) {
41 DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
42 DependsOn(SigninManagerFactory::GetInstance());
43 DependsOn(UserCloudPolicyManagerFactory::GetInstance());
46 UserPolicySigninServiceFactory::~UserPolicySigninServiceFactory() {}
49 UserPolicySigninService* UserPolicySigninServiceFactory::GetForProfile(
51 return static_cast<UserPolicySigninService*>(
52 GetInstance()->GetServiceForBrowserContext(profile, true));
56 UserPolicySigninServiceFactory* UserPolicySigninServiceFactory::GetInstance() {
57 return Singleton<UserPolicySigninServiceFactory>::get();
61 void UserPolicySigninServiceFactory::SetDeviceManagementServiceForTesting(
62 DeviceManagementService* device_management_service) {
63 g_device_management_service = device_management_service;
66 BrowserContextKeyedService*
67 UserPolicySigninServiceFactory::BuildServiceInstanceFor(
68 content::BrowserContext* context) const {
69 Profile* profile = static_cast<Profile*>(context);
70 BrowserPolicyConnector* connector =
71 g_browser_process->browser_policy_connector();
72 DeviceManagementService* device_management_service =
73 g_device_management_service ? g_device_management_service
74 : connector->device_management_service();
75 UserPolicySigninService* service = new UserPolicySigninService(
77 g_browser_process->local_state(),
78 device_management_service,
79 UserCloudPolicyManagerFactory::GetForBrowserContext(context),
80 SigninManagerFactory::GetForProfile(profile),
81 g_browser_process->system_request_context(),
82 ProfileOAuth2TokenServiceFactory::GetForProfile(profile));
87 UserPolicySigninServiceFactory::ServiceIsCreatedWithBrowserContext() const {
89 // This service isn't required at Profile creation time on iOS.
90 // Creating it at that time also leads to a crash, because the SigninManager
91 // trigger a token fetch too early (this isn't a problem on other platforms,
92 // because the refresh token isn't available that early).
95 // Create this object when the profile is created so it can track any
96 // user signin activity.
101 void UserPolicySigninServiceFactory::RegisterProfilePrefs(
102 user_prefs::PrefRegistrySyncable* user_prefs) {
103 #if defined(OS_ANDROID)
104 user_prefs->RegisterInt64Pref(
105 prefs::kLastPolicyCheckTime,
107 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
111 } // namespace policy