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