| 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/barrier_closure.h" | 7 #include "base/barrier_closure.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "device/core/device_client.h" | 11 #include "device/core/device_client.h" |
| 12 #include "device/usb/usb_device.h" | 12 #include "device/usb/usb_device.h" |
| 13 #include "device/usb/usb_device_filter.h" | 13 #include "device/usb/usb_device_filter.h" |
| 14 #include "device/usb/usb_ids.h" | 14 #include "device/usb/usb_ids.h" |
| 15 #include "device/usb/usb_service.h" | 15 #include "device/usb/usb_service.h" |
| 16 #include "extensions/browser/api/device_permissions_manager.h" | 16 #include "extensions/browser/api/device_permissions_manager.h" |
| 17 #include "extensions/common/extension.h" | 17 #include "extensions/common/extension.h" |
| 18 #include "extensions/strings/grit/extensions_strings.h" | 18 #include "extensions/strings/grit/extensions_strings.h" |
| 19 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
| 20 | 20 |
| 21 using content::BrowserThread; | 21 using content::BrowserThread; |
| 22 using device::UsbDevice; | 22 using device::UsbDevice; |
| 23 using device::UsbDeviceFilter; | 23 using device::UsbDeviceFilter; |
| 24 using device::UsbService; | 24 using device::UsbService; |
| 25 | 25 |
| 26 namespace extensions { | 26 namespace extensions { |
| 27 | 27 |
| 28 DevicePermissionsPrompt::Delegate::~Delegate() { |
| 29 } |
| 30 |
| 28 DevicePermissionsPrompt::Prompt::DeviceInfo::DeviceInfo( | 31 DevicePermissionsPrompt::Prompt::DeviceInfo::DeviceInfo( |
| 29 scoped_refptr<UsbDevice> device) | 32 scoped_refptr<UsbDevice> device) |
| 30 : device(device) { | 33 : device(device) { |
| 31 base::string16 manufacturer_string = device->manufacturer_string(); | 34 base::string16 manufacturer_string = device->manufacturer_string(); |
| 32 if (manufacturer_string.empty()) { | 35 if (manufacturer_string.empty()) { |
| 33 const char* vendor_name = | 36 const char* vendor_name = |
| 34 device::UsbIds::GetVendorName(device->vendor_id()); | 37 device::UsbIds::GetVendorName(device->vendor_id()); |
| 35 if (vendor_name) { | 38 if (vendor_name) { |
| 36 manufacturer_string = base::UTF8ToUTF16(vendor_name); | 39 manufacturer_string = base::UTF8ToUTF16(vendor_name); |
| 37 } else { | 40 } else { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 56 } | 59 } |
| 57 } | 60 } |
| 58 | 61 |
| 59 name = l10n_util::GetStringFUTF16(IDS_DEVICE_PERMISSIONS_DEVICE_NAME, | 62 name = l10n_util::GetStringFUTF16(IDS_DEVICE_PERMISSIONS_DEVICE_NAME, |
| 60 product_string, manufacturer_string); | 63 product_string, manufacturer_string); |
| 61 } | 64 } |
| 62 | 65 |
| 63 DevicePermissionsPrompt::Prompt::DeviceInfo::~DeviceInfo() { | 66 DevicePermissionsPrompt::Prompt::DeviceInfo::~DeviceInfo() { |
| 64 } | 67 } |
| 65 | 68 |
| 66 DevicePermissionsPrompt::Prompt::Prompt() : usb_service_observer_(this) { | 69 DevicePermissionsPrompt::Prompt::Observer::~Observer() { |
| 70 } |
| 71 |
| 72 DevicePermissionsPrompt::Prompt::Prompt(Delegate* delegate, |
| 73 const Extension* extension, |
| 74 content::BrowserContext* context) |
| 75 : extension_(extension), |
| 76 browser_context_(context), |
| 77 delegate_(delegate), |
| 78 usb_service_observer_(this) { |
| 67 } | 79 } |
| 68 | 80 |
| 69 void DevicePermissionsPrompt::Prompt::SetObserver(Observer* observer) { | 81 void DevicePermissionsPrompt::Prompt::SetObserver(Observer* observer) { |
| 70 observer_ = observer; | 82 observer_ = observer; |
| 71 | 83 |
| 72 if (observer_) { | 84 if (observer_) { |
| 73 UsbService* service = device::DeviceClient::Get()->GetUsbService(); | 85 UsbService* service = device::DeviceClient::Get()->GetUsbService(); |
| 74 if (service && !usb_service_observer_.IsObserving(service)) { | 86 if (service && !usb_service_observer_.IsObserving(service)) { |
| 75 service->GetDevices(base::Bind( | 87 service->GetDevices(base::Bind( |
| 76 &DevicePermissionsPrompt::Prompt::OnDevicesEnumerated, this)); | 88 &DevicePermissionsPrompt::Prompt::OnDevicesEnumerated, this)); |
| 77 usb_service_observer_.Add(service); | 89 usb_service_observer_.Add(service); |
| 78 } | 90 } |
| 79 } | 91 } |
| 80 } | 92 } |
| 81 | 93 |
| 82 base::string16 DevicePermissionsPrompt::Prompt::GetHeading() const { | 94 base::string16 DevicePermissionsPrompt::Prompt::GetHeading() const { |
| 83 return l10n_util::GetStringUTF16( | 95 return l10n_util::GetStringUTF16( |
| 84 multiple_ ? IDS_DEVICE_PERMISSIONS_PROMPT_TITLE_MULTIPLE | 96 multiple_ ? IDS_DEVICE_PERMISSIONS_PROMPT_TITLE_MULTIPLE |
| 85 : IDS_DEVICE_PERMISSIONS_PROMPT_TITLE_SINGLE); | 97 : IDS_DEVICE_PERMISSIONS_PROMPT_TITLE_SINGLE); |
| 86 } | 98 } |
| 87 | 99 |
| 88 base::string16 DevicePermissionsPrompt::Prompt::GetPromptMessage() const { | 100 base::string16 DevicePermissionsPrompt::Prompt::GetPromptMessage() const { |
| 89 return l10n_util::GetStringFUTF16(multiple_ | 101 return l10n_util::GetStringFUTF16(multiple_ |
| 90 ? IDS_DEVICE_PERMISSIONS_PROMPT_MULTIPLE | 102 ? IDS_DEVICE_PERMISSIONS_PROMPT_MULTIPLE |
| 91 : IDS_DEVICE_PERMISSIONS_PROMPT_SINGLE, | 103 : IDS_DEVICE_PERMISSIONS_PROMPT_SINGLE, |
| 92 base::UTF8ToUTF16(extension_->name())); | 104 base::UTF8ToUTF16(extension_->name())); |
| 93 } | 105 } |
| 94 | 106 |
| 95 scoped_refptr<UsbDevice> DevicePermissionsPrompt::Prompt::GetDevice( | 107 base::string16 DevicePermissionsPrompt::Prompt::GetDeviceName( |
| 96 size_t index) const { | 108 size_t index) const { |
| 97 DCHECK_LT(index, devices_.size()); | 109 DCHECK_LT(index, devices_.size()); |
| 98 return devices_[index].device; | 110 return devices_[index].name; |
| 99 } | 111 } |
| 100 | 112 |
| 101 void DevicePermissionsPrompt::Prompt::GrantDevicePermission( | 113 base::string16 DevicePermissionsPrompt::Prompt::GetDeviceSerialNumber( |
| 102 size_t index) const { | 114 size_t index) const { |
| 103 DCHECK_LT(index, devices_.size()); | 115 DCHECK_LT(index, devices_.size()); |
| 116 return devices_[index].device->serial_number(); |
| 117 } |
| 118 |
| 119 void DevicePermissionsPrompt::Prompt::GrantDevicePermission(size_t index) { |
| 120 DCHECK_LT(index, devices_.size()); |
| 121 devices_[index].granted = true; |
| 122 } |
| 123 |
| 124 void DevicePermissionsPrompt::Prompt::Dismissed() { |
| 104 DevicePermissionsManager* permissions_manager = | 125 DevicePermissionsManager* permissions_manager = |
| 105 DevicePermissionsManager::Get(browser_context_); | 126 DevicePermissionsManager::Get(browser_context_); |
| 106 if (permissions_manager) { | 127 std::vector<scoped_refptr<UsbDevice>> devices; |
| 107 const DeviceInfo& device = devices_[index]; | 128 for (const DeviceInfo& device : devices_) { |
| 108 permissions_manager->AllowUsbDevice(extension_->id(), device.device); | 129 if (device.granted) { |
| 130 devices.push_back(device.device); |
| 131 if (permissions_manager) { |
| 132 permissions_manager->AllowUsbDevice(extension_->id(), device.device); |
| 133 } |
| 134 } |
| 109 } | 135 } |
| 136 delegate_->OnUsbDevicesChosen(devices); |
| 110 } | 137 } |
| 111 | 138 |
| 112 void DevicePermissionsPrompt::Prompt::set_filters( | 139 void DevicePermissionsPrompt::Prompt::set_filters( |
| 113 const std::vector<UsbDeviceFilter>& filters) { | 140 const std::vector<UsbDeviceFilter>& filters) { |
| 114 filters_ = filters; | 141 filters_ = filters; |
| 115 } | 142 } |
| 116 | 143 |
| 117 DevicePermissionsPrompt::Prompt::~Prompt() { | 144 DevicePermissionsPrompt::Prompt::~Prompt() { |
| 118 } | 145 } |
| 119 | 146 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 if (allowed) { | 186 if (allowed) { |
| 160 devices_.push_back(DeviceInfo(device)); | 187 devices_.push_back(DeviceInfo(device)); |
| 161 if (observer_) { | 188 if (observer_) { |
| 162 observer_->OnDevicesChanged(); | 189 observer_->OnDevicesChanged(); |
| 163 } | 190 } |
| 164 } | 191 } |
| 165 } | 192 } |
| 166 | 193 |
| 167 DevicePermissionsPrompt::DevicePermissionsPrompt( | 194 DevicePermissionsPrompt::DevicePermissionsPrompt( |
| 168 content::WebContents* web_contents) | 195 content::WebContents* web_contents) |
| 169 : web_contents_(web_contents), delegate_(nullptr) { | 196 : web_contents_(web_contents) { |
| 170 } | 197 } |
| 171 | 198 |
| 172 DevicePermissionsPrompt::~DevicePermissionsPrompt() { | 199 DevicePermissionsPrompt::~DevicePermissionsPrompt() { |
| 173 } | 200 } |
| 174 | 201 |
| 175 void DevicePermissionsPrompt::AskForUsbDevices( | 202 void DevicePermissionsPrompt::AskForUsbDevices( |
| 176 Delegate* delegate, | 203 Delegate* delegate, |
| 177 const Extension* extension, | 204 const Extension* extension, |
| 178 content::BrowserContext* context, | 205 content::BrowserContext* context, |
| 179 bool multiple, | 206 bool multiple, |
| 180 const std::vector<UsbDeviceFilter>& filters) { | 207 const std::vector<UsbDeviceFilter>& filters) { |
| 181 prompt_ = new Prompt(); | 208 prompt_ = new Prompt(delegate, extension, context); |
| 182 prompt_->set_extension(extension); | |
| 183 prompt_->set_browser_context(context); | |
| 184 prompt_->set_multiple(multiple); | 209 prompt_->set_multiple(multiple); |
| 185 prompt_->set_filters(filters); | 210 prompt_->set_filters(filters); |
| 186 delegate_ = delegate; | |
| 187 | 211 |
| 188 ShowDialog(); | 212 ShowDialog(); |
| 189 } | 213 } |
| 190 | 214 |
| 191 } // namespace extensions | 215 } // namespace extensions |
| OLD | NEW |