Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(409)

Side by Side Diff: content/renderer/usb/web_usb_client_impl.cc

Issue 1848603002: Remove single observer assumption in WebUSBClient. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use EAGERLY_FINALIZE() instead. Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <stddef.h> 7 #include <stddef.h>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 116
117 mojo::Array<device::usb::DeviceFilterPtr> device_filters = 117 mojo::Array<device::usb::DeviceFilterPtr> device_filters =
118 mojo::Array<device::usb::DeviceFilterPtr>::From(options.filters); 118 mojo::Array<device::usb::DeviceFilterPtr>::From(options.filters);
119 119
120 chooser_service_->GetPermission( 120 chooser_service_->GetPermission(
121 std::move(device_filters), 121 std::move(device_filters),
122 base::Bind(&OnRequestDevicesComplete, base::Passed(&scoped_callbacks), 122 base::Bind(&OnRequestDevicesComplete, base::Passed(&scoped_callbacks),
123 base::Unretained(device_manager_.get()))); 123 base::Unretained(device_manager_.get())));
124 } 124 }
125 125
126 void WebUSBClientImpl::setObserver(Observer* observer) { 126 void WebUSBClientImpl::addObserver(Observer* observer) {
127 if (!observer_) { 127 if (observers_.empty()) {
128 // Set up two sequential calls to GetDeviceChanges to avoid latency. 128 // Set up two sequential calls to GetDeviceChanges to avoid latency.
129 device::usb::DeviceManager* device_manager = GetDeviceManager(); 129 device::usb::DeviceManager* device_manager = GetDeviceManager();
130 device_manager->GetDeviceChanges(base::Bind( 130 device_manager->GetDeviceChanges(base::Bind(
131 &WebUSBClientImpl::OnDeviceChangeNotification, base::Unretained(this))); 131 &WebUSBClientImpl::OnDeviceChangeNotification, base::Unretained(this)));
132 device_manager->GetDeviceChanges(base::Bind( 132 device_manager->GetDeviceChanges(base::Bind(
133 &WebUSBClientImpl::OnDeviceChangeNotification, base::Unretained(this))); 133 &WebUSBClientImpl::OnDeviceChangeNotification, base::Unretained(this)));
134 } 134 }
135 135
136 observer_ = observer; 136 observers_.insert(observer);
sof 2016/04/02 07:02:17 It sounds as if this code won't stay around for lo
137 }
138
139 void WebUSBClientImpl::removeObserver(Observer* observer) {
140 DCHECK(ContainsKey(observers_, observer));
141 observers_.erase(observer);
137 } 142 }
138 143
139 device::usb::DeviceManager* WebUSBClientImpl::GetDeviceManager() { 144 device::usb::DeviceManager* WebUSBClientImpl::GetDeviceManager() {
140 if (!device_manager_) 145 if (!device_manager_)
141 service_registry_->ConnectToRemoteService(mojo::GetProxy(&device_manager_)); 146 service_registry_->ConnectToRemoteService(mojo::GetProxy(&device_manager_));
142 return device_manager_.get(); 147 return device_manager_.get();
143 } 148 }
144 149
145 void WebUSBClientImpl::OnDeviceChangeNotification( 150 void WebUSBClientImpl::OnDeviceChangeNotification(
146 device::usb::DeviceChangeNotificationPtr notification) { 151 device::usb::DeviceChangeNotificationPtr notification) {
147 if (!observer_) 152 if (observers_.empty())
148 return; 153 return;
149 154
150 device_manager_->GetDeviceChanges(base::Bind( 155 device_manager_->GetDeviceChanges(base::Bind(
151 &WebUSBClientImpl::OnDeviceChangeNotification, base::Unretained(this))); 156 &WebUSBClientImpl::OnDeviceChangeNotification, base::Unretained(this)));
152 for (size_t i = 0; i < notification->devices_added.size(); ++i) { 157 for (size_t i = 0; i < notification->devices_added.size(); ++i) {
153 const device::usb::DeviceInfoPtr& device_info = 158 const device::usb::DeviceInfoPtr& device_info =
154 notification->devices_added[i]; 159 notification->devices_added[i];
155 device::usb::DevicePtr device; 160 for (auto observer : observers_) {
156 device_manager_->GetDevice(device_info->guid, mojo::GetProxy(&device)); 161 device::usb::DevicePtr device;
157 observer_->onDeviceConnected(blink::adoptWebPtr(new WebUSBDeviceImpl( 162 device_manager_->GetDevice(device_info->guid, mojo::GetProxy(&device));
158 std::move(device), 163 observer->onDeviceConnected(blink::adoptWebPtr(new WebUSBDeviceImpl(
159 mojo::ConvertTo<blink::WebUSBDeviceInfo>(device_info)))); 164 std::move(device),
165 mojo::ConvertTo<blink::WebUSBDeviceInfo>(device_info))));
166 }
160 } 167 }
161 for (size_t i = 0; i < notification->devices_removed.size(); ++i) { 168 for (size_t i = 0; i < notification->devices_removed.size(); ++i) {
162 const device::usb::DeviceInfoPtr& device_info = 169 const device::usb::DeviceInfoPtr& device_info =
163 notification->devices_removed[i]; 170 notification->devices_removed[i];
164 observer_->onDeviceDisconnected(blink::adoptWebPtr(new WebUSBDeviceImpl( 171 for (auto observer : observers_)
165 nullptr, mojo::ConvertTo<blink::WebUSBDeviceInfo>(device_info)))); 172 observer->onDeviceDisconnected(blink::adoptWebPtr(new WebUSBDeviceImpl(
173 nullptr, mojo::ConvertTo<blink::WebUSBDeviceInfo>(device_info))));
166 } 174 }
167 } 175 }
168 176
169 } // namespace content 177 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/usb/web_usb_client_impl.h ('k') | third_party/WebKit/Source/modules/webusb/USB.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698