summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/policy/consumer_management_service.h
blob: 848284f52dea3e29302d2a618aec170c3ca4da73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_CHROMEOS_POLICY_CONSUMER_MANAGEMENT_SERVICE_H_
#define CHROME_BROWSER_CHROMEOS_POLICY_CONSUMER_MANAGEMENT_SERVICE_H_

#include <string>

#include "base/callback_forward.h"
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "chrome/browser/chromeos/policy/consumer_management_stage.h"
#include "chrome/browser/chromeos/settings/device_settings_service.h"
#include "chromeos/dbus/dbus_method_call_status.h"

class PrefRegistrySimple;

namespace chromeos {
class CryptohomeClient;
}

namespace cryptohome {
class BaseReply;
}

namespace policy {

// The consumer management service handles several things:
//
// 1. The consumer management status: The consumer management status is an enum
//    indicating if the device is consumer-managed and if enrollment or un-
//    enrollment is in progress. The service can be observed and the observers
//    will be notified when the status is changed. Note that the observers may
//    be notified even when the status is NOT changed. The observers need to
//    check the status upon receiving the notification.
//
// 2. The consumer management stage: The consumer management stage is a value
//    indicating the enrollment or the unenrollment process, stored in local
//    state to pass the information across reboots and between components,
//    including settings page, sign-in screen, and user notification.
//
// 3. Boot lockbox owner ID: Unlike the owner ID in CrosSettings, the owner ID
//    stored in the boot lockbox can only be modified after reboot and before
//    the first session starts. It is guaranteed that if the device is consumer
//    managed, the owner ID in the boot lockbox will be available, but not the
//    other way.
class ConsumerManagementService
    : public chromeos::DeviceSettingsService::Observer {
 public:
  // The status indicates if the device is enrolled, or if enrollment or
  // unenrollment is in progress. If you want to add a value here, please also
  // update |kStatusString| in the .cc file, and |ConsumerManagementStatus| in
  // chrome/browser/resources/options/chromeos/consumer_management_overlay.js
  enum Status {
    // The status is currently unavailable.
    STATUS_UNKNOWN = 0,

    STATUS_ENROLLED,
    STATUS_ENROLLING,
    STATUS_UNENROLLED,
    STATUS_UNENROLLING,

    // This should always be the last one.
    STATUS_LAST,
  };

  class Observer {
   public:
    // Called when the status changes.
    virtual void OnConsumerManagementStatusChanged() = 0;
  };

  // GetOwner() invokes this with an argument set to the owner user ID,
  // or an empty string on failure.
  typedef base::Callback<void(const std::string&)> GetOwnerCallback;

  // SetOwner() invokes this with an argument indicating success or failure.
  typedef base::Callback<void(bool)> SetOwnerCallback;

  // |client| and |device_settings_service| should outlive this object.
  ConsumerManagementService(
      chromeos::CryptohomeClient* client,
      chromeos::DeviceSettingsService* device_settings_service);

  ~ConsumerManagementService() override;

  // Registers prefs.
  static void RegisterPrefs(PrefRegistrySimple* registry);

  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

  // Returns the status.
  virtual Status GetStatus() const;

  // Returns the string value of the status.
  std::string GetStatusString() const;

  // Returns the stage.
  virtual ConsumerManagementStage GetStage() const;

  // Sets the stage.
  virtual void SetStage(const ConsumerManagementStage& stage);

  // Returns the device owner stored in the boot lockbox via |callback|.
  void GetOwner(const GetOwnerCallback& callback);

  // Stores the device owner user ID into the boot lockbox and signs it.
  // |callback| is invoked with an agument indicating success or failure.
  void SetOwner(const std::string& user_id, const SetOwnerCallback& callback);

  // chromeos::DeviceSettingsService::Observer:
  void OwnershipStatusChanged() override;
  void DeviceSettingsUpdated() override;
  void OnDeviceSettingsServiceShutdown() override;

 protected:
  void NotifyStatusChanged();

 private:
  void OnGetBootAttributeDone(
      const GetOwnerCallback& callback,
      chromeos::DBusMethodCallStatus call_status,
      bool dbus_success,
      const cryptohome::BaseReply& reply);

  void OnSetBootAttributeDone(const SetOwnerCallback& callback,
                              chromeos::DBusMethodCallStatus call_status,
                              bool dbus_success,
                              const cryptohome::BaseReply& reply);

  void OnFlushAndSignBootAttributesDone(
      const SetOwnerCallback& callback,
      chromeos::DBusMethodCallStatus call_status,
      bool dbus_success,
      const cryptohome::BaseReply& reply);

  chromeos::CryptohomeClient* client_;
  chromeos::DeviceSettingsService* device_settings_service_;

  base::ObserverList<Observer, true> observers_;
  base::WeakPtrFactory<ConsumerManagementService> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(ConsumerManagementService);
};

}  // namespace policy

#endif  // CHROME_BROWSER_CHROMEOS_POLICY_CONSUMER_MANAGEMENT_SERVICE_H_