Convert browser_tests to Swarming.
[chromium-blink-merge.git] / chrome / browser / policy / profile_policy_connector_factory.cc
blob5c2cb666bca537a3d01f7fcc843f0e50abb3d66b
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"
24 #else
25 #include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h"
26 #include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
27 #endif
28 #endif
30 namespace policy {
32 // static
33 ProfilePolicyConnectorFactory* ProfilePolicyConnectorFactory::GetInstance() {
34 return Singleton<ProfilePolicyConnectorFactory>::get();
37 // static
38 ProfilePolicyConnector* ProfilePolicyConnectorFactory::GetForBrowserContext(
39 content::BrowserContext* context) {
40 return GetInstance()->GetForBrowserContextInternal(context);
43 // static
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];
56 CHECK(!map_entry);
57 map_entry = connector;
60 #if defined(ENABLE_CONFIGURATION_POLICY)
61 void ProfilePolicyConnectorFactory::PushProviderForTesting(
62 ConfigurationPolicyProvider* provider) {
63 test_providers_.push_back(provider);
65 #endif
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());
75 #else
76 DependsOn(UserCloudPolicyManagerFactory::GetInstance());
77 #endif
78 #endif
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());
94 return it->second;
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 = NULL;
104 CloudPolicyManager* user_cloud_policy_manager = NULL;
106 #if defined(ENABLE_CONFIGURATION_POLICY)
107 schema_registry =
108 SchemaRegistryServiceFactory::GetForContext(context)->registry();
110 #if defined(OS_CHROMEOS)
111 Profile* const profile = Profile::FromBrowserContext(context);
112 const user_manager::User* user = NULL;
113 if (!chromeos::ProfileHelper::IsSigninProfile(profile)) {
114 user = chromeos::ProfileHelper::Get()->GetUserByProfile(profile);
115 CHECK(user);
117 user_cloud_policy_manager =
118 UserCloudPolicyManagerFactoryChromeOS::GetForProfile(profile);
119 #else
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()) {
129 connector->Init(force_immediate_load,
130 #if defined(OS_CHROMEOS)
131 user,
132 #endif
133 schema_registry,
134 user_cloud_policy_manager);
135 } else {
136 PolicyServiceImpl::Providers providers;
137 providers.push_back(test_providers_.front());
138 test_providers_.pop_front();
139 scoped_ptr<PolicyService> service(new PolicyServiceImpl(providers));
140 connector->InitForTesting(service.Pass());
142 #else
143 connector->Init(false, NULL, NULL);
144 #endif
146 connectors_[context] = connector.get();
147 return connector.Pass();
150 void ProfilePolicyConnectorFactory::BrowserContextShutdown(
151 content::BrowserContext* context) {
152 if (Profile::FromBrowserContext(context)->IsOffTheRecord())
153 return;
154 const ConnectorMap::const_iterator it = connectors_.find(context);
155 if (it != connectors_.end())
156 it->second->Shutdown();
159 void ProfilePolicyConnectorFactory::BrowserContextDestroyed(
160 content::BrowserContext* context) {
161 const ConnectorMap::iterator it = connectors_.find(context);
162 if (it != connectors_.end())
163 connectors_.erase(it);
164 BrowserContextKeyedBaseFactory::BrowserContextDestroyed(context);
167 void ProfilePolicyConnectorFactory::SetEmptyTestingFactory(
168 content::BrowserContext* context) {}
170 bool ProfilePolicyConnectorFactory::HasTestingFactory(
171 content::BrowserContext* context) {
172 return false;
175 void ProfilePolicyConnectorFactory::CreateServiceNow(
176 content::BrowserContext* context) {}
178 } // namespace policy