21124b961bc6adf424e84f2d5c8c28b35c36692b
[platform/framework/web/crosswalk.git] / src / chrome / browser / policy / chrome_browser_policy_connector.cc
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.
4
5 #include "chrome/browser/policy/chrome_browser_policy_connector.h"
6
7 #include <string>
8
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"
27
28 #if defined(OS_WIN)
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"
36 #endif
37
38 #if defined(OS_CHROMEOS)
39 #include "chromeos/system/statistics_provider.h"
40 #endif
41
42 using content::BrowserThread;
43
44 namespace policy {
45
46 namespace {
47
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;
52
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
59   // missed the change.
60   base::FilePath path;
61   if (!PathService::Get(chrome::DIR_MANAGED_PREFS, &path))
62     return base::FilePath();
63
64   CFBundleRef bundle(CFBundleGetMainBundle());
65   if (!bundle)
66     return base::FilePath();
67
68   CFStringRef bundle_id = CFBundleGetIdentifier(bundle);
69   if (!bundle_id)
70     return base::FilePath();
71
72   return path.Append(base::SysCFStringRefToUTF8(bundle_id) + ".plist");
73 }
74 #endif  // defined(OS_MACOSX)
75
76 class DeviceManagementServiceConfiguration
77     : public DeviceManagementService::Configuration {
78  public:
79   DeviceManagementServiceConfiguration() {}
80   virtual ~DeviceManagementServiceConfiguration() {}
81
82   virtual std::string GetServerUrl() OVERRIDE {
83     return BrowserPolicyConnector::GetDeviceManagementUrl();
84   }
85
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());
92   }
93
94   virtual std::string GetPlatformParameter() OVERRIDE {
95     std::string os_name = base::SysInfo::OperatingSystemName();
96     std::string os_hardware = base::SysInfo::OperatingSystemArchitecture();
97
98 #if defined(OS_CHROMEOS)
99     chromeos::system::StatisticsProvider* provider =
100         chromeos::system::StatisticsProvider::GetInstance();
101
102     std::string hwclass;
103     if (!provider->GetMachineStatistic(chromeos::system::kHardwareClassKey,
104                                        &hwclass)) {
105       LOG(ERROR) << "Failed to get machine information";
106     }
107     os_name += ",CrOS," + base::SysInfo::GetLsbReleaseBoard();
108     os_hardware += "," + hwclass;
109 #endif
110
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,
117                                                  &os_minor_version,
118                                                  &os_bugfix_version);
119     os_version = base::StringPrintf("%d.%d.%d",
120                                     os_major_version,
121                                     os_minor_version,
122                                     os_bugfix_version);
123 #endif
124
125     return base::StringPrintf(
126         "%s|%s|%s", os_name.c_str(), os_hardware.c_str(), os_version.c_str());
127   }
128 };
129
130 }  // namespace
131
132 ChromeBrowserPolicyConnector::ChromeBrowserPolicyConnector()
133     : BrowserPolicyConnector(BuildHandlerList()) {
134   ConfigurationPolicyProvider* platform_provider = CreatePlatformProvider();
135   if (platform_provider)
136     SetPlatformPolicyProvider(make_scoped_ptr(platform_provider));
137 }
138
139 ChromeBrowserPolicyConnector::~ChromeBrowserPolicyConnector() {}
140
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));
147
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);
154
155   BrowserPolicyConnector::Init(
156       local_state, request_context, device_management_service.Pass());
157 }
158
159 ConfigurationPolicyProvider*
160     ChromeBrowserPolicyConnector::CreatePlatformProvider() {
161 #if defined(OS_WIN)
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),
177         config_dir_path,
178         POLICY_SCOPE_MACHINE));
179     return new AsyncPolicyProvider(GetSchemaRegistry(), loader.Pass());
180   } else {
181     return NULL;
182   }
183 #else
184   return NULL;
185 #endif
186 }
187
188 }  // namespace policy