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.
4
5#include "chrome/browser/policy/profile_policy_connector_factory.h"
6
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 "components/keyed_service/content/browser_context_dependency_manager.h"
12#include "components/policy/core/common/policy_service.h"
13
14#if defined(ENABLE_CONFIGURATION_POLICY)
15#include "chrome/browser/policy/schema_registry_service.h"
16#include "chrome/browser/policy/schema_registry_service_factory.h"
17#include "components/policy/core/common/policy_service_impl.h"
18#if defined(OS_CHROMEOS)
19#include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h"
20#include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chromeos.h"
21#include "chrome/browser/chromeos/profiles/profile_helper.h"
22#include "components/user_manager/user.h"
23#else
24#include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h"
25#include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
26#endif
27#endif
28
29namespace policy {
30
31// static
32ProfilePolicyConnectorFactory* ProfilePolicyConnectorFactory::GetInstance() {
33  return Singleton<ProfilePolicyConnectorFactory>::get();
34}
35
36// static
37ProfilePolicyConnector* ProfilePolicyConnectorFactory::GetForProfile(
38    Profile* profile) {
39  return GetInstance()->GetForProfileInternal(profile);
40}
41
42// static
43scoped_ptr<ProfilePolicyConnector>
44ProfilePolicyConnectorFactory::CreateForProfile(Profile* profile,
45                                                bool force_immediate_load) {
46  return GetInstance()->CreateForProfileInternal(profile, force_immediate_load);
47}
48
49void ProfilePolicyConnectorFactory::SetServiceForTesting(
50    Profile* profile,
51    ProfilePolicyConnector* connector) {
52  ProfilePolicyConnector*& map_entry = connectors_[profile];
53  CHECK(!map_entry);
54  map_entry = connector;
55}
56
57#if defined(ENABLE_CONFIGURATION_POLICY)
58void ProfilePolicyConnectorFactory::PushProviderForTesting(
59    ConfigurationPolicyProvider* provider) {
60  test_providers_.push_back(provider);
61}
62#endif
63
64ProfilePolicyConnectorFactory::ProfilePolicyConnectorFactory()
65    : BrowserContextKeyedBaseFactory(
66        "ProfilePolicyConnector",
67        BrowserContextDependencyManager::GetInstance()) {
68#if defined(ENABLE_CONFIGURATION_POLICY)
69  DependsOn(SchemaRegistryServiceFactory::GetInstance());
70#if defined(OS_CHROMEOS)
71  DependsOn(UserCloudPolicyManagerFactoryChromeOS::GetInstance());
72#else
73  DependsOn(UserCloudPolicyManagerFactory::GetInstance());
74#endif
75#endif
76}
77
78ProfilePolicyConnectorFactory::~ProfilePolicyConnectorFactory() {
79  DCHECK(connectors_.empty());
80}
81
82ProfilePolicyConnector*
83    ProfilePolicyConnectorFactory::GetForProfileInternal(Profile* profile) {
84  // Get the connector for the original Profile, so that the incognito Profile
85  // gets managed settings from the same PolicyService.
86  ConnectorMap::const_iterator it =
87      connectors_.find(profile->GetOriginalProfile());
88  CHECK(it != connectors_.end());
89  return it->second;
90}
91
92scoped_ptr<ProfilePolicyConnector>
93ProfilePolicyConnectorFactory::CreateForProfileInternal(
94    Profile* profile,
95    bool force_immediate_load) {
96  DCHECK(connectors_.find(profile) == connectors_.end());
97
98  SchemaRegistry* schema_registry = NULL;
99  CloudPolicyManager* user_cloud_policy_manager = NULL;
100
101#if defined(ENABLE_CONFIGURATION_POLICY)
102  schema_registry =
103      SchemaRegistryServiceFactory::GetForContext(profile)->registry();
104
105#if defined(OS_CHROMEOS)
106  user_manager::User* user = NULL;
107  if (!chromeos::ProfileHelper::IsSigninProfile(profile)) {
108    user = chromeos::ProfileHelper::Get()->GetUserByProfile(profile);
109    CHECK(user);
110  }
111  user_cloud_policy_manager =
112      UserCloudPolicyManagerFactoryChromeOS::GetForProfile(profile);
113#else
114  user_cloud_policy_manager =
115      UserCloudPolicyManagerFactory::GetForBrowserContext(profile);
116#endif  // defined(OS_CHROMEOS)
117#endif  // defined(ENABLE_CONFIGURATION_POLICY)
118
119  ProfilePolicyConnector* connector = new ProfilePolicyConnector();
120
121#if defined(ENABLE_CONFIGURATION_POLICY)
122  if (test_providers_.empty()) {
123    connector->Init(force_immediate_load,
124#if defined(OS_CHROMEOS)
125                    user,
126#endif
127                    schema_registry,
128                    user_cloud_policy_manager);
129  } else {
130    PolicyServiceImpl::Providers providers;
131    providers.push_back(test_providers_.front());
132    test_providers_.pop_front();
133    scoped_ptr<PolicyService> service(new PolicyServiceImpl(providers));
134    connector->InitForTesting(service.Pass());
135  }
136#else
137  connector->Init(false, NULL, NULL);
138#endif
139
140  connectors_[profile] = connector;
141  return make_scoped_ptr(connector);
142}
143
144void ProfilePolicyConnectorFactory::BrowserContextShutdown(
145    content::BrowserContext* context) {
146  Profile* profile = static_cast<Profile*>(context);
147  if (profile->IsOffTheRecord())
148    return;
149  ConnectorMap::iterator it = connectors_.find(profile);
150  if (it != connectors_.end())
151    it->second->Shutdown();
152}
153
154void ProfilePolicyConnectorFactory::BrowserContextDestroyed(
155    content::BrowserContext* context) {
156  ConnectorMap::iterator it = connectors_.find(static_cast<Profile*>(context));
157  if (it != connectors_.end())
158    connectors_.erase(it);
159  BrowserContextKeyedBaseFactory::BrowserContextDestroyed(context);
160}
161
162void ProfilePolicyConnectorFactory::SetEmptyTestingFactory(
163    content::BrowserContext* context) {}
164
165bool ProfilePolicyConnectorFactory::HasTestingFactory(
166    content::BrowserContext* context) {
167  return false;
168}
169
170void ProfilePolicyConnectorFactory::CreateServiceNow(
171    content::BrowserContext* context) {}
172
173}  // namespace policy
174