Commit c23d1b73 authored by Dave Tapuska's avatar Dave Tapuska Committed by Commit Bot

Remove usage of PostMessage IDL extended attribute.

In anticipation of adding a few more overloads to the postMessage APIs
in https://github.com/whatwg/html/issues/3799 ensure that the code doesn't
use the PostMessage extended attribute. Since the extended attribute doesn't support
overloading it was decided that these implementations should really be
reading the transferable argument themselves not in the bindings code.

Make two versions one that is used by service workers that causes
transferables to be copied and another that is used by workers,
and message_ports that treat them as moveable types.

BUG=861735

Change-Id: I82720aae73a1285d74b5c8f10b244c719290e2d9
Reviewed-on: https://chromium-review.googlesource.com/1165835
Commit-Queue: Dave Tapuska <dtapuska@chromium.org>
Reviewed-by: default avatarHitoshi Yoshida <peria@chromium.org>
Reviewed-by: default avatarYuki Shiino <yukishiino@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#582127}
parent d995adf5
...@@ -156,6 +156,8 @@ bindings_core_v8_files = ...@@ -156,6 +156,8 @@ bindings_core_v8_files =
"core/v8/window_proxy_manager.h", "core/v8/window_proxy_manager.h",
"core/v8/worker_or_worklet_script_controller.cc", "core/v8/worker_or_worklet_script_controller.cc",
"core/v8/worker_or_worklet_script_controller.h", "core/v8/worker_or_worklet_script_controller.h",
"core/v8/serialization/post_message_helper.cc",
"core/v8/serialization/post_message_helper.h",
"core/v8/serialization/serialized_color_params.cc", "core/v8/serialization/serialized_color_params.cc",
"core/v8/serialization/serialized_color_params.h", "core/v8/serialization/serialized_color_params.h",
"core/v8/serialization/serialization_tag.h", "core/v8/serialization/serialization_tag.h",
......
// Copyright 2018 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/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/core/imagebitmap/image_bitmap.h"
#include "third_party/blink/renderer/core/messaging/post_message_options.h"
namespace blink {
scoped_refptr<SerializedScriptValue> PostMessageHelper::SerializeMessageByMove(
v8::Isolate* isolate,
const ScriptValue& message,
const PostMessageOptions& options,
Transferables& transferables,
ExceptionState& exception_state) {
if (options.hasTransfer() && !options.transfer().IsEmpty()) {
if (!SerializedScriptValue::ExtractTransferables(
isolate, options.transfer(), transferables, exception_state)) {
return nullptr;
}
}
SerializedScriptValue::SerializeOptions serialize_options;
serialize_options.transferables = &transferables;
scoped_refptr<SerializedScriptValue> serialized_message =
SerializedScriptValue::Serialize(isolate, message.V8Value(),
serialize_options, exception_state);
if (exception_state.HadException())
return nullptr;
serialized_message->UnregisterMemoryAllocatedWithCurrentScriptContext();
return serialized_message;
}
scoped_refptr<SerializedScriptValue> PostMessageHelper::SerializeMessageByCopy(
v8::Isolate* isolate,
const ScriptValue& message,
const PostMessageOptions& options,
Transferables& transferables,
ExceptionState& exception_state) {
if (options.hasTransfer() && !options.transfer().IsEmpty()) {
if (!SerializedScriptValue::ExtractTransferables(
isolate, options.transfer(), transferables, exception_state)) {
return nullptr;
}
}
// Copying the transferables by move semantics is not supported for the
// caller of this function so emulate it by copy-and-neuter semantics
// that sends array buffers and image
// bitmaps via structured clone and then neuters the original objects.
// Clear references to array buffers and image bitmaps from transferables
// so that the serializer can consider the array buffers as
// non-transferable and serialize them into the message.
ArrayBufferArray transferable_array_buffers =
SerializedScriptValue::ExtractNonSharedArrayBuffers(transferables);
ImageBitmapArray transferable_image_bitmaps = transferables.image_bitmaps;
transferables.image_bitmaps.clear();
SerializedScriptValue::SerializeOptions serialize_options;
serialize_options.transferables = &transferables;
scoped_refptr<SerializedScriptValue> serialized_message =
SerializedScriptValue::Serialize(isolate, message.V8Value(),
serialize_options, exception_state);
if (exception_state.HadException())
return nullptr;
// Neuter the original array buffers on the sender context.
SerializedScriptValue::TransferArrayBufferContents(
isolate, transferable_array_buffers, exception_state);
if (exception_state.HadException())
return nullptr;
// Neuter the original image bitmaps on the sender context.
SerializedScriptValue::TransferImageBitmapContents(
isolate, transferable_image_bitmaps, exception_state);
if (exception_state.HadException())
return nullptr;
serialized_message->UnregisterMemoryAllocatedWithCurrentScriptContext();
return serialized_message;
}
} // namespace blink
// Copyright 2018 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_BINDINGS_CORE_V8_SERIALIZATION_POST_MESSAGE_HELPER_H_
#define THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_SERIALIZATION_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"
#include "v8/include/v8.h"
namespace blink {
class ExceptionState;
class PostMessageOptions;
class ScriptValue;
class SerializedScriptValue;
class Transferables;
class CORE_EXPORT PostMessageHelper {
STATIC_ONLY(PostMessageHelper);
public:
static scoped_refptr<SerializedScriptValue> SerializeMessageByMove(
v8::Isolate*,
const ScriptValue& message,
const PostMessageOptions& options,
Transferables& transferables,
ExceptionState&);
static scoped_refptr<SerializedScriptValue> SerializeMessageByCopy(
v8::Isolate*,
const ScriptValue& message,
const PostMessageOptions& options,
Transferables& transferables,
ExceptionState&);
};
} // namespace blink
#endif
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <memory> #include <memory>
#include "third_party/blink/renderer/bindings/core/v8/serialization/post_message_helper.h"
#include "third_party/blink/renderer/bindings/core/v8/window_proxy_manager.h" #include "third_party/blink/renderer/bindings/core/v8/window_proxy_manager.h"
#include "third_party/blink/renderer/core/dom/document.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/events/message_event.h"
...@@ -135,22 +136,12 @@ void DOMWindow::postMessage(LocalDOMWindow* incumbent_window, ...@@ -135,22 +136,12 @@ void DOMWindow::postMessage(LocalDOMWindow* incumbent_window,
v8::Isolate* isolate = window_proxy_manager_->GetIsolate(); v8::Isolate* isolate = window_proxy_manager_->GetIsolate();
Transferables transferables; Transferables transferables;
if (options.hasTransfer() && !options.transfer().IsEmpty()) {
if (!SerializedScriptValue::ExtractTransferables(
isolate, options.transfer(), transferables, exception_state)) {
return;
}
}
SerializedScriptValue::SerializeOptions serialize_options;
serialize_options.transferables = &transferables;
scoped_refptr<SerializedScriptValue> serialized_message = scoped_refptr<SerializedScriptValue> serialized_message =
SerializedScriptValue::Serialize(isolate, message.V8Value(), PostMessageHelper::SerializeMessageByMove(isolate, message, options,
serialize_options, exception_state); transferables, exception_state);
if (exception_state.HadException()) if (exception_state.HadException())
return; return;
DCHECK(serialized_message);
serialized_message->UnregisterMemoryAllocatedWithCurrentScriptContext();
DoPostMessage(std::move(serialized_message), transferables.message_ports, DoPostMessage(std::move(serialized_message), transferables.message_ports,
options, incumbent_window, exception_state); options, incumbent_window, exception_state);
} }
......
...@@ -31,14 +31,14 @@ ...@@ -31,14 +31,14 @@
#include "mojo/public/cpp/base/big_buffer_mojom_traits.h" #include "mojo/public/cpp/base/big_buffer_mojom_traits.h"
#include "third_party/blink/public/platform/task_type.h" #include "third_party/blink/public/platform/task_type.h"
#include "third_party/blink/public/platform/web_string.h" #include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/renderer/bindings/core/v8/serialization/serialized_script_value.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_factory.h"
#include "third_party/blink/renderer/core/events/message_event.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/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h" #include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/use_counter.h" #include "third_party/blink/renderer/core/frame/use_counter.h"
#include "third_party/blink/renderer/core/inspector/thread_debugger.h" #include "third_party/blink/renderer/core/inspector/thread_debugger.h"
#include "third_party/blink/renderer/core/messaging/blink_transferable_message_struct_traits.h" #include "third_party/blink/renderer/core/messaging/blink_transferable_message_struct_traits.h"
#include "third_party/blink/renderer/core/messaging/post_message_options.h"
#include "third_party/blink/renderer/core/workers/worker_global_scope.h" #include "third_party/blink/renderer/core/workers/worker_global_scope.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.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/bindings/script_state.h"
...@@ -63,20 +63,30 @@ MessagePort::~MessagePort() { ...@@ -63,20 +63,30 @@ MessagePort::~MessagePort() {
} }
void MessagePort::postMessage(ScriptState* script_state, void MessagePort::postMessage(ScriptState* script_state,
scoped_refptr<SerializedScriptValue> message, const ScriptValue& message,
const MessagePortArray& ports, Vector<ScriptValue>& transfer,
ExceptionState& exception_state) { ExceptionState& exception_state) {
if (!IsEntangled()) if (!IsEntangled())
return; return;
DCHECK(GetExecutionContext()); DCHECK(GetExecutionContext());
DCHECK(!IsNeutered()); DCHECK(!IsNeutered());
PostMessageOptions options;
BlinkTransferableMessage msg; BlinkTransferableMessage msg;
msg.message = message; Transferables transferables;
if (!transfer.IsEmpty())
options.setTransfer(transfer);
msg.message = PostMessageHelper::SerializeMessageByMove(
script_state->GetIsolate(), message, options, transferables,
exception_state);
if (exception_state.HadException())
return;
DCHECK(msg.message);
// Make sure we aren't connected to any of the passed-in ports. // Make sure we aren't connected to any of the passed-in ports.
for (unsigned i = 0; i < ports.size(); ++i) { for (unsigned i = 0; i < transferables.message_ports.size(); ++i) {
if (ports[i] == this) { if (transferables.message_ports[i] == this) {
exception_state.ThrowDOMException( exception_state.ThrowDOMException(
DOMExceptionCode::kDataCloneError, DOMExceptionCode::kDataCloneError,
"Port at index " + String::Number(i) + " contains the source port."); "Port at index " + String::Number(i) + " contains the source port.");
...@@ -84,7 +94,8 @@ void MessagePort::postMessage(ScriptState* script_state, ...@@ -84,7 +94,8 @@ void MessagePort::postMessage(ScriptState* script_state,
} }
} }
msg.ports = MessagePort::DisentanglePorts( msg.ports = MessagePort::DisentanglePorts(
ExecutionContext::From(script_state), ports, exception_state); ExecutionContext::From(script_state), transferables.message_ports,
exception_state);
if (exception_state.HadException()) if (exception_state.HadException())
return; return;
...@@ -92,7 +103,7 @@ void MessagePort::postMessage(ScriptState* script_state, ...@@ -92,7 +103,7 @@ void MessagePort::postMessage(ScriptState* script_state,
if (debugger) if (debugger)
msg.sender_stack_trace_id = debugger->StoreCurrentStackTrace("postMessage"); msg.sender_stack_trace_id = debugger->StoreCurrentStackTrace("postMessage");
if (message->IsLockedToAgentCluster()) { if (msg.message->IsLockedToAgentCluster()) {
msg.locked_agent_cluster_id = GetExecutionContext()->GetAgentClusterID(); msg.locked_agent_cluster_id = GetExecutionContext()->GetAgentClusterID();
} else { } else {
msg.locked_agent_cluster_id = base::nullopt; msg.locked_agent_cluster_id = base::nullopt;
......
...@@ -47,7 +47,6 @@ namespace blink { ...@@ -47,7 +47,6 @@ namespace blink {
class ExceptionState; class ExceptionState;
class ExecutionContext; class ExecutionContext;
class ScriptState; class ScriptState;
class SerializedScriptValue;
class CORE_EXPORT MessagePort : public EventTargetWithInlineData, class CORE_EXPORT MessagePort : public EventTargetWithInlineData,
public mojo::MessageReceiver, public mojo::MessageReceiver,
...@@ -61,10 +60,9 @@ class CORE_EXPORT MessagePort : public EventTargetWithInlineData, ...@@ -61,10 +60,9 @@ class CORE_EXPORT MessagePort : public EventTargetWithInlineData,
~MessagePort() override; ~MessagePort() override;
void postMessage(ScriptState*, void postMessage(ScriptState*,
scoped_refptr<SerializedScriptValue> message, const ScriptValue& message,
const MessagePortArray&, Vector<ScriptValue>& transfer,
ExceptionState&); ExceptionState&);
static bool CanTransferArrayBuffersAndImageBitmaps() { return true; }
void start(); void start();
void close(); void close();
......
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
ActiveScriptWrappable, ActiveScriptWrappable,
Exposed=(Window,Worker,AudioWorklet) Exposed=(Window,Worker,AudioWorklet)
] interface MessagePort : EventTarget { ] interface MessagePort : EventTarget {
[PostMessage, RaisesException, Measure] void postMessage(any message, optional sequence<object> transfer = []); [CallWith=ScriptState, RaisesException, Measure] void postMessage(any message, optional sequence<object> transfer = []);
[Measure] void start(); [Measure] void start();
[Measure] void close(); [Measure] void close();
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "third_party/blink/public/platform/web_content_settings_client.h" #include "third_party/blink/public/platform/web_content_settings_client.h"
#include "third_party/blink/public/platform/web_layer_tree_view.h" #include "third_party/blink/public/platform/web_layer_tree_view.h"
#include "third_party/blink/public/web/web_local_frame_client.h" #include "third_party/blink/public/web/web_local_frame_client.h"
#include "third_party/blink/renderer/bindings/core/v8/serialization/post_message_helper.h"
#include "third_party/blink/renderer/core/core_initializer.h" #include "third_party/blink/renderer/core/core_initializer.h"
#include "third_party/blink/renderer/core/dom/document.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/events/message_event.h"
...@@ -19,6 +20,7 @@ ...@@ -19,6 +20,7 @@
#include "third_party/blink/renderer/core/frame/use_counter.h" #include "third_party/blink/renderer/core/frame/use_counter.h"
#include "third_party/blink/renderer/core/frame/web_local_frame_impl.h" #include "third_party/blink/renderer/core/frame/web_local_frame_impl.h"
#include "third_party/blink/renderer/core/inspector/main_thread_debugger.h" #include "third_party/blink/renderer/core/inspector/main_thread_debugger.h"
#include "third_party/blink/renderer/core/messaging/post_message_options.h"
#include "third_party/blink/renderer/core/origin_trials/origin_trial_context.h" #include "third_party/blink/renderer/core/origin_trials/origin_trial_context.h"
#include "third_party/blink/renderer/core/page/page.h" #include "third_party/blink/renderer/core/page/page.h"
#include "third_party/blink/renderer/core/probe/core_probes.h" #include "third_party/blink/renderer/core/probe/core_probes.h"
...@@ -110,19 +112,34 @@ DedicatedWorker::~DedicatedWorker() { ...@@ -110,19 +112,34 @@ DedicatedWorker::~DedicatedWorker() {
} }
void DedicatedWorker::postMessage(ScriptState* script_state, void DedicatedWorker::postMessage(ScriptState* script_state,
scoped_refptr<SerializedScriptValue> message, const ScriptValue& message,
const MessagePortArray& ports, Vector<ScriptValue>& transfer,
ExceptionState& exception_state) { ExceptionState& exception_state) {
DCHECK(GetExecutionContext()->IsContextThread()); DCHECK(GetExecutionContext()->IsContextThread());
PostMessageOptions options;
if (!transfer.IsEmpty())
options.setTransfer(transfer);
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);
// Disentangle the port in preparation for sending it to the remote context. // Disentangle the port in preparation for sending it to the remote context.
auto channels = MessagePort::DisentanglePorts( auto channels = MessagePort::DisentanglePorts(
ExecutionContext::From(script_state), ports, exception_state); ExecutionContext::From(script_state), transferables.message_ports,
exception_state);
if (exception_state.HadException()) if (exception_state.HadException())
return; return;
v8_inspector::V8StackTraceId stack_id = v8_inspector::V8StackTraceId stack_id =
ThreadDebugger::From(script_state->GetIsolate()) ThreadDebugger::From(script_state->GetIsolate())
->StoreCurrentStackTrace("Worker.postMessage"); ->StoreCurrentStackTrace("Worker.postMessage");
context_proxy_->PostMessageToWorkerGlobalScope(std::move(message), context_proxy_->PostMessageToWorkerGlobalScope(std::move(serialized_message),
std::move(channels), stack_id); std::move(channels), stack_id);
} }
......
...@@ -55,10 +55,9 @@ class CORE_EXPORT DedicatedWorker final ...@@ -55,10 +55,9 @@ class CORE_EXPORT DedicatedWorker final
~DedicatedWorker() override; ~DedicatedWorker() override;
void postMessage(ScriptState*, void postMessage(ScriptState*,
scoped_refptr<SerializedScriptValue> message, const ScriptValue& message,
const MessagePortArray&, Vector<ScriptValue>& transfer,
ExceptionState&); ExceptionState&);
static bool CanTransferArrayBuffersAndImageBitmaps() { return true; }
void terminate(); void terminate();
BeginFrameProviderParams CreateBeginFrameProviderParams(); BeginFrameProviderParams CreateBeginFrameProviderParams();
......
...@@ -31,11 +31,13 @@ ...@@ -31,11 +31,13 @@
#include "third_party/blink/renderer/core/workers/dedicated_worker_global_scope.h" #include "third_party/blink/renderer/core/workers/dedicated_worker_global_scope.h"
#include <memory> #include <memory>
#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/serialized_script_value.h"
#include "third_party/blink/renderer/bindings/core/v8/worker_or_worklet_script_controller.h" #include "third_party/blink/renderer/bindings/core/v8/worker_or_worklet_script_controller.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h" #include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/frame/csp/content_security_policy.h" #include "third_party/blink/renderer/core/frame/csp/content_security_policy.h"
#include "third_party/blink/renderer/core/inspector/thread_debugger.h" #include "third_party/blink/renderer/core/inspector/thread_debugger.h"
#include "third_party/blink/renderer/core/messaging/post_message_options.h"
#include "third_party/blink/renderer/core/origin_trials/origin_trial_context.h" #include "third_party/blink/renderer/core/origin_trials/origin_trial_context.h"
#include "third_party/blink/renderer/core/script/fetch_client_settings_object_snapshot.h" #include "third_party/blink/renderer/core/script/fetch_client_settings_object_snapshot.h"
#include "third_party/blink/renderer/core/script/modulator.h" #include "third_party/blink/renderer/core/script/modulator.h"
...@@ -82,20 +84,32 @@ void DedicatedWorkerGlobalScope::ImportModuleScript( ...@@ -82,20 +84,32 @@ void DedicatedWorkerGlobalScope::ImportModuleScript(
new WorkerModuleTreeClient(modulator)); new WorkerModuleTreeClient(modulator));
} }
void DedicatedWorkerGlobalScope::postMessage( void DedicatedWorkerGlobalScope::postMessage(ScriptState* script_state,
ScriptState* script_state, const ScriptValue& message,
scoped_refptr<SerializedScriptValue> message, Vector<ScriptValue>& transfer,
const MessagePortArray& ports, ExceptionState& exception_state) {
ExceptionState& exception_state) { PostMessageOptions options;
if (!transfer.IsEmpty())
options.setTransfer(transfer);
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);
// Disentangle the port in preparation for sending it to the remote context. // Disentangle the port in preparation for sending it to the remote context.
auto channels = MessagePort::DisentanglePorts( auto channels = MessagePort::DisentanglePorts(
ExecutionContext::From(script_state), ports, exception_state); ExecutionContext::From(script_state), transferables.message_ports,
exception_state);
if (exception_state.HadException()) if (exception_state.HadException())
return; return;
ThreadDebugger* debugger = ThreadDebugger::From(script_state->GetIsolate()); ThreadDebugger* debugger = ThreadDebugger::From(script_state->GetIsolate());
v8_inspector::V8StackTraceId stack_id = v8_inspector::V8StackTraceId stack_id =
debugger->StoreCurrentStackTrace("postMessage"); debugger->StoreCurrentStackTrace("postMessage");
WorkerObjectProxy().PostMessageToWorkerObject(std::move(message), WorkerObjectProxy().PostMessageToWorkerObject(std::move(serialized_message),
std::move(channels), stack_id); std::move(channels), stack_id);
} }
......
...@@ -65,12 +65,10 @@ class CORE_EXPORT DedicatedWorkerGlobalScope final : public WorkerGlobalScope { ...@@ -65,12 +65,10 @@ class CORE_EXPORT DedicatedWorkerGlobalScope final : public WorkerGlobalScope {
network::mojom::FetchCredentialsMode) override; network::mojom::FetchCredentialsMode) override;
void postMessage(ScriptState*, void postMessage(ScriptState*,
scoped_refptr<SerializedScriptValue>, const ScriptValue& message,
const MessagePortArray&, Vector<ScriptValue>& transfer,
ExceptionState&); ExceptionState&);
static bool CanTransferArrayBuffersAndImageBitmaps() { return true; }
DEFINE_ATTRIBUTE_EVENT_LISTENER(message); DEFINE_ATTRIBUTE_EVENT_LISTENER(message);
DEFINE_ATTRIBUTE_EVENT_LISTENER(messageerror); DEFINE_ATTRIBUTE_EVENT_LISTENER(messageerror);
......
...@@ -34,7 +34,7 @@ ...@@ -34,7 +34,7 @@
Global=(Worker,DedicatedWorker), Global=(Worker,DedicatedWorker),
Exposed=DedicatedWorker Exposed=DedicatedWorker
] interface DedicatedWorkerGlobalScope : WorkerGlobalScope { ] interface DedicatedWorkerGlobalScope : WorkerGlobalScope {
[PostMessage, RaisesException] void postMessage(any message, optional sequence<object> transfer = []); [CallWith=ScriptState, RaisesException] void postMessage(any message, optional sequence<object> transfer = []);
void close(); void close();
......
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
] interface Worker : EventTarget { ] interface Worker : EventTarget {
void terminate(); void terminate();
[PostMessage, RaisesException] void postMessage(any message, optional sequence<object> transfer); [CallWith=ScriptState, RaisesException] void postMessage(any message, optional sequence<object> transfer=[]);
attribute EventHandler onmessage; attribute EventHandler onmessage;
}; };
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
readonly attribute USVString url; readonly attribute USVString url;
readonly attribute DOMString id; readonly attribute DOMString id;
readonly attribute ClientType type; readonly attribute ClientType type;
[PostMessage, RaisesException, CallWith=ScriptState] void postMessage(any message, optional sequence<object> transfer); [CallWith=ScriptState, RaisesException] void postMessage(any message, optional sequence<object> transfer = []);
// FIXME: frameType is non-standard, see https://crbug.com/697110 // FIXME: frameType is non-standard, see https://crbug.com/697110
[CallWith=ScriptState] readonly attribute ContextFrameType frameType; [CallWith=ScriptState] readonly attribute ContextFrameType frameType;
......
...@@ -34,10 +34,12 @@ ...@@ -34,10 +34,12 @@
#include "third_party/blink/public/mojom/service_worker/service_worker_state.mojom-blink.h" #include "third_party/blink/public/mojom/service_worker/service_worker_state.mojom-blink.h"
#include "third_party/blink/public/platform/web_string.h" #include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/renderer/bindings/core/v8/callback_promise_adapter.h" #include "third_party/blink/renderer/bindings/core/v8/callback_promise_adapter.h"
#include "third_party/blink/renderer/bindings/core/v8/serialization/post_message_helper.h"
#include "third_party/blink/renderer/core/dom/events/event.h" #include "third_party/blink/renderer/core/dom/events/event.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h" #include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/messaging/blink_transferable_message.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/message_port.h"
#include "third_party/blink/renderer/core/messaging/post_message_options.h"
#include "third_party/blink/renderer/modules/event_target_modules.h" #include "third_party/blink/renderer/modules/event_target_modules.h"
#include "third_party/blink/renderer/modules/service_worker/service_worker_container_client.h" #include "third_party/blink/renderer/modules/service_worker/service_worker_container_client.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h" #include "third_party/blink/renderer/platform/bindings/exception_state.h"
...@@ -50,8 +52,8 @@ const AtomicString& ServiceWorker::InterfaceName() const { ...@@ -50,8 +52,8 @@ const AtomicString& ServiceWorker::InterfaceName() const {
} }
void ServiceWorker::postMessage(ScriptState* script_state, void ServiceWorker::postMessage(ScriptState* script_state,
scoped_refptr<SerializedScriptValue> message, const ScriptValue& message,
const MessagePortArray& ports, Vector<ScriptValue>& transfer,
ExceptionState& exception_state) { ExceptionState& exception_state) {
ServiceWorkerContainerClient* client = ServiceWorkerContainerClient* client =
ServiceWorkerContainerClient::From(GetExecutionContext()); ServiceWorkerContainerClient::From(GetExecutionContext());
...@@ -62,10 +64,24 @@ void ServiceWorker::postMessage(ScriptState* script_state, ...@@ -62,10 +64,24 @@ void ServiceWorker::postMessage(ScriptState* script_state,
return; return;
} }
PostMessageOptions options;
Transferables transferables;
if (!transfer.IsEmpty())
options.setTransfer(transfer);
scoped_refptr<SerializedScriptValue> serialized_message =
PostMessageHelper::SerializeMessageByCopy(script_state->GetIsolate(),
message, options, transferables,
exception_state);
if (exception_state.HadException())
return;
DCHECK(serialized_message);
BlinkTransferableMessage msg; BlinkTransferableMessage msg;
msg.message = message; msg.message = serialized_message;
msg.ports = MessagePort::DisentanglePorts( msg.ports = MessagePort::DisentanglePorts(
ExecutionContext::From(script_state), ports, exception_state); ExecutionContext::From(script_state), transferables.message_ports,
exception_state);
if (exception_state.HadException()) if (exception_state.HadException())
return; return;
......
...@@ -63,10 +63,9 @@ class MODULES_EXPORT ServiceWorker final ...@@ -63,10 +63,9 @@ class MODULES_EXPORT ServiceWorker final
EAGERLY_FINALIZE(); EAGERLY_FINALIZE();
void postMessage(ScriptState*, void postMessage(ScriptState*,
scoped_refptr<SerializedScriptValue> message, const ScriptValue& message,
const MessagePortArray&, Vector<ScriptValue>& transfer,
ExceptionState&); ExceptionState&);
static bool CanTransferArrayBuffersAndImageBitmaps() { return false; }
String scriptURL() const; String scriptURL() const;
String state() const; String state() const;
......
...@@ -43,7 +43,7 @@ enum ServiceWorkerState { ...@@ -43,7 +43,7 @@ enum ServiceWorkerState {
SecureContext SecureContext
] interface ServiceWorker : EventTarget { ] interface ServiceWorker : EventTarget {
[PostMessage, RaisesException] void postMessage(any message, optional sequence<object> transfer); [CallWith=ScriptState, RaisesException] void postMessage(any message, optional sequence<object> transfer = []);
readonly attribute USVString scriptURL; readonly attribute USVString scriptURL;
readonly attribute ServiceWorkerState state; readonly attribute ServiceWorkerState state;
......
...@@ -11,10 +11,11 @@ ...@@ -11,10 +11,11 @@
#include "third_party/blink/public/mojom/service_worker/service_worker_client.mojom-blink.h" #include "third_party/blink/public/mojom/service_worker/service_worker_client.mojom-blink.h"
#include "third_party/blink/public/platform/web_string.h" #include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/renderer/bindings/core/v8/callback_promise_adapter.h" #include "third_party/blink/renderer/bindings/core/v8/callback_promise_adapter.h"
#include "third_party/blink/renderer/bindings/core/v8/serialization/serialized_script_value.h" #include "third_party/blink/renderer/bindings/core/v8/serialization/post_message_helper.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h" #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/frame/use_counter.h"
#include "third_party/blink/renderer/core/messaging/blink_transferable_message.h" #include "third_party/blink/renderer/core/messaging/blink_transferable_message.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_client.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.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/bindings/script_state.h"
...@@ -86,15 +87,28 @@ String ServiceWorkerClient::frameType(ScriptState* script_state) const { ...@@ -86,15 +87,28 @@ String ServiceWorkerClient::frameType(ScriptState* script_state) const {
return String(); return String();
} }
void ServiceWorkerClient::postMessage( void ServiceWorkerClient::postMessage(ScriptState* script_state,
ScriptState* script_state, const ScriptValue& message,
scoped_refptr<SerializedScriptValue> message, Vector<ScriptValue>& transfer,
const MessagePortArray& ports, ExceptionState& exception_state) {
ExceptionState& exception_state) {
ExecutionContext* context = ExecutionContext::From(script_state); ExecutionContext* context = ExecutionContext::From(script_state);
PostMessageOptions options;
Transferables transferables;
if (!transfer.IsEmpty())
options.setTransfer(transfer);
scoped_refptr<SerializedScriptValue> serialized_message =
PostMessageHelper::SerializeMessageByCopy(script_state->GetIsolate(),
message, options, transferables,
exception_state);
if (exception_state.HadException())
return;
DCHECK(serialized_message);
BlinkTransferableMessage msg; BlinkTransferableMessage msg;
msg.message = message; msg.message = serialized_message;
msg.ports = MessagePort::DisentanglePorts(context, ports, exception_state); msg.ports = MessagePort::DisentanglePorts(
context, transferables.message_ports, exception_state);
if (exception_state.HadException()) if (exception_state.HadException())
return; return;
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
#include <memory> #include <memory>
#include "third_party/blink/public/platform/modules/service_worker/web_service_worker_clients_info.h" #include "third_party/blink/public/platform/modules/service_worker/web_service_worker_clients_info.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h" #include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/bindings/core/v8/serialization/serialized_script_value.h"
#include "third_party/blink/renderer/modules/modules_export.h" #include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h" #include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/heap/handle.h" #include "third_party/blink/renderer/platform/heap/handle.h"
...@@ -38,12 +37,10 @@ class MODULES_EXPORT ServiceWorkerClient : public ScriptWrappable { ...@@ -38,12 +37,10 @@ class MODULES_EXPORT ServiceWorkerClient : public ScriptWrappable {
String frameType(ScriptState*) const; String frameType(ScriptState*) const;
String id() const { return uuid_; } String id() const { return uuid_; }
void postMessage(ScriptState*, void postMessage(ScriptState*,
scoped_refptr<SerializedScriptValue> message, const ScriptValue& message,
const MessagePortArray&, Vector<ScriptValue>& transfer,
ExceptionState&); ExceptionState&);
static bool CanTransferArrayBuffersAndImageBitmaps() { return false; }
protected: protected:
explicit ServiceWorkerClient(const WebServiceWorkerClientInfo&); explicit ServiceWorkerClient(const WebServiceWorkerClientInfo&);
......
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