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

Side by Side Diff: third_party/WebKit/Source/modules/webusb/USB.cpp

Issue 1946063002: Replace DeviceManager::GetDeviceChanges with a client interface. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@proper_stubs
Patch Set: Rebase.d Created 4 years, 7 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
« no previous file with comments | « third_party/WebKit/Source/modules/webusb/USB.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "modules/webusb/USB.h" 5 #include "modules/webusb/USB.h"
6 6
7 #include "bindings/core/v8/ScriptPromise.h" 7 #include "bindings/core/v8/ScriptPromise.h"
8 #include "bindings/core/v8/ScriptPromiseResolver.h" 8 #include "bindings/core/v8/ScriptPromiseResolver.h"
9 #include "core/dom/DOMException.h" 9 #include "core/dom/DOMException.h"
10 #include "core/dom/Document.h" 10 #include "core/dom/Document.h"
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 mojoFilter->has_protocol_code = filter.hasProtocolCode(); 45 mojoFilter->has_protocol_code = filter.hasProtocolCode();
46 if (mojoFilter->has_protocol_code) 46 if (mojoFilter->has_protocol_code)
47 mojoFilter->protocol_code = filter.protocolCode(); 47 mojoFilter->protocol_code = filter.protocolCode();
48 return mojoFilter; 48 return mojoFilter;
49 } 49 }
50 50
51 } // namespace 51 } // namespace
52 52
53 USB::USB(LocalFrame& frame) 53 USB::USB(LocalFrame& frame)
54 : ContextLifecycleObserver(frame.document()) 54 : ContextLifecycleObserver(frame.document())
55 , m_clientBinding(this)
55 { 56 {
57 ThreadState::current()->registerPreFinalizer(this);
56 frame.serviceRegistry()->connectToRemoteService(mojo::GetProxy(&m_deviceMana ger)); 58 frame.serviceRegistry()->connectToRemoteService(mojo::GetProxy(&m_deviceMana ger));
57 m_deviceManager.set_connection_error_handler(createBaseCallback(bind(&USB::o nDeviceManagerConnectionError, WeakPersistentThisPointer<USB>(this)))); 59 m_deviceManager.set_connection_error_handler(createBaseCallback(bind(&USB::o nDeviceManagerConnectionError, WeakPersistentThisPointer<USB>(this))));
58 // Set up two sequential calls to GetDeviceChanges to avoid latency. 60 m_deviceManager->SetClient(m_clientBinding.CreateInterfacePtrAndBind());
59 m_deviceManager->GetDeviceChanges(createBaseCallback(bind<usb::DeviceChangeN otificationPtr>(&USB::onDeviceChanges, WeakPersistentThisPointer<USB>(this))));
60 m_deviceManager->GetDeviceChanges(createBaseCallback(bind<usb::DeviceChangeN otificationPtr>(&USB::onDeviceChanges, WeakPersistentThisPointer<USB>(this))));
61 } 61 }
62 62
63 USB::~USB() 63 USB::~USB()
64 { 64 {
65 // |m_deviceManager| and |m_chooserService| may still be valid but there 65 // |m_deviceManager| and |m_chooserService| may still be valid but there
66 // should be no more outstanding requests to them because each holds a 66 // should be no more outstanding requests to them because each holds a
67 // persistent handle to this object. 67 // persistent handle to this object.
68 DCHECK(m_deviceManagerRequests.isEmpty()); 68 DCHECK(m_deviceManagerRequests.isEmpty());
69 DCHECK(m_chooserServiceRequests.isEmpty()); 69 DCHECK(m_chooserServiceRequests.isEmpty());
70 } 70 }
71 71
72 void USB::dispose()
73 {
74 // The pipe to this object must be closed when is marked unreachable to
75 // prevent messages from being dispatched before lazy sweeping.
76 m_clientBinding.Close();
77 }
78
72 ScriptPromise USB::getDevices(ScriptState* scriptState) 79 ScriptPromise USB::getDevices(ScriptState* scriptState)
73 { 80 {
74 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ; 81 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState) ;
75 ScriptPromise promise = resolver->promise(); 82 ScriptPromise promise = resolver->promise();
76 if (!m_deviceManager) { 83 if (!m_deviceManager) {
77 resolver->reject(DOMException::create(NotSupportedError)); 84 resolver->reject(DOMException::create(NotSupportedError));
78 } else { 85 } else {
79 String errorMessage; 86 String errorMessage;
80 if (!scriptState->getExecutionContext()->isSecureContext(errorMessage)) { 87 if (!scriptState->getExecutionContext()->isSecureContext(errorMessage)) {
81 resolver->reject(DOMException::create(SecurityError, errorMessage)); 88 resolver->reject(DOMException::create(SecurityError, errorMessage));
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 176
170 if (deviceInfo) { 177 if (deviceInfo) {
171 usb::DevicePtr device; 178 usb::DevicePtr device;
172 m_deviceManager->GetDevice(deviceInfo->guid, mojo::GetProxy(&device)); 179 m_deviceManager->GetDevice(deviceInfo->guid, mojo::GetProxy(&device));
173 resolver->resolve(USBDevice::create(std::move(deviceInfo), std::move(dev ice), resolver->getExecutionContext())); 180 resolver->resolve(USBDevice::create(std::move(deviceInfo), std::move(dev ice), resolver->getExecutionContext()));
174 } else { 181 } else {
175 resolver->reject(DOMException::create(NotFoundError, "No device selected .")); 182 resolver->reject(DOMException::create(NotFoundError, "No device selected ."));
176 } 183 }
177 } 184 }
178 185
179 void USB::onDeviceChanges(usb::DeviceChangeNotificationPtr notification) 186 void USB::OnDeviceAdded(usb::DeviceInfoPtr deviceInfo)
180 { 187 {
181 m_deviceManager->GetDeviceChanges(createBaseCallback(bind<usb::DeviceChangeN otificationPtr>(&USB::onDeviceChanges, WeakPersistentThisPointer<USB>(this)))); 188 if (!m_deviceManager)
182 for (auto& deviceInfo : notification->devices_added.PassStorage()) { 189 return;
183 usb::DevicePtr device; 190
184 m_deviceManager->GetDevice(deviceInfo->guid, mojo::GetProxy(&device)); 191 usb::DevicePtr device;
185 dispatchEvent(USBConnectionEvent::create(EventTypeNames::connect, USBDev ice::create(std::move(deviceInfo), std::move(device), getExecutionContext()))); 192 m_deviceManager->GetDevice(deviceInfo->guid, mojo::GetProxy(&device));
186 } 193 dispatchEvent(USBConnectionEvent::create(EventTypeNames::connect, USBDevice: :create(std::move(deviceInfo), std::move(device), getExecutionContext())));
187 for (auto& deviceInfo : notification->devices_removed.PassStorage()) 194 }
188 dispatchEvent(USBConnectionEvent::create(EventTypeNames::disconnect, USB Device::create(std::move(deviceInfo), nullptr, getExecutionContext()))); 195
196 void USB::OnDeviceRemoved(usb::DeviceInfoPtr deviceInfo)
197 {
198 dispatchEvent(USBConnectionEvent::create(EventTypeNames::disconnect, USBDevi ce::create(std::move(deviceInfo), nullptr, getExecutionContext())));
189 } 199 }
190 200
191 void USB::onDeviceManagerConnectionError() 201 void USB::onDeviceManagerConnectionError()
192 { 202 {
193 m_deviceManager.reset(); 203 m_deviceManager.reset();
194 for (ScriptPromiseResolver* resolver : m_deviceManagerRequests) 204 for (ScriptPromiseResolver* resolver : m_deviceManagerRequests)
195 resolver->reject(DOMException::create(NotFoundError, kNoServiceError)); 205 resolver->reject(DOMException::create(NotFoundError, kNoServiceError));
196 m_deviceManagerRequests.clear(); 206 m_deviceManagerRequests.clear();
197 } 207 }
198 208
199 void USB::onChooserServiceConnectionError() 209 void USB::onChooserServiceConnectionError()
200 { 210 {
201 m_chooserService.reset(); 211 m_chooserService.reset();
202 for (ScriptPromiseResolver* resolver : m_chooserServiceRequests) 212 for (ScriptPromiseResolver* resolver : m_chooserServiceRequests)
203 resolver->reject(DOMException::create(NotFoundError, kNoServiceError)); 213 resolver->reject(DOMException::create(NotFoundError, kNoServiceError));
204 m_chooserServiceRequests.clear(); 214 m_chooserServiceRequests.clear();
205 } 215 }
206 216
207 DEFINE_TRACE(USB) 217 DEFINE_TRACE(USB)
208 { 218 {
209 EventTargetWithInlineData::trace(visitor); 219 EventTargetWithInlineData::trace(visitor);
210 ContextLifecycleObserver::trace(visitor); 220 ContextLifecycleObserver::trace(visitor);
211 visitor->trace(m_deviceManagerRequests); 221 visitor->trace(m_deviceManagerRequests);
212 visitor->trace(m_chooserServiceRequests); 222 visitor->trace(m_chooserServiceRequests);
213 } 223 }
214 224
215 } // namespace blink 225 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/modules/webusb/USB.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698