-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
usb: Introduce ServiceWorkerUsbDelegateObserver for WebUsbService
This change introduces ServiceWorkerUsbDelegateObserver as a broker between UsbDelegate and WebUsbService. It is not covered in this CL yet, but this class is set to be responsible to keep track what ServiceWorkerRegistration should respond to USB events and wake up the service worker if it is not in running state. The full functionality will be completed by the following CL. Bug: 1446487 Change-Id: I1ab8809ec816ed3ad5c4cf55d0bd3521452a41c9 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4763985 Reviewed-by: Yoshisato Yanagisawa <yyanagisawa@chromium.org> Reviewed-by: Jack Hsieh <chengweih@chromium.org> Commit-Queue: Juan Garza Sanchez <juangarza@chromium.org> Auto-Submit: Juan Garza Sanchez <juangarza@chromium.org> Cr-Commit-Position: refs/heads/main@{#1187996}
- Loading branch information
Juan Garza Sanchez
authored and
Chromium LUCI CQ
committed
Aug 24, 2023
1 parent
3d35ece
commit 62cce61
Showing
17 changed files
with
836 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
content/browser/service_worker/service_worker_usb_delegate_observer.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "content/browser/service_worker/service_worker_usb_delegate_observer.h" | ||
|
||
#include <cstdint> | ||
|
||
#include "base/functional/bind.h" | ||
#include "base/memory/weak_ptr.h" | ||
#include "content/browser/service_worker/embedded_worker_status.h" | ||
#include "content/browser/service_worker/service_worker_context_core.h" | ||
#include "content/browser/service_worker/service_worker_context_wrapper.h" | ||
#include "content/browser/service_worker/service_worker_registration.h" | ||
#include "content/browser/service_worker/service_worker_version.h" | ||
#include "content/browser/usb/web_usb_service_impl.h" | ||
#include "content/public/common/content_client.h" | ||
|
||
namespace content { | ||
|
||
ServiceWorkerUsbDelegateObserver::ServiceWorkerUsbDelegateObserver( | ||
ServiceWorkerContextCore* context) | ||
: ServiceWorkerDeviceDelegateObserver(context) {} | ||
|
||
ServiceWorkerUsbDelegateObserver::~ServiceWorkerUsbDelegateObserver() = default; | ||
|
||
void ServiceWorkerUsbDelegateObserver::OnDeviceAdded( | ||
const device::mojom::UsbDeviceInfo& device_info) { | ||
DispatchUsbDeviceEventToWorkers( | ||
device_info, | ||
base::BindRepeating([](const device::mojom::UsbDeviceInfo& device_info, | ||
WebUsbServiceImpl* service) { | ||
service->OnDeviceAdded(device_info); | ||
})); | ||
} | ||
|
||
void ServiceWorkerUsbDelegateObserver::OnDeviceRemoved( | ||
const device::mojom::UsbDeviceInfo& device_info) { | ||
DispatchUsbDeviceEventToWorkers( | ||
device_info, | ||
base::BindRepeating([](const device::mojom::UsbDeviceInfo& device_info, | ||
WebUsbServiceImpl* service) { | ||
service->OnDeviceRemoved(device_info); | ||
})); | ||
} | ||
|
||
void ServiceWorkerUsbDelegateObserver::OnDeviceManagerConnectionError() { | ||
for (auto const& [id, info] : registration_id_map()) { | ||
auto* usb_service = GetUsbService(id); | ||
if (usb_service) { | ||
usb_service->OnDeviceManagerConnectionError(); | ||
} | ||
} | ||
} | ||
|
||
void ServiceWorkerUsbDelegateObserver::OnPermissionRevoked( | ||
const url::Origin& origin) { | ||
for (auto const& [id, info] : registration_id_map()) { | ||
auto* usb_service = GetUsbService(id); | ||
if (usb_service) { | ||
usb_service->OnPermissionRevoked(origin); | ||
} | ||
} | ||
} | ||
|
||
void ServiceWorkerUsbDelegateObserver::RegisterUsbService( | ||
int64_t registration_id, | ||
base::WeakPtr<WebUsbServiceImpl> usb_service) { | ||
Register(registration_id); | ||
// `usb_services_` may already have an entry for `registration_id` in a case | ||
// where the service worker went to sleep and now is worken up. In that | ||
// case, the WebUsbServiceImpl from `usb_services_[registration_id]` is the | ||
// weak ptr of previous WebUsbServiceImpl before the service worker went to | ||
// sleep. We don't care about the previous WebUsbServiceImpl, so here just | ||
// overwrite it with `usb_service`, which is the latest one. | ||
usb_services_[registration_id] = usb_service; | ||
} | ||
|
||
void ServiceWorkerUsbDelegateObserver::RegistrationAdded( | ||
int64_t registration_id) { | ||
if (registration_id_map().size() == 1) { | ||
UsbDelegate* delegate = GetContentClient()->browser()->GetUsbDelegate(); | ||
if (delegate) { | ||
usb_delegate_observation.Observe(delegate); | ||
} | ||
} | ||
} | ||
|
||
void ServiceWorkerUsbDelegateObserver::RegistrationRemoved( | ||
int64_t registration_id) { | ||
if (registration_id_map().empty()) { | ||
usb_delegate_observation.Reset(); | ||
} | ||
} | ||
|
||
void ServiceWorkerUsbDelegateObserver::DispatchUsbDeviceEventToWorkers( | ||
const device::mojom::UsbDeviceInfo& device_info, | ||
UsbServiceDeviceEventCallback callback) { | ||
for (auto const& [id, info] : registration_id_map()) { | ||
// No need to proceed if the registration doesn't have any event listeners. | ||
if (!info.has_event_handlers) { | ||
continue; | ||
} | ||
// Forward it to UsbService if the service worker is running, UsbService is | ||
// available, and it has clients registered. | ||
auto* usb_service = GetUsbService(id); | ||
if (usb_service) { | ||
auto version = usb_service->service_worker_version(); | ||
if (version && | ||
version->running_status() == EmbeddedWorkerStatus::RUNNING) { | ||
callback.Run(device_info, usb_service); | ||
continue; | ||
} | ||
} | ||
} | ||
} | ||
|
||
WebUsbServiceImpl* ServiceWorkerUsbDelegateObserver::GetUsbService( | ||
int64_t registration_id) { | ||
auto it = usb_services_.find(registration_id); | ||
if (it == usb_services_.end()) { | ||
return nullptr; | ||
} | ||
return it->second.get(); | ||
} | ||
|
||
} // namespace content |
Oops, something went wrong.