Skip to content
This repository has been archived by the owner on Aug 4, 2022. It is now read-only.

Commit

Permalink
Bug 1587007 - P1 - Tunnel the prefs for Coop and Coep to js; r=jandem
Browse files Browse the repository at this point in the history
  • Loading branch information
chihweitung committed Nov 26, 2019
1 parent 0f0e9f2 commit d863bde
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
10 changes: 10 additions & 0 deletions js/public/RealmOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions js/src/jit-test/tests/structured-clone/sab-errMsg.js
Original file line number Diff line number Diff line change
@@ -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.`);
10 changes: 10 additions & 0 deletions js/src/jsapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
8 changes: 8 additions & 0 deletions js/src/shell/js.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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())) {
Expand Down
3 changes: 3 additions & 0 deletions js/xpconnect/src/XPCJSContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,9 @@ static mozilla::Atomic<bool> 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())
Expand Down

0 comments on commit d863bde

Please sign in to comment.