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)); | |
Daniel Erat
2017/03/29 22:11:57
i think that this part is wrong. this class is sup
sammiequon
2017/03/30 00:22:27
If we are going with the possible single Biometric
Daniel Erat
2017/03/30 00:25:38
nope, it's correct then.
this class should probab
| |
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 LOG(ERROR) << "Failed to get response for " | |
Daniel Erat
2017/03/28 00:14:34
you can remove the LOG(ERROR)s in the response-is-
sammiequon
2017/03/28 01:31:05
Done.
| |
144 << biod::kBiometricsManagerStartEnrollSessionMethod | |
145 << " request."; | |
146 callback.Run(result); | |
147 return; | |
148 } | |
149 | |
150 dbus::MessageReader reader(response); | |
151 if (!reader.PopObjectPath(&result)) { | |
152 LOG(ERROR) << biod::kBiometricsManagerStartEnrollSessionMethod | |
153 << " had incorrect response."; | |
154 callback.Run(result); | |
155 return; | |
156 } | |
157 | |
158 callback.Run(result); | |
159 } | |
160 | |
161 void OnGetRecordsForUser(const UserRecordsCallback& callback, | |
162 dbus::Response* response) { | |
163 std::vector<dbus::ObjectPath> result; | |
164 if (!response) { | |
165 LOG(ERROR) << "Failed to get response for " | |
166 << biod::kBiometricsManagerGetRecordsForUserMethod | |
167 << " request."; | |
168 callback.Run(result); | |
169 return; | |
170 } | |
171 | |
172 dbus::MessageReader reader(response); | |
173 if (!reader.PopArrayOfObjectPaths(&result)) { | |
174 LOG(ERROR) << biod::kBiometricsManagerGetRecordsForUserMethod | |
175 << " had incorrect response."; | |
176 callback.Run(result); | |
177 return; | |
178 } | |
179 | |
180 callback.Run(result); | |
181 } | |
182 | |
183 void OnStartAuthSession(const ObjectPathCallback& callback, | |
184 dbus::Response* response) { | |
185 dbus::ObjectPath result; | |
186 if (!response) { | |
187 LOG(ERROR) << "Failed to get response for " | |
188 << biod::kBiometricsManagerStartAuthSessionMethod | |
189 << " request."; | |
190 callback.Run(result); | |
191 return; | |
192 } | |
193 | |
194 dbus::MessageReader reader(response); | |
195 if (!reader.PopObjectPath(&result)) { | |
196 LOG(ERROR) << biod::kBiometricsManagerStartAuthSessionMethod | |
197 << " had incorrect response."; | |
198 callback.Run(result); | |
199 return; | |
200 } | |
201 | |
202 callback.Run(result); | |
203 } | |
204 | |
205 void OnRequestType(const BiometricTypeCallback& callback, | |
206 dbus::Response* response) { | |
207 uint32_t result; | |
208 if (!response) { | |
209 LOG(ERROR) << "Failed to get response for " | |
210 << biod::kBiometricsManagerBiometricTypeProperty | |
211 << " request."; | |
212 callback.Run(biod::BiometricType::BIOMETRIC_TYPE_UNKNOWN); | |
213 return; | |
214 } | |
215 | |
216 dbus::MessageReader reader(response); | |
217 if (!reader.PopVariantOfUint32(&result)) { | |
218 LOG(ERROR) << biod::kBiometricsManagerBiometricTypeProperty | |
219 << " had incorrect response."; | |
220 callback.Run(biod::BiometricType::BIOMETRIC_TYPE_UNKNOWN); | |
221 return; | |
222 } | |
223 | |
224 callback.Run(static_cast<biod::BiometricType>(result)); | |
225 } | |
226 | |
227 // Called when the biometrics signal is initially connected. | |
228 void OnSignalConnected(const std::string& interface_name, | |
229 const std::string& signal_name, | |
230 bool success) { | |
231 LOG_IF(ERROR, !success) | |
232 << "Failed to connect to biometrics signal:" << signal_name; | |
233 } | |
234 | |
235 void NameOwnerChangedReceived(const std::string& /* old_owner */, | |
236 const std::string& new_owner) { | |
237 if (!new_owner.empty()) { | |
238 for (auto& observer : observers_) | |
239 observer.BiodServiceRestarted(); | |
240 } | |
241 } | |
242 | |
243 void EnrollScanDoneReceived(dbus::Signal* signal) { | |
244 dbus::MessageReader reader(signal); | |
245 uint32_t scan_result; | |
246 bool is_complete; | |
247 if (!reader.PopUint32(&scan_result) || !reader.PopBool(&is_complete)) { | |
248 LOG(ERROR) << "Error reading signal from biometrics:" | |
249 << signal->ToString(); | |
250 return; | |
251 } | |
252 | |
253 for (auto& observer : observers_) { | |
254 observer.BiodEnrollScanDoneReceived( | |
255 static_cast<biod::ScanResult>(scan_result), is_complete); | |
256 } | |
257 } | |
258 | |
259 void AuthScanDoneReceived(dbus::Signal* signal) { | |
260 dbus::MessageReader signal_reader(signal); | |
261 dbus::MessageReader array_reader(nullptr); | |
262 uint32_t scan_result; | |
263 AuthScanMatches matches; | |
264 if (!signal_reader.PopUint32(&scan_result) || | |
265 !signal_reader.PopArray(&array_reader)) { | |
266 LOG(ERROR) << "Error reading signal from biometrics:" | |
267 << signal->ToString(); | |
268 return; | |
269 } | |
270 | |
271 while (array_reader.HasMoreData()) { | |
272 dbus::MessageReader entry_reader(nullptr); | |
273 std::string user_id; | |
274 std::vector<std::string> labels; | |
275 if (!array_reader.PopDictEntry(&entry_reader) || | |
276 !entry_reader.PopString(&user_id) || | |
277 !entry_reader.PopArrayOfStrings(&labels)) { | |
278 LOG(ERROR) << "Error reading signal from biometrics:" | |
279 << signal->ToString(); | |
280 return; | |
281 } | |
282 | |
283 matches[user_id] = std::move(labels); | |
284 } | |
285 | |
286 for (auto& observer : observers_) { | |
287 observer.BiodAuthScanDoneReceived( | |
288 static_cast<biod::ScanResult>(scan_result), matches); | |
289 } | |
290 } | |
291 | |
292 void SessionFailedReceived(dbus::Signal* signal) { | |
293 for (auto& observer : observers_) | |
294 observer.BiodSessionFailedReceived(); | |
295 } | |
296 | |
297 dbus::ObjectProxy* biometrics_manager_proxy_; | |
298 base::ObserverList<Observer> observers_; | |
299 | |
300 // Note: This should remain the last member so it'll be destroyed and | |
301 // invalidate its weak pointers before any other members are destroyed. | |
302 base::WeakPtrFactory<BiodBiometricsManagerClientImpl> weak_ptr_factory_; | |
303 | |
304 DISALLOW_COPY_AND_ASSIGN(BiodBiometricsManagerClientImpl); | |
305 }; | |
306 | |
307 BiodBiometricsManagerClient::BiodBiometricsManagerClient() {} | |
308 | |
309 BiodBiometricsManagerClient::~BiodBiometricsManagerClient() {} | |
310 | |
311 // static | |
312 BiodBiometricsManagerClient* BiodBiometricsManagerClient::Create( | |
313 DBusClientImplementationType /* type */) { | |
314 return new BiodBiometricsManagerClientImpl(); | |
315 } | |
316 | |
317 } // namespace chromeos | |
OLD | NEW |