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
{
24 // Adds the permissions from the |data_set| to the permission lists that are
25 // not NULL. If NULL, that list is ignored.
26 void AddPermissionsToLists(const std::set
<UsbDevicePermissionData
>& data_set
,
28 PermissionMessages
* messages
) {
29 // TODO(sashab): Once GetMessages() is deprecated, move this logic back into
31 // TODO(sashab, reillyg): Once GetMessages() is deprecated, rework the
32 // permission message logic for USB devices to generate more meaningful
33 // messages and better fit the current rules system.
34 if (data_set
.size() == 1) {
35 const UsbDevicePermissionData
& data
= *data_set
.begin();
37 const char* vendor
= device::UsbIds::GetVendorName(data
.vendor_id());
40 device::UsbIds::GetProductName(data
.vendor_id(), data
.product_id());
42 base::string16 product_name_and_vendor
= l10n_util::GetStringFUTF16(
43 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_PRODUCT_NAME_AND_VENDOR
,
44 base::UTF8ToUTF16(product
), base::UTF8ToUTF16(vendor
));
48 PermissionMessage(PermissionMessage::kUsbDevice
,
49 l10n_util::GetStringFUTF16(
50 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE
,
51 product_name_and_vendor
)));
54 ids
->insert(APIPermission::kUsbDevice
, product_name_and_vendor
);
57 messages
->push_back(PermissionMessage(
58 PermissionMessage::kUsbDevice
,
59 l10n_util::GetStringFUTF16(
60 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_UNKNOWN_PRODUCT
,
61 base::UTF8ToUTF16(vendor
))));
64 ids
->insert(APIPermission::kUsbDeviceUnknownProduct
,
65 base::UTF8ToUTF16(vendor
));
70 messages
->push_back(PermissionMessage(
71 PermissionMessage::kUsbDevice
,
72 l10n_util::GetStringUTF16(
73 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_UNKNOWN_VENDOR
)));
76 ids
->insert(APIPermission::kUsbDeviceUnknownVendor
);
79 } else if (data_set
.size() > 1) {
80 std::vector
<base::string16
> details
;
81 std::set
<uint16
> unknown_product_vendors
;
82 bool found_unknown_vendor
= false;
84 for (const UsbDevicePermissionData
& data
: data_set
) {
85 const char* vendor
= device::UsbIds::GetVendorName(data
.vendor_id());
88 device::UsbIds::GetProductName(data
.vendor_id(), data
.product_id());
90 base::string16 product_name_and_vendor
= l10n_util::GetStringFUTF16(
91 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_PRODUCT_NAME_AND_VENDOR
,
92 base::UTF8ToUTF16(product
), base::UTF8ToUTF16(vendor
));
93 details
.push_back(l10n_util::GetStringFUTF16(
94 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_LIST_ITEM
,
95 product_name_and_vendor
));
97 unknown_product_vendors
.insert(data
.vendor_id());
100 found_unknown_vendor
= true;
104 // List generic "devices from this vendor" entries after specific devices.
105 for (const uint16
& vendor_id
: unknown_product_vendors
) {
106 const char* vendor
= device::UsbIds::GetVendorName(vendor_id
);
108 details
.push_back(l10n_util::GetStringFUTF16(
109 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_LIST_ITEM_UNKNOWN_PRODUCT
,
110 base::UTF8ToUTF16(vendor
)));
113 // Display the catch all "device from an unknown vendor" last.
114 if (found_unknown_vendor
) {
115 details
.push_back(l10n_util::GetStringUTF16(
116 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_LIST_ITEM_UNKNOWN_VENDOR
));
120 messages
->push_back(PermissionMessage(
121 PermissionMessage::kUsbDevice
,
122 l10n_util::GetStringUTF16(
123 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_LIST
),
124 base::JoinString(details
, base::ASCIIToUTF16("\n"))));
127 for (const auto& detail
: details
)
128 ids
->insert(APIPermission::kUsbDeviceList
, detail
);
135 UsbDevicePermission::UsbDevicePermission(const APIPermissionInfo
* info
)
136 : SetDisjunctionPermission
<UsbDevicePermissionData
, UsbDevicePermission
>(
140 UsbDevicePermission::~UsbDevicePermission() {
143 PermissionIDSet
UsbDevicePermission::GetPermissions() const {
145 AddPermissionsToLists(data_set_
, &ids
, NULL
);
149 PermissionMessages
UsbDevicePermission::GetMessages() const {
150 DCHECK(HasMessages());
151 PermissionMessages messages
;
152 AddPermissionsToLists(data_set_
, NULL
, &messages
);
156 } // namespace extensions