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