Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 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/browser/api/device_permissions_prompt.h" | |
| 6 | |
| 7 #include "base/strings/stringprintf.h" | |
| 8 #include "base/strings/utf_string_conversions.h" | |
| 9 #include "content/public/browser/browser_thread.h" | |
| 10 #include "device/core/device_client.h" | |
| 11 #include "device/usb/usb_device.h" | |
| 12 #include "device/usb/usb_device_filter.h" | |
| 13 #include "device/usb/usb_ids.h" | |
| 14 #include "device/usb/usb_service.h" | |
| 15 #include "extensions/browser/api/device_permissions_manager.h" | |
| 16 #include "extensions/common/extension.h" | |
| 17 #include "extensions/strings/grit/extensions_strings.h" | |
| 18 #include "ui/base/l10n/l10n_util.h" | |
| 19 | |
| 20 namespace extensions { | |
| 21 | |
| 22 using device::UsbDevice; | |
| 23 using device::UsbDeviceFilter; | |
| 24 using device::UsbService; | |
| 25 | |
| 26 DevicePermissionsPrompt::Prompt::DeviceInfo::DeviceInfo( | |
| 27 scoped_refptr<UsbDevice> device, | |
| 28 base::string16 name, | |
| 29 base::string16 serial_number, | |
| 30 base::string16 tooltip) | |
| 31 : device(device), | |
| 32 name(name), | |
| 33 serial_number(serial_number), | |
| 34 tooltip(tooltip) { | |
| 35 } | |
| 36 | |
| 37 DevicePermissionsPrompt::Prompt::DeviceInfo::~DeviceInfo() { | |
| 38 } | |
| 39 | |
| 40 DevicePermissionsPrompt::Prompt::Prompt() { | |
| 41 } | |
|
Finnur
2014/10/15 10:01:25
None of the class members get initialized?
Reilly Grant (use Gerrit)
2014/10/15 19:15:33
Done.
| |
| 42 | |
| 43 void DevicePermissionsPrompt::Prompt::SetObserver(Observer* observer) { | |
| 44 observer_ = observer; | |
| 45 | |
| 46 content::BrowserThread::PostTask( | |
| 47 content::BrowserThread::FILE, | |
| 48 FROM_HERE, | |
| 49 base::Bind(&DevicePermissionsPrompt::Prompt::DoDeviceQuery, this)); | |
| 50 } | |
| 51 | |
| 52 base::string16 DevicePermissionsPrompt::Prompt::GetHeading() const { | |
| 53 return l10n_util::GetStringUTF16(IDS_DEVICE_PERMISSIONS_PROMPT_HEADING); | |
| 54 } | |
| 55 | |
| 56 base::string16 DevicePermissionsPrompt::Prompt::GetPromptMessage() const { | |
| 57 return l10n_util::GetStringFUTF16(multiple_ | |
| 58 ? IDS_DEVICE_PERMISSIONS_PROMPT_MULTIPLE | |
| 59 : IDS_DEVICE_PERMISSIONS_PROMPT_SINGLE, | |
| 60 base::UTF8ToUTF16(extension_->name())); | |
| 61 } | |
| 62 | |
| 63 scoped_refptr<UsbDevice> DevicePermissionsPrompt::Prompt::GetDevice( | |
| 64 size_t index) const { | |
| 65 DCHECK_LT(index, devices_.size()); | |
| 66 return devices_[index].device; | |
| 67 } | |
| 68 | |
| 69 void DevicePermissionsPrompt::Prompt::GrantDevicePermission( | |
| 70 size_t index) const { | |
| 71 DCHECK_LT(index, devices_.size()); | |
| 72 DevicePermissionsManager* permissions_manager = | |
| 73 DevicePermissionsManager::Get(browser_context_); | |
| 74 if (permissions_manager) { | |
| 75 permissions_manager->AllowUsbDevice(extension_->id(), | |
| 76 devices_[index].device, | |
| 77 devices_[index].serial_number); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 void DevicePermissionsPrompt::Prompt::set_filters( | |
| 82 const std::vector<UsbDeviceFilter>& filters) { | |
| 83 filters_ = filters; | |
| 84 } | |
| 85 | |
| 86 DevicePermissionsPrompt::Prompt::~Prompt() { | |
| 87 } | |
| 88 | |
| 89 void DevicePermissionsPrompt::Prompt::DoDeviceQuery() { | |
| 90 UsbService* service = device::DeviceClient::Get()->GetUsbService(); | |
| 91 if (!service) { | |
| 92 return; | |
| 93 } | |
|
Finnur
2014/10/15 10:01:26
nit: Single-line if, no braces. Here and everywher
Reilly Grant (use Gerrit)
2014/10/15 19:15:33
"In general, curly braces are not required for sin
| |
| 94 | |
| 95 std::vector<scoped_refptr<UsbDevice>> devices; | |
|
Finnur
2014/10/15 10:01:25
style: s/>>/> >/
Reilly Grant (use Gerrit)
2014/10/15 19:15:33
clang-format has been updated to prefer the no-spa
| |
| 96 service->GetDevices(&devices); | |
| 97 | |
| 98 std::vector<DeviceInfo> device_info; | |
| 99 for (const auto& device : devices) { | |
| 100 if (!(filters_.empty() || UsbDeviceFilter::MatchesAny(device, filters_))) { | |
| 101 continue; | |
| 102 } | |
| 103 | |
| 104 const char* vendor_name_raw = | |
| 105 device::UsbIds::GetVendorName(device->vendor_id()); | |
| 106 base::string16 vendor_name; | |
| 107 if (vendor_name_raw) { | |
| 108 vendor_name = base::UTF8ToUTF16(vendor_name_raw); | |
| 109 } else { | |
| 110 vendor_name = l10n_util::GetStringUTF16(IDS_DEVICE_UNKNOWN_VENDOR); | |
| 111 } | |
| 112 | |
| 113 const char* product_name_raw = device::UsbIds::GetProductName( | |
| 114 device->vendor_id(), device->product_id()); | |
| 115 base::string16 product_name; | |
| 116 if (product_name_raw) { | |
| 117 product_name = base::UTF8ToUTF16(product_name_raw); | |
| 118 } else { | |
| 119 product_name = l10n_util::GetStringUTF16(IDS_DEVICE_UNKNOWN_PRODUCT); | |
| 120 } | |
| 121 | |
| 122 base::string16 manufacturer_string; | |
| 123 if (!device->GetManufacturer(&manufacturer_string)) { | |
| 124 manufacturer_string = vendor_name; | |
| 125 } | |
| 126 | |
| 127 base::string16 product_string; | |
| 128 if (!device->GetProduct(&product_string)) { | |
| 129 product_string = product_name; | |
| 130 } | |
| 131 | |
| 132 base::string16 serial_number; | |
| 133 if (!device->GetSerialNumber(&serial_number)) { | |
| 134 serial_number.clear(); | |
| 135 } | |
| 136 | |
| 137 base::string16 vendor_id = | |
| 138 base::ASCIIToUTF16(base::StringPrintf("0x%04x", device->vendor_id())); | |
| 139 base::string16 product_id = | |
| 140 base::ASCIIToUTF16(base::StringPrintf("0x%04x", device->product_id())); | |
| 141 | |
| 142 device_info.push_back(DeviceInfo( | |
| 143 device, | |
| 144 l10n_util::GetStringFUTF16(IDS_DEVICE_PERMISSIONS_PROMPT_DEVICE_NAME, | |
| 145 product_string, | |
| 146 manufacturer_string), | |
| 147 serial_number, | |
| 148 l10n_util::GetStringFUTF16(IDS_DEVICE_PERMISSIONS_PROMPT_DEVICE_TOOLTIP, | |
| 149 vendor_name, | |
| 150 vendor_id, | |
| 151 product_name, | |
| 152 product_id))); | |
| 153 } | |
| 154 | |
| 155 content::BrowserThread::PostTask( | |
| 156 content::BrowserThread::UI, | |
| 157 FROM_HERE, | |
| 158 base::Bind( | |
| 159 &DevicePermissionsPrompt::Prompt::SetDevices, this, device_info)); | |
| 160 } | |
| 161 | |
| 162 void DevicePermissionsPrompt::Prompt::SetDevices( | |
| 163 const std::vector<DeviceInfo>& devices) { | |
| 164 devices_ = devices; | |
| 165 if (observer_) { | |
| 166 observer_->OnDevicesChanged(); | |
| 167 } | |
| 168 } | |
| 169 | |
| 170 DevicePermissionsPrompt::DevicePermissionsPrompt( | |
| 171 content::WebContents* web_contents) | |
| 172 : web_contents_(web_contents) { | |
|
Finnur
2014/10/15 10:01:26
delegate_(NULL) ?
Well, I guess we're using nullpt
Reilly Grant (use Gerrit)
2014/10/15 19:15:33
Done.
| |
| 173 } | |
| 174 | |
| 175 DevicePermissionsPrompt::~DevicePermissionsPrompt() { | |
| 176 } | |
| 177 | |
| 178 void DevicePermissionsPrompt::AskForUsbDevices( | |
| 179 Delegate* delegate, | |
| 180 const Extension* extension, | |
| 181 content::BrowserContext* context, | |
| 182 bool multiple, | |
| 183 std::vector<UsbDeviceFilter> filters) { | |
| 184 prompt_ = new Prompt(); | |
| 185 prompt_->set_extension(extension); | |
| 186 prompt_->set_browser_context(context); | |
| 187 prompt_->set_multiple(multiple); | |
| 188 prompt_->set_filters(filters); | |
| 189 delegate_ = delegate; | |
| 190 | |
| 191 GetDefaultShowDialogCallback().Run(web_contents_, delegate_, prompt_); | |
| 192 } | |
| 193 | |
| 194 } // namespace extensions | |
| OLD | NEW |