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 "extensions/common/permissions/usb_device_permission_data.h"
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_split.h"
14 #include "base/values.h"
15 #include "extensions/common/permissions/api_permission.h"
16 #include "extensions/common/permissions/usb_device_permission.h"
20 const char* kProductIdKey
= "productId";
21 const char* kVendorIdKey
= "vendorId";
22 const char* kInterfaceIdKey
= "interfaceId";
26 namespace extensions
{
28 UsbDevicePermissionData::UsbDevicePermissionData()
29 : vendor_id_(0), product_id_(0), interface_id_(ANY_INTERFACE
) {
32 UsbDevicePermissionData::UsbDevicePermissionData(uint16 vendor_id
,
35 : vendor_id_(vendor_id
),
36 product_id_(product_id
),
37 interface_id_(interface_id
) {
40 bool UsbDevicePermissionData::Check(
41 const APIPermission::CheckParam
* param
) const {
44 const UsbDevicePermission::CheckParam
& specific_param
=
45 *static_cast<const UsbDevicePermission::CheckParam
*>(param
);
46 return vendor_id_
== specific_param
.vendor_id
&&
47 product_id_
== specific_param
.product_id
&&
48 (specific_param
.interface_id
== UNSPECIFIED_INTERFACE
||
49 interface_id_
== specific_param
.interface_id
);
52 scoped_ptr
<base::Value
> UsbDevicePermissionData::ToValue() const {
53 base::DictionaryValue
* result
= new base::DictionaryValue();
54 result
->SetInteger(kVendorIdKey
, vendor_id_
);
55 result
->SetInteger(kProductIdKey
, product_id_
);
56 result
->SetInteger(kInterfaceIdKey
, interface_id_
);
57 return scoped_ptr
<base::Value
>(result
);
60 bool UsbDevicePermissionData::FromValue(const base::Value
* value
) {
64 const base::DictionaryValue
* dict_value
;
65 if (!value
->GetAsDictionary(&dict_value
))
69 if (!dict_value
->GetInteger(kVendorIdKey
, &temp
))
71 if (temp
< 0 || temp
> kuint16max
)
75 if (!dict_value
->GetInteger(kProductIdKey
, &temp
))
77 if (temp
< 0 || temp
> kuint16max
)
81 if (!dict_value
->GetInteger(kInterfaceIdKey
, &temp
))
82 interface_id_
= ANY_INTERFACE
;
83 else if (temp
< ANY_INTERFACE
|| temp
> kuint8max
)
91 bool UsbDevicePermissionData::operator<(
92 const UsbDevicePermissionData
& rhs
) const {
93 if (vendor_id_
== rhs
.vendor_id_
) {
94 if (product_id_
== rhs
.product_id_
)
95 return interface_id_
< rhs
.interface_id_
;
97 return product_id_
< rhs
.product_id_
;
99 return vendor_id_
< rhs
.vendor_id_
;
102 bool UsbDevicePermissionData::operator==(
103 const UsbDevicePermissionData
& rhs
) const {
104 return vendor_id_
== rhs
.vendor_id_
&&
105 product_id_
== rhs
.product_id_
&&
106 interface_id_
== rhs
.interface_id_
;
109 } // namespace extensions