1 // Copyright (c) 2012 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/cloud/user_cloud_policy_manager_factory.h"
7 #include "base/files/file_path.h"
8 #include "base/logging.h"
9 #include "base/sequenced_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "chrome/browser/policy/schema_registry_service.h"
12 #include "chrome/browser/policy/schema_registry_service_factory.h"
13 #include "components/keyed_service/content/browser_context_dependency_manager.h"
14 #include "components/keyed_service/core/keyed_service.h"
15 #include "components/policy/core/common/cloud/cloud_external_data_manager.h"
16 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
17 #include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
18 #include "components/policy/core/common/cloud/user_cloud_policy_store.h"
19 #include "content/public/browser/browser_context.h"
25 // Directory inside the profile directory where policy-related resources are
27 const base::FilePath::CharType kPolicy
[] = FILE_PATH_LITERAL("Policy");
29 // Directory under kPolicy, in the user's profile dir, where policy for
30 // components is cached.
31 const base::FilePath::CharType kComponentsDir
[] =
32 FILE_PATH_LITERAL("Components");
36 // A KeyedService that wraps a UserCloudPolicyManager.
37 class UserCloudPolicyManagerFactory::ManagerWrapper
: public KeyedService
{
39 explicit ManagerWrapper(UserCloudPolicyManager
* manager
)
43 ~ManagerWrapper() override
{}
45 void Shutdown() override
{ manager_
->Shutdown(); }
47 UserCloudPolicyManager
* manager() { return manager_
; }
50 UserCloudPolicyManager
* manager_
;
52 DISALLOW_COPY_AND_ASSIGN(ManagerWrapper
);
56 UserCloudPolicyManagerFactory
* UserCloudPolicyManagerFactory::GetInstance() {
57 return Singleton
<UserCloudPolicyManagerFactory
>::get();
61 UserCloudPolicyManager
* UserCloudPolicyManagerFactory::GetForBrowserContext(
62 content::BrowserContext
* context
) {
63 return GetInstance()->GetManagerForBrowserContext(context
);
67 scoped_ptr
<UserCloudPolicyManager
>
68 UserCloudPolicyManagerFactory::CreateForOriginalBrowserContext(
69 content::BrowserContext
* context
,
70 bool force_immediate_load
,
71 const scoped_refptr
<base::SequencedTaskRunner
>& background_task_runner
,
72 const scoped_refptr
<base::SequencedTaskRunner
>& file_task_runner
,
73 const scoped_refptr
<base::SequencedTaskRunner
>& io_task_runner
) {
74 UserCloudPolicyManagerFactory
* factory
= GetInstance();
75 // If there's a testing factory set, don't bother creating a new one.
76 if (factory
->testing_factory_
!= NULL
)
77 return scoped_ptr
<UserCloudPolicyManager
>();
78 return factory
->CreateManagerForOriginalBrowserContext(
81 background_task_runner
,
87 UserCloudPolicyManager
*
88 UserCloudPolicyManagerFactory::RegisterForOffTheRecordBrowserContext(
89 content::BrowserContext
* original_context
,
90 content::BrowserContext
* off_the_record_context
) {
91 return GetInstance()->RegisterManagerForOffTheRecordBrowserContext(
92 original_context
, off_the_record_context
);
95 void UserCloudPolicyManagerFactory::RegisterTestingFactory(
96 TestingFactoryFunction factory
) {
97 // Can't set a testing factory when a testing factory has already been
98 // created, or after UCPMs have already been built.
99 DCHECK(!testing_factory_
);
101 DCHECK(manager_wrappers_
.empty());
102 testing_factory_
= factory
;
105 void UserCloudPolicyManagerFactory::ClearTestingFactory() {
106 testing_factory_
= NULL
;
109 UserCloudPolicyManagerFactory::UserCloudPolicyManagerFactory()
110 : BrowserContextKeyedBaseFactory(
111 "UserCloudPolicyManager",
112 BrowserContextDependencyManager::GetInstance()),
113 testing_factory_(NULL
) {
114 DependsOn(SchemaRegistryServiceFactory::GetInstance());
117 UserCloudPolicyManagerFactory::~UserCloudPolicyManagerFactory() {
118 DCHECK(manager_wrappers_
.empty());
121 UserCloudPolicyManager
*
122 UserCloudPolicyManagerFactory::GetManagerForBrowserContext(
123 content::BrowserContext
* context
) {
124 // In case |context| is an incognito Profile/Context, |manager_wrappers_|
125 // will have a matching entry pointing to the manager of the original context.
126 ManagerWrapperMap::const_iterator it
= manager_wrappers_
.find(context
);
127 return it
!= manager_wrappers_
.end() ? it
->second
->manager() : NULL
;
130 scoped_ptr
<UserCloudPolicyManager
>
131 UserCloudPolicyManagerFactory::CreateManagerForOriginalBrowserContext(
132 content::BrowserContext
* context
,
133 bool force_immediate_load
,
134 const scoped_refptr
<base::SequencedTaskRunner
>& background_task_runner
,
135 const scoped_refptr
<base::SequencedTaskRunner
>& file_task_runner
,
136 const scoped_refptr
<base::SequencedTaskRunner
>& io_task_runner
) {
137 DCHECK(!context
->IsOffTheRecord());
139 // This should never be called if we're using a testing factory.
140 // Instead, instances are instantiated via CreateServiceNow().
141 DCHECK(!testing_factory_
);
143 scoped_ptr
<UserCloudPolicyStore
> store(
144 UserCloudPolicyStore::Create(context
->GetPath(),
145 GetPolicyVerificationKey(),
146 background_task_runner
));
147 if (force_immediate_load
)
148 store
->LoadImmediately();
150 const base::FilePath component_policy_cache_dir
=
151 context
->GetPath().Append(kPolicy
).Append(kComponentsDir
);
153 scoped_ptr
<UserCloudPolicyManager
> manager
;
154 manager
.reset(new UserCloudPolicyManager(
156 component_policy_cache_dir
,
157 scoped_ptr
<CloudExternalDataManager
>(),
158 base::ThreadTaskRunnerHandle::Get(),
162 SchemaRegistryServiceFactory::GetForContext(context
)->registry());
163 manager_wrappers_
[context
] = new ManagerWrapper(manager
.get());
164 return manager
.Pass();
167 UserCloudPolicyManager
*
168 UserCloudPolicyManagerFactory::RegisterManagerForOffTheRecordBrowserContext(
169 content::BrowserContext
* original_context
,
170 content::BrowserContext
* off_the_record_context
) {
171 // Register the UserCloudPolicyManager of the original context for the
172 // respective incognito context. See also GetManagerForBrowserContext.
173 UserCloudPolicyManager
* manager
=
174 GetManagerForBrowserContext(original_context
);
175 manager_wrappers_
[off_the_record_context
] = new ManagerWrapper(manager
);
179 void UserCloudPolicyManagerFactory::BrowserContextShutdown(
180 content::BrowserContext
* context
) {
181 if (context
->IsOffTheRecord())
183 ManagerWrapperMap::iterator it
= manager_wrappers_
.find(context
);
184 // E.g. for a TestingProfile there might not be a manager created.
185 if (it
!= manager_wrappers_
.end())
186 it
->second
->Shutdown();
189 void UserCloudPolicyManagerFactory::BrowserContextDestroyed(
190 content::BrowserContext
* context
) {
191 ManagerWrapperMap::iterator it
= manager_wrappers_
.find(context
);
192 if (it
!= manager_wrappers_
.end()) {
193 // The manager is not owned by the factory, so it's not deleted here.
195 manager_wrappers_
.erase(it
);
199 void UserCloudPolicyManagerFactory::SetEmptyTestingFactory(
200 content::BrowserContext
* context
) {}
202 bool UserCloudPolicyManagerFactory::HasTestingFactory(
203 content::BrowserContext
* context
) {
204 return testing_factory_
!= NULL
;
207 // If there's a TestingFactory set, then create a service during BrowserContext
209 bool UserCloudPolicyManagerFactory::ServiceIsCreatedWithBrowserContext() const {
210 return testing_factory_
!= NULL
;
213 void UserCloudPolicyManagerFactory::CreateServiceNow(
214 content::BrowserContext
* context
) {
215 DCHECK(testing_factory_
);
216 manager_wrappers_
[context
] = new ManagerWrapper(testing_factory_(context
));
219 } // namespace policy