device_cloud_policy_initializer.h revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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#ifndef CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_CLOUD_POLICY_INITIALIZER_H_
6#define CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_CLOUD_POLICY_INITIALIZER_H_
7
8#include <bitset>
9#include <string>
10
11#include "base/callback.h"
12#include "base/compiler_specific.h"
13#include "base/macros.h"
14#include "base/memory/ref_counted.h"
15#include "base/memory/scoped_ptr.h"
16#include "chrome/browser/chromeos/policy/enrollment_status_chromeos.h"
17#include "chrome/browser/chromeos/policy/server_backed_state_keys_broker.h"
18#include "components/policy/core/common/cloud/cloud_policy_client.h"
19#include "components/policy/core/common/cloud/cloud_policy_store.h"
20#include "policy/proto/device_management_backend.pb.h"
21
22class PrefService;
23
24namespace base {
25class SequencedTaskRunner;
26}
27
28namespace chromeos {
29class DeviceSettingsService;
30}
31
32namespace policy {
33
34class DeviceCloudPolicyManagerChromeOS;
35class DeviceCloudPolicyStoreChromeOS;
36class DeviceManagementService;
37class EnrollmentHandlerChromeOS;
38class EnterpriseInstallAttributes;
39
40// This class connects DCPM to the correct device management service, and
41// handles the enrollment process.
42class DeviceCloudPolicyInitializer : public CloudPolicyStore::Observer {
43 public:
44  typedef std::bitset<32> AllowedDeviceModes;
45  typedef base::Callback<void(EnrollmentStatus)> EnrollmentCallback;
46
47  // |background_task_runner| is used to execute long-running background tasks
48  // that may involve file I/O.
49  // |on_connected_callback| is invoked after the device cloud policy manager
50  // is connected.
51  DeviceCloudPolicyInitializer(
52      PrefService* local_state,
53      DeviceManagementService* enterprise_service,
54      DeviceManagementService* consumer_service,
55      const scoped_refptr<base::SequencedTaskRunner>& background_task_runner,
56      EnterpriseInstallAttributes* install_attributes,
57      ServerBackedStateKeysBroker* state_keys_broker,
58      DeviceCloudPolicyStoreChromeOS* device_store,
59      DeviceCloudPolicyManagerChromeOS* manager,
60      chromeos::DeviceSettingsService* device_settings_service,
61      const base::Closure& on_connected_callback);
62
63  virtual ~DeviceCloudPolicyInitializer();
64
65  void Shutdown();
66
67  // Starts enrollment or re-enrollment. Once the enrollment process completes,
68  // |enrollment_callback| is invoked and gets passed the status of the
69  // operation.
70  // |allowed_modes| specifies acceptable DEVICE_MODE_* constants for
71  // enrollment.
72  // |management_mode| should be either ENTERPRISE_MANAGED or CONSUMER_MANAGED.
73  void StartEnrollment(
74      enterprise_management::PolicyData::ManagementMode management_mode,
75      DeviceManagementService* device_management_service,
76      const std::string& auth_token,
77      bool is_auto_enrollment,
78      const AllowedDeviceModes& allowed_modes,
79      const EnrollmentCallback& enrollment_callback);
80
81  // Checks whether enterprise enrollment should be a regular step during OOBE.
82  bool ShouldAutoStartEnrollment() const;
83
84  // Checks whether enterprise enrollment recovery is required.
85  bool ShouldRecoverEnrollment() const;
86
87  // Looks up the domain from |install_attributes_|.
88  std::string GetEnrollmentRecoveryDomain() const;
89
90  // Checks whether the user can cancel enrollment.
91  bool CanExitEnrollment() const;
92
93  // Gets the domain this device is supposed to be enrolled to.
94  std::string GetForcedEnrollmentDomain() const;
95
96  // CloudPolicyStore::Observer:
97  virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE;
98  virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE;
99
100 private:
101  // Handles completion signaled by |enrollment_handler_|.
102  void EnrollmentCompleted(const EnrollmentCallback& enrollment_callback,
103                           EnrollmentStatus status);
104
105  // Creates a new CloudPolicyClient.
106  scoped_ptr<CloudPolicyClient> CreateClient(
107      DeviceManagementService* device_management_service);
108
109  void TryToCreateClient();
110  void StartConnection(scoped_ptr<CloudPolicyClient> client);
111
112  // Gets the device restore mode as stored in |local_state_|.
113  std::string GetRestoreMode() const;
114
115  PrefService* local_state_;
116  DeviceManagementService* enterprise_service_;
117  DeviceManagementService* consumer_service_;
118  scoped_refptr<base::SequencedTaskRunner> background_task_runner_;
119  EnterpriseInstallAttributes* install_attributes_;
120  ServerBackedStateKeysBroker* state_keys_broker_;
121  DeviceCloudPolicyStoreChromeOS* device_store_;
122  DeviceCloudPolicyManagerChromeOS* manager_;
123  chromeos::DeviceSettingsService* device_settings_service_;
124  base::Closure on_connected_callback_;
125
126  // Non-NULL if there is an enrollment operation pending.
127  scoped_ptr<EnrollmentHandlerChromeOS> enrollment_handler_;
128
129  ServerBackedStateKeysBroker::Subscription state_keys_update_subscription_;
130
131  scoped_ptr<CloudPolicyClient::StatusProvider> device_status_provider_;
132
133  DISALLOW_COPY_AND_ASSIGN(DeviceCloudPolicyInitializer);
134};
135
136}  // namespace policy
137
138#endif  // CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_CLOUD_POLICY_INITIALIZER_H_
139