| OLD | NEW |
| (Empty) |
| 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 #include "chrome/browser/chromeos/policy/consumer_management_service.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/callback.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/macros.h" | |
| 11 #include "chrome/browser/browser_process.h" | |
| 12 #include "chrome/common/pref_names.h" | |
| 13 #include "chromeos/dbus/cryptohome/rpc.pb.h" | |
| 14 #include "chromeos/dbus/cryptohome_client.h" | |
| 15 #include "components/policy/core/common/cloud/cloud_policy_constants.h" | |
| 16 #include "components/prefs/pref_registry_simple.h" | |
| 17 #include "components/prefs/pref_service.h" | |
| 18 #include "policy/proto/device_management_backend.pb.h" | |
| 19 | |
| 20 namespace em = enterprise_management; | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 // Boot atttributes ID. | |
| 25 const char kAttributeOwnerId[] = "consumer_management.owner_id"; | |
| 26 | |
| 27 // The string of Status enum. | |
| 28 const char* const kStatusString[] = { | |
| 29 "StatusUnknown", | |
| 30 "StatusEnrolled", | |
| 31 "StatusEnrolling", | |
| 32 "StatusUnenrolled", | |
| 33 "StatusUnenrolling", | |
| 34 }; | |
| 35 | |
| 36 static_assert( | |
| 37 arraysize(kStatusString) == policy::ConsumerManagementService::STATUS_LAST, | |
| 38 "invalid kStatusString array size"); | |
| 39 | |
| 40 } // namespace | |
| 41 | |
| 42 namespace policy { | |
| 43 | |
| 44 ConsumerManagementService::ConsumerManagementService( | |
| 45 chromeos::CryptohomeClient* client, | |
| 46 chromeos::DeviceSettingsService* device_settings_service) | |
| 47 : client_(client), | |
| 48 device_settings_service_(device_settings_service), | |
| 49 weak_ptr_factory_(this) { | |
| 50 // A NULL value may be passed in tests. | |
| 51 if (device_settings_service_) | |
| 52 device_settings_service_->AddObserver(this); | |
| 53 } | |
| 54 | |
| 55 ConsumerManagementService::~ConsumerManagementService() { | |
| 56 if (device_settings_service_) | |
| 57 device_settings_service_->RemoveObserver(this); | |
| 58 } | |
| 59 | |
| 60 // static | |
| 61 void ConsumerManagementService::RegisterPrefs(PrefRegistrySimple* registry) { | |
| 62 registry->RegisterIntegerPref( | |
| 63 prefs::kConsumerManagementStage, | |
| 64 ConsumerManagementStage::None().ToInternalValue()); | |
| 65 } | |
| 66 | |
| 67 void ConsumerManagementService::AddObserver(Observer* observer) { | |
| 68 observers_.AddObserver(observer); | |
| 69 } | |
| 70 | |
| 71 void ConsumerManagementService::RemoveObserver(Observer* observer) { | |
| 72 observers_.RemoveObserver(observer); | |
| 73 } | |
| 74 | |
| 75 ConsumerManagementService::Status | |
| 76 ConsumerManagementService::GetStatus() const { | |
| 77 if (!device_settings_service_) | |
| 78 return STATUS_UNKNOWN; | |
| 79 | |
| 80 const em::PolicyData* policy_data = device_settings_service_->policy_data(); | |
| 81 if (!policy_data) | |
| 82 return STATUS_UNKNOWN; | |
| 83 | |
| 84 const ConsumerManagementStage stage = GetStage(); | |
| 85 if (GetManagementMode(*policy_data) == MANAGEMENT_MODE_CONSUMER_MANAGED) { | |
| 86 if (stage.IsUnenrolling()) | |
| 87 return STATUS_UNENROLLING; | |
| 88 return STATUS_ENROLLED; | |
| 89 } | |
| 90 | |
| 91 if (stage.IsEnrolling()) | |
| 92 return STATUS_ENROLLING; | |
| 93 | |
| 94 return STATUS_UNENROLLED; | |
| 95 } | |
| 96 | |
| 97 std::string ConsumerManagementService::GetStatusString() const { | |
| 98 return kStatusString[GetStatus()]; | |
| 99 } | |
| 100 | |
| 101 ConsumerManagementStage ConsumerManagementService::GetStage() const { | |
| 102 const PrefService* prefs = g_browser_process->local_state(); | |
| 103 const int stage = prefs->GetInteger(prefs::kConsumerManagementStage); | |
| 104 return ConsumerManagementStage::FromInternalValue(stage); | |
| 105 } | |
| 106 | |
| 107 void ConsumerManagementService::SetStage( | |
| 108 const ConsumerManagementStage& stage) { | |
| 109 PrefService* prefs = g_browser_process->local_state(); | |
| 110 prefs->SetInteger(prefs::kConsumerManagementStage, stage.ToInternalValue()); | |
| 111 | |
| 112 NotifyStatusChanged(); | |
| 113 } | |
| 114 | |
| 115 void ConsumerManagementService::GetOwner(const GetOwnerCallback& callback) { | |
| 116 cryptohome::GetBootAttributeRequest request; | |
| 117 request.set_name(kAttributeOwnerId); | |
| 118 client_->GetBootAttribute( | |
| 119 request, | |
| 120 base::Bind(&ConsumerManagementService::OnGetBootAttributeDone, | |
| 121 weak_ptr_factory_.GetWeakPtr(), | |
| 122 callback)); | |
| 123 } | |
| 124 | |
| 125 void ConsumerManagementService::SetOwner(const std::string& user_id, | |
| 126 const SetOwnerCallback& callback) { | |
| 127 cryptohome::SetBootAttributeRequest request; | |
| 128 request.set_name(kAttributeOwnerId); | |
| 129 request.set_value(user_id.data(), user_id.size()); | |
| 130 client_->SetBootAttribute( | |
| 131 request, | |
| 132 base::Bind(&ConsumerManagementService::OnSetBootAttributeDone, | |
| 133 weak_ptr_factory_.GetWeakPtr(), | |
| 134 callback)); | |
| 135 } | |
| 136 | |
| 137 void ConsumerManagementService::OwnershipStatusChanged() { | |
| 138 } | |
| 139 | |
| 140 void ConsumerManagementService::DeviceSettingsUpdated() { | |
| 141 NotifyStatusChanged(); | |
| 142 } | |
| 143 | |
| 144 void ConsumerManagementService::OnDeviceSettingsServiceShutdown() { | |
| 145 device_settings_service_ = nullptr; | |
| 146 } | |
| 147 | |
| 148 void ConsumerManagementService::NotifyStatusChanged() { | |
| 149 FOR_EACH_OBSERVER(Observer, observers_, OnConsumerManagementStatusChanged()); | |
| 150 } | |
| 151 | |
| 152 void ConsumerManagementService::OnGetBootAttributeDone( | |
| 153 const GetOwnerCallback& callback, | |
| 154 chromeos::DBusMethodCallStatus call_status, | |
| 155 bool dbus_success, | |
| 156 const cryptohome::BaseReply& reply) { | |
| 157 if (!dbus_success || reply.error() != 0) { | |
| 158 LOG(ERROR) << "Failed to get the owner info from boot lockbox."; | |
| 159 callback.Run(""); | |
| 160 return; | |
| 161 } | |
| 162 | |
| 163 callback.Run( | |
| 164 reply.GetExtension(cryptohome::GetBootAttributeReply::reply).value()); | |
| 165 } | |
| 166 | |
| 167 void ConsumerManagementService::OnSetBootAttributeDone( | |
| 168 const SetOwnerCallback& callback, | |
| 169 chromeos::DBusMethodCallStatus call_status, | |
| 170 bool dbus_success, | |
| 171 const cryptohome::BaseReply& reply) { | |
| 172 if (!dbus_success || reply.error() != 0) { | |
| 173 LOG(ERROR) << "Failed to set owner info in boot lockbox."; | |
| 174 callback.Run(false); | |
| 175 return; | |
| 176 } | |
| 177 | |
| 178 cryptohome::FlushAndSignBootAttributesRequest request; | |
| 179 client_->FlushAndSignBootAttributes( | |
| 180 request, | |
| 181 base::Bind(&ConsumerManagementService::OnFlushAndSignBootAttributesDone, | |
| 182 weak_ptr_factory_.GetWeakPtr(), | |
| 183 callback)); | |
| 184 } | |
| 185 | |
| 186 void ConsumerManagementService::OnFlushAndSignBootAttributesDone( | |
| 187 const SetOwnerCallback& callback, | |
| 188 chromeos::DBusMethodCallStatus call_status, | |
| 189 bool dbus_success, | |
| 190 const cryptohome::BaseReply& reply) { | |
| 191 if (!dbus_success || reply.error() != 0) { | |
| 192 LOG(ERROR) << "Failed to flush and sign boot lockbox."; | |
| 193 callback.Run(false); | |
| 194 return; | |
| 195 } | |
| 196 | |
| 197 callback.Run(true); | |
| 198 } | |
| 199 | |
| 200 } // namespace policy | |
| OLD | NEW |