Commit 60a85e75 authored by Makoto Shimazu's avatar Makoto Shimazu Committed by Commit Bot

ServiceWorker: Move OnScriptLoaded to the right place

This is 6th patch of script streaming project split off from
https://chromium-review.googlesource.com/c/538477.

This patch:
 - Moves where OnScriptLoaded() is called to after calling
   InstalledScriptsManager::GetScriptData when script streaming is enabled.
 - Changes to use StringBuilder in ServiceWorker in
   ServiceWorkerInstalledScriptsManager.

The script streaming TRACE_EVENT is broken by this patch. I'll fix it in the
following patch (https://crrev.com/c/566783).

Bug: 683037
Change-Id: Ice19b90bf32d007c8a25993b207f6eb5088047a7
Reviewed-on: https://chromium-review.googlesource.com/566781Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Reviewed-by: default avatarHiroki Nakagawa <nhiroki@chromium.org>
Reviewed-by: default avatarMatt Falkenhagen <falken@chromium.org>
Commit-Queue: Makoto Shimazu <shimazu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#487404}
parent 0761944b
......@@ -689,9 +689,6 @@ void ServiceWorkerContextClient::WorkerContextFailedToStart() {
}
void ServiceWorkerContextClient::WorkerScriptLoaded() {
DCHECK(main_thread_task_runner_->RunsTasksInCurrentSequence());
DCHECK(!proxy_);
(*instance_host_)->OnScriptLoaded();
}
......
......@@ -128,9 +128,8 @@ class ServiceWorkerContextClient : public blink::WebServiceWorkerContextClient,
// Called on the main thread.
void WorkerContextFailedToStart() override;
void WorkerScriptLoaded() override;
bool HasAssociatedRegistration() override;
void WorkerScriptLoaded() override;
void WorkerContextStarted(
blink::WebServiceWorkerContextProxy* proxy) override;
void DidEvaluateWorkerScript(bool success) override;
......
......@@ -59,26 +59,32 @@ class CORE_EXPORT WorkerReportingProxy {
SourceLocation*) {}
virtual void PostMessageToPageInspector(int session_id, const String&) {}
// Invoked when the new WorkerGlobalScope is created. This is called after
// didLoadWorkerScript().
// Invoked when the new WorkerGlobalScope is created on
// WorkerThread::InitializeOnWorkerThread.
virtual void DidCreateWorkerGlobalScope(WorkerOrWorkletGlobalScope*) {}
// Invoked when the WorkerGlobalScope is initialized. This is called after
// didCreateWorkerGlobalScope().
// Invoked when the WorkerGlobalScope is initialized on
// WorkerThread::InitializeOnWorkerThread.
virtual void DidInitializeWorkerContext() {}
// Invoked when the worker script is about to be evaluated. This is called
// after didInitializeWorkerContext().
// Invoked when the worker's main script is loaded on
// WorkerThread::InitializeOnWorkerThread. Only invoked when the script was
// loaded on the worker thread, i.e., via InstalledScriptsManager rather than
// via ResourceLoader.
virtual void DidLoadInstalledScript() {}
// Invoked when the worker script is about to be evaluated on
// WorkerThread::InitializeOnWorkerThread.
virtual void WillEvaluateWorkerScript(size_t script_size,
size_t cached_metadata_size) {}
// Invoked when an imported script is about to be evaluated. This is called
// after willEvaluateWorkerScript().
// Invoked when an imported script is about to be evaluated.
virtual void WillEvaluateImportedScript(size_t script_size,
size_t cached_metadata_size) {}
// Invoked when the worker script is evaluated. |success| is true if the
// evaluation completed with no uncaught exception.
// Invoked when the worker script is evaluated on
// WorkerThread::InitializeOnWorkerThread. |success| is true if the evaluation
// completed with no uncaught exception.
virtual void DidEvaluateWorkerScript(bool success) {}
// Invoked when close() is invoked on the worker context.
......
......@@ -453,12 +453,15 @@ void WorkerThread::InitializeOnWorkerThread(
// OriginTrialTokens to |startup_data|.
// TODO(shimazu): Add a post task to the main thread for setting
// ContentSecurityPolicy and ReferrerPolicy.
// GetScriptData blocks until the script is received from the browser.
auto script_data = GetInstalledScriptsManager()->GetScriptData(script_url);
DCHECK(script_data);
DCHECK(source_code.IsEmpty());
DCHECK(!cached_meta_data);
source_code = std::move(script_data->source_text);
cached_meta_data = std::move(script_data->meta_data);
worker_reporting_proxy_.DidLoadInstalledScript();
} else {
source_code = std::move(given_source_code);
cached_meta_data = std::move(given_cached_meta_data);
......
......@@ -343,16 +343,10 @@ void WebEmbeddedWorkerImpl::DidFinishDocumentLoad() {
// Kickstart the worker before loading the script when the script has been
// installed.
if (RuntimeEnabledFeatures::ServiceWorkerScriptStreamingEnabled() &&
installed_scripts_manager_ &&
installed_scripts_manager_->IsScriptInstalled(
worker_start_data_.script_url)) {
// TODO(shimazu): Move WorkerScriptLoaded to the correct place which is
// after InstalledScriptsManager::GetScriptData() called at
// WorkerThread::InitializeOnWorkerThread().
worker_context_client_->WorkerScriptLoaded();
if (pause_after_download_state_ == kDoPauseAfterDownload) {
pause_after_download_state_ = kIsPausedAfterDownload;
return;
}
DCHECK_EQ(pause_after_download_state_, kDontPauseAfterDownload);
StartWorkerThread();
return;
}
......
......@@ -545,6 +545,10 @@ void ServiceWorkerGlobalScopeProxy::DidInitializeWorkerContext() {
WorkerGlobalScope()->ScriptController()->GetContext());
}
void ServiceWorkerGlobalScopeProxy::DidLoadInstalledScript() {
Client().WorkerScriptLoaded();
}
void ServiceWorkerGlobalScopeProxy::WillEvaluateWorkerScript(
size_t script_size,
size_t cached_metadata_size) {
......
......@@ -150,6 +150,7 @@ class ServiceWorkerGlobalScopeProxy final
void PostMessageToPageInspector(int session_id, const String&) override;
void DidCreateWorkerGlobalScope(WorkerOrWorkletGlobalScope*) override;
void DidInitializeWorkerContext() override;
void DidLoadInstalledScript() override;
void WillEvaluateWorkerScript(size_t script_size,
size_t cached_metadata_size) override;
void WillEvaluateImportedScript(size_t script_size,
......
......@@ -6,12 +6,15 @@
#include "core/html/parser/TextResourceDecoder.h"
#include "modules/serviceworkers/ServiceWorkerThread.h"
#include "platform/wtf/text/StringBuilder.h"
namespace blink {
ServiceWorkerInstalledScriptsManager::ServiceWorkerInstalledScriptsManager(
std::unique_ptr<WebServiceWorkerInstalledScriptsManager> manager)
: manager_(std::move(manager)) {}
: manager_(std::move(manager)) {
DCHECK(manager_);
}
bool ServiceWorkerInstalledScriptsManager::IsScriptInstalled(
const KURL& script_url) const {
......@@ -24,7 +27,6 @@ ServiceWorkerInstalledScriptsManager::GetScriptData(const KURL& script_url) {
// This blocks until the script is received from the browser.
std::unique_ptr<WebServiceWorkerInstalledScriptsManager::RawScriptData>
raw_script_data = manager_->GetRawScriptData(script_url);
if (!raw_script_data)
return WTF::nullopt;
......@@ -42,9 +44,11 @@ ServiceWorkerInstalledScriptsManager::GetScriptData(const KURL& script_url) {
InstalledScriptsManager::ScriptData script_data;
// TODO(shimazu): Read the headers for ContentSecurityPolicy, ReferrerPolicy,
// and OriginTrialTokens and set them to |script_data|.
for (const auto& chunk : raw_script_data->ScriptTextChunks()) {
script_data.source_text.append(decoder->Decode(chunk.Data(), chunk.size()));
}
StringBuilder source_text_builder;
for (const auto& chunk : raw_script_data->ScriptTextChunks())
source_text_builder.Append(decoder->Decode(chunk.Data(), chunk.size()));
script_data.source_text = source_text_builder.ToString();
if (raw_script_data->MetaDataChunks().size() > 0) {
size_t total_metadata_size = 0;
for (const auto& chunk : raw_script_data->MetaDataChunks())
......@@ -54,6 +58,7 @@ ServiceWorkerInstalledScriptsManager::GetScriptData(const KURL& script_url) {
for (const auto& chunk : raw_script_data->MetaDataChunks())
script_data.meta_data->Append(chunk.Data(), chunk.size());
}
script_data.headers.Adopt(raw_script_data->TakeHeaders());
return Optional<ScriptData>(std::move(script_data));
}
......
......@@ -73,7 +73,9 @@ class WebServiceWorkerContextClient {
virtual void WorkerReadyForInspection() {}
// The worker script is successfully loaded and a new thread is about to
// be started. Called on the main thread.
// be started. Called on the main thread when the script is served from
// ResourceLoader or on the worker thread when the script is served via
// WebServiceWorkerInstalledScriptsManager.
virtual void WorkerScriptLoaded() {}
virtual bool HasAssociatedRegistration() { return false; }
......
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