Bug 1369073 - Expose MessagePort/MessageChannel to xpcshell tests, r=bholley
authorAndrea Marchesini <amarchesini@mozilla.com>
Wed, 31 May 2017 21:16:03 +0200
changeset 412130 313abe9e730ed46ac008d9867cc1e9619eb881c8
parent 412129 78a9d7baaf8bf0073e06d8369ca33bd5134b92f9
child 412131 7bd0e6b5a99a7401f4e17abeff03b366f07955e6
push id1490
push usermtabara@mozilla.com
push dateMon, 31 Jul 2017 14:08:16 +0000
treeherdermozilla-release@70e32e6bf15e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbholley
bugs1369073
milestone55.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1369073 - Expose MessagePort/MessageChannel to xpcshell tests, r=bholley
js/xpconnect/src/Sandbox.cpp
js/xpconnect/src/xpcprivate.h
js/xpconnect/tests/unit/test_messageChannel.js
js/xpconnect/tests/unit/xpcshell.ini
--- a/js/xpconnect/src/Sandbox.cpp
+++ b/js/xpconnect/src/Sandbox.cpp
@@ -30,16 +30,18 @@
 #include "mozilla/dom/BindingUtils.h"
 #include "mozilla/dom/BlobBinding.h"
 #include "mozilla/dom/cache/CacheStorage.h"
 #include "mozilla/dom/CSSBinding.h"
 #include "mozilla/dom/DirectoryBinding.h"
 #include "mozilla/dom/IndexedDatabaseManager.h"
 #include "mozilla/dom/Fetch.h"
 #include "mozilla/dom/FileBinding.h"
+#include "mozilla/dom/MessageChannelBinding.h"
+#include "mozilla/dom/MessagePortBinding.h"
 #include "mozilla/dom/PromiseBinding.h"
 #include "mozilla/dom/RequestBinding.h"
 #include "mozilla/dom/ResponseBinding.h"
 #ifdef MOZ_WEBRTC
 #include "mozilla/dom/RTCIdentityProviderRegistrar.h"
 #endif
 #include "mozilla/dom/FileReaderBinding.h"
 #include "mozilla/dom/ScriptSettings.h"
@@ -929,16 +931,18 @@ xpc::GlobalProperties::Parse(JSContext* 
             rtcIdentityProvider = true;
 #endif
         } else if (!strcmp(name.ptr(), "fetch")) {
             fetch = true;
         } else if (!strcmp(name.ptr(), "caches")) {
             caches = true;
         } else if (!strcmp(name.ptr(), "FileReader")) {
             fileReader = true;
+        } else if (!strcmp(name.ptr(), "MessageChannel")) {
+            messageChannel = true;
         } else {
             JS_ReportErrorUTF8(cx, "Unknown property name: %s", name.ptr());
             return false;
         }
     }
     return true;
 }
 
@@ -1006,16 +1010,21 @@ xpc::GlobalProperties::Define(JSContext*
         return false;
 
     if (caches && !dom::cache::CacheStorage::DefineCaches(cx, obj))
         return false;
 
     if (fileReader && !dom::FileReaderBinding::GetConstructorObject(cx))
         return false;
 
+    if (messageChannel &&
+        (!dom::MessageChannelBinding::GetConstructorObject(cx) ||
+         !dom::MessagePortBinding::GetConstructorObject(cx)))
+        return false;
+
     return true;
 }
 
 bool
 xpc::GlobalProperties::DefineInXPCComponents(JSContext* cx, JS::HandleObject obj)
 {
     if (indexedDB &&
         !IndexedDatabaseManager::DefineIndexedDB(cx, obj))
--- a/js/xpconnect/src/xpcprivate.h
+++ b/js/xpconnect/src/xpcprivate.h
@@ -2751,16 +2751,17 @@ struct GlobalProperties {
     bool Blob : 1;
     bool Directory : 1;
     bool File : 1;
     bool crypto : 1;
     bool rtcIdentityProvider : 1;
     bool fetch : 1;
     bool caches : 1;
     bool fileReader: 1;
+    bool messageChannel: 1;
 private:
     bool Define(JSContext* cx, JS::HandleObject obj);
 };
 
 // Infallible.
 already_AddRefed<nsIXPCComponents_utils_Sandbox>
 NewSandboxConstructor();
 
new file mode 100644
--- /dev/null
+++ b/js/xpconnect/tests/unit/test_messageChannel.js
@@ -0,0 +1,30 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+add_task(function*() {
+  let Cu = Components.utils;
+  let sb = new Cu.Sandbox('http://www.example.com',
+                          { wantGlobalProperties: ["MessageChannel"] });
+  sb.do_check_true = do_check_true;
+  Cu.evalInSandbox('do_check_true((new MessageChannel()) instanceof MessageChannel);',
+                   sb);
+  Cu.evalInSandbox('do_check_true((new MessageChannel()).port1 instanceof MessagePort);',
+                   sb);
+
+  Cu.importGlobalProperties(["MessageChannel"]);
+
+  let mc = new MessageChannel();
+  do_check_true(mc instanceof MessageChannel);
+  do_check_true(mc.port1 instanceof MessagePort);
+  do_check_true(mc.port2 instanceof MessagePort);
+
+  mc.port1.postMessage(42);
+
+  let result = yield new Promise(resolve => {
+    mc.port2.onmessage = e => {
+      resolve(e.data);
+    }
+  });
+
+  do_check_eq(result, 42);
+});
--- a/js/xpconnect/tests/unit/xpcshell.ini
+++ b/js/xpconnect/tests/unit/xpcshell.ini
@@ -103,16 +103,17 @@ skip-if = toolkit == "android" # bug 134
 [test_sandbox_metadata.js]
 [test_exportFunction.js]
 [test_promise.js]
 [test_returncode.js]
 [test_textDecoder.js]
 [test_url.js]
 [test_URLSearchParams.js]
 [test_fileReader.js]
+[test_messageChannel.js]
 [test_crypto.js]
 [test_css.js]
 [test_rtcIdentityProvider.js]
 [test_sandbox_atob.js]
 [test_isProxy.js]
 [test_getObjectPrincipal.js]
 [test_sandbox_name.js]
 [test_watchdog_enable.js]