Commit f631bd72 authored by Hiroki Nakagawa's avatar Hiroki Nakagawa Committed by Commit Bot

ServiceWorker: Move InstalledScriptsManager from WorkerThread to WorkerGlobalScope

WorkerGlobalScope, not WorkerThread, is responsible for script fetch and
evaluation. Therefore it would be clearer to place InstalledScriptsManager in
WorkerGlobalScope, not in WorkerThread.

Bug: 938219
Change-Id: Iee76b943ef435326ea055e65fb75e579dd7d75ef
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1824522
Commit-Queue: Hiroki Nakagawa <nhiroki@chromium.org>
Reviewed-by: default avatarMakoto Shimazu <shimazu@chromium.org>
Reviewed-by: default avatarMatt Falkenhagen <falken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#700063}
parent 6456b845
......@@ -15,7 +15,9 @@ namespace blink {
InstalledServiceWorkerModuleScriptFetcher::
InstalledServiceWorkerModuleScriptFetcher(WorkerGlobalScope* global_scope)
: global_scope_(global_scope) {}
: global_scope_(global_scope) {
DCHECK(global_scope_->IsServiceWorkerGlobalScope());
}
void InstalledServiceWorkerModuleScriptFetcher::Fetch(
FetchParameters& fetch_params,
......@@ -24,8 +26,7 @@ void InstalledServiceWorkerModuleScriptFetcher::Fetch(
ModuleGraphLevel level,
ModuleScriptFetcher::Client* client) {
DCHECK(global_scope_->IsContextThread());
InstalledScriptsManager* installed_scripts_manager =
global_scope_->GetThread()->GetInstalledScriptsManager();
auto* installed_scripts_manager = global_scope_->GetInstalledScriptsManager();
DCHECK(installed_scripts_manager);
DCHECK(installed_scripts_manager->IsScriptInstalled(fetch_params.Url()));
......
......@@ -282,27 +282,6 @@ bool WorkerGlobalScope::FetchClassicImportedScript(
KURL* out_response_url,
String* out_source_code,
std::unique_ptr<Vector<uint8_t>>* out_cached_meta_data) {
// InstalledScriptsManager is now used only for starting installed service
// workers.
// TODO(nhiroki): Consider moving this into ServiceWorkerGlobalScope.
InstalledScriptsManager* installed_scripts_manager =
GetThread()->GetInstalledScriptsManager();
if (installed_scripts_manager &&
installed_scripts_manager->IsScriptInstalled(script_url)) {
DCHECK(IsServiceWorkerGlobalScope());
std::unique_ptr<InstalledScriptsManager::ScriptData> script_data =
installed_scripts_manager->GetScriptData(script_url);
if (!script_data)
return false;
*out_response_url = script_url;
*out_source_code = script_data->TakeSourceText();
*out_cached_meta_data = script_data->TakeMetaData();
// TODO(shimazu): Add appropriate probes for inspector.
return true;
}
// If the script wasn't provided by the InstalledScriptsManager, load from
// ResourceLoader.
ExecutionContext* execution_context = GetExecutionContext();
WorkerClassicScriptLoader* classic_script_loader =
MakeGarbageCollected<WorkerClassicScriptLoader>();
......
......@@ -59,6 +59,7 @@ class ConsoleMessage;
class ExceptionState;
class FetchClientSettingsObjectSnapshot;
class FontFaceSet;
class InstalledScriptsManager;
class OffscreenFontSelector;
class WorkerResourceTimingNotifier;
class StringOrTrustedScriptURL;
......@@ -190,6 +191,10 @@ class CORE_EXPORT WorkerGlobalScope
void Trace(blink::Visitor*) override;
virtual InstalledScriptsManager* GetInstalledScriptsManager() {
return nullptr;
}
// TODO(fserb): This can be removed once we WorkerGlobalScope implements
// FontFaceSource on the IDL.
FontFaceSet* fonts();
......@@ -211,6 +216,12 @@ class CORE_EXPORT WorkerGlobalScope
void ExceptionThrown(ErrorEvent*) override;
void RemoveURLFromMemoryCache(const KURL&) final;
virtual bool FetchClassicImportedScript(
const KURL& script_url,
KURL* out_response_url,
String* out_source_code,
std::unique_ptr<Vector<uint8_t>>* out_cached_meta_data);
// Notifies that the top-level worker script is ready to evaluate.
// Worker top-level script is evaluated after it is fetched and
// ReadyToRunWorkerScript() is called.
......@@ -228,12 +239,6 @@ class CORE_EXPORT WorkerGlobalScope
// Used for importScripts().
void ImportScriptsInternal(const Vector<String>& urls, ExceptionState&);
bool FetchClassicImportedScript(
const KURL& script_url,
KURL* out_response_url,
String* out_source_code,
std::unique_ptr<Vector<uint8_t>>* out_cached_meta_data);
// ExecutionContext
EventTarget* ErrorEventTarget() final { return this; }
......
......@@ -57,7 +57,6 @@ namespace blink {
class ConsoleMessageStorage;
class InspectorTaskRunner;
class InstalledScriptsManager;
class WorkerBackingThread;
class WorkerInspectorController;
class WorkerOrWorkletGlobalScope;
......@@ -214,11 +213,6 @@ class CORE_EXPORT WorkerThread : public Thread::TaskObserver {
return parent_thread_default_task_runner_;
}
// For ServiceWorkerScriptStreaming. Returns nullptr otherwise.
virtual InstalledScriptsManager* GetInstalledScriptsManager() {
return nullptr;
}
scheduler::WorkerScheduler* GetScheduler();
// Returns a task runner bound to the per-global-scope scheduler's task queue.
......
......@@ -61,7 +61,6 @@
#include "third_party/blink/renderer/core/probe/core_probes.h"
#include "third_party/blink/renderer/core/trustedtypes/trusted_script_url.h"
#include "third_party/blink/renderer/core/workers/global_scope_creation_params.h"
#include "third_party/blink/renderer/core/workers/installed_scripts_manager.h"
#include "third_party/blink/renderer/core/workers/worker_classic_script_loader.h"
#include "third_party/blink/renderer/core/workers/worker_clients.h"
#include "third_party/blink/renderer/core/workers/worker_reporting_proxy.h"
......@@ -181,15 +180,17 @@ static std::string MojoEnumToString(T mojo_enum) {
ServiceWorkerGlobalScope* ServiceWorkerGlobalScope::Create(
ServiceWorkerThread* thread,
std::unique_ptr<GlobalScopeCreationParams> creation_params,
std::unique_ptr<ServiceWorkerInstalledScriptsManager>
installed_scripts_manager,
mojo::PendingRemote<mojom::blink::CacheStorage> cache_storage_remote,
base::TimeTicks time_origin) {
DCHECK_EQ(creation_params->off_main_thread_fetch_option,
OffMainThreadWorkerScriptFetchOption::kEnabled);
#if DCHECK_IS_ON()
// If the script is being loaded via script streaming, the script is not yet
// loaded.
if (thread->GetInstalledScriptsManager() &&
thread->GetInstalledScriptsManager()->IsScriptInstalled(
if (installed_scripts_manager && installed_scripts_manager->IsScriptInstalled(
creation_params->script_url)) {
// CSP headers, referrer policy, and origin trial tokens will be provided by
// the InstalledScriptsManager in EvaluateClassicScript().
......@@ -198,18 +199,22 @@ ServiceWorkerGlobalScope* ServiceWorkerGlobalScope::Create(
creation_params->referrer_policy);
DCHECK(creation_params->origin_trial_tokens->IsEmpty());
}
#endif // DCHECK_IS_ON()
return MakeGarbageCollected<ServiceWorkerGlobalScope>(
std::move(creation_params), thread, std::move(cache_storage_remote),
time_origin);
std::move(creation_params), thread, std::move(installed_scripts_manager),
std::move(cache_storage_remote), time_origin);
}
ServiceWorkerGlobalScope::ServiceWorkerGlobalScope(
std::unique_ptr<GlobalScopeCreationParams> creation_params,
ServiceWorkerThread* thread,
std::unique_ptr<ServiceWorkerInstalledScriptsManager>
installed_scripts_manager,
mojo::PendingRemote<mojom::blink::CacheStorage> cache_storage_remote,
base::TimeTicks time_origin)
: WorkerGlobalScope(std::move(creation_params), thread, time_origin),
installed_scripts_manager_(std::move(installed_scripts_manager)),
cache_storage_remote_(std::move(cache_storage_remote)) {
// Create the idle timer. At this point the timer is not started. It will be
// started by DidEvaluateScript().
......@@ -292,14 +297,12 @@ void ServiceWorkerGlobalScope::RunInstalledClassicScript(
const v8_inspector::V8StackTraceId& stack_id) {
DCHECK(IsContextThread());
InstalledScriptsManager* installed_scripts_manager =
GetThread()->GetInstalledScriptsManager();
DCHECK(installed_scripts_manager);
DCHECK(installed_scripts_manager->IsScriptInstalled(script_url));
DCHECK(installed_scripts_manager_);
DCHECK(installed_scripts_manager_->IsScriptInstalled(script_url));
// GetScriptData blocks until the script is received from the browser.
std::unique_ptr<InstalledScriptsManager::ScriptData> script_data =
installed_scripts_manager->GetScriptData(script_url);
installed_scripts_manager_->GetScriptData(script_url);
if (!script_data) {
ReportingProxy().DidFailToLoadClassicScript();
// This will eventually initiate worker thread termination. See
......@@ -359,6 +362,11 @@ void ServiceWorkerGlobalScope::Dispose() {
WorkerGlobalScope::Dispose();
}
InstalledScriptsManager*
ServiceWorkerGlobalScope::GetInstalledScriptsManager() {
return installed_scripts_manager_.get();
}
void ServiceWorkerGlobalScope::CountWorkerScript(size_t script_size,
size_t cached_metadata_size) {
DCHECK_EQ(GetScriptType(), mojom::ScriptType::kClassic);
......@@ -695,6 +703,33 @@ bool ServiceWorkerGlobalScope::AddEventListenerInternal(
options);
}
bool ServiceWorkerGlobalScope::FetchClassicImportedScript(
const KURL& script_url,
KURL* out_response_url,
String* out_source_code,
std::unique_ptr<Vector<uint8_t>>* out_cached_meta_data) {
// InstalledScriptsManager is used only for starting installed service
// workers.
if (installed_scripts_manager_) {
// All imported scripts must be installed. This is already checked in
// ServiceWorkerGlobalScope::importScripts().
DCHECK(installed_scripts_manager_->IsScriptInstalled(script_url));
std::unique_ptr<InstalledScriptsManager::ScriptData> script_data =
installed_scripts_manager_->GetScriptData(script_url);
if (!script_data)
return false;
*out_response_url = script_url;
*out_source_code = script_data->TakeSourceText();
*out_cached_meta_data = script_data->TakeMetaData();
// TODO(shimazu): Add appropriate probes for inspector.
return true;
}
// This is a new service worker. Proceed with importing scripts and installing
// them.
return WorkerGlobalScope::FetchClassicImportedScript(
script_url, out_response_url, out_source_code, out_cached_meta_data);
}
const AtomicString& ServiceWorkerGlobalScope::InterfaceName() const {
return event_target_names::kServiceWorkerGlobalScope;
}
......@@ -734,17 +769,15 @@ void ServiceWorkerGlobalScope::Trace(blink::Visitor* visitor) {
void ServiceWorkerGlobalScope::importScripts(
const HeapVector<StringOrTrustedScriptURL>& urls,
ExceptionState& exception_state) {
InstalledScriptsManager* installed_scripts_manager =
GetThread()->GetInstalledScriptsManager();
for (const StringOrTrustedScriptURL& stringOrUrl : urls) {
String string_url = stringOrUrl.IsString()
? stringOrUrl.GetAsString()
: stringOrUrl.GetAsTrustedScriptURL()->toString();
KURL completed_url = CompleteURL(string_url);
if (installed_scripts_manager &&
!installed_scripts_manager->IsScriptInstalled(completed_url)) {
DCHECK(installed_scripts_manager->IsScriptInstalled(Url()));
if (installed_scripts_manager_ &&
!installed_scripts_manager_->IsScriptInstalled(completed_url)) {
DCHECK(installed_scripts_manager_->IsScriptInstalled(Url()));
exception_state.ThrowDOMException(
DOMExceptionCode::kNetworkError,
"Failed to import '" + completed_url.ElidedString() +
......
......@@ -44,6 +44,7 @@
#include "third_party/blink/renderer/bindings/core/v8/request_or_usv_string.h"
#include "third_party/blink/renderer/core/workers/worker_global_scope.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/modules/service_worker/service_worker_installed_scripts_manager.h"
#include "third_party/blink/renderer/modules/service_worker/service_worker_timeout_timer.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/wtf/casting.h"
......@@ -59,6 +60,7 @@ class ScriptPromise;
class ScriptState;
class ServiceWorker;
class ServiceWorkerClients;
class ServiceWorkerInstalledScriptsManager;
class ServiceWorkerRegistration;
class ServiceWorkerThread;
class StringOrTrustedScriptURL;
......@@ -83,11 +85,14 @@ class MODULES_EXPORT ServiceWorkerGlobalScope final
static ServiceWorkerGlobalScope* Create(
ServiceWorkerThread*,
std::unique_ptr<GlobalScopeCreationParams>,
std::unique_ptr<ServiceWorkerInstalledScriptsManager>,
mojo::PendingRemote<mojom::blink::CacheStorage>,
base::TimeTicks time_origin);
ServiceWorkerGlobalScope(std::unique_ptr<GlobalScopeCreationParams>,
ServiceWorkerGlobalScope(
std::unique_ptr<GlobalScopeCreationParams>,
ServiceWorkerThread*,
std::unique_ptr<ServiceWorkerInstalledScriptsManager>,
mojo::PendingRemote<mojom::blink::CacheStorage>,
base::TimeTicks time_origin);
~ServiceWorkerGlobalScope() override;
......@@ -118,6 +123,7 @@ class MODULES_EXPORT ServiceWorkerGlobalScope final
WorkerResourceTimingNotifier& outside_resource_timing_notifier,
network::mojom::CredentialsMode) override;
void Dispose() override;
InstalledScriptsManager* GetInstalledScriptsManager() override;
// Runs the installed top-level classic worker script for the 'installed'
// service worker case.
......@@ -306,6 +312,13 @@ class MODULES_EXPORT ServiceWorkerGlobalScope final
EventListener*,
const AddEventListenerOptionsResolved*) override;
// WorkerGlobalScope
bool FetchClassicImportedScript(
const KURL& script_url,
KURL* out_response_url,
String* out_source_code,
std::unique_ptr<Vector<uint8_t>>* out_cached_meta_data) override;
private:
void importScripts(const HeapVector<StringOrTrustedScriptURL>& urls,
ExceptionState&) override;
......@@ -471,6 +484,10 @@ class MODULES_EXPORT ServiceWorkerGlobalScope final
uint64_t cache_storage_installed_script_total_size_ = 0;
uint64_t cache_storage_installed_script_metadata_total_size_ = 0;
// Non-null only when this service worker is already installed.
std::unique_ptr<ServiceWorkerInstalledScriptsManager>
installed_scripts_manager_;
// May be provided in the constructor as an optimization so InterfaceProvider
// doesn't need to be used. Taken at the initial call to
// ServiceWorkerGlobalScope#caches.
......
......@@ -68,10 +68,6 @@ void ServiceWorkerThread::ClearWorkerBackingThread() {
worker_backing_thread_ = nullptr;
}
InstalledScriptsManager* ServiceWorkerThread::GetInstalledScriptsManager() {
return installed_scripts_manager_.get();
}
void ServiceWorkerThread::TerminateForTesting() {
global_scope_proxy_->TerminateWorkerContext();
WorkerThread::TerminateForTesting();
......@@ -127,9 +123,9 @@ void ServiceWorkerThread::RunInstalledModuleScriptOnWorkerThread(
WorkerOrWorkletGlobalScope* ServiceWorkerThread::CreateWorkerGlobalScope(
std::unique_ptr<GlobalScopeCreationParams> creation_params) {
return ServiceWorkerGlobalScope::Create(this, std::move(creation_params),
std::move(cache_storage_remote_),
time_origin_);
return ServiceWorkerGlobalScope::Create(
this, std::move(creation_params), std::move(installed_scripts_manager_),
std::move(cache_storage_remote_), time_origin_);
}
} // namespace blink
......@@ -60,7 +60,6 @@ class MODULES_EXPORT ServiceWorkerThread final : public WorkerThread {
return *worker_backing_thread_;
}
void ClearWorkerBackingThread() override;
InstalledScriptsManager* GetInstalledScriptsManager() override;
void TerminateForTesting() override;
void RunInstalledClassicScript(const KURL& script_url,
......@@ -90,6 +89,8 @@ class MODULES_EXPORT ServiceWorkerThread final : public WorkerThread {
std::unique_ptr<ServiceWorkerGlobalScopeProxy> global_scope_proxy_;
std::unique_ptr<WorkerBackingThread> worker_backing_thread_;
// Ownership of these members is moved out in CreateWorkerGlobalScope().
std::unique_ptr<ServiceWorkerInstalledScriptsManager>
installed_scripts_manager_;
mojo::PendingRemote<mojom::blink::CacheStorage> cache_storage_remote_;
......
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