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/stl_util.h" | 10 #include "base/stl_util.h" |
11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/usb/usb_blocklist.h" | 12 #include "chrome/browser/usb/usb_blocklist.h" |
13 #include "chrome/browser/usb/usb_chooser_context.h" | 13 #include "chrome/browser/usb/usb_chooser_context.h" |
14 #include "chrome/browser/usb/usb_chooser_context_factory.h" | 14 #include "chrome/browser/usb/usb_chooser_context_factory.h" |
15 #include "chrome/browser/usb/usb_tab_helper.h" | 15 #include "chrome/browser/usb/usb_tab_helper.h" |
16 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
17 #include "content/public/browser/render_frame_host.h" | 17 #include "content/public/browser/render_frame_host.h" |
18 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
19 #include "device/usb/usb_device.h" | 19 #include "device/usb/usb_device.h" |
20 #include "device/usb/webusb_descriptors.h" | 20 #include "device/usb/webusb_descriptors.h" |
21 | 21 |
22 using content::RenderFrameHost; | 22 using content::RenderFrameHost; |
23 using content::WebContents; | 23 using content::WebContents; |
24 | 24 |
25 namespace { | |
26 | |
27 bool FindOriginInDescriptorSet(const device::WebUsbAllowedOrigins* set, | |
28 const GURL& origin, | |
29 const uint8_t* configuration_value, | |
30 const uint8_t* first_interface) { | |
31 if (!set) | |
32 return false; | |
33 if (base::ContainsValue(set->origins, origin)) | |
34 return true; | |
35 for (const auto& configuration : set->configurations) { | |
36 if (configuration_value && | |
37 *configuration_value != configuration.configuration_value) | |
38 continue; | |
39 if (base::ContainsValue(configuration.origins, origin)) | |
40 return true; | |
41 for (const auto& function : configuration.functions) { | |
42 if (first_interface && *first_interface != function.first_interface) | |
43 continue; | |
44 if (base::ContainsValue(function.origins, origin)) | |
45 return true; | |
46 } | |
47 } | |
48 return false; | |
49 } | |
50 | |
51 } // namespace | |
52 | |
53 // static | 25 // static |
54 bool WebUSBPermissionProvider::HasDevicePermission( | 26 bool WebUSBPermissionProvider::HasDevicePermission( |
55 RenderFrameHost* render_frame_host, | 27 UsbChooserContext* chooser_context, |
| 28 const GURL& requesting_origin, |
| 29 const GURL& embedding_origin, |
| 30 bool is_embedded_frame, |
56 scoped_refptr<const device::UsbDevice> device) { | 31 scoped_refptr<const device::UsbDevice> device) { |
57 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 32 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
58 | 33 |
59 if (UsbBlocklist::Get().IsExcluded(device)) | 34 if (UsbBlocklist::Get().IsExcluded(device)) |
60 return false; | 35 return false; |
61 | 36 |
62 WebContents* web_contents = | |
63 WebContents::FromRenderFrameHost(render_frame_host); | |
64 RenderFrameHost* main_frame = web_contents->GetMainFrame(); | |
65 GURL embedding_origin = main_frame->GetLastCommittedURL().GetOrigin(); | |
66 GURL requesting_origin = render_frame_host->GetLastCommittedURL().GetOrigin(); | |
67 Profile* profile = | |
68 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
69 UsbChooserContext* chooser_context = | |
70 UsbChooserContextFactory::GetForProfile(profile); | |
71 | |
72 if (!chooser_context->HasDevicePermission(requesting_origin, embedding_origin, | 37 if (!chooser_context->HasDevicePermission(requesting_origin, embedding_origin, |
73 device)) { | 38 device)) { |
74 return false; | 39 return false; |
75 } | 40 } |
76 | 41 |
77 // On Android it is not possible to read the WebUSB descriptors until Chrome | 42 // On Android it is not possible to read the WebUSB descriptors until Chrome |
78 // has been granted permission to open it. Instead we grant provisional access | 43 // has been granted permission to open it. Instead we grant provisional access |
79 // to the device and perform the allowed origins check when the client tries | 44 // to the device and perform the allowed origins check when the client tries |
80 // to open it. | 45 // to open it. |
81 if (!device->permission_granted()) | 46 if (!device->permission_granted()) |
82 return true; | 47 return true; |
83 | 48 |
84 // Embedded frames must have their origin in the list provided by the device. | 49 // Embedded frames must have their origin in the list provided by the device. |
85 if (render_frame_host != main_frame) { | 50 if (is_embedded_frame) { |
86 return FindOriginInDescriptorSet(device->webusb_allowed_origins(), | 51 return device::FindInWebUsbAllowedOrigins(device->webusb_allowed_origins(), |
87 requesting_origin, nullptr, nullptr); | 52 requesting_origin, base::nullopt, |
| 53 base::nullopt); |
88 } | 54 } |
89 | 55 |
90 return true; | 56 return true; |
91 } | 57 } |
92 | 58 |
93 WebUSBPermissionProvider::WebUSBPermissionProvider( | 59 WebUSBPermissionProvider::WebUSBPermissionProvider( |
94 RenderFrameHost* render_frame_host) | 60 RenderFrameHost* render_frame_host) |
95 : render_frame_host_(render_frame_host), weak_factory_(this) { | 61 : render_frame_host_(render_frame_host), weak_factory_(this) { |
96 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 62 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
97 DCHECK(render_frame_host_); | 63 DCHECK(render_frame_host_); |
98 } | 64 } |
99 | 65 |
100 WebUSBPermissionProvider::~WebUSBPermissionProvider() {} | 66 WebUSBPermissionProvider::~WebUSBPermissionProvider() {} |
101 | 67 |
102 base::WeakPtr<device::usb::PermissionProvider> | 68 base::WeakPtr<device::usb::PermissionProvider> |
103 WebUSBPermissionProvider::GetWeakPtr() { | 69 WebUSBPermissionProvider::GetWeakPtr() { |
104 return weak_factory_.GetWeakPtr(); | 70 return weak_factory_.GetWeakPtr(); |
105 } | 71 } |
106 | 72 |
107 bool WebUSBPermissionProvider::HasDevicePermission( | 73 bool WebUSBPermissionProvider::HasDevicePermission( |
108 scoped_refptr<const device::UsbDevice> device) const { | 74 scoped_refptr<const device::UsbDevice> device) const { |
109 return HasDevicePermission(render_frame_host_, device); | 75 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 76 |
| 77 WebContents* web_contents = |
| 78 WebContents::FromRenderFrameHost(render_frame_host_); |
| 79 RenderFrameHost* main_frame = web_contents->GetMainFrame(); |
| 80 Profile* profile = |
| 81 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 82 |
| 83 return HasDevicePermission( |
| 84 UsbChooserContextFactory::GetForProfile(profile), |
| 85 render_frame_host_->GetLastCommittedURL().GetOrigin(), |
| 86 main_frame->GetLastCommittedURL().GetOrigin(), |
| 87 render_frame_host_ != main_frame, device); |
110 } | 88 } |
111 | 89 |
112 bool WebUSBPermissionProvider::HasConfigurationPermission( | 90 bool WebUSBPermissionProvider::HasConfigurationPermission( |
113 uint8_t requested_configuration_value, | 91 uint8_t requested_configuration_value, |
114 scoped_refptr<const device::UsbDevice> device) const { | 92 scoped_refptr<const device::UsbDevice> device) const { |
115 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 93 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
116 | 94 |
117 // Embedded frames may only access configurations if their origin in the list | 95 // Embedded frames may only access configurations if their origin in the list |
118 // provided by the device. | 96 // provided by the device. |
119 RenderFrameHost* main_frame = | 97 RenderFrameHost* main_frame = |
120 WebContents::FromRenderFrameHost(render_frame_host_)->GetMainFrame(); | 98 WebContents::FromRenderFrameHost(render_frame_host_)->GetMainFrame(); |
121 if (render_frame_host_ != main_frame) { | 99 if (render_frame_host_ != main_frame) { |
122 return FindOriginInDescriptorSet( | 100 return device::FindInWebUsbAllowedOrigins( |
123 device->webusb_allowed_origins(), | 101 device->webusb_allowed_origins(), |
124 render_frame_host_->GetLastCommittedURL().GetOrigin(), | 102 render_frame_host_->GetLastCommittedURL().GetOrigin(), |
125 &requested_configuration_value, nullptr); | 103 requested_configuration_value, base::nullopt); |
126 } | 104 } |
127 | 105 |
128 return true; | 106 return true; |
129 } | 107 } |
130 | 108 |
131 bool WebUSBPermissionProvider::HasFunctionPermission( | 109 bool WebUSBPermissionProvider::HasFunctionPermission( |
132 uint8_t requested_function, | 110 uint8_t requested_function, |
133 uint8_t configuration_value, | 111 uint8_t configuration_value, |
134 scoped_refptr<const device::UsbDevice> device) const { | 112 scoped_refptr<const device::UsbDevice> device) const { |
135 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 113 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
136 | 114 |
137 // Embedded frames may only access configurations if their origin in the list | 115 // Embedded frames may only access configurations if their origin in the list |
138 // provided by the device. | 116 // provided by the device. |
139 RenderFrameHost* main_frame = | 117 RenderFrameHost* main_frame = |
140 WebContents::FromRenderFrameHost(render_frame_host_)->GetMainFrame(); | 118 WebContents::FromRenderFrameHost(render_frame_host_)->GetMainFrame(); |
141 if (render_frame_host_ != main_frame) { | 119 if (render_frame_host_ != main_frame) { |
142 return FindOriginInDescriptorSet( | 120 return device::FindInWebUsbAllowedOrigins( |
143 device->webusb_allowed_origins(), | 121 device->webusb_allowed_origins(), |
144 render_frame_host_->GetLastCommittedURL().GetOrigin(), | 122 render_frame_host_->GetLastCommittedURL().GetOrigin(), |
145 &configuration_value, &requested_function); | 123 configuration_value, requested_function); |
146 } | 124 } |
147 | 125 |
148 return true; | 126 return true; |
149 } | 127 } |
150 | 128 |
151 void WebUSBPermissionProvider::IncrementConnectionCount() { | 129 void WebUSBPermissionProvider::IncrementConnectionCount() { |
152 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 130 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
153 WebContents* web_contents = | 131 WebContents* web_contents = |
154 WebContents::FromRenderFrameHost(render_frame_host_); | 132 WebContents::FromRenderFrameHost(render_frame_host_); |
155 UsbTabHelper* tab_helper = UsbTabHelper::FromWebContents(web_contents); | 133 UsbTabHelper* tab_helper = UsbTabHelper::FromWebContents(web_contents); |
156 tab_helper->IncrementConnectionCount(render_frame_host_); | 134 tab_helper->IncrementConnectionCount(render_frame_host_); |
157 } | 135 } |
158 | 136 |
159 void WebUSBPermissionProvider::DecrementConnectionCount() { | 137 void WebUSBPermissionProvider::DecrementConnectionCount() { |
160 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 138 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
161 WebContents* web_contents = | 139 WebContents* web_contents = |
162 WebContents::FromRenderFrameHost(render_frame_host_); | 140 WebContents::FromRenderFrameHost(render_frame_host_); |
163 UsbTabHelper* tab_helper = UsbTabHelper::FromWebContents(web_contents); | 141 UsbTabHelper* tab_helper = UsbTabHelper::FromWebContents(web_contents); |
164 tab_helper->DecrementConnectionCount(render_frame_host_); | 142 tab_helper->DecrementConnectionCount(render_frame_host_); |
165 } | 143 } |
OLD | NEW |