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/stringprintf.h"
16 #include "base/strings/sys_string_conversions.h"
17 #include "base/sys_info.h"
18 #include "chrome/browser/policy/configuration_policy_handler_list_factory.h"
19 #include "chrome/common/chrome_paths.h"
20 #include "chrome/common/chrome_version_info.h"
21 #include "components/policy/core/common/async_policy_provider.h"
22 #include "components/policy/core/common/cloud/device_management_service.h"
23 #include "components/policy/core/common/configuration_policy_provider.h"
24 #include "components/policy/core/common/policy_types.h"
25 #include "content/public/browser/browser_thread.h"
26 #include "net/url_request/url_request_context_getter.h"
27 #include "policy/policy_constants.h"
30 #include "components/policy/core/common/policy_loader_win.h"
31 #elif defined(OS_MACOSX)
32 #include <CoreFoundation/CoreFoundation.h>
33 #include "components/policy/core/common/policy_loader_mac.h"
34 #include "components/policy/core/common/preferences_mac.h"
35 #elif defined(OS_POSIX) && !defined(OS_ANDROID)
36 #include "components/policy/core/common/config_dir_policy_loader.h"
37 #elif defined(OS_ANDROID)
38 #include "components/policy/core/common/policy_provider_android.h"
41 #if defined(OS_CHROMEOS)
42 #include "chromeos/system/statistics_provider.h"
45 using content::BrowserThread;
51 // The following constants define delays applied before the initial policy fetch
52 // on startup. (So that displaying Chrome's GUI does not get delayed.)
53 // Delay in milliseconds from startup.
54 const int64 kServiceInitializationStartupDelay = 5000;
56 #if defined(OS_MACOSX)
57 base::FilePath GetManagedPolicyPath() {
58 // This constructs the path to the plist file in which Mac OS X stores the
59 // managed preference for the application. This is undocumented and therefore
60 // fragile, but if it doesn't work out, AsyncPolicyLoader has a task that
61 // polls periodically in order to reload managed preferences later even if we
64 if (!PathService::Get(chrome::DIR_MANAGED_PREFS, &path))
65 return base::FilePath();
67 CFBundleRef bundle(CFBundleGetMainBundle());
69 return base::FilePath();
71 CFStringRef bundle_id = CFBundleGetIdentifier(bundle);
73 return base::FilePath();
75 return path.Append(base::SysCFStringRefToUTF8(bundle_id) + ".plist");
77 #endif // defined(OS_MACOSX)
79 class DeviceManagementServiceConfiguration
80 : public DeviceManagementService::Configuration {
82 DeviceManagementServiceConfiguration() {}
83 virtual ~DeviceManagementServiceConfiguration() {}
85 virtual std::string GetServerUrl() OVERRIDE {
86 return BrowserPolicyConnector::GetDeviceManagementUrl();
89 virtual std::string GetAgentParameter() OVERRIDE {
90 chrome::VersionInfo version_info;
91 return base::StringPrintf("%s %s(%s)",
92 version_info.Name().c_str(),
93 version_info.Version().c_str(),
94 version_info.LastChange().c_str());
97 virtual std::string GetPlatformParameter() OVERRIDE {
98 std::string os_name = base::SysInfo::OperatingSystemName();
99 std::string os_hardware = base::SysInfo::OperatingSystemArchitecture();
101 #if defined(OS_CHROMEOS)
102 chromeos::system::StatisticsProvider* provider =
103 chromeos::system::StatisticsProvider::GetInstance();
106 if (!provider->GetMachineStatistic(chromeos::system::kHardwareClassKey,
108 LOG(ERROR) << "Failed to get machine information";
110 os_name += ",CrOS," + base::SysInfo::GetLsbReleaseBoard();
111 os_hardware += "," + hwclass;
114 std::string os_version("-");
115 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS)
116 int32 os_major_version = 0;
117 int32 os_minor_version = 0;
118 int32 os_bugfix_version = 0;
119 base::SysInfo::OperatingSystemVersionNumbers(&os_major_version,
122 os_version = base::StringPrintf("%d.%d.%d",
128 return base::StringPrintf(
129 "%s|%s|%s", os_name.c_str(), os_hardware.c_str(), os_version.c_str());
135 ChromeBrowserPolicyConnector::ChromeBrowserPolicyConnector()
136 : BrowserPolicyConnector(base::Bind(&BuildHandlerList)) {
137 ConfigurationPolicyProvider* platform_provider = CreatePlatformProvider();
138 if (platform_provider)
139 SetPlatformPolicyProvider(make_scoped_ptr(platform_provider));
142 ChromeBrowserPolicyConnector::~ChromeBrowserPolicyConnector() {}
144 void ChromeBrowserPolicyConnector::Init(
145 PrefService* local_state,
146 scoped_refptr<net::URLRequestContextGetter> request_context) {
147 // Initialization of some of the providers requires the FILE thread; make
148 // sure that threading is ready at this point.
149 DCHECK(BrowserThread::IsThreadInitialized(BrowserThread::FILE));
151 scoped_ptr<DeviceManagementService::Configuration> configuration(
152 new DeviceManagementServiceConfiguration);
153 scoped_ptr<DeviceManagementService> device_management_service(
154 new DeviceManagementService(configuration.Pass()));
155 device_management_service->ScheduleInitialization(
156 kServiceInitializationStartupDelay);
158 BrowserPolicyConnector::Init(
159 local_state, request_context, device_management_service.Pass());
162 ConfigurationPolicyProvider*
163 ChromeBrowserPolicyConnector::CreatePlatformProvider() {
165 scoped_ptr<AsyncPolicyLoader> loader(PolicyLoaderWin::Create(
166 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
167 kRegistryChromePolicyKey));
168 return new AsyncPolicyProvider(GetSchemaRegistry(), loader.Pass());
169 #elif defined(OS_MACOSX)
170 scoped_ptr<AsyncPolicyLoader> loader(new PolicyLoaderMac(
171 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
172 GetManagedPolicyPath(),
173 new MacPreferences()));
174 return new AsyncPolicyProvider(GetSchemaRegistry(), loader.Pass());
175 #elif defined(OS_POSIX) && !defined(OS_ANDROID)
176 base::FilePath config_dir_path;
177 if (PathService::Get(chrome::DIR_POLICY_FILES, &config_dir_path)) {
178 scoped_ptr<AsyncPolicyLoader> loader(new ConfigDirPolicyLoader(
179 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
181 POLICY_SCOPE_MACHINE));
182 return new AsyncPolicyProvider(GetSchemaRegistry(), loader.Pass());
186 #elif defined(OS_ANDROID)
187 return new PolicyProviderAndroid();
193 } // namespace policy