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/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/path_service.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/strings/sys_string_conversions.h"
16 #include "base/sys_info.h"
17 #include "chrome/browser/policy/configuration_policy_handler_list_factory.h"
18 #include "chrome/common/chrome_paths.h"
19 #include "chrome/common/chrome_version_info.h"
20 #include "components/policy/core/common/async_policy_provider.h"
21 #include "components/policy/core/common/cloud/device_management_service.h"
22 #include "components/policy/core/common/configuration_policy_provider.h"
23 #include "components/policy/core/common/policy_types.h"
24 #include "content/public/browser/browser_thread.h"
25 #include "net/url_request/url_request_context_getter.h"
26 #include "policy/policy_constants.h"
29 #include "components/policy/core/common/policy_loader_win.h"
30 #elif defined(OS_MACOSX)
31 #include <CoreFoundation/CoreFoundation.h>
32 #include "components/policy/core/common/policy_loader_mac.h"
33 #include "components/policy/core/common/preferences_mac.h"
34 #elif defined(OS_POSIX) && !defined(OS_ANDROID)
35 #include "components/policy/core/common/config_dir_policy_loader.h"
38 #if defined(OS_CHROMEOS)
39 #include "chromeos/system/statistics_provider.h"
42 using content::BrowserThread;
48 // The following constants define delays applied before the initial policy fetch
49 // on startup. (So that displaying Chrome's GUI does not get delayed.)
50 // Delay in milliseconds from startup.
51 const int64 kServiceInitializationStartupDelay = 5000;
53 #if defined(OS_MACOSX)
54 base::FilePath GetManagedPolicyPath() {
55 // This constructs the path to the plist file in which Mac OS X stores the
56 // managed preference for the application. This is undocumented and therefore
57 // fragile, but if it doesn't work out, AsyncPolicyLoader has a task that
58 // polls periodically in order to reload managed preferences later even if we
61 if (!PathService::Get(chrome::DIR_MANAGED_PREFS, &path))
62 return base::FilePath();
64 CFBundleRef bundle(CFBundleGetMainBundle());
66 return base::FilePath();
68 CFStringRef bundle_id = CFBundleGetIdentifier(bundle);
70 return base::FilePath();
72 return path.Append(base::SysCFStringRefToUTF8(bundle_id) + ".plist");
74 #endif // defined(OS_MACOSX)
76 class DeviceManagementServiceConfiguration
77 : public DeviceManagementService::Configuration {
79 DeviceManagementServiceConfiguration() {}
80 virtual ~DeviceManagementServiceConfiguration() {}
82 virtual std::string GetServerUrl() OVERRIDE {
83 return BrowserPolicyConnector::GetDeviceManagementUrl();
86 virtual std::string GetAgentParameter() OVERRIDE {
87 chrome::VersionInfo version_info;
88 return base::StringPrintf("%s %s(%s)",
89 version_info.Name().c_str(),
90 version_info.Version().c_str(),
91 version_info.LastChange().c_str());
94 virtual std::string GetPlatformParameter() OVERRIDE {
95 std::string os_name = base::SysInfo::OperatingSystemName();
96 std::string os_hardware = base::SysInfo::OperatingSystemArchitecture();
98 #if defined(OS_CHROMEOS)
99 chromeos::system::StatisticsProvider* provider =
100 chromeos::system::StatisticsProvider::GetInstance();
103 if (!provider->GetMachineStatistic(chromeos::system::kHardwareClassKey,
105 LOG(ERROR) << "Failed to get machine information";
107 os_name += ",CrOS," + base::SysInfo::GetLsbReleaseBoard();
108 os_hardware += "," + hwclass;
111 std::string os_version("-");
112 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS)
113 int32 os_major_version = 0;
114 int32 os_minor_version = 0;
115 int32 os_bugfix_version = 0;
116 base::SysInfo::OperatingSystemVersionNumbers(&os_major_version,
119 os_version = base::StringPrintf("%d.%d.%d",
125 return base::StringPrintf(
126 "%s|%s|%s", os_name.c_str(), os_hardware.c_str(), os_version.c_str());
132 ChromeBrowserPolicyConnector::ChromeBrowserPolicyConnector()
133 : BrowserPolicyConnector(BuildHandlerList()) {
134 ConfigurationPolicyProvider* platform_provider = CreatePlatformProvider();
135 if (platform_provider)
136 SetPlatformPolicyProvider(make_scoped_ptr(platform_provider));
139 ChromeBrowserPolicyConnector::~ChromeBrowserPolicyConnector() {}
141 void ChromeBrowserPolicyConnector::Init(
142 PrefService* local_state,
143 scoped_refptr<net::URLRequestContextGetter> request_context) {
144 // Initialization of some of the providers requires the FILE thread; make
145 // sure that threading is ready at this point.
146 DCHECK(BrowserThread::IsThreadInitialized(BrowserThread::FILE));
148 scoped_ptr<DeviceManagementService::Configuration> configuration(
149 new DeviceManagementServiceConfiguration);
150 scoped_ptr<DeviceManagementService> device_management_service(
151 new DeviceManagementService(configuration.Pass()));
152 device_management_service->ScheduleInitialization(
153 kServiceInitializationStartupDelay);
155 BrowserPolicyConnector::Init(
156 local_state, request_context, device_management_service.Pass());
159 ConfigurationPolicyProvider*
160 ChromeBrowserPolicyConnector::CreatePlatformProvider() {
162 scoped_ptr<AsyncPolicyLoader> loader(PolicyLoaderWin::Create(
163 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
164 kRegistryChromePolicyKey));
165 return new AsyncPolicyProvider(GetSchemaRegistry(), loader.Pass());
166 #elif defined(OS_MACOSX)
167 scoped_ptr<AsyncPolicyLoader> loader(new PolicyLoaderMac(
168 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
169 GetManagedPolicyPath(),
170 new MacPreferences()));
171 return new AsyncPolicyProvider(GetSchemaRegistry(), loader.Pass());
172 #elif defined(OS_POSIX) && !defined(OS_ANDROID)
173 base::FilePath config_dir_path;
174 if (PathService::Get(chrome::DIR_POLICY_FILES, &config_dir_path)) {
175 scoped_ptr<AsyncPolicyLoader> loader(new ConfigDirPolicyLoader(
176 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
178 POLICY_SCOPE_MACHINE));
179 return new AsyncPolicyProvider(GetSchemaRegistry(), loader.Pass());
188 } // namespace policy