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/mojo/type_converters.h" |
12 #include "content/child/scoped_web_callbacks.h" | 13 #include "content/child/scoped_web_callbacks.h" |
13 #include "content/public/common/service_registry.h" | 14 #include "content/public/common/service_registry.h" |
14 #include "content/renderer/usb/type_converters.h" | 15 #include "content/renderer/usb/type_converters.h" |
15 #include "content/renderer/usb/web_usb_device_impl.h" | 16 #include "content/renderer/usb/web_usb_device_impl.h" |
16 #include "third_party/WebKit/public/platform/WebCallbacks.h" | 17 #include "third_party/WebKit/public/platform/WebCallbacks.h" |
17 #include "third_party/WebKit/public/platform/WebPassOwnPtr.h" | 18 #include "third_party/WebKit/public/platform/WebPassOwnPtr.h" |
18 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBDeviceFilter.h
" | 19 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBDeviceFilter.h
" |
19 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBDeviceInfo.h" | 20 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBDeviceInfo.h" |
20 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBDeviceRequestO
ptions.h" | 21 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBDeviceRequestO
ptions.h" |
21 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBError.h" | 22 #include "third_party/WebKit/public/platform/modules/webusb/WebUSBError.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 new blink::WebVector<blink::WebUSBDevice*>(results.size()); | 58 new blink::WebVector<blink::WebUSBDevice*>(results.size()); |
58 for (size_t i = 0; i < results.size(); ++i) { | 59 for (size_t i = 0; i < results.size(); ++i) { |
59 device::usb::DevicePtr device; | 60 device::usb::DevicePtr device; |
60 device_manager->GetDevice(results[i]->guid, mojo::GetProxy(&device)); | 61 device_manager->GetDevice(results[i]->guid, mojo::GetProxy(&device)); |
61 (*devices)[i] = new WebUSBDeviceImpl( | 62 (*devices)[i] = new WebUSBDeviceImpl( |
62 device.Pass(), mojo::ConvertTo<blink::WebUSBDeviceInfo>(results[i])); | 63 device.Pass(), mojo::ConvertTo<blink::WebUSBDeviceInfo>(results[i])); |
63 } | 64 } |
64 scoped_callbacks.PassCallbacks()->onSuccess(blink::adoptWebPtr(devices)); | 65 scoped_callbacks.PassCallbacks()->onSuccess(blink::adoptWebPtr(devices)); |
65 } | 66 } |
66 | 67 |
| 68 void OnRequestDevicesComplete( |
| 69 ScopedWebCallbacks<blink::WebUSBClientRequestDeviceCallbacks> callbacks, |
| 70 device::usb::DeviceManager* device_manager, |
| 71 device::usb::DeviceInfoPtr result) { |
| 72 auto scoped_callbacks = callbacks.PassCallbacks(); |
| 73 if (result) { |
| 74 device::usb::DevicePtr device; |
| 75 device_manager->GetDevice(result->guid, mojo::GetProxy(&device)); |
| 76 blink::WebUSBDevice* web_usb_device = new WebUSBDeviceImpl( |
| 77 device.Pass(), mojo::ConvertTo<blink::WebUSBDeviceInfo>(result)); |
| 78 |
| 79 scoped_callbacks->onSuccess(blink::adoptWebPtr(web_usb_device)); |
| 80 } else { |
| 81 scoped_callbacks->onSuccess( |
| 82 blink::adoptWebPtr<blink::WebUSBDevice>(nullptr)); |
| 83 } |
| 84 } |
| 85 |
67 } // namespace | 86 } // namespace |
68 | 87 |
69 WebUSBClientImpl::WebUSBClientImpl(content::ServiceRegistry* service_registry) | 88 WebUSBClientImpl::WebUSBClientImpl(content::ServiceRegistry* service_registry) |
70 : service_registry_(service_registry) {} | 89 : service_registry_(service_registry) {} |
71 | 90 |
72 WebUSBClientImpl::~WebUSBClientImpl() {} | 91 WebUSBClientImpl::~WebUSBClientImpl() {} |
73 | 92 |
74 void WebUSBClientImpl::getDevices( | 93 void WebUSBClientImpl::getDevices( |
75 blink::WebUSBClientGetDevicesCallbacks* callbacks) { | 94 blink::WebUSBClientGetDevicesCallbacks* callbacks) { |
76 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); | 95 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); |
77 GetDeviceManager()->GetDevices( | 96 GetDeviceManager()->GetDevices( |
78 nullptr, | 97 nullptr, |
79 base::Bind(&OnGetDevicesComplete, base::Passed(&scoped_callbacks), | 98 base::Bind(&OnGetDevicesComplete, base::Passed(&scoped_callbacks), |
80 base::Unretained(device_manager_.get()))); | 99 base::Unretained(device_manager_.get()))); |
81 } | 100 } |
82 | 101 |
83 void WebUSBClientImpl::requestDevice( | 102 void WebUSBClientImpl::requestDevice( |
84 const blink::WebUSBDeviceRequestOptions& options, | 103 const blink::WebUSBDeviceRequestOptions& options, |
85 blink::WebUSBClientRequestDeviceCallbacks* callbacks) { | 104 blink::WebUSBClientRequestDeviceCallbacks* callbacks) { |
86 callbacks->onError(blink::WebUSBError(blink::WebUSBError::Error::Service, | 105 if (!webusb_permission_bubble_) { |
87 base::UTF8ToUTF16("Not implemented."))); | 106 service_registry_->ConnectToRemoteService( |
88 delete callbacks; | 107 mojo::GetProxy(&webusb_permission_bubble_)); |
| 108 } |
| 109 |
| 110 auto scoped_callbacks = MakeScopedUSBCallbacks(callbacks); |
| 111 |
| 112 mojo::Array<device::usb::DeviceFilterPtr> device_filters = |
| 113 mojo::Array<device::usb::DeviceFilterPtr>::From(options.filters); |
| 114 |
| 115 webusb_permission_bubble_->GetPermission( |
| 116 device_filters.Pass(), |
| 117 base::Bind(&OnRequestDevicesComplete, base::Passed(&scoped_callbacks), |
| 118 base::Unretained(device_manager_.get()))); |
89 } | 119 } |
90 | 120 |
91 void WebUSBClientImpl::setObserver(Observer* observer) { | 121 void WebUSBClientImpl::setObserver(Observer* observer) { |
92 if (!observer_) { | 122 if (!observer_) { |
93 // Set up two sequential calls to GetDeviceChanges to avoid latency. | 123 // Set up two sequential calls to GetDeviceChanges to avoid latency. |
94 device::usb::DeviceManager* device_manager = GetDeviceManager(); | 124 device::usb::DeviceManager* device_manager = GetDeviceManager(); |
95 device_manager->GetDeviceChanges(base::Bind( | 125 device_manager->GetDeviceChanges(base::Bind( |
96 &WebUSBClientImpl::OnDeviceChangeNotification, base::Unretained(this))); | 126 &WebUSBClientImpl::OnDeviceChangeNotification, base::Unretained(this))); |
97 device_manager->GetDeviceChanges(base::Bind( | 127 device_manager->GetDeviceChanges(base::Bind( |
98 &WebUSBClientImpl::OnDeviceChangeNotification, base::Unretained(this))); | 128 &WebUSBClientImpl::OnDeviceChangeNotification, base::Unretained(this))); |
(...skipping 27 matching lines...) Expand all Loading... |
126 const device::usb::DeviceInfoPtr& device_info = | 156 const device::usb::DeviceInfoPtr& device_info = |
127 notification->devices_removed[i]; | 157 notification->devices_removed[i]; |
128 device::usb::DevicePtr device; | 158 device::usb::DevicePtr device; |
129 device_manager_->GetDevice(device_info->guid, mojo::GetProxy(&device)); | 159 device_manager_->GetDevice(device_info->guid, mojo::GetProxy(&device)); |
130 observer_->onDeviceDisconnected(blink::adoptWebPtr(new WebUSBDeviceImpl( | 160 observer_->onDeviceDisconnected(blink::adoptWebPtr(new WebUSBDeviceImpl( |
131 device.Pass(), mojo::ConvertTo<blink::WebUSBDeviceInfo>(device_info)))); | 161 device.Pass(), mojo::ConvertTo<blink::WebUSBDeviceInfo>(device_info)))); |
132 } | 162 } |
133 } | 163 } |
134 | 164 |
135 } // namespace content | 165 } // namespace content |
OLD | NEW |