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"
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_types.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "net/url_request/url_request_context_getter.h"
25 #include "policy/policy_constants.h"
28 #include "components/policy/core/common/policy_loader_win.h"
29 #elif defined(OS_MACOSX)
30 #include <CoreFoundation/CoreFoundation.h>
31 #include "components/policy/core/common/policy_loader_mac.h"
32 #include "components/policy/core/common/preferences_mac.h"
33 #elif defined(OS_POSIX) && !defined(OS_ANDROID)
34 #include "components/policy/core/common/config_dir_policy_loader.h"
35 #elif defined(OS_ANDROID)
36 #include "components/policy/core/common/policy_provider_android.h"
39 using content::BrowserThread
;
45 #if defined(OS_MACOSX)
46 base::FilePath
GetManagedPolicyPath() {
47 // This constructs the path to the plist file in which Mac OS X stores the
48 // managed preference for the application. This is undocumented and therefore
49 // fragile, but if it doesn't work out, AsyncPolicyLoader has a task that
50 // polls periodically in order to reload managed preferences later even if we
53 if (!PathService::Get(chrome::DIR_MANAGED_PREFS
, &path
))
54 return base::FilePath();
56 CFBundleRef
bundle(CFBundleGetMainBundle());
58 return base::FilePath();
60 CFStringRef bundle_id
= CFBundleGetIdentifier(bundle
);
62 return base::FilePath();
64 return path
.Append(base::SysCFStringRefToUTF8(bundle_id
) + ".plist");
66 #endif // defined(OS_MACOSX)
70 ChromeBrowserPolicyConnector::ChromeBrowserPolicyConnector()
71 : BrowserPolicyConnector(base::Bind(&BuildHandlerList
)) {
72 ConfigurationPolicyProvider
* platform_provider
= CreatePlatformProvider();
73 if (platform_provider
)
74 SetPlatformPolicyProvider(make_scoped_ptr(platform_provider
));
77 ChromeBrowserPolicyConnector::~ChromeBrowserPolicyConnector() {}
79 void ChromeBrowserPolicyConnector::Init(
80 PrefService
* local_state
,
81 scoped_refptr
<net::URLRequestContextGetter
> request_context
) {
82 // Initialization of some of the providers requires the FILE thread; make
83 // sure that threading is ready at this point.
84 DCHECK(BrowserThread::IsThreadInitialized(BrowserThread::FILE));
86 scoped_ptr
<DeviceManagementService::Configuration
> configuration(
87 new DeviceManagementServiceConfiguration(
88 BrowserPolicyConnector::GetDeviceManagementUrl()));
89 scoped_ptr
<DeviceManagementService
> device_management_service(
90 new DeviceManagementService(configuration
.Pass()));
91 device_management_service
->ScheduleInitialization(
92 kServiceInitializationStartupDelay
);
94 BrowserPolicyConnector::Init(
95 local_state
, request_context
, device_management_service
.Pass());
98 ConfigurationPolicyProvider
*
99 ChromeBrowserPolicyConnector::CreatePlatformProvider() {
101 scoped_ptr
<AsyncPolicyLoader
> loader(PolicyLoaderWin::Create(
102 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
103 kRegistryChromePolicyKey
));
104 return new AsyncPolicyProvider(GetSchemaRegistry(), loader
.Pass());
105 #elif defined(OS_MACOSX)
106 scoped_ptr
<AsyncPolicyLoader
> loader(new PolicyLoaderMac(
107 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
108 GetManagedPolicyPath(),
109 new MacPreferences()));
110 return new AsyncPolicyProvider(GetSchemaRegistry(), loader
.Pass());
111 #elif defined(OS_POSIX) && !defined(OS_ANDROID)
112 base::FilePath config_dir_path
;
113 if (PathService::Get(chrome::DIR_POLICY_FILES
, &config_dir_path
)) {
114 scoped_ptr
<AsyncPolicyLoader
> loader(new ConfigDirPolicyLoader(
115 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
117 POLICY_SCOPE_MACHINE
));
118 return new AsyncPolicyProvider(GetSchemaRegistry(), loader
.Pass());
122 #elif defined(OS_ANDROID)
123 return new PolicyProviderAndroid();
129 } // namespace policy