diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2019-07-31 15:50:41 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2019-08-30 12:35:23 +0000 |
commit | 7b2ffa587235a47d4094787d72f38102089f402a (patch) | |
tree | 30e82af9cbab08a7fa028bb18f4f2987a3f74dfa /chromium/third_party/blink/renderer/core/html/portal | |
parent | d94af01c90575348c4e81a418257f254b6f8d225 (diff) | |
download | qtwebengine-chromium-7b2ffa587235a47d4094787d72f38102089f402a.tar.gz |
BASELINE: Update Chromium to 76.0.3809.94
Change-Id: I321c3f5f929c105aec0f98c5091ef6108822e647
Reviewed-by: Michael BrĂ¼ning <michael.bruning@qt.io>
Diffstat (limited to 'chromium/third_party/blink/renderer/core/html/portal')
10 files changed, 381 insertions, 90 deletions
diff --git a/chromium/third_party/blink/renderer/core/html/portal/dom_window_portal_host.cc b/chromium/third_party/blink/renderer/core/html/portal/dom_window_portal_host.cc index b86b3154914..9b449d37e38 100644 --- a/chromium/third_party/blink/renderer/core/html/portal/dom_window_portal_host.cc +++ b/chromium/third_party/blink/renderer/core/html/portal/dom_window_portal_host.cc @@ -13,12 +13,17 @@ namespace blink { // static PortalHost* DOMWindowPortalHost::portalHost(LocalDOMWindow& window) { + if (ShouldExposePortalHost(window)) + return &PortalHost::From(window); + return nullptr; +} + +// static +bool DOMWindowPortalHost::ShouldExposePortalHost(const LocalDOMWindow& window) { // The portal host is only exposed in the main frame of a page // embedded in a portal. - if (!window.GetFrame() || !window.GetFrame()->IsMainFrame() || - !window.GetFrame()->GetPage()->InsidePortal()) - return nullptr; - return &PortalHost::From(window); + return window.GetFrame() && window.GetFrame()->IsMainFrame() && + window.GetFrame()->GetPage()->InsidePortal(); } } // namespace blink diff --git a/chromium/third_party/blink/renderer/core/html/portal/dom_window_portal_host.h b/chromium/third_party/blink/renderer/core/html/portal/dom_window_portal_host.h index 96e0154f385..87794ad6524 100644 --- a/chromium/third_party/blink/renderer/core/html/portal/dom_window_portal_host.h +++ b/chromium/third_party/blink/renderer/core/html/portal/dom_window_portal_host.h @@ -15,6 +15,7 @@ class PortalHost; class CORE_EXPORT DOMWindowPortalHost { public: static PortalHost* portalHost(LocalDOMWindow& window); + static bool ShouldExposePortalHost(const LocalDOMWindow& window); }; } // namespace blink diff --git a/chromium/third_party/blink/renderer/core/html/portal/html_portal_element.cc b/chromium/third_party/blink/renderer/core/html/portal/html_portal_element.cc index ab3210d703e..bf97a2d4bd7 100644 --- a/chromium/third_party/blink/renderer/core/html/portal/html_portal_element.cc +++ b/chromium/third_party/blink/renderer/core/html/portal/html_portal_element.cc @@ -6,6 +6,7 @@ #include <utility> #include "services/service_manager/public/cpp/interface_provider.h" +#include "third_party/blink/renderer/bindings/core/v8/script_event_listener.h" #include "third_party/blink/renderer/bindings/core/v8/script_promise.h" #include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h" #include "third_party/blink/renderer/bindings/core/v8/serialization/post_message_helper.h" @@ -13,6 +14,8 @@ #include "third_party/blink/renderer/core/dom/document.h" #include "third_party/blink/renderer/core/dom/dom_exception.h" #include "third_party/blink/renderer/core/dom/node.h" +#include "third_party/blink/renderer/core/event_type_names.h" +#include "third_party/blink/renderer/core/events/message_event.h" #include "third_party/blink/renderer/core/execution_context/execution_context.h" #include "third_party/blink/renderer/core/frame/local_frame.h" #include "third_party/blink/renderer/core/frame/local_frame_client.h" @@ -22,6 +25,7 @@ #include "third_party/blink/renderer/core/html/parser/html_parser_idioms.h" #include "third_party/blink/renderer/core/html/portal/document_portals.h" #include "third_party/blink/renderer/core/html/portal/portal_activate_options.h" +#include "third_party/blink/renderer/core/html/portal/portal_post_message_helper.h" #include "third_party/blink/renderer/core/html_names.h" #include "third_party/blink/renderer/core/inspector/console_message.h" #include "third_party/blink/renderer/core/inspector/thread_debugger.h" @@ -30,7 +34,9 @@ #include "third_party/blink/renderer/core/page/page.h" #include "third_party/blink/renderer/platform/bindings/script_state.h" #include "third_party/blink/renderer/platform/heap/handle.h" +#include "third_party/blink/renderer/platform/heap/heap.h" #include "third_party/blink/renderer/platform/runtime_enabled_features.h" +#include "third_party/blink/renderer/platform/weborigin/security_origin.h" #include "third_party/blink/renderer/platform/wtf/functional.h" namespace blink { @@ -38,10 +44,12 @@ namespace blink { HTMLPortalElement::HTMLPortalElement( Document& document, const base::UnguessableToken& portal_token, - mojom::blink::PortalAssociatedPtr portal_ptr) + mojom::blink::PortalAssociatedPtr portal_ptr, + mojom::blink::PortalClientAssociatedRequest portal_client_request) : HTMLFrameOwnerElement(html_names::kPortalTag, document), portal_token_(portal_token), - portal_ptr_(std::move(portal_ptr)) {} + portal_ptr_(std::move(portal_ptr)), + portal_client_binding_(this, std::move(portal_client_request)) {} HTMLPortalElement::~HTMLPortalElement() {} @@ -50,17 +58,20 @@ void HTMLPortalElement::Trace(Visitor* visitor) { visitor->Trace(portal_frame_); } -HTMLElement* HTMLPortalElement::Create(Document& document) { - if (RuntimeEnabledFeatures::PortalsEnabled()) - return MakeGarbageCollected<HTMLPortalElement>(document); - return HTMLUnknownElement::Create(html_names::kPortalTag, document); -} - void HTMLPortalElement::Navigate() { KURL url = GetNonEmptyURLAttribute(html_names::kSrcAttr); - if (!url.IsEmpty() && portal_ptr_) { - portal_ptr_->Navigate(url); + if (!portal_ptr_ || url.IsEmpty()) + return; + + if (!url.ProtocolIsInHTTPFamily()) { + GetDocument().AddConsoleMessage(ConsoleMessage::Create( + mojom::ConsoleMessageSource::kRendering, + mojom::ConsoleMessageLevel::kWarning, + "Portals only allow navigation to protocols in the HTTP family.")); + return; } + + portal_ptr_->Navigate(url); } void HTMLPortalElement::ConsumePortal() { @@ -69,6 +80,7 @@ void HTMLPortalElement::ConsumePortal() { portal_token_ = base::UnguessableToken(); } portal_ptr_.reset(); + portal_client_binding_.Close(); } namespace { @@ -124,42 +136,6 @@ BlinkTransferableMessage ActivateDataAsMessage( return msg; } -BlinkTransferableMessage CreateMessageForPostMessage( - ScriptState* script_state, - const ScriptValue& message, - const WindowPostMessageOptions* options, - ExceptionState& exception_state) { - BlinkTransferableMessage transferable_message; - Transferables transferables; - scoped_refptr<SerializedScriptValue> serialized_message = - PostMessageHelper::SerializeMessageByMove(script_state->GetIsolate(), - message, options, transferables, - exception_state); - if (exception_state.HadException()) - return {}; - DCHECK(serialized_message); - transferable_message.message = serialized_message; - - // Disentangle the port in preparation for sending it to the remote context. - auto* execution_context = ExecutionContext::From(script_state); - transferable_message.ports = MessagePort::DisentanglePorts( - execution_context, transferables.message_ports, exception_state); - if (exception_state.HadException()) - return {}; - - if (ThreadDebugger* debugger = - ThreadDebugger::From(script_state->GetIsolate())) { - transferable_message.sender_stack_trace_id = - debugger->StoreCurrentStackTrace("postMessage"); - } - - transferable_message.user_activation = - PostMessageHelper::CreateUserActivationSnapshot(execution_context, - options); - - return transferable_message; -} - } // namespace ScriptPromise HTMLPortalElement::activate(ScriptState* script_state, @@ -254,18 +230,55 @@ void HTMLPortalElement::postMessage(ScriptState* script_state, return; } - scoped_refptr<const SecurityOrigin> target_origin = nullptr; - if (options->targetOrigin() == "/") - target_origin = GetDocument().GetSecurityOrigin(); - else if (options->targetOrigin() != "*") - target_origin = SecurityOrigin::CreateFromString(options->targetOrigin()); + scoped_refptr<const SecurityOrigin> target_origin = + PostMessageHelper::GetTargetOrigin(options, GetDocument(), + exception_state); + if (exception_state.HadException()) + return; - BlinkTransferableMessage transferable_message = CreateMessageForPostMessage( - script_state, message, options, exception_state); + BlinkTransferableMessage transferable_message = + PortalPostMessageHelper::CreateMessage(script_state, message, options, + exception_state); if (exception_state.HadException()) return; - portal_ptr_->PostMessage(std::move(transferable_message), target_origin); + portal_ptr_->PostMessageToGuest(std::move(transferable_message), + target_origin); +} + +EventListener* HTMLPortalElement::onmessage() { + return GetAttributeEventListener(event_type_names::kMessage); +} + +void HTMLPortalElement::setOnmessage(EventListener* listener) { + SetAttributeEventListener(event_type_names::kMessage, listener); +} + +EventListener* HTMLPortalElement::onmessageerror() { + return GetAttributeEventListener(event_type_names::kMessageerror); +} + +void HTMLPortalElement::setOnmessageerror(EventListener* listener) { + SetAttributeEventListener(event_type_names::kMessageerror, listener); +} + +void HTMLPortalElement::ForwardMessageFromGuest( + BlinkTransferableMessage message, + const scoped_refptr<const SecurityOrigin>& source_origin, + const scoped_refptr<const SecurityOrigin>& target_origin) { + if (!portal_ptr_) + return; + + PortalPostMessageHelper::CreateAndDispatchMessageEvent( + this, std::move(message), source_origin, target_origin); +} + +void HTMLPortalElement::DispatchLoadEvent() { + if (!portal_ptr_) + return; + + DispatchLoad(); + GetDocument().CheckCompleted(); } HTMLPortalElement::InsertionNotificationRequest HTMLPortalElement::InsertedInto( @@ -293,9 +306,11 @@ HTMLPortalElement::InsertionNotificationRequest HTMLPortalElement::InsertedInto( WTF::Bind(&HTMLPortalElement::ConsumePortal, WrapWeakPersistent(this))); DocumentPortals::From(GetDocument()).OnPortalInserted(this); } else { + mojom::blink::PortalClientAssociatedPtr client; + portal_client_binding_.Bind(mojo::MakeRequest(&client)); std::tie(portal_frame_, portal_token_) = GetDocument().GetFrame()->Client()->CreatePortal( - this, mojo::MakeRequest(&portal_ptr_)); + this, mojo::MakeRequest(&portal_ptr_), client.PassInterface()); portal_ptr_.set_connection_error_handler( WTF::Bind(&HTMLPortalElement::ConsumePortal, WrapWeakPersistent(this))); DocumentPortals::From(GetDocument()).OnPortalInserted(this); @@ -324,8 +339,26 @@ void HTMLPortalElement::ParseAttribute( const AttributeModificationParams& params) { HTMLFrameOwnerElement::ParseAttribute(params); - if (params.name == html_names::kSrcAttr) + if (params.name == html_names::kSrcAttr) { Navigate(); + return; + } + + struct { + const QualifiedName& name; + const AtomicString& event_name; + } event_handler_attributes[] = { + {html_names::kOnmessageAttr, event_type_names::kMessage}, + {html_names::kOnmessageerrorAttr, event_type_names::kMessageerror}, + }; + for (const auto& attribute : event_handler_attributes) { + if (params.name == attribute.name) { + SetAttributeEventListener( + attribute.event_name, + CreateAttributeEventListener(this, attribute.name, params.new_value)); + return; + } + } } LayoutObject* HTMLPortalElement::CreateLayoutObject(const ComputedStyle& style, diff --git a/chromium/third_party/blink/renderer/core/html/portal/html_portal_element.h b/chromium/third_party/blink/renderer/core/html/portal/html_portal_element.h index 331fd841e6e..296f102267d 100644 --- a/chromium/third_party/blink/renderer/core/html/portal/html_portal_element.h +++ b/chromium/third_party/blink/renderer/core/html/portal/html_portal_element.h @@ -6,6 +6,7 @@ #define THIRD_PARTY_BLINK_RENDERER_CORE_HTML_PORTAL_HTML_PORTAL_ELEMENT_H_ #include "base/unguessable_token.h" +#include "mojo/public/cpp/bindings/associated_binding.h" #include "third_party/blink/public/mojom/portal/portal.mojom-blink.h" #include "third_party/blink/renderer/bindings/core/v8/script_promise.h" #include "third_party/blink/renderer/core/core_export.h" @@ -24,16 +25,17 @@ class ScriptState; // activated using script. The portal element is still under development and not // part of the HTML standard. It can be enabled by passing // --enable-features=Portals. See also https://github.com/WICG/portals. -class CORE_EXPORT HTMLPortalElement : public HTMLFrameOwnerElement { +class CORE_EXPORT HTMLPortalElement : public HTMLFrameOwnerElement, + public mojom::blink::PortalClient { DEFINE_WRAPPERTYPEINFO(); public: - static HTMLElement* Create(Document&); - explicit HTMLPortalElement( Document& document, const base::UnguessableToken& portal_token = base::UnguessableToken(), - mojom::blink::PortalAssociatedPtr portal_ptr = nullptr); + mojom::blink::PortalAssociatedPtr portal_ptr = nullptr, + mojom::blink::PortalClientAssociatedRequest portal_client_request = + nullptr); ~HTMLPortalElement() override; // ScriptWrappable overrides. @@ -50,6 +52,17 @@ class CORE_EXPORT HTMLPortalElement : public HTMLFrameOwnerElement { const ScriptValue& message, const WindowPostMessageOptions* options, ExceptionState& exception_state); + EventListener* onmessage(); + void setOnmessage(EventListener* listener); + EventListener* onmessageerror(); + void setOnmessageerror(EventListener* listener); + + // blink::mojom::PortalClient implementation + void ForwardMessageFromGuest( + BlinkTransferableMessage message, + const scoped_refptr<const SecurityOrigin>& source_origin, + const scoped_refptr<const SecurityOrigin>& target_origin) override; + void DispatchLoadEvent() override; const base::UnguessableToken& GetToken() const { return portal_token_; } @@ -94,6 +107,7 @@ class CORE_EXPORT HTMLPortalElement : public HTMLFrameOwnerElement { bool is_activating_ = false; mojom::blink::PortalAssociatedPtr portal_ptr_; + mojo::AssociatedBinding<mojom::blink::PortalClient> portal_client_binding_; }; } // namespace blink diff --git a/chromium/third_party/blink/renderer/core/html/portal/html_portal_element.idl b/chromium/third_party/blink/renderer/core/html/portal/html_portal_element.idl index e0b1eae11e4..cb1b02fc1ff 100644 --- a/chromium/third_party/blink/renderer/core/html/portal/html_portal_element.idl +++ b/chromium/third_party/blink/renderer/core/html/portal/html_portal_element.idl @@ -11,4 +11,7 @@ interface HTMLPortalElement : HTMLElement { [CallWith=ScriptState, RaisesException] void postMessage(any message, DOMString targetOrigin, optional sequence<object> transfer = []); [CallWith=ScriptState, RaisesException] void postMessage(any message, optional WindowPostMessageOptions options); + + attribute EventHandler onmessage; + attribute EventHandler onmessageerror; }; diff --git a/chromium/third_party/blink/renderer/core/html/portal/portal_host.cc b/chromium/third_party/blink/renderer/core/html/portal/portal_host.cc index 1dc6169b02a..0754209ec3c 100644 --- a/chromium/third_party/blink/renderer/core/html/portal/portal_host.cc +++ b/chromium/third_party/blink/renderer/core/html/portal/portal_host.cc @@ -4,12 +4,17 @@ #include "third_party/blink/renderer/core/html/portal/portal_host.h" +#include <utility> +#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h" +#include "third_party/blink/renderer/bindings/core/v8/script_value.h" +#include "third_party/blink/renderer/bindings/core/v8/serialization/post_message_helper.h" #include "third_party/blink/renderer/core/dom/document.h" -#include "third_party/blink/renderer/core/events/message_event.h" #include "third_party/blink/renderer/core/execution_context/execution_context.h" #include "third_party/blink/renderer/core/frame/local_dom_window.h" -#include "third_party/blink/renderer/core/frame/user_activation.h" -#include "third_party/blink/renderer/core/inspector/main_thread_debugger.h" +#include "third_party/blink/renderer/core/frame/local_frame_client.h" +#include "third_party/blink/renderer/core/frame/window_post_message_options.h" +#include "third_party/blink/renderer/core/html/portal/dom_window_portal_host.h" +#include "third_party/blink/renderer/core/html/portal/portal_post_message_helper.h" #include "third_party/blink/renderer/core/page/page.h" #include "third_party/blink/renderer/platform/wtf/text/atomic_string.h" @@ -49,34 +54,87 @@ PortalHost* PortalHost::ToPortalHost() { return this; } +Document* PortalHost::GetDocument() const { + return To<Document>(GetExecutionContext()); +} + +void PortalHost::OnPortalActivated() { + portal_host_ptr_.reset(); +} + +void PortalHost::postMessage(ScriptState* script_state, + const ScriptValue& message, + const String& target_origin, + Vector<ScriptValue>& transfer, + ExceptionState& exception_state) { + WindowPostMessageOptions* options = WindowPostMessageOptions::Create(); + options->setTargetOrigin(target_origin); + if (!transfer.IsEmpty()) + options->setTransfer(transfer); + postMessage(script_state, message, options, exception_state); +} + +void PortalHost::postMessage(ScriptState* script_state, + const ScriptValue& message, + const WindowPostMessageOptions* options, + ExceptionState& exception_state) { + if (!DOMWindowPortalHost::ShouldExposePortalHost(*GetSupplementable())) { + exception_state.ThrowDOMException( + DOMExceptionCode::kInvalidStateError, + "The document is no longer inside a portal"); + return; + } + + scoped_refptr<const SecurityOrigin> target_origin = + PostMessageHelper::GetTargetOrigin(options, *GetDocument(), + exception_state); + if (exception_state.HadException()) + return; + + BlinkTransferableMessage transferable_message = + PortalPostMessageHelper::CreateMessage(script_state, message, options, + exception_state); + if (exception_state.HadException()) + return; + + GetPortalHostInterface().PostMessageToHost(std::move(transferable_message), + target_origin); +} + +EventListener* PortalHost::onmessage() { + return GetAttributeEventListener(event_type_names::kMessage); +} + +void PortalHost::setOnmessage(EventListener* listener) { + SetAttributeEventListener(event_type_names::kMessage, listener); +} + +EventListener* PortalHost::onmessageerror() { + return GetAttributeEventListener(event_type_names::kMessageerror); +} + +void PortalHost::setOnmessageerror(EventListener* listener) { + SetAttributeEventListener(event_type_names::kMessageerror, listener); +} + void PortalHost::ReceiveMessage( BlinkTransferableMessage message, scoped_refptr<const SecurityOrigin> source_origin, scoped_refptr<const SecurityOrigin> target_origin) { - DCHECK(GetSupplementable()->document()->GetPage()->InsidePortal()); - if (target_origin && !target_origin->IsSameSchemeHostPort( - GetExecutionContext()->GetSecurityOrigin())) { - return; - } + DCHECK(GetDocument()->GetPage()->InsidePortal()); + PortalPostMessageHelper::CreateAndDispatchMessageEvent( + this, std::move(message), source_origin, target_origin); +} - UserActivation* user_activation = nullptr; - if (message.user_activation) { - user_activation = MakeGarbageCollected<UserActivation>( - message.user_activation->has_been_active, - message.user_activation->was_active); +mojom::blink::PortalHost& PortalHost::GetPortalHostInterface() { + if (!portal_host_ptr_) { + DCHECK(GetDocument()->GetFrame()); + GetDocument() + ->GetFrame() + ->GetRemoteNavigationAssociatedInterfaces() + ->GetInterface(&portal_host_ptr_); } - - MessageEvent* event = MessageEvent::Create(message.ports, message.message, - source_origin->ToString(), - String(), this, user_activation); - event->EntangleMessagePorts(GetExecutionContext()); - - ThreadDebugger* debugger = MainThreadDebugger::Instance(); - if (debugger) - debugger->ExternalAsyncTaskStarted(message.sender_stack_trace_id); - DispatchEvent(*event); - if (debugger) - debugger->ExternalAsyncTaskFinished(message.sender_stack_trace_id); + return *portal_host_ptr_; } } // namespace blink diff --git a/chromium/third_party/blink/renderer/core/html/portal/portal_host.h b/chromium/third_party/blink/renderer/core/html/portal/portal_host.h index 1ba227fbda7..9fa6c67e828 100644 --- a/chromium/third_party/blink/renderer/core/html/portal/portal_host.h +++ b/chromium/third_party/blink/renderer/core/html/portal/portal_host.h @@ -5,6 +5,7 @@ #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_HTML_PORTAL_PORTAL_HOST_H_ #define THIRD_PARTY_BLINK_RENDERER_CORE_HTML_PORTAL_PORTAL_HOST_H_ +#include "third_party/blink/public/mojom/portal/portal.mojom-blink.h" #include "third_party/blink/renderer/core/core_export.h" #include "third_party/blink/renderer/core/dom/events/event_target.h" #include "third_party/blink/renderer/core/messaging/blink_transferable_message.h" @@ -12,9 +13,12 @@ namespace blink { +class Document; class ExecutionContext; class LocalDOMWindow; +class ScriptValue; class SecurityOrigin; +class WindowPostMessageOptions; class CORE_EXPORT PortalHost : public EventTargetWithInlineData, public Supplement<LocalDOMWindow> { @@ -34,9 +38,34 @@ class CORE_EXPORT PortalHost : public EventTargetWithInlineData, ExecutionContext* GetExecutionContext() const override; PortalHost* ToPortalHost() override; + Document* GetDocument() const; + + // Called immediately before dispatching the onactivate event. + void OnPortalActivated(); + + // idl implementation + void postMessage(ScriptState* script_state, + const ScriptValue& message, + const String& target_origin, + Vector<ScriptValue>& transfer, + ExceptionState& exception_state); + void postMessage(ScriptState* script_state, + const ScriptValue& message, + const WindowPostMessageOptions* options, + ExceptionState& exception_state); + EventListener* onmessage(); + void setOnmessage(EventListener* listener); + EventListener* onmessageerror(); + void setOnmessageerror(EventListener* listener); + void ReceiveMessage(BlinkTransferableMessage message, scoped_refptr<const SecurityOrigin> source_origin, scoped_refptr<const SecurityOrigin> target_origin); + + private: + mojom::blink::PortalHost& GetPortalHostInterface(); + + mojom::blink::PortalHostAssociatedPtr portal_host_ptr_; }; } // namespace blink diff --git a/chromium/third_party/blink/renderer/core/html/portal/portal_host.idl b/chromium/third_party/blink/renderer/core/html/portal/portal_host.idl index 2cf404856c1..ced86df51f9 100644 --- a/chromium/third_party/blink/renderer/core/html/portal/portal_host.idl +++ b/chromium/third_party/blink/renderer/core/html/portal/portal_host.idl @@ -5,4 +5,12 @@ // https://wicg.github.io/portals/#the-portalhost-interface [Exposed=Window, RuntimeEnabled=Portals] -interface PortalHost : EventTarget {}; +interface PortalHost : EventTarget { + [RaisesException, CallWith=ScriptState] void postMessage(any message, DOMString targetOrigin, + optional sequence<object> transfer = []); + [RaisesException, CallWith=ScriptState] void postMessage(any message, + optional WindowPostMessageOptions options); + + attribute EventHandler onmessage; + attribute EventHandler onmessageerror; +}; diff --git a/chromium/third_party/blink/renderer/core/html/portal/portal_post_message_helper.cc b/chromium/third_party/blink/renderer/core/html/portal/portal_post_message_helper.cc new file mode 100644 index 00000000000..82e04cc752c --- /dev/null +++ b/chromium/third_party/blink/renderer/core/html/portal/portal_post_message_helper.cc @@ -0,0 +1,99 @@ +// Copyright 2019 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 "third_party/blink/renderer/core/html/portal/portal_post_message_helper.h" + +#include "third_party/blink/renderer/bindings/core/v8/serialization/post_message_helper.h" +#include "third_party/blink/renderer/bindings/core/v8/serialization/serialized_script_value.h" +#include "third_party/blink/renderer/bindings/core/v8/serialization/transferables.h" +#include "third_party/blink/renderer/core/dom/events/event_target.h" +#include "third_party/blink/renderer/core/events/message_event.h" +#include "third_party/blink/renderer/core/execution_context/execution_context.h" +#include "third_party/blink/renderer/core/frame/user_activation.h" +#include "third_party/blink/renderer/core/frame/window_post_message_options.h" +#include "third_party/blink/renderer/core/html/portal/html_portal_element.h" +#include "third_party/blink/renderer/core/inspector/main_thread_debugger.h" +#include "third_party/blink/renderer/core/inspector/thread_debugger.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/platform/bindings/exception_state.h" +#include "third_party/blink/renderer/platform/bindings/script_state.h" +#include "third_party/blink/renderer/platform/weborigin/security_origin.h" + +namespace blink { + +// static +BlinkTransferableMessage PortalPostMessageHelper::CreateMessage( + ScriptState* script_state, + const ScriptValue& message, + const WindowPostMessageOptions* options, + ExceptionState& exception_state) { + BlinkTransferableMessage transferable_message; + Transferables transferables; + scoped_refptr<SerializedScriptValue> serialized_message = + PostMessageHelper::SerializeMessageByMove(script_state->GetIsolate(), + message, options, transferables, + exception_state); + if (exception_state.HadException()) + return {}; + DCHECK(serialized_message); + transferable_message.message = serialized_message; + + // Disentangle the port in preparation for sending it to the remote context. + auto* execution_context = ExecutionContext::From(script_state); + transferable_message.ports = MessagePort::DisentanglePorts( + execution_context, transferables.message_ports, exception_state); + if (exception_state.HadException()) + return {}; + + if (ThreadDebugger* debugger = + ThreadDebugger::From(script_state->GetIsolate())) { + transferable_message.sender_stack_trace_id = + debugger->StoreCurrentStackTrace("postMessage"); + } + + transferable_message.user_activation = + PostMessageHelper::CreateUserActivationSnapshot(execution_context, + options); + + return transferable_message; +} + +// static +void PortalPostMessageHelper::CreateAndDispatchMessageEvent( + EventTarget* event_target, + BlinkTransferableMessage message, + scoped_refptr<const SecurityOrigin> source_origin, + scoped_refptr<const SecurityOrigin> target_origin) { + DCHECK(event_target->ToPortalHost() || + (event_target->ToNode() && + ToHTMLPortalElementOrNull(event_target->ToNode()))); + + if (target_origin && + !target_origin->IsSameSchemeHostPort( + event_target->GetExecutionContext()->GetSecurityOrigin())) { + return; + } + + UserActivation* user_activation = nullptr; + if (message.user_activation) { + user_activation = MakeGarbageCollected<UserActivation>( + message.user_activation->has_been_active, + message.user_activation->was_active); + } + + MessageEvent* event = MessageEvent::Create( + message.ports, message.message, source_origin->ToString(), String(), + event_target, user_activation); + event->EntangleMessagePorts(event_target->GetExecutionContext()); + + ThreadDebugger* debugger = MainThreadDebugger::Instance(); + if (debugger) + debugger->ExternalAsyncTaskStarted(message.sender_stack_trace_id); + event_target->DispatchEvent(*event); + if (debugger) + debugger->ExternalAsyncTaskFinished(message.sender_stack_trace_id); +} + +} // namespace blink diff --git a/chromium/third_party/blink/renderer/core/html/portal/portal_post_message_helper.h b/chromium/third_party/blink/renderer/core/html/portal/portal_post_message_helper.h new file mode 100644 index 00000000000..8a6a24782f5 --- /dev/null +++ b/chromium/third_party/blink/renderer/core/html/portal/portal_post_message_helper.h @@ -0,0 +1,41 @@ +// Copyright 2019 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 THIRD_PARTY_BLINK_RENDERER_CORE_HTML_PORTAL_PORTAL_POST_MESSAGE_HELPER_H_ +#define THIRD_PARTY_BLINK_RENDERER_CORE_HTML_PORTAL_PORTAL_POST_MESSAGE_HELPER_H_ + +#include "base/memory/scoped_refptr.h" +#include "third_party/blink/renderer/core/core_export.h" +#include "third_party/blink/renderer/platform/wtf/allocator.h" + +namespace blink { + +class EventTarget; +class ExceptionState; +class ScriptState; +class ScriptValue; +class SecurityOrigin; +class WindowPostMessageOptions; +struct BlinkTransferableMessage; + +class CORE_EXPORT PortalPostMessageHelper { + STATIC_ONLY(PortalPostMessageHelper); + + public: + static BlinkTransferableMessage CreateMessage( + ScriptState* script_state, + const ScriptValue& message, + const WindowPostMessageOptions* options, + ExceptionState& exception_state); + + static void CreateAndDispatchMessageEvent( + EventTarget* target, + BlinkTransferableMessage message, + scoped_refptr<const SecurityOrigin> source_origin, + scoped_refptr<const SecurityOrigin> target_origin); +}; + +} // namespace blink + +#endif // THIRD_PARTY_BLINK_RENDERER_CORE_HTML_PORTAL_PORTAL_POST_MESSAGE_HELPER_H_ |