Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / extensions / common / permissions / usb_device_permission.cc
bloba79d47f109b46e25759e88a594780795cb547394
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.h"
7 #include <set>
8 #include <string>
10 #include "base/logging.h"
11 #include "base/strings/string16.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "device/usb/usb_ids.h"
16 #include "extensions/common/permissions/permissions_info.h"
17 #include "grit/extensions_strings.h"
18 #include "ui/base/l10n/l10n_util.h"
20 namespace extensions {
22 UsbDevicePermission::UsbDevicePermission(const APIPermissionInfo* info)
23 : SetDisjunctionPermission<UsbDevicePermissionData, UsbDevicePermission>(
24 info) {}
26 UsbDevicePermission::~UsbDevicePermission() {}
28 PermissionIDSet UsbDevicePermission::GetPermissions() const {
29 PermissionIDSet ids;
31 std::set<uint16> unknown_product_vendors;
32 bool found_unknown_vendor = false;
34 for (const UsbDevicePermissionData& data : data_set_) {
35 const char* vendor = device::UsbIds::GetVendorName(data.vendor_id());
36 if (vendor) {
37 const char* product =
38 device::UsbIds::GetProductName(data.vendor_id(), data.product_id());
39 if (product) {
40 base::string16 product_name_and_vendor = l10n_util::GetStringFUTF16(
41 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_PRODUCT_NAME_AND_VENDOR,
42 base::UTF8ToUTF16(product), base::UTF8ToUTF16(vendor));
43 ids.insert(APIPermission::kUsbDevice, product_name_and_vendor);
44 } else {
45 unknown_product_vendors.insert(data.vendor_id());
47 } else {
48 found_unknown_vendor = true;
52 for (uint16 vendor_id : unknown_product_vendors) {
53 const char* vendor = device::UsbIds::GetVendorName(vendor_id);
54 DCHECK(vendor);
55 ids.insert(APIPermission::kUsbDeviceUnknownProduct,
56 base::UTF8ToUTF16(vendor));
59 if (found_unknown_vendor)
60 ids.insert(APIPermission::kUsbDeviceUnknownVendor);
62 return ids;
65 } // namespace extensions