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