Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / policy / cloud / policy_header_service_factory.cc
blob98d2ffcd9933c5586cd603cefece469aaff32c2f
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/policy/browser_policy_connector.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "components/browser_context_keyed_service/browser_context_dependency_manager.h"
12 #include "components/policy/core/common/cloud/cloud_policy_store.h"
13 #include "components/policy/core/common/cloud/device_management_service.h"
14 #include "components/policy/core/common/cloud/policy_header_service.h"
16 #if defined(OS_CHROMEOS)
17 #include "chrome/browser/chromeos/policy/device_cloud_policy_manager_chromeos.h"
18 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_chromeos.h"
19 #include "chrome/browser/chromeos/policy/user_cloud_policy_manager_factory_chromeos.h"
20 #else
21 #include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h"
22 #include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
23 #endif
25 namespace policy {
27 namespace {
29 class PolicyHeaderServiceWrapper : public BrowserContextKeyedService {
30 public:
31 explicit PolicyHeaderServiceWrapper(scoped_ptr<PolicyHeaderService> service)
32 : policy_header_service_(service.Pass()) {}
34 PolicyHeaderService* policy_header_service() const {
35 return policy_header_service_.get();
38 virtual void Shutdown() OVERRIDE {
39 // Shutdown our core object so it can unregister any observers before the
40 // services we depend on are shutdown.
41 policy_header_service_.reset();
44 private:
45 scoped_ptr<PolicyHeaderService> policy_header_service_;
48 } // namespace
50 PolicyHeaderServiceFactory::PolicyHeaderServiceFactory()
51 : BrowserContextKeyedServiceFactory(
52 "PolicyHeaderServiceFactory",
53 BrowserContextDependencyManager::GetInstance()) {
54 #if defined(OS_CHROMEOS)
55 DependsOn(UserCloudPolicyManagerFactoryChromeOS::GetInstance());
56 #else
57 DependsOn(UserCloudPolicyManagerFactory::GetInstance());
58 #endif
61 PolicyHeaderServiceFactory::~PolicyHeaderServiceFactory() {
64 // static
65 PolicyHeaderService* PolicyHeaderServiceFactory::GetForBrowserContext(
66 content::BrowserContext* context) {
67 PolicyHeaderServiceWrapper* wrapper =
68 static_cast<PolicyHeaderServiceWrapper*>(
69 GetInstance()->GetServiceForBrowserContext(context, true));
70 if (wrapper)
71 return wrapper->policy_header_service();
72 else
73 return NULL;
76 BrowserContextKeyedService*
77 PolicyHeaderServiceFactory::BuildServiceInstanceFor(
78 content::BrowserContext* context) const {
79 BrowserPolicyConnector* connector =
80 g_browser_process->browser_policy_connector();
81 DeviceManagementService* device_management_service =
82 connector->device_management_service();
83 #if defined(OS_CHROMEOS)
84 CloudPolicyManager* manager =
85 UserCloudPolicyManagerFactoryChromeOS::GetForProfile(
86 Profile::FromBrowserContext(context));
87 #else
88 CloudPolicyManager* manager =
89 UserCloudPolicyManagerFactory::GetForBrowserContext(context);
90 #endif
91 if (!manager)
92 return NULL;
93 CloudPolicyStore* user_store = manager->core()->store();
94 CloudPolicyStore* device_store = NULL;
95 #if defined(OS_CHROMEOS)
96 device_store = connector->GetDeviceCloudPolicyManager()->core()->store();
97 #endif
99 scoped_ptr<PolicyHeaderService> service = make_scoped_ptr(
100 new PolicyHeaderService(
101 device_management_service->GetServerUrl(), user_store, device_store));
102 return new PolicyHeaderServiceWrapper(service.Pass());
105 // static
106 PolicyHeaderServiceFactory* PolicyHeaderServiceFactory::GetInstance() {
107 return Singleton<PolicyHeaderServiceFactory>::get();
110 } // namespace policy