| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/common/permissions/usb_device_permission.h" | 5 #include "extensions/common/permissions/usb_device_permission.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "device/usb/usb_ids.h" | 15 #include "device/usb/usb_ids.h" |
| 16 #include "extensions/common/permissions/permissions_info.h" | 16 #include "extensions/common/permissions/permissions_info.h" |
| 17 #include "grit/extensions_strings.h" | 17 #include "grit/extensions_strings.h" |
| 18 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
| 19 | 19 |
| 20 namespace extensions { | 20 namespace extensions { |
| 21 | 21 |
| 22 UsbDevicePermission::UsbDevicePermission( | 22 namespace { |
| 23 const APIPermissionInfo* info) | |
| 24 : SetDisjunctionPermission<UsbDevicePermissionData, | |
| 25 UsbDevicePermission>(info) { | |
| 26 } | |
| 27 | 23 |
| 28 UsbDevicePermission::~UsbDevicePermission() { | 24 // Adds the permissions from the |data_set| to the permission lists that are |
| 29 } | 25 // not NULL. If NULL, that list is ignored. |
| 30 | 26 void AddPermissionsToLists(const std::set<UsbDevicePermissionData>& data_set, |
| 31 PermissionMessages UsbDevicePermission::GetMessages() const { | 27 PermissionIDSet* ids, |
| 32 DCHECK(HasMessages()); | 28 PermissionMessages* messages) { |
| 33 PermissionMessages result; | 29 // TODO(sashab): Once GetMessages() is deprecated, move this logic back into |
| 34 | 30 // GetPermissions(). |
| 35 if (data_set_.size() == 1) { | 31 // TODO(sashab, reillyg): Once GetMessages() is deprecated, rework the |
| 36 const UsbDevicePermissionData& data = *data_set_.begin(); | 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 | 36 |
| 38 const char* vendor = device::UsbIds::GetVendorName(data.vendor_id()); | 37 const char* vendor = device::UsbIds::GetVendorName(data.vendor_id()); |
| 39 if (vendor) { | 38 if (vendor) { |
| 40 const char* product = | 39 const char* product = |
| 41 device::UsbIds::GetProductName(data.vendor_id(), data.product_id()); | 40 device::UsbIds::GetProductName(data.vendor_id(), data.product_id()); |
| 42 if (product) { | 41 if (product) { |
| 43 result.push_back(PermissionMessage( | 42 base::string16 product_name_and_vendor = l10n_util::GetStringFUTF16( |
| 44 PermissionMessage::kUsbDevice, | 43 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_PRODUCT_NAME_AND_VENDOR, |
| 45 l10n_util::GetStringFUTF16(IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE, | 44 base::UTF8ToUTF16(product), base::UTF8ToUTF16(vendor)); |
| 46 base::UTF8ToUTF16(product), | 45 |
| 47 base::UTF8ToUTF16(vendor)))); | 46 if (messages) { |
| 47 messages->push_back( |
| 48 PermissionMessage(PermissionMessage::kUsbDevice, |
| 49 l10n_util::GetStringFUTF16( |
| 50 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE, |
| 51 product_name_and_vendor))); |
| 52 } |
| 53 if (ids) |
| 54 ids->insert(APIPermission::kUsbDevice, product_name_and_vendor); |
| 48 } else { | 55 } else { |
| 49 result.push_back(PermissionMessage( | 56 if (messages) { |
| 50 PermissionMessage::kUsbDevice, | 57 messages->push_back(PermissionMessage( |
| 51 l10n_util::GetStringFUTF16( | 58 PermissionMessage::kUsbDevice, |
| 52 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_UNKNOWN_PRODUCT, | 59 l10n_util::GetStringFUTF16( |
| 53 base::UTF8ToUTF16(vendor)))); | 60 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_UNKNOWN_PRODUCT, |
| 61 base::UTF8ToUTF16(vendor)))); |
| 62 } |
| 63 if (ids) { |
| 64 ids->insert(APIPermission::kUsbDeviceUnknownProduct, |
| 65 base::UTF8ToUTF16(vendor)); |
| 66 } |
| 54 } | 67 } |
| 55 } else { | 68 } else { |
| 56 result.push_back(PermissionMessage( | 69 if (messages) { |
| 57 PermissionMessage::kUsbDevice, | 70 messages->push_back(PermissionMessage( |
| 58 l10n_util::GetStringUTF16( | 71 PermissionMessage::kUsbDevice, |
| 59 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_UNKNOWN_VENDOR))); | 72 l10n_util::GetStringUTF16( |
| 73 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_UNKNOWN_VENDOR))); |
| 74 } |
| 75 if (ids) { |
| 76 ids->insert(APIPermission::kUsbDeviceUnknownVendor); |
| 60 } | 77 } |
| 61 } else if (data_set_.size() > 1) { | 78 } |
| 79 } else if (data_set.size() > 1) { |
| 62 std::vector<base::string16> details; | 80 std::vector<base::string16> details; |
| 63 std::set<uint16> unknown_product_vendors; | 81 std::set<uint16> unknown_product_vendors; |
| 64 bool found_unknown_vendor = false; | 82 bool found_unknown_vendor = false; |
| 65 | 83 |
| 66 for (const UsbDevicePermissionData& data : data_set_) { | 84 for (const UsbDevicePermissionData& data : data_set) { |
| 67 const char* vendor = device::UsbIds::GetVendorName(data.vendor_id()); | 85 const char* vendor = device::UsbIds::GetVendorName(data.vendor_id()); |
| 68 if (vendor) { | 86 if (vendor) { |
| 69 const char* product = | 87 const char* product = |
| 70 device::UsbIds::GetProductName(data.vendor_id(), data.product_id()); | 88 device::UsbIds::GetProductName(data.vendor_id(), data.product_id()); |
| 71 if (product) { | 89 if (product) { |
| 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)); |
| 72 details.push_back(l10n_util::GetStringFUTF16( | 93 details.push_back(l10n_util::GetStringFUTF16( |
| 73 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_LIST_ITEM, | 94 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_LIST_ITEM, |
| 74 base::UTF8ToUTF16(product), base::UTF8ToUTF16(vendor))); | 95 product_name_and_vendor)); |
| 75 } else { | 96 } else { |
| 76 unknown_product_vendors.insert(data.vendor_id()); | 97 unknown_product_vendors.insert(data.vendor_id()); |
| 77 } | 98 } |
| 78 } else { | 99 } else { |
| 79 found_unknown_vendor = true; | 100 found_unknown_vendor = true; |
| 80 } | 101 } |
| 81 } | 102 } |
| 82 | 103 |
| 83 // List generic "devices from this vendor" entries after specific devices. | 104 // List generic "devices from this vendor" entries after specific devices. |
| 84 for (const uint16& vendor_id : unknown_product_vendors) { | 105 for (const uint16& vendor_id : unknown_product_vendors) { |
| 85 const char* vendor = device::UsbIds::GetVendorName(vendor_id); | 106 const char* vendor = device::UsbIds::GetVendorName(vendor_id); |
| 86 DCHECK(vendor); | 107 DCHECK(vendor); |
| 87 details.push_back(l10n_util::GetStringFUTF16( | 108 details.push_back(l10n_util::GetStringFUTF16( |
| 88 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_LIST_ITEM_UNKNOWN_PRODUCT, | 109 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_LIST_ITEM_UNKNOWN_PRODUCT, |
| 89 base::UTF8ToUTF16(vendor))); | 110 base::UTF8ToUTF16(vendor))); |
| 90 } | 111 } |
| 91 | 112 |
| 92 // Display the catch all "device from an unknown vendor" last. | 113 // Display the catch all "device from an unknown vendor" last. |
| 93 if (found_unknown_vendor) { | 114 if (found_unknown_vendor) { |
| 94 details.push_back(l10n_util::GetStringUTF16( | 115 details.push_back(l10n_util::GetStringUTF16( |
| 95 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_LIST_ITEM_UNKNOWN_VENDOR)); | 116 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_LIST_ITEM_UNKNOWN_VENDOR)); |
| 96 } | 117 } |
| 97 | 118 |
| 98 result.push_back(PermissionMessage( | 119 if (messages) { |
| 99 PermissionMessage::kUsbDevice, | 120 messages->push_back( |
| 100 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_LIST), | 121 PermissionMessage(PermissionMessage::kUsbDevice, |
| 101 JoinString(details, base::char16('\n')))); | 122 l10n_util::GetStringUTF16( |
| 123 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_LIST), |
| 124 JoinString(details, base::char16('\n')))); |
| 125 } |
| 126 if (ids) { |
| 127 for (const auto& detail : details) |
| 128 ids->insert(APIPermission::kUsbDeviceList, detail); |
| 129 } |
| 102 } | 130 } |
| 131 } |
| 103 | 132 |
| 104 return result; | 133 } // namespace |
| 134 |
| 135 UsbDevicePermission::UsbDevicePermission(const APIPermissionInfo* info) |
| 136 : SetDisjunctionPermission<UsbDevicePermissionData, UsbDevicePermission>( |
| 137 info) { |
| 138 } |
| 139 |
| 140 UsbDevicePermission::~UsbDevicePermission() { |
| 141 } |
| 142 |
| 143 PermissionIDSet UsbDevicePermission::GetPermissions() const { |
| 144 PermissionIDSet ids; |
| 145 AddPermissionsToLists(data_set_, &ids, NULL); |
| 146 return ids; |
| 147 } |
| 148 |
| 149 PermissionMessages UsbDevicePermission::GetMessages() const { |
| 150 DCHECK(HasMessages()); |
| 151 PermissionMessages messages; |
| 152 AddPermissionsToLists(data_set_, NULL, &messages); |
| 153 return messages; |
| 105 } | 154 } |
| 106 | 155 |
| 107 } // namespace extensions | 156 } // namespace extensions |
| OLD | NEW |