1 // Copyright 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/cloud/policy_header_service_factory.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "components/keyed_service/content/browser_context_dependency_manager.h"
11 #include "components/keyed_service/core/keyed_service.h"
12 #include "components/policy/core/browser/browser_policy_connector.h"
13 #include "components/policy/core/common/cloud/cloud_policy_store.h"
14 #include "components/policy/core/common/cloud/device_management_service.h"
15 #include "components/policy/core/common/cloud/policy_header_service.h"
17 #if defined(OS_CHROMEOS)
18 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
19 #include "chrome/browser/chromeos/policy/device_cloud_policy_manager_chromeos.h"
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"
23 #include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h"
24 #include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
31 class PolicyHeaderServiceWrapper
: public KeyedService
{
33 explicit PolicyHeaderServiceWrapper(scoped_ptr
<PolicyHeaderService
> service
)
34 : policy_header_service_(service
.Pass()) {}
36 PolicyHeaderService
* policy_header_service() const {
37 return policy_header_service_
.get();
40 void Shutdown() override
{
41 // Shutdown our core object so it can unregister any observers before the
42 // services we depend on are shutdown.
43 policy_header_service_
.reset();
47 scoped_ptr
<PolicyHeaderService
> policy_header_service_
;
52 PolicyHeaderServiceFactory::PolicyHeaderServiceFactory()
53 : BrowserContextKeyedServiceFactory(
54 "PolicyHeaderServiceFactory",
55 BrowserContextDependencyManager::GetInstance()) {
56 #if defined(OS_CHROMEOS)
57 DependsOn(UserCloudPolicyManagerFactoryChromeOS::GetInstance());
59 DependsOn(UserCloudPolicyManagerFactory::GetInstance());
63 PolicyHeaderServiceFactory::~PolicyHeaderServiceFactory() {
67 PolicyHeaderService
* PolicyHeaderServiceFactory::GetForBrowserContext(
68 content::BrowserContext
* context
) {
69 PolicyHeaderServiceWrapper
* wrapper
=
70 static_cast<PolicyHeaderServiceWrapper
*>(
71 GetInstance()->GetServiceForBrowserContext(context
, true));
73 return wrapper
->policy_header_service();
78 KeyedService
* PolicyHeaderServiceFactory::BuildServiceInstanceFor(
79 content::BrowserContext
* context
) const {
80 #if defined(OS_CHROMEOS)
81 BrowserPolicyConnectorChromeOS
* connector
=
82 g_browser_process
->platform_part()->browser_policy_connector_chromeos();
84 BrowserPolicyConnector
* connector
=
85 g_browser_process
->browser_policy_connector();
88 DeviceManagementService
* device_management_service
=
89 connector
->device_management_service();
90 #if defined(OS_CHROMEOS)
91 CloudPolicyManager
* manager
=
92 UserCloudPolicyManagerFactoryChromeOS::GetForProfile(
93 Profile::FromBrowserContext(context
));
95 CloudPolicyManager
* manager
=
96 UserCloudPolicyManagerFactory::GetForBrowserContext(context
);
100 CloudPolicyStore
* user_store
= manager
->core()->store();
101 CloudPolicyStore
* device_store
= NULL
;
102 #if defined(OS_CHROMEOS)
103 device_store
= connector
->GetDeviceCloudPolicyManager()->core()->store();
106 scoped_ptr
<PolicyHeaderService
> service
= make_scoped_ptr(
107 new PolicyHeaderService(device_management_service
->GetServerUrl(),
108 kPolicyVerificationKeyHash
,
111 return new PolicyHeaderServiceWrapper(service
.Pass());
115 PolicyHeaderServiceFactory
* PolicyHeaderServiceFactory::GetInstance() {
116 return Singleton
<PolicyHeaderServiceFactory
>::get();
119 } // namespace policy