Upstream version 7.35.139.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / policy / device_cloud_policy_manager_chromeos.h
1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_CLOUD_POLICY_MANAGER_CHROMEOS_H_
6 #define CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_CLOUD_POLICY_MANAGER_CHROMEOS_H_
7
8 #include <bitset>
9 #include <string>
10 #include <vector>
11
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/compiler_specific.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "chrome/browser/chromeos/policy/enrollment_status_chromeos.h"
18 #include "components/policy/core/common/cloud/cloud_policy_client.h"
19 #include "components/policy/core/common/cloud/cloud_policy_manager.h"
20 #include "components/policy/core/common/cloud/cloud_policy_store.h"
21
22 namespace base {
23 class SequencedTaskRunner;
24 }
25
26 namespace chromeos {
27 namespace attestation {
28 class AttestationPolicyObserver;
29 }
30 }
31
32 class PrefRegistrySimple;
33 class PrefService;
34
35 namespace policy {
36
37 class DeviceCloudPolicyStoreChromeOS;
38 class DeviceManagementService;
39 class EnrollmentHandlerChromeOS;
40 class EnterpriseInstallAttributes;
41
42 // CloudPolicyManager specialization for device policy on Chrome OS. The most
43 // significant addition is support for device enrollment.
44 class DeviceCloudPolicyManagerChromeOS : public CloudPolicyManager {
45  public:
46   typedef std::bitset<32> AllowedDeviceModes;
47   typedef base::Callback<void(EnrollmentStatus)> EnrollmentCallback;
48
49   // The power of two determining the size of the time quanta for device state
50   // keys, i.e. the quanta will be of size 2^kDeviceStateKeyTimeQuantumPower
51   // seconds. 2^23 seconds corresponds to 97.09 days.
52   static const int kDeviceStateKeyTimeQuantumPower = 23;
53
54   // The number of future time quanta to generate device state identifiers for.
55   // This determines the interval after which a device will no longer receive
56   // server-backed state information and thus corresponds to the delay until a
57   // device becomes anonymous to the server again.
58   //
59   // The goal here is to guarantee state key validity for 1 year into the
60   // future. 4 quanta are needed to cover a year, but the current quantum is
61   // clipped short in the general case. Hence, one buffer quantum is needed to
62   // make up for the clipping, yielding a total of 5 quanta.
63   static const int kDeviceStateKeyFutureQuanta = 5;
64
65   // |task_runner| is the runner for policy refresh tasks.
66   // |background_task_runner| is used to execute long-running background tasks
67   // that may involve file I/O.
68   DeviceCloudPolicyManagerChromeOS(
69       scoped_ptr<DeviceCloudPolicyStoreChromeOS> store,
70       const scoped_refptr<base::SequencedTaskRunner>& task_runner,
71       const scoped_refptr<base::SequencedTaskRunner>& background_task_runner,
72       EnterpriseInstallAttributes* install_attributes);
73   virtual ~DeviceCloudPolicyManagerChromeOS();
74
75   // Establishes the connection to the cloud, updating policy as necessary.
76   void Connect(
77       PrefService* local_state,
78       DeviceManagementService* device_management_service,
79       scoped_ptr<CloudPolicyClient::StatusProvider> device_status_provider);
80
81   // Starts enrollment or re-enrollment. Once the enrollment process completes,
82   // |callback| is invoked and gets passed the status of the operation.
83   // |allowed_modes| specifies acceptable DEVICE_MODE_* constants for
84   // enrollment.
85   void StartEnrollment(const std::string& auth_token,
86                        bool is_auto_enrollment,
87                        const AllowedDeviceModes& allowed_modes,
88                        const EnrollmentCallback& callback);
89
90   // Cancels a pending enrollment operation, if any.
91   void CancelEnrollment();
92
93   // Gets/Sets the device requisition.
94   std::string GetDeviceRequisition() const;
95   void SetDeviceRequisition(const std::string& requisition);
96
97   // Checks whether enterprise enrollment should be a regular step during OOBE.
98   bool ShouldAutoStartEnrollment() const;
99
100   // Checks whether the user can cancel enrollment.
101   bool CanExitEnrollment() const;
102
103   // Gets the domain this device is supposed to be enrolled to.
104   std::string GetForcedEnrollmentDomain() const;
105
106   // CloudPolicyManager:
107   virtual void Shutdown() OVERRIDE;
108
109   // CloudPolicyStore::Observer:
110   virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE;
111
112   // Pref registration helper.
113   static void RegisterPrefs(PrefRegistrySimple* registry);
114
115   // Returns the device serial number, or an empty string if not available.
116   static std::string GetMachineID();
117
118   // Returns the machine model, or an empty string if not available.
119   static std::string GetMachineModel();
120
121   // Gets the device state keys for |timestamp|. These will cover a time frame
122   // including |timestamp| and extending into the future as configured by the
123   // constants declared above.
124   static bool GetDeviceStateKeys(const base::Time& timestamp,
125                                  std::vector<std::string>* state_keys);
126
127   // Returns the currently valid device state key.
128   static std::string GetCurrentDeviceStateKey();
129
130   // Returns the robot 'email address' associated with the device robot
131   // account (sometimes called a service account) associated with this device
132   // during enterprise enrollment.
133   std::string GetRobotAccountId();
134
135  private:
136   // Creates a new CloudPolicyClient.
137   scoped_ptr<CloudPolicyClient> CreateClient();
138
139   // Starts policy refreshes if |store_| indicates a managed device and the
140   // necessary dependencies have been provided via Initialize().
141   void StartIfManaged();
142
143   // Handles completion signaled by |enrollment_handler_|.
144   void EnrollmentCompleted(const EnrollmentCallback& callback,
145                            EnrollmentStatus status);
146
147   // Starts the connection via |client_to_connect|.
148   void StartConnection(scoped_ptr<CloudPolicyClient> client_to_connect);
149
150   // Initializes requisition settings at OOBE with values from VPD.
151   void InitalizeRequisition();
152
153   // Gets the device restore mode as stored in |local_state_|.
154   std::string GetRestoreMode() const;
155
156   // Points to the same object as the base CloudPolicyManager::store(), but with
157   // actual device policy specific type.
158   scoped_ptr<DeviceCloudPolicyStoreChromeOS> device_store_;
159   scoped_refptr<base::SequencedTaskRunner> background_task_runner_;
160   EnterpriseInstallAttributes* install_attributes_;
161
162   DeviceManagementService* device_management_service_;
163   scoped_ptr<CloudPolicyClient::StatusProvider> device_status_provider_;
164
165   // PrefService instance to read the policy refresh rate from.
166   PrefService* local_state_;
167
168   // Non-null if there is an enrollment operation pending.
169   scoped_ptr<EnrollmentHandlerChromeOS> enrollment_handler_;
170
171   scoped_ptr<chromeos::attestation::AttestationPolicyObserver>
172       attestation_policy_observer_;
173
174   DISALLOW_COPY_AND_ASSIGN(DeviceCloudPolicyManagerChromeOS);
175 };
176
177 }  // namespace policy
178
179 #endif  // CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_CLOUD_POLICY_MANAGER_CHROMEOS_H_