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 "extensions/common/permissions/usb_device_permission_data.h" | 5 #include "extensions/common/permissions/usb_device_permission_data.h" |
6 | 6 |
| 7 #include <limits> |
7 #include <string> | 8 #include <string> |
8 #include <tuple> | 9 #include <tuple> |
9 #include <vector> | 10 #include <vector> |
10 | 11 |
11 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
13 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
14 #include "base/strings/string_split.h" | 15 #include "base/strings/string_split.h" |
15 #include "base/values.h" | 16 #include "base/values.h" |
16 #include "extensions/common/permissions/api_permission.h" | 17 #include "extensions/common/permissions/api_permission.h" |
17 #include "extensions/common/permissions/usb_device_permission.h" | 18 #include "extensions/common/permissions/usb_device_permission.h" |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 const char* kProductIdKey = "productId"; | 22 const char* kProductIdKey = "productId"; |
22 const char* kVendorIdKey = "vendorId"; | 23 const char* kVendorIdKey = "vendorId"; |
23 const char* kInterfaceIdKey = "interfaceId"; | 24 const char* kInterfaceIdKey = "interfaceId"; |
24 | 25 |
25 } // namespace | 26 } // namespace |
26 | 27 |
27 namespace extensions { | 28 namespace extensions { |
28 | 29 |
29 UsbDevicePermissionData::UsbDevicePermissionData() | 30 UsbDevicePermissionData::UsbDevicePermissionData() |
30 : vendor_id_(0), product_id_(0), interface_id_(ANY_INTERFACE) { | 31 : vendor_id_(0), product_id_(0), interface_id_(ANY_INTERFACE) { |
31 } | 32 } |
32 | 33 |
33 UsbDevicePermissionData::UsbDevicePermissionData(uint16 vendor_id, | 34 UsbDevicePermissionData::UsbDevicePermissionData(uint16_t vendor_id, |
34 uint16 product_id, | 35 uint16_t product_id, |
35 int interface_id) | 36 int interface_id) |
36 : vendor_id_(vendor_id), | 37 : vendor_id_(vendor_id), |
37 product_id_(product_id), | 38 product_id_(product_id), |
38 interface_id_(interface_id) { | 39 interface_id_(interface_id) {} |
39 } | |
40 | 40 |
41 bool UsbDevicePermissionData::Check( | 41 bool UsbDevicePermissionData::Check( |
42 const APIPermission::CheckParam* param) const { | 42 const APIPermission::CheckParam* param) const { |
43 if (!param) | 43 if (!param) |
44 return false; | 44 return false; |
45 const UsbDevicePermission::CheckParam& specific_param = | 45 const UsbDevicePermission::CheckParam& specific_param = |
46 *static_cast<const UsbDevicePermission::CheckParam*>(param); | 46 *static_cast<const UsbDevicePermission::CheckParam*>(param); |
47 return vendor_id_ == specific_param.vendor_id && | 47 return vendor_id_ == specific_param.vendor_id && |
48 product_id_ == specific_param.product_id && | 48 product_id_ == specific_param.product_id && |
49 (specific_param.interface_id == UNSPECIFIED_INTERFACE || | 49 (specific_param.interface_id == UNSPECIFIED_INTERFACE || |
(...skipping 12 matching lines...) Expand all Loading... |
62 if (!value) | 62 if (!value) |
63 return false; | 63 return false; |
64 | 64 |
65 const base::DictionaryValue* dict_value; | 65 const base::DictionaryValue* dict_value; |
66 if (!value->GetAsDictionary(&dict_value)) | 66 if (!value->GetAsDictionary(&dict_value)) |
67 return false; | 67 return false; |
68 | 68 |
69 int temp; | 69 int temp; |
70 if (!dict_value->GetInteger(kVendorIdKey, &temp)) | 70 if (!dict_value->GetInteger(kVendorIdKey, &temp)) |
71 return false; | 71 return false; |
72 if (temp < 0 || temp > kuint16max) | 72 if (temp < 0 || temp > std::numeric_limits<uint16_t>::max()) |
73 return false; | 73 return false; |
74 vendor_id_ = temp; | 74 vendor_id_ = temp; |
75 | 75 |
76 if (!dict_value->GetInteger(kProductIdKey, &temp)) | 76 if (!dict_value->GetInteger(kProductIdKey, &temp)) |
77 return false; | 77 return false; |
78 if (temp < 0 || temp > kuint16max) | 78 if (temp < 0 || temp > std::numeric_limits<uint16_t>::max()) |
79 return false; | 79 return false; |
80 product_id_ = temp; | 80 product_id_ = temp; |
81 | 81 |
82 if (!dict_value->GetInteger(kInterfaceIdKey, &temp)) | 82 if (!dict_value->GetInteger(kInterfaceIdKey, &temp)) |
83 interface_id_ = ANY_INTERFACE; | 83 interface_id_ = ANY_INTERFACE; |
84 else if (temp < ANY_INTERFACE || temp > kuint8max) | 84 else if (temp < ANY_INTERFACE || temp > std::numeric_limits<uint8_t>::max()) |
85 return false; | 85 return false; |
86 else | 86 else |
87 interface_id_ = temp; | 87 interface_id_ = temp; |
88 | 88 |
89 return true; | 89 return true; |
90 } | 90 } |
91 | 91 |
92 bool UsbDevicePermissionData::operator<( | 92 bool UsbDevicePermissionData::operator<( |
93 const UsbDevicePermissionData& rhs) const { | 93 const UsbDevicePermissionData& rhs) const { |
94 return std::tie(vendor_id_, product_id_, interface_id_) < | 94 return std::tie(vendor_id_, product_id_, interface_id_) < |
95 std::tie(rhs.vendor_id_, rhs.product_id_, rhs.interface_id_); | 95 std::tie(rhs.vendor_id_, rhs.product_id_, rhs.interface_id_); |
96 } | 96 } |
97 | 97 |
98 bool UsbDevicePermissionData::operator==( | 98 bool UsbDevicePermissionData::operator==( |
99 const UsbDevicePermissionData& rhs) const { | 99 const UsbDevicePermissionData& rhs) const { |
100 return vendor_id_ == rhs.vendor_id_ && | 100 return vendor_id_ == rhs.vendor_id_ && |
101 product_id_ == rhs.product_id_ && | 101 product_id_ == rhs.product_id_ && |
102 interface_id_ == rhs.interface_id_; | 102 interface_id_ == rhs.interface_id_; |
103 } | 103 } |
104 | 104 |
105 } // namespace extensions | 105 } // namespace extensions |
OLD | NEW |