Upstream version 7.35.139.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_map.h"
25 #include "components/policy/core/common/policy_namespace.h"
26 #include "components/policy/core/common/policy_service.h"
27 #include "components/policy/core/common/policy_types.h"
28 #include "chrome/common/chrome_switches.h"
29 #include "content/public/browser/browser_thread.h"
30 #include "net/url_request/url_request_context_getter.h"
31 #include "policy/policy_constants.h"
32
33 #if defined(OS_WIN)
34 #include "components/policy/core/common/policy_loader_win.h"
35 #elif defined(OS_MACOSX)
36 #include <CoreFoundation/CoreFoundation.h>
37 #include "components/policy/core/common/policy_loader_mac.h"
38 #include "components/policy/core/common/preferences_mac.h"
39 #elif defined(OS_POSIX) && !defined(OS_ANDROID)
40 #include "components/policy/core/common/config_dir_policy_loader.h"
41 #elif defined(OS_ANDROID)
42 #include "components/policy/core/common/policy_provider_android.h"
43 #endif
44
45 #if defined(OS_CHROMEOS)
46 #include "chromeos/system/statistics_provider.h"
47 #endif
48
49 using content::BrowserThread;
50
51 namespace policy {
52
53 namespace {
54
55 // The following constants define delays applied before the initial policy fetch
56 // on startup. (So that displaying Chrome's GUI does not get delayed.)
57 // Delay in milliseconds from startup.
58 const int64 kServiceInitializationStartupDelay = 5000;
59
60 #if defined(OS_MACOSX)
61 base::FilePath GetManagedPolicyPath() {
62   // This constructs the path to the plist file in which Mac OS X stores the
63   // managed preference for the application. This is undocumented and therefore
64   // fragile, but if it doesn't work out, AsyncPolicyLoader has a task that
65   // polls periodically in order to reload managed preferences later even if we
66   // missed the change.
67   base::FilePath path;
68   if (!PathService::Get(chrome::DIR_MANAGED_PREFS, &path))
69     return base::FilePath();
70
71   CFBundleRef bundle(CFBundleGetMainBundle());
72   if (!bundle)
73     return base::FilePath();
74
75   CFStringRef bundle_id = CFBundleGetIdentifier(bundle);
76   if (!bundle_id)
77     return base::FilePath();
78
79   return path.Append(base::SysCFStringRefToUTF8(bundle_id) + ".plist");
80 }
81 #endif  // defined(OS_MACOSX)
82
83 class DeviceManagementServiceConfiguration
84     : public DeviceManagementService::Configuration {
85  public:
86   DeviceManagementServiceConfiguration() {}
87   virtual ~DeviceManagementServiceConfiguration() {}
88
89   virtual std::string GetServerUrl() OVERRIDE {
90     return BrowserPolicyConnector::GetDeviceManagementUrl();
91   }
92
93   virtual std::string GetAgentParameter() OVERRIDE {
94     chrome::VersionInfo version_info;
95     return base::StringPrintf("%s %s(%s)",
96                               version_info.Name().c_str(),
97                               version_info.Version().c_str(),
98                               version_info.LastChange().c_str());
99   }
100
101   virtual std::string GetPlatformParameter() OVERRIDE {
102     std::string os_name = base::SysInfo::OperatingSystemName();
103     std::string os_hardware = base::SysInfo::OperatingSystemArchitecture();
104
105 #if defined(OS_CHROMEOS)
106     chromeos::system::StatisticsProvider* provider =
107         chromeos::system::StatisticsProvider::GetInstance();
108
109     std::string hwclass;
110     if (!provider->GetMachineStatistic(chromeos::system::kHardwareClassKey,
111                                        &hwclass)) {
112       LOG(ERROR) << "Failed to get machine information";
113     }
114     os_name += ",CrOS," + base::SysInfo::GetLsbReleaseBoard();
115     os_hardware += "," + hwclass;
116 #endif
117
118     std::string os_version("-");
119 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS)
120     int32 os_major_version = 0;
121     int32 os_minor_version = 0;
122     int32 os_bugfix_version = 0;
123     base::SysInfo::OperatingSystemVersionNumbers(&os_major_version,
124                                                  &os_minor_version,
125                                                  &os_bugfix_version);
126     os_version = base::StringPrintf("%d.%d.%d",
127                                     os_major_version,
128                                     os_minor_version,
129                                     os_bugfix_version);
130 #endif
131
132     return base::StringPrintf(
133         "%s|%s|%s", os_name.c_str(), os_hardware.c_str(), os_version.c_str());
134   }
135 };
136
137 }  // namespace
138
139 ChromeBrowserPolicyConnector::ChromeBrowserPolicyConnector()
140     : BrowserPolicyConnector(base::Bind(&BuildHandlerList)) {
141   ConfigurationPolicyProvider* platform_provider = CreatePlatformProvider();
142   if (platform_provider)
143     SetPlatformPolicyProvider(make_scoped_ptr(platform_provider));
144 }
145
146 ChromeBrowserPolicyConnector::~ChromeBrowserPolicyConnector() {}
147
148 void ChromeBrowserPolicyConnector::Init(
149     PrefService* local_state,
150     scoped_refptr<net::URLRequestContextGetter> request_context) {
151   // Initialization of some of the providers requires the FILE thread; make
152   // sure that threading is ready at this point.
153   DCHECK(BrowserThread::IsThreadInitialized(BrowserThread::FILE));
154
155   scoped_ptr<DeviceManagementService::Configuration> configuration(
156       new DeviceManagementServiceConfiguration);
157   scoped_ptr<DeviceManagementService> device_management_service(
158       new DeviceManagementService(configuration.Pass()));
159   device_management_service->ScheduleInitialization(
160       kServiceInitializationStartupDelay);
161
162   BrowserPolicyConnector::Init(
163       local_state, request_context, device_management_service.Pass());
164
165   AppendExtraFlagPerPolicy();
166 }
167
168 ConfigurationPolicyProvider*
169     ChromeBrowserPolicyConnector::CreatePlatformProvider() {
170 #if defined(OS_WIN)
171   scoped_ptr<AsyncPolicyLoader> loader(PolicyLoaderWin::Create(
172       BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
173       kRegistryChromePolicyKey));
174   return new AsyncPolicyProvider(GetSchemaRegistry(), loader.Pass());
175 #elif defined(OS_MACOSX)
176   scoped_ptr<AsyncPolicyLoader> loader(new PolicyLoaderMac(
177       BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
178       GetManagedPolicyPath(),
179       new MacPreferences()));
180   return new AsyncPolicyProvider(GetSchemaRegistry(), loader.Pass());
181 #elif defined(OS_POSIX) && !defined(OS_ANDROID)
182   base::FilePath config_dir_path;
183   if (PathService::Get(chrome::DIR_POLICY_FILES, &config_dir_path)) {
184     scoped_ptr<AsyncPolicyLoader> loader(new ConfigDirPolicyLoader(
185         BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE),
186         config_dir_path,
187         POLICY_SCOPE_MACHINE));
188     return new AsyncPolicyProvider(GetSchemaRegistry(), loader.Pass());
189   } else {
190     return NULL;
191   }
192 #elif defined(OS_ANDROID)
193   return new PolicyProviderAndroid();
194 #else
195   return NULL;
196 #endif
197 }
198
199 void ChromeBrowserPolicyConnector::AppendExtraFlagPerPolicy() {
200   PolicyService* policy_service = GetPolicyService();
201   PolicyNamespace chrome_ns = PolicyNamespace(POLICY_DOMAIN_CHROME, "");
202   const PolicyMap& chrome_policy = policy_service->GetPolicies(chrome_ns);
203   const base::Value* policy_value =
204       chrome_policy.GetValue(key::kEnableWebBasedSignin);
205   bool enabled = false;
206   CommandLine* command_line = CommandLine::ForCurrentProcess();
207   if (policy_value && policy_value->GetAsBoolean(&enabled) && enabled &&
208       !command_line->HasSwitch(switches::kEnableWebBasedSignin)) {
209     command_line->AppendSwitch(switches::kEnableWebBasedSignin);
210   }
211 }
212
213 }  // namespace policy