Bug 792808 - Change devtools/ to import and instantiate XHRs from global properties rather than using Cc.createInstance(Ci.nsIXMLHttpRequest), and the same for FormData; r?Honza draft
authorThomas Wisniewski <wisniewskit@gmail.com>
Mon, 20 Nov 2017 00:27:39 -0500
changeset 756143 7f0ef328bab7ed8a4786206b128428b79c2bb404
parent 756142 ed3ba4b507e4220e59db80bb3204d7baecae5819
child 756144 595df7b89b706c3737b3b90e1ddfb8744a2fae4e
push id99394
push userwisniewskit@gmail.com
push dateFri, 16 Feb 2018 14:37:01 +0000
reviewersHonza
bugs792808
milestone60.0a1
Bug 792808 - Change devtools/ to import and instantiate XHRs from global properties rather than using Cc.createInstance(Ci.nsIXMLHttpRequest), and the same for FormData; r?Honza MozReview-Commit-ID: HCziNedavb0
devtools/client/shared/getjson.js
devtools/client/webide/modules/app-validator.js
devtools/shared/builtin-modules.js
devtools/shared/fronts/device.js
devtools/shared/gcli/commands/jsb.js
devtools/shared/gcli/commands/screenshot.js
devtools/shared/gcli/source/lib/gcli/util/host.js
--- a/devtools/client/shared/getjson.js
+++ b/devtools/client/shared/getjson.js
@@ -1,21 +1,18 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 "use strict";
 
-const {CC} = require("chrome");
 const Services = require("Services");
 
 loader.lazyRequireGetter(this, "asyncStorage", "devtools/shared/async-storage");
 
-const XMLHttpRequest = CC("@mozilla.org/xmlextras/xmlhttprequest;1");
-
 /**
  * Downloads and caches a JSON file from an URL given by a pref.
  *
  * @param {String} prefName
  *        The preference for the target URL
  *
  * @return {Promise}
  *         - Resolved with the JSON object in case of successful request
--- a/devtools/client/webide/modules/app-validator.js
+++ b/devtools/client/webide/modules/app-validator.js
@@ -1,19 +1,18 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 "use strict";
 
-var {Ci, Cu, CC} = require("chrome");
+var {Ci, Cu} = require("chrome");
 
 const {FileUtils} = require("resource://gre/modules/FileUtils.jsm");
 const Services = require("Services");
 const {Task} = require("devtools/shared/task");
-var XMLHttpRequest = CC("@mozilla.org/xmlextras/xmlhttprequest;1");
 var strings = Services.strings.createBundle("chrome://devtools/locale/app-manager.properties");
 
 function AppValidator({ type, location }) {
   this.type = type;
   this.location = location;
   this.errors = [];
   this.warnings = [];
 }
--- a/devtools/shared/builtin-modules.js
+++ b/devtools/shared/builtin-modules.js
@@ -13,17 +13,17 @@
  * they would also miss them.
  */
 
 const { Cu, CC, Cc, Ci } = require("chrome");
 const promise = require("resource://gre/modules/Promise.jsm").Promise;
 const jsmScope = require("resource://gre/modules/Services.jsm");
 const { Services } = jsmScope;
 // Steal various globals only available in JSM scope (and not Sandbox one)
-const { ChromeUtils, HeapSnapshot,
+const { ChromeUtils, HeapSnapshot, XMLHttpRequest
         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, CSSRule, FileReader, indexedDB, InspectorUtils, URL } =
     Cu.Sandbox(CC("@mozilla.org/systemprincipal;1", "nsIPrincipal")(), {
       wantGlobalProperties: [
         "CSS", "CSSRule", "FileReader", "indexedDB", "InspectorUtils", "URL",
@@ -226,21 +226,17 @@ exports.globals = {
     lazyGetter: defineLazyGetter,
     lazyImporter: defineLazyModuleGetter,
     lazyServiceGetter: defineLazyServiceGetter,
     lazyRequireGetter: lazyRequireGetter,
     // Defined by Loader.jsm
     id: null
   },
 
-  // Let new XMLHttpRequest do the right thing.
-  XMLHttpRequest: function () {
-    return Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
-           .createInstance(Ci.nsIXMLHttpRequest);
-  },
+  XMLHttpRequest: XMLHttpRequest,
 
   Node: Ci.nsIDOMNode,
   Element: Ci.nsIDOMElement,
   DocumentFragment: Ci.nsIDOMDocumentFragment,
 
   // Make sure `define` function exists.  This allows defining some modules
   // in AMD format while retaining CommonJS compatibility through this hook.
   // JSON Viewer needs modules in AMD format, as it currently uses RequireJS
--- a/devtools/shared/fronts/device.js
+++ b/devtools/shared/fronts/device.js
@@ -1,32 +1,31 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 "use strict";
 
-const {Cc, Ci, Cu} = require("chrome");
+const {Cu} = require("chrome");
 const {deviceSpec} = require("devtools/shared/specs/device");
 const protocol = require("devtools/shared/protocol");
 const defer = require("devtools/shared/defer");
 
 const DeviceFront = protocol.FrontClassWithSpec(deviceSpec, {
   initialize: function (client, form) {
     protocol.Front.prototype.initialize.call(this, client);
     this.actorID = form.deviceActor;
     this.manage(this);
   },
 
   screenshotToBlob: function () {
     return this.screenshotToDataURL().then(longstr => {
       return longstr.string().then(dataURL => {
         let deferred = defer();
         longstr.release().catch(Cu.reportError);
-        let req = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
-            .createInstance(Ci.nsIXMLHttpRequest);
+        let req = new XMLHttpRequest();
         req.open("GET", dataURL, true);
         req.responseType = "blob";
         req.onload = () => {
           deferred.resolve(req.response);
         };
         req.onerror = () => {
           deferred.reject(req.status);
         };
--- a/devtools/shared/gcli/commands/jsb.js
+++ b/devtools/shared/gcli/commands/jsb.js
@@ -1,17 +1,15 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 "use strict";
 
-const { Cc } = require("chrome");
 const l10n = require("gcli/l10n");
-const XMLHttpRequest = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"];
 
 loader.lazyImporter(this, "Preferences", "resource://gre/modules/Preferences.jsm");
 loader.lazyImporter(this, "ScratchpadManager", "resource://devtools/client/scratchpad/scratchpad-manager.jsm");
 
 loader.lazyRequireGetter(this, "beautify", "devtools/shared/jsbeautify/beautify");
 
 exports.items = [
   {
--- a/devtools/shared/gcli/commands/screenshot.js
+++ b/devtools/shared/gcli/commands/screenshot.js
@@ -423,20 +423,18 @@ function saveToClipboard(context, reply)
   });
 }
 
 /**
  * Upload screenshot data to Imgur, returning a promise of a URL (as a string)
  */
 function uploadToImgur(reply) {
   return new Promise((resolve, reject) => {
-    const xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
-                  .createInstance(Ci.nsIXMLHttpRequest);
-    const fd = Cc["@mozilla.org/files/formdata;1"]
-                  .createInstance(Ci.nsIDOMFormData);
+    const xhr = new XMLHttpRequest();
+    const fd = new FormData();
     fd.append("image", reply.data.split(",")[1]);
     fd.append("type", "base64");
     fd.append("title", reply.filename);
 
     const postURL = Services.prefs.getCharPref("devtools.gcli.imgurUploadURL");
     const clientID = "Client-ID " +
                      Services.prefs.getCharPref("devtools.gcli.imgurClientID");
 
--- a/devtools/shared/gcli/source/lib/gcli/util/host.js
+++ b/devtools/shared/gcli/source/lib/gcli/util/host.js
@@ -11,18 +11,19 @@
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 'use strict';
 
-var Cc = require('chrome').Cc;
-var Ci = require('chrome').Ci;
+const { Cc, Ci, Cu } = require("chrome");
+
+Cu.importGlobalProperties(["XMLHttpRequest"]);
 
 var { Task } = require("devtools/shared/task");
 
 var util = require('./util');
 
 function Highlighter(document) {
   this._document = document;
   this._nodes = util.createEmptyNodeList(this._document);
@@ -105,18 +106,17 @@ exports.staticRequire = function(requist
     return Promise.resolve('');
   }
   else {
     return new Promise((resolve, reject) => {
       var filename = resourceDirName(requistingModule.id) + '/' + name;
       filename = filename.replace(/\/\.\//g, '/');
       filename = 'resource://devtools/shared/gcli/source/lib/' + filename;
 
-      var xhr = Cc['@mozilla.org/xmlextras/xmlhttprequest;1']
-                  .createInstance(Ci.nsIXMLHttpRequest);
+      var xhr = new XMLHttpRequest();
 
       xhr.onload = () => {
         resolve(xhr.responseText);
       };
 
       xhr.onabort = xhr.onerror = xhr.ontimeout = err => {
         reject(err);
       };