Update broken references to image assets
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / consumer_management_service.cc
blob2e64d9b582991b9d04272930b81c24cec968c4a1
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"
7 #include "base/bind.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;
21 namespace {
23 // Boot atttributes ID.
24 const char kAttributeOwnerId[] = "consumer_management.owner_id";
26 // The string of Status enum.
27 const char* const kStatusString[] = {
28 "StatusUnknown",
29 "StatusEnrolled",
30 "StatusEnrolling",
31 "StatusUnenrolled",
32 "StatusUnenrolling",
35 static_assert(
36 arraysize(kStatusString) == policy::ConsumerManagementService::STATUS_LAST,
37 "invalid kStatusString array size");
39 } // namespace
41 namespace policy {
43 ConsumerManagementService::ConsumerManagementService(
44 chromeos::CryptohomeClient* client,
45 chromeos::DeviceSettingsService* device_settings_service)
46 : client_(client),
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);
59 // static
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();
80 if (!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(
118 request,
119 base::Bind(&ConsumerManagementService::OnGetBootAttributeDone,
120 weak_ptr_factory_.GetWeakPtr(),
121 callback));
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(
130 request,
131 base::Bind(&ConsumerManagementService::OnSetBootAttributeDone,
132 weak_ptr_factory_.GetWeakPtr(),
133 callback));
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,
154 bool dbus_success,
155 const cryptohome::BaseReply& reply) {
156 if (!dbus_success || reply.error() != 0) {
157 LOG(ERROR) << "Failed to get the owner info from boot lockbox.";
158 callback.Run("");
159 return;
162 callback.Run(
163 reply.GetExtension(cryptohome::GetBootAttributeReply::reply).value());
166 void ConsumerManagementService::OnSetBootAttributeDone(
167 const SetOwnerCallback& callback,
168 chromeos::DBusMethodCallStatus call_status,
169 bool dbus_success,
170 const cryptohome::BaseReply& reply) {
171 if (!dbus_success || reply.error() != 0) {
172 LOG(ERROR) << "Failed to set owner info in boot lockbox.";
173 callback.Run(false);
174 return;
177 cryptohome::FlushAndSignBootAttributesRequest request;
178 client_->FlushAndSignBootAttributes(
179 request,
180 base::Bind(&ConsumerManagementService::OnFlushAndSignBootAttributesDone,
181 weak_ptr_factory_.GetWeakPtr(),
182 callback));
185 void ConsumerManagementService::OnFlushAndSignBootAttributesDone(
186 const SetOwnerCallback& callback,
187 chromeos::DBusMethodCallStatus call_status,
188 bool dbus_success,
189 const cryptohome::BaseReply& reply) {
190 if (!dbus_success || reply.error() != 0) {
191 LOG(ERROR) << "Failed to flush and sign boot lockbox.";
192 callback.Run(false);
193 return;
196 callback.Run(true);
199 } // namespace policy