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 "content/renderer/usb/web_usb_client_impl.h"
8 #include "base/callback.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/move.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "content/child/scoped_web_callbacks.h"
13 #include "content/renderer/usb/type_converters.h"
14 #include "content/renderer/usb/web_usb_device_impl.h"
15 #include "device/devices_app/public/cpp/constants.h"
16 #include "mojo/application/public/cpp/connect.h"
17 #include "mojo/application/public/interfaces/shell.mojom.h"
18 #include "third_party/WebKit/public/platform/WebCallbacks.h"
19 #include "third_party/WebKit/public/platform/WebPassOwnPtr.h"
20 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBDeviceFilter.h"
21 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBDeviceInfo.h"
22 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBDeviceRequestOptions.h"
23 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBError.h"
24 #include "third_party/mojo/src/mojo/public/cpp/bindings/array.h"
25 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h"
31 const char kNoServiceError
[] = "USB service unavailable.";
33 // Generic default rejection handler for any WebUSB callbacks type. Assumes
34 // |CallbacksType| is a blink::WebCallbacks<T, const blink::WebUSBError&>
36 template <typename CallbacksType
>
37 void RejectCallbacksWithError(const blink::WebUSBError
& error
,
38 scoped_ptr
<CallbacksType
> callbacks
) {
39 callbacks
->onError(error
);
42 // Create a new ScopedWebCallbacks for WebUSB client callbacks, defaulting to
43 // a "no service" rejection.
44 template <typename CallbacksType
>
45 ScopedWebCallbacks
<CallbacksType
> MakeScopedUSBCallbacks(
46 CallbacksType
* callbacks
) {
47 return make_scoped_web_callbacks(
49 base::Bind(&RejectCallbacksWithError
<CallbacksType
>,
50 blink::WebUSBError(blink::WebUSBError::Error::Service
,
51 base::UTF8ToUTF16(kNoServiceError
))));
54 void OnGetDevicesComplete(
55 ScopedWebCallbacks
<blink::WebUSBClientGetDevicesCallbacks
> scoped_callbacks
,
56 mojo::ServiceProvider
* device_services
,
57 mojo::Array
<device::usb::DeviceInfoPtr
> results
) {
58 blink::WebVector
<blink::WebUSBDevice
*>* devices
=
59 new blink::WebVector
<blink::WebUSBDevice
*>(results
.size());
60 for (size_t i
= 0; i
< results
.size(); ++i
) {
61 device::usb::DeviceManagerPtr device_manager
;
62 mojo::ConnectToService(device_services
, &device_manager
);
63 (*devices
)[i
] = new WebUSBDeviceImpl(
64 device_manager
.Pass(),
65 mojo::ConvertTo
<blink::WebUSBDeviceInfo
>(results
[i
]));
67 scoped_callbacks
.PassCallbacks()->onSuccess(blink::adoptWebPtr(devices
));
72 WebUSBClientImpl::WebUSBClientImpl(mojo::ServiceProviderPtr device_services
)
73 : device_services_(device_services
.Pass()) {
74 mojo::ConnectToService(device_services_
.get(), &device_manager_
);
77 WebUSBClientImpl::~WebUSBClientImpl() {}
79 void WebUSBClientImpl::getDevices(
80 blink::WebUSBClientGetDevicesCallbacks
* callbacks
) {
81 auto scoped_callbacks
= MakeScopedUSBCallbacks(callbacks
);
82 // TODO(rockot): Remove this once DeviceManager is updated. It should no
83 // longer take enumeration options.
84 device::usb::EnumerationOptionsPtr options
=
85 device::usb::EnumerationOptions::New();
86 options
->filters
= mojo::Array
<device::usb::DeviceFilterPtr
>::New(0);
87 device_manager_
->GetDevices(
89 base::Bind(&OnGetDevicesComplete
, base::Passed(&scoped_callbacks
),
90 base::Unretained(device_services_
.get())));
93 void WebUSBClientImpl::requestDevice(
94 const blink::WebUSBDeviceRequestOptions
& options
,
95 blink::WebUSBClientRequestDeviceCallbacks
* callbacks
) {
96 callbacks
->onError(blink::WebUSBError(blink::WebUSBError::Error::Service
,
97 base::UTF8ToUTF16("Not implemented.")));
101 void WebUSBClientImpl::setObserver(Observer
* observer
) {
103 // Set up two sequential calls to GetDeviceChanges to avoid latency.
104 device_manager_
->GetDeviceChanges(base::Bind(
105 &WebUSBClientImpl::OnDeviceChangeNotification
, base::Unretained(this)));
106 device_manager_
->GetDeviceChanges(base::Bind(
107 &WebUSBClientImpl::OnDeviceChangeNotification
, base::Unretained(this)));
110 observer_
= observer
;
113 void WebUSBClientImpl::OnDeviceChangeNotification(
114 device::usb::DeviceChangeNotificationPtr notification
) {
118 device_manager_
->GetDeviceChanges(base::Bind(
119 &WebUSBClientImpl::OnDeviceChangeNotification
, base::Unretained(this)));
120 for (size_t i
= 0; i
< notification
->devices_added
.size(); ++i
) {
121 device::usb::DeviceManagerPtr device_manager
;
122 mojo::ConnectToService(device_services_
.get(), &device_manager
);
123 observer_
->onDeviceConnected(blink::adoptWebPtr(new WebUSBDeviceImpl(
124 device_manager
.Pass(), mojo::ConvertTo
<blink::WebUSBDeviceInfo
>(
125 notification
->devices_added
[i
]))));
127 for (size_t i
= 0; i
< notification
->devices_removed
.size(); ++i
) {
128 device::usb::DeviceManagerPtr device_manager
;
129 mojo::ConnectToService(device_services_
.get(), &device_manager
);
130 observer_
->onDeviceDisconnected(blink::adoptWebPtr(new WebUSBDeviceImpl(
131 device_manager
.Pass(), mojo::ConvertTo
<blink::WebUSBDeviceInfo
>(
132 notification
->devices_removed
[i
]))));
136 } // namespace content