Bug 1186409 - Use Cu.getGlobalForObject when importing properties off a JSM global. r=kmag
authorAndrew McCreight <continuation@gmail.com>
Fri, 23 Jun 2017 13:51:14 -0700
changeset 429297 9109b1f49d233b53246887ae39c3fd598720d54b
parent 429296 ba636d93c4d54cd216e52e37ef306e878bfcb32d
child 429298 37e7829372ebe046dfd7624ee8a74bc6f4cd5c2d
push id7761
push userjlund@mozilla.com
push dateFri, 15 Sep 2017 00:19:52 +0000
treeherdermozilla-beta@c38455951db4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskmag
bugs1186409
milestone57.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 1186409 - Use Cu.getGlobalForObject when importing properties off a JSM global. r=kmag With JSM global sharing, the object returned by Cu.import() is a NonSyntacticVariablesObject, rather than a global. Various code tries to use properties from a JSM global via an import. Cu.importGlobalProperties can also be used in some places. MozReview-Commit-ID: HudCXO2GKN0
addon-sdk/source/lib/sdk/base64.js
addon-sdk/source/test/addons/places/lib/favicon-helpers.js
caps/tests/mochitest/test_addonMayLoad.html
caps/tests/mochitest/test_extensionURL.html
devtools/shared/builtin-modules.js
dom/tests/browser/browser_ConsoleAPI_originAttributes.js
toolkit/components/extensions/test/xpcshell/test_ext_unknown_permissions.js
toolkit/components/url-classifier/tests/unit/test_backoff.js
--- a/addon-sdk/source/lib/sdk/base64.js
+++ b/addon-sdk/source/lib/sdk/base64.js
@@ -5,20 +5,17 @@
 "use strict";
 
 module.metadata = {
   "stability": "unstable"
 };
 
 const { Cu } = require("chrome");
 
-// Passing an empty object as second argument to avoid scope's pollution
-// (devtools loader injects these symbols as global and prevent using
-// const here)
-var { atob, btoa } = Cu.import("resource://gre/modules/Services.jsm", {});
+Cu.importGlobalProperties(["atob", "btoa"]);
 
 function isUTF8(charset) {
   let type = typeof charset;
 
   if (type === "undefined")
     return false;
 
   if (type === "string" && charset.toLowerCase() === "utf-8")
--- a/addon-sdk/source/test/addons/places/lib/favicon-helpers.js
+++ b/addon-sdk/source/test/addons/places/lib/favicon-helpers.js
@@ -4,17 +4,17 @@
 
 const { Cc, Ci, Cu } = require('chrome');
 const { Loader } = require('sdk/test/loader');
 const loader = Loader(module);
 const httpd = loader.require('./httpd');
 const { pathFor } = require('sdk/system');
 const { startServerAsync } = httpd;
 const basePath = pathFor('ProfD');
-const { atob } = Cu.import("resource://gre/modules/Services.jsm", {});
+Cu.importGlobalProperties(["atob"]);
 const historyService = Cc["@mozilla.org/browser/nav-history-service;1"]
                        .getService(Ci.nsINavHistoryService);
 const { events } = require('sdk/places/events');
 const { OS } = Cu.import("resource://gre/modules/osfile.jsm", {});
 
 function onFaviconChange (url) {
   return new Promise(resolve => {
     function handler ({data, type}) {
--- a/caps/tests/mochitest/test_addonMayLoad.html
+++ b/caps/tests/mochitest/test_addonMayLoad.html
@@ -10,17 +10,17 @@ https://bugzilla.mozilla.org/show_bug.cg
   <link rel="stylesheet" type="text/css" href="chrome://global/skin"/>
   <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
   <script type="application/javascript">
 
   /** Test for Bug 1180921 **/
   const Cc = Components.classes;
   const Ci = Components.interfaces;
   const Cu = Components.utils;
-  let module = Cu.import("resource://gre/modules/Services.jsm", window);
+  let module = Cu.getGlobalForObject(Cu.import("resource://gre/modules/Services.jsm", window));
   let ssm = Services.scriptSecurityManager;
 
   function StubPolicy(id, subdomain) {
     /* globals MatchPatternSet */
     return new module.WebExtensionPolicy({
       id,
       mozExtensionHostname: id,
       baseURL: `file:///{id}`,
--- a/caps/tests/mochitest/test_extensionURL.html
+++ b/caps/tests/mochitest/test_extensionURL.html
@@ -8,17 +8,17 @@ https://bugzilla.mozilla.org/show_bug.cg
   <title>Test for Bug 1161831</title>
   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
   <script type="application/javascript">
 
   /** Test for Bug 1161831 **/
   SimpleTest.waitForExplicitFinish();
 
-  let module = SpecialPowers.Cu.import("resource://gre/modules/Services.jsm", {});
+  let module = SpecialPowers.Cu.getGlobalForObject(SpecialPowers.Cu.import("resource://gre/modules/Services.jsm", {}));
   var {MatchGlob, MatchPatternSet, WebExtensionPolicy} = module;
 
   var policy1, policy2;
 
   var XPCOMUtils = SpecialPowers.Cu.import("resource://gre/modules/XPCOMUtils.jsm").XPCOMUtils;
   var resourceHandler = SpecialPowers.Services.io.getProtocolHandler("resource")
                                      .QueryInterface(SpecialPowers.Ci.nsISubstitutingProtocolHandler);
   var extensionHandler = SpecialPowers.Services.io.getProtocolHandler("moz-extension")
--- a/devtools/shared/builtin-modules.js
+++ b/devtools/shared/builtin-modules.js
@@ -14,17 +14,17 @@
  */
 
 const { Cu, CC, Cc, Ci } = require("chrome");
 const promise = Cu.import("resource://gre/modules/Promise.jsm", {}).Promise;
 const jsmScope = Cu.import("resource://gre/modules/Services.jsm", {});
 const { Services } = jsmScope;
 // Steal various globals only available in JSM scope (and not Sandbox one)
 const { PromiseDebugging, ChromeUtils, ThreadSafeChromeUtils, HeapSnapshot,
-        atob, btoa, TextEncoder, TextDecoder } = jsmScope;
+        atob, btoa, TextEncoder, TextDecoder } = Cu.getGlobalForObject(jsmScope);
 
 // Create a single Sandbox to access global properties needed in this module.
 // Sandbox are memory expensive, so we should create as little as possible.
 const { CSS, FileReader, indexedDB, URL } =
     Cu.Sandbox(CC("@mozilla.org/systemprincipal;1", "nsIPrincipal")(), {
       wantGlobalProperties: ["CSS", "FileReader", "indexedDB", "URL"]
     });
 
--- a/dom/tests/browser/browser_ConsoleAPI_originAttributes.js
+++ b/dom/tests/browser/browser_ConsoleAPI_originAttributes.js
@@ -1,15 +1,15 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 const ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"]
       .getService(Ci.nsIConsoleAPIStorage);
 
-const {WebExtensionPolicy} = Cu.import("resource://gre/modules/Services.jsm", {});
+const {WebExtensionPolicy} = Cu.getGlobalForObject(Cu.import("resource://gre/modules/Services.jsm", {}));
 
 const FAKE_ADDON_ID = "test-webext-addon@mozilla.org";
 const EXPECTED_CONSOLE_ID = `addon/${FAKE_ADDON_ID}`;
 const EXPECTED_CONSOLE_MESSAGE_CONTENT = "fake-webext-addon-test-log-message";
 const ConsoleObserver = {
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
 
   init() {
--- a/toolkit/components/extensions/test/xpcshell/test_ext_unknown_permissions.js
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_unknown_permissions.js
@@ -14,17 +14,17 @@ add_task(async function test_unknown_per
         "https://example.com/",
       ],
     },
   });
 
   let {messages} = await promiseConsoleOutput(
     () => extension.startup());
 
-  const {WebExtensionPolicy} = Cu.import("resource://gre/modules/Extension.jsm", {});
+  const {WebExtensionPolicy} = Cu.getGlobalForObject(Cu.import("resource://gre/modules/Extension.jsm", {}));
 
   let policy = WebExtensionPolicy.getByID(extension.id);
   Assert.deepEqual(Array.from(policy.permissions).sort(), ["activeTab", "http://*/*"]);
 
   Assert.deepEqual(extension.extension.manifest.optional_permissions, ["https://example.com/"]);
 
   ok(messages.some(message => /Error processing permissions\.1: Value "fooUnknownPermission" must/.test(message)),
      'Got expected error for "fooUnknownPermission"');
--- a/toolkit/components/url-classifier/tests/unit/test_backoff.js
+++ b/toolkit/components/url-classifier/tests/unit/test_backoff.js
@@ -1,14 +1,17 @@
 // Some unittests (e.g., paste into JS shell)
 var jslib = Cc["@mozilla.org/url-classifier/jslib;1"].
             getService().wrappedJSObject;
-var _Datenow = jslib.Date.now;
+
+var jslibDate = Cu.getGlobalForObject(jslib).Date;
+
+var _Datenow = jslibDate.now;
 function setNow(time) {
-  jslib.Date.now = function() {
+  jslibDate.now = function() {
     return time;
   }
 }
 
 function run_test() {
   // 3 errors, 1ms retry period, max 3 requests per ten milliseconds,
   // 5ms backoff interval, 19ms max delay
   var rb = new jslib.RequestBackoff(3, 1, 3, 10, 5, 19, 0);
@@ -80,10 +83,10 @@ function run_test() {
   rb.noteRequest();
   setNow(202);
   do_check_true(rb.canMakeRequest());
   rb.noteRequest();
   do_check_false(rb.canMakeRequest());
   setNow(211);
   do_check_true(rb.canMakeRequest());
 
-  jslib.Date.now = _Datenow;
+  jslibDate.now = _Datenow;
 }