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; |
| 140 if (response) { |
| 141 dbus::MessageReader reader(response); |
| 142 if (!reader.PopObjectPath(&result)) { |
| 143 LOG(ERROR) << biod::kBiometricsManagerStartEnrollSessionMethod |
| 144 << " had incorrect response."; |
| 145 } |
| 146 } |
| 147 |
| 148 callback.Run(result); |
| 149 } |
| 150 |
| 151 void OnGetRecordsForUser(const UserRecordsCallback& callback, |
| 152 dbus::Response* response) { |
| 153 std::vector<dbus::ObjectPath> result; |
| 154 if (response) { |
| 155 dbus::MessageReader reader(response); |
| 156 if (!reader.PopArrayOfObjectPaths(&result)) { |
| 157 LOG(ERROR) << biod::kBiometricsManagerGetRecordsForUserMethod |
| 158 << " had incorrect response."; |
| 159 } |
| 160 } |
| 161 |
| 162 callback.Run(result); |
| 163 } |
| 164 |
| 165 void OnStartAuthSession(const ObjectPathCallback& callback, |
| 166 dbus::Response* response) { |
| 167 dbus::ObjectPath result; |
| 168 if (response) { |
| 169 dbus::MessageReader reader(response); |
| 170 if (!reader.PopObjectPath(&result)) { |
| 171 LOG(ERROR) << biod::kBiometricsManagerStartAuthSessionMethod |
| 172 << " had incorrect response."; |
| 173 } |
| 174 } |
| 175 |
| 176 callback.Run(result); |
| 177 } |
| 178 |
| 179 void OnRequestType(const BiometricTypeCallback& callback, |
| 180 dbus::Response* response) { |
| 181 uint32_t result = |
| 182 static_cast<uint32_t>(biod::BiometricType::BIOMETRIC_TYPE_UNKNOWN); |
| 183 if (response) { |
| 184 dbus::MessageReader reader(response); |
| 185 if (!reader.PopVariantOfUint32(&result)) { |
| 186 LOG(ERROR) << biod::kBiometricsManagerBiometricTypeProperty |
| 187 << " had incorrect response."; |
| 188 } |
| 189 } |
| 190 |
| 191 callback.Run(static_cast<biod::BiometricType>(result)); |
| 192 } |
| 193 |
| 194 // Called when the biometrics signal is initially connected. |
| 195 void OnSignalConnected(const std::string& interface_name, |
| 196 const std::string& signal_name, |
| 197 bool success) { |
| 198 LOG_IF(ERROR, !success) |
| 199 << "Failed to connect to biometrics signal: " << signal_name; |
| 200 } |
| 201 |
| 202 void NameOwnerChangedReceived(const std::string& /* old_owner */, |
| 203 const std::string& new_owner) { |
| 204 if (!new_owner.empty()) { |
| 205 for (auto& observer : observers_) |
| 206 observer.BiodServiceRestarted(); |
| 207 } |
| 208 } |
| 209 |
| 210 void EnrollScanDoneReceived(dbus::Signal* signal) { |
| 211 dbus::MessageReader reader(signal); |
| 212 uint32_t scan_result; |
| 213 bool enroll_session_complete; |
| 214 if (!reader.PopUint32(&scan_result) || |
| 215 !reader.PopBool(&enroll_session_complete)) { |
| 216 LOG(ERROR) << "Error reading signal from biometrics: " |
| 217 << signal->ToString(); |
| 218 return; |
| 219 } |
| 220 |
| 221 for (auto& observer : observers_) { |
| 222 observer.BiodEnrollScanDoneReceived( |
| 223 static_cast<biod::ScanResult>(scan_result), enroll_session_complete); |
| 224 } |
| 225 } |
| 226 |
| 227 void AuthScanDoneReceived(dbus::Signal* signal) { |
| 228 dbus::MessageReader signal_reader(signal); |
| 229 dbus::MessageReader array_reader(nullptr); |
| 230 uint32_t scan_result; |
| 231 AuthScanMatches matches; |
| 232 if (!signal_reader.PopUint32(&scan_result) || |
| 233 !signal_reader.PopArray(&array_reader)) { |
| 234 LOG(ERROR) << "Error reading signal from biometrics: " |
| 235 << signal->ToString(); |
| 236 return; |
| 237 } |
| 238 |
| 239 while (array_reader.HasMoreData()) { |
| 240 dbus::MessageReader entry_reader(nullptr); |
| 241 std::string user_id; |
| 242 std::vector<std::string> labels; |
| 243 if (!array_reader.PopDictEntry(&entry_reader) || |
| 244 !entry_reader.PopString(&user_id) || |
| 245 !entry_reader.PopArrayOfStrings(&labels)) { |
| 246 LOG(ERROR) << "Error reading signal from biometrics: " |
| 247 << signal->ToString(); |
| 248 return; |
| 249 } |
| 250 |
| 251 matches[user_id] = std::move(labels); |
| 252 } |
| 253 |
| 254 for (auto& observer : observers_) { |
| 255 observer.BiodAuthScanDoneReceived( |
| 256 static_cast<biod::ScanResult>(scan_result), matches); |
| 257 } |
| 258 } |
| 259 |
| 260 void SessionFailedReceived(dbus::Signal* signal) { |
| 261 for (auto& observer : observers_) |
| 262 observer.BiodSessionFailedReceived(); |
| 263 } |
| 264 |
| 265 dbus::ObjectProxy* biod_proxy_; |
| 266 base::ObserverList<Observer> observers_; |
| 267 |
| 268 // Note: This should remain the last member so it'll be destroyed and |
| 269 // invalidate its weak pointers before any other members are destroyed. |
| 270 base::WeakPtrFactory<BiodClientImpl> weak_ptr_factory_; |
| 271 |
| 272 DISALLOW_COPY_AND_ASSIGN(BiodClientImpl); |
| 273 }; |
| 274 |
| 275 BiodClient::BiodClient() {} |
| 276 |
| 277 BiodClient::~BiodClient() {} |
| 278 |
| 279 // static |
| 280 BiodClient* BiodClient::Create(DBusClientImplementationType /* type */) { |
| 281 return new BiodClientImpl(); |
| 282 } |
| 283 |
| 284 } // namespace chromeos |
OLD | NEW |