OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "content/renderer/usb/web_usb_client_impl.h" |
| 6 |
| 7 #include "base/bind.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/WebUSBDeviceRequestO
ptions.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/error_handler.h" |
| 26 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h" |
| 27 |
| 28 namespace content { |
| 29 |
| 30 namespace { |
| 31 |
| 32 const char kNoServiceError[] = "USB service unavailable."; |
| 33 |
| 34 // Generic default rejection handler for any WebUSB callbacks type. Assumes |
| 35 // |CallbacksType| is a blink::WebCallbacks<T, const blink::WebUSBError&> |
| 36 // for any type |T|. |
| 37 template <typename CallbacksType> |
| 38 void RejectCallbacksWithError(const blink::WebUSBError& error, |
| 39 scoped_ptr<CallbacksType> callbacks) { |
| 40 callbacks->onError(error); |
| 41 } |
| 42 |
| 43 // Create a new ScopedWebCallbacks for WebUSB client callbacks, defaulting to |
| 44 // a "no service" rejection. |
| 45 template <typename CallbacksType> |
| 46 ScopedWebCallbacks<CallbacksType> MakeScopedUSBCallbacks( |
| 47 CallbacksType* callbacks) { |
| 48 return make_scoped_web_callbacks( |
| 49 callbacks, |
| 50 base::Bind(&RejectCallbacksWithError<CallbacksType>, |
| 51 blink::WebUSBError(blink::WebUSBError::Error::Service, |
| 52 base::UTF8ToUTF16(kNoServiceError)))); |
| 53 } |
| 54 |
| 55 void OnGetDevicesComplete( |
| 56 ScopedWebCallbacks<blink::WebUSBClientGetDevicesCallbacks> scoped_callbacks, |
| 57 mojo::ServiceProvider* device_services, |
| 58 mojo::Array<device::usb::DeviceInfoPtr> results) { |
| 59 blink::WebVector<blink::WebUSBDevice*>* devices = |
| 60 new blink::WebVector<blink::WebUSBDevice*>(results.size()); |
| 61 for (size_t i = 0; i < results.size(); ++i) { |
| 62 device::usb::DeviceManagerPtr device_manager; |
| 63 mojo::ConnectToService(device_services, &device_manager); |
| 64 (*devices)[i] = new WebUSBDeviceImpl( |
| 65 device_manager.Pass(), |
| 66 mojo::ConvertTo<blink::WebUSBDeviceInfo>(results[i])); |
| 67 } |
| 68 scoped_callbacks.PassCallbacks()->onSuccess(blink::adoptWebPtr(devices)); |
| 69 } |
| 70 |
| 71 } // namespace |
| 72 |
| 73 WebUSBClientImpl::WebUSBClientImpl(mojo::ServiceProviderPtr device_services) |
| 74 : device_services_(device_services.Pass()) { |
| 75 mojo::ConnectToService(device_services_.get(), &device_manager_); |
| 76 } |
| 77 |
| 78 WebUSBClientImpl::~WebUSBClientImpl() {} |
| 79 |
| 80 void WebUSBClientImpl::getDevices( |
| 81 blink::WebUSBClientGetDevicesCallbacks* callbacks) { |
| 82 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); |
| 83 // TODO(rockot): Remove this once DeviceManager is updated. It should no |
| 84 // longer take enumeration options. |
| 85 device::usb::EnumerationOptionsPtr options = |
| 86 device::usb::EnumerationOptions::New(); |
| 87 options->filters = mojo::Array<device::usb::DeviceFilterPtr>::New(0); |
| 88 device_manager_->GetDevices( |
| 89 options.Pass(), |
| 90 base::Bind(&OnGetDevicesComplete, base::Passed(&scoped_callbacks), |
| 91 base::Unretained(device_services_.get()))); |
| 92 } |
| 93 |
| 94 void WebUSBClientImpl::requestDevice( |
| 95 const blink::WebUSBDeviceRequestOptions& options, |
| 96 blink::WebUSBClientRequestDeviceCallbacks* callbacks) { |
| 97 callbacks->onError(blink::WebUSBError(blink::WebUSBError::Error::Service, |
| 98 base::UTF8ToUTF16("Not implemented."))); |
| 99 delete callbacks; |
| 100 } |
| 101 |
| 102 } // namespace content |
OLD | NEW |