Bug 1772718 - Reduce mozilla/reject-scriptableunicodeconverter eslint warnings in DevTools code r=nchevobbe

Differential Revision: https://phabricator.services.mozilla.com/D148346
This commit is contained in:
Michael Ratcliffe 2022-06-16 18:24:17 +00:00
Родитель 36a20b7fab
Коммит 7860244360
9 изменённых файлов: 37 добавлений и 51 удалений

Просмотреть файл

@ -1997,3 +1997,13 @@ async function closeRDM(tab, options) {
await manager.closeIfNeeded(tab.ownerGlobal, tab, options); await manager.closeIfNeeded(tab.ownerGlobal, tab, options);
info("Responsive design mode closed"); info("Responsive design mode closed");
} }
function getInputStream(data) {
const BufferStream = CC(
"@mozilla.org/io/arraybuffer-input-stream;1",
"nsIArrayBufferInputStream",
"setData"
);
const buffer = new TextEncoder().encode(data).buffer;
return new BufferStream(buffer, 0, buffer.byteLength);
}

Просмотреть файл

@ -20,6 +20,16 @@ const EventEmitter = require("devtools/shared/event-emitter");
const lazy = {}; const lazy = {};
loader.lazyGetter(lazy, "BufferStream", () => {
const { CC } = require("chrome");
return CC(
"@mozilla.org/io/arraybuffer-input-stream;1",
"nsIArrayBufferInputStream",
"setData"
);
});
loader.lazyRequireGetter( loader.lazyRequireGetter(
lazy, lazy,
"FileUtils", "FileUtils",
@ -752,11 +762,8 @@ StyleSheetEditor.prototype = {
} }
const ostream = lazy.FileUtils.openSafeFileOutputStream(returnFile); const ostream = lazy.FileUtils.openSafeFileOutputStream(returnFile);
const converter = Cc[ const buffer = new TextEncoder().encode(this._state.text).buffer;
"@mozilla.org/intl/scriptableunicodeconverter" const istream = new lazy.BufferStream(buffer, 0, buffer.byteLength);
].createInstance(Ci.nsIScriptableUnicodeConverter);
converter.charset = "UTF-8";
const istream = converter.convertToInputStream(this._state.text);
lazy.NetUtil.asyncCopy(istream, ostream, status => { lazy.NetUtil.asyncCopy(istream, ostream, status => {
if (!Components.isSuccessCode(status)) { if (!Components.isSuccessCode(status)) {

Просмотреть файл

@ -81,13 +81,7 @@ function read(srcChromeURL) {
} }
function write(data, file, callback) { function write(data, file, callback) {
const converter = Cc[ const istream = getInputStream(data);
"@mozilla.org/intl/scriptableunicodeconverter"
].createInstance(Ci.nsIScriptableUnicodeConverter);
converter.charset = "UTF-8";
const istream = converter.convertToInputStream(data);
const ostream = FileUtils.openSafeFileOutputStream(file); const ostream = FileUtils.openSafeFileOutputStream(file);
NetUtil.asyncCopy(istream, ostream, function(status) { NetUtil.asyncCopy(istream, ostream, function(status) {

Просмотреть файл

@ -39,11 +39,8 @@ function importSheet(ui, panelWindow) {
// create file to import first // create file to import first
const file = FileUtils.getFile("ProfD", [FILENAME]); const file = FileUtils.getFile("ProfD", [FILENAME]);
const ostream = FileUtils.openSafeFileOutputStream(file); const ostream = FileUtils.openSafeFileOutputStream(file);
const converter = Cc[ const istream = getInputStream(SOURCE);
"@mozilla.org/intl/scriptableunicodeconverter"
].createInstance(Ci.nsIScriptableUnicodeConverter);
converter.charset = "UTF-8";
const istream = converter.convertToInputStream(SOURCE);
NetUtil.asyncCopy(istream, ostream, function() { NetUtil.asyncCopy(istream, ostream, function() {
FileUtils.closeSafeFileOutputStream(ostream); FileUtils.closeSafeFileOutputStream(ostream);

Просмотреть файл

@ -145,13 +145,7 @@ function read(srcChromeURL) {
function write(data, file) { function write(data, file) {
return new Promise(resolve => { return new Promise(resolve => {
const converter = Cc[ const istream = getInputStream(data);
"@mozilla.org/intl/scriptableunicodeconverter"
].createInstance(Ci.nsIScriptableUnicodeConverter);
converter.charset = "UTF-8";
const istream = converter.convertToInputStream(data);
const ostream = FileUtils.openSafeFileOutputStream(file); const ostream = FileUtils.openSafeFileOutputStream(file);
NetUtil.asyncCopy(istream, ostream, function(status) { NetUtil.asyncCopy(istream, ostream, function(status) {

Просмотреть файл

@ -74,17 +74,8 @@ async function createLocalFile() {
return file; return file;
} }
function getUnicodeConverter() {
const className = "@mozilla.org/intl/scriptableunicodeconverter";
const converter = Cc[className].createInstance(
Ci.nsIScriptableUnicodeConverter
);
converter.charset = "UTF-8";
return converter;
}
function writeInFile(string, file) { function writeInFile(string, file) {
const inputStream = getUnicodeConverter().convertToInputStream(string); const inputStream = getInputStream(string);
const outputStream = FileUtils.openSafeFileOutputStream(file); const outputStream = FileUtils.openSafeFileOutputStream(file);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {

Просмотреть файл

@ -46,16 +46,6 @@ const UPDATE_PORT = 50625;
const ADDRESS = "224.0.0.115"; const ADDRESS = "224.0.0.115";
const REPLY_TIMEOUT = 5000; const REPLY_TIMEOUT = 5000;
const { XPCOMUtils } = require("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.defineLazyGetter(this, "converter", () => {
const conv = Cc[
"@mozilla.org/intl/scriptableunicodeconverter"
].createInstance(Ci.nsIScriptableUnicodeConverter);
conv.charset = "utf8";
return conv;
});
var logging = Services.prefs.getBoolPref("devtools.discovery.log"); var logging = Services.prefs.getBoolPref("devtools.discovery.log");
function log(msg) { function log(msg) {
if (logging) { if (logging) {
@ -96,7 +86,7 @@ Transport.prototype = {
log("Send to " + port + ":\n" + JSON.stringify(object, null, 2)); log("Send to " + port + ":\n" + JSON.stringify(object, null, 2));
} }
const message = JSON.stringify(object); const message = JSON.stringify(object);
const rawMessage = converter.convertToByteArray(message); const rawMessage = Uint8Array.from(message, x => x.charCodeAt(0));
try { try {
this.socket.send(ADDRESS, port, rawMessage, rawMessage.length); this.socket.send(ADDRESS, port, rawMessage, rawMessage.length);
} catch (e) { } catch (e) {

Просмотреть файл

@ -123,6 +123,9 @@ var NetworkHelper = {
* Converted text. * Converted text.
*/ */
convertToUnicode: function(text, charset) { convertToUnicode: function(text, charset) {
// FIXME: We need to throw when text can't be converted e.g. the contents of
// an image. Until we have a way to do so with TextEncoder and TextDecoder
// we need to use nsIScriptableUnicodeConverter instead.
const conv = Cc[ const conv = Cc[
"@mozilla.org/intl/scriptableunicodeconverter" "@mozilla.org/intl/scriptableunicodeconverter"
].createInstance(Ci.nsIScriptableUnicodeConverter); ].createInstance(Ci.nsIScriptableUnicodeConverter);

Просмотреть файл

@ -33,12 +33,12 @@ function doFileActivity()
const fout = FileUtils.openSafeFileOutputStream(gTmpFile, const fout = FileUtils.openSafeFileOutputStream(gTmpFile,
FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE | FileUtils.MODE_TRUNCATE); FileUtils.MODE_WRONLY | FileUtils.MODE_CREATE | FileUtils.MODE_TRUNCATE);
const converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"]. const stream = Cc[
createInstance(Ci.nsIScriptableUnicodeConverter); "@mozilla.org/io/arraybuffer-input-stream;1"
converter.charset = "UTF-8"; ].createInstance(Ci.nsIArrayBufferInputStream);
const fileContentStream = converter.convertToInputStream(fileContent); const buffer = new TextEncoder().encode(fileContent).buffer;
stream.setData(buffer, 0, buffer.byteLength);
NetUtil.asyncCopy(fileContentStream, fout, addIframe); NetUtil.asyncCopy(stream, fout, addIframe);
} }
function addIframe(aStatus) function addIframe(aStatus)