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