Issue #2197 - Part 1b: Transferables should be arrays of objects

Based on https://bugzilla.mozilla.org/show_bug.cgi?id=1336020
This commit is contained in:
FranklinDM 2023-04-07 12:04:38 +08:00 committed by roytam1
commit 47147d58b0
24 changed files with 118 additions and 129 deletions

View file

@ -396,49 +396,37 @@ MessagePort::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
void
MessagePort::PostMessage(JSContext* aCx,
JS::Handle<JS::Value> aMessage,
const Sequence<JS::Value>& aTransferable,
const Sequence<JSObject*>& aTransferable,
ErrorResult& aRv)
{
// We *must* clone the data here, or the JS::Value could be modified
// by script
JS::Rooted<JS::Value> transferable(aCx, JS::UndefinedValue());
if (!aTransferable.IsEmpty()) {
// Here we want to check if the transerable object list contains
// this port. No other checks are done.
for (const JS::Value& value : aTransferable) {
if (!value.isObject()) {
continue;
}
JS::Rooted<JSObject*> object(aCx, &value.toObject());
MessagePort* port = nullptr;
nsresult rv = UNWRAP_OBJECT(MessagePort, &object, port);
if (NS_FAILED(rv)) {
continue;
}
if (port == this) {
aRv.Throw(NS_ERROR_DOM_DATA_CLONE_ERR);
return;
}
// Here we want to check if the transferable object list contains
// this port.
for (uint32_t i = 0; i < aTransferable.Length(); ++i) {
JSObject* rawObject = aTransferable[i];
if (!rawObject) {
continue;
}
// The input sequence only comes from the generated bindings code, which
// ensures it is rooted.
JS::HandleValueArray elements =
JS::HandleValueArray::fromMarkedLocation(aTransferable.Length(),
aTransferable.Elements());
JS::Rooted<JSObject*> object(aCx, rawObject);
JSObject* array =
JS_NewArrayObject(aCx, elements);
if (!array) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
MessagePort* port = nullptr;
nsresult rv = UNWRAP_OBJECT(MessagePort, &object, port);
if (NS_SUCCEEDED(rv) && port == this) {
aRv.Throw(NS_ERROR_DOM_DATA_CLONE_ERR);
return;
}
}
transferable.setObject(*array);
JS::Rooted<JS::Value> transferable(aCx, JS::UndefinedValue());
aRv = nsContentUtils::CreateJSValueFromSequenceOfObject(aCx,
aTransferable,
&transferable);
if (NS_WARN_IF(aRv.Failed())) {
return;
}
RefPtr<SharedMessagePortMessage> data = new SharedMessagePortMessage();