Commit d0e46e3c authored by Asami Doi's avatar Asami Doi Committed by Chromium LUCI CQ

PlzDedicatedWorker: inherit the service worker from the parent worker

This CL enables a nested blob URL worker to inherit the active service
worker from the parent worker host. `creator_worker_token` has the
parent worker token when the worker is nested, and the child worker can
access the parent's service worker via the parent worker host.

Bug: 1017034
Change-Id: If28169fa5cedac158c220d7a5f16c7874e733a3d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2614526Reviewed-by: default avatarMatt Falkenhagen <falken@chromium.org>
Reviewed-by: default avatarHiroki Nakagawa <nhiroki@chromium.org>
Reviewed-by: default avatarMakoto Shimazu <shimazu@chromium.org>
Commit-Queue: Asami Doi <asamidoi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#843409}
parent 0f51a364
......@@ -7949,6 +7949,7 @@ void RenderFrameHostImpl::CreateDedicatedWorkerHostFactory(
std::make_unique<DedicatedWorkerHostFactoryImpl>(
worker_process_id,
/*creator_render_frame_host_id=*/GetGlobalFrameRoutingId(),
/*creator_worker_token=*/base::nullopt,
/*ancestor_render_frame_host_id=*/GetGlobalFrameRoutingId(),
last_committed_origin_, isolation_info_,
cross_origin_embedder_policy_, std::move(coep_reporter)),
......
......@@ -44,6 +44,7 @@ DedicatedWorkerHost::DedicatedWorkerHost(
const blink::DedicatedWorkerToken& token,
RenderProcessHost* worker_process_host,
base::Optional<GlobalFrameRoutingId> creator_render_frame_host_id,
base::Optional<blink::DedicatedWorkerToken> creator_worker_token,
GlobalFrameRoutingId ancestor_render_frame_host_id,
const url::Origin& creator_origin,
const net::IsolationInfo& isolation_info,
......@@ -54,6 +55,7 @@ DedicatedWorkerHost::DedicatedWorkerHost(
token_(token),
worker_process_host_(worker_process_host),
creator_render_frame_host_id_(creator_render_frame_host_id),
creator_worker_token_(creator_worker_token),
ancestor_render_frame_host_id_(ancestor_render_frame_host_id),
creator_origin_(creator_origin),
// TODO(https://crbug.com/1058759): Calculate the worker origin based on
......@@ -66,6 +68,8 @@ DedicatedWorkerHost::DedicatedWorkerHost(
DCHECK(worker_process_host_);
DCHECK(worker_process_host_->IsInitializedAndNotDead());
DCHECK(coep_reporter_);
DCHECK((creator_render_frame_host_id_ && !creator_worker_token_) ||
(!creator_render_frame_host_id_ && creator_worker_token_));
scoped_process_host_observation_.Observe(worker_process_host_);
......@@ -190,16 +194,28 @@ void DedicatedWorkerHost::StartScriptLoad(
// See https://w3c.github.io/ServiceWorker/#control-and-use-worker-client
if (script_url.SchemeIsBlob()) {
if (creator_render_frame_host_id_) {
// The creator of this worker is a frame.
base::WeakPtr<ServiceWorkerContainerHost> creator_container_host =
RenderFrameHostImpl::FromID(creator_render_frame_host_id_.value())
->GetLastCommittedServiceWorkerHost();
service_worker_handle_->set_parent_container_host(creator_container_host);
} else {
// TODO(https://crbug.com/1017034): When this worker is nested, the worker
// should inherit the active service worker from the parent worker host.
// Implement this behavior.
NOTIMPLEMENTED();
// The creator of this worker is a dedicated worker.
DCHECK(creator_worker_token_);
DedicatedWorkerHost* creator_worker =
service_->GetDedicatedWorkerHostFromToken(
creator_worker_token_.value());
if (!creator_worker) {
client_->OnScriptLoadStartFailed();
return;
}
base::WeakPtr<ServiceWorkerContainerHost> creator_container_host =
creator_worker->service_worker_handle()->container_host();
service_worker_handle_->set_parent_container_host(creator_container_host);
}
}
......@@ -426,14 +442,16 @@ void DedicatedWorkerHost::CreateNestedDedicatedWorker(
mojo::PendingRemote<network::mojom::CrossOriginEmbedderPolicyReporter>
coep_reporter;
coep_reporter_->Clone(coep_reporter.InitWithNewPipeAndPassReceiver());
// There is no creator frame when the worker is nested.
// Set this worker as the creator of the new worker and inherit the ancestor
// render frame.
mojo::MakeSelfOwnedReceiver(
std::make_unique<DedicatedWorkerHostFactoryImpl>(
worker_process_host_->GetID(),
/*creator_render_frame_host_id_=*/base::nullopt,
ancestor_render_frame_host_id_, worker_origin_, isolation_info_,
cross_origin_embedder_policy_, std::move(coep_reporter)),
/*creator_worker_token=*/token_, ancestor_render_frame_host_id_,
worker_origin_, isolation_info_, cross_origin_embedder_policy_,
std::move(coep_reporter)),
std::move(receiver));
}
......
......@@ -54,6 +54,7 @@ class DedicatedWorkerHost final : public RenderProcessHostObserver {
const blink::DedicatedWorkerToken& token,
RenderProcessHost* worker_process_host,
base::Optional<GlobalFrameRoutingId> creator_render_frame_host_id,
base::Optional<blink::DedicatedWorkerToken> creator_worker_token,
GlobalFrameRoutingId ancestor_render_frame_host_id,
const url::Origin& creator_origin,
const net::IsolationInfo& isolation_info,
......@@ -119,6 +120,10 @@ class DedicatedWorkerHost final : public RenderProcessHostObserver {
return cross_origin_embedder_policy_;
}
ServiceWorkerMainResourceHandle* service_worker_handle() {
return service_worker_handle_.get();
}
private:
// RenderProcessHostObserver:
void RenderProcessExited(RenderProcessHost* render_process_host,
......@@ -187,6 +192,10 @@ class DedicatedWorkerHost final : public RenderProcessHostObserver {
// when this worker is nested.
const base::Optional<GlobalFrameRoutingId> creator_render_frame_host_id_;
// The token of the dedicated worker that directly starts this worker. This is
// base::nullopt when this worker is created from a frame.
const base::Optional<blink::DedicatedWorkerToken> creator_worker_token_;
// The ID of the frame that owns this worker, either directly, or (in the case
// of nested workers) indirectly via a tree of dedicated workers.
const GlobalFrameRoutingId ancestor_render_frame_host_id_;
......
......@@ -38,6 +38,7 @@ DedicatedWorkerServiceImpl* GetDedicatedWorkerServiceImplForRenderProcessHost(
DedicatedWorkerHostFactoryImpl::DedicatedWorkerHostFactoryImpl(
int worker_process_id,
base::Optional<GlobalFrameRoutingId> creator_render_frame_host_id,
base::Optional<blink::DedicatedWorkerToken> creator_worker_token,
GlobalFrameRoutingId ancestor_render_frame_host_id,
const url::Origin& creator_origin,
const net::IsolationInfo& isolation_info,
......@@ -46,12 +47,15 @@ DedicatedWorkerHostFactoryImpl::DedicatedWorkerHostFactoryImpl(
coep_reporter)
: worker_process_id_(worker_process_id),
creator_render_frame_host_id_(creator_render_frame_host_id),
creator_worker_token_(creator_worker_token),
ancestor_render_frame_host_id_(ancestor_render_frame_host_id),
creator_origin_(creator_origin),
isolation_info_(isolation_info),
cross_origin_embedder_policy_(cross_origin_embedder_policy),
coep_reporter_(std::move(coep_reporter)) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK((creator_render_frame_host_id_ && !creator_worker_token_) ||
(!creator_render_frame_host_id_ && creator_worker_token_));
}
DedicatedWorkerHostFactoryImpl::~DedicatedWorkerHostFactoryImpl() = default;
......@@ -92,8 +96,8 @@ void DedicatedWorkerHostFactoryImpl::CreateWorkerHost(
auto* host = new DedicatedWorkerHost(
service, token, worker_process_host, creator_render_frame_host_id_,
ancestor_render_frame_host_id_, creator_origin_, isolation_info_,
cross_origin_embedder_policy_, std::move(coep_reporter));
creator_worker_token_, ancestor_render_frame_host_id_, creator_origin_,
isolation_info_, cross_origin_embedder_policy_, std::move(coep_reporter));
host->BindBrowserInterfaceBrokerReceiver(std::move(broker_receiver));
}
......@@ -134,8 +138,8 @@ void DedicatedWorkerHostFactoryImpl::CreateWorkerHostAndStartScriptLoad(
auto* host = new DedicatedWorkerHost(
service, token, worker_process_host, creator_render_frame_host_id_,
ancestor_render_frame_host_id_, creator_origin_, isolation_info_,
cross_origin_embedder_policy_, std::move(coep_reporter));
creator_worker_token_, ancestor_render_frame_host_id_, creator_origin_,
isolation_info_, cross_origin_embedder_policy_, std::move(coep_reporter));
mojo::PendingRemote<blink::mojom::BrowserInterfaceBroker> broker;
host->BindBrowserInterfaceBrokerReceiver(
broker.InitWithNewPipeAndPassReceiver());
......
......@@ -26,6 +26,7 @@ class CONTENT_EXPORT DedicatedWorkerHostFactoryImpl
DedicatedWorkerHostFactoryImpl(
int worker_process_id,
base::Optional<GlobalFrameRoutingId> creator_render_frame_host_id,
base::Optional<blink::DedicatedWorkerToken> creator_worker_token,
GlobalFrameRoutingId ancestor_render_frame_host_id,
const url::Origin& creator_origin,
const net::IsolationInfo& isolation_info,
......@@ -59,6 +60,7 @@ class CONTENT_EXPORT DedicatedWorkerHostFactoryImpl
// See comments on the corresponding members of DedicatedWorkerHost.
const base::Optional<GlobalFrameRoutingId> creator_render_frame_host_id_;
const base::Optional<blink::DedicatedWorkerToken> creator_worker_token_;
const GlobalFrameRoutingId ancestor_render_frame_host_id_;
const url::Origin creator_origin_;
......
......@@ -42,7 +42,8 @@ class MockDedicatedWorker
mojo::MakeSelfOwnedReceiver(
std::make_unique<DedicatedWorkerHostFactoryImpl>(
worker_process_id, render_frame_host_id, render_frame_host_id,
worker_process_id, render_frame_host_id,
/*creator_worker_token=*/base::nullopt, render_frame_host_id,
url::Origin(), net::IsolationInfo::CreateTransient(),
network::CrossOriginEmbedderPolicy(),
std::move(coep_reporter_remote)),
......
This is a testharness.js-based test.
FAIL Nested blob URL workers should be intercepted by a service worker. assert_equals: fetch() should be intercepted. expected "intercepted by service worker" but got "{\"error\": {\"code\": 404, \"message\": \"\"}}"
PASS Nested worker created from a blob URL worker should be intercepted by a service worker.
FAIL Nested blob URL worker created from a worker should be intercepted by a service worker. assert_equals: fetch() should be intercepted. expected "intercepted by service worker" but got "{\"error\": {\"code\": 404, \"message\": \"\"}}"
Harness: the test ran to completion.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment