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 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 } | 91 } |
92 | 92 |
93 void WebUSBClientImpl::requestDevice( | 93 void WebUSBClientImpl::requestDevice( |
94 const blink::WebUSBDeviceRequestOptions& options, | 94 const blink::WebUSBDeviceRequestOptions& options, |
95 blink::WebUSBClientRequestDeviceCallbacks* callbacks) { | 95 blink::WebUSBClientRequestDeviceCallbacks* callbacks) { |
96 callbacks->onError(blink::WebUSBError(blink::WebUSBError::Error::Service, | 96 callbacks->onError(blink::WebUSBError(blink::WebUSBError::Error::Service, |
97 base::UTF8ToUTF16("Not implemented."))); | 97 base::UTF8ToUTF16("Not implemented."))); |
98 delete callbacks; | 98 delete callbacks; |
99 } | 99 } |
100 | 100 |
| 101 void WebUSBClientImpl::setObserver(Observer* observer) { |
| 102 if (!observer_) { |
| 103 // Set up two sequential calls to GetDeviceChanges to avoid latency. |
| 104 device_manager_->GetDeviceChanges(base::Bind( |
| 105 &WebUSBClientImpl::OnDeviceChangeNotification, base::Unretained(this))); |
| 106 device_manager_->GetDeviceChanges(base::Bind( |
| 107 &WebUSBClientImpl::OnDeviceChangeNotification, base::Unretained(this))); |
| 108 } |
| 109 |
| 110 observer_ = observer; |
| 111 } |
| 112 |
| 113 void WebUSBClientImpl::OnDeviceChangeNotification( |
| 114 device::usb::DeviceChangeNotificationPtr notification) { |
| 115 if (!observer_) |
| 116 return; |
| 117 |
| 118 device_manager_->GetDeviceChanges(base::Bind( |
| 119 &WebUSBClientImpl::OnDeviceChangeNotification, base::Unretained(this))); |
| 120 for (size_t i = 0; i < notification->devices_added.size(); ++i) { |
| 121 device::usb::DeviceManagerPtr device_manager; |
| 122 mojo::ConnectToService(device_services_.get(), &device_manager); |
| 123 observer_->onDeviceConnected(blink::adoptWebPtr(new WebUSBDeviceImpl( |
| 124 device_manager.Pass(), mojo::ConvertTo<blink::WebUSBDeviceInfo>( |
| 125 notification->devices_added[i])))); |
| 126 } |
| 127 for (size_t i = 0; i < notification->devices_removed.size(); ++i) { |
| 128 device::usb::DeviceManagerPtr device_manager; |
| 129 mojo::ConnectToService(device_services_.get(), &device_manager); |
| 130 observer_->onDeviceDisconnected(blink::adoptWebPtr(new WebUSBDeviceImpl( |
| 131 device_manager.Pass(), mojo::ConvertTo<blink::WebUSBDeviceInfo>( |
| 132 notification->devices_removed[i])))); |
| 133 } |
| 134 } |
| 135 |
101 } // namespace content | 136 } // namespace content |
OLD | NEW |