- add sources.
[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/browser_policy_connector.h"
11 #include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
14 #include "chrome/browser/signin/signin_manager_factory.h"
15 #include "chrome/common/pref_names.h"
16 #include "components/browser_context_keyed_service/browser_context_dependency_manager.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 #else
23 #include "chrome/browser/policy/cloud/user_policy_signin_service.h"
24 #endif
25
26 namespace policy {
27
28 namespace {
29
30 // Used only for testing.
31 DeviceManagementService* g_device_management_service = NULL;
32
33 }  // namespace
34
35 UserPolicySigninServiceFactory::UserPolicySigninServiceFactory()
36     : BrowserContextKeyedServiceFactory(
37         "UserPolicySigninService",
38         BrowserContextDependencyManager::GetInstance()) {
39   DependsOn(ProfileOAuth2TokenServiceFactory::GetInstance());
40   DependsOn(SigninManagerFactory::GetInstance());
41   DependsOn(UserCloudPolicyManagerFactory::GetInstance());
42 }
43
44 UserPolicySigninServiceFactory::~UserPolicySigninServiceFactory() {}
45
46 // static
47 UserPolicySigninService* UserPolicySigninServiceFactory::GetForProfile(
48     Profile* profile) {
49   return static_cast<UserPolicySigninService*>(
50       GetInstance()->GetServiceForBrowserContext(profile, true));
51 }
52
53 // static
54 UserPolicySigninServiceFactory* UserPolicySigninServiceFactory::GetInstance() {
55   return Singleton<UserPolicySigninServiceFactory>::get();
56 }
57
58 // static
59 void UserPolicySigninServiceFactory::SetDeviceManagementServiceForTesting(
60     DeviceManagementService* device_management_service) {
61   g_device_management_service = device_management_service;
62 }
63
64 BrowserContextKeyedService*
65 UserPolicySigninServiceFactory::BuildServiceInstanceFor(
66     content::BrowserContext* context) const {
67   Profile* profile = static_cast<Profile*>(context);
68   BrowserPolicyConnector* connector =
69       g_browser_process->browser_policy_connector();
70   DeviceManagementService* device_management_service =
71       g_device_management_service ? g_device_management_service
72                                   : connector->device_management_service();
73   // TODO(atwilson): Inject SigninManager here or remove the dependency
74   // entirely. http://crbug.com/276270.
75   return new UserPolicySigninService(
76       profile,
77       g_browser_process->local_state(),
78       g_browser_process->system_request_context(),
79       device_management_service,
80       ProfileOAuth2TokenServiceFactory::GetForProfile(profile));
81 }
82
83 bool
84 UserPolicySigninServiceFactory::ServiceIsCreatedWithBrowserContext() const {
85   // Create this object when the profile is created so it can track any
86   // user signin activity.
87   return true;
88 }
89
90 void UserPolicySigninServiceFactory::RegisterProfilePrefs(
91     user_prefs::PrefRegistrySyncable* user_prefs) {
92 #if defined(OS_ANDROID)
93   user_prefs->RegisterInt64Pref(
94       prefs::kLastPolicyCheckTime,
95       0,
96       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
97 #endif
98 }
99
100 }  // namespace policy