Merge remote-tracking branch 'origin/tracking' into custom

This commit is contained in:
roytam1 2023-04-10 07:27:58 +08:00
commit 20b787a4df
37 changed files with 523 additions and 153 deletions

View file

@ -326,6 +326,43 @@ SandboxCreateFetch(JSContext* cx, HandleObject obj)
dom::HeadersBinding::GetConstructorObject(cx);
}
static bool SandboxStructuredClone(JSContext* cx, unsigned argc, Value* vp) {
CallArgs args = CallArgsFromVp(argc, vp);
if (!args.requireAtLeast(cx, "structuredClone", 1)) {
return false;
}
RootedDictionary<dom::StructuredSerializeOptions> options(cx);
if (!options.Init(cx, args.hasDefined(1) ? args[1] : JS::NullHandleValue,
"Argument 2", false)) {
return false;
}
nsIGlobalObject* global = xpc::NativeGlobal(JS::CurrentGlobalOrNull(cx));
if (!global) {
JS_ReportErrorASCII(cx, "structuredClone: Missing global");
return false;
}
JS::Rooted<JS::Value> result(cx);
ErrorResult rv;
nsContentUtils::StructuredClone(cx, global, args[0], options, &result, rv);
if (rv.MaybeSetPendingException(cx)) {
return false;
}
args.rval().set(result);
return true;
}
static bool SandboxCreateStructuredClone(JSContext* cx, HandleObject obj) {
MOZ_ASSERT(JS_IsGlobalObject(obj));
return JS_DefineFunction(cx, obj, "structuredClone", SandboxStructuredClone,
1, 0);
}
static bool
SandboxIsProxy(JSContext* cx, unsigned argc, Value* vp)
{
@ -931,6 +968,8 @@ xpc::GlobalProperties::Parse(JSContext* cx, JS::HandleObject obj)
#endif
} else if (!strcmp(name.ptr(), "fetch")) {
fetch = true;
} else if (!strcmp(name.ptr(), "structuredClone")) {
structuredClone = true;
} else if (!strcmp(name.ptr(), "caches")) {
caches = true;
} else if (!strcmp(name.ptr(), "FileReader")) {
@ -1006,6 +1045,9 @@ xpc::GlobalProperties::Define(JSContext* cx, JS::HandleObject obj)
if (fetch && !SandboxCreateFetch(cx, obj))
return false;
if (structuredClone && !SandboxCreateStructuredClone(cx, obj))
return false;
if (caches && !dom::cache::CacheStorage::DefineCaches(cx, obj))
return false;

View file

@ -2879,6 +2879,7 @@ struct GlobalProperties {
bool crypto : 1;
bool rtcIdentityProvider : 1;
bool fetch : 1;
bool structuredClone : 1;
bool caches : 1;
bool fileReader: 1;
private:

View file

@ -0,0 +1,25 @@
function run_test() {
var sb = new Cu.Sandbox('http://www.example.com',
{ wantGlobalProperties: ["structuredClone"] });
sb.equal = equal;
sb.testing = Components.utils.cloneInto({xyz: 123}, sb);
Cu.evalInSandbox(`
equal(structuredClone("abc"), "abc");
var obj = {a: 1};
obj.self = obj;
var clone = structuredClone(obj);
equal(clone.a, 1);
equal(clone.self, clone);
var ab = new ArrayBuffer(1);
clone = structuredClone(ab, {transfer: [ab]});
equal(clone.byteLength, 1);
equal(ab.byteLength, 0);
clone = structuredClone(testing);
equal(clone.xyz, 123);
`, sb);
}

View file

@ -114,6 +114,7 @@ skip-if = os == "android" # native test components aren't available on Android
[test_css.js]
[test_rtcIdentityProvider.js]
[test_sandbox_atob.js]
[test_structuredClone.js]
[test_isProxy.js]
[test_getObjectPrincipal.js]
[test_sandbox_name.js]