[Extensions Page] Fix focus when an extension is removed
[chromium-blink-merge.git] / device / usb / usb_device_filter.cc
blobb52a4a3edc9af7af8603b80288d8181ce942441a
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"
11 namespace device {
13 namespace {
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";
21 } // namespace
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 {
60 if (vendor_id_set_) {
61 if (device->vendor_id() != vendor_id_) {
62 return false;
65 if (product_id_set_ && device->product_id() != product_id_) {
66 return false;
70 if (interface_class_set_) {
71 const UsbConfigDescriptor* config = device->GetConfiguration();
72 if (!config) {
73 return false;
76 // TODO(reillyg): Check device configuration if the class is not defined at
77 // a per-interface level. This is not really important because most devices
78 // have per-interface classes. The only counter-examples I know of are hubs.
80 bool foundMatch = false;
81 for (const UsbInterfaceDescriptor& iface : config->interfaces) {
82 if (iface.interface_class == interface_class_ &&
83 (!interface_subclass_set_ ||
84 (iface.interface_subclass == interface_subclass_ &&
85 (!interface_protocol_set_ ||
86 iface.interface_protocol == interface_protocol_)))) {
87 foundMatch = true;
91 if (!foundMatch) {
92 return false;
96 return true;
99 scoped_ptr<base::Value> UsbDeviceFilter::ToValue() const {
100 scoped_ptr<base::DictionaryValue> obj(new base::DictionaryValue());
102 if (vendor_id_set_) {
103 obj->SetInteger(kVendorIdKey, vendor_id_);
104 if (product_id_set_) {
105 obj->SetInteger(kProductIdKey, product_id_);
109 if (interface_class_set_) {
110 obj->SetInteger(kInterfaceClassKey, interface_class_);
111 if (interface_subclass_set_) {
112 obj->SetInteger(kInterfaceSubclassKey, interface_subclass_);
113 if (interface_protocol_set_) {
114 obj->SetInteger(kInterfaceProtocolKey, interface_protocol_);
119 return obj.Pass();
122 // static
123 bool UsbDeviceFilter::MatchesAny(scoped_refptr<UsbDevice> device,
124 const std::vector<UsbDeviceFilter>& filters) {
125 for (std::vector<UsbDeviceFilter>::const_iterator i = filters.begin();
126 i != filters.end();
127 ++i) {
128 if (i->Matches(device)) {
129 return true;
132 return false;
135 } // namespace device