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/incognito_helpers.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "components/keyed_service/content/browser_context_dependency_manager.h"
13 #include "components/policy/core/common/policy_service.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 #include "components/policy/core/common/policy_service_impl.h"
19 #if defined(OS_CHROMEOS)
20 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h"
21 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chromeos.h"
22 #include "chrome/browser/chromeos/profiles/profile_helper.h"
23 #include "components/user_manager/user.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::GetForBrowserContext(
39 content::BrowserContext
* context
) {
40 return GetInstance()->GetForBrowserContextInternal(context
);
44 scoped_ptr
<ProfilePolicyConnector
>
45 ProfilePolicyConnectorFactory::CreateForBrowserContext(
46 content::BrowserContext
* context
,
47 bool force_immediate_load
) {
48 return GetInstance()->CreateForBrowserContextInternal(context
,
49 force_immediate_load
);
52 void ProfilePolicyConnectorFactory::SetServiceForTesting(
53 content::BrowserContext
* context
,
54 ProfilePolicyConnector
* connector
) {
55 ProfilePolicyConnector
*& map_entry
= connectors_
[context
];
57 map_entry
= connector
;
60 #if defined(ENABLE_CONFIGURATION_POLICY)
61 void ProfilePolicyConnectorFactory::PushProviderForTesting(
62 ConfigurationPolicyProvider
* provider
) {
63 test_providers_
.push_back(provider
);
67 ProfilePolicyConnectorFactory::ProfilePolicyConnectorFactory()
68 : BrowserContextKeyedBaseFactory(
69 "ProfilePolicyConnector",
70 BrowserContextDependencyManager::GetInstance()) {
71 #if defined(ENABLE_CONFIGURATION_POLICY)
72 DependsOn(SchemaRegistryServiceFactory::GetInstance());
73 #if defined(OS_CHROMEOS)
74 DependsOn(UserCloudPolicyManagerFactoryChromeOS::GetInstance());
76 DependsOn(UserCloudPolicyManagerFactory::GetInstance());
81 ProfilePolicyConnectorFactory::~ProfilePolicyConnectorFactory() {
82 DCHECK(connectors_
.empty());
85 ProfilePolicyConnector
*
86 ProfilePolicyConnectorFactory::GetForBrowserContextInternal(
87 content::BrowserContext
* context
) {
88 // Get the connector for the original Profile, so that the incognito Profile
89 // gets managed settings from the same PolicyService.
90 content::BrowserContext
* const original_context
=
91 chrome::GetBrowserContextRedirectedInIncognito(context
);
92 const ConnectorMap::const_iterator it
= connectors_
.find(original_context
);
93 CHECK(it
!= connectors_
.end());
97 scoped_ptr
<ProfilePolicyConnector
>
98 ProfilePolicyConnectorFactory::CreateForBrowserContextInternal(
99 content::BrowserContext
* context
,
100 bool force_immediate_load
) {
101 DCHECK(connectors_
.find(context
) == connectors_
.end());
103 SchemaRegistry
* schema_registry
= nullptr;
104 CloudPolicyManager
* user_cloud_policy_manager
= nullptr;
106 #if defined(ENABLE_CONFIGURATION_POLICY)
108 SchemaRegistryServiceFactory::GetForContext(context
)->registry();
110 #if defined(OS_CHROMEOS)
111 Profile
* const profile
= Profile::FromBrowserContext(context
);
112 const user_manager::User
* user
= nullptr;
113 if (!chromeos::ProfileHelper::IsSigninProfile(profile
)) {
114 user
= chromeos::ProfileHelper::Get()->GetUserByProfile(profile
);
117 user_cloud_policy_manager
=
118 UserCloudPolicyManagerFactoryChromeOS::GetForProfile(profile
);
120 user_cloud_policy_manager
=
121 UserCloudPolicyManagerFactory::GetForBrowserContext(context
);
122 #endif // defined(OS_CHROMEOS)
123 #endif // defined(ENABLE_CONFIGURATION_POLICY)
125 scoped_ptr
<ProfilePolicyConnector
> connector(new ProfilePolicyConnector());
127 #if defined(ENABLE_CONFIGURATION_POLICY)
128 if (test_providers_
.empty()) {
130 #if defined(OS_CHROMEOS)
133 schema_registry
, user_cloud_policy_manager
);
135 PolicyServiceImpl::Providers providers
;
136 providers
.push_back(test_providers_
.front());
137 test_providers_
.pop_front();
138 scoped_ptr
<PolicyService
> service(new PolicyServiceImpl(providers
));
139 connector
->InitForTesting(service
.Pass());
142 connector
->Init(nullptr, nullptr);
145 connectors_
[context
] = connector
.get();
146 return connector
.Pass();
149 void ProfilePolicyConnectorFactory::BrowserContextShutdown(
150 content::BrowserContext
* context
) {
151 if (Profile::FromBrowserContext(context
)->IsOffTheRecord())
153 const ConnectorMap::const_iterator it
= connectors_
.find(context
);
154 if (it
!= connectors_
.end())
155 it
->second
->Shutdown();
158 void ProfilePolicyConnectorFactory::BrowserContextDestroyed(
159 content::BrowserContext
* context
) {
160 const ConnectorMap::iterator it
= connectors_
.find(context
);
161 if (it
!= connectors_
.end())
162 connectors_
.erase(it
);
163 BrowserContextKeyedBaseFactory::BrowserContextDestroyed(context
);
166 void ProfilePolicyConnectorFactory::SetEmptyTestingFactory(
167 content::BrowserContext
* context
) {}
169 bool ProfilePolicyConnectorFactory::HasTestingFactory(
170 content::BrowserContext
* context
) {
174 void ProfilePolicyConnectorFactory::CreateServiceNow(
175 content::BrowserContext
* context
) {}
177 } // namespace policy