1 // Copyright (c) 2013 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/profile_policy_connector_factory.h"
7 #include "base/logging.h"
8 #include "base/memory/singleton.h"
9 #include "chrome/browser/policy/profile_policy_connector.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/common/pref_names.h"
12 #include "components/browser_context_keyed_service/browser_context_dependency_manager.h"
13 #include "components/user_prefs/pref_registry_syncable.h"
15 #if defined(ENABLE_CONFIGURATION_POLICY)
16 #include "chrome/browser/policy/schema_registry_service.h"
17 #include "chrome/browser/policy/schema_registry_service_factory.h"
18 #if defined(OS_CHROMEOS)
19 #include "chrome/browser/chromeos/login/user.h"
20 #include "chrome/browser/chromeos/login/user_manager.h"
21 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h"
22 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chromeos.h"
23 #include "chrome/browser/chromeos/profiles/profile_helper.h"
25 #include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h"
26 #include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
33 ProfilePolicyConnectorFactory
* ProfilePolicyConnectorFactory::GetInstance() {
34 return Singleton
<ProfilePolicyConnectorFactory
>::get();
38 ProfilePolicyConnector
* ProfilePolicyConnectorFactory::GetForProfile(
40 return GetInstance()->GetForProfileInternal(profile
);
44 scoped_ptr
<ProfilePolicyConnector
>
45 ProfilePolicyConnectorFactory::CreateForProfile(Profile
* profile
,
46 bool force_immediate_load
) {
47 return GetInstance()->CreateForProfileInternal(profile
, force_immediate_load
);
50 void ProfilePolicyConnectorFactory::SetServiceForTesting(
52 ProfilePolicyConnector
* connector
) {
53 ProfilePolicyConnector
*& map_entry
= connectors_
[profile
];
55 map_entry
= connector
;
58 ProfilePolicyConnectorFactory::ProfilePolicyConnectorFactory()
59 : BrowserContextKeyedBaseFactory(
60 "ProfilePolicyConnector",
61 BrowserContextDependencyManager::GetInstance()) {
62 #if defined(ENABLE_CONFIGURATION_POLICY)
63 DependsOn(SchemaRegistryServiceFactory::GetInstance());
64 #if defined(OS_CHROMEOS)
65 DependsOn(UserCloudPolicyManagerFactoryChromeOS::GetInstance());
67 DependsOn(UserCloudPolicyManagerFactory::GetInstance());
72 ProfilePolicyConnectorFactory::~ProfilePolicyConnectorFactory() {
73 DCHECK(connectors_
.empty());
76 ProfilePolicyConnector
*
77 ProfilePolicyConnectorFactory::GetForProfileInternal(Profile
* profile
) {
78 // Get the connector for the original Profile, so that the incognito Profile
79 // gets managed settings from the same PolicyService.
80 ConnectorMap::const_iterator it
=
81 connectors_
.find(profile
->GetOriginalProfile());
82 CHECK(it
!= connectors_
.end());
86 scoped_ptr
<ProfilePolicyConnector
>
87 ProfilePolicyConnectorFactory::CreateForProfileInternal(
89 bool force_immediate_load
) {
90 DCHECK(connectors_
.find(profile
) == connectors_
.end());
92 SchemaRegistry
* schema_registry
= NULL
;
93 CloudPolicyManager
* user_cloud_policy_manager
= NULL
;
95 #if defined(ENABLE_CONFIGURATION_POLICY)
96 schema_registry
= SchemaRegistryServiceFactory::GetForContext(profile
);
98 #if defined(OS_CHROMEOS)
99 chromeos::User
* user
= NULL
;
100 if (!chromeos::ProfileHelper::IsSigninProfile(profile
)) {
101 chromeos::UserManager
* user_manager
= chromeos::UserManager::Get();
102 user
= user_manager
->GetUserByProfile(profile
);
105 user_cloud_policy_manager
=
106 UserCloudPolicyManagerFactoryChromeOS::GetForProfile(profile
);
108 user_cloud_policy_manager
=
109 UserCloudPolicyManagerFactory::GetForBrowserContext(profile
);
110 #endif // defined(OS_CHROMEOS)
111 #endif // defined(ENABLE_CONFIGURATION_POLICY)
113 ProfilePolicyConnector
* connector
= new ProfilePolicyConnector();
114 connector
->Init(force_immediate_load
,
115 #if defined(ENABLE_CONFIGURATION_POLICY) && defined(OS_CHROMEOS)
119 user_cloud_policy_manager
);
120 connectors_
[profile
] = connector
;
121 return make_scoped_ptr(connector
);
124 void ProfilePolicyConnectorFactory::BrowserContextShutdown(
125 content::BrowserContext
* context
) {
126 Profile
* profile
= static_cast<Profile
*>(context
);
127 if (profile
->IsOffTheRecord())
129 ConnectorMap::iterator it
= connectors_
.find(profile
);
130 if (it
!= connectors_
.end())
131 it
->second
->Shutdown();
134 void ProfilePolicyConnectorFactory::BrowserContextDestroyed(
135 content::BrowserContext
* context
) {
136 ConnectorMap::iterator it
= connectors_
.find(static_cast<Profile
*>(context
));
137 if (it
!= connectors_
.end())
138 connectors_
.erase(it
);
139 BrowserContextKeyedBaseFactory::BrowserContextDestroyed(context
);
142 void ProfilePolicyConnectorFactory::RegisterProfilePrefs(
143 user_prefs::PrefRegistrySyncable
* registry
) {
144 #if defined(OS_ANDROID)
145 registry
->RegisterListPref(
146 prefs::kManagedBookmarks
,
147 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
151 void ProfilePolicyConnectorFactory::SetEmptyTestingFactory(
152 content::BrowserContext
* context
) {}
154 void ProfilePolicyConnectorFactory::CreateServiceNow(
155 content::BrowserContext
* context
) {}
157 } // namespace policy