Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / components / usb_service / usb_device_filter.cc
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.
4
5 #include "components/usb_service/usb_device_filter.h"
6
7 #include "base/values.h"
8 #include "components/usb_service/usb_device.h"
9 #include "components/usb_service/usb_device_handle.h"
10 #include "components/usb_service/usb_interface.h"
11
12 namespace usb_service {
13
14 namespace {
15
16 const char kProductIdKey[] = "productId";
17 const char kVendorIdKey[] = "vendorId";
18 const char kInterfaceClassKey[] = "interfaceClass";
19 const char kInterfaceSubclassKey[] = "interfaceSubclass";
20 const char kInterfaceProtocolKey[] = "interfaceProtocol";
21
22 }  // namespace
23
24 UsbDeviceFilter::UsbDeviceFilter()
25     : vendor_id_set_(false),
26       product_id_set_(false),
27       interface_class_set_(false),
28       interface_subclass_set_(false),
29       interface_protocol_set_(false) {
30 }
31
32 UsbDeviceFilter::~UsbDeviceFilter() {
33 }
34
35 void UsbDeviceFilter::SetVendorId(uint16 vendor_id) {
36   vendor_id_set_ = true;
37   vendor_id_ = vendor_id;
38 }
39
40 void UsbDeviceFilter::SetProductId(uint16 product_id) {
41   product_id_set_ = true;
42   product_id_ = product_id;
43 }
44
45 void UsbDeviceFilter::SetInterfaceClass(uint8 interface_class) {
46   interface_class_set_ = true;
47   interface_class_ = interface_class;
48 }
49
50 void UsbDeviceFilter::SetInterfaceSubclass(uint8 interface_subclass) {
51   interface_subclass_set_ = true;
52   interface_subclass_ = interface_subclass;
53 }
54
55 void UsbDeviceFilter::SetInterfaceProtocol(uint8 interface_protocol) {
56   interface_protocol_set_ = true;
57   interface_protocol_ = interface_protocol;
58 }
59
60 bool UsbDeviceFilter::Matches(scoped_refptr<UsbDevice> device) {
61   if (vendor_id_set_) {
62     if (device->vendor_id() != vendor_id_) {
63       return false;
64     }
65
66     if (product_id_set_ && device->product_id() != product_id_) {
67       return false;
68     }
69   }
70
71   if (interface_class_set_) {
72     bool foundMatch = false;
73     scoped_refptr<const UsbConfigDescriptor> config = device->ListInterfaces();
74
75     // TODO(reillyg): Check device configuration if the class is not defined at
76     // a per-interface level. This is not really important because most devices
77     // have per-interface classes. The only counter-examples I know of are hubs.
78
79     for (size_t i = 0; i < config->GetNumInterfaces() && !foundMatch; ++i) {
80       scoped_refptr<const UsbInterfaceDescriptor> iface =
81           config->GetInterface(i);
82
83       for (size_t j = 0; j < iface->GetNumAltSettings() && !foundMatch; ++j) {
84         scoped_refptr<const UsbInterfaceAltSettingDescriptor> altSetting =
85             iface->GetAltSetting(j);
86
87         if (altSetting->GetInterfaceClass() == interface_class_ &&
88             (!interface_subclass_set_ ||
89              (altSetting->GetInterfaceSubclass() == interface_subclass_ &&
90               (!interface_protocol_set_ ||
91                altSetting->GetInterfaceProtocol() == interface_protocol_)))) {
92           foundMatch = true;
93         }
94       }
95     }
96
97     if (!foundMatch) {
98       return false;
99     }
100   }
101
102   return true;
103 }
104
105 base::Value* UsbDeviceFilter::ToValue() const {
106   scoped_ptr<base::DictionaryValue> obj(new base::DictionaryValue());
107
108   if (vendor_id_set_) {
109     obj->SetInteger(kVendorIdKey, vendor_id_);
110     if (product_id_set_) {
111       obj->SetInteger(kProductIdKey, product_id_);
112     }
113   }
114
115   if (interface_class_set_) {
116     obj->SetInteger(kInterfaceClassKey, interface_class_);
117     if (interface_subclass_set_) {
118       obj->SetInteger(kInterfaceSubclassKey, interface_subclass_);
119       if (interface_protocol_set_) {
120         obj->SetInteger(kInterfaceProtocolKey, interface_protocol_);
121       }
122     }
123   }
124
125   return obj.release();
126 }
127
128 }  // namespace usb_service