Skip to content

Commit

Permalink
[extensions] Deal with a MessagePort being opened for a stopping worker
Browse files Browse the repository at this point in the history
Calling GetRemoteAssociatedInterfaces requires that the worker be
running. When a worker is requested to be stopped
GetRemoteAssociatedInterfaces will cause a CHECK if
IsLiveRunningServiceWorker is not checked before hand. It was not
checeked in the DispatchOnConnect case but was in the UpdatePermissions
case. Return nullptr for GetServiceWorker if we cannot make a connection.

Bug: 1501112
Change-Id: I9fba8d59f570b56b82275a0f7c6b1caf8e4f9af3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5017446
Reviewed-by: Reilly Grant <reillyg@chromium.org>
Commit-Queue: Dave Tapuska <dtapuska@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1223981}
  • Loading branch information
dtapuska authored and Chromium LUCI CQ committed Nov 14, 2023
1 parent 8562867 commit 0b04c4b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
7 changes: 5 additions & 2 deletions extensions/browser/api/messaging/extension_message_port.cc
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,11 @@ void ExtensionMessagePort::DispatchOnConnect(
worker.render_process_id,
PortContext::ForWorker(worker.thread_id, worker.version_id,
worker.extension_id));

host->GetServiceWorker()->DispatchOnConnect(
auto* service_worker_remote = host->GetServiceWorker();
if (!service_worker_remote) {
continue;
}
service_worker_remote->DispatchOnConnect(
port_id_, channel_type, channel_name, source.Clone(), info.Clone(),
std::move(message_port), std::move(message_port_host),
base::BindOnce(&ExtensionMessagePort::OnConnectResponse,
Expand Down
9 changes: 7 additions & 2 deletions extensions/browser/service_worker/service_worker_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ mojom::ServiceWorker* ServiceWorkerHost::GetServiceWorker() {
util::GetServiceWorkerContextForExtensionId(worker_id_.extension_id,
GetBrowserContext());
CHECK(context);
if (!context->IsLiveRunningServiceWorker(worker_id_.version_id)) {
return nullptr;
}

context->GetRemoteAssociatedInterfaces(worker_id_.version_id)
.GetInterface(&remote_);
}
Expand All @@ -279,12 +283,13 @@ void ServiceWorkerHost::OnExtensionPermissionsUpdated(
util::GetServiceWorkerContextForExtensionId(worker_id_.extension_id,
browser_context);
CHECK(context);
if (!context->IsLiveRunningServiceWorker(worker_id_.version_id)) {
auto* service_worker_remote = GetServiceWorker();
if (!service_worker_remote) {
return;
}

const PermissionsData* permissions_data = extension.permissions_data();
GetServiceWorker()->UpdatePermissions(
service_worker_remote->UpdatePermissions(
std::move(*permissions_data->active_permissions().Clone()),
std::move(*permissions_data->withheld_permissions().Clone()));
}
Expand Down
3 changes: 3 additions & 0 deletions extensions/browser/service_worker/service_worker_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ class ServiceWorkerHost :
const PermissionSet& permissions,
PermissionsManager::UpdateReason reason) override;

// Returns the mojo channel to the service worker. It may be null
// if the service worker doesn't have a live service worker matching
// the version id.
mojom::ServiceWorker* GetServiceWorker();

mojo::AssociatedReceiver<mojom::ServiceWorkerHost>& receiver_for_testing() {
Expand Down

0 comments on commit 0b04c4b

Please sign in to comment.