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.
5 #include "chrome/browser/chromeos/policy/consumer_management_service.h"
8 #include "base/callback.h"
9 #include "base/logging.h"
10 #include "base/prefs/pref_registry_simple.h"
11 #include "base/prefs/pref_service.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/common/pref_names.h"
14 #include "chromeos/dbus/cryptohome/rpc.pb.h"
15 #include "chromeos/dbus/cryptohome_client.h"
16 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
17 #include "policy/proto/device_management_backend.pb.h"
19 namespace em
= enterprise_management
;
23 // Boot atttributes ID.
24 const char kAttributeOwnerId
[] = "consumer_management.owner_id";
26 // The string of Status enum.
27 const char* const kStatusString
[] = {
36 arraysize(kStatusString
) == policy::ConsumerManagementService::STATUS_LAST
,
37 "invalid kStatusString array size");
43 ConsumerManagementService::ConsumerManagementService(
44 chromeos::CryptohomeClient
* client
,
45 chromeos::DeviceSettingsService
* device_settings_service
)
47 device_settings_service_(device_settings_service
),
48 weak_ptr_factory_(this) {
49 // A NULL value may be passed in tests.
50 if (device_settings_service_
)
51 device_settings_service_
->AddObserver(this);
54 ConsumerManagementService::~ConsumerManagementService() {
55 if (device_settings_service_
)
56 device_settings_service_
->RemoveObserver(this);
60 void ConsumerManagementService::RegisterPrefs(PrefRegistrySimple
* registry
) {
61 registry
->RegisterIntegerPref(
62 prefs::kConsumerManagementStage
,
63 ConsumerManagementStage::None().ToInternalValue());
66 void ConsumerManagementService::AddObserver(Observer
* observer
) {
67 observers_
.AddObserver(observer
);
70 void ConsumerManagementService::RemoveObserver(Observer
* observer
) {
71 observers_
.RemoveObserver(observer
);
74 ConsumerManagementService::Status
75 ConsumerManagementService::GetStatus() const {
76 if (!device_settings_service_
)
77 return STATUS_UNKNOWN
;
79 const em::PolicyData
* policy_data
= device_settings_service_
->policy_data();
81 return STATUS_UNKNOWN
;
83 const ConsumerManagementStage stage
= GetStage();
84 if (GetManagementMode(*policy_data
) == MANAGEMENT_MODE_CONSUMER_MANAGED
) {
85 if (stage
.IsUnenrolling())
86 return STATUS_UNENROLLING
;
87 return STATUS_ENROLLED
;
90 if (stage
.IsEnrolling())
91 return STATUS_ENROLLING
;
93 return STATUS_UNENROLLED
;
96 std::string
ConsumerManagementService::GetStatusString() const {
97 return kStatusString
[GetStatus()];
100 ConsumerManagementStage
ConsumerManagementService::GetStage() const {
101 const PrefService
* prefs
= g_browser_process
->local_state();
102 const int stage
= prefs
->GetInteger(prefs::kConsumerManagementStage
);
103 return ConsumerManagementStage::FromInternalValue(stage
);
106 void ConsumerManagementService::SetStage(
107 const ConsumerManagementStage
& stage
) {
108 PrefService
* prefs
= g_browser_process
->local_state();
109 prefs
->SetInteger(prefs::kConsumerManagementStage
, stage
.ToInternalValue());
111 NotifyStatusChanged();
114 void ConsumerManagementService::GetOwner(const GetOwnerCallback
& callback
) {
115 cryptohome::GetBootAttributeRequest request
;
116 request
.set_name(kAttributeOwnerId
);
117 client_
->GetBootAttribute(
119 base::Bind(&ConsumerManagementService::OnGetBootAttributeDone
,
120 weak_ptr_factory_
.GetWeakPtr(),
124 void ConsumerManagementService::SetOwner(const std::string
& user_id
,
125 const SetOwnerCallback
& callback
) {
126 cryptohome::SetBootAttributeRequest request
;
127 request
.set_name(kAttributeOwnerId
);
128 request
.set_value(user_id
.data(), user_id
.size());
129 client_
->SetBootAttribute(
131 base::Bind(&ConsumerManagementService::OnSetBootAttributeDone
,
132 weak_ptr_factory_
.GetWeakPtr(),
136 void ConsumerManagementService::OwnershipStatusChanged() {
139 void ConsumerManagementService::DeviceSettingsUpdated() {
140 NotifyStatusChanged();
143 void ConsumerManagementService::OnDeviceSettingsServiceShutdown() {
144 device_settings_service_
= nullptr;
147 void ConsumerManagementService::NotifyStatusChanged() {
148 FOR_EACH_OBSERVER(Observer
, observers_
, OnConsumerManagementStatusChanged());
151 void ConsumerManagementService::OnGetBootAttributeDone(
152 const GetOwnerCallback
& callback
,
153 chromeos::DBusMethodCallStatus call_status
,
155 const cryptohome::BaseReply
& reply
) {
156 if (!dbus_success
|| reply
.error() != 0) {
157 LOG(ERROR
) << "Failed to get the owner info from boot lockbox.";
163 reply
.GetExtension(cryptohome::GetBootAttributeReply::reply
).value());
166 void ConsumerManagementService::OnSetBootAttributeDone(
167 const SetOwnerCallback
& callback
,
168 chromeos::DBusMethodCallStatus call_status
,
170 const cryptohome::BaseReply
& reply
) {
171 if (!dbus_success
|| reply
.error() != 0) {
172 LOG(ERROR
) << "Failed to set owner info in boot lockbox.";
177 cryptohome::FlushAndSignBootAttributesRequest request
;
178 client_
->FlushAndSignBootAttributes(
180 base::Bind(&ConsumerManagementService::OnFlushAndSignBootAttributesDone
,
181 weak_ptr_factory_
.GetWeakPtr(),
185 void ConsumerManagementService::OnFlushAndSignBootAttributesDone(
186 const SetOwnerCallback
& callback
,
187 chromeos::DBusMethodCallStatus call_status
,
189 const cryptohome::BaseReply
& reply
) {
190 if (!dbus_success
|| reply
.error() != 0) {
191 LOG(ERROR
) << "Failed to flush and sign boot lockbox.";
199 } // namespace policy