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 namespace { | 22 namespace { |
23 | 23 |
24 // Adds the permissions from the |data_set| to the permission lists that are | 24 // Adds the permissions from the |data_set| to |ids|. |
25 // not NULL. If NULL, that list is ignored. | |
26 void AddPermissionsToLists(const std::set<UsbDevicePermissionData>& data_set, | 25 void AddPermissionsToLists(const std::set<UsbDevicePermissionData>& data_set, |
27 PermissionIDSet* ids, | 26 PermissionIDSet* ids) { |
28 PermissionMessages* messages) { | |
29 // TODO(sashab): Once GetMessages() is deprecated, move this logic back into | 27 // TODO(sashab): Once GetMessages() is deprecated, move this logic back into |
30 // GetPermissions(). | 28 // GetPermissions(). |
31 // TODO(sashab, reillyg): Once GetMessages() is deprecated, rework the | 29 // TODO(sashab, reillyg): Once GetMessages() is deprecated, rework the |
32 // permission message logic for USB devices to generate more meaningful | 30 // permission message logic for USB devices to generate more meaningful |
33 // messages and better fit the current rules system. | 31 // messages and better fit the current rules system. |
34 if (data_set.size() == 1) { | 32 if (data_set.size() == 1) { |
35 const UsbDevicePermissionData& data = *data_set.begin(); | 33 const UsbDevicePermissionData& data = *data_set.begin(); |
36 | 34 |
37 const char* vendor = device::UsbIds::GetVendorName(data.vendor_id()); | 35 const char* vendor = device::UsbIds::GetVendorName(data.vendor_id()); |
38 if (vendor) { | 36 if (vendor) { |
39 const char* product = | 37 const char* product = |
40 device::UsbIds::GetProductName(data.vendor_id(), data.product_id()); | 38 device::UsbIds::GetProductName(data.vendor_id(), data.product_id()); |
41 if (product) { | 39 if (product) { |
42 base::string16 product_name_and_vendor = l10n_util::GetStringFUTF16( | 40 base::string16 product_name_and_vendor = l10n_util::GetStringFUTF16( |
43 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_PRODUCT_NAME_AND_VENDOR, | 41 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_PRODUCT_NAME_AND_VENDOR, |
44 base::UTF8ToUTF16(product), base::UTF8ToUTF16(vendor)); | 42 base::UTF8ToUTF16(product), base::UTF8ToUTF16(vendor)); |
45 | 43 |
46 if (messages) { | 44 ids->insert(APIPermission::kUsbDevice, product_name_and_vendor); |
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); | |
55 } else { | 45 } else { |
56 if (messages) { | 46 ids->insert(APIPermission::kUsbDeviceUnknownProduct, |
57 messages->push_back(PermissionMessage( | 47 base::UTF8ToUTF16(vendor)); |
58 PermissionMessage::kUsbDevice, | |
59 l10n_util::GetStringFUTF16( | |
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 } | |
67 } | 48 } |
68 } else { | 49 } else { |
69 if (messages) { | 50 ids->insert(APIPermission::kUsbDeviceUnknownVendor); |
70 messages->push_back(PermissionMessage( | |
71 PermissionMessage::kUsbDevice, | |
72 l10n_util::GetStringUTF16( | |
73 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_UNKNOWN_VENDOR))); | |
74 } | |
75 if (ids) { | |
76 ids->insert(APIPermission::kUsbDeviceUnknownVendor); | |
77 } | |
78 } | 51 } |
79 } else if (data_set.size() > 1) { | 52 } else if (data_set.size() > 1) { |
80 std::vector<base::string16> details; | 53 std::vector<base::string16> details; |
81 std::set<uint16> unknown_product_vendors; | 54 std::set<uint16> unknown_product_vendors; |
82 bool found_unknown_vendor = false; | 55 bool found_unknown_vendor = false; |
83 | 56 |
84 for (const UsbDevicePermissionData& data : data_set) { | 57 for (const UsbDevicePermissionData& data : data_set) { |
85 const char* vendor = device::UsbIds::GetVendorName(data.vendor_id()); | 58 const char* vendor = device::UsbIds::GetVendorName(data.vendor_id()); |
86 if (vendor) { | 59 if (vendor) { |
87 const char* product = | 60 const char* product = |
(...skipping 21 matching lines...) Expand all Loading... |
109 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_LIST_ITEM_UNKNOWN_PRODUCT, | 82 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_LIST_ITEM_UNKNOWN_PRODUCT, |
110 base::UTF8ToUTF16(vendor))); | 83 base::UTF8ToUTF16(vendor))); |
111 } | 84 } |
112 | 85 |
113 // Display the catch all "device from an unknown vendor" last. | 86 // Display the catch all "device from an unknown vendor" last. |
114 if (found_unknown_vendor) { | 87 if (found_unknown_vendor) { |
115 details.push_back(l10n_util::GetStringUTF16( | 88 details.push_back(l10n_util::GetStringUTF16( |
116 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_LIST_ITEM_UNKNOWN_VENDOR)); | 89 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_LIST_ITEM_UNKNOWN_VENDOR)); |
117 } | 90 } |
118 | 91 |
119 if (messages) { | 92 for (const auto& detail : details) |
120 messages->push_back(PermissionMessage( | 93 ids->insert(APIPermission::kUsbDeviceList, detail); |
121 PermissionMessage::kUsbDevice, | |
122 l10n_util::GetStringUTF16( | |
123 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_LIST), | |
124 base::JoinString(details, base::ASCIIToUTF16("\n")))); | |
125 } | |
126 if (ids) { | |
127 for (const auto& detail : details) | |
128 ids->insert(APIPermission::kUsbDeviceList, detail); | |
129 } | |
130 } | 94 } |
131 } | 95 } |
132 | 96 |
133 } // namespace | 97 } // namespace |
134 | 98 |
135 UsbDevicePermission::UsbDevicePermission(const APIPermissionInfo* info) | 99 UsbDevicePermission::UsbDevicePermission(const APIPermissionInfo* info) |
136 : SetDisjunctionPermission<UsbDevicePermissionData, UsbDevicePermission>( | 100 : SetDisjunctionPermission<UsbDevicePermissionData, UsbDevicePermission>( |
137 info) { | 101 info) { |
138 } | 102 } |
139 | 103 |
140 UsbDevicePermission::~UsbDevicePermission() { | 104 UsbDevicePermission::~UsbDevicePermission() { |
141 } | 105 } |
142 | 106 |
143 PermissionIDSet UsbDevicePermission::GetPermissions() const { | 107 PermissionIDSet UsbDevicePermission::GetPermissions() const { |
144 PermissionIDSet ids; | 108 PermissionIDSet ids; |
145 AddPermissionsToLists(data_set_, &ids, NULL); | 109 AddPermissionsToLists(data_set_, &ids); |
146 return ids; | 110 return ids; |
147 } | 111 } |
148 | 112 |
149 } // namespace extensions | 113 } // namespace extensions |
OLD | NEW |