Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / extensions / common / permissions / usb_device_permission_data.cc
blob532886d79ef43243234275f0ed3775e06f720002
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"
7 #include <string>
8 #include <vector>
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"
18 namespace {
20 const char* kProductIdKey = "productId";
21 const char* kVendorIdKey = "vendorId";
22 const char* kInterfaceIdKey = "interfaceId";
24 } // namespace
26 namespace extensions {
28 UsbDevicePermissionData::UsbDevicePermissionData()
29 : vendor_id_(0), product_id_(0), interface_id_(ANY_INTERFACE) {
32 UsbDevicePermissionData::UsbDevicePermissionData(uint16 vendor_id,
33 uint16 product_id,
34 int interface_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 {
42 if (!param)
43 return false;
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) {
61 if (!value)
62 return false;
64 const base::DictionaryValue* dict_value;
65 if (!value->GetAsDictionary(&dict_value))
66 return false;
68 int temp;
69 if (!dict_value->GetInteger(kVendorIdKey, &temp))
70 return false;
71 if (temp < 0 || temp > kuint16max)
72 return false;
73 vendor_id_ = temp;
75 if (!dict_value->GetInteger(kProductIdKey, &temp))
76 return false;
77 if (temp < 0 || temp > kuint16max)
78 return false;
79 product_id_ = temp;
81 if (!dict_value->GetInteger(kInterfaceIdKey, &temp))
82 interface_id_ = ANY_INTERFACE;
83 else if (temp < ANY_INTERFACE || temp > kuint8max)
84 return false;
85 else
86 interface_id_ = temp;
88 return true;
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