Issue #2197 - Part 4: Expose structuredClone in Sandbox

Partially based on https://bugzilla.mozilla.org/show_bug.cgi?id=1734320
This commit is contained in:
FranklinDM 2023-04-07 21:45:15 +08:00 committed by roytam1
commit bbcfb62753
10 changed files with 129 additions and 67 deletions

View file

@ -47,6 +47,7 @@
#include "mozilla/dom/HTMLTemplateElement.h"
#include "mozilla/dom/ipc/BlobChild.h"
#include "mozilla/dom/ipc/BlobParent.h"
#include "mozilla/dom/MessagePort.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/dom/TabParent.h"
@ -10023,3 +10024,41 @@ nsContentUtils::CreateJSValueFromSequenceOfObject(JSContext* aCx,
aValue.setObject(*array);
return NS_OK;
}
/* static */
void nsContentUtils::StructuredClone(JSContext* aCx,
nsIGlobalObject* aGlobal,
JS::Handle<JS::Value> aValue,
const StructuredSerializeOptions& aOptions,
JS::MutableHandle<JS::Value> aRv,
ErrorResult& aError) {
JS::Rooted<JS::Value> transferArray(aCx, JS::UndefinedValue());
aError = nsContentUtils::CreateJSValueFromSequenceOfObject(
aCx, aOptions.mTransfer, &transferArray);
if (NS_WARN_IF(aError.Failed())) {
return;
}
JS::CloneDataPolicy clonePolicy;
// FIXME: Uncomment once bug 1609990 and bug 1611855 lands.
//clonePolicy.allowIntraClusterClonableSharedObjects();
//clonePolicy.allowSharedMemoryObjects();
StructuredCloneHolder holder(StructuredCloneHolder::CloningSupported,
StructuredCloneHolder::TransferringSupported,
JS::StructuredCloneScope::SameProcessDifferentThread);
holder.Write(aCx, aValue, transferArray, clonePolicy, aError);
if (NS_WARN_IF(aError.Failed())) {
return;
}
// TODO: Pass clonePolicy.
//holder.Read(this, aCx, aRv, clonePolicy, aError);
holder.Read(aGlobal, aCx, aRv, aError);
if (NS_WARN_IF(aError.Failed())) {
return;
}
nsTArray<RefPtr<MessagePort>> ports = holder.TakeTransferredPorts();
Unused << ports;
}