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 "content/renderer/usb/web_usb_client_impl.h" | 5 #include "content/renderer/usb/web_usb_client_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/move.h" | 10 #include "base/move.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "content/child/scoped_web_callbacks.h" | 12 #include "content/child/scoped_web_callbacks.h" |
13 #include "content/public/common/service_registry.h" | |
14 #include "content/renderer/usb/type_converters.h" | 13 #include "content/renderer/usb/type_converters.h" |
15 #include "content/renderer/usb/web_usb_device_impl.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" |
16 #include "third_party/WebKit/public/platform/WebCallbacks.h" | 18 #include "third_party/WebKit/public/platform/WebCallbacks.h" |
17 #include "third_party/WebKit/public/platform/WebPassOwnPtr.h" | 19 #include "third_party/WebKit/public/platform/WebPassOwnPtr.h" |
18 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBDeviceFilter.h
" | 20 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBDeviceFilter.h
" |
19 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBDeviceInfo.h" | 21 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBDeviceInfo.h" |
20 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBDeviceRequestO
ptions.h" | 22 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBDeviceRequestO
ptions.h" |
21 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBError.h" | 23 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBError.h" |
22 #include "third_party/mojo/src/mojo/public/cpp/bindings/array.h" | 24 #include "third_party/mojo/src/mojo/public/cpp/bindings/array.h" |
23 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h" | 25 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h" |
24 | 26 |
25 namespace content { | 27 namespace content { |
(...skipping 18 matching lines...) Expand all Loading... |
44 CallbacksType* callbacks) { | 46 CallbacksType* callbacks) { |
45 return make_scoped_web_callbacks( | 47 return make_scoped_web_callbacks( |
46 callbacks, | 48 callbacks, |
47 base::Bind(&RejectCallbacksWithError<CallbacksType>, | 49 base::Bind(&RejectCallbacksWithError<CallbacksType>, |
48 blink::WebUSBError(blink::WebUSBError::Error::Service, | 50 blink::WebUSBError(blink::WebUSBError::Error::Service, |
49 base::UTF8ToUTF16(kNoServiceError)))); | 51 base::UTF8ToUTF16(kNoServiceError)))); |
50 } | 52 } |
51 | 53 |
52 void OnGetDevicesComplete( | 54 void OnGetDevicesComplete( |
53 ScopedWebCallbacks<blink::WebUSBClientGetDevicesCallbacks> scoped_callbacks, | 55 ScopedWebCallbacks<blink::WebUSBClientGetDevicesCallbacks> scoped_callbacks, |
54 device::usb::DeviceManager* device_manager, | 56 mojo::ServiceProvider* device_services, |
55 mojo::Array<device::usb::DeviceInfoPtr> results) { | 57 mojo::Array<device::usb::DeviceInfoPtr> results) { |
| 58 device::usb::DeviceManagerPtr device_manager; |
| 59 mojo::ConnectToService(device_services, &device_manager); |
56 blink::WebVector<blink::WebUSBDevice*>* devices = | 60 blink::WebVector<blink::WebUSBDevice*>* devices = |
57 new blink::WebVector<blink::WebUSBDevice*>(results.size()); | 61 new blink::WebVector<blink::WebUSBDevice*>(results.size()); |
58 for (size_t i = 0; i < results.size(); ++i) { | 62 for (size_t i = 0; i < results.size(); ++i) { |
59 device::usb::DevicePtr device; | 63 device::usb::DevicePtr device; |
60 device_manager->GetDevice(results[i]->guid, mojo::GetProxy(&device)); | 64 device_manager->GetDevice(results[i]->guid, mojo::GetProxy(&device)); |
61 (*devices)[i] = new WebUSBDeviceImpl( | 65 (*devices)[i] = new WebUSBDeviceImpl( |
62 device.Pass(), mojo::ConvertTo<blink::WebUSBDeviceInfo>(results[i])); | 66 device.Pass(), mojo::ConvertTo<blink::WebUSBDeviceInfo>(results[i])); |
63 } | 67 } |
64 scoped_callbacks.PassCallbacks()->onSuccess(blink::adoptWebPtr(devices)); | 68 scoped_callbacks.PassCallbacks()->onSuccess(blink::adoptWebPtr(devices)); |
65 } | 69 } |
66 | 70 |
67 } // namespace | 71 } // namespace |
68 | 72 |
69 WebUSBClientImpl::WebUSBClientImpl(content::ServiceRegistry* service_registry) { | 73 WebUSBClientImpl::WebUSBClientImpl(mojo::ServiceProviderPtr device_services) |
70 service_registry->ConnectToRemoteService(mojo::GetProxy(&device_manager_)); | 74 : device_services_(device_services.Pass()) { |
71 device_manager_.set_connection_error_handler( | 75 mojo::ConnectToService(device_services_.get(), &device_manager_); |
72 [this]() { LOG(ERROR) << "Device manager connection failed."; }); | |
73 } | 76 } |
74 | 77 |
75 WebUSBClientImpl::~WebUSBClientImpl() {} | 78 WebUSBClientImpl::~WebUSBClientImpl() {} |
76 | 79 |
77 void WebUSBClientImpl::getDevices( | 80 void WebUSBClientImpl::getDevices( |
78 blink::WebUSBClientGetDevicesCallbacks* callbacks) { | 81 blink::WebUSBClientGetDevicesCallbacks* callbacks) { |
79 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); | 82 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); |
80 // TODO(rockot): Remove this once DeviceManager is updated. It should no | 83 // TODO(rockot): Remove this once DeviceManager is updated. It should no |
81 // longer take enumeration options. | 84 // longer take enumeration options. |
82 device::usb::EnumerationOptionsPtr options = | 85 device::usb::EnumerationOptionsPtr options = |
83 device::usb::EnumerationOptions::New(); | 86 device::usb::EnumerationOptions::New(); |
84 options->filters = mojo::Array<device::usb::DeviceFilterPtr>::New(0); | 87 options->filters = mojo::Array<device::usb::DeviceFilterPtr>::New(0); |
85 device_manager_->GetDevices( | 88 device_manager_->GetDevices( |
86 options.Pass(), | 89 options.Pass(), |
87 base::Bind(&OnGetDevicesComplete, base::Passed(&scoped_callbacks), | 90 base::Bind(&OnGetDevicesComplete, base::Passed(&scoped_callbacks), |
88 base::Unretained(device_manager_.get()))); | 91 base::Unretained(device_services_.get()))); |
89 } | 92 } |
90 | 93 |
91 void WebUSBClientImpl::requestDevice( | 94 void WebUSBClientImpl::requestDevice( |
92 const blink::WebUSBDeviceRequestOptions& options, | 95 const blink::WebUSBDeviceRequestOptions& options, |
93 blink::WebUSBClientRequestDeviceCallbacks* callbacks) { | 96 blink::WebUSBClientRequestDeviceCallbacks* callbacks) { |
94 callbacks->onError(blink::WebUSBError(blink::WebUSBError::Error::Service, | 97 callbacks->onError(blink::WebUSBError(blink::WebUSBError::Error::Service, |
95 base::UTF8ToUTF16("Not implemented."))); | 98 base::UTF8ToUTF16("Not implemented."))); |
96 delete callbacks; | 99 delete callbacks; |
97 } | 100 } |
98 | 101 |
(...skipping 28 matching lines...) Expand all Loading... |
127 const device::usb::DeviceInfoPtr& device_info = | 130 const device::usb::DeviceInfoPtr& device_info = |
128 notification->devices_removed[i]; | 131 notification->devices_removed[i]; |
129 device::usb::DevicePtr device; | 132 device::usb::DevicePtr device; |
130 device_manager_->GetDevice(device_info->guid, mojo::GetProxy(&device)); | 133 device_manager_->GetDevice(device_info->guid, mojo::GetProxy(&device)); |
131 observer_->onDeviceDisconnected(blink::adoptWebPtr(new WebUSBDeviceImpl( | 134 observer_->onDeviceDisconnected(blink::adoptWebPtr(new WebUSBDeviceImpl( |
132 device.Pass(), mojo::ConvertTo<blink::WebUSBDeviceInfo>(device_info)))); | 135 device.Pass(), mojo::ConvertTo<blink::WebUSBDeviceInfo>(device_info)))); |
133 } | 136 } |
134 } | 137 } |
135 | 138 |
136 } // namespace content | 139 } // namespace content |
OLD | NEW |