Commit a87baf7d authored by Kenichi Ishibashi's avatar Kenichi Ishibashi Committed by Commit Bot

service worker: Move ServiceWorkerGlobalScopeProxy off Oilpan

The motivation is to avoid creating GarbageCollected objects in
WebEmbeddedWorkerImpl so that we can start service worker on the IO thread,
not on the main thread. The IO thread runs without Oilpan and thus we can't
create GarbageCollected objects on the IO thread.

ServiceWorkerGlobalScopeProxy was a GarbageCollected object because it
contained ParentExecutionContextTaskRunners which is also a
GarbageCollected object. The only usage of the task runners was to get
the default task runner so we can replace it with
Thread::Current()->GetTaskRunner(), which returns a non GarbageCollected
task runner.

After this CL, ServiceWorkerThread owns ServiceWorkerGlobalScopeProxy as
a unique_ptr, not a Persistent. The ownership of these classes are as
follows and there seems no cycle ownership, so that's fine IIUC.

WebEmbeddedWorkerImpl --(unique_ptr)--> ServiceWorkerThread
ServiceWorkerThread --(was Persistent, now unique_ptr)--> ServiceWorkerGlobalScopeProxy
ServiceWorkerThread --(CrossThreadPersistent)--> ServiceWorkerGlobalScope
ServiceWorkerGlobalScopeProxy --(CrossThreadPersistent)--> ServiceWorkerGlobalScope
ServiceWorkerGlobalScope --(raw reference)--> ServiceWorkerGlobalScopeProxy
ServiceWorkerGlobalScope --(raw ptr)--> ServiceWorkerThread

Bug: 988335

Change-Id: I5c9104e73300ac14b56f7ad9b337d71c53321d02
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1725530Reviewed-by: default avatarHiroki Nakagawa <nhiroki@chromium.org>
Commit-Queue: Kenichi Ishibashi <bashi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#683105}
parent 44759908
...@@ -47,7 +47,6 @@ ...@@ -47,7 +47,6 @@
#include "third_party/blink/renderer/core/inspector/console_message.h" #include "third_party/blink/renderer/core/inspector/console_message.h"
#include "third_party/blink/renderer/core/messaging/message_port.h" #include "third_party/blink/renderer/core/messaging/message_port.h"
#include "third_party/blink/renderer/core/origin_trials/origin_trials.h" #include "third_party/blink/renderer/core/origin_trials/origin_trials.h"
#include "third_party/blink/renderer/core/workers/parent_execution_context_task_runners.h"
#include "third_party/blink/renderer/core/workers/worker_global_scope.h" #include "third_party/blink/renderer/core/workers/worker_global_scope.h"
#include "third_party/blink/renderer/core/workers/worker_thread.h" #include "third_party/blink/renderer/core/workers/worker_thread.h"
#include "third_party/blink/renderer/modules/exported/web_embedded_worker_impl.h" #include "third_party/blink/renderer/modules/exported/web_embedded_worker_impl.h"
...@@ -61,11 +60,11 @@ ...@@ -61,11 +60,11 @@
namespace blink { namespace blink {
ServiceWorkerGlobalScopeProxy* ServiceWorkerGlobalScopeProxy::Create( std::unique_ptr<ServiceWorkerGlobalScopeProxy>
WebEmbeddedWorkerImpl& embedded_worker, ServiceWorkerGlobalScopeProxy::Create(WebEmbeddedWorkerImpl& embedded_worker,
WebServiceWorkerContextClient& client) { WebServiceWorkerContextClient& client) {
return MakeGarbageCollected<ServiceWorkerGlobalScopeProxy>(embedded_worker, return std::make_unique<ServiceWorkerGlobalScopeProxy>(embedded_worker,
client); client);
} }
ServiceWorkerGlobalScopeProxy::~ServiceWorkerGlobalScopeProxy() { ServiceWorkerGlobalScopeProxy::~ServiceWorkerGlobalScopeProxy() {
...@@ -74,10 +73,6 @@ ServiceWorkerGlobalScopeProxy::~ServiceWorkerGlobalScopeProxy() { ...@@ -74,10 +73,6 @@ ServiceWorkerGlobalScopeProxy::~ServiceWorkerGlobalScopeProxy() {
DCHECK(!embedded_worker_); DCHECK(!embedded_worker_);
} }
void ServiceWorkerGlobalScopeProxy::Trace(blink::Visitor* visitor) {
visitor->Trace(parent_execution_context_task_runners_);
}
void ServiceWorkerGlobalScopeProxy::BindServiceWorker( void ServiceWorkerGlobalScopeProxy::BindServiceWorker(
mojo::ScopedMessagePipeHandle request) { mojo::ScopedMessagePipeHandle request) {
DCHECK_CALLED_ON_VALID_THREAD(worker_thread_checker_); DCHECK_CALLED_ON_VALID_THREAD(worker_thread_checker_);
...@@ -264,8 +259,7 @@ void ServiceWorkerGlobalScopeProxy::DidCloseWorkerGlobalScope() { ...@@ -264,8 +259,7 @@ void ServiceWorkerGlobalScopeProxy::DidCloseWorkerGlobalScope() {
// ServiceWorkerGlobalScope expects us to terminate the thread, so request // ServiceWorkerGlobalScope expects us to terminate the thread, so request
// that here. // that here.
PostCrossThreadTask( PostCrossThreadTask(
*parent_execution_context_task_runners_->Get(TaskType::kInternalDefault), *parent_task_runner_, FROM_HERE,
FROM_HERE,
CrossThreadBindOnce(&WebEmbeddedWorkerImpl::TerminateWorkerContext, CrossThreadBindOnce(&WebEmbeddedWorkerImpl::TerminateWorkerContext,
CrossThreadUnretained(embedded_worker_))); CrossThreadUnretained(embedded_worker_)));
...@@ -320,12 +314,7 @@ ServiceWorkerGlobalScopeProxy::ServiceWorkerGlobalScopeProxy( ...@@ -320,12 +314,7 @@ ServiceWorkerGlobalScopeProxy::ServiceWorkerGlobalScopeProxy(
worker_global_scope_(nullptr) { worker_global_scope_(nullptr) {
DCHECK(IsMainThread()); DCHECK(IsMainThread());
DETACH_FROM_THREAD(worker_thread_checker_); DETACH_FROM_THREAD(worker_thread_checker_);
// ServiceWorker can sometimes run tasks that are initiated by/associated parent_task_runner_ = Thread::Current()->GetTaskRunner();
// with a document's frame but these documents can be from a different
// process. So we intentionally populate the task runners with default task
// runners of the main thread.
parent_execution_context_task_runners_ =
ParentExecutionContextTaskRunners::Create();
} }
void ServiceWorkerGlobalScopeProxy::Detach() { void ServiceWorkerGlobalScopeProxy::Detach() {
......
...@@ -39,7 +39,6 @@ ...@@ -39,7 +39,6 @@
#include "third_party/blink/public/platform/web_string.h" #include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/public/web/modules/service_worker/web_service_worker_context_proxy.h" #include "third_party/blink/public/web/modules/service_worker/web_service_worker_context_proxy.h"
#include "third_party/blink/renderer/core/workers/worker_reporting_proxy.h" #include "third_party/blink/renderer/core/workers/worker_reporting_proxy.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/heap/persistent.h" #include "third_party/blink/renderer/platform/heap/persistent.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h" #include "third_party/blink/renderer/platform/weborigin/kurl.h"
#include "third_party/blink/renderer/platform/wtf/casting.h" #include "third_party/blink/renderer/platform/wtf/casting.h"
...@@ -47,7 +46,6 @@ ...@@ -47,7 +46,6 @@
namespace blink { namespace blink {
class ParentExecutionContextTaskRunners;
class ServiceWorkerGlobalScope; class ServiceWorkerGlobalScope;
class WebEmbeddedWorkerImpl; class WebEmbeddedWorkerImpl;
class WebServiceWorkerContextClient; class WebServiceWorkerContextClient;
...@@ -64,13 +62,12 @@ class WebServiceWorkerContextClient; ...@@ -64,13 +62,12 @@ class WebServiceWorkerContextClient;
// An instance of this class is supposed to outlive until // An instance of this class is supposed to outlive until
// workerThreadTerminated() is called by its corresponding // workerThreadTerminated() is called by its corresponding
// WorkerGlobalScope. // WorkerGlobalScope.
class ServiceWorkerGlobalScopeProxy final class ServiceWorkerGlobalScopeProxy final : public WebServiceWorkerContextProxy,
: public GarbageCollectedFinalized<ServiceWorkerGlobalScopeProxy>, public WorkerReportingProxy {
public WebServiceWorkerContextProxy,
public WorkerReportingProxy {
public: public:
static ServiceWorkerGlobalScopeProxy* Create(WebEmbeddedWorkerImpl&, static std::unique_ptr<ServiceWorkerGlobalScopeProxy> Create(
WebServiceWorkerContextClient&); WebEmbeddedWorkerImpl&,
WebServiceWorkerContextClient&);
ServiceWorkerGlobalScopeProxy(WebEmbeddedWorkerImpl&, ServiceWorkerGlobalScopeProxy(WebEmbeddedWorkerImpl&,
WebServiceWorkerContextClient&); WebServiceWorkerContextClient&);
...@@ -131,8 +128,6 @@ class ServiceWorkerGlobalScopeProxy final ...@@ -131,8 +128,6 @@ class ServiceWorkerGlobalScopeProxy final
mojom::blink::FetchEventPreloadHandlePtr preload_handle); mojom::blink::FetchEventPreloadHandlePtr preload_handle);
void RequestTermination(base::OnceCallback<void(bool)> callback); void RequestTermination(base::OnceCallback<void(bool)> callback);
void Trace(blink::Visitor*);
// Detaches this proxy object entirely from the outside world, clearing out // Detaches this proxy object entirely from the outside world, clearing out
// all references. // all references.
// //
...@@ -150,8 +145,7 @@ class ServiceWorkerGlobalScopeProxy final ...@@ -150,8 +145,7 @@ class ServiceWorkerGlobalScopeProxy final
// as part of its finalization. // as part of its finalization.
WebEmbeddedWorkerImpl* embedded_worker_; WebEmbeddedWorkerImpl* embedded_worker_;
Member<ParentExecutionContextTaskRunners> scoped_refptr<base::SingleThreadTaskRunner> parent_task_runner_;
parent_execution_context_task_runners_;
WebServiceWorkerContextClient* client_; WebServiceWorkerContextClient* client_;
......
...@@ -46,12 +46,12 @@ ...@@ -46,12 +46,12 @@
namespace blink { namespace blink {
ServiceWorkerThread::ServiceWorkerThread( ServiceWorkerThread::ServiceWorkerThread(
ServiceWorkerGlobalScopeProxy* global_scope_proxy, std::unique_ptr<ServiceWorkerGlobalScopeProxy> global_scope_proxy,
std::unique_ptr<ServiceWorkerInstalledScriptsManager> std::unique_ptr<ServiceWorkerInstalledScriptsManager>
installed_scripts_manager, installed_scripts_manager,
mojom::blink::CacheStoragePtrInfo cache_storage_info) mojom::blink::CacheStoragePtrInfo cache_storage_info)
: WorkerThread(*global_scope_proxy), : WorkerThread(*global_scope_proxy),
global_scope_proxy_(global_scope_proxy), global_scope_proxy_(std::move(global_scope_proxy)),
worker_backing_thread_(std::make_unique<WorkerBackingThread>( worker_backing_thread_(std::make_unique<WorkerBackingThread>(
ThreadCreationParams(GetThreadType()))), ThreadCreationParams(GetThreadType()))),
installed_scripts_manager_(std::move(installed_scripts_manager)), installed_scripts_manager_(std::move(installed_scripts_manager)),
......
...@@ -47,7 +47,7 @@ class MODULES_EXPORT ServiceWorkerThread final : public WorkerThread { ...@@ -47,7 +47,7 @@ class MODULES_EXPORT ServiceWorkerThread final : public WorkerThread {
public: public:
// ServiceWorkerThread owns a given ServiceWorkerGlobalScopeProxy via // ServiceWorkerThread owns a given ServiceWorkerGlobalScopeProxy via
// Persistent. // Persistent.
ServiceWorkerThread(ServiceWorkerGlobalScopeProxy*, ServiceWorkerThread(std::unique_ptr<ServiceWorkerGlobalScopeProxy>,
std::unique_ptr<ServiceWorkerInstalledScriptsManager>, std::unique_ptr<ServiceWorkerInstalledScriptsManager>,
mojom::blink::CacheStoragePtrInfo cache_storage_info); mojom::blink::CacheStoragePtrInfo cache_storage_info);
~ServiceWorkerThread() override; ~ServiceWorkerThread() override;
...@@ -84,7 +84,7 @@ class MODULES_EXPORT ServiceWorkerThread final : public WorkerThread { ...@@ -84,7 +84,7 @@ class MODULES_EXPORT ServiceWorkerThread final : public WorkerThread {
outside_settings_object, outside_settings_object,
network::mojom::CredentialsMode); network::mojom::CredentialsMode);
Persistent<ServiceWorkerGlobalScopeProxy> global_scope_proxy_; std::unique_ptr<ServiceWorkerGlobalScopeProxy> global_scope_proxy_;
std::unique_ptr<WorkerBackingThread> worker_backing_thread_; std::unique_ptr<WorkerBackingThread> worker_backing_thread_;
std::unique_ptr<ServiceWorkerInstalledScriptsManager> std::unique_ptr<ServiceWorkerInstalledScriptsManager>
installed_scripts_manager_; installed_scripts_manager_;
......
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