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 "components/policy/core/browser/browser_policy_connector_ios.h"
6
7#include "base/sequenced_task_runner.h"
8#include "base/strings/stringprintf.h"
9#include "base/sys_info.h"
10#include "components/policy/core/common/async_policy_provider.h"
11#include "components/policy/core/common/cloud/device_management_service.h"
12#include "components/policy/core/common/policy_loader_ios.h"
13#include "net/url_request/url_request_context_getter.h"
14
15namespace policy {
16
17namespace {
18
19class DeviceManagementServiceConfiguration
20    : public DeviceManagementService::Configuration {
21 public:
22  explicit DeviceManagementServiceConfiguration(const std::string& user_agent)
23      : user_agent_(user_agent) {}
24
25  virtual ~DeviceManagementServiceConfiguration() {}
26
27  virtual std::string GetServerUrl() OVERRIDE {
28    return BrowserPolicyConnector::GetDeviceManagementUrl();
29  }
30
31  virtual std::string GetAgentParameter() OVERRIDE {
32    return user_agent_;
33  }
34
35  virtual std::string GetPlatformParameter() OVERRIDE {
36    std::string os_name = base::SysInfo::OperatingSystemName();
37    std::string os_hardware = base::SysInfo::OperatingSystemArchitecture();
38    std::string os_version("-");
39    int32 os_major_version = 0;
40    int32 os_minor_version = 0;
41    int32 os_bugfix_version = 0;
42    base::SysInfo::OperatingSystemVersionNumbers(&os_major_version,
43                                                 &os_minor_version,
44                                                 &os_bugfix_version);
45    os_version = base::StringPrintf("%d.%d.%d",
46                                    os_major_version,
47                                    os_minor_version,
48                                    os_bugfix_version);
49    return base::StringPrintf(
50        "%s|%s|%s", os_name.c_str(), os_hardware.c_str(), os_version.c_str());
51  }
52
53 private:
54  std::string user_agent_;
55
56  DISALLOW_COPY_AND_ASSIGN(DeviceManagementServiceConfiguration);
57};
58
59}  // namespace
60
61BrowserPolicyConnectorIOS::BrowserPolicyConnectorIOS(
62    const HandlerListFactory& handler_list_factory,
63    const std::string& user_agent,
64    scoped_refptr<base::SequencedTaskRunner> background_task_runner)
65    : BrowserPolicyConnector(handler_list_factory),
66      user_agent_(user_agent) {
67  scoped_ptr<AsyncPolicyLoader> loader(
68      new PolicyLoaderIOS(background_task_runner));
69  scoped_ptr<ConfigurationPolicyProvider> provider(
70      new AsyncPolicyProvider(GetSchemaRegistry(), loader.Pass()));
71  SetPlatformPolicyProvider(provider.Pass());
72}
73
74BrowserPolicyConnectorIOS::~BrowserPolicyConnectorIOS() {}
75
76void BrowserPolicyConnectorIOS::Init(
77    PrefService* local_state,
78    scoped_refptr<net::URLRequestContextGetter> request_context) {
79  scoped_ptr<DeviceManagementService::Configuration> configuration(
80      new DeviceManagementServiceConfiguration(user_agent_));
81  scoped_ptr<DeviceManagementService> device_management_service(
82      new DeviceManagementService(configuration.Pass()));
83
84  // Delay initialization of the cloud policy requests by 5 seconds.
85  const int64 kServiceInitializationStartupDelay = 5000;
86  device_management_service->ScheduleInitialization(
87      kServiceInitializationStartupDelay);
88
89  BrowserPolicyConnector::Init(
90      local_state, request_context, device_management_service.Pass());
91}
92
93}  // namespace policy
94