| 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/browser/api/device_permissions_prompt.h" | 5 #include "extensions/browser/api/device_permissions_prompt.h" |
| 6 | 6 |
| 7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "device/core/device_client.h" | 10 #include "device/core/device_client.h" |
| 11 #include "device/usb/usb_device.h" | 11 #include "device/usb/usb_device.h" |
| 12 #include "device/usb/usb_device_filter.h" | 12 #include "device/usb/usb_device_filter.h" |
| 13 #include "device/usb/usb_ids.h" | 13 #include "device/usb/usb_ids.h" |
| 14 #include "device/usb/usb_service.h" | 14 #include "device/usb/usb_service.h" |
| 15 #include "extensions/browser/api/device_permissions_manager.h" | 15 #include "extensions/browser/api/device_permissions_manager.h" |
| 16 #include "extensions/common/extension.h" | 16 #include "extensions/common/extension.h" |
| 17 #include "extensions/strings/grit/extensions_strings.h" | 17 #include "extensions/strings/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 { | |
| 21 | |
| 22 using device::UsbDevice; | 20 using device::UsbDevice; |
| 23 using device::UsbDeviceFilter; | 21 using device::UsbDeviceFilter; |
| 24 using device::UsbService; | 22 using device::UsbService; |
| 25 | 23 |
| 24 namespace extensions { |
| 25 |
| 26 DevicePermissionsPrompt::Prompt::DeviceInfo::DeviceInfo( | 26 DevicePermissionsPrompt::Prompt::DeviceInfo::DeviceInfo( |
| 27 scoped_refptr<UsbDevice> device, | 27 scoped_refptr<UsbDevice> device) |
| 28 const base::string16& name, | 28 : device(device) { |
| 29 const base::string16& product_string, | 29 base::string16 manufacturer_string; |
| 30 const base::string16& manufacturer_string, | 30 if (device->GetManufacturer(&manufacturer_string)) { |
| 31 const base::string16& serial_number) | 31 original_manufacturer_string = manufacturer_string; |
| 32 : device(device), | 32 } else { |
| 33 name(name), | 33 const char* vendor_name = |
| 34 product_string(product_string), | 34 device::UsbIds::GetVendorName(device->vendor_id()); |
| 35 manufacturer_string(manufacturer_string), | 35 if (vendor_name) { |
| 36 serial_number(serial_number) { | 36 manufacturer_string = base::UTF8ToUTF16(vendor_name); |
| 37 } else { |
| 38 base::string16 vendor_id = |
| 39 base::ASCIIToUTF16(base::StringPrintf("0x%04x", device->vendor_id())); |
| 40 manufacturer_string = |
| 41 l10n_util::GetStringFUTF16(IDS_DEVICE_UNKNOWN_VENDOR, vendor_id); |
| 42 } |
| 43 } |
| 44 |
| 45 base::string16 product_string; |
| 46 if (device->GetProduct(&product_string)) { |
| 47 original_product_string = product_string; |
| 48 } else { |
| 49 const char* product_name = device::UsbIds::GetProductName( |
| 50 device->vendor_id(), device->product_id()); |
| 51 if (product_name) { |
| 52 product_string = base::UTF8ToUTF16(product_name); |
| 53 } else { |
| 54 base::string16 product_id = base::ASCIIToUTF16( |
| 55 base::StringPrintf("0x%04x", device->product_id())); |
| 56 product_string = |
| 57 l10n_util::GetStringFUTF16(IDS_DEVICE_UNKNOWN_PRODUCT, product_id); |
| 58 } |
| 59 } |
| 60 |
| 61 if (!device->GetSerialNumber(&serial_number)) { |
| 62 serial_number.clear(); |
| 63 } |
| 64 |
| 65 name = l10n_util::GetStringFUTF16(IDS_DEVICE_PERMISSIONS_DEVICE_NAME, |
| 66 product_string, manufacturer_string); |
| 37 } | 67 } |
| 38 | 68 |
| 39 DevicePermissionsPrompt::Prompt::DeviceInfo::~DeviceInfo() { | 69 DevicePermissionsPrompt::Prompt::DeviceInfo::~DeviceInfo() { |
| 40 } | 70 } |
| 41 | 71 |
| 42 DevicePermissionsPrompt::Prompt::Prompt() | 72 DevicePermissionsPrompt::Prompt::Prompt() |
| 43 : extension_(nullptr), | 73 : extension_(nullptr), |
| 44 browser_context_(nullptr), | 74 browser_context_(nullptr), |
| 45 multiple_(false), | 75 multiple_(false), |
| 46 observer_(nullptr) { | 76 observer_(nullptr), |
| 77 usb_service_observer_(this) { |
| 47 } | 78 } |
| 48 | 79 |
| 49 void DevicePermissionsPrompt::Prompt::SetObserver(Observer* observer) { | 80 void DevicePermissionsPrompt::Prompt::SetObserver(Observer* observer) { |
| 50 observer_ = observer; | 81 observer_ = observer; |
| 51 | 82 |
| 52 content::BrowserThread::PostTask( | 83 if (observer_) { |
| 53 content::BrowserThread::FILE, | 84 content::BrowserThread::PostTask( |
| 54 FROM_HERE, | 85 content::BrowserThread::FILE, FROM_HERE, |
| 55 base::Bind(&DevicePermissionsPrompt::Prompt::DoDeviceQuery, this)); | 86 base::Bind(&DevicePermissionsPrompt::Prompt::DoDeviceQuery, this)); |
| 87 } |
| 56 } | 88 } |
| 57 | 89 |
| 58 base::string16 DevicePermissionsPrompt::Prompt::GetHeading() const { | 90 base::string16 DevicePermissionsPrompt::Prompt::GetHeading() const { |
| 59 return l10n_util::GetStringUTF16( | 91 return l10n_util::GetStringUTF16( |
| 60 multiple_ ? IDS_DEVICE_PERMISSIONS_PROMPT_TITLE_MULTIPLE | 92 multiple_ ? IDS_DEVICE_PERMISSIONS_PROMPT_TITLE_MULTIPLE |
| 61 : IDS_DEVICE_PERMISSIONS_PROMPT_TITLE_SINGLE); | 93 : IDS_DEVICE_PERMISSIONS_PROMPT_TITLE_SINGLE); |
| 62 } | 94 } |
| 63 | 95 |
| 64 base::string16 DevicePermissionsPrompt::Prompt::GetPromptMessage() const { | 96 base::string16 DevicePermissionsPrompt::Prompt::GetPromptMessage() const { |
| 65 return l10n_util::GetStringFUTF16(multiple_ | 97 return l10n_util::GetStringFUTF16(multiple_ |
| 66 ? IDS_DEVICE_PERMISSIONS_PROMPT_MULTIPLE | 98 ? IDS_DEVICE_PERMISSIONS_PROMPT_MULTIPLE |
| 67 : IDS_DEVICE_PERMISSIONS_PROMPT_SINGLE, | 99 : IDS_DEVICE_PERMISSIONS_PROMPT_SINGLE, |
| 68 base::UTF8ToUTF16(extension_->name())); | 100 base::UTF8ToUTF16(extension_->name())); |
| 69 } | 101 } |
| 70 | 102 |
| 71 scoped_refptr<UsbDevice> DevicePermissionsPrompt::Prompt::GetDevice( | 103 scoped_refptr<UsbDevice> DevicePermissionsPrompt::Prompt::GetDevice( |
| 72 size_t index) const { | 104 size_t index) const { |
| 73 DCHECK_LT(index, devices_.size()); | 105 DCHECK_LT(index, devices_.size()); |
| 74 return devices_[index].device; | 106 return devices_[index].device; |
| 75 } | 107 } |
| 76 | 108 |
| 77 void DevicePermissionsPrompt::Prompt::GrantDevicePermission( | 109 void DevicePermissionsPrompt::Prompt::GrantDevicePermission( |
| 78 size_t index) const { | 110 size_t index) const { |
| 79 DCHECK_LT(index, devices_.size()); | 111 DCHECK_LT(index, devices_.size()); |
| 80 DevicePermissionsManager* permissions_manager = | 112 DevicePermissionsManager* permissions_manager = |
| 81 DevicePermissionsManager::Get(browser_context_); | 113 DevicePermissionsManager::Get(browser_context_); |
| 82 if (permissions_manager) { | 114 if (permissions_manager) { |
| 83 const DeviceInfo& device = devices_[index]; | 115 const DeviceInfo& device = devices_[index]; |
| 84 permissions_manager->AllowUsbDevice(extension_->id(), | 116 permissions_manager->AllowUsbDevice( |
| 85 device.device, | 117 extension_->id(), device.device, device.original_product_string, |
| 86 device.product_string, | 118 device.original_manufacturer_string, device.serial_number); |
| 87 device.manufacturer_string, | |
| 88 device.serial_number); | |
| 89 } | 119 } |
| 90 } | 120 } |
| 91 | 121 |
| 92 void DevicePermissionsPrompt::Prompt::set_filters( | 122 void DevicePermissionsPrompt::Prompt::set_filters( |
| 93 const std::vector<UsbDeviceFilter>& filters) { | 123 const std::vector<UsbDeviceFilter>& filters) { |
| 94 filters_ = filters; | 124 filters_ = filters; |
| 95 } | 125 } |
| 96 | 126 |
| 97 DevicePermissionsPrompt::Prompt::~Prompt() { | 127 DevicePermissionsPrompt::Prompt::~Prompt() { |
| 98 } | 128 } |
| 99 | 129 |
| 100 void DevicePermissionsPrompt::Prompt::DoDeviceQuery() { | 130 void DevicePermissionsPrompt::Prompt::DoDeviceQuery() { |
| 101 UsbService* service = device::DeviceClient::Get()->GetUsbService(); | 131 UsbService* service = device::DeviceClient::Get()->GetUsbService(); |
| 102 if (!service) { | 132 if (!service) { |
| 103 return; | 133 return; |
| 104 } | 134 } |
| 105 | 135 |
| 106 std::vector<scoped_refptr<UsbDevice>> devices; | 136 std::vector<scoped_refptr<UsbDevice>> devices; |
| 107 service->GetDevices(&devices); | 137 service->GetDevices(&devices); |
| 108 | 138 |
| 109 std::vector<DeviceInfo> device_info; | 139 std::vector<DeviceInfo> device_info; |
| 110 for (const auto& device : devices) { | 140 for (const auto& device : devices) { |
| 111 if (!(filters_.empty() || UsbDeviceFilter::MatchesAny(device, filters_))) { | 141 if (!(filters_.empty() || UsbDeviceFilter::MatchesAny(device, filters_))) { |
| 112 continue; | 142 continue; |
| 113 } | 143 } |
| 114 | 144 |
| 115 base::string16 manufacturer_string; | 145 device_info.push_back(DeviceInfo(device)); |
| 116 base::string16 original_manufacturer_string; | 146 } |
| 117 if (device->GetManufacturer(&original_manufacturer_string)) { | |
| 118 manufacturer_string = original_manufacturer_string; | |
| 119 } else { | |
| 120 const char* vendor_name = | |
| 121 device::UsbIds::GetVendorName(device->vendor_id()); | |
| 122 if (vendor_name) { | |
| 123 manufacturer_string = base::UTF8ToUTF16(vendor_name); | |
| 124 } else { | |
| 125 base::string16 vendor_id = base::ASCIIToUTF16( | |
| 126 base::StringPrintf("0x%04x", device->vendor_id())); | |
| 127 manufacturer_string = | |
| 128 l10n_util::GetStringFUTF16(IDS_DEVICE_UNKNOWN_VENDOR, vendor_id); | |
| 129 } | |
| 130 } | |
| 131 | 147 |
| 132 base::string16 product_string; | 148 if (!usb_service_observer_.IsObserving(service)) { |
| 133 base::string16 original_product_string; | 149 usb_service_observer_.Add(service); |
| 134 if (device->GetProduct(&original_product_string)) { | |
| 135 product_string = original_product_string; | |
| 136 } else { | |
| 137 const char* product_name = device::UsbIds::GetProductName( | |
| 138 device->vendor_id(), device->product_id()); | |
| 139 if (product_name) { | |
| 140 product_string = base::UTF8ToUTF16(product_name); | |
| 141 } else { | |
| 142 base::string16 product_id = base::ASCIIToUTF16( | |
| 143 base::StringPrintf("0x%04x", device->product_id())); | |
| 144 product_string = | |
| 145 l10n_util::GetStringFUTF16(IDS_DEVICE_UNKNOWN_PRODUCT, product_id); | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 base::string16 serial_number; | |
| 150 if (!device->GetSerialNumber(&serial_number)) { | |
| 151 serial_number.clear(); | |
| 152 } | |
| 153 | |
| 154 device_info.push_back(DeviceInfo( | |
| 155 device, | |
| 156 l10n_util::GetStringFUTF16(IDS_DEVICE_PERMISSIONS_DEVICE_NAME, | |
| 157 product_string, | |
| 158 manufacturer_string), | |
| 159 original_product_string, | |
| 160 original_manufacturer_string, | |
| 161 serial_number)); | |
| 162 } | 150 } |
| 163 | 151 |
| 164 content::BrowserThread::PostTask( | 152 content::BrowserThread::PostTask( |
| 165 content::BrowserThread::UI, | 153 content::BrowserThread::UI, |
| 166 FROM_HERE, | 154 FROM_HERE, |
| 167 base::Bind( | 155 base::Bind( |
| 168 &DevicePermissionsPrompt::Prompt::SetDevices, this, device_info)); | 156 &DevicePermissionsPrompt::Prompt::SetDevices, this, device_info)); |
| 169 } | 157 } |
| 170 | 158 |
| 171 void DevicePermissionsPrompt::Prompt::SetDevices( | 159 void DevicePermissionsPrompt::Prompt::SetDevices( |
| 172 const std::vector<DeviceInfo>& devices) { | 160 const std::vector<DeviceInfo>& devices) { |
| 173 devices_ = devices; | 161 devices_ = devices; |
| 174 if (observer_) { | 162 if (observer_) { |
| 175 observer_->OnDevicesChanged(); | 163 observer_->OnDevicesChanged(); |
| 176 } | 164 } |
| 177 } | 165 } |
| 178 | 166 |
| 167 void DevicePermissionsPrompt::Prompt::AddDevice(const DeviceInfo& device) { |
| 168 devices_.push_back(device); |
| 169 if (observer_) { |
| 170 observer_->OnDevicesChanged(); |
| 171 } |
| 172 } |
| 173 |
| 174 void DevicePermissionsPrompt::Prompt::RemoveDevice( |
| 175 scoped_refptr<UsbDevice> device) { |
| 176 bool removed_entry = false; |
| 177 for (std::vector<DeviceInfo>::iterator it = devices_.begin(); |
| 178 it != devices_.end(); ++it) { |
| 179 if (it->device == device) { |
| 180 devices_.erase(it); |
| 181 removed_entry = true; |
| 182 break; |
| 183 } |
| 184 } |
| 185 if (observer_ && removed_entry) { |
| 186 observer_->OnDevicesChanged(); |
| 187 } |
| 188 } |
| 189 |
| 190 void DevicePermissionsPrompt::Prompt::OnDeviceAdded( |
| 191 scoped_refptr<UsbDevice> device) { |
| 192 if (!(filters_.empty() || UsbDeviceFilter::MatchesAny(device, filters_))) { |
| 193 return; |
| 194 } |
| 195 |
| 196 content::BrowserThread::PostTask( |
| 197 content::BrowserThread::UI, FROM_HERE, |
| 198 base::Bind(&DevicePermissionsPrompt::Prompt::AddDevice, this, |
| 199 DeviceInfo(device))); |
| 200 } |
| 201 |
| 202 void DevicePermissionsPrompt::Prompt::OnDeviceRemoved( |
| 203 scoped_refptr<UsbDevice> device) { |
| 204 content::BrowserThread::PostTask( |
| 205 content::BrowserThread::UI, FROM_HERE, |
| 206 base::Bind(&DevicePermissionsPrompt::Prompt::RemoveDevice, this, device)); |
| 207 } |
| 208 |
| 179 DevicePermissionsPrompt::DevicePermissionsPrompt( | 209 DevicePermissionsPrompt::DevicePermissionsPrompt( |
| 180 content::WebContents* web_contents) | 210 content::WebContents* web_contents) |
| 181 : web_contents_(web_contents), delegate_(nullptr) { | 211 : web_contents_(web_contents), delegate_(nullptr) { |
| 182 } | 212 } |
| 183 | 213 |
| 184 DevicePermissionsPrompt::~DevicePermissionsPrompt() { | 214 DevicePermissionsPrompt::~DevicePermissionsPrompt() { |
| 185 } | 215 } |
| 186 | 216 |
| 187 void DevicePermissionsPrompt::AskForUsbDevices( | 217 void DevicePermissionsPrompt::AskForUsbDevices( |
| 188 Delegate* delegate, | 218 Delegate* delegate, |
| 189 const Extension* extension, | 219 const Extension* extension, |
| 190 content::BrowserContext* context, | 220 content::BrowserContext* context, |
| 191 bool multiple, | 221 bool multiple, |
| 192 const std::vector<UsbDeviceFilter>& filters) { | 222 const std::vector<UsbDeviceFilter>& filters) { |
| 193 prompt_ = new Prompt(); | 223 prompt_ = new Prompt(); |
| 194 prompt_->set_extension(extension); | 224 prompt_->set_extension(extension); |
| 195 prompt_->set_browser_context(context); | 225 prompt_->set_browser_context(context); |
| 196 prompt_->set_multiple(multiple); | 226 prompt_->set_multiple(multiple); |
| 197 prompt_->set_filters(filters); | 227 prompt_->set_filters(filters); |
| 198 delegate_ = delegate; | 228 delegate_ = delegate; |
| 199 | 229 |
| 200 ShowDialog(); | 230 ShowDialog(); |
| 201 } | 231 } |
| 202 | 232 |
| 203 } // namespace extensions | 233 } // namespace extensions |
| OLD | NEW |