Commit 70506ba4 authored by nhiroki's avatar nhiroki Committed by Commit bot

Worklet: Introduce PaintWorkletGlobalScopeProxy

This is split from https://codereview.chromium.org/2840523002/

Before this CL, PaintWorklet is tightly coupled with PaintWorkletGlobalScope,
and that is an obstacle to unifying MainThreadWorklet and ThreadedWorklet.

This CL introduces PaintWorkletGlobalScopeProxy to separate
PaintWorkletGlobalScope from PaintWorklet and makes it easier to abstract
communication between Worklet and WorkletGlobalScope in future CLs.

BUG=627945

Review-Url: https://codereview.chromium.org/2853743002
Cr-Commit-Position: refs/heads/master@{#468572}
parent 6d749aec
......@@ -66,15 +66,8 @@ void MainThreadWorkletGlobalScope::FetchAndInvokeScript(
script_loader->FetchScript(module_url_record);
}
void MainThreadWorkletGlobalScope::EvaluateScript(
const ScriptSourceCode& script_source_code) {
// This should be called only for threaded worklets that still use classic
// script loading.
NOTREACHED();
}
// TODO(nhiroki): Add tests for termination.
void MainThreadWorkletGlobalScope::TerminateWorkletGlobalScope() {
void MainThreadWorkletGlobalScope::Terminate() {
for (auto it = loader_map_.begin(); it != loader_map_.end();) {
WorkletScriptLoader* script_loader = it->key;
// Cancel() eventually calls NotifyWorkletScriptLoadingFinished() and
......
......@@ -10,7 +10,6 @@
#include "core/dom/ExecutionContext.h"
#include "core/loader/WorkletScriptLoader.h"
#include "core/workers/WorkletGlobalScope.h"
#include "core/workers/WorkletGlobalScopeProxy.h"
#include "core/workers/WorkletPendingTasks.h"
namespace blink {
......@@ -21,7 +20,6 @@ class ScriptSourceCode;
class CORE_EXPORT MainThreadWorkletGlobalScope
: public WorkletGlobalScope,
public WorkletGlobalScopeProxy,
public WorkletScriptLoader::Client,
public ContextClient {
USING_GARBAGE_COLLECTED_MIXIN(MainThreadWorkletGlobalScope);
......@@ -40,11 +38,9 @@ class CORE_EXPORT MainThreadWorkletGlobalScope
void CountDeprecation(UseCounter::Feature) final;
WorkerThread* GetThread() const final;
// WorkletGlobalScopeProxy
void FetchAndInvokeScript(const KURL& module_url_record,
WorkletPendingTasks*) final;
void EvaluateScript(const ScriptSourceCode&) final;
void TerminateWorkletGlobalScope() final;
WorkletPendingTasks*);
void Terminate();
// WorkletScriptLoader::Client
void NotifyWorkletScriptLoadingFinished(WorkletScriptLoader*,
......
......@@ -22,7 +22,7 @@ class MainThreadWorkletTest : public ::testing::Test {
ToIsolate(page_->GetFrame().GetDocument()));
}
void TearDown() override { global_scope_->TerminateWorkletGlobalScope(); }
void TearDown() override { global_scope_->Terminate(); }
protected:
RefPtr<SecurityOrigin> security_origin_;
......
......@@ -13,8 +13,9 @@ namespace blink {
class ScriptSourceCode;
class WorkletPendingTasks;
// A proxy to talk to the worklet global scope. The global scope may exist in
// the main thread or on a different thread.
// Abstracts communication from (Main/Threaded)Worklet on the main thread to
// (Main/Threaded)WorkletGlobalScope so that Worklet class doesn't have to care
// about the thread WorkletGlobalScope runs on.
class CORE_EXPORT WorkletGlobalScopeProxy {
public:
virtual ~WorkletGlobalScopeProxy() {}
......
......@@ -17,6 +17,8 @@ blink_modules_sources("csspaint") {
"PaintWorklet.h",
"PaintWorkletGlobalScope.cpp",
"PaintWorkletGlobalScope.h",
"PaintWorkletGlobalScopeProxy.cpp",
"PaintWorkletGlobalScopeProxy.h",
"WindowPaintWorklet.cpp",
"WindowPaintWorklet.h",
]
......
......@@ -18,30 +18,25 @@ PaintWorklet* PaintWorklet::Create(LocalFrame* frame) {
PaintWorklet::PaintWorklet(LocalFrame* frame)
: MainThreadWorklet(frame),
paint_worklet_global_scope_(PaintWorkletGlobalScope::Create(
frame,
frame->GetDocument()->Url(),
frame->GetDocument()->UserAgent(),
frame->GetDocument()->GetSecurityOrigin(),
ToIsolate(frame->GetDocument()))) {}
global_scope_proxy_(
WTF::MakeUnique<PaintWorkletGlobalScopeProxy>(frame)) {}
PaintWorklet::~PaintWorklet() {}
PaintWorklet::~PaintWorklet() = default;
PaintWorkletGlobalScope* PaintWorklet::GetWorkletGlobalScopeProxy() const {
return paint_worklet_global_scope_.Get();
WorkletGlobalScopeProxy* PaintWorklet::GetWorkletGlobalScopeProxy() const {
return global_scope_proxy_.get();
}
CSSPaintDefinition* PaintWorklet::FindDefinition(const String& name) {
return paint_worklet_global_scope_->FindDefinition(name);
return global_scope_proxy_->FindDefinition(name);
}
void PaintWorklet::AddPendingGenerator(const String& name,
CSSPaintImageGeneratorImpl* generator) {
return paint_worklet_global_scope_->AddPendingGenerator(name, generator);
return global_scope_proxy_->AddPendingGenerator(name, generator);
}
DEFINE_TRACE(PaintWorklet) {
visitor->Trace(paint_worklet_global_scope_);
MainThreadWorklet::Trace(visitor);
}
......
......@@ -7,7 +7,7 @@
#include "core/workers/MainThreadWorklet.h"
#include "modules/ModulesExport.h"
#include "modules/csspaint/PaintWorkletGlobalScope.h"
#include "modules/csspaint/PaintWorkletGlobalScopeProxy.h"
#include "platform/heap/Handle.h"
namespace blink {
......@@ -15,6 +15,8 @@ namespace blink {
class CSSPaintDefinition;
class CSSPaintImageGeneratorImpl;
// Manages a paint worklet:
// https://drafts.css-houdini.org/css-paint-api/#dom-css-paintworklet
class MODULES_EXPORT PaintWorklet final : public MainThreadWorklet {
WTF_MAKE_NONCOPYABLE(PaintWorklet);
......@@ -22,7 +24,7 @@ class MODULES_EXPORT PaintWorklet final : public MainThreadWorklet {
static PaintWorklet* Create(LocalFrame*);
~PaintWorklet() override;
PaintWorkletGlobalScope* GetWorkletGlobalScopeProxy() const final;
WorkletGlobalScopeProxy* GetWorkletGlobalScopeProxy() const final;
CSSPaintDefinition* FindDefinition(const String& name);
void AddPendingGenerator(const String& name, CSSPaintImageGeneratorImpl*);
......@@ -31,7 +33,8 @@ class MODULES_EXPORT PaintWorklet final : public MainThreadWorklet {
private:
explicit PaintWorklet(LocalFrame*);
Member<PaintWorkletGlobalScope> paint_worklet_global_scope_;
// TODO(nhiroki): Make (Paint)WorkletGlobalScopeProxy GC-managed object.
std::unique_ptr<PaintWorkletGlobalScopeProxy> global_scope_proxy_;
};
} // namespace blink
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "modules/csspaint/PaintWorkletGlobalScopeProxy.h"
#include "bindings/core/v8/ScriptSourceCode.h"
#include "bindings/core/v8/V8BindingForCore.h"
#include "core/dom/Document.h"
#include "core/frame/LocalFrame.h"
#include "platform/weborigin/KURL.h"
#include "platform/wtf/WTF.h"
namespace blink {
PaintWorkletGlobalScopeProxy* PaintWorkletGlobalScopeProxy::From(
WorkletGlobalScopeProxy* proxy) {
return static_cast<PaintWorkletGlobalScopeProxy*>(proxy);
}
PaintWorkletGlobalScopeProxy::PaintWorkletGlobalScopeProxy(LocalFrame* frame) {
DCHECK(IsMainThread());
Document* document = frame->GetDocument();
global_scope_ = PaintWorkletGlobalScope::Create(
frame, document->Url(), document->UserAgent(),
document->GetSecurityOrigin(), ToIsolate(document));
}
void PaintWorkletGlobalScopeProxy::FetchAndInvokeScript(
const KURL& module_url_record,
WorkletPendingTasks* pending_tasks) {
DCHECK(IsMainThread());
global_scope_->FetchAndInvokeScript(module_url_record, pending_tasks);
}
void PaintWorkletGlobalScopeProxy::EvaluateScript(
const ScriptSourceCode& script_source_code) {
// This should be called only for threaded worklets that still use classic
// script loading.
NOTREACHED();
}
void PaintWorkletGlobalScopeProxy::TerminateWorkletGlobalScope() {
DCHECK(IsMainThread());
global_scope_->Terminate();
// Nullify the global scope to cut a potential reference cycle.
global_scope_ = nullptr;
}
CSSPaintDefinition* PaintWorkletGlobalScopeProxy::FindDefinition(
const String& name) {
DCHECK(IsMainThread());
return global_scope_->FindDefinition(name);
}
void PaintWorkletGlobalScopeProxy::AddPendingGenerator(
const String& name,
CSSPaintImageGeneratorImpl* generator) {
DCHECK(IsMainThread());
global_scope_->AddPendingGenerator(name, generator);
}
} // namespace blink
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef PaintWorkletGlobalScopeProxy_h
#define PaintWorkletGlobalScopeProxy_h
#include "core/workers/WorkletGlobalScopeProxy.h"
#include "modules/ModulesExport.h"
#include "modules/csspaint/PaintWorkletGlobalScope.h"
namespace blink {
class CSSPaintDefinition;
class LocalFrame;
// A proxy for PaintWorklet to talk to PaintWorkletGlobalScope.
class MODULES_EXPORT PaintWorkletGlobalScopeProxy
: public WorkletGlobalScopeProxy {
public:
static PaintWorkletGlobalScopeProxy* From(WorkletGlobalScopeProxy*);
explicit PaintWorkletGlobalScopeProxy(LocalFrame*);
virtual ~PaintWorkletGlobalScopeProxy() = default;
// Implements WorkletGlobalScopeProxy.
void FetchAndInvokeScript(const KURL& module_url_record,
WorkletPendingTasks*) override;
void EvaluateScript(const ScriptSourceCode&) override;
void TerminateWorkletGlobalScope() override;
CSSPaintDefinition* FindDefinition(const String& name);
void AddPendingGenerator(const String& name, CSSPaintImageGeneratorImpl*);
PaintWorkletGlobalScope* global_scope() const { return global_scope_.Get(); }
private:
Persistent<PaintWorkletGlobalScope> global_scope_;
};
} // namespace blink
#endif // PaintWorkletGlobalScopeProxy_h
......@@ -31,8 +31,9 @@ class PaintWorkletTest : public testing::Test {
};
TEST_F(PaintWorkletTest, GarbageCollectionOfCSSPaintDefinition) {
PaintWorkletGlobalScope* global_scope =
GetPaintWorklet()->GetWorkletGlobalScopeProxy();
PaintWorkletGlobalScopeProxy* proxy = PaintWorkletGlobalScopeProxy::From(
GetPaintWorklet()->GetWorkletGlobalScopeProxy());
PaintWorkletGlobalScope* global_scope = proxy->global_scope();
global_scope->ScriptController()->Evaluate(
ScriptSourceCode("registerPaint('foo', class { paint() { } });"));
......
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