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 "u2f_enumerate.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "device/base/device_client.h" |
| 10 #include "u2f_hid_device.h" |
| 11 |
| 12 namespace device { |
| 13 |
| 14 U2fEnumerate::U2fEnumerate() |
| 15 : hid_service_observer_(this), weak_factory_(this) { |
| 16 filter_.SetUsagePage(0xf1d0); |
| 17 } |
| 18 |
| 19 U2fEnumerate::~U2fEnumerate() {} |
| 20 |
| 21 void U2fEnumerate::EnumerateU2fDevices( |
| 22 U2fEnumerate::U2fEnumerateCallback enumerate_cb) { |
| 23 HidService* hid_service = DeviceClient::Get()->GetHidService(); |
| 24 DCHECK(hid_service); |
| 25 hid_service_observer_.Add(hid_service); |
| 26 hid_service->GetDevices(base::Bind(&U2fEnumerate::OnHidEnumerationComplete, |
| 27 weak_factory_.GetWeakPtr(), |
| 28 base::Passed(&enumerate_cb))); |
| 29 } |
| 30 |
| 31 void U2fEnumerate::OnHidEnumerationComplete( |
| 32 U2fEnumerate::U2fEnumerateCallback callback, |
| 33 const std::vector<scoped_refptr<HidDeviceInfo>>& devices) { |
| 34 std::list<std::unique_ptr<U2fDevice>> u2f_devices; |
| 35 for (const scoped_refptr<HidDeviceInfo>& device_info : devices) { |
| 36 if (filter_.Matches(device_info)) |
| 37 u2f_devices.push_back(base::MakeUnique<U2fHidDevice>(device_info)); |
| 38 } |
| 39 std::move(callback).Run(std::move(u2f_devices)); |
| 40 } |
| 41 |
| 42 void U2fEnumerate::OnDeviceAdded(scoped_refptr<HidDeviceInfo> device_info) { |
| 43 if (device_added_cb_ && filter_.Matches(device_info)) |
| 44 device_added_cb_.Run(base::MakeUnique<U2fHidDevice>(device_info)); |
| 45 } |
| 46 |
| 47 void U2fEnumerate::OnDeviceRemoved(scoped_refptr<HidDeviceInfo> device_info) { |
| 48 if (device_removed_cb_ && filter_.Matches(device_info)) |
| 49 device_removed_cb_.Run(base::MakeUnique<U2fHidDevice>(device_info)); |
| 50 } |
| 51 |
| 52 } // namespace device |
OLD | NEW |