• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "extensions/common/permissions/usb_device_permission.h"
6 
7 #include <set>
8 #include <string>
9 
10 #include "base/logging.h"
11 #include "base/strings/string16.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "extensions/common/permissions/permissions_info.h"
14 #include "grit/extensions_strings.h"
15 #include "ui/base/l10n/l10n_util.h"
16 
17 #if defined(ENABLE_EXTENSIONS)
18 #include "device/usb/usb_ids.h"
19 #endif
20 
21 namespace extensions {
22 
UsbDevicePermission(const APIPermissionInfo * info)23 UsbDevicePermission::UsbDevicePermission(
24     const APIPermissionInfo* info)
25   : SetDisjunctionPermission<UsbDevicePermissionData,
26                              UsbDevicePermission>(info) {
27 }
28 
~UsbDevicePermission()29 UsbDevicePermission::~UsbDevicePermission() {
30 }
31 
GetMessages() const32 PermissionMessages UsbDevicePermission::GetMessages() const {
33   DCHECK(HasMessages());
34   PermissionMessages result;
35 
36 #if defined(ENABLE_EXTENSIONS)
37   // //device/usb/usb.gyp:device_usb is not available when extensions are
38   // disabled.
39   for (std::set<UsbDevicePermissionData>::const_iterator i =
40       data_set_.begin(); i != data_set_.end(); ++i) {
41     const char* vendor = device::UsbIds::GetVendorName(i->vendor_id());
42 
43     if (vendor) {
44       const char* product =
45           device::UsbIds::GetProductName(i->vendor_id(), i->product_id());
46       if (product) {
47         result.push_back(PermissionMessage(
48             PermissionMessage::kUsbDevice,
49             l10n_util::GetStringFUTF16(
50                 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE,
51                 base::ASCIIToUTF16(product),
52                 base::ASCIIToUTF16(vendor))));
53       } else {
54         result.push_back(PermissionMessage(
55             PermissionMessage::kUsbDevice,
56             l10n_util::GetStringFUTF16(
57                 IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_MISSING_PRODUCT,
58                 base::ASCIIToUTF16(vendor))));
59       }
60     } else {
61       result.push_back(PermissionMessage(
62           PermissionMessage::kUsbDevice,
63           l10n_util::GetStringUTF16(
64               IDS_EXTENSION_PROMPT_WARNING_USB_DEVICE_MISSING_VENDOR)));
65     }
66   }
67 #else
68   NOTREACHED();
69 #endif  // defined(ENABLE_EXTENSIONS)
70 
71   return result;
72 }
73 
74 }  // namespace extensions
75