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
3 // found in the LICENSE file.
5 #include "device/usb/usb_device_filter.h"
7 #include "base/values.h"
8 #include "device/usb/usb_descriptors.h"
9 #include "device/usb/usb_device.h"
15 const char kProductIdKey
[] = "productId";
16 const char kVendorIdKey
[] = "vendorId";
17 const char kInterfaceClassKey
[] = "interfaceClass";
18 const char kInterfaceSubclassKey
[] = "interfaceSubclass";
19 const char kInterfaceProtocolKey
[] = "interfaceProtocol";
23 UsbDeviceFilter::UsbDeviceFilter()
24 : vendor_id_set_(false),
25 product_id_set_(false),
26 interface_class_set_(false),
27 interface_subclass_set_(false),
28 interface_protocol_set_(false) {
31 UsbDeviceFilter::~UsbDeviceFilter() {
34 void UsbDeviceFilter::SetVendorId(uint16 vendor_id
) {
35 vendor_id_set_
= true;
36 vendor_id_
= vendor_id
;
39 void UsbDeviceFilter::SetProductId(uint16 product_id
) {
40 product_id_set_
= true;
41 product_id_
= product_id
;
44 void UsbDeviceFilter::SetInterfaceClass(uint8 interface_class
) {
45 interface_class_set_
= true;
46 interface_class_
= interface_class
;
49 void UsbDeviceFilter::SetInterfaceSubclass(uint8 interface_subclass
) {
50 interface_subclass_set_
= true;
51 interface_subclass_
= interface_subclass
;
54 void UsbDeviceFilter::SetInterfaceProtocol(uint8 interface_protocol
) {
55 interface_protocol_set_
= true;
56 interface_protocol_
= interface_protocol
;
59 bool UsbDeviceFilter::Matches(scoped_refptr
<UsbDevice
> device
) const {
61 if (device
->vendor_id() != vendor_id_
) {
65 if (product_id_set_
&& device
->product_id() != product_id_
) {
70 if (interface_class_set_
) {
71 for (const UsbConfigDescriptor
& config
: device
->configurations()) {
72 for (const UsbInterfaceDescriptor
& iface
: config
.interfaces
) {
73 if (iface
.interface_class
== interface_class_
&&
74 (!interface_subclass_set_
||
75 (iface
.interface_subclass
== interface_subclass_
&&
76 (!interface_protocol_set_
||
77 iface
.interface_protocol
== interface_protocol_
)))) {
89 scoped_ptr
<base::Value
> UsbDeviceFilter::ToValue() const {
90 scoped_ptr
<base::DictionaryValue
> obj(new base::DictionaryValue());
93 obj
->SetInteger(kVendorIdKey
, vendor_id_
);
94 if (product_id_set_
) {
95 obj
->SetInteger(kProductIdKey
, product_id_
);
99 if (interface_class_set_
) {
100 obj
->SetInteger(kInterfaceClassKey
, interface_class_
);
101 if (interface_subclass_set_
) {
102 obj
->SetInteger(kInterfaceSubclassKey
, interface_subclass_
);
103 if (interface_protocol_set_
) {
104 obj
->SetInteger(kInterfaceProtocolKey
, interface_protocol_
);
113 bool UsbDeviceFilter::MatchesAny(scoped_refptr
<UsbDevice
> device
,
114 const std::vector
<UsbDeviceFilter
>& filters
) {
115 for (std::vector
<UsbDeviceFilter
>::const_iterator i
= filters
.begin();
118 if (i
->Matches(device
)) {
125 } // namespace device