Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / policy / profile_policy_connector_factory.cc
blobb3c23e6bf8bdbbea8e4df1eecac67df7719daacc
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 = nullptr;
104 CloudPolicyManager* user_cloud_policy_manager = nullptr;
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 = nullptr;
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(
130 #if defined(OS_CHROMEOS)
131 user,
132 #endif
133 schema_registry, user_cloud_policy_manager);
134 } else {
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());
141 #else
142 connector->Init(nullptr, nullptr);
143 #endif
145 connectors_[context] = connector.get();
146 return connector.Pass();
149 void ProfilePolicyConnectorFactory::BrowserContextShutdown(
150 content::BrowserContext* context) {
151 if (Profile::FromBrowserContext(context)->IsOffTheRecord())
152 return;
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) {
171 return false;
174 void ProfilePolicyConnectorFactory::CreateServiceNow(
175 content::BrowserContext* context) {}
177 } // namespace policy