Commit 71d7f0df authored by Hiroki Nakagawa's avatar Hiroki Nakagawa Committed by Commit Bot

ServiceWorker: Move PostMessageToClient() and CountFeature() to ServiceWorkerContainerHost

This is a part of the effort to split ServiceWorkerProviderHost into
ServiceWorkerHost and ServiceWorkerContainerHost.

This CL moves PostMessageToClient(), CountFeature() and relevant
functions from ServiceWorkerProviderHost to ServiceWorkerContainerHost.

Design doc:
https://docs.google.com/document/d/1epWIgelE-7uwxJHrYPKlbwqMRP9in2xLUR6mpiU_afY/edit?usp=sharing

Bug: 931087
Change-Id: If15c9606e7a48bdbab40d2fece6a09bcaf4d0eb9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1930363
Commit-Queue: Hiroki Nakagawa <nhiroki@chromium.org>
Reviewed-by: default avatarKenichi Ishibashi <bashi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#718496}
parent f1dc63bf
......@@ -29,6 +29,8 @@ void RunCallbacks(
ServiceWorkerContainerHost::ServiceWorkerContainerHost(
blink::mojom::ServiceWorkerProviderType type,
bool is_parent_frame_secure,
mojo::PendingAssociatedRemote<blink::mojom::ServiceWorkerContainer>
container_remote,
ServiceWorkerProviderHost* provider_host,
base::WeakPtr<ServiceWorkerContextCore> context)
: type_(type),
......@@ -37,6 +39,12 @@ ServiceWorkerContainerHost::ServiceWorkerContainerHost(
context_(std::move(context)) {
DCHECK(provider_host_);
DCHECK(context_);
if (container_remote) {
DCHECK(IsContainerForClient());
container_.Bind(std::move(container_remote));
} else {
DCHECK(IsContainerForServiceWorker());
}
}
ServiceWorkerContainerHost::~ServiceWorkerContainerHost() {
......@@ -44,6 +52,40 @@ ServiceWorkerContainerHost::~ServiceWorkerContainerHost() {
RunExecutionReadyCallbacks();
}
void ServiceWorkerContainerHost::PostMessageToClient(
ServiceWorkerVersion* version,
blink::TransferableMessage message) {
DCHECK(IsContainerForClient());
blink::mojom::ServiceWorkerObjectInfoPtr info;
base::WeakPtr<ServiceWorkerObjectHost> object_host =
GetOrCreateServiceWorkerObjectHost(version);
if (object_host)
info = object_host->CreateCompleteObjectInfoToSend();
container_->PostMessageToClient(std::move(info), std::move(message));
}
void ServiceWorkerContainerHost::CountFeature(
blink::mojom::WebFeature feature) {
// CountFeature is a message about the client's controller. It should be sent
// only for clients.
DCHECK(IsContainerForClient());
// And only when loading finished so the controller is really settled.
if (!IsControllerDecided())
return;
container_->CountFeature(feature);
}
void ServiceWorkerContainerHost::SendSetControllerServiceWorker(
blink::mojom::ControllerServiceWorkerInfoPtr controller_info,
bool notify_controllerchange) {
DCHECK(IsContainerForClient());
container_->SetController(std::move(controller_info),
notify_controllerchange);
}
blink::mojom::ServiceWorkerRegistrationObjectInfoPtr
ServiceWorkerContainerHost::CreateServiceWorkerRegistrationObjectInfo(
scoped_refptr<ServiceWorkerRegistration> registration) {
......@@ -234,6 +276,36 @@ void ServiceWorkerContainerHost::TransitionToClientPhase(
client_phase_ = new_phase;
}
bool ServiceWorkerContainerHost::IsControllerDecided() const {
DCHECK(IsContainerForClient());
if (is_execution_ready())
return true;
// TODO(falken): This function just becomes |is_execution_ready()|
// when NetworkService is enabled, so remove/simplify it when
// non-NetworkService code is removed.
switch (client_type()) {
case blink::mojom::ServiceWorkerClientType::kWindow:
// |this| is hosting a reserved client undergoing navigation. Don't send
// the controller since it can be changed again before the final
// response. The controller will be sent on navigation commit. See
// CommitNavigation in frame.mojom.
return false;
case blink::mojom::ServiceWorkerClientType::kDedicatedWorker:
case blink::mojom::ServiceWorkerClientType::kSharedWorker:
// When PlzWorker is enabled, the controller will be sent when the
// response is committed to the renderer.
return false;
case blink::mojom::ServiceWorkerClientType::kAll:
NOTREACHED();
}
NOTREACHED();
return true;
}
void ServiceWorkerContainerHost::RunExecutionReadyCallbacks() {
std::vector<ExecutionReadyCallback> callbacks;
execution_ready_callbacks_.swap(callbacks);
......
......@@ -11,7 +11,9 @@
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "content/common/content_export.h"
#include "mojo/public/cpp/bindings/associated_remote.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_client.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_container.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_provider_type.mojom.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_registration.mojom.h"
......@@ -55,10 +57,13 @@ class CONTENT_EXPORT ServiceWorkerContainerHost {
// TODO(https://crbug.com/931087): Rename ServiceWorkerProviderType to
// ServiceWorkerContainerType.
ServiceWorkerContainerHost(blink::mojom::ServiceWorkerProviderType type,
bool is_parent_frame_secure,
ServiceWorkerProviderHost* provider_host,
base::WeakPtr<ServiceWorkerContextCore> context);
ServiceWorkerContainerHost(
blink::mojom::ServiceWorkerProviderType type,
bool is_parent_frame_secure,
mojo::PendingAssociatedRemote<blink::mojom::ServiceWorkerContainer>
container_remote,
ServiceWorkerProviderHost* provider_host,
base::WeakPtr<ServiceWorkerContextCore> context);
~ServiceWorkerContainerHost();
ServiceWorkerContainerHost(const ServiceWorkerContainerHost& other) = delete;
......@@ -68,6 +73,22 @@ class CONTENT_EXPORT ServiceWorkerContainerHost {
ServiceWorkerContainerHost& operator=(ServiceWorkerContainerHost&& other) =
delete;
// Dispatches message event to the client (document, dedicated worker when
// PlzDedicatedWorker is enabled, or shared worker).
void PostMessageToClient(ServiceWorkerVersion* version,
blink::TransferableMessage message);
// Notifies the client that its controller used a feature, for UseCounter
// purposes. This can only be called if IsContainerForClient() is true.
void CountFeature(blink::mojom::WebFeature feature);
// Sends information about the controller to the container of the service
// worker clients in the renderer. If |notify_controllerchange| is true,
// instructs the renderer to dispatch a 'controllerchange' event.
void SendSetControllerServiceWorker(
blink::mojom::ControllerServiceWorkerInfoPtr controller_info,
bool notify_controllerchange);
// Returns an object info representing |registration|. The object info holds a
// Mojo connection to the ServiceWorkerRegistrationObjectHost for the
// |registration| to ensure the host stays alive while the object info is
......@@ -205,6 +226,14 @@ class CONTENT_EXPORT ServiceWorkerContainerHost {
// section.
void TransitionToClientPhase(ClientPhase new_phase);
// For service worker clients. Returns false if it's not yet time to send the
// renderer information about the controller. Basically returns false if this
// client is still loading so due to potential redirects the initial
// controller has not yet been decided.
// TODO(https://crbug.com/931087): Move this function into the private
// section.
bool IsControllerDecided() const;
private:
friend class service_worker_object_host_unittest::ServiceWorkerObjectHostTest;
FRIEND_TEST_ALL_PREFIXES(ServiceWorkerJobTest, Unregister);
......@@ -254,6 +283,10 @@ class CONTENT_EXPORT ServiceWorkerContainerHost {
std::map<int64_t /* version_id */, std::unique_ptr<ServiceWorkerObjectHost>>
service_worker_object_hosts_;
// |container_| is the remote renderer-side ServiceWorkerContainer that |this|
// is hosting.
mojo::AssociatedRemote<blink::mojom::ServiceWorkerContainer> container_;
// This provider host owns |this|.
// TODO(https://crbug.com/931087): Remove dependencies on |provider_host_|.
ServiceWorkerProviderHost* provider_host_ = nullptr;
......
......@@ -162,11 +162,11 @@ ServiceWorkerProviderHost::PreCreateNavigationHost(
mojo::PendingAssociatedReceiver<blink::mojom::ServiceWorkerContainerHost>
host_receiver,
mojo::PendingAssociatedRemote<blink::mojom::ServiceWorkerContainer>
client_remote) {
container_remote) {
DCHECK(context);
auto host = base::WrapUnique(new ServiceWorkerProviderHost(
blink::mojom::ServiceWorkerProviderType::kForWindow, are_ancestors_secure,
frame_tree_node_id, std::move(host_receiver), std::move(client_remote),
frame_tree_node_id, std::move(host_receiver), std::move(container_remote),
/*running_hosted_version=*/nullptr, context));
auto weak_ptr = host->AsWeakPtr();
RegisterToContextCore(context, std::move(host));
......@@ -184,7 +184,7 @@ ServiceWorkerProviderHost::CreateForServiceWorker(
blink::mojom::ServiceWorkerProviderType::kForServiceWorker,
/*is_parent_frame_secure=*/true, FrameTreeNode::kFrameTreeNodeInvalidId,
(*out_provider_info)->host_remote.InitWithNewEndpointAndPassReceiver(),
/*client_remote=*/mojo::NullAssociatedRemote(), std::move(version),
/*container_remote=*/mojo::NullAssociatedRemote(), std::move(version),
context));
auto weak_ptr = host->AsWeakPtr();
RegisterToContextCore(context, std::move(host));
......@@ -200,7 +200,7 @@ ServiceWorkerProviderHost::CreateForWebWorker(
mojo::PendingAssociatedReceiver<blink::mojom::ServiceWorkerContainerHost>
host_receiver,
mojo::PendingAssociatedRemote<blink::mojom::ServiceWorkerContainer>
client_remote) {
container_remote) {
using ServiceWorkerProviderType = blink::mojom::ServiceWorkerProviderType;
DCHECK((base::FeatureList::IsEnabled(blink::features::kPlzDedicatedWorker) &&
provider_type == ServiceWorkerProviderType::kForDedicatedWorker) ||
......@@ -208,7 +208,8 @@ ServiceWorkerProviderHost::CreateForWebWorker(
auto host = base::WrapUnique(new ServiceWorkerProviderHost(
provider_type, /*is_parent_frame_secure=*/true,
FrameTreeNode::kFrameTreeNodeInvalidId, std::move(host_receiver),
std::move(client_remote), /*running_hosted_version=*/nullptr, context));
std::move(container_remote), /*running_hosted_version=*/nullptr,
context));
host->SetRenderProcessId(process_id);
auto weak_ptr = host->AsWeakPtr();
......@@ -234,7 +235,7 @@ ServiceWorkerProviderHost::ServiceWorkerProviderHost(
mojo::PendingAssociatedReceiver<blink::mojom::ServiceWorkerContainerHost>
host_receiver,
mojo::PendingAssociatedRemote<blink::mojom::ServiceWorkerContainer>
client_remote,
container_remote,
scoped_refptr<ServiceWorkerVersion> running_hosted_version,
base::WeakPtr<ServiceWorkerContextCore> context)
: provider_id_(NextProviderId()),
......@@ -255,27 +256,23 @@ ServiceWorkerProviderHost::ServiceWorkerProviderHost(
container_host_(std::make_unique<content::ServiceWorkerContainerHost>(
type,
is_parent_frame_secure,
std::move(container_remote),
this,
context)) {
DCHECK_NE(blink::mojom::ServiceWorkerProviderType::kUnknown, type);
if (type == blink::mojom::ServiceWorkerProviderType::kForServiceWorker) {
DCHECK(!client_remote);
DCHECK(running_hosted_version_);
container_host_->UpdateUrls(running_hosted_version_->script_url(),
running_hosted_version_->script_url(),
running_hosted_version_->script_origin());
} else {
DCHECK(client_remote.is_valid());
// For service worker clients, ServiceWorkerProviderHost::UpdateUrls() will
// be called later.
}
// For service worker clients, ServiceWorkerProviderHost::UpdateUrls() will
// be called later.
context_->RegisterProviderHostByClientID(client_uuid_, this);
DCHECK(host_receiver.is_valid());
if (client_remote.is_valid())
container_.Bind(std::move(client_remote));
receiver_.Bind(std::move(host_receiver));
}
......@@ -497,7 +494,7 @@ void ServiceWorkerProviderHost::UpdateController(bool notify_controllerchange) {
// SetController message should be sent only for clients.
DCHECK(IsProviderForClient());
if (!IsControllerDecided())
if (!container_host_->IsControllerDecided())
return;
SendSetControllerServiceWorker(notify_controllerchange);
......@@ -589,31 +586,6 @@ void ServiceWorkerProviderHost::AddServiceWorkerToUpdate(
versions_to_update_.emplace(std::move(version));
}
void ServiceWorkerProviderHost::PostMessageToClient(
ServiceWorkerVersion* version,
blink::TransferableMessage message) {
DCHECK(IsProviderForClient());
blink::mojom::ServiceWorkerObjectInfoPtr info;
base::WeakPtr<ServiceWorkerObjectHost> object_host =
container_host_->GetOrCreateServiceWorkerObjectHost(version);
if (object_host)
info = object_host->CreateCompleteObjectInfoToSend();
container_->PostMessageToClient(std::move(info), std::move(message));
}
void ServiceWorkerProviderHost::CountFeature(blink::mojom::WebFeature feature) {
// CountFeature is a message about the client's controller. It should be sent
// only for clients.
DCHECK(IsProviderForClient());
// And only when loading finished so the controller is really settled.
if (!IsControllerDecided())
return;
container_->CountFeature(feature);
}
void ServiceWorkerProviderHost::ClaimedByRegistration(
scoped_refptr<ServiceWorkerRegistration> registration) {
DCHECK(registration->active_version());
......@@ -784,8 +756,8 @@ void ServiceWorkerProviderHost::SendSetControllerServiceWorker(
}
if (!controller_) {
container_->SetController(std::move(controller_info),
notify_controllerchange);
container_host_->SendSetControllerServiceWorker(std::move(controller_info),
notify_controllerchange);
return;
}
......@@ -812,38 +784,8 @@ void ServiceWorkerProviderHost::SendSetControllerServiceWorker(
for (const blink::mojom::WebFeature feature : controller_->used_features())
controller_info->used_features.push_back(feature);
container_->SetController(std::move(controller_info),
notify_controllerchange);
}
bool ServiceWorkerProviderHost::IsControllerDecided() const {
DCHECK(IsProviderForClient());
if (container_host_->is_execution_ready())
return true;
// TODO(falken): This function just becomes |is_execution_ready()|
// when NetworkService is enabled, so remove/simplify it when
// non-NetworkService code is removed.
switch (container_host_->client_type()) {
case blink::mojom::ServiceWorkerClientType::kWindow:
// |this| is hosting a reserved client undergoing navigation. Don't send
// the controller since it can be changed again before the final
// response. The controller will be sent on navigation commit. See
// CommitNavigation in frame.mojom.
return false;
case blink::mojom::ServiceWorkerClientType::kDedicatedWorker:
case blink::mojom::ServiceWorkerClientType::kSharedWorker:
// When PlzWorker is enabled, the controller will be sent when the
// response is committed to the renderer.
return false;
case blink::mojom::ServiceWorkerClientType::kAll:
NOTREACHED();
}
NOTREACHED();
return true;
container_host_->SendSetControllerServiceWorker(std::move(controller_info),
notify_controllerchange);
}
#if DCHECK_IS_ON()
......@@ -1230,7 +1172,7 @@ void ServiceWorkerProviderHost::OnExecutionReady() {
// because 1) the controller might have changed since navigation commit due to
// skipWaiting(), and 2) the UseCounter might have changed since navigation
// commit, in such cases the updated information was prevented being sent due
// to false IsControllerDecided().
// to false ServiceWorkerContainerHost::IsControllerDecided().
// TODO(leonhsl): Create some layout tests covering the above case 1), in
// which case we may also need to set |notify_controllerchange| correctly.
SendSetControllerServiceWorker(false /* notify_controllerchange */);
......
......@@ -137,7 +137,7 @@ class CONTENT_EXPORT ServiceWorkerProviderHost
mojo::PendingAssociatedReceiver<blink::mojom::ServiceWorkerContainerHost>
host_receiver,
mojo::PendingAssociatedRemote<blink::mojom::ServiceWorkerContainer>
client_remote);
container_remote);
// Used for starting a service worker. Returns a provider host for the service
// worker and partially fills |out_provider_info|. The host stays alive as
......@@ -161,7 +161,7 @@ class CONTENT_EXPORT ServiceWorkerProviderHost
mojo::PendingAssociatedReceiver<blink::mojom::ServiceWorkerContainerHost>
host_receiver,
mojo::PendingAssociatedRemote<blink::mojom::ServiceWorkerContainer>
client_remote);
container_remote);
~ServiceWorkerProviderHost() override;
......@@ -265,14 +265,6 @@ class CONTENT_EXPORT ServiceWorkerProviderHost
// May return nullptr if the context has shut down.
base::WeakPtr<ServiceWorkerContextCore> context() { return context_; }
// Dispatches message event to the document.
void PostMessageToClient(ServiceWorkerVersion* version,
blink::TransferableMessage message);
// Notifies the client that its controller used a feature, for UseCounter
// purposes. This can only be called if IsProviderForClient() is true.
void CountFeature(blink::mojom::WebFeature feature);
// |registration| claims the document to be controlled.
void ClaimedByRegistration(
scoped_refptr<ServiceWorkerRegistration> registration);
......@@ -384,7 +376,7 @@ class CONTENT_EXPORT ServiceWorkerProviderHost
mojo::PendingAssociatedReceiver<blink::mojom::ServiceWorkerContainerHost>
host_receiver,
mojo::PendingAssociatedRemote<blink::mojom::ServiceWorkerContainer>
client_remote,
container_remote,
scoped_refptr<ServiceWorkerVersion> running_hosted_version,
base::WeakPtr<ServiceWorkerContextCore> context);
......@@ -422,12 +414,6 @@ class CONTENT_EXPORT ServiceWorkerProviderHost
// instructs the renderer to dispatch a 'controllerchange' event.
void SendSetControllerServiceWorker(bool notify_controllerchange);
// For service worker clients. Returns false if it's not yet time to send the
// renderer information about the controller. Basically returns false if this
// client is still loading so due to potential redirects the initial
// controller has not yet been decided.
bool IsControllerDecided() const;
#if DCHECK_IS_ON()
void CheckControllerConsistency(bool should_crash) const;
#endif // DCHECK_IS_ON()
......@@ -580,9 +566,6 @@ class CONTENT_EXPORT ServiceWorkerProviderHost
// just be a raw ptr that is never null.
base::WeakPtr<ServiceWorkerContextCore> context_;
// |container_| is the remote renderer-side ServiceWorkerContainer that |this|
// is a ServiceWorkerContainerHost for.
mojo::AssociatedRemote<blink::mojom::ServiceWorkerContainer> container_;
// |receiver_| keeps the connection to the renderer-side counterpart
// (content::ServiceWorkerProviderContext). When the connection bound on
// |receiver_| gets killed from the renderer side, or the bound
......
......@@ -1309,7 +1309,7 @@ void ServiceWorkerVersion::PostMessageToClient(
receiver_.reset();
return;
}
provider_host->PostMessageToClient(this, std::move(message));
container_host->PostMessageToClient(this, std::move(message));
}
void ServiceWorkerVersion::FocusClient(const std::string& client_uuid,
......@@ -1530,7 +1530,7 @@ void ServiceWorkerVersion::CountFeature(blink::mojom::WebFeature feature) {
if (!used_features_.insert(feature).second)
return;
for (auto provider_host_by_uuid : controllee_map_)
provider_host_by_uuid.second->CountFeature(feature);
provider_host_by_uuid.second->container_host()->CountFeature(feature);
}
// static
......
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