Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / extensions / common / permissions / usb_device_permission.cc
blob851fbd32599a3ff92507f87ed61bbefd70051d0c
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(
23 const APIPermissionInfo* info)
24 : SetDisjunctionPermission<UsbDevicePermissionData,
25 UsbDevicePermission>(info) {
28 UsbDevicePermission::~UsbDevicePermission() {
31 PermissionMessages UsbDevicePermission::GetMessages() const {
32 DCHECK(HasMessages());
33 PermissionMessages result;
35 if (data_set_.size() == 1) {
36 const UsbDevicePermissionData& data = *data_set_.begin();
38 const char* vendor = device::UsbIds::GetVendorName(data.vendor_id());
39 if (vendor) {
40 const char* product =
41 device::UsbIds::GetProductName(data.vendor_id(), data.product_id());
42 if (product) {
43 result.push_back(PermissionMessage(
44 PermissionMessage::kUsbDevice,
45 l10n_util::GetStringFUTF16(IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE,
46 base::UTF8ToUTF16(product),
47 base::UTF8ToUTF16(vendor))));
48 } else {
49 result.push_back(PermissionMessage(
50 PermissionMessage::kUsbDevice,
51 l10n_util::GetStringFUTF16(
52 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_UNKNOWN_PRODUCT,
53 base::UTF8ToUTF16(vendor))));
55 } else {
56 result.push_back(PermissionMessage(
57 PermissionMessage::kUsbDevice,
58 l10n_util::GetStringUTF16(
59 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_UNKNOWN_VENDOR)));
61 } else if (data_set_.size() > 1) {
62 std::vector<base::string16> details;
63 std::set<uint16> unknown_product_vendors;
64 bool found_unknown_vendor = false;
66 for (const UsbDevicePermissionData& data : data_set_) {
67 const char* vendor = device::UsbIds::GetVendorName(data.vendor_id());
68 if (vendor) {
69 const char* product =
70 device::UsbIds::GetProductName(data.vendor_id(), data.product_id());
71 if (product) {
72 details.push_back(l10n_util::GetStringFUTF16(
73 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_LIST_ITEM,
74 base::UTF8ToUTF16(product), base::UTF8ToUTF16(vendor)));
75 } else {
76 unknown_product_vendors.insert(data.vendor_id());
78 } else {
79 found_unknown_vendor = true;
83 // List generic "devices from this vendor" entries after specific devices.
84 for (const uint16& vendor_id : unknown_product_vendors) {
85 const char* vendor = device::UsbIds::GetVendorName(vendor_id);
86 DCHECK(vendor);
87 details.push_back(l10n_util::GetStringFUTF16(
88 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_LIST_ITEM_UNKNOWN_PRODUCT,
89 base::UTF8ToUTF16(vendor)));
92 // Display the catch all "device from an unknown vendor" last.
93 if (found_unknown_vendor) {
94 details.push_back(l10n_util::GetStringUTF16(
95 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_LIST_ITEM_UNKNOWN_VENDOR));
98 result.push_back(PermissionMessage(
99 PermissionMessage::kUsbDevice,
100 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_LIST),
101 JoinString(details, base::char16('\n'))));
104 return result;
107 } // namespace extensions