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