Index: third_party/WebKit/Source/bindings/templates/methods.cpp.tmpl |
diff --git a/third_party/WebKit/Source/bindings/templates/methods.cpp.tmpl b/third_party/WebKit/Source/bindings/templates/methods.cpp.tmpl |
index a27c7725120fc9071f1412e353214e8dd55a3d17..21f232013878cff7dd3c688566c36d64c9e81ade 100644 |
--- a/third_party/WebKit/Source/bindings/templates/methods.cpp.tmpl |
+++ b/third_party/WebKit/Source/bindings/templates/methods.cpp.tmpl |
@@ -435,6 +435,7 @@ static void postMessageImpl(const char* interfaceName, {{cpp_class}}* instance, |
exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments({{method.number_of_required_arguments}}, info.Length())); |
return; |
} |
+ |
Transferables transferables; |
if (info.Length() > 1) { |
const int transferablesArgIndex = 1; |
@@ -442,9 +443,34 @@ static void postMessageImpl(const char* interfaceName, {{cpp_class}}* instance, |
return; |
} |
} |
- RefPtr<SerializedScriptValue> message = SerializedScriptValue::serialize(info.GetIsolate(), info[0], &transferables, nullptr, exceptionState); |
- if (exceptionState.hadException()) |
- return; |
+ |
+ RefPtr<SerializedScriptValue> message; |
+ if (instance->canTransferArrayBuffer()) { |
+ // This instance supports sending array buffers by move semantics. |
+ message = SerializedScriptValue::serialize(info.GetIsolate(), info[0], &transferables, nullptr, exceptionState); |
+ if (exceptionState.hadException()) |
+ return; |
+ } else { |
+ // This instance doesn't support sending array buffers by move |
+ // semantics. Emulate it by copy-and-neuter semantics that sends array |
+ // buffers by copy semantics and then neuters the original array |
+ // buffers. |
+ |
+ // Clear references to array buffers from transferables so that the |
+ // serializer can consider the array buffers as non-transferable and |
+ // copy them into the message. |
+ ArrayBufferArray transferableArrayBuffers = transferables.arrayBuffers; |
+ transferables.arrayBuffers.clear(); |
+ message = SerializedScriptValue::serialize(info.GetIsolate(), info[0], &transferables, nullptr, exceptionState); |
+ if (exceptionState.hadException()) |
+ return; |
+ |
+ // Neuter the original array buffers on the sender context. |
+ SerializedScriptValue::transferArrayBufferContents(info.GetIsolate(), transferableArrayBuffers, exceptionState); |
+ if (exceptionState.hadException()) |
+ return; |
+ } |
+ |
// FIXME: Only pass context/exceptionState if instance really requires it. |
ExecutionContext* context = currentExecutionContext(info.GetIsolate()); |
instance->postMessage(context, message.release(), transferables.messagePorts, exceptionState); |