Commit 308ed559 authored by Leon Han's avatar Leon Han Committed by Commit Bot

[ServiceWorker] Make ServiceWorkerGlobalScope hold ServiceWorkerHostAssociatedPtr

This CL aims to slim down ServiceWorkerGlobalScopeClient by moving its
ServiceWorkerHostAssociatedPtr member into ServiceWorkerGlobalScope, to
facilitate removing ServiceWorkerGlobalScopeClient completely in the
future.

Previously,
CallerXXX --> ServiceWorkerGlobalScopeClient -Mojo calls-> Remote
ServiceWorkerHost impl.
Now,
CallerXXX -GetServiceWorkerHost()-> ServiceWorkerGlobalScope
          then -Mojo calls-> Remote ServiceWorkerHost impl

BUG=931142

Change-Id: I1f8250210faf4ef0f34ef81f06d1a79ba23c3a3a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1636517Reviewed-by: default avatarMatt Falkenhagen <falken@chromium.org>
Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Reviewed-by: default avatarMakoto Shimazu <shimazu@chromium.org>
Commit-Queue: Leon Han <leon.han@intel.com>
Cr-Commit-Position: refs/heads/master@{#665487}
parent cbf3a7ac
......@@ -15,7 +15,8 @@
#include "third_party/blink/renderer/modules/payments/payment_method_change_response.h"
#include "third_party/blink/renderer/modules/payments/payments_validators.h"
#include "third_party/blink/renderer/modules/service_worker/respond_with_observer.h"
#include "third_party/blink/renderer/modules/service_worker/service_worker_global_scope_client.h"
#include "third_party/blink/renderer/modules/service_worker/service_worker_global_scope.h"
#include "third_party/blink/renderer/modules/service_worker/service_worker_window_client.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"
#include "third_party/blink/renderer/platform/heap/heap.h"
#include "third_party/blink/renderer/platform/wtf/text/atomic_string.h"
......@@ -139,8 +140,12 @@ ScriptPromise PaymentRequestEvent::openWindow(ScriptState* script_state,
}
context->ConsumeWindowInteraction();
ServiceWorkerGlobalScopeClient::From(context)->OpenWindowForPaymentHandler(
parsed_url_to_open, resolver);
To<ServiceWorkerGlobalScope>(context)
->GetServiceWorkerHost()
->OpenPaymentHandlerWindow(
parsed_url_to_open,
ServiceWorkerWindowClient::CreateResolveWindowClientCallback(
resolver));
return promise;
}
......
......@@ -14,8 +14,9 @@
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/frame/use_counter.h"
#include "third_party/blink/renderer/core/messaging/blink_transferable_message.h"
#include "third_party/blink/renderer/core/messaging/message_port.h"
#include "third_party/blink/renderer/core/messaging/post_message_options.h"
#include "third_party/blink/renderer/modules/service_worker/service_worker_global_scope_client.h"
#include "third_party/blink/renderer/modules/service_worker/service_worker_global_scope.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"
......@@ -100,8 +101,9 @@ void ServiceWorkerClient::postMessage(ScriptState* script_state,
if (exception_state.HadException())
return;
ServiceWorkerGlobalScopeClient::From(context)->PostMessageToClient(
uuid_, std::move(msg));
To<ServiceWorkerGlobalScope>(context)
->GetServiceWorkerHost()
->PostMessageToClient(uuid_, std::move(msg));
}
} // namespace blink
......@@ -14,10 +14,9 @@
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/workers/worker_global_scope.h"
#include "third_party/blink/renderer/core/workers/worker_location.h"
#include "third_party/blink/renderer/modules/service_worker/service_worker_error.h"
#include "third_party/blink/renderer/modules/service_worker/service_worker_global_scope_client.h"
#include "third_party/blink/renderer/modules/service_worker/service_worker_global_scope.h"
#include "third_party/blink/renderer/modules/service_worker/service_worker_window_client.h"
#include "third_party/blink/renderer/platform/bindings/v8_throw_exception.h"
#include "third_party/blink/renderer/platform/heap/heap.h"
......@@ -110,45 +109,47 @@ ServiceWorkerClients::ServiceWorkerClients() = default;
ScriptPromise ServiceWorkerClients::get(ScriptState* script_state,
const String& id) {
ExecutionContext* execution_context = ExecutionContext::From(script_state);
ServiceWorkerGlobalScope* global_scope =
To<ServiceWorkerGlobalScope>(ExecutionContext::From(script_state));
// TODO(jungkees): May be null due to worker termination:
// http://crbug.com/413518.
if (!execution_context)
if (!global_scope)
return ScriptPromise();
auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(script_state);
ServiceWorkerGlobalScopeClient::From(execution_context)
->GetClient(id, WTF::Bind(&DidGetClient, WrapPersistent(resolver)));
global_scope->GetServiceWorkerHost()->GetClient(
id, WTF::Bind(&DidGetClient, WrapPersistent(resolver)));
return resolver->Promise();
}
ScriptPromise ServiceWorkerClients::matchAll(
ScriptState* script_state,
const ClientQueryOptions* options) {
ExecutionContext* execution_context = ExecutionContext::From(script_state);
ServiceWorkerGlobalScope* global_scope =
To<ServiceWorkerGlobalScope>(ExecutionContext::From(script_state));
// FIXME: May be null due to worker termination: http://crbug.com/413518.
if (!execution_context)
if (!global_scope)
return ScriptPromise();
auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(script_state);
ServiceWorkerGlobalScopeClient::From(execution_context)
->GetClients(
mojom::blink::ServiceWorkerClientQueryOptions::New(
options->includeUncontrolled(), GetClientType(options->type())),
WTF::Bind(&DidGetClients, WrapPersistent(resolver)));
global_scope->GetServiceWorkerHost()->GetClients(
mojom::blink::ServiceWorkerClientQueryOptions::New(
options->includeUncontrolled(), GetClientType(options->type())),
WTF::Bind(&DidGetClients, WrapPersistent(resolver)));
return resolver->Promise();
}
ScriptPromise ServiceWorkerClients::claim(ScriptState* script_state) {
ExecutionContext* execution_context = ExecutionContext::From(script_state);
ServiceWorkerGlobalScope* global_scope =
To<ServiceWorkerGlobalScope>(ExecutionContext::From(script_state));
// FIXME: May be null due to worker termination: http://crbug.com/413518.
if (!execution_context)
if (!global_scope)
return ScriptPromise();
auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(script_state);
ServiceWorkerGlobalScopeClient::From(execution_context)
->Claim(WTF::Bind(&DidClaim, WrapPersistent(resolver)));
global_scope->GetServiceWorkerHost()->ClaimClients(
WTF::Bind(&DidClaim, WrapPersistent(resolver)));
return resolver->Promise();
}
......@@ -156,33 +157,34 @@ ScriptPromise ServiceWorkerClients::openWindow(ScriptState* script_state,
const String& url) {
auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(script_state);
ScriptPromise promise = resolver->Promise();
ExecutionContext* context = ExecutionContext::From(script_state);
ServiceWorkerGlobalScope* global_scope =
To<ServiceWorkerGlobalScope>(ExecutionContext::From(script_state));
KURL parsed_url =
KURL(To<WorkerGlobalScope>(context)->location()->Url(), url);
KURL parsed_url = KURL(global_scope->location()->Url(), url);
if (!parsed_url.IsValid()) {
resolver->Reject(V8ThrowException::CreateTypeError(
script_state->GetIsolate(), "'" + url + "' is not a valid URL."));
return promise;
}
if (!context->GetSecurityOrigin()->CanDisplay(parsed_url)) {
if (!global_scope->GetSecurityOrigin()->CanDisplay(parsed_url)) {
resolver->Reject(V8ThrowException::CreateTypeError(
script_state->GetIsolate(),
"'" + parsed_url.ElidedString() + "' cannot be opened."));
return promise;
}
if (!context->IsWindowInteractionAllowed()) {
if (!global_scope->IsWindowInteractionAllowed()) {
resolver->Reject(MakeGarbageCollected<DOMException>(
DOMExceptionCode::kInvalidAccessError,
"Not allowed to open a window."));
return promise;
}
context->ConsumeWindowInteraction();
global_scope->ConsumeWindowInteraction();
ServiceWorkerGlobalScopeClient::From(context)->OpenWindowForClients(
parsed_url, resolver);
global_scope->GetServiceWorkerHost()->OpenNewTab(
parsed_url,
ServiceWorkerWindowClient::CreateResolveWindowClientCallback(resolver));
return promise;
}
......
......@@ -378,9 +378,8 @@ void ServiceWorkerGlobalScope::RunInstalledModuleScript(
void ServiceWorkerGlobalScope::Dispose() {
DCHECK(IsContextThread());
ServiceWorkerGlobalScopeClient::From(GetExecutionContext())
->WillDestroyWorkerContext();
timeout_timer_.reset();
service_worker_host_.reset();
binding_.Close();
controller_.reset();
WorkerGlobalScope::Dispose();
......@@ -608,8 +607,8 @@ ScriptPromise ServiceWorkerGlobalScope::skipWaiting(ScriptState* script_state) {
return ScriptPromise();
auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(script_state);
ServiceWorkerGlobalScopeClient::From(execution_context)
->SkipWaiting(WTF::Bind(&DidSkipWaiting, WrapPersistent(resolver)));
GetServiceWorkerHost()->SkipWaiting(
WTF::Bind(&DidSkipWaiting, WrapPersistent(resolver)));
return resolver->Promise();
}
......@@ -1249,6 +1248,12 @@ mojom::blink::CacheStoragePtrInfo ServiceWorkerGlobalScope::TakeCacheStorage() {
return std::move(cache_storage_info_);
}
mojom::blink::ServiceWorkerHost*
ServiceWorkerGlobalScope::GetServiceWorkerHost() {
DCHECK(service_worker_host_);
return service_worker_host_.get();
}
int ServiceWorkerGlobalScope::WillStartTask() {
DCHECK(IsContextThread());
DCHECK(timeout_timer_);
......@@ -1348,9 +1353,10 @@ void ServiceWorkerGlobalScope::InitializeGlobalScope(
mojom::blink::FetchHandlerExistence fetch_hander_existence) {
DCHECK(IsContextThread());
ServiceWorkerGlobalScopeClient::From(GetExecutionContext())
->BindServiceWorkerHost(std::move(service_worker_host),
GetTaskRunner(TaskType::kInternalDefault));
DCHECK(service_worker_host.is_valid());
DCHECK(!service_worker_host_);
service_worker_host_.Bind(std::move(service_worker_host),
GetTaskRunner(TaskType::kInternalDefault));
// Set ServiceWorkerGlobalScope#registration.
DCHECK_NE(registration_info->registration_id,
......
......@@ -276,6 +276,8 @@ class MODULES_EXPORT ServiceWorkerGlobalScope final
mojom::blink::CacheStoragePtrInfo TakeCacheStorage();
mojom::blink::ServiceWorkerHost* GetServiceWorkerHost();
// Called when a task is going to be scheduled on the service worker.
// The service worker shouldn't request to be terminated until the task is
// finished. Returns an id for the task. The caller must call DidEndTask()
......@@ -436,6 +438,10 @@ class MODULES_EXPORT ServiceWorkerGlobalScope final
// ServiceWorkerGlobalScope#caches.
mojom::blink::CacheStoragePtrInfo cache_storage_info_;
// Bound by the first Mojo call received on the service worker thread
// mojom::blink::ServiceWorker::InitializeGlobalScope().
mojom::blink::ServiceWorkerHostAssociatedPtr service_worker_host_;
mojo::Binding<mojom::blink::ServiceWorker> binding_;
// Maps for inflight event callbacks.
......
......@@ -32,128 +32,22 @@
#include <memory>
#include <utility>
#include "third_party/blink/public/platform/modules/service_worker/web_service_worker_error.h"
#include "third_party/blink/public/web/modules/service_worker/web_service_worker_context_client.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/fetch/response.h"
#include "third_party/blink/renderer/core/workers/worker_global_scope.h"
#include "third_party/blink/renderer/modules/service_worker/service_worker_window_client.h"
#include "third_party/blink/renderer/platform/bindings/v8_throw_exception.h"
#include "third_party/blink/renderer/platform/heap/persistent.h"
#include "third_party/blink/renderer/platform/wtf/std_lib_extras.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"
namespace blink {
namespace {
void DidNavigateOrOpenWindow(ScriptPromiseResolver* resolver,
bool success,
mojom::blink::ServiceWorkerClientInfoPtr info,
const String& error_msg) {
if (!resolver->GetExecutionContext() ||
resolver->GetExecutionContext()->IsContextDestroyed()) {
return;
}
if (!success) {
DCHECK(!info);
DCHECK(!error_msg.IsNull());
ScriptState::Scope scope(resolver->GetScriptState());
resolver->Reject(V8ThrowException::CreateTypeError(
resolver->GetScriptState()->GetIsolate(), error_msg));
return;
}
ServiceWorkerWindowClient* window_client = nullptr;
// Even if the open/navigation succeeded, |info| may be null if information of
// the opened/navigated window could not be obtained (this can happen for a
// cross-origin window, or if the browser process could not get the
// information in time before the window was closed).
if (info)
window_client = ServiceWorkerWindowClient::Create(*info);
resolver->Resolve(window_client);
}
} // namespace
ServiceWorkerGlobalScopeClient::ServiceWorkerGlobalScopeClient(
WebServiceWorkerContextClient& client)
: client_(client) {}
void ServiceWorkerGlobalScopeClient::GetClient(const String& id,
GetClientCallback callback) {
service_worker_host_->GetClient(id, std::move(callback));
}
void ServiceWorkerGlobalScopeClient::GetClients(
mojom::blink::ServiceWorkerClientQueryOptionsPtr options,
GetClientsCallback callback) {
service_worker_host_->GetClients(std::move(options), std::move(callback));
}
void ServiceWorkerGlobalScopeClient::OpenWindowForClients(
const KURL& url,
ScriptPromiseResolver* resolver) {
service_worker_host_->OpenNewTab(
url, WTF::Bind(&DidNavigateOrOpenWindow, WrapPersistent(resolver)));
}
void ServiceWorkerGlobalScopeClient::OpenWindowForPaymentHandler(
const KURL& url,
ScriptPromiseResolver* resolver) {
service_worker_host_->OpenPaymentHandlerWindow(
url, WTF::Bind(&DidNavigateOrOpenWindow, WrapPersistent(resolver)));
}
void ServiceWorkerGlobalScopeClient::SetCachedMetadata(const KURL& url,
const uint8_t* data,
size_t size) {
Vector<uint8_t> meta_data;
meta_data.Append(data, SafeCast<wtf_size_t>(size));
service_worker_host_->SetCachedMetadata(url, meta_data);
}
void ServiceWorkerGlobalScopeClient::ClearCachedMetadata(const KURL& url) {
service_worker_host_->ClearCachedMetadata(url);
}
void ServiceWorkerGlobalScopeClient::PostMessageToClient(
const String& client_uuid,
BlinkTransferableMessage message) {
service_worker_host_->PostMessageToClient(client_uuid, std::move(message));
}
void ServiceWorkerGlobalScopeClient::SkipWaiting(SkipWaitingCallback callback) {
service_worker_host_->SkipWaiting(std::move(callback));
}
void ServiceWorkerGlobalScopeClient::Claim(ClaimCallback callback) {
service_worker_host_->ClaimClients(std::move(callback));
}
void ServiceWorkerGlobalScopeClient::Focus(const String& client_uuid,
FocusCallback callback) {
service_worker_host_->FocusClient(client_uuid, std::move(callback));
}
void ServiceWorkerGlobalScopeClient::Navigate(const String& client_uuid,
const KURL& url,
ScriptPromiseResolver* resolver) {
service_worker_host_->NavigateClient(
client_uuid, url,
WTF::Bind(&DidNavigateOrOpenWindow, WrapPersistent(resolver)));
}
void ServiceWorkerGlobalScopeClient::BindServiceWorkerHost(
mojom::blink::ServiceWorkerHostAssociatedPtrInfo service_worker_host,
scoped_refptr<base::SequencedTaskRunner> task_runner) {
DCHECK(service_worker_host.is_valid());
DCHECK(!service_worker_host_);
service_worker_host_.Bind(std::move(service_worker_host),
std::move(task_runner));
}
void ServiceWorkerGlobalScopeClient::SetupNavigationPreload(
int fetch_event_id,
const KURL& url,
......@@ -171,10 +65,6 @@ void ServiceWorkerGlobalScopeClient::RequestTermination(
client_.RequestTermination(std::move(callback));
}
void ServiceWorkerGlobalScopeClient::WillDestroyWorkerContext() {
service_worker_host_.reset();
}
const char ServiceWorkerGlobalScopeClient::kSupplementName[] =
"ServiceWorkerGlobalScopeClient";
......
......@@ -34,21 +34,15 @@
#include <memory>
#include "base/macros.h"
#include "third_party/blink/public/common/messaging/transferable_message.h"
#include "third_party/blink/public/mojom/service_worker/service_worker.mojom-blink.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_event_status.mojom-blink.h"
#include "third_party/blink/renderer/core/messaging/message_port.h"
#include "third_party/blink/public/mojom/service_worker/dispatch_fetch_event_params.mojom-blink.h"
#include "third_party/blink/renderer/core/workers/worker_clients.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
namespace blink {
class ExecutionContext;
class KURL;
class ScriptPromiseResolver;
class WebServiceWorkerContextClient;
class WorkerClients;
class MODULES_EXPORT ServiceWorkerGlobalScopeClient final
: public GarbageCollectedFinalized<ServiceWorkerGlobalScopeClient>,
......@@ -56,44 +50,17 @@ class MODULES_EXPORT ServiceWorkerGlobalScopeClient final
USING_GARBAGE_COLLECTED_MIXIN(ServiceWorkerGlobalScopeClient);
public:
using ClaimCallback = mojom::blink::ServiceWorkerHost::ClaimClientsCallback;
using SkipWaitingCallback =
mojom::blink::ServiceWorkerHost::SkipWaitingCallback;
using GetClientCallback = mojom::blink::ServiceWorkerHost::GetClientCallback;
using GetClientsCallback =
mojom::blink::ServiceWorkerHost::GetClientsCallback;
using FocusCallback = mojom::blink::ServiceWorkerHost::FocusClientCallback;
static const char kSupplementName[];
explicit ServiceWorkerGlobalScopeClient(WebServiceWorkerContextClient&);
// Called from ServiceWorkerClients.
void GetClient(const String&, GetClientCallback);
void GetClients(mojom::blink::ServiceWorkerClientQueryOptionsPtr,
GetClientsCallback);
void OpenWindowForClients(const KURL&, ScriptPromiseResolver*);
void OpenWindowForPaymentHandler(const KURL&, ScriptPromiseResolver*);
void SetCachedMetadata(const KURL&, const uint8_t*, size_t);
void ClearCachedMetadata(const KURL&);
void PostMessageToClient(const String& client_uuid, BlinkTransferableMessage);
void SkipWaiting(SkipWaitingCallback);
void Claim(ClaimCallback);
void Focus(const String& client_uuid, FocusCallback);
void Navigate(const String& client_uuid, const KURL&, ScriptPromiseResolver*);
// Called from ServiceWorkerGlobalScope.
void BindServiceWorkerHost(
mojom::blink::ServiceWorkerHostAssociatedPtrInfo service_worker_host,
scoped_refptr<base::SequencedTaskRunner> task_runner);
void SetupNavigationPreload(
int fetch_event_id,
const KURL& url,
mojom::blink::FetchEventPreloadHandlePtr preload_handle);
void RequestTermination(base::OnceCallback<void(bool)> callback);
void WillDestroyWorkerContext();
static ServiceWorkerGlobalScopeClient* From(ExecutionContext*);
void Trace(blink::Visitor*) override;
......@@ -101,12 +68,6 @@ class MODULES_EXPORT ServiceWorkerGlobalScopeClient final
private:
WebServiceWorkerContextClient& client_;
// Lives on the service worker thread, is bound by BindServiceWorkerHost()
// which is triggered by the first Mojo call received on the service worker
// thread mojom::blink::ServiceWorker::InitializeGlobalScope(), and is closed
// by WillDestroyWorkerContext().
mojom::blink::ServiceWorkerHostAssociatedPtr service_worker_host_;
DISALLOW_COPY_AND_ASSIGN(ServiceWorkerGlobalScopeClient);
};
......
......@@ -4,8 +4,7 @@
#include "third_party/blink/renderer/modules/service_worker/service_worker_script_cached_metadata_handler.h"
#include "third_party/blink/renderer/core/workers/worker_global_scope.h"
#include "third_party/blink/renderer/modules/service_worker/service_worker_global_scope_client.h"
#include "third_party/blink/renderer/modules/service_worker/service_worker_global_scope.h"
#include "third_party/blink/renderer/platform/loader/fetch/cached_metadata.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource.h"
......@@ -13,10 +12,10 @@ namespace blink {
ServiceWorkerScriptCachedMetadataHandler::
ServiceWorkerScriptCachedMetadataHandler(
WorkerGlobalScope* worker_global_scope,
ServiceWorkerGlobalScope* global_scope,
const KURL& script_url,
std::unique_ptr<Vector<uint8_t>> meta_data)
: worker_global_scope_(worker_global_scope), script_url_(script_url) {
: global_scope_(global_scope), script_url_(script_url) {
if (meta_data) {
cached_metadata_ =
CachedMetadata::CreateFromSerializedData(std::move(*meta_data));
......@@ -27,7 +26,7 @@ ServiceWorkerScriptCachedMetadataHandler::
~ServiceWorkerScriptCachedMetadataHandler() = default;
void ServiceWorkerScriptCachedMetadataHandler::Trace(blink::Visitor* visitor) {
visitor->Trace(worker_global_scope_);
visitor->Trace(global_scope_);
CachedMetadataHandler::Trace(visitor);
}
......@@ -40,9 +39,8 @@ void ServiceWorkerScriptCachedMetadataHandler::SetCachedMetadata(
return;
cached_metadata_ = CachedMetadata::Create(data_type_id, data, size);
const Vector<uint8_t>& serialized_data = cached_metadata_->SerializedData();
ServiceWorkerGlobalScopeClient::From(worker_global_scope_)
->SetCachedMetadata(script_url_, serialized_data.data(),
serialized_data.size());
global_scope_->GetServiceWorkerHost()->SetCachedMetadata(script_url_,
serialized_data);
}
void ServiceWorkerScriptCachedMetadataHandler::ClearCachedMetadata(
......@@ -50,8 +48,7 @@ void ServiceWorkerScriptCachedMetadataHandler::ClearCachedMetadata(
if (type != kSendToPlatform)
return;
cached_metadata_ = nullptr;
ServiceWorkerGlobalScopeClient::From(worker_global_scope_)
->ClearCachedMetadata(script_url_);
global_scope_->GetServiceWorkerHost()->ClearCachedMetadata(script_url_);
}
scoped_refptr<CachedMetadata>
......
......@@ -13,22 +13,22 @@
namespace blink {
class WorkerGlobalScope;
class CachedMetadata;
class ServiceWorkerGlobalScope;
class ServiceWorkerScriptCachedMetadataHandler
: public SingleCachedMetadataHandler {
public:
static ServiceWorkerScriptCachedMetadataHandler* Create(
WorkerGlobalScope* worker_global_scope,
ServiceWorkerGlobalScope* global_scope,
const KURL& script_url,
std::unique_ptr<Vector<uint8_t>> meta_data) {
return MakeGarbageCollected<ServiceWorkerScriptCachedMetadataHandler>(
worker_global_scope, script_url, std::move(meta_data));
global_scope, script_url, std::move(meta_data));
}
ServiceWorkerScriptCachedMetadataHandler(
WorkerGlobalScope*,
ServiceWorkerGlobalScope*,
const KURL& script_url,
std::unique_ptr<Vector<uint8_t>> meta_data);
~ServiceWorkerScriptCachedMetadataHandler() override;
......@@ -47,7 +47,7 @@ class ServiceWorkerScriptCachedMetadataHandler
size_t GetCodeCacheSize() const override;
private:
Member<WorkerGlobalScope> worker_global_scope_;
Member<ServiceWorkerGlobalScope> global_scope_;
KURL script_url_;
scoped_refptr<CachedMetadata> cached_metadata_;
};
......
......@@ -10,11 +10,11 @@
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/messaging/message_port.h"
#include "third_party/blink/renderer/core/page/page_hidden_state.h"
#include "third_party/blink/renderer/core/workers/worker_global_scope.h"
#include "third_party/blink/renderer/core/workers/worker_location.h"
#include "third_party/blink/renderer/modules/service_worker/service_worker_error.h"
#include "third_party/blink/renderer/modules/service_worker/service_worker_global_scope_client.h"
#include "third_party/blink/renderer/modules/service_worker/service_worker_global_scope.h"
#include "third_party/blink/renderer/platform/bindings/v8_throw_exception.h"
#include "third_party/blink/renderer/platform/heap/heap.h"
......@@ -38,6 +38,33 @@ void DidFocus(ScriptPromiseResolver* resolver,
resolver->Resolve(ServiceWorkerWindowClient::Create(*client));
}
void DidNavigateOrOpenWindow(ScriptPromiseResolver* resolver,
bool success,
mojom::blink::ServiceWorkerClientInfoPtr info,
const String& error_msg) {
if (!resolver->GetExecutionContext() ||
resolver->GetExecutionContext()->IsContextDestroyed()) {
return;
}
if (!success) {
DCHECK(!info);
DCHECK(!error_msg.IsNull());
ScriptState::Scope scope(resolver->GetScriptState());
resolver->Reject(V8ThrowException::CreateTypeError(
resolver->GetScriptState()->GetIsolate(), error_msg));
return;
}
ServiceWorkerWindowClient* window_client = nullptr;
// Even if the open/navigation succeeded, |info| may be null if information of
// the opened/navigated window could not be obtained (this can happen for a
// cross-origin window, or if the browser process could not get the
// information in time before the window was closed).
if (info)
window_client = MakeGarbageCollected<ServiceWorkerWindowClient>(*info);
resolver->Resolve(window_client);
}
} // namespace
ServiceWorkerWindowClient* ServiceWorkerWindowClient::Create(
......@@ -46,6 +73,13 @@ ServiceWorkerWindowClient* ServiceWorkerWindowClient::Create(
return MakeGarbageCollected<ServiceWorkerWindowClient>(info);
}
// static
ServiceWorkerWindowClient::ResolveWindowClientCallback
ServiceWorkerWindowClient::CreateResolveWindowClientCallback(
ScriptPromiseResolver* resolver) {
return WTF::Bind(&DidNavigateOrOpenWindow, WrapPersistent(resolver));
}
ServiceWorkerWindowClient::ServiceWorkerWindowClient(
const mojom::blink::ServiceWorkerClientInfo& info)
: ServiceWorkerClient(info),
......@@ -61,17 +95,19 @@ String ServiceWorkerWindowClient::visibilityState() const {
ScriptPromise ServiceWorkerWindowClient::focus(ScriptState* script_state) {
auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(script_state);
ScriptPromise promise = resolver->Promise();
ServiceWorkerGlobalScope* global_scope =
To<ServiceWorkerGlobalScope>(ExecutionContext::From(script_state));
if (!ExecutionContext::From(script_state)->IsWindowInteractionAllowed()) {
if (!global_scope->IsWindowInteractionAllowed()) {
resolver->Reject(MakeGarbageCollected<DOMException>(
DOMExceptionCode::kInvalidAccessError,
"Not allowed to focus a window."));
return promise;
}
ExecutionContext::From(script_state)->ConsumeWindowInteraction();
global_scope->ConsumeWindowInteraction();
ServiceWorkerGlobalScopeClient::From(ExecutionContext::From(script_state))
->Focus(Uuid(), WTF::Bind(&DidFocus, WrapPersistent(resolver)));
global_scope->GetServiceWorkerHost()->FocusClient(
Uuid(), WTF::Bind(&DidFocus, WrapPersistent(resolver)));
return promise;
}
......@@ -79,24 +115,24 @@ ScriptPromise ServiceWorkerWindowClient::navigate(ScriptState* script_state,
const String& url) {
auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(script_state);
ScriptPromise promise = resolver->Promise();
ExecutionContext* context = ExecutionContext::From(script_state);
ServiceWorkerGlobalScope* global_scope =
To<ServiceWorkerGlobalScope>(ExecutionContext::From(script_state));
KURL parsed_url =
KURL(To<WorkerGlobalScope>(context)->location()->Url(), url);
KURL parsed_url = KURL(global_scope->location()->Url(), url);
if (!parsed_url.IsValid() || parsed_url.ProtocolIsAbout()) {
resolver->Reject(V8ThrowException::CreateTypeError(
script_state->GetIsolate(), "'" + url + "' is not a valid URL."));
return promise;
}
if (!context->GetSecurityOrigin()->CanDisplay(parsed_url)) {
if (!global_scope->GetSecurityOrigin()->CanDisplay(parsed_url)) {
resolver->Reject(V8ThrowException::CreateTypeError(
script_state->GetIsolate(),
"'" + parsed_url.ElidedString() + "' cannot navigate."));
return promise;
}
ServiceWorkerGlobalScopeClient::From(context)->Navigate(Uuid(), parsed_url,
resolver);
global_scope->GetServiceWorkerHost()->NavigateClient(
Uuid(), parsed_url, CreateResolveWindowClientCallback(resolver));
return promise;
}
......
......@@ -14,6 +14,7 @@
namespace blink {
class ScriptPromiseResolver;
class ScriptState;
class MODULES_EXPORT ServiceWorkerWindowClient final
......@@ -21,9 +22,15 @@ class MODULES_EXPORT ServiceWorkerWindowClient final
DEFINE_WRAPPERTYPEINFO();
public:
using ResolveWindowClientCallback = base::OnceCallback<
void(bool, mojom::blink::ServiceWorkerClientInfoPtr, const String&)>;
static ServiceWorkerWindowClient* Create(
const mojom::blink::ServiceWorkerClientInfo&);
static ResolveWindowClientCallback CreateResolveWindowClientCallback(
ScriptPromiseResolver*);
explicit ServiceWorkerWindowClient(
const mojom::blink::ServiceWorkerClientInfo&);
~ServiceWorkerWindowClient() override;
......
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