| 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/web_usb_permission_provider.h" | 5 #include "chrome/browser/usb/web_usb_permission_provider.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/stl_util.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/usb/usb_chooser_context.h" | 13 #include "chrome/browser/usb/usb_chooser_context.h" |
| 13 #include "chrome/browser/usb/usb_chooser_context_factory.h" | 14 #include "chrome/browser/usb/usb_chooser_context_factory.h" |
| 14 #include "chrome/browser/usb/usb_tab_helper.h" | 15 #include "chrome/browser/usb/usb_tab_helper.h" |
| 15 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
| 16 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/browser/render_frame_host.h" | 18 #include "content/public/browser/render_frame_host.h" |
| 18 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
| 19 #include "device/usb/public/interfaces/device.mojom.h" | 20 #include "device/usb/usb_device.h" |
| 21 #include "device/usb/webusb_descriptors.h" |
| 20 | 22 |
| 21 using content::WebContents; | 23 using content::WebContents; |
| 22 using device::usb::WebUsbDescriptorSet; | |
| 23 using device::usb::WebUsbConfigurationSubsetPtr; | |
| 24 using device::usb::WebUsbFunctionSubsetPtr; | |
| 25 | 24 |
| 26 namespace { | 25 namespace { |
| 27 | 26 |
| 28 bool FindOriginInDescriptorSet(const WebUsbDescriptorSet* set, | 27 bool FindOriginInDescriptorSet(const device::WebUsbAllowedOrigins* set, |
| 29 const GURL& origin, | 28 const GURL& origin, |
| 30 const uint8_t* configuration_value, | 29 const uint8_t* configuration_value, |
| 31 const uint8_t* first_interface) { | 30 const uint8_t* first_interface) { |
| 32 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 31 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 33 switches::kDisableWebUsbSecurity)) | 32 switches::kDisableWebUsbSecurity)) |
| 34 return true; | 33 return true; |
| 35 | 34 |
| 36 if (!set) | 35 if (!set) |
| 37 return false; | 36 return false; |
| 38 for (size_t i = 0; i < set->origins.size(); ++i) | 37 if (ContainsValue(set->origins, origin)) |
| 39 if (origin.spec() == set->origins[i]) | 38 return true; |
| 39 for (const auto& configuration : set->configurations) { |
| 40 if (configuration_value && |
| 41 *configuration_value != configuration.configuration_value) |
| 42 continue; |
| 43 if (ContainsValue(configuration.origins, origin)) |
| 40 return true; | 44 return true; |
| 41 for (size_t i = 0; i < set->configurations.size(); ++i) { | 45 for (const auto& function : configuration.functions) { |
| 42 const WebUsbConfigurationSubsetPtr& config = set->configurations[i]; | 46 if (first_interface && *first_interface != function.first_interface) |
| 43 if (configuration_value && | 47 continue; |
| 44 *configuration_value != config->configuration_value) | 48 if (ContainsValue(function.origins, origin)) |
| 45 continue; | |
| 46 for (size_t j = 0; i < config->origins.size(); ++j) | |
| 47 if (origin.spec() == config->origins[j]) | |
| 48 return true; | 49 return true; |
| 49 for (size_t j = 0; j < config->functions.size(); ++j) { | |
| 50 const WebUsbFunctionSubsetPtr& function = config->functions[j]; | |
| 51 if (first_interface && *first_interface != function->first_interface) | |
| 52 continue; | |
| 53 for (size_t k = 0; k < function->origins.size(); ++k) | |
| 54 if (origin.spec() == function->origins[k]) | |
| 55 return true; | |
| 56 } | 50 } |
| 57 } | 51 } |
| 58 return false; | 52 return false; |
| 59 } | 53 } |
| 60 | 54 |
| 61 } // namespace | 55 } // namespace |
| 62 | 56 |
| 63 WebUSBPermissionProvider::WebUSBPermissionProvider( | 57 WebUSBPermissionProvider::WebUSBPermissionProvider( |
| 64 content::RenderFrameHost* render_frame_host) | 58 content::RenderFrameHost* render_frame_host) |
| 65 : render_frame_host_(render_frame_host), weak_factory_(this) { | 59 : render_frame_host_(render_frame_host), weak_factory_(this) { |
| 66 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 60 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 67 DCHECK(render_frame_host_); | 61 DCHECK(render_frame_host_); |
| 68 } | 62 } |
| 69 | 63 |
| 70 WebUSBPermissionProvider::~WebUSBPermissionProvider() {} | 64 WebUSBPermissionProvider::~WebUSBPermissionProvider() {} |
| 71 | 65 |
| 72 base::WeakPtr<device::usb::PermissionProvider> | 66 base::WeakPtr<device::usb::PermissionProvider> |
| 73 WebUSBPermissionProvider::GetWeakPtr() { | 67 WebUSBPermissionProvider::GetWeakPtr() { |
| 74 return weak_factory_.GetWeakPtr(); | 68 return weak_factory_.GetWeakPtr(); |
| 75 } | 69 } |
| 76 | 70 |
| 77 bool WebUSBPermissionProvider::HasDevicePermission( | 71 bool WebUSBPermissionProvider::HasDevicePermission( |
| 78 const device::usb::DeviceInfo& device_info) const { | 72 scoped_refptr<const device::UsbDevice> device) const { |
| 79 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 73 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 80 WebContents* web_contents = | 74 WebContents* web_contents = |
| 81 WebContents::FromRenderFrameHost(render_frame_host_); | 75 WebContents::FromRenderFrameHost(render_frame_host_); |
| 82 GURL embedding_origin = | 76 GURL embedding_origin = |
| 83 web_contents->GetMainFrame()->GetLastCommittedURL().GetOrigin(); | 77 web_contents->GetMainFrame()->GetLastCommittedURL().GetOrigin(); |
| 84 GURL requesting_origin = | 78 GURL requesting_origin = |
| 85 render_frame_host_->GetLastCommittedURL().GetOrigin(); | 79 render_frame_host_->GetLastCommittedURL().GetOrigin(); |
| 86 Profile* profile = | 80 Profile* profile = |
| 87 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 81 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 88 UsbChooserContext* chooser_context = | 82 UsbChooserContext* chooser_context = |
| 89 UsbChooserContextFactory::GetForProfile(profile); | 83 UsbChooserContextFactory::GetForProfile(profile); |
| 90 | 84 |
| 91 return FindOriginInDescriptorSet(device_info.webusb_allowed_origins.get(), | 85 return FindOriginInDescriptorSet(device->webusb_allowed_origins(), |
| 92 requesting_origin, nullptr, nullptr) && | 86 requesting_origin, nullptr, nullptr) && |
| 93 chooser_context->HasDevicePermission(requesting_origin, | 87 chooser_context->HasDevicePermission(requesting_origin, |
| 94 embedding_origin, device_info); | 88 embedding_origin, device); |
| 95 } | 89 } |
| 96 | 90 |
| 97 bool WebUSBPermissionProvider::HasConfigurationPermission( | 91 bool WebUSBPermissionProvider::HasConfigurationPermission( |
| 98 uint8_t requested_configuration_value, | 92 uint8_t requested_configuration_value, |
| 99 const device::usb::DeviceInfo& device_info) const { | 93 scoped_refptr<const device::UsbDevice> device) const { |
| 100 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 94 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 101 return FindOriginInDescriptorSet( | 95 return FindOriginInDescriptorSet( |
| 102 device_info.webusb_allowed_origins.get(), | 96 device->webusb_allowed_origins(), |
| 103 render_frame_host_->GetLastCommittedURL().GetOrigin(), | 97 render_frame_host_->GetLastCommittedURL().GetOrigin(), |
| 104 &requested_configuration_value, nullptr); | 98 &requested_configuration_value, nullptr); |
| 105 } | 99 } |
| 106 | 100 |
| 107 bool WebUSBPermissionProvider::HasFunctionPermission( | 101 bool WebUSBPermissionProvider::HasFunctionPermission( |
| 108 uint8_t requested_function, | 102 uint8_t requested_function, |
| 109 uint8_t configuration_value, | 103 uint8_t configuration_value, |
| 110 const device::usb::DeviceInfo& device_info) const { | 104 scoped_refptr<const device::UsbDevice> device) const { |
| 111 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 105 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 112 return FindOriginInDescriptorSet( | 106 return FindOriginInDescriptorSet( |
| 113 device_info.webusb_allowed_origins.get(), | 107 device->webusb_allowed_origins(), |
| 114 render_frame_host_->GetLastCommittedURL().GetOrigin(), | 108 render_frame_host_->GetLastCommittedURL().GetOrigin(), |
| 115 &configuration_value, &requested_function); | 109 &configuration_value, &requested_function); |
| 116 } | 110 } |
| 117 | 111 |
| 118 void WebUSBPermissionProvider::IncrementConnectionCount() { | 112 void WebUSBPermissionProvider::IncrementConnectionCount() { |
| 119 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 113 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 120 WebContents* web_contents = | 114 WebContents* web_contents = |
| 121 WebContents::FromRenderFrameHost(render_frame_host_); | 115 WebContents::FromRenderFrameHost(render_frame_host_); |
| 122 UsbTabHelper* tab_helper = UsbTabHelper::FromWebContents(web_contents); | 116 UsbTabHelper* tab_helper = UsbTabHelper::FromWebContents(web_contents); |
| 123 tab_helper->IncrementConnectionCount(); | 117 tab_helper->IncrementConnectionCount(); |
| 124 } | 118 } |
| 125 | 119 |
| 126 void WebUSBPermissionProvider::DecrementConnectionCount() { | 120 void WebUSBPermissionProvider::DecrementConnectionCount() { |
| 127 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 121 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 128 WebContents* web_contents = | 122 WebContents* web_contents = |
| 129 WebContents::FromRenderFrameHost(render_frame_host_); | 123 WebContents::FromRenderFrameHost(render_frame_host_); |
| 130 UsbTabHelper* tab_helper = UsbTabHelper::FromWebContents(web_contents); | 124 UsbTabHelper* tab_helper = UsbTabHelper::FromWebContents(web_contents); |
| 131 tab_helper->DecrementConnectionCount(); | 125 tab_helper->DecrementConnectionCount(); |
| 132 } | 126 } |
| OLD | NEW |