bug 872470 - add EventSource to frameworker, r=markh, sr=gavin
authorShane Caraveo <scaraveo@mozilla.com>
Thu, 16 May 2013 13:16:17 +0200
changeset 133070 3f37696472d493160f2f09de489bb57d52b07abf
parent 133065 1d6a04be8c0f91865c2fc844c79314450ed580ed
child 133071 4e3c25f31bdcbb479cd422865c0f912ced74374f
push id24740
push useremorley@mozilla.com
push dateTue, 28 May 2013 11:07:27 +0000
treeherdermozilla-central@7be40b778117 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmarkh, gavin
bugs872470
milestone24.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 872470 - add EventSource to frameworker, r=markh, sr=gavin
toolkit/components/social/FrameWorker.jsm
toolkit/components/social/test/browser/Makefile.in
toolkit/components/social/test/browser/browser_frameworker.js
toolkit/components/social/test/browser/eventsource.resource
toolkit/components/social/test/browser/eventsource.resource^headers^
toolkit/components/social/test/browser/worker_eventsource.js
--- a/toolkit/components/social/FrameWorker.jsm
+++ b/toolkit/components/social/FrameWorker.jsm
@@ -131,17 +131,17 @@ FrameWorker.prototype = {
     let sandbox = new Cu.Sandbox(workerWindow);
 
     // copy the window apis onto the sandbox namespace only functions or
     // objects that are naturally a part of an iframe, I'm assuming they are
     // safe to import this way
     let workerAPI = ['WebSocket', 'localStorage', 'atob', 'btoa',
                      'clearInterval', 'clearTimeout', 'dump',
                      'setInterval', 'setTimeout', 'XMLHttpRequest',
-                     'FileReader', 'Blob',
+                     'FileReader', 'Blob', 'EventSource',
                      'location'];
     // Bug 798660 - XHR and WebSocket have issues in a sandbox and need
     // to be unwrapped to work
     let needsWaive = ['XMLHttpRequest', 'WebSocket'];
     // Methods need to be bound with the proper |this|.
     let needsBind = ['atob', 'btoa', 'dump', 'setInterval', 'clearInterval',
                      'setTimeout', 'clearTimeout'];
     workerAPI.forEach(function(fn) {
--- a/toolkit/components/social/test/browser/Makefile.in
+++ b/toolkit/components/social/test/browser/Makefile.in
@@ -19,13 +19,16 @@ MOCHITEST_BROWSER_FILES = \
   worker_xhr.js \
   browser_frameworker.js \
   worker_relative.js \
   relative_import.js \
   browser_workerAPI.js \
   worker_social.js \
   browser_SocialProvider.js \
   browser_notifications.js \
+  worker_eventsource.js \
+  eventsource.resource \
+  eventsource.resource^headers^ \
   $(NULL)
 
 endif
 
 include $(topsrcdir)/config/rules.mk
--- a/toolkit/components/social/test/browser/browser_frameworker.js
+++ b/toolkit/components/social/test/browser/browser_frameworker.js
@@ -627,9 +627,29 @@ let tests = {
         is(e.data.result.ping, 1, "the worker got the ping");
         is(e.data.result.close, 1, "the worker got 1 close message");
         worker.terminate();
         cbnext();
       }
     }
     worker.port.postMessage({topic: "get-ready"});
   },
+
+  testEventSource: function(cbnext) {
+    let worker = getFrameWorkerHandle("https://example.com/browser/toolkit/components/social/test/browser/worker_eventsource.js", undefined, "testEventSource");
+    worker.port.onmessage = function(e) {
+      let m = e.data;
+      if (m.topic == "eventSourceTest") {
+        if (m.result.ok != undefined)
+          ok(m.result.ok, e.data.result.msg);
+        if (m.result.is != undefined)
+          is(m.result.is, m.result.match, m.result.msg);
+        if (m.result.info != undefined)
+          info(m.result.info);
+      } else if (e.data.topic == "pong") {
+        worker.terminate();
+        cbnext();
+      }
+    }
+    worker.port.postMessage({topic: "ping"})
+  },
+
 }
new file mode 100644
--- /dev/null
+++ b/toolkit/components/social/test/browser/eventsource.resource
@@ -0,0 +1,12 @@
+:this file must be enconded in utf8
+:and its Content-Type must be equal to text/event-stream
+
+retry:10
+data: 1
+event: test-message
+
+retry:10
+data: 1
+
+retry:10
+data: 1
new file mode 100644
--- /dev/null
+++ b/toolkit/components/social/test/browser/eventsource.resource^headers^
@@ -0,0 +1,2 @@
+Content-Type: text/event-stream
+Cache-Control: no-cache, must-revalidate
\ No newline at end of file
new file mode 100644
--- /dev/null
+++ b/toolkit/components/social/test/browser/worker_eventsource.js
@@ -0,0 +1,43 @@
+let port, es;
+
+let url = "https://example.com/browser/toolkit/components/social/test/browser/eventsource.resource";
+
+function ok(a, msg) {
+  port.postMessage({topic: "eventSourceTest",
+                   result: {ok: a, msg: msg}});
+}
+
+function is(a, b, msg) {
+  port.postMessage({topic: "eventSourceTest",
+                   result: {is: a, match: b, msg: msg}});
+}
+
+function esListener(e) {
+  esListener.msg_ok = true;
+}
+
+function esOnmessage(e) {
+  ok(true, "onmessage test");
+  ok(esListener.msg_ok, "listener test");
+  es.close();
+  port.postMessage({topic: "pong"});
+}
+
+function doTest() {
+  try {
+    es = new EventSource(url);
+    is(es.url, url, "eventsource.resource accessed", "we can create an eventsource instance");
+    es.addEventListener('test-message', esListener, true);
+    es.onmessage = esOnmessage;
+  } catch (e) {}
+  ok(!!es, "we can create an eventsource instance");
+}
+
+onconnect = function(e) {
+  port = e.ports[0];
+  port.onmessage = function(e) {
+    if (e.data.topic == "ping") {
+      doTest();
+    }
+  }
+}