Bug 1140275 - System messages shouldn't be sent to app pages not registered in manifests. r=fabrice
authorSean Lin <selin@mozilla.com>
Mon, 03 Aug 2015 01:05:00 +0200
changeset 288467 f85097eaf25514cdd5ce8a9dc4b9a7a3f25bab00
parent 288466 ce43b991306ff30a8f3c62430fd285e7b69a131c
child 288468 7e9801d71ae1750e6d5d61a6a6022295d8987218
push id5067
push userraliiev@mozilla.com
push dateMon, 21 Sep 2015 14:04:52 +0000
treeherdermozilla-beta@14221ffe5b2f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfabrice
bugs1140275
milestone42.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 1140275 - System messages shouldn't be sent to app pages not registered in manifests. r=fabrice
dom/alarm/test/mochitest.ini
dom/alarm/test/system_message_chrome_script.js
dom/alarm/test/test_bug1037079.html
dom/alarm/test/test_bug1090896.html
dom/messages/SystemMessageInternal.js
dom/messages/test/mochitest.ini
dom/messages/test/system_message_chrome_script.js
dom/messages/test/test_bug_993732.html
dom/requestsync/tests/mochitest.ini
dom/requestsync/tests/system_message_chrome_script.js
dom/requestsync/tests/test_runNow.html
dom/requestsync/tests/test_wakeUp.html
--- a/dom/alarm/test/mochitest.ini
+++ b/dom/alarm/test/mochitest.ini
@@ -1,11 +1,12 @@
 [DEFAULT]
 support-files =
   file_empty.html
+  system_message_chrome_script.js
 
 [test_alarm_add_data.html]
 skip-if = ((buildapp == 'mulet' || buildapp == 'b2g') && toolkit != 'gonk') #Bug 931116, b2g desktop specific, initial triage
 [test_alarm_add_date.html]
 skip-if = ((buildapp == 'mulet' || buildapp == 'b2g') && toolkit != 'gonk') #Bug 931116, b2g desktop specific, initial triage
 [test_alarm_add_respectTimezone.html]
 skip-if = ((buildapp == 'mulet' || buildapp == 'b2g') && toolkit != 'gonk') #Bug 931116, b2g desktop specific, initial triage
 [test_alarm_non_permitted_app.html]
new file mode 100644
--- /dev/null
+++ b/dom/alarm/test/system_message_chrome_script.js
@@ -0,0 +1,18 @@
+/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+'use strict';
+
+const { classes: Cc, interfaces: Ci } = Components;
+
+const systemMessenger = Cc["@mozilla.org/system-message-internal;1"]
+                          .getService(Ci.nsISystemMessagesInternal);
+const ioService = Cc["@mozilla.org/network/io-service;1"]
+                    .getService(Ci.nsIIOService);
+
+addMessageListener("trigger-register-page", function(aData) {
+  systemMessenger.registerPage(aData.type,
+                               ioService.newURI(aData.pageURL, null, null),
+                               ioService.newURI(aData.manifestURL, null, null));
+  sendAsyncMessage("page-registered");
+});
--- a/dom/alarm/test/test_bug1037079.html
+++ b/dom/alarm/test/test_bug1037079.html
@@ -9,30 +9,44 @@
 <body>
 <p id="display"></p>
 <div id="content" style="display: none"></div>
 <pre id="test">
   <script type="application/javascript">
 
   "use strict";
 
+  function registerPage() {
+    var gScript = SpecialPowers.loadChromeScript(SimpleTest.getTestFileURL('system_message_chrome_script.js'));
+    gScript.addMessageListener("page-registered", function pageRegisteredHandler() {
+      gScript.removeMessageListener("page-registered", pageRegisteredHandler);
+      gScript.destroy();
+      testFireTimeAlert();
+    });
+
+    gScript.sendAsyncMessage("trigger-register-page",
+                             { type: "alarm",
+                               manifestURL: window.location.origin + "/manifest.webapp",
+                               pageURL: window.location.href });
+  }
+
   function testFireTimeAlert() {
     var secondsLater = new Date();
     secondsLater.setSeconds(secondsLater.getSeconds() + 10);
 
     var domRequest;
     try {
       // Set system message handler.
       navigator.mozSetMessageHandler('alarm', function(message){
         ok(true, "Time alert has been fired.");
         SimpleTest.finish();
       });
 
       domRequest = navigator.mozAlarms.add(secondsLater, "honorTimezone",
-    		                               {type: "timer"});
+                                           {type: "timer"});
     } catch (e) {
       ok(false, "Unexpected exception trying to set time alert.");
 
       return SimpleTest.finish();
     }
     domRequest.onsuccess = function(e) {
       ok(true, "Set time alert. Waiting to be fired.");
     };
@@ -57,17 +71,17 @@
                  SpecialPowers.Ci.nsIPrincipal.APP_STATUS_NOT_INSTALLED) {
         ok(true, "mozAlarms is not allowed for non-installed apps. " +
                  "TODO Bug 876981.");
         isAllowedToTest = false;
       }
 
       if (isAllowedToTest) {
         ok(true, "Start to test...");
-        testFireTimeAlert();
+        registerPage();
       } else {
         // A sanity check to make sure we must run tests on Firefox OS (B2G).
         if (navigator.userAgent.indexOf("Mobile") != -1 &&
             navigator.appVersion.indexOf("Android") == -1) {
           ok(false, "Should run the test on Firefox OS (B2G)!");
         }
 
         SimpleTest.finish();
--- a/dom/alarm/test/test_bug1090896.html
+++ b/dom/alarm/test/test_bug1090896.html
@@ -9,16 +9,30 @@
 <body>
 <p id="display"></p>
 <div id="content" style="display: none"></div>
 <pre id="test">
   <script type="application/javascript">
 
   "use strict";
 
+  function registerPage() {
+    var gScript = SpecialPowers.loadChromeScript(SimpleTest.getTestFileURL('system_message_chrome_script.js'));
+    gScript.addMessageListener("page-registered", function pageRegisteredHandler() {
+      gScript.removeMessageListener("page-registered", pageRegisteredHandler);
+      gScript.destroy();
+      testFireTimeAlertWithNoData();
+    });
+
+    gScript.sendAsyncMessage("trigger-register-page",
+                             { type: "alarm",
+                               manifestURL: window.location.origin + "/manifest.webapp",
+                               pageURL: window.location.href });
+  }
+
   function testFireTimeAlertWithNoData() {
     var secondsLater = new Date();
     secondsLater.setSeconds(secondsLater.getSeconds() + 1);
 
     var domRequest;
     try {
       // Set system message handler.
       navigator.mozSetMessageHandler('alarm', function(message){
@@ -56,17 +70,17 @@
                  SpecialPowers.Ci.nsIPrincipal.APP_STATUS_NOT_INSTALLED) {
         ok(true, "mozAlarms is not allowed for non-installed apps. " +
                  "TODO Bug 876981.");
         isAllowedToTest = false;
       }
 
       if (isAllowedToTest) {
         ok(true, "Start to test...");
-        testFireTimeAlertWithNoData();
+        registerPage();
       } else {
         // A sanity check to make sure we must run tests on Firefox OS (B2G).
         if (navigator.userAgent.indexOf("Mobile") != -1 &&
             navigator.appVersion.indexOf("Android") == -1) {
           ok(false, "Should run the test on Firefox OS (B2G)!");
         }
 
         SimpleTest.finish();
--- a/dom/messages/SystemMessageInternal.js
+++ b/dom/messages/SystemMessageInternal.js
@@ -737,21 +737,19 @@ SystemMessageInternal.prototype = {
       return MSG_SENT_FAILURE_PERM_DENIED;
     }
 
     // Queue this message in the corresponding pages.
     let page = this._findPage(aType, aPageURL, aManifestURL);
     if (!page) {
       debug("Message " + aType + " is not registered for " +
             aPageURL + " @ " + aManifestURL);
-      // FIXME bug 1140275 should only send message to page registered in manifest
-      // return MSG_SENT_FAILURE_PERM_DENIED;
+      return MSG_SENT_FAILURE_PERM_DENIED;
     }
-    if (page)
-      this._queueMessage(page, aMessage, aMessageID);
+    this._queueMessage(page, aMessage, aMessageID);
 
     let appPageIsRunning = false;
     let pageKey = this._createKeyForPage({ type: aType,
                                            manifestURL: aManifestURL,
                                            pageURL: aPageURL });
 
     let cache = this._findCacheForApp(aManifestURL);
     let targets = this._listeners[aManifestURL];
@@ -796,18 +794,17 @@ SystemMessageInternal.prototype = {
       // wake it up. We still need to acquire a CPU wake lock for that page and
       // expect that we will receive a "SystemMessageManager:HandleMessagesDone"
       // or a "SystemMessageManager:HandleMessageDone" message when the page
       // finishes handling the system message with other pending messages. At
       // that point, we'll release the lock we acquired.
       result = MSG_SENT_FAILURE_APP_NOT_RUNNING;
       this._acquireCpuWakeLock(pageKey);
     }
-    if (page)
-      this._openAppPage(page, aMessage, aExtra, result);
+    this._openAppPage(page, aMessage, aExtra, result);
     return result;
   },
 
   _resolvePendingPromises: function(aMessageID) {
     if (!this._pendingPromises.has(aMessageID)) {
       debug("Unknown pendingPromise messageID. This seems a bug!!");
       return;
     }
--- a/dom/messages/test/mochitest.ini
+++ b/dom/messages/test/mochitest.ini
@@ -1,2 +1,6 @@
+[DEFAULT]
+support-files =
+  system_message_chrome_script.js
+
 [test_bug_993732.html]
 skip-if = buildapp == 'mulet' || (buildapp == 'b2g' && toolkit != 'gonk') #Bug 931116, b2g desktop specific, initial triage
new file mode 100644
--- /dev/null
+++ b/dom/messages/test/system_message_chrome_script.js
@@ -0,0 +1,18 @@
+/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+'use strict';
+
+const { classes: Cc, interfaces: Ci } = Components;
+
+const systemMessenger = Cc["@mozilla.org/system-message-internal;1"]
+                          .getService(Ci.nsISystemMessagesInternal);
+const ioService = Cc["@mozilla.org/network/io-service;1"]
+                    .getService(Ci.nsIIOService);
+
+addMessageListener("trigger-register-page", function(aData) {
+  systemMessenger.registerPage(aData.type,
+                               ioService.newURI(aData.pageURL, null, null),
+                               ioService.newURI(aData.manifestURL, null, null));
+  sendAsyncMessage("page-registered");
+});
--- a/dom/messages/test/test_bug_993732.html
+++ b/dom/messages/test/test_bug_993732.html
@@ -14,16 +14,30 @@
 
   "use strict";
 
   // The syndrome of Bug 993732 is that the running app (either foreground or background)
   // is not able to receive system messages. Even worse, the app will be killed when the
   // listening system message is broadcast. So this test case uses the alarm message
   // to test if a running app can receive the system message.
 
+  function registerPage() {
+    var gScript = SpecialPowers.loadChromeScript(SimpleTest.getTestFileURL('system_message_chrome_script.js'));
+    gScript.addMessageListener("page-registered", function pageRegisteredHandler() {
+      gScript.removeMessageListener("page-registered", pageRegisteredHandler);
+      gScript.destroy();
+      testAlarm(10000);
+    });
+
+    gScript.sendAsyncMessage("trigger-register-page",
+                             { type: "alarm",
+                               manifestURL: window.location.origin + "/manifest.webapp",
+                               pageURL: window.location.href });
+  }
+
   function testAlarm(aMillisecondsFromNow) {
     var at = new Date();
     at.setTime(at.getTime() + aMillisecondsFromNow);
 
     navigator.mozSetMessageHandler('alarm', function(message) {
       ok(true, "We got alarm message!");
       SimpleTest.finish();
     });
@@ -35,29 +49,29 @@
       ok(false,
          "Unexpected exception while adding alarm " + aMillisecondsFromNow + " ms from now.");
       SimpleTest.finish();
     }
     domRequest.onsuccess = function(e) {
       // Waiting for alarm message.
     };
     domRequest.onerror = function(e) {
-      ok(false, "Unable to add alarm for tomorrow`.");
+      ok(false, "Unable to add alarm.");
       SimpleTest.finish();
     };
   }
 
   function startTests() {
 
     SpecialPowers.pushPrefEnv({"set": [["dom.mozAlarms.enabled", true]]}, function() {
       // Currently applicable only on FxOS
       if (navigator.userAgent.indexOf("Mobile") != -1 &&
           navigator.appVersion.indexOf("Android") == -1)
       {
-        testAlarm(10000);
+        registerPage();
       } else {
         ok(true, "mozAlarms on Firefox OS only.");
         SimpleTest.finish();
       }
     });
   }
 
   SimpleTest.expectAssertions(0, 9);
--- a/dom/requestsync/tests/mochitest.ini
+++ b/dom/requestsync/tests/mochitest.ini
@@ -1,16 +1,17 @@
 [DEFAULT]
 skip-if = e10s
 support-files =
   file_app.template.webapp
   file_app.sjs
   file_basic_app.html
   common_app.js
   common_basic.js
+  system_message_chrome_script.js
 
 [test_webidl.html]
 [test_minInterval.html]
 [test_basic.html]
 [test_basic_app.html]
 skip-if = buildapp == 'b2g'
 [test_wakeUp.html]
 skip-if = !(buildapp == 'b2g' && toolkit == 'gonk')
new file mode 100644
--- /dev/null
+++ b/dom/requestsync/tests/system_message_chrome_script.js
@@ -0,0 +1,18 @@
+/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+'use strict';
+
+const { classes: Cc, interfaces: Ci } = Components;
+
+const systemMessenger = Cc["@mozilla.org/system-message-internal;1"]
+                          .getService(Ci.nsISystemMessagesInternal);
+const ioService = Cc["@mozilla.org/network/io-service;1"]
+                    .getService(Ci.nsIIOService);
+
+addMessageListener("trigger-register-page", function(aData) {
+  systemMessenger.registerPage(aData.type,
+                               ioService.newURI(aData.pageURL, null, null),
+                               ioService.newURI(aData.manifestURL, null, null));
+  sendAsyncMessage("page-registered");
+});
--- a/dom/requestsync/tests/test_runNow.html
+++ b/dom/requestsync/tests/test_runNow.html
@@ -8,16 +8,30 @@
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
 </head>
 <body>
 <div id="container"></div>
   <script type="application/javascript;version=1.7">
 
   var taskExecuted = false;
 
+  function registerPage() {
+    var gScript = SpecialPowers.loadChromeScript(SimpleTest.getTestFileURL('system_message_chrome_script.js'));
+    gScript.addMessageListener("page-registered", function pageRegisteredHandler() {
+      gScript.removeMessageListener("page-registered", pageRegisteredHandler);
+      gScript.destroy();
+      runTests();
+    });
+
+    gScript.sendAsyncMessage("trigger-register-page",
+                             { type: "request-sync",
+                               manifestURL: window.location.origin + "/manifest.webapp",
+                               pageURL: window.location.href });
+  }
+
   function setMessageHandler() {
     navigator.mozSetMessageHandler('request-sync', function(e) {
       ok(true, "One event has been received!");
 
       if (e.task == "oneShot") {
         is(e.data, 42, "e.data is correct");
         is(e.lastSync, 0, "e.lastSync is correct");
         is(e.oneShot, true, "e.oneShot is correct");
@@ -80,16 +94,18 @@
 
     function() {
       if (SpecialPowers.isMainProcess()) {
         SpecialPowers.Cu.import("resource://gre/modules/RequestSyncService.jsm");
       }
       runTests();
     },
 
+    registerPage,
+
     setMessageHandler,
 
     test_register_oneShot,
 
     test_runNow,
 
     test_unregister_oneShot,
   ];
--- a/dom/requestsync/tests/test_wakeUp.html
+++ b/dom/requestsync/tests/test_wakeUp.html
@@ -15,16 +15,30 @@
   var multiShotCounter = 0;
 
   function maybeDone() {
     if (oneShotCounter == 1 && multiShotCounter == 5) {
       runTests();
     }
   }
 
+  function registerPage() {
+    var gScript = SpecialPowers.loadChromeScript(SimpleTest.getTestFileURL('system_message_chrome_script.js'));
+    gScript.addMessageListener("page-registered", function pageRegisteredHandler() {
+      gScript.removeMessageListener("page-registered", pageRegisteredHandler);
+      gScript.destroy();
+      runTests();
+    });
+
+    gScript.sendAsyncMessage("trigger-register-page",
+                             { type: "request-sync",
+                               manifestURL: window.location.origin + "/manifest.webapp",
+                               pageURL: window.location.href });
+  }
+
   function setMessageHandler() {
     navigator.mozSetMessageHandler('request-sync', function(e) {
       ok(true, "One event has been received!");
 
       if (e.task == "oneShot") {
         is(e.data, 42, "e.data is correct");
         is(e.lastSync, 0, "e.lastSync is correct");
         is(e.oneShot, true, "e.oneShot is correct");
@@ -141,16 +155,17 @@
 
     function() {
       if (SpecialPowers.isMainProcess()) {
         SpecialPowers.Cu.import("resource://gre/modules/RequestSyncService.jsm");
       }
       runTests();
     },
 
+    registerPage,
     setMessageHandler,
 
     test_register_oneShot,
     test_register_multiShots,
 
     test_wait,
 
     test_unregister_oneShot,