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 | 13 |
14 using device::usb::WebUsbDescriptorSet; | |
15 using device::usb::WebUsbConfigurationSubsetPtr; | |
16 using device::usb::WebUsbFunctionSubsetPtr; | |
17 | |
14 namespace { | 18 namespace { |
15 | 19 |
16 bool FindOriginInDescriptorSet(const device::usb::WebUsbDescriptorSet* set, | 20 bool FindOriginInDescriptorSet(const WebUsbDescriptorSet* set, |
17 const GURL& origin) { | 21 const GURL& origin, |
22 const uint8_t* configuration_value, | |
23 const uint8_t* interface_number) { | |
18 if (!set) | 24 if (!set) |
19 return false; | 25 return false; |
20 for (size_t i = 0; i < set->origins.size(); ++i) { | 26 for (size_t i = 0; i < set->origins.size(); ++i) { |
21 if (origin.spec() == set->origins[i]) | 27 if (origin.spec() == set->origins[i]) |
22 return true; | 28 return true; |
23 } | 29 } |
24 for (size_t i = 0; i < set->configurations.size(); ++i) { | 30 for (size_t i = 0; i < set->configurations.size(); ++i) { |
25 const device::usb::WebUsbConfigurationSubsetPtr& config = | 31 const device::usb::WebUsbConfigurationSubsetPtr& config = |
26 set->configurations[i]; | 32 set->configurations[i]; |
33 if (configuration_value && | |
34 *configuration_value != config->configuration_value) | |
35 continue; | |
27 for (size_t j = 0; i < config->origins.size(); ++j) { | 36 for (size_t j = 0; i < config->origins.size(); ++j) { |
28 if (origin.spec() == config->origins[j]) | 37 if (origin.spec() == config->origins[j]) |
29 return true; | 38 return true; |
30 } | 39 } |
31 for (size_t j = 0; j < config->functions.size(); ++j) { | 40 for (size_t j = 0; j < config->functions.size(); ++j) { |
32 const device::usb::WebUsbFunctionSubsetPtr& function = | 41 const device::usb::WebUsbFunctionSubsetPtr& function = |
33 config->functions[j]; | 42 config->functions[j]; |
43 // TODO(reillyg): Implement support for Interface Association Descriptors | |
44 // so that this check will match associated interfaces. | |
45 if (interface_number && *interface_number != function->first_interface) | |
46 continue; | |
34 for (size_t k = 0; k < function->origins.size(); ++k) { | 47 for (size_t k = 0; k < function->origins.size(); ++k) { |
35 if (origin.spec() == function->origins[k]) | 48 if (origin.spec() == function->origins[k]) |
36 return true; | 49 return true; |
37 } | 50 } |
38 } | 51 } |
39 } | 52 } |
40 return false; | 53 return false; |
41 } | 54 } |
42 | 55 |
43 bool EnableWebUsbOnAnyOrigin() { | 56 bool EnableWebUsbOnAnyOrigin() { |
44 return base::CommandLine::ForCurrentProcess()->HasSwitch( | 57 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
45 switches::kEnableWebUsbOnAnyOrigin); | 58 switches::kEnableWebUsbOnAnyOrigin); |
46 } | 59 } |
47 | 60 |
48 } // namespace | 61 } // namespace |
49 | 62 |
50 // static | 63 // static |
51 void WebUSBPermissionProvider::Create( | 64 void WebUSBPermissionProvider::Create( |
52 content::RenderFrameHost* render_frame_host, | 65 content::RenderFrameHost* render_frame_host, |
53 mojo::InterfaceRequest<PermissionProvider> request) { | 66 mojo::InterfaceRequest<PermissionProvider> request) { |
54 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 67 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
55 DCHECK(render_frame_host); | 68 DCHECK(render_frame_host); |
56 | 69 |
57 // The created object is strongly bound to (and owned by) the pipe. | 70 // The created object is owned by its bindings. |
58 new WebUSBPermissionProvider(render_frame_host, request.Pass()); | 71 new WebUSBPermissionProvider(render_frame_host, request.Pass()); |
59 } | 72 } |
60 | 73 |
61 WebUSBPermissionProvider::~WebUSBPermissionProvider() {} | 74 WebUSBPermissionProvider::~WebUSBPermissionProvider() {} |
62 | 75 |
63 WebUSBPermissionProvider::WebUSBPermissionProvider( | 76 WebUSBPermissionProvider::WebUSBPermissionProvider( |
64 content::RenderFrameHost* render_frame_host, | 77 content::RenderFrameHost* render_frame_host, |
65 mojo::InterfaceRequest<PermissionProvider> request) | 78 mojo::InterfaceRequest<PermissionProvider> request) |
66 : binding_(this, request.Pass()), | 79 : render_frame_host_(render_frame_host) { |
67 render_frame_host_(render_frame_host) {} | 80 bindings_.set_connection_error_handler(base::Bind( |
81 &WebUSBPermissionProvider::OnConnectionError, base::Unretained(this))); | |
82 bindings_.AddBinding(this, request.Pass()); | |
83 } | |
68 | 84 |
69 void WebUSBPermissionProvider::HasDevicePermission( | 85 void WebUSBPermissionProvider::HasDevicePermission( |
70 mojo::Array<device::usb::DeviceInfoPtr> requested_devices, | 86 mojo::Array<device::usb::DeviceInfoPtr> requested_devices, |
71 const HasDevicePermissionCallback& callback) { | 87 const HasDevicePermissionCallback& callback) { |
72 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 88 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
73 GURL origin = render_frame_host_->GetLastCommittedURL().GetOrigin(); | 89 GURL origin = render_frame_host_->GetLastCommittedURL().GetOrigin(); |
74 | 90 |
75 mojo::Array<mojo::String> allowed_guids(0); | 91 mojo::Array<mojo::String> allowed_guids(0); |
76 for (size_t i = 0; i < requested_devices.size(); ++i) { | 92 for (size_t i = 0; i < requested_devices.size(); ++i) { |
77 const device::usb::DeviceInfoPtr& device = requested_devices[i]; | 93 const device::usb::DeviceInfoPtr& device = requested_devices[i]; |
78 if (FindOriginInDescriptorSet(device->webusb_allowed_origins.get(), | 94 if (FindOriginInDescriptorSet(device->webusb_allowed_origins.get(), origin, |
79 origin) && | 95 nullptr, nullptr) && |
80 EnableWebUsbOnAnyOrigin()) | 96 EnableWebUsbOnAnyOrigin()) |
81 allowed_guids.push_back(device->guid); | 97 allowed_guids.push_back(device->guid); |
82 } | 98 } |
83 callback.Run(allowed_guids.Pass()); | 99 callback.Run(allowed_guids.Pass()); |
84 } | 100 } |
101 | |
102 void WebUSBPermissionProvider::HasConfigurationPermission( | |
103 uint8_t requested_configuration_value, | |
104 device::usb::DeviceInfoPtr device, | |
105 const HasInterfacePermissionCallback& callback) { | |
106 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
107 callback.Run(FindOriginInDescriptorSet( | |
108 device->webusb_allowed_origins.get(), | |
109 render_frame_host_->GetLastCommittedURL().GetOrigin(), | |
110 &requested_configuration_value, nullptr)); | |
111 } | |
112 | |
113 void WebUSBPermissionProvider::HasInterfacePermission( | |
114 uint8_t requested_interface, | |
115 uint8_t configuration_value, | |
116 device::usb::DeviceInfoPtr device, | |
117 const HasInterfacePermissionCallback& callback) { | |
118 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
119 callback.Run(FindOriginInDescriptorSet( | |
120 device->webusb_allowed_origins.get(), | |
121 render_frame_host_->GetLastCommittedURL().GetOrigin(), | |
122 &configuration_value, &requested_interface)); | |
123 } | |
124 | |
125 void WebUSBPermissionProvider::Bind( | |
126 mojo::InterfaceRequest<device::usb::PermissionProvider> request) { | |
127 bindings_.AddBinding(this, request.Pass()); | |
128 } | |
129 | |
130 void WebUSBPermissionProvider::OnConnectionError() { | |
131 if (bindings_.empty()) | |
132 delete this; | |
Ken Rockot(use gerrit already)
2015/09/28 17:43:40
Ah right - this seems like a good solution that do
| |
133 } | |
OLD | NEW |