diff --git a/js/public/RealmOptions.h b/js/public/RealmOptions.h index acb850e273a9..da3dc58e1894 100644 --- a/js/public/RealmOptions.h +++ b/js/public/RealmOptions.h @@ -128,6 +128,15 @@ class JS_PUBLIC_API RealmCreationOptions { bool getSharedMemoryAndAtomicsEnabled() const; RealmCreationOptions& setSharedMemoryAndAtomicsEnabled(bool flag); + // When these prefs (COOP and COEP) are not enabled, shared memory objects + // (e.g. SAB) are not allowed to be postMessage()'ed. And we want to provide + // a clear warning message to users/developer so that they would have an idea + // if the implementations of the COOP and COEP headers are finished or not. So + // that they would know if they can fix the SAB by deploying the COOP and + // COEP headers or not. + bool getCoopAndCoepEnabled() const; + RealmCreationOptions& setCoopAndCoepEnabled(bool flag); + bool getStreamsEnabled() const { return streams_; } RealmCreationOptions& setStreamsEnabled(bool flag) { streams_ = flag; @@ -199,6 +208,7 @@ class JS_PUBLIC_API RealmCreationOptions { bool preserveJitCode_ = false; bool cloneSingletons_ = false; bool sharedMemoryAndAtomics_ = false; + bool coopAndCoep_ = false; bool streams_ = false; bool readableByteStreams_ = false; bool byobStreamReaders_ = false; diff --git a/js/src/jit-test/tests/structured-clone/sab-errMsg.js b/js/src/jit-test/tests/structured-clone/sab-errMsg.js new file mode 100644 index 000000000000..2887f46734a9 --- /dev/null +++ b/js/src/jit-test/tests/structured-clone/sab-errMsg.js @@ -0,0 +1,16 @@ +// |jit-test| skip-if: !this.sharedMemoryEnabled +// Need this testing function to continue. + +// Check the error mssage when the prefs for COOP/COEP are both enable or not. +var g = newGlobal(); +var ex; +const sab = new SharedArrayBuffer(); +try { + g.serialize(sab); +} catch (e) { + ex = e; +} +assertEq(ex.toString(), + `TypeError: The SharedArrayBuffer object cannot be serialized. The ` + + `Cross-Origin-Opener-Policy and Cross-Origin-Embedder-Policy HTTP ` + + `headers will enable this in the future.`); diff --git a/js/src/jsapi.cpp b/js/src/jsapi.cpp index 068af69a4c17..b40f11b61d7a 100644 --- a/js/src/jsapi.cpp +++ b/js/src/jsapi.cpp @@ -1717,6 +1717,16 @@ JS::RealmCreationOptions::setSharedMemoryAndAtomicsEnabled(bool flag) { return *this; } +bool JS::RealmCreationOptions::getCoopAndCoepEnabled() const { + return coopAndCoep_; +} + +JS::RealmCreationOptions& JS::RealmCreationOptions::setCoopAndCoepEnabled( + bool flag) { + coopAndCoep_ = flag; + return *this; +} + JS::RealmBehaviors& JS::RealmBehaviorsRef(JS::Realm* realm) { return realm->behaviors(); } diff --git a/js/src/shell/js.cpp b/js/src/shell/js.cpp index 0c8d454fec56..40cdaaf57748 100644 --- a/js/src/shell/js.cpp +++ b/js/src/shell/js.cpp @@ -3731,6 +3731,7 @@ static const JSClass sandbox_class = {"sandbox", JSCLASS_GLOBAL_FLAGS, static void SetStandardRealmOptions(JS::RealmOptions& options) { options.creationOptions() .setSharedMemoryAndAtomicsEnabled(enableSharedMemory) + .setCoopAndCoepEnabled(false) .setStreamsEnabled(enableStreams) .setReadableByteStreamsEnabled(enableReadableByteStreams) .setBYOBStreamReadersEnabled(enableBYOBStreamReaders) @@ -6201,6 +6202,13 @@ static bool NewGlobal(JSContext* cx, unsigned argc, Value* vp) { } principals.reset(newPrincipals); } + + if (!JS_GetProperty(cx, opts, "enableCoopAndCoep", &v)) { + return false; + } + if (v.isBoolean()) { + creationOptions.setCoopAndCoepEnabled(v.toBoolean()); + } } if (!CheckRealmOptions(cx, options, principals.get())) { diff --git a/js/xpconnect/src/XPCJSContext.cpp b/js/xpconnect/src/XPCJSContext.cpp index b5c1949dfbbc..deeb684df17f 100644 --- a/js/xpconnect/src/XPCJSContext.cpp +++ b/js/xpconnect/src/XPCJSContext.cpp @@ -771,6 +771,9 @@ static mozilla::Atomic sAwaitFixEnabled(false); void xpc::SetPrefableRealmOptions(JS::RealmOptions& options) { options.creationOptions() .setSharedMemoryAndAtomicsEnabled(sSharedMemoryEnabled) + .setCoopAndCoepEnabled( + StaticPrefs::browser_tabs_remote_useCrossOriginOpenerPolicy() && + StaticPrefs::browser_tabs_remote_useCrossOriginEmbedderPolicy()) .setStreamsEnabled(sStreamsEnabled) .setWritableStreamsEnabled( StaticPrefs::javascript_options_writable_streams())