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

Unified Diff: third_party/WebKit/Source/modules/webusb/USB.cpp

Issue 1912223002: Make new WebUSB implementation more resilient to garbage collection. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/modules/webusb/USB.cpp
diff --git a/third_party/WebKit/Source/modules/webusb/USB.cpp b/third_party/WebKit/Source/modules/webusb/USB.cpp
index 4811ea22af4f0c53fa6bc874ea4271c4ea178d6c..6a0792015ec253f9f2db361eadf534f0923abbfc 100644
--- a/third_party/WebKit/Source/modules/webusb/USB.cpp
+++ b/third_party/WebKit/Source/modules/webusb/USB.cpp
@@ -60,24 +60,18 @@ USB::USB(LocalFrame& frame)
: ContextLifecycleObserver(frame.document())
{
frame.serviceRegistry()->connectToRemoteService(mojo::GetProxy(&m_deviceManager));
- m_deviceManager.set_connection_error_handler([this]() {
- m_deviceManager.reset();
- for (ScriptPromiseResolver* resolver : m_deviceManagerRequests) {
- if (isActive(resolver))
- resolver->reject(DOMException::create(NotFoundError, kNoServiceError));
- }
- m_deviceManagerRequests.clear();
- });
+ m_deviceManager.set_connection_error_handler(createBaseCallback(bind(&USB::onDeviceManagerConnectionError, WeakPersistentThisPointer<USB>(this))));
// Set up two sequential calls to GetDeviceChanges to avoid latency.
- m_deviceManager->GetDeviceChanges(createBaseCallback(bind<usb::DeviceChangeNotificationPtr>(&USB::onDeviceChanges, this)));
- m_deviceManager->GetDeviceChanges(createBaseCallback(bind<usb::DeviceChangeNotificationPtr>(&USB::onDeviceChanges, this)));
+ m_deviceManager->GetDeviceChanges(createBaseCallback(bind<usb::DeviceChangeNotificationPtr>(&USB::onDeviceChanges, WeakPersistentThisPointer<USB>(this))));
+ m_deviceManager->GetDeviceChanges(createBaseCallback(bind<usb::DeviceChangeNotificationPtr>(&USB::onDeviceChanges, WeakPersistentThisPointer<USB>(this))));
}
USB::~USB()
{
- DCHECK(!m_deviceManager);
+ // |m_deviceManager| and |m_chooserService| may still be valid but there
+ // should be no more outstanding requests to them because each holds a
+ // persistent handle to this object.
DCHECK(m_deviceManagerRequests.isEmpty());
- DCHECK(!m_chooserService);
DCHECK(m_chooserServiceRequests.isEmpty());
}
@@ -111,14 +105,7 @@ ScriptPromise USB::requestDevice(ScriptState* scriptState, const USBDeviceReques
return promise;
}
frame->serviceRegistry()->connectToRemoteService(mojo::GetProxy(&m_chooserService));
- m_chooserService.set_connection_error_handler([this]() {
- m_chooserService.reset();
- for (ScriptPromiseResolver* resolver : m_chooserServiceRequests) {
- if (isActive(resolver))
- resolver->reject(DOMException::create(NotFoundError, kNoServiceError));
- }
- m_chooserServiceRequests.clear();
- });
+ m_chooserService.set_connection_error_handler(createBaseCallback(bind(&USB::onChooserServiceConnectionError, WeakPersistentThisPointer<USB>(this))));
}
String errorMessage;
@@ -188,7 +175,7 @@ void USB::onGetPermission(ScriptPromiseResolver* resolver, usb::DeviceInfoPtr de
void USB::onDeviceChanges(usb::DeviceChangeNotificationPtr notification)
{
- m_deviceManager->GetDeviceChanges(createBaseCallback(bind<usb::DeviceChangeNotificationPtr>(&USB::onDeviceChanges, this)));
+ m_deviceManager->GetDeviceChanges(createBaseCallback(bind<usb::DeviceChangeNotificationPtr>(&USB::onDeviceChanges, WeakPersistentThisPointer<USB>(this))));
for (auto& deviceInfo : notification->devices_added.PassStorage()) {
usb::DevicePtr device;
m_deviceManager->GetDevice(deviceInfo->guid, mojo::GetProxy(&device));
@@ -198,6 +185,26 @@ void USB::onDeviceChanges(usb::DeviceChangeNotificationPtr notification)
dispatchEvent(USBConnectionEvent::create(EventTypeNames::disconnect, USBDevice::create(std::move(deviceInfo), nullptr, getExecutionContext())));
}
+void USB::onDeviceManagerConnectionError()
+{
+ m_deviceManager.reset();
+ for (ScriptPromiseResolver* resolver : m_deviceManagerRequests) {
esprehn 2016/04/23 00:54:02 seems like this needs to be more centralized, doin
+ if (isActive(resolver))
+ resolver->reject(DOMException::create(NotFoundError, kNoServiceError));
+ }
+ m_deviceManagerRequests.clear();
+}
+
+void USB::onChooserServiceConnectionError()
+{
+ m_chooserService.reset();
+ for (ScriptPromiseResolver* resolver : m_chooserServiceRequests) {
+ if (isActive(resolver))
+ resolver->reject(DOMException::create(NotFoundError, kNoServiceError));
+ }
+ m_chooserServiceRequests.clear();
+}
+
DEFINE_TRACE(USB)
{
EventTargetWithInlineData::trace(visitor);

Powered by Google App Engine
This is Rietveld 408576698