Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / policy / cloud / policy_header_service_factory.cc
blobc2164a7f6225f155348b8eaa35139379daf99e2e
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"
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
27 namespace policy {
29 namespace {
31 class PolicyHeaderServiceWrapper : public KeyedService {
32 public:
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();
46 private:
47 scoped_ptr<PolicyHeaderService> policy_header_service_;
50 } // namespace
52 PolicyHeaderServiceFactory::PolicyHeaderServiceFactory()
53 : BrowserContextKeyedServiceFactory(
54 "PolicyHeaderServiceFactory",
55 BrowserContextDependencyManager::GetInstance()) {
56 #if defined(OS_CHROMEOS)
57 DependsOn(UserCloudPolicyManagerFactoryChromeOS::GetInstance());
58 #else
59 DependsOn(UserCloudPolicyManagerFactory::GetInstance());
60 #endif
63 PolicyHeaderServiceFactory::~PolicyHeaderServiceFactory() {
66 // static
67 PolicyHeaderService* PolicyHeaderServiceFactory::GetForBrowserContext(
68 content::BrowserContext* context) {
69 PolicyHeaderServiceWrapper* wrapper =
70 static_cast<PolicyHeaderServiceWrapper*>(
71 GetInstance()->GetServiceForBrowserContext(context, true));
72 if (wrapper)
73 return wrapper->policy_header_service();
74 else
75 return NULL;
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();
83 #else
84 BrowserPolicyConnector* connector =
85 g_browser_process->browser_policy_connector();
86 #endif
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));
94 #else
95 CloudPolicyManager* manager =
96 UserCloudPolicyManagerFactory::GetForBrowserContext(context);
97 #endif
98 if (!manager)
99 return NULL;
100 CloudPolicyStore* user_store = manager->core()->store();
101 CloudPolicyStore* device_store = NULL;
102 #if defined(OS_CHROMEOS)
103 device_store = connector->GetDeviceCloudPolicyManager()->core()->store();
104 #endif
106 scoped_ptr<PolicyHeaderService> service = make_scoped_ptr(
107 new PolicyHeaderService(device_management_service->GetServerUrl(),
108 kPolicyVerificationKeyHash,
109 user_store,
110 device_store));
111 return new PolicyHeaderServiceWrapper(service.Pass());
114 // static
115 PolicyHeaderServiceFactory* PolicyHeaderServiceFactory::GetInstance() {
116 return Singleton<PolicyHeaderServiceFactory>::get();
119 } // namespace policy