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  virtual void Init();
66  virtual void Shutdown();
67
68  // Starts enrollment or re-enrollment. Once the enrollment process completes,
69  // |enrollment_callback| is invoked and gets passed the status of the
70  // operation.
71  // |allowed_modes| specifies acceptable DEVICE_MODE_* constants for
72  // enrollment.
73  // |management_mode| should be either ENTERPRISE_MANAGED or CONSUMER_MANAGED.
74  virtual void StartEnrollment(
75      enterprise_management::PolicyData::ManagementMode management_mode,
76      DeviceManagementService* device_management_service,
77      const std::string& auth_token,
78      bool is_auto_enrollment,
79      const AllowedDeviceModes& allowed_modes,
80      const EnrollmentCallback& enrollment_callback);
81
82  // Checks whether enterprise enrollment should be a regular step during OOBE.
83  bool ShouldAutoStartEnrollment() const;
84
85  // Checks whether enterprise enrollment recovery is required.
86  bool ShouldRecoverEnrollment() const;
87
88  // Looks up the domain from |install_attributes_|.
89  std::string GetEnrollmentRecoveryDomain() const;
90
91  // Checks whether the user can cancel enrollment.
92  bool CanExitEnrollment() const;
93
94  // Gets the domain this device is supposed to be enrolled to.
95  std::string GetForcedEnrollmentDomain() const;
96
97  // CloudPolicyStore::Observer:
98  virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE;
99  virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE;
100
101 private:
102  // Handles completion signaled by |enrollment_handler_|.
103  void EnrollmentCompleted(const EnrollmentCallback& enrollment_callback,
104                           EnrollmentStatus status);
105
106  // Creates a new CloudPolicyClient.
107  scoped_ptr<CloudPolicyClient> CreateClient(
108      DeviceManagementService* device_management_service);
109
110  void TryToCreateClient();
111  void StartConnection(scoped_ptr<CloudPolicyClient> client);
112
113  // Gets the device restore mode as stored in |local_state_|.
114  std::string GetRestoreMode() const;
115
116  PrefService* local_state_;
117  DeviceManagementService* enterprise_service_;
118  DeviceManagementService* consumer_service_;
119  scoped_refptr<base::SequencedTaskRunner> background_task_runner_;
120  EnterpriseInstallAttributes* install_attributes_;
121  ServerBackedStateKeysBroker* state_keys_broker_;
122  DeviceCloudPolicyStoreChromeOS* device_store_;
123  DeviceCloudPolicyManagerChromeOS* manager_;
124  chromeos::DeviceSettingsService* device_settings_service_;
125  base::Closure on_connected_callback_;
126  bool is_initialized_;
127
128  // Non-NULL if there is an enrollment operation pending.
129  scoped_ptr<EnrollmentHandlerChromeOS> enrollment_handler_;
130
131  ServerBackedStateKeysBroker::Subscription state_keys_update_subscription_;
132
133  scoped_ptr<CloudPolicyClient::StatusProvider> device_status_provider_;
134
135  DISALLOW_COPY_AND_ASSIGN(DeviceCloudPolicyInitializer);
136};
137
138}  // namespace policy
139
140#endif  // CHROME_BROWSER_CHROMEOS_POLICY_DEVICE_CLOUD_POLICY_INITIALIZER_H_
141