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