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"
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());
41 device::UsbIds::GetProductName(data
.vendor_id(), data
.product_id());
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
))));
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
))));
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());
70 device::UsbIds::GetProductName(data
.vendor_id(), data
.product_id());
72 details
.push_back(l10n_util::GetStringFUTF16(
73 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_LIST_ITEM
,
74 base::UTF8ToUTF16(product
), base::UTF8ToUTF16(vendor
)));
76 unknown_product_vendors
.insert(data
.vendor_id());
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
);
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'))));
107 } // namespace extensions