OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 "chromeos/dbus/biod/biod_biometrics_manager_client.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/macros.h" |
| 11 #include "dbus/bus.h" |
| 12 #include "dbus/message.h" |
| 13 #include "dbus/object_path.h" |
| 14 #include "dbus/object_proxy.h" |
| 15 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 16 |
| 17 namespace chromeos { |
| 18 |
| 19 // The BiodBiometricsManagerClient implementation used in production. |
| 20 class BiodBiometricsManagerClientImpl : public BiodBiometricsManagerClient { |
| 21 public: |
| 22 BiodBiometricsManagerClientImpl() |
| 23 : biometrics_manager_proxy_(nullptr), weak_ptr_factory_(this) {} |
| 24 |
| 25 ~BiodBiometricsManagerClientImpl() override {} |
| 26 |
| 27 // BiodBiometricsManagerClient overrides: |
| 28 void AddObserver(Observer* observer) override { |
| 29 observers_.AddObserver(observer); |
| 30 } |
| 31 |
| 32 void RemoveObserver(Observer* observer) override { |
| 33 observers_.RemoveObserver(observer); |
| 34 } |
| 35 |
| 36 bool HasObserver(const Observer* observer) const override { |
| 37 return observers_.HasObserver(observer); |
| 38 } |
| 39 |
| 40 void StartEnrollSession(const std::string& user_id, |
| 41 const std::string& label, |
| 42 const ObjectPathCallback& callback) override { |
| 43 dbus::MethodCall method_call( |
| 44 biod::kBiometricsManagerInterface, |
| 45 biod::kBiometricsManagerStartEnrollSessionMethod); |
| 46 dbus::MessageWriter writer(&method_call); |
| 47 writer.AppendString(user_id); |
| 48 writer.AppendString(label); |
| 49 |
| 50 biometrics_manager_proxy_->CallMethod( |
| 51 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 52 base::Bind(&BiodBiometricsManagerClientImpl::OnStartEnrollSession, |
| 53 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 54 } |
| 55 |
| 56 void GetRecordsForUser(const std::string& user_id, |
| 57 const UserRecordsCallback& callback) override { |
| 58 dbus::MethodCall method_call( |
| 59 biod::kBiometricsManagerInterface, |
| 60 biod::kBiometricsManagerGetRecordsForUserMethod); |
| 61 dbus::MessageWriter writer(&method_call); |
| 62 writer.AppendString(user_id); |
| 63 |
| 64 biometrics_manager_proxy_->CallMethod( |
| 65 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 66 base::Bind(&BiodBiometricsManagerClientImpl::OnGetRecordsForUser, |
| 67 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 68 } |
| 69 |
| 70 void DestroyAllRecords() override { |
| 71 dbus::MethodCall method_call( |
| 72 biod::kBiometricsManagerInterface, |
| 73 biod::kBiometricsManagerDestroyAllRecordsMethod); |
| 74 |
| 75 biometrics_manager_proxy_->CallMethod( |
| 76 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 77 dbus::ObjectProxy::EmptyResponseCallback()); |
| 78 } |
| 79 |
| 80 void StartAuthSession(const ObjectPathCallback& callback) override { |
| 81 dbus::MethodCall method_call( |
| 82 biod::kBiometricsManagerInterface, |
| 83 biod::kBiometricsManagerStartAuthSessionMethod); |
| 84 |
| 85 biometrics_manager_proxy_->CallMethod( |
| 86 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 87 base::Bind(&BiodBiometricsManagerClientImpl::OnStartAuthSession, |
| 88 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 89 } |
| 90 |
| 91 void RequestType(const BiometricTypeCallback& callback) override { |
| 92 dbus::MethodCall method_call(dbus::kDBusPropertiesInterface, |
| 93 dbus::kDBusPropertiesGet); |
| 94 dbus::MessageWriter writer(&method_call); |
| 95 writer.AppendString(biod::kBiometricsManagerInterface); |
| 96 writer.AppendString(biod::kBiometricsManagerBiometricTypeProperty); |
| 97 |
| 98 biometrics_manager_proxy_->CallMethod( |
| 99 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 100 base::Bind(&BiodBiometricsManagerClientImpl::OnRequestType, |
| 101 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 102 } |
| 103 |
| 104 protected: |
| 105 void Init(dbus::Bus* bus) override { |
| 106 biometrics_manager_proxy_ = bus->GetObjectProxy( |
| 107 biod::kBiodServiceName, dbus::ObjectPath(biod::kBiodServicePath)); |
| 108 |
| 109 biometrics_manager_proxy_->SetNameOwnerChangedCallback( |
| 110 base::Bind(&BiodBiometricsManagerClientImpl::NameOwnerChangedReceived, |
| 111 weak_ptr_factory_.GetWeakPtr())); |
| 112 |
| 113 biometrics_manager_proxy_->ConnectToSignal( |
| 114 biod::kBiometricsManagerInterface, |
| 115 biod::kBiometricsManagerEnrollScanDoneSignal, |
| 116 base::Bind(&BiodBiometricsManagerClientImpl::EnrollScanDoneReceived, |
| 117 weak_ptr_factory_.GetWeakPtr()), |
| 118 base::Bind(&BiodBiometricsManagerClientImpl::OnSignalConnected, |
| 119 weak_ptr_factory_.GetWeakPtr())); |
| 120 |
| 121 biometrics_manager_proxy_->ConnectToSignal( |
| 122 biod::kBiometricsManagerInterface, |
| 123 biod::kBiometricsManagerAuthScanDoneSignal, |
| 124 base::Bind(&BiodBiometricsManagerClientImpl::AuthScanDoneReceived, |
| 125 weak_ptr_factory_.GetWeakPtr()), |
| 126 base::Bind(&BiodBiometricsManagerClientImpl::OnSignalConnected, |
| 127 weak_ptr_factory_.GetWeakPtr())); |
| 128 |
| 129 biometrics_manager_proxy_->ConnectToSignal( |
| 130 biod::kBiometricsManagerInterface, |
| 131 biod::kBiometricsManagerSessionFailedSignal, |
| 132 base::Bind(&BiodBiometricsManagerClientImpl::SessionFailedReceived, |
| 133 weak_ptr_factory_.GetWeakPtr()), |
| 134 base::Bind(&BiodBiometricsManagerClientImpl::OnSignalConnected, |
| 135 weak_ptr_factory_.GetWeakPtr())); |
| 136 } |
| 137 |
| 138 private: |
| 139 void OnStartEnrollSession(const ObjectPathCallback& callback, |
| 140 dbus::Response* response) { |
| 141 dbus::ObjectPath result; |
| 142 if (!response) { |
| 143 callback.Run(result); |
| 144 return; |
| 145 } |
| 146 |
| 147 dbus::MessageReader reader(response); |
| 148 if (!reader.PopObjectPath(&result)) { |
| 149 LOG(ERROR) << biod::kBiometricsManagerStartEnrollSessionMethod |
| 150 << " had incorrect response."; |
| 151 callback.Run(result); |
| 152 return; |
| 153 } |
| 154 |
| 155 callback.Run(result); |
| 156 } |
| 157 |
| 158 void OnGetRecordsForUser(const UserRecordsCallback& callback, |
| 159 dbus::Response* response) { |
| 160 std::vector<dbus::ObjectPath> result; |
| 161 if (!response) { |
| 162 callback.Run(result); |
| 163 return; |
| 164 } |
| 165 |
| 166 dbus::MessageReader reader(response); |
| 167 if (!reader.PopArrayOfObjectPaths(&result)) { |
| 168 LOG(ERROR) << biod::kBiometricsManagerGetRecordsForUserMethod |
| 169 << " had incorrect response."; |
| 170 callback.Run(result); |
| 171 return; |
| 172 } |
| 173 |
| 174 callback.Run(result); |
| 175 } |
| 176 |
| 177 void OnStartAuthSession(const ObjectPathCallback& callback, |
| 178 dbus::Response* response) { |
| 179 dbus::ObjectPath result; |
| 180 if (!response) { |
| 181 callback.Run(result); |
| 182 return; |
| 183 } |
| 184 |
| 185 dbus::MessageReader reader(response); |
| 186 if (!reader.PopObjectPath(&result)) { |
| 187 LOG(ERROR) << biod::kBiometricsManagerStartAuthSessionMethod |
| 188 << " had incorrect response."; |
| 189 callback.Run(result); |
| 190 return; |
| 191 } |
| 192 |
| 193 callback.Run(result); |
| 194 } |
| 195 |
| 196 void OnRequestType(const BiometricTypeCallback& callback, |
| 197 dbus::Response* response) { |
| 198 uint32_t result; |
| 199 if (!response) { |
| 200 callback.Run(biod::BiometricType::BIOMETRIC_TYPE_UNKNOWN); |
| 201 return; |
| 202 } |
| 203 |
| 204 dbus::MessageReader reader(response); |
| 205 if (!reader.PopVariantOfUint32(&result)) { |
| 206 LOG(ERROR) << biod::kBiometricsManagerBiometricTypeProperty |
| 207 << " had incorrect response."; |
| 208 callback.Run(biod::BiometricType::BIOMETRIC_TYPE_UNKNOWN); |
| 209 return; |
| 210 } |
| 211 |
| 212 callback.Run(static_cast<biod::BiometricType>(result)); |
| 213 } |
| 214 |
| 215 // Called when the biometrics signal is initially connected. |
| 216 void OnSignalConnected(const std::string& interface_name, |
| 217 const std::string& signal_name, |
| 218 bool success) { |
| 219 LOG_IF(ERROR, !success) |
| 220 << "Failed to connect to biometrics signal:" << signal_name; |
| 221 } |
| 222 |
| 223 void NameOwnerChangedReceived(const std::string& /* old_owner */, |
| 224 const std::string& new_owner) { |
| 225 if (!new_owner.empty()) { |
| 226 for (auto& observer : observers_) |
| 227 observer.BiodServiceRestarted(); |
| 228 } |
| 229 } |
| 230 |
| 231 void EnrollScanDoneReceived(dbus::Signal* signal) { |
| 232 dbus::MessageReader reader(signal); |
| 233 uint32_t scan_result; |
| 234 bool enroll_session_complete; |
| 235 if (!reader.PopUint32(&scan_result) || |
| 236 !reader.PopBool(&enroll_session_complete)) { |
| 237 LOG(ERROR) << "Error reading signal from biometrics:" |
| 238 << signal->ToString(); |
| 239 return; |
| 240 } |
| 241 |
| 242 for (auto& observer : observers_) { |
| 243 observer.BiodEnrollScanDoneReceived( |
| 244 static_cast<biod::ScanResult>(scan_result), enroll_session_complete); |
| 245 } |
| 246 } |
| 247 |
| 248 void AuthScanDoneReceived(dbus::Signal* signal) { |
| 249 dbus::MessageReader signal_reader(signal); |
| 250 dbus::MessageReader array_reader(nullptr); |
| 251 uint32_t scan_result; |
| 252 AuthScanMatches matches; |
| 253 if (!signal_reader.PopUint32(&scan_result) || |
| 254 !signal_reader.PopArray(&array_reader)) { |
| 255 LOG(ERROR) << "Error reading signal from biometrics:" |
| 256 << signal->ToString(); |
| 257 return; |
| 258 } |
| 259 |
| 260 while (array_reader.HasMoreData()) { |
| 261 dbus::MessageReader entry_reader(nullptr); |
| 262 std::string user_id; |
| 263 std::vector<std::string> labels; |
| 264 if (!array_reader.PopDictEntry(&entry_reader) || |
| 265 !entry_reader.PopString(&user_id) || |
| 266 !entry_reader.PopArrayOfStrings(&labels)) { |
| 267 LOG(ERROR) << "Error reading signal from biometrics:" |
| 268 << signal->ToString(); |
| 269 return; |
| 270 } |
| 271 |
| 272 matches[user_id] = std::move(labels); |
| 273 } |
| 274 |
| 275 for (auto& observer : observers_) { |
| 276 observer.BiodAuthScanDoneReceived( |
| 277 static_cast<biod::ScanResult>(scan_result), matches); |
| 278 } |
| 279 } |
| 280 |
| 281 void SessionFailedReceived(dbus::Signal* signal) { |
| 282 for (auto& observer : observers_) |
| 283 observer.BiodSessionFailedReceived(); |
| 284 } |
| 285 |
| 286 dbus::ObjectProxy* biometrics_manager_proxy_; |
| 287 base::ObserverList<Observer> observers_; |
| 288 |
| 289 // Note: This should remain the last member so it'll be destroyed and |
| 290 // invalidate its weak pointers before any other members are destroyed. |
| 291 base::WeakPtrFactory<BiodBiometricsManagerClientImpl> weak_ptr_factory_; |
| 292 |
| 293 DISALLOW_COPY_AND_ASSIGN(BiodBiometricsManagerClientImpl); |
| 294 }; |
| 295 |
| 296 BiodBiometricsManagerClient::BiodBiometricsManagerClient() {} |
| 297 |
| 298 BiodBiometricsManagerClient::~BiodBiometricsManagerClient() {} |
| 299 |
| 300 // static |
| 301 BiodBiometricsManagerClient* BiodBiometricsManagerClient::Create( |
| 302 DBusClientImplementationType /* type */) { |
| 303 return new BiodBiometricsManagerClientImpl(); |
| 304 } |
| 305 |
| 306 } // namespace chromeos |
OLD | NEW |