Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / policy / chrome_browser_policy_connector.cc
blob5f19b2a4ad3b59f7b9afacef7905a8f22a51b0ac
1 // Copyright 2014 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/chrome_browser_policy_connector.h"
7 #include <string>
9 #include "base/callback.h"
10 #include "base/command_line.h"
11 #include "base/files/file_path.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/path_service.h"
15 #include "base/strings/sys_string_conversions.h"
16 #include "chrome/browser/policy/configuration_policy_handler_list_factory.h"
17 #include "chrome/browser/policy/device_management_service_configuration.h"
18 #include "chrome/common/chrome_paths.h"
19 #include "components/policy/core/common/async_policy_provider.h"
20 #include "components/policy/core/common/cloud/device_management_service.h"
21 #include "components/policy/core/common/configuration_policy_provider.h"
22 #include "components/policy/core/common/policy_map.h"
23 #include "components/policy/core/common/policy_namespace.h"
24 #include "components/policy/core/common/policy_service.h"
25 #include "components/policy/core/common/policy_types.h"
26 #include "content/public/browser/browser_thread.h"
27 #include "content/public/common/content_switches.h"
28 #include "net/url_request/url_request_context_getter.h"
29 #include "policy/policy_constants.h"
31 #if defined(OS_WIN)
32 #include "components/policy/core/common/policy_loader_win.h"
33 #elif defined(OS_MACOSX)
34 #include <CoreFoundation/CoreFoundation.h>
35 #include "components/policy/core/common/policy_loader_mac.h"
36 #include "components/policy/core/common/preferences_mac.h"
37 #elif defined(OS_POSIX) && !defined(OS_ANDROID)
38 #include "components/policy/core/common/config_dir_policy_loader.h"
39 #elif defined(OS_ANDROID)
40 #include "components/policy/core/browser/android/android_combined_policy_provider.h"
41 #endif
43 using content::BrowserThread;
45 namespace policy {
47 namespace {
49 #if defined(OS_MACOSX)
50 base::FilePath GetManagedPolicyPath() {
51 CFBundleRef bundle(CFBundleGetMainBundle());
52 if (!bundle)
53 return base::FilePath();
55 CFStringRef bundle_id = CFBundleGetIdentifier(bundle);
56 if (!bundle_id)
57 return base::FilePath();
59 return policy::PolicyLoaderMac::GetManagedPolicyPath(bundle_id);
61 #endif // defined(OS_MACOSX)
63 } // namespace
65 ChromeBrowserPolicyConnector::ChromeBrowserPolicyConnector()
66 : BrowserPolicyConnector(base::Bind(&BuildHandlerList)) {
67 ConfigurationPolicyProvider* platform_provider = CreatePlatformProvider();
68 if (platform_provider)
69 SetPlatformPolicyProvider(make_scoped_ptr(platform_provider));
72 ChromeBrowserPolicyConnector::~ChromeBrowserPolicyConnector() {}
74 void ChromeBrowserPolicyConnector::Init(
75 PrefService* local_state,
76 scoped_refptr<net::URLRequestContextGetter> request_context) {
77 // Initialization of some of the providers requires the FILE thread; make
78 // sure that threading is ready at this point.
79 DCHECK(BrowserThread::IsThreadInitialized(BrowserThread::FILE));
81 scoped_ptr<DeviceManagementService::Configuration> configuration(
82 new DeviceManagementServiceConfiguration(
83 BrowserPolicyConnector::GetDeviceManagementUrl()));
84 scoped_ptr<DeviceManagementService> device_management_service(
85 new DeviceManagementService(configuration.Pass()));
86 device_management_service->ScheduleInitialization(
87 kServiceInitializationStartupDelay);
89 InitInternal(local_state, device_management_service.Pass());
92 ConfigurationPolicyProvider*
93 ChromeBrowserPolicyConnector::CreatePlatformProvider() {
94 #if defined(OS_WIN)
95 scoped_ptr<AsyncPolicyLoader> loader(PolicyLoaderWin::Create(
96 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
97 kRegistryChromePolicyKey));
98 return new AsyncPolicyProvider(GetSchemaRegistry(), loader.Pass());
99 #elif defined(OS_MACOSX)
100 scoped_ptr<AsyncPolicyLoader> loader(new PolicyLoaderMac(
101 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
102 GetManagedPolicyPath(),
103 new MacPreferences()));
104 return new AsyncPolicyProvider(GetSchemaRegistry(), loader.Pass());
105 #elif defined(OS_POSIX) && !defined(OS_ANDROID)
106 base::FilePath config_dir_path;
107 if (PathService::Get(chrome::DIR_POLICY_FILES, &config_dir_path)) {
108 scoped_ptr<AsyncPolicyLoader> loader(new ConfigDirPolicyLoader(
109 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
110 config_dir_path,
111 POLICY_SCOPE_MACHINE));
112 return new AsyncPolicyProvider(GetSchemaRegistry(), loader.Pass());
113 } else {
114 return NULL;
116 #elif defined(OS_ANDROID)
117 return new policy::android::AndroidCombinedPolicyProvider(
118 GetSchemaRegistry());
119 #else
120 return NULL;
121 #endif
124 } // namespace policy