Commit 715a1c5f authored by Hiroki Nakagawa's avatar Hiroki Nakagawa Committed by Commit Bot

Worker: Implement WorkerGlobalScope::ImportModuleScript()

This CL implements WorkerGlobalScope::ImportModuleScript() that imports module
scripts using WorkerModulatorImpl, and wires up DedicatedWorkerMessagingProxy
via WorkerThread.

Note that module workers are still not available because WorkerOptions is not
exposed on the ctor of Worker and WorkerModulatorImpl is not fully implemented
yet.

Design doc: https://docs.google.com/a/chromium.org/document/d/1IMGWAK7Wq37mLehwkbysNRBBnhQBo3z2MbYyMkViEnY/edit?usp=sharing

Bug: 680046
Change-Id: I0c616b7a1b79abb02c1085be356293510dd4c04b
Reviewed-on: https://chromium-review.googlesource.com/807675Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Reviewed-by: default avatarKouhei Ueno <kouhei@chromium.org>
Commit-Queue: Hiroki Nakagawa <nhiroki@chromium.org>
Cr-Commit-Position: refs/heads/master@{#525270}
parent 53488b57
...@@ -63,6 +63,8 @@ blink_core_sources("workers") { ...@@ -63,6 +63,8 @@ blink_core_sources("workers") {
"WorkerInspectorProxy.cpp", "WorkerInspectorProxy.cpp",
"WorkerInspectorProxy.h", "WorkerInspectorProxy.h",
"WorkerLocation.h", "WorkerLocation.h",
"WorkerModuleTreeClient.cpp",
"WorkerModuleTreeClient.h",
"WorkerNavigator.cpp", "WorkerNavigator.cpp",
"WorkerNavigator.h", "WorkerNavigator.h",
"WorkerOrWorkletGlobalScope.cpp", "WorkerOrWorkletGlobalScope.cpp",
......
...@@ -24,6 +24,24 @@ ...@@ -24,6 +24,24 @@
namespace blink { namespace blink {
namespace {
// TODO(nhiroki): Merge this into ParseCredentialsOption() in Worklet.cpp, or
// move this into core/fetch (see https://crbug.com/794837).
network::mojom::FetchCredentialsMode ParseCredentialsOption(
const String& credentials_option) {
if (credentials_option == "omit")
return network::mojom::FetchCredentialsMode::kOmit;
if (credentials_option == "same-origin")
return network::mojom::FetchCredentialsMode::kSameOrigin;
if (credentials_option == "include")
return network::mojom::FetchCredentialsMode::kInclude;
NOTREACHED();
return network::mojom::FetchCredentialsMode::kOmit;
}
} // namespace
struct DedicatedWorkerMessagingProxy::QueuedTask { struct DedicatedWorkerMessagingProxy::QueuedTask {
scoped_refptr<SerializedScriptValue> message; scoped_refptr<SerializedScriptValue> message;
Vector<MessagePortChannel> channels; Vector<MessagePortChannel> channels;
...@@ -62,10 +80,15 @@ void DedicatedWorkerMessagingProxy::StartWorkerGlobalScope( ...@@ -62,10 +80,15 @@ void DedicatedWorkerMessagingProxy::StartWorkerGlobalScope(
std::move(creation_params), std::move(creation_params),
CreateBackingThreadStartupData(ToIsolate(GetExecutionContext()))); CreateBackingThreadStartupData(ToIsolate(GetExecutionContext())));
// TODO(nhiroki): Support module scripts (https://crbug.com/680046). if (options.type() == "classic") {
DCHECK_EQ("classic", options.type()); GetWorkerThread()->EvaluateClassicScript(
GetWorkerThread()->EvaluateClassicScript( script_url, source_code, nullptr /* cached_meta_data */, stack_id);
script_url, source_code, nullptr /* cached_meta_data */, stack_id); } else if (options.type() == "module") {
GetWorkerThread()->ImportModuleScript(
script_url, ParseCredentialsOption(options.credentials()));
} else {
NOTREACHED();
}
// Post all queued tasks to the worker. // Post all queued tasks to the worker.
for (auto& queued_task : queued_early_tasks_) { for (auto& queued_task : queued_early_tasks_) {
......
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
#include "core/css/OffscreenFontSelector.h" #include "core/css/OffscreenFontSelector.h"
#include "core/dom/ContextLifecycleNotifier.h" #include "core/dom/ContextLifecycleNotifier.h"
#include "core/dom/ExceptionCode.h" #include "core/dom/ExceptionCode.h"
#include "core/dom/Modulator.h"
#include "core/dom/PausableObject.h" #include "core/dom/PausableObject.h"
#include "core/dom/events/Event.h" #include "core/dom/events/Event.h"
#include "core/events/ErrorEvent.h" #include "core/events/ErrorEvent.h"
...@@ -50,6 +51,7 @@ ...@@ -50,6 +51,7 @@
#include "core/workers/GlobalScopeCreationParams.h" #include "core/workers/GlobalScopeCreationParams.h"
#include "core/workers/InstalledScriptsManager.h" #include "core/workers/InstalledScriptsManager.h"
#include "core/workers/WorkerLocation.h" #include "core/workers/WorkerLocation.h"
#include "core/workers/WorkerModuleTreeClient.h"
#include "core/workers/WorkerNavigator.h" #include "core/workers/WorkerNavigator.h"
#include "core/workers/WorkerReportingProxy.h" #include "core/workers/WorkerReportingProxy.h"
#include "core/workers/WorkerScriptLoader.h" #include "core/workers/WorkerScriptLoader.h"
...@@ -311,6 +313,14 @@ ExecutionContext* WorkerGlobalScope::GetExecutionContext() const { ...@@ -311,6 +313,14 @@ ExecutionContext* WorkerGlobalScope::GetExecutionContext() const {
return const_cast<WorkerGlobalScope*>(this); return const_cast<WorkerGlobalScope*>(this);
} }
void WorkerGlobalScope::ImportModuleScript(
const KURL& module_url_record,
network::mojom::FetchCredentialsMode credentials_mode) {
Modulator* modulator = Modulator::From(ScriptController()->GetScriptState());
FetchModuleScript(module_url_record, credentials_mode,
new WorkerModuleTreeClient(modulator));
}
WorkerGlobalScope::WorkerGlobalScope( WorkerGlobalScope::WorkerGlobalScope(
std::unique_ptr<GlobalScopeCreationParams> creation_params, std::unique_ptr<GlobalScopeCreationParams> creation_params,
WorkerThread* thread, WorkerThread* thread,
......
...@@ -40,6 +40,7 @@ ...@@ -40,6 +40,7 @@
#include "platform/heap/Handle.h" #include "platform/heap/Handle.h"
#include "platform/loader/fetch/CachedMetadataHandler.h" #include "platform/loader/fetch/CachedMetadataHandler.h"
#include "platform/wtf/ListHashSet.h" #include "platform/wtf/ListHashSet.h"
#include "services/network/public/interfaces/fetch_api.mojom-shared.h"
#include "services/service_manager/public/cpp/interface_provider.h" #include "services/service_manager/public/cpp/interface_provider.h"
#include "services/service_manager/public/interfaces/interface_provider.mojom-blink.h" #include "services/service_manager/public/interfaces/interface_provider.mojom-blink.h"
...@@ -126,6 +127,9 @@ class CORE_EXPORT WorkerGlobalScope ...@@ -126,6 +127,9 @@ class CORE_EXPORT WorkerGlobalScope
// EventTarget // EventTarget
ExecutionContext* GetExecutionContext() const final; ExecutionContext* GetExecutionContext() const final;
void ImportModuleScript(const KURL& module_url_record,
network::mojom::FetchCredentialsMode);
double TimeOrigin() const { return time_origin_; } double TimeOrigin() const { return time_origin_; }
WorkerSettings* GetWorkerSettings() const { return worker_settings_.get(); } WorkerSettings* GetWorkerSettings() const { return worker_settings_.get(); }
......
// 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 "core/workers/WorkerModuleTreeClient.h"
#include "core/dom/ExecutionContext.h"
#include "core/dom/ModuleScript.h"
#include "core/workers/WorkerGlobalScope.h"
#include "core/workers/WorkerReportingProxy.h"
namespace blink {
WorkerModuleTreeClient::WorkerModuleTreeClient(Modulator* modulator)
: modulator_(modulator) {}
// A partial implementation of the "Processing model" algorithm in the HTML
// WebWorker spec:
// https://html.spec.whatwg.org/multipage/workers.html#worker-processing-model
void WorkerModuleTreeClient::NotifyModuleTreeLoadFinished(
ModuleScript* module_script) {
if (!module_script) {
// Step 11: ... "If the algorithm asynchronously completes with null, queue
// a task to fire an event named error at worker, and abort these steps."
// ...
// TODO(nhiroki): Throw an ErrorEvent at the Worker object on the owner
// Document.
return;
}
// Step 11: ... "Otherwise, continue the rest of these steps after the
// algorithm's asynchronous completion, with script being the asynchronous
// completion value." ...
// TODO(nhiroki): Call WorkerReportingProxy::WillEvaluateWorkerScript() or
// something like that (e.g., WillEvaluateModuleScript()).
modulator_->ExecuteModule(module_script,
Modulator::CaptureEvalErrorFlag::kReport);
WorkerGlobalScope* global_scope =
ToWorkerGlobalScope(ExecutionContext::From(modulator_->GetScriptState()));
global_scope->ReportingProxy().DidEvaluateModuleScript(
!module_script->IsErrored());
}
void WorkerModuleTreeClient::Trace(blink::Visitor* visitor) {
visitor->Trace(modulator_);
ModuleTreeClient::Trace(visitor);
}
} // 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 WorkerModuleTreeClient_h
#define WorkerModuleTreeClient_h
#include "core/dom/Modulator.h"
#include "platform/heap/GarbageCollected.h"
namespace blink {
class ModuleScript;
// A ModuleTreeClient that lives on the worker context's thread.
class WorkerModuleTreeClient final : public ModuleTreeClient {
public:
explicit WorkerModuleTreeClient(Modulator*);
// Implements ModuleTreeClient.
void NotifyModuleTreeLoadFinished(ModuleScript*) final;
void Trace(blink::Visitor*) override;
private:
Member<Modulator> modulator_;
};
} // namespace blink
#endif // WorkerModuleTreeClient_h
...@@ -127,6 +127,7 @@ void WorkerThread::EvaluateClassicScript( ...@@ -127,6 +127,7 @@ void WorkerThread::EvaluateClassicScript(
const String& source_code, const String& source_code,
std::unique_ptr<Vector<char>> cached_meta_data, std::unique_ptr<Vector<char>> cached_meta_data,
const v8_inspector::V8StackTraceId& stack_id) { const v8_inspector::V8StackTraceId& stack_id) {
DCHECK(IsMainThread());
GetTaskRunner(TaskType::kUnthrottled) GetTaskRunner(TaskType::kUnthrottled)
->PostTask( ->PostTask(
FROM_HERE, FROM_HERE,
...@@ -135,6 +136,17 @@ void WorkerThread::EvaluateClassicScript( ...@@ -135,6 +136,17 @@ void WorkerThread::EvaluateClassicScript(
WTF::Passed(std::move(cached_meta_data)), stack_id)); WTF::Passed(std::move(cached_meta_data)), stack_id));
} }
void WorkerThread::ImportModuleScript(
const KURL& script_url,
network::mojom::FetchCredentialsMode credentials_mode) {
DCHECK(IsMainThread());
GetTaskRunner(TaskType::kUnthrottled)
->PostTask(FROM_HERE, CrossThreadBind(
&WorkerThread::ImportModuleScriptOnWorkerThread,
CrossThreadUnretained(this), script_url,
credentials_mode));
}
void WorkerThread::Terminate() { void WorkerThread::Terminate() {
DCHECK(IsMainThread()); DCHECK(IsMainThread());
...@@ -454,6 +466,16 @@ void WorkerThread::EvaluateClassicScriptOnWorkerThread( ...@@ -454,6 +466,16 @@ void WorkerThread::EvaluateClassicScriptOnWorkerThread(
debugger->ExternalAsyncTaskFinished(stack_id); debugger->ExternalAsyncTaskFinished(stack_id);
} }
void WorkerThread::ImportModuleScriptOnWorkerThread(
const KURL& script_url,
network::mojom::FetchCredentialsMode credentials_mode) {
// Worklets have a different code path to import module scripts.
// TODO(nhiroki): Consider excluding this code path from WorkerThread like
// Worklets.
ToWorkerGlobalScope(GlobalScope())
->ImportModuleScript(script_url, credentials_mode);
}
void WorkerThread::PrepareForShutdownOnWorkerThread() { void WorkerThread::PrepareForShutdownOnWorkerThread() {
DCHECK(IsCurrentThread()); DCHECK(IsCurrentThread());
{ {
......
...@@ -45,6 +45,7 @@ ...@@ -45,6 +45,7 @@
#include "platform/wtf/Functional.h" #include "platform/wtf/Functional.h"
#include "platform/wtf/Optional.h" #include "platform/wtf/Optional.h"
#include "public/platform/WebThread.h" #include "public/platform/WebThread.h"
#include "services/network/public/interfaces/fetch_api.mojom-shared.h"
#include "v8/include/v8.h" #include "v8/include/v8.h"
namespace blink { namespace blink {
...@@ -105,8 +106,10 @@ class CORE_EXPORT WorkerThread : public WebThread::TaskObserver { ...@@ -105,8 +106,10 @@ class CORE_EXPORT WorkerThread : public WebThread::TaskObserver {
std::unique_ptr<Vector<char>> cached_meta_data, std::unique_ptr<Vector<char>> cached_meta_data,
const v8_inspector::V8StackTraceId& stack_id); const v8_inspector::V8StackTraceId& stack_id);
// TODO(nhiroki): Implement ImportModuleScript() for module workers. // Posts a task to import a top-level module script on the worker thread.
// (https://crbug.com/680046) // Called on the main thread after start().
void ImportModuleScript(const KURL& script_url,
network::mojom::FetchCredentialsMode);
// Closes the global scope and terminates the underlying thread. Called on the // Closes the global scope and terminates the underlying thread. Called on the
// main thread. // main thread.
...@@ -253,6 +256,8 @@ class CORE_EXPORT WorkerThread : public WebThread::TaskObserver { ...@@ -253,6 +256,8 @@ class CORE_EXPORT WorkerThread : public WebThread::TaskObserver {
String source_code, String source_code,
std::unique_ptr<Vector<char>> cached_meta_data, std::unique_ptr<Vector<char>> cached_meta_data,
const v8_inspector::V8StackTraceId& stack_id); const v8_inspector::V8StackTraceId& stack_id);
void ImportModuleScriptOnWorkerThread(const KURL& script_url,
network::mojom::FetchCredentialsMode);
// These are called in this order during worker thread termination. // These are called in this order during worker thread termination.
void PrepareForShutdownOnWorkerThread(); void PrepareForShutdownOnWorkerThread();
......
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