Upstream version 5.34.104.0
[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/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"
28
29 #if defined(OS_WIN)
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"
39 #endif
40
41 #if defined(OS_CHROMEOS)
42 #include "chromeos/system/statistics_provider.h"
43 #endif
44
45 using content::BrowserThread;
46
47 namespace policy {
48
49 namespace {
50
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;
55
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
62   // missed the change.
63   base::FilePath path;
64   if (!PathService::Get(chrome::DIR_MANAGED_PREFS, &path))
65     return base::FilePath();
66
67   CFBundleRef bundle(CFBundleGetMainBundle());
68   if (!bundle)
69     return base::FilePath();
70
71   CFStringRef bundle_id = CFBundleGetIdentifier(bundle);
72   if (!bundle_id)
73     return base::FilePath();
74
75   return path.Append(base::SysCFStringRefToUTF8(bundle_id) + ".plist");
76 }
77 #endif  // defined(OS_MACOSX)
78
79 class DeviceManagementServiceConfiguration
80     : public DeviceManagementService::Configuration {
81  public:
82   DeviceManagementServiceConfiguration() {}
83   virtual ~DeviceManagementServiceConfiguration() {}
84
85   virtual std::string GetServerUrl() OVERRIDE {
86     return BrowserPolicyConnector::GetDeviceManagementUrl();
87   }
88
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());
95   }
96
97   virtual std::string GetPlatformParameter() OVERRIDE {
98     std::string os_name = base::SysInfo::OperatingSystemName();
99     std::string os_hardware = base::SysInfo::OperatingSystemArchitecture();
100
101 #if defined(OS_CHROMEOS)
102     chromeos::system::StatisticsProvider* provider =
103         chromeos::system::StatisticsProvider::GetInstance();
104
105     std::string hwclass;
106     if (!provider->GetMachineStatistic(chromeos::system::kHardwareClassKey,
107                                        &hwclass)) {
108       LOG(ERROR) << "Failed to get machine information";
109     }
110     os_name += ",CrOS," + base::SysInfo::GetLsbReleaseBoard();
111     os_hardware += "," + hwclass;
112 #endif
113
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,
120                                                  &os_minor_version,
121                                                  &os_bugfix_version);
122     os_version = base::StringPrintf("%d.%d.%d",
123                                     os_major_version,
124                                     os_minor_version,
125                                     os_bugfix_version);
126 #endif
127
128     return base::StringPrintf(
129         "%s|%s|%s", os_name.c_str(), os_hardware.c_str(), os_version.c_str());
130   }
131 };
132
133 }  // namespace
134
135 ChromeBrowserPolicyConnector::ChromeBrowserPolicyConnector()
136     : BrowserPolicyConnector(base::Bind(&BuildHandlerList)) {
137   ConfigurationPolicyProvider* platform_provider = CreatePlatformProvider();
138   if (platform_provider)
139     SetPlatformPolicyProvider(make_scoped_ptr(platform_provider));
140 }
141
142 ChromeBrowserPolicyConnector::~ChromeBrowserPolicyConnector() {}
143
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));
150
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);
157
158   BrowserPolicyConnector::Init(
159       local_state, request_context, device_management_service.Pass());
160 }
161
162 ConfigurationPolicyProvider*
163     ChromeBrowserPolicyConnector::CreatePlatformProvider() {
164 #if defined(OS_WIN)
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),
180         config_dir_path,
181         POLICY_SCOPE_MACHINE));
182     return new AsyncPolicyProvider(GetSchemaRegistry(), loader.Pass());
183   } else {
184     return NULL;
185   }
186 #elif defined(OS_ANDROID)
187   return new PolicyProviderAndroid();
188 #else
189   return NULL;
190 #endif
191 }
192
193 }  // namespace policy