Bug 792808 - Change browser/[components|modules|experiments] to import and instantiate XHRs from global properties rather than using Cc.createInstance(Ci.nsIXMLHttpRequest); r?mconley
MozReview-Commit-ID: 66ubjdJssU9
--- a/browser/components/translation/BingTranslator.jsm
+++ b/browser/components/translation/BingTranslator.jsm
@@ -7,16 +7,18 @@
this.EXPORTED_SYMBOLS = [ "BingTranslator" ];
ChromeUtils.import("resource://gre/modules/Services.jsm");
ChromeUtils.import("resource://gre/modules/Log.jsm");
ChromeUtils.import("resource://gre/modules/PromiseUtils.jsm");
ChromeUtils.import("resource://services-common/utils.js");
ChromeUtils.import("resource://gre/modules/Http.jsm");
+Cu.importGlobalProperties(["XMLHttpRequest"]);
+
// The maximum amount of net data allowed per request on Bing's API.
const MAX_REQUEST_DATA = 5000; // Documentation says 10000 but anywhere
// close to that is refused by the service.
// The maximum number of chunks allowed to be translated in a single
// request.
const MAX_REQUEST_CHUNKS = 1000; // Documentation says 2000.
@@ -124,17 +126,17 @@ this.BingTranslator.prototype = {
* service is unavailable (zero balance on the key or request credentials are
* not in an active state) and calling the function to resolve the promise
* returned by the public `translate()` method when there's no pending.
* request left.
*
* @param aError [optional] The XHR object of the request that failed.
*/
_chunkFailed(aError) {
- if (aError instanceof Ci.nsIXMLHttpRequest &&
+ if (aError instanceof XMLHttpRequest &&
[400, 401].includes(aError.status)) {
let body = aError.responseText;
if (body && body.includes("TranslateApiException") &&
(body.includes("balance") || body.includes("active state")))
this._serviceUnavailable = true;
}
this._checkIfFinished();
--- a/browser/components/translation/YandexTranslator.jsm
+++ b/browser/components/translation/YandexTranslator.jsm
@@ -7,16 +7,18 @@
this.EXPORTED_SYMBOLS = [ "YandexTranslator" ];
ChromeUtils.import("resource://gre/modules/Services.jsm");
ChromeUtils.import("resource://gre/modules/Log.jsm");
ChromeUtils.import("resource://gre/modules/PromiseUtils.jsm");
ChromeUtils.import("resource://services-common/utils.js");
ChromeUtils.import("resource://gre/modules/Http.jsm");
+Cu.importGlobalProperties(["XMLHttpRequest"]);
+
// The maximum amount of net data allowed per request on Bing's API.
const MAX_REQUEST_DATA = 5000; // Documentation says 10000 but anywhere
// close to that is refused by the service.
// The maximum number of chunks allowed to be translated in a single
// request.
const MAX_REQUEST_CHUNKS = 1000; // Documentation says 2000.
@@ -133,17 +135,17 @@ this.YandexTranslator.prototype = {
* service is unavailable (zero balance on the key or request credentials are
* not in an active state) and calling the function to resolve the promise
* returned by the public `translate()` method when there are no pending
* requests left.
*
* @param aError [optional] The XHR object of the request that failed.
*/
_chunkFailed(aError) {
- if (aError instanceof Ci.nsIXMLHttpRequest) {
+ if (aError instanceof XMLHttpRequest) {
let body = aError.responseText;
let json = { code: 0 };
try {
json = JSON.parse(body);
} catch (e) {}
if (json.code && YANDEX_PERMANENT_ERRORS.includes(json.code))
this._serviceUnavailable = true;
--- a/browser/experiments/Experiments.jsm
+++ b/browser/experiments/Experiments.jsm
@@ -9,16 +9,18 @@ this.EXPORTED_SYMBOLS = [
];
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
ChromeUtils.import("resource://gre/modules/Services.jsm");
ChromeUtils.import("resource://gre/modules/osfile.jsm");
ChromeUtils.import("resource://gre/modules/Log.jsm");
ChromeUtils.import("resource://gre/modules/AsyncShutdown.jsm");
+Cu.importGlobalProperties(["XMLHttpRequest"]);
+
ChromeUtils.defineModuleGetter(this, "UpdateUtils",
"resource://gre/modules/UpdateUtils.jsm");
ChromeUtils.defineModuleGetter(this, "AddonManager",
"resource://gre/modules/AddonManager.jsm");
ChromeUtils.defineModuleGetter(this, "AddonManagerPrivate",
"resource://gre/modules/AddonManager.jsm");
ChromeUtils.defineModuleGetter(this, "TelemetryEnvironment",
"resource://gre/modules/TelemetryEnvironment.jsm");
@@ -932,17 +934,17 @@ Experiments.Experiments.prototype = {
},
/*
* Helper function to make HTTP GET requests. Returns a promise that is resolved with
* the responseText when the request is complete.
*/
_httpGetRequest(url) {
this._log.trace("httpGetRequest(" + url + ")");
- let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
+ let xhr = new XMLHttpRequest();
this._networkRequest = xhr;
return new Promise((resolve, reject) => {
let log = this._log;
let errorhandler = (evt) => {
log.error("httpGetRequest::onError() - Error making request to " + url + ": " + evt.type);
reject(new Error("Experiments - XHR error for " + url + " - " + evt.type));
--- a/browser/modules/ContentSearch.jsm
+++ b/browser/modules/ContentSearch.jsm
@@ -5,16 +5,18 @@
this.EXPORTED_SYMBOLS = [
"ContentSearch",
];
ChromeUtils.import("resource://gre/modules/Services.jsm");
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
+Cu.importGlobalProperties(["XMLHttpRequest"]);
+
ChromeUtils.defineModuleGetter(this, "FormHistory",
"resource://gre/modules/FormHistory.jsm");
ChromeUtils.defineModuleGetter(this, "PrivateBrowsingUtils",
"resource://gre/modules/PrivateBrowsingUtils.jsm");
ChromeUtils.defineModuleGetter(this, "SearchSuggestionController",
"resource://gre/modules/SearchSuggestionController.jsm");
const INBOUND_MESSAGE = "ContentSearch";
@@ -512,18 +514,17 @@ this.ContentSearch = {
return obj;
},
_arrayBufferFromDataURI(uri) {
if (!uri) {
return Promise.resolve(null);
}
return new Promise(resolve => {
- let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].
- createInstance(Ci.nsIXMLHttpRequest);
+ let xhr = new XMLHttpRequest();
xhr.open("GET", uri, true);
xhr.responseType = "arraybuffer";
xhr.onload = () => {
resolve(xhr.response);
};
xhr.onerror = xhr.onabort = xhr.ontimeout = () => {
resolve(null);
};
--- a/browser/modules/test/browser/browser_ContentSearch.js
+++ b/browser/modules/test/browser/browser_ContentSearch.js
@@ -1,16 +1,19 @@
/* 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/. */
const TEST_MSG = "ContentSearchTest";
const CONTENT_SEARCH_MSG = "ContentSearch";
const TEST_CONTENT_SCRIPT_BASENAME = "contentSearch.js";
+const { utils: Cu } = Components;
+Cu.importGlobalProperties(["XMLHttpRequest"]);
+
var gMsgMan;
/* import-globals-from ../../../components/search/test/head.js */
Services.scriptloader.loadSubScript(
"chrome://mochitests/content/browser/browser/components/search/test/head.js",
this);
let originalEngine = Services.search.currentEngine;
@@ -388,18 +391,17 @@ var currentEngineObj = async function()
};
};
function arrayBufferFromDataURI(uri) {
if (!uri) {
return Promise.resolve(null);
}
return new Promise(resolve => {
- let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].
- createInstance(Ci.nsIXMLHttpRequest);
+ let xhr = new XMLHttpRequest();
xhr.open("GET", uri, true);
xhr.responseType = "arraybuffer";
xhr.onerror = () => {
resolve(null);
};
xhr.onload = () => {
resolve(xhr.response);
};