Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / policy / cloud / user_policy_signin_service_factory.cc
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/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"
19
20 #if defined(OS_ANDROID)
21 #include "chrome/browser/policy/cloud/user_policy_signin_service_android.h"
22 #elif defined(OS_IOS)
23 #include "chrome/browser/policy/cloud/user_policy_signin_service_ios.h"
24 #else
25 #include "chrome/browser/policy/cloud/user_policy_signin_service.h"
26 #endif
27
28 namespace policy {
29
30 namespace {
31
32 // Used only for testing.
33 DeviceManagementService* g_device_management_service = NULL;
34
35 }  // namespace
36
37 UserPolicySigninServiceFactory::UserPolicySigninServiceFactory()
38     : BrowserContextKeyedServiceFactory(
39         "UserPolicySigninService",
40         BrowserContextDependencyManager::GetInstance()) {
41   DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
42   DependsOn(SigninManagerFactory::GetInstance());
43   DependsOn(UserCloudPolicyManagerFactory::GetInstance());
44 }
45
46 UserPolicySigninServiceFactory::~UserPolicySigninServiceFactory() {}
47
48 // static
49 UserPolicySigninService* UserPolicySigninServiceFactory::GetForProfile(
50     Profile* profile) {
51   return static_cast<UserPolicySigninService*>(
52       GetInstance()->GetServiceForBrowserContext(profile, true));
53 }
54
55 // static
56 UserPolicySigninServiceFactory* UserPolicySigninServiceFactory::GetInstance() {
57   return Singleton<UserPolicySigninServiceFactory>::get();
58 }
59
60 // static
61 void UserPolicySigninServiceFactory::SetDeviceManagementServiceForTesting(
62     DeviceManagementService* device_management_service) {
63   g_device_management_service = device_management_service;
64 }
65
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(
76       profile,
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));
83   return service;
84 }
85
86 bool
87 UserPolicySigninServiceFactory::ServiceIsCreatedWithBrowserContext() const {
88 #if defined(OS_IOS)
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).
93   return false;
94 #else
95   // Create this object when the profile is created so it can track any
96   // user signin activity.
97   return true;
98 #endif
99 }
100
101 void UserPolicySigninServiceFactory::RegisterProfilePrefs(
102     user_prefs::PrefRegistrySyncable* user_prefs) {
103 #if defined(OS_ANDROID)
104   user_prefs->RegisterInt64Pref(
105       prefs::kLastPolicyCheckTime,
106       0,
107       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
108 #endif
109 }
110
111 }  // namespace policy