| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "chrome/browser/usb/usb_chooser_controller.h" | 5 #include "chrome/browser/usb/usb_chooser_controller.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/command_line.h" | |
| 12 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 14 #include "chrome/browser/net/referrer.h" | 13 #include "chrome/browser/net/referrer.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/profiles/profile_manager.h" | 15 #include "chrome/browser/profiles/profile_manager.h" |
| 17 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
| 18 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" | 17 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" |
| 19 #include "chrome/browser/usb/usb_blocklist.h" | 18 #include "chrome/browser/usb/usb_blocklist.h" |
| 20 #include "chrome/browser/usb/usb_chooser_context.h" | 19 #include "chrome/browser/usb/usb_chooser_context.h" |
| 21 #include "chrome/browser/usb/usb_chooser_context_factory.h" | 20 #include "chrome/browser/usb/usb_chooser_context_factory.h" |
| 22 #include "chrome/browser/usb/web_usb_histograms.h" | 21 #include "chrome/browser/usb/web_usb_histograms.h" |
| 23 #include "chrome/browser/usb/web_usb_permission_provider.h" | 22 #include "chrome/browser/usb/web_usb_permission_provider.h" |
| 24 #include "chrome/common/chrome_switches.h" | |
| 25 #include "chrome/common/url_constants.h" | 23 #include "chrome/common/url_constants.h" |
| 26 #include "chrome/grit/generated_resources.h" | 24 #include "chrome/grit/generated_resources.h" |
| 27 #include "content/public/browser/render_frame_host.h" | 25 #include "content/public/browser/render_frame_host.h" |
| 28 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
| 29 #include "device/base/device_client.h" | 27 #include "device/base/device_client.h" |
| 30 #include "device/usb/mojo/type_converters.h" | 28 #include "device/usb/mojo/type_converters.h" |
| 31 #include "device/usb/usb_device.h" | 29 #include "device/usb/usb_device.h" |
| 32 #include "device/usb/usb_device_filter.h" | 30 #include "device/usb/usb_device_filter.h" |
| 33 #include "device/usb/usb_ids.h" | 31 #include "device/usb/usb_ids.h" |
| 34 #include "device/usb/webusb_descriptors.h" | 32 #include "device/usb/webusb_descriptors.h" |
| 35 #include "ui/base/l10n/l10n_util.h" | 33 #include "ui/base/l10n/l10n_util.h" |
| 36 #include "url/gurl.h" | 34 #include "url/gurl.h" |
| 37 | 35 |
| 36 using content::RenderFrameHost; |
| 37 using content::WebContents; |
| 38 |
| 38 namespace { | 39 namespace { |
| 39 | 40 |
| 40 Browser* GetBrowser() { | 41 Browser* GetBrowser() { |
| 41 chrome::ScopedTabbedBrowserDisplayer browser_displayer( | 42 chrome::ScopedTabbedBrowserDisplayer browser_displayer( |
| 42 ProfileManager::GetActiveUserProfile()); | 43 ProfileManager::GetActiveUserProfile()); |
| 43 DCHECK(browser_displayer.browser()); | 44 DCHECK(browser_displayer.browser()); |
| 44 return browser_displayer.browser(); | 45 return browser_displayer.browser(); |
| 45 } | 46 } |
| 46 | 47 |
| 47 base::string16 GetDeviceName(scoped_refptr<device::UsbDevice> device) { | 48 base::string16 GetDeviceName(scoped_refptr<device::UsbDevice> device) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 64 base::ASCIIToUTF16(base::StringPrintf("%04x", product_id))); | 65 base::ASCIIToUTF16(base::StringPrintf("%04x", product_id))); |
| 65 } | 66 } |
| 66 } | 67 } |
| 67 | 68 |
| 68 return device_name; | 69 return device_name; |
| 69 } | 70 } |
| 70 | 71 |
| 71 } // namespace | 72 } // namespace |
| 72 | 73 |
| 73 UsbChooserController::UsbChooserController( | 74 UsbChooserController::UsbChooserController( |
| 74 content::RenderFrameHost* owner, | 75 RenderFrameHost* render_frame_host, |
| 75 mojo::Array<device::usb::DeviceFilterPtr> device_filters, | 76 mojo::Array<device::usb::DeviceFilterPtr> device_filters, |
| 76 content::RenderFrameHost* render_frame_host, | |
| 77 const device::usb::ChooserService::GetPermissionCallback& callback) | 77 const device::usb::ChooserService::GetPermissionCallback& callback) |
| 78 : ChooserController(owner, | 78 : ChooserController(render_frame_host, |
| 79 IDS_USB_DEVICE_CHOOSER_PROMPT_ORIGIN, | 79 IDS_USB_DEVICE_CHOOSER_PROMPT_ORIGIN, |
| 80 IDS_USB_DEVICE_CHOOSER_PROMPT_EXTENSION_NAME), | 80 IDS_USB_DEVICE_CHOOSER_PROMPT_EXTENSION_NAME), |
| 81 render_frame_host_(render_frame_host), | 81 render_frame_host_(render_frame_host), |
| 82 callback_(callback), | 82 callback_(callback), |
| 83 usb_service_observer_(this), | 83 usb_service_observer_(this), |
| 84 weak_factory_(this) { | 84 weak_factory_(this) { |
| 85 device::UsbService* usb_service = | 85 device::UsbService* usb_service = |
| 86 device::DeviceClient::Get()->GetUsbService(); | 86 device::DeviceClient::Get()->GetUsbService(); |
| 87 if (!usb_service) | 87 if (!usb_service) |
| 88 return; | 88 return; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 void UsbChooserController::RefreshOptions() {} | 134 void UsbChooserController::RefreshOptions() {} |
| 135 | 135 |
| 136 base::string16 UsbChooserController::GetStatus() const { | 136 base::string16 UsbChooserController::GetStatus() const { |
| 137 return base::string16(); | 137 return base::string16(); |
| 138 } | 138 } |
| 139 | 139 |
| 140 void UsbChooserController::Select(const std::vector<size_t>& indices) { | 140 void UsbChooserController::Select(const std::vector<size_t>& indices) { |
| 141 DCHECK_EQ(1u, indices.size()); | 141 DCHECK_EQ(1u, indices.size()); |
| 142 size_t index = indices[0]; | 142 size_t index = indices[0]; |
| 143 DCHECK_LT(index, devices_.size()); | 143 DCHECK_LT(index, devices_.size()); |
| 144 content::WebContents* web_contents = | 144 WebContents* web_contents = |
| 145 content::WebContents::FromRenderFrameHost(render_frame_host_); | 145 WebContents::FromRenderFrameHost(render_frame_host_); |
| 146 GURL embedding_origin = | 146 GURL embedding_origin = |
| 147 web_contents->GetMainFrame()->GetLastCommittedURL().GetOrigin(); | 147 web_contents->GetMainFrame()->GetLastCommittedURL().GetOrigin(); |
| 148 Profile* profile = | 148 Profile* profile = |
| 149 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 149 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 150 UsbChooserContext* chooser_context = | 150 UsbChooserContext* chooser_context = |
| 151 UsbChooserContextFactory::GetForProfile(profile); | 151 UsbChooserContextFactory::GetForProfile(profile); |
| 152 chooser_context->GrantDevicePermission( | 152 chooser_context->GrantDevicePermission( |
| 153 render_frame_host_->GetLastCommittedURL().GetOrigin(), embedding_origin, | 153 render_frame_host_->GetLastCommittedURL().GetOrigin(), embedding_origin, |
| 154 devices_[index].first->guid()); | 154 devices_[index].first->guid()); |
| 155 | 155 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 devices_.push_back(std::make_pair(device, device_name)); | 216 devices_.push_back(std::make_pair(device, device_name)); |
| 217 ++device_name_map_[device_name]; | 217 ++device_name_map_[device_name]; |
| 218 } | 218 } |
| 219 } | 219 } |
| 220 if (view()) | 220 if (view()) |
| 221 view()->OnOptionsInitialized(); | 221 view()->OnOptionsInitialized(); |
| 222 } | 222 } |
| 223 | 223 |
| 224 bool UsbChooserController::DisplayDevice( | 224 bool UsbChooserController::DisplayDevice( |
| 225 scoped_refptr<device::UsbDevice> device) const { | 225 scoped_refptr<device::UsbDevice> device) const { |
| 226 return device::UsbDeviceFilter::MatchesAny(device, filters_) && | 226 if (!device::UsbDeviceFilter::MatchesAny(device, filters_)) |
| 227 !UsbBlocklist::Get().IsExcluded(device) && | 227 return false; |
| 228 (base::CommandLine::ForCurrentProcess()->HasSwitch( | 228 |
| 229 switches::kDisableWebUsbSecurity) || | 229 if (UsbBlocklist::Get().IsExcluded(device)) |
| 230 device::FindInWebUsbAllowedOrigins( | 230 return false; |
| 231 device->webusb_allowed_origins(), | 231 |
| 232 render_frame_host_->GetLastCommittedURL().GetOrigin())); | 232 // Embedded frames must have their origin in the list provided by the device. |
| 233 RenderFrameHost* main_frame = |
| 234 WebContents::FromRenderFrameHost(render_frame_host_)->GetMainFrame(); |
| 235 if (render_frame_host_ != main_frame) { |
| 236 return device::FindInWebUsbAllowedOrigins( |
| 237 device->webusb_allowed_origins(), |
| 238 render_frame_host_->GetLastCommittedURL().GetOrigin()); |
| 239 } |
| 240 |
| 241 return true; |
| 233 } | 242 } |
| OLD | NEW |