| 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 "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/common/chrome_switches.h" | 9 #include "chrome/common/chrome_switches.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/render_frame_host.h" | 11 #include "content/public/browser/render_frame_host.h" |
| 12 #include "device/core/device_client.h" | 12 #include "device/core/device_client.h" |
| 13 #include "device/usb/usb_device.h" | |
| 14 #include "device/usb/usb_service.h" | |
| 15 #include "device/usb/webusb_descriptors.h" | |
| 16 | 13 |
| 17 namespace { | 14 namespace { |
| 18 | 15 |
| 19 bool FindOriginInDescriptorSet(const device::WebUsbDescriptorSet* set, | 16 bool FindOriginInDescriptorSet(const device::usb::WebUsbDescriptorSet* set, |
| 20 const GURL& origin) { | 17 const GURL& origin) { |
| 21 if (!set) | 18 if (!set) |
| 22 return false; | 19 return false; |
| 23 if (ContainsValue(set->origins, origin)) | 20 for (size_t i = 0; i < set->origins.size(); ++i) { |
| 24 return true; | 21 if (origin.spec() == set->origins[i]) |
| 25 for (const device::WebUsbConfigurationSubset& config_subset : | |
| 26 set->configurations) { | |
| 27 if (ContainsValue(config_subset.origins, origin)) | |
| 28 return true; | 22 return true; |
| 29 for (const device::WebUsbFunctionSubset& function_subset : | 23 } |
| 30 config_subset.functions) { | 24 for (size_t i = 0; i < set->configurations.size(); ++i) { |
| 31 if (ContainsValue(function_subset.origins, origin)) | 25 const device::usb::WebUsbConfigurationSubsetPtr& config = |
| 26 set->configurations[i]; |
| 27 for (size_t j = 0; i < config->origins.size(); ++j) { |
| 28 if (origin.spec() == config->origins[j]) |
| 32 return true; | 29 return true; |
| 33 } | 30 } |
| 31 for (size_t j = 0; j < config->functions.size(); ++j) { |
| 32 const device::usb::WebUsbFunctionSubsetPtr& function = |
| 33 config->functions[j]; |
| 34 for (size_t k = 0; k < function->origins.size(); ++k) { |
| 35 if (origin.spec() == function->origins[k]) |
| 36 return true; |
| 37 } |
| 38 } |
| 34 } | 39 } |
| 35 return false; | 40 return false; |
| 36 } | 41 } |
| 37 | 42 |
| 38 bool EnableWebUsbOnAnyOrigin() { | 43 bool EnableWebUsbOnAnyOrigin() { |
| 39 return base::CommandLine::ForCurrentProcess()->HasSwitch( | 44 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 40 switches::kEnableWebUsbOnAnyOrigin); | 45 switches::kEnableWebUsbOnAnyOrigin); |
| 41 } | 46 } |
| 42 | 47 |
| 43 } // namespace | 48 } // namespace |
| (...skipping 11 matching lines...) Expand all Loading... |
| 55 | 60 |
| 56 WebUSBPermissionProvider::~WebUSBPermissionProvider() {} | 61 WebUSBPermissionProvider::~WebUSBPermissionProvider() {} |
| 57 | 62 |
| 58 WebUSBPermissionProvider::WebUSBPermissionProvider( | 63 WebUSBPermissionProvider::WebUSBPermissionProvider( |
| 59 content::RenderFrameHost* render_frame_host, | 64 content::RenderFrameHost* render_frame_host, |
| 60 mojo::InterfaceRequest<PermissionProvider> request) | 65 mojo::InterfaceRequest<PermissionProvider> request) |
| 61 : binding_(this, request.Pass()), | 66 : binding_(this, request.Pass()), |
| 62 render_frame_host_(render_frame_host) {} | 67 render_frame_host_(render_frame_host) {} |
| 63 | 68 |
| 64 void WebUSBPermissionProvider::HasDevicePermission( | 69 void WebUSBPermissionProvider::HasDevicePermission( |
| 65 mojo::Array<mojo::String> requested_guids, | 70 mojo::Array<device::usb::DeviceInfoPtr> requested_devices, |
| 66 const HasDevicePermissionCallback& callback) { | 71 const HasDevicePermissionCallback& callback) { |
| 67 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 72 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 68 device::UsbService* usb_service = | |
| 69 device::DeviceClient::Get()->GetUsbService(); | |
| 70 GURL origin = render_frame_host_->GetLastCommittedURL().GetOrigin(); | 73 GURL origin = render_frame_host_->GetLastCommittedURL().GetOrigin(); |
| 71 | 74 |
| 72 mojo::Array<mojo::String> allowed_guids(0); | 75 mojo::Array<mojo::String> allowed_guids(0); |
| 73 for (size_t i = 0; i < requested_guids.size(); ++i) { | 76 for (size_t i = 0; i < requested_devices.size(); ++i) { |
| 74 const mojo::String& guid = requested_guids[i]; | 77 const device::usb::DeviceInfoPtr& device = requested_devices[i]; |
| 75 scoped_refptr<device::UsbDevice> device = usb_service->GetDevice(guid); | 78 if (FindOriginInDescriptorSet(device->webusb_allowed_origins.get(), |
| 76 if (FindOriginInDescriptorSet(device->webusb_allowed_origins(), origin) && | 79 origin) && |
| 77 EnableWebUsbOnAnyOrigin()) | 80 EnableWebUsbOnAnyOrigin()) |
| 78 allowed_guids.push_back(guid); | 81 allowed_guids.push_back(device->guid); |
| 79 } | 82 } |
| 80 callback.Run(allowed_guids.Pass()); | 83 callback.Run(allowed_guids.Pass()); |
| 81 } | 84 } |
| OLD | NEW |