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