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
3 // found in the LICENSE file.
5 #include "chrome/browser/usb/web_usb_permission_provider.h"
7 #include "base/command_line.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/common/chrome_switches.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/render_frame_host.h"
12 #include "device/core/device_client.h"
16 bool FindOriginInDescriptorSet(const device::usb::WebUsbDescriptorSet
* set
,
20 for (size_t i
= 0; i
< set
->origins
.size(); ++i
) {
21 if (origin
.spec() == set
->origins
[i
])
24 for (size_t i
= 0; i
< set
->configurations
.size(); ++i
) {
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
])
31 for (size_t j
= 0; j
< config
->functions
.size(); ++j
) {
32 const device::usb::WebUsbFunctionSubsetPtr
& function
=
34 for (size_t k
= 0; k
< function
->origins
.size(); ++k
) {
35 if (origin
.spec() == function
->origins
[k
])
43 bool EnableWebUsbOnAnyOrigin() {
44 return base::CommandLine::ForCurrentProcess()->HasSwitch(
45 switches::kEnableWebUsbOnAnyOrigin
);
51 void WebUSBPermissionProvider::Create(
52 content::RenderFrameHost
* render_frame_host
,
53 mojo::InterfaceRequest
<PermissionProvider
> request
) {
54 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
55 DCHECK(render_frame_host
);
57 // The created object is strongly bound to (and owned by) the pipe.
58 new WebUSBPermissionProvider(render_frame_host
, request
.Pass());
61 WebUSBPermissionProvider::~WebUSBPermissionProvider() {}
63 WebUSBPermissionProvider::WebUSBPermissionProvider(
64 content::RenderFrameHost
* render_frame_host
,
65 mojo::InterfaceRequest
<PermissionProvider
> request
)
66 : binding_(this, request
.Pass()),
67 render_frame_host_(render_frame_host
) {}
69 void WebUSBPermissionProvider::HasDevicePermission(
70 mojo::Array
<device::usb::DeviceInfoPtr
> requested_devices
,
71 const HasDevicePermissionCallback
& callback
) {
72 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
73 GURL origin
= render_frame_host_
->GetLastCommittedURL().GetOrigin();
75 mojo::Array
<mojo::String
> allowed_guids(0);
76 for (size_t i
= 0; i
< requested_devices
.size(); ++i
) {
77 const device::usb::DeviceInfoPtr
& device
= requested_devices
[i
];
78 if (FindOriginInDescriptorSet(device
->webusb_allowed_origins
.get(),
80 EnableWebUsbOnAnyOrigin())
81 allowed_guids
.push_back(device
->guid
);
83 callback
.Run(allowed_guids
.Pass());