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