OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "device/usb/usb_device_filter.h" | 5 #include "device/usb/usb_device_filter.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/memory/ptr_util.h" | |
10 #include "base/values.h" | 11 #include "base/values.h" |
11 #include "device/usb/usb_descriptors.h" | 12 #include "device/usb/usb_descriptors.h" |
12 #include "device/usb/usb_device.h" | 13 #include "device/usb/usb_device.h" |
13 | 14 |
14 namespace device { | 15 namespace device { |
15 | 16 |
16 namespace { | 17 namespace { |
17 | 18 |
18 const char kProductIdKey[] = "productId"; | 19 const char kProductIdKey[] = "productId"; |
19 const char kVendorIdKey[] = "vendorId"; | 20 const char kVendorIdKey[] = "vendorId"; |
20 const char kInterfaceClassKey[] = "interfaceClass"; | 21 const char kInterfaceClassKey[] = "interfaceClass"; |
21 const char kInterfaceSubclassKey[] = "interfaceSubclass"; | 22 const char kInterfaceSubclassKey[] = "interfaceSubclass"; |
22 const char kInterfaceProtocolKey[] = "interfaceProtocol"; | 23 const char kInterfaceProtocolKey[] = "interfaceProtocol"; |
23 | 24 |
24 } // namespace | 25 } // namespace |
25 | 26 |
26 UsbDeviceFilter::UsbDeviceFilter() | 27 UsbDeviceFilter::UsbDeviceFilter() = default; |
27 : vendor_id_set_(false), | |
28 product_id_set_(false), | |
29 interface_class_set_(false), | |
30 interface_subclass_set_(false), | |
31 interface_protocol_set_(false) { | |
32 } | |
33 | 28 |
34 UsbDeviceFilter::UsbDeviceFilter(const UsbDeviceFilter& other) = default; | 29 UsbDeviceFilter::UsbDeviceFilter(const UsbDeviceFilter& other) = default; |
35 | 30 |
36 UsbDeviceFilter::~UsbDeviceFilter() { | 31 UsbDeviceFilter::~UsbDeviceFilter() = default; |
37 } | |
38 | |
39 void UsbDeviceFilter::SetVendorId(uint16_t vendor_id) { | |
40 vendor_id_set_ = true; | |
41 vendor_id_ = vendor_id; | |
42 } | |
43 | |
44 void UsbDeviceFilter::SetProductId(uint16_t product_id) { | |
45 product_id_set_ = true; | |
46 product_id_ = product_id; | |
47 } | |
48 | |
49 void UsbDeviceFilter::SetInterfaceClass(uint8_t interface_class) { | |
50 interface_class_set_ = true; | |
51 interface_class_ = interface_class; | |
52 } | |
53 | |
54 void UsbDeviceFilter::SetInterfaceSubclass(uint8_t interface_subclass) { | |
55 interface_subclass_set_ = true; | |
56 interface_subclass_ = interface_subclass; | |
57 } | |
58 | |
59 void UsbDeviceFilter::SetInterfaceProtocol(uint8_t interface_protocol) { | |
60 interface_protocol_set_ = true; | |
61 interface_protocol_ = interface_protocol; | |
62 } | |
63 | 32 |
64 bool UsbDeviceFilter::Matches(scoped_refptr<UsbDevice> device) const { | 33 bool UsbDeviceFilter::Matches(scoped_refptr<UsbDevice> device) const { |
65 if (vendor_id_set_) { | 34 if (vendor_id) { |
66 if (device->vendor_id() != vendor_id_) { | 35 if (device->vendor_id() != *vendor_id) |
67 return false; | 36 return false; |
68 } | |
69 | 37 |
70 if (product_id_set_ && device->product_id() != product_id_) { | 38 if (product_id && device->product_id() != *product_id) |
71 return false; | 39 return false; |
72 } | |
73 } | 40 } |
74 | 41 |
75 if (interface_class_set_) { | 42 if (interface_class) { |
76 for (const UsbConfigDescriptor& config : device->configurations()) { | 43 for (const UsbConfigDescriptor& config : device->configurations()) { |
77 for (const UsbInterfaceDescriptor& iface : config.interfaces) { | 44 for (const UsbInterfaceDescriptor& iface : config.interfaces) { |
78 if (iface.interface_class == interface_class_ && | 45 if (iface.interface_class == *interface_class && |
79 (!interface_subclass_set_ || | 46 (!interface_subclass || |
80 (iface.interface_subclass == interface_subclass_ && | 47 (iface.interface_subclass == *interface_subclass && |
81 (!interface_protocol_set_ || | 48 (!interface_protocol || |
82 iface.interface_protocol == interface_protocol_)))) { | 49 iface.interface_protocol == *interface_protocol)))) { |
83 return true; | 50 return true; |
84 } | 51 } |
85 } | 52 } |
86 } | 53 } |
87 | 54 |
88 return false; | 55 return false; |
89 } | 56 } |
90 | 57 |
91 return true; | 58 return true; |
92 } | 59 } |
93 | 60 |
94 std::unique_ptr<base::Value> UsbDeviceFilter::ToValue() const { | 61 std::unique_ptr<base::Value> UsbDeviceFilter::ToValue() const { |
95 std::unique_ptr<base::DictionaryValue> obj(new base::DictionaryValue()); | 62 auto obj = base::MakeUnique<base::DictionaryValue>(); |
96 | 63 |
97 if (vendor_id_set_) { | 64 if (vendor_id) { |
98 obj->SetInteger(kVendorIdKey, vendor_id_); | 65 obj->SetInteger(kVendorIdKey, *vendor_id); |
99 if (product_id_set_) { | 66 if (product_id) { |
juncai
2017/01/12 00:50:23
nit: remove '{}' for single line.
Reilly Grant (use Gerrit)
2017/01/12 02:18:44
Done.
| |
100 obj->SetInteger(kProductIdKey, product_id_); | 67 obj->SetInteger(kProductIdKey, *product_id); |
101 } | 68 } |
102 } | 69 } |
103 | 70 |
104 if (interface_class_set_) { | 71 if (interface_class) { |
105 obj->SetInteger(kInterfaceClassKey, interface_class_); | 72 obj->SetInteger(kInterfaceClassKey, *interface_class); |
106 if (interface_subclass_set_) { | 73 if (interface_subclass) { |
107 obj->SetInteger(kInterfaceSubclassKey, interface_subclass_); | 74 obj->SetInteger(kInterfaceSubclassKey, *interface_subclass); |
108 if (interface_protocol_set_) { | 75 if (interface_protocol) |
109 obj->SetInteger(kInterfaceProtocolKey, interface_protocol_); | 76 obj->SetInteger(kInterfaceProtocolKey, *interface_protocol); |
110 } | |
111 } | 77 } |
112 } | 78 } |
113 | 79 |
114 return std::move(obj); | 80 return std::move(obj); |
115 } | 81 } |
116 | 82 |
117 // static | 83 // static |
118 bool UsbDeviceFilter::MatchesAny(scoped_refptr<UsbDevice> device, | 84 bool UsbDeviceFilter::MatchesAny(scoped_refptr<UsbDevice> device, |
119 const std::vector<UsbDeviceFilter>& filters) { | 85 const std::vector<UsbDeviceFilter>& filters) { |
120 if (filters.empty()) | 86 if (filters.empty()) |
121 return true; | 87 return true; |
122 | 88 |
123 for (std::vector<UsbDeviceFilter>::const_iterator i = filters.begin(); | 89 for (const UsbDeviceFilter& filter : filters) { |
juncai
2017/01/12 00:50:23
nit: const auto&?
Reilly Grant (use Gerrit)
2017/01/12 02:18:44
Done.
| |
124 i != filters.end(); | 90 if (filter.Matches(device)) |
125 ++i) { | |
126 if (i->Matches(device)) { | |
127 return true; | 91 return true; |
128 } | |
129 } | 92 } |
130 return false; | 93 return false; |
131 } | 94 } |
132 | 95 |
133 } // namespace device | 96 } // namespace device |
OLD | NEW |