Bug 1419471 - Cleanup after removing UTF-16 support. draft
authorOriol Brufau <oriol-bugzilla@hotmail.com>
Fri, 24 Nov 2017 21:43:49 +0100
changeset 703275 ee4df6795bc4d9094add06d4a9b1171a32bd9df0
parent 703179 f2e36fbb90fad27c034105709a4cbbaef9bd58c8
child 741735 f7c1b9481a11bb1f9bc3d5ee1a83c8dcdd5c2bef
push id90778
push userbmo:oriol-bugzilla@hotmail.com
push dateFri, 24 Nov 2017 20:44:31 +0000
bugs1419471
milestone59.0a1
Bug 1419471 - Cleanup after removing UTF-16 support. MozReview-Commit-ID: 39N2Sb9G0Ab
devtools/client/jsonview/converter-child.js
devtools/client/jsonview/test/browser.ini
devtools/client/jsonview/test/browser_jsonview_encoding.js
--- a/devtools/client/jsonview/converter-child.js
+++ b/devtools/client/jsonview/converter-child.js
@@ -19,17 +19,16 @@ loader.lazyGetter(this, "debug", functio
 
 const childProcessMessageManager =
   Cc["@mozilla.org/childprocessmessagemanager;1"]
     .getService(Ci.nsISyncMessageSender);
 const BinaryInput = CC("@mozilla.org/binaryinputstream;1",
                        "nsIBinaryInputStream", "setInputStream");
 const BufferStream = CC("@mozilla.org/io/arraybuffer-input-stream;1",
                        "nsIArrayBufferInputStream", "setData");
-const encodingLength = 0;
 
 // Localization
 loader.lazyGetter(this, "jsonViewStrings", () => {
   return Services.strings.createBundle(
     "chrome://devtools/locale/jsonview.properties");
 });
 
 /**
@@ -66,45 +65,31 @@ Converter.prototype = {
     return fromStream;
   },
 
   asyncConvertData: function (fromType, toType, listener, ctx) {
     this.listener = listener;
   },
 
   onDataAvailable: function (request, context, inputStream, offset, count) {
-    // If the encoding is not known, store data in an array until we have enough bytes.
-    if (this.encodingArray) {
-      let desired = encodingLength - this.encodingArray.length;
-      let n = Math.min(desired, count);
-      let bytes = new BinaryInput(inputStream).readByteArray(n);
-      offset += n;
-      count -= n;
-      this.encodingArray.push(...bytes);
-      if (n < desired) {
-        // Wait until there is more data.
-        return;
-      }
-      this.determineEncoding(request, context);
-    }
-
     // Decode and insert data.
     let buffer = new ArrayBuffer(count);
     new BinaryInput(inputStream).readArrayBuffer(count, buffer);
-    this.decodeAndInsertBuffer(request, context, buffer);
+    this.decodeAndInsertBuffer(buffer);
   },
 
   onStartRequest: function (request, context) {
     // Set the content type to HTML in order to parse the doctype, styles
     // and scripts. The JSON will be manually inserted as text.
     request.QueryInterface(Ci.nsIChannel);
     request.contentType = "text/html";
 
     // Don't honor the charset parameter and use UTF-8 (see bug 741776).
     request.contentCharset = "UTF-8";
+    this.decoder = new TextDecoder("UTF-8");
 
     // Changing the content type breaks saving functionality. Fix it.
     fixSave(request);
 
     // Because content might still have a reference to this window,
     // force setting it to a null principal to avoid it being same-
     // origin with (other) content.
     request.loadInfo.resetPrincipalToInheritToNullPrincipal();
@@ -118,54 +103,31 @@ Converter.prototype = {
     insertJsonData(win, this.data.json);
     win.addEventListener("contentMessage", onContentMessage, false, true);
     keepThemeUpdated(win);
 
     // Send the initial HTML code.
     let buffer = new TextEncoder().encode(initialHTML(win.document)).buffer;
     let stream = new BufferStream(buffer, 0, buffer.byteLength);
     this.listener.onDataAvailable(request, context, stream, 0, stream.available());
-
-    // Create an array to store data until the encoding is determined.
-    this.encodingArray = [];
   },
 
   onStopRequest: function (request, context, statusCode) {
     // Flush data.
-    if (this.encodingArray) {
-      this.determineEncoding(request, context, true);
-    } else {
-      this.decodeAndInsertBuffer(request, context, new ArrayBuffer(0), true);
-    }
+    this.decodeAndInsertBuffer(new ArrayBuffer(0), true);
 
     // Stop the request.
     this.listener.onStopRequest(request, context, statusCode);
     this.listener = null;
     this.decoder = null;
     this.data = null;
   },
 
-  // Determines the encoding of the response.
-  determineEncoding: function (request, context, flush = false) {
-    // Always use UTF-8
-    let encoding = "UTF-8";
-    let bytes = this.encodingArray;
-
-    // Create a decoder for that encoding.
-    this.decoder = new TextDecoder(encoding);
-    this.data.encoding = encoding;
-
-    // Decode and insert the bytes in encodingArray, and remove it.
-    let buffer = new Uint8Array(bytes).buffer;
-    this.decodeAndInsertBuffer(request, context, buffer, flush);
-    this.encodingArray = null;
-  },
-
   // Decodes an ArrayBuffer into a string and inserts it into the page.
-  decodeAndInsertBuffer: function (request, context, buffer, flush = false) {
+  decodeAndInsertBuffer: function (buffer, flush = false) {
     // Decode the buffer into a string.
     let data = this.decoder.decode(buffer, {stream: !flush});
 
     // Using `appendData` instead of `textContent +=` is important to avoid
     // repainting previous data.
     this.data.json.appendData(data);
   }
 };
--- a/devtools/client/jsonview/test/browser.ini
+++ b/devtools/client/jsonview/test/browser.ini
@@ -32,17 +32,16 @@ skip-if = (os == 'linux' && bits == 32 &
 subsuite = clipboard
 skip-if = (os == 'linux' && bits == 32 && debug) # bug 1328915, disable linux32 debug devtools for timeouts
 [browser_jsonview_copy_rawdata.js]
 subsuite = clipboard
 skip-if = (os == 'linux' && bits == 32 && debug) # bug 1328915, disable linux32 debug devtools for timeouts
 [browser_jsonview_csp_json.js]
 [browser_jsonview_empty_object.js]
 [browser_jsonview_encoding.js]
-skip-if = true # bug 1419416, remove UTF-16 support
 [browser_jsonview_filter.js]
 [browser_jsonview_invalid_json.js]
 [browser_jsonview_manifest.js]
 [browser_jsonview_nojs.js]
 [browser_jsonview_nul.js]
 [browser_jsonview_object-type.js]
 [browser_jsonview_row_selection.js]
 [browser_jsonview_save_json.js]
--- a/devtools/client/jsonview/test/browser_jsonview_encoding.js
+++ b/devtools/client/jsonview/test/browser_jsonview_encoding.js
@@ -3,78 +3,57 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
 "use strict";
 
 add_task(function* () {
   info("Test JSON encoding started");
 
-  const text = Symbol("text");
-
+  const bom = "%EF%BB%BF"; // UTF-8 BOM
   const tests = [
     {
-      "UTF-8 with BOM": "",
-      "UTF-16BE with BOM": "",
-      "UTF-16LE with BOM": "",
-      [text]: ""
+      input: bom,
+      output: ""
+    }, {
+      input: "%FE%FF", // UTF-16BE BOM
+      output: "\uFFFD\uFFFD"
     }, {
-      "UTF-8": "%30",
-      "UTF-16BE": "%00%30",
-      "UTF-16LE": "%30%00",
-      [text]: "0"
+      input: "%FF%FE", // UTF-16LE BOM
+      output: "\uFFFD\uFFFD"
     }, {
-      "UTF-8": "%30%FF",
-      "UTF-16BE": "%00%30%00",
-      "UTF-16LE": "%30%00%00",
-      [text]: "0\uFFFD" // 0�
+      input: bom + "%30",
+      output: "0"
+    }, {
+      input: bom + bom,
+      output: "\uFEFF"
     }, {
-      "UTF-8": "%C3%A0",
-      "UTF-16BE": "%00%E0",
-      "UTF-16LE": "%E0%00",
-      [text]: "\u00E0" // à
+      input: "%00%61",
+      output: "\u0000a"
+    }, {
+      input: "%61%00",
+      output: "a\u0000"
+    }, {
+      input: "%30%FF",
+      output: "0\uFFFD" // 0�
     }, {
-      "UTF-8 with BOM": "%E2%9D%A4",
-      "UTF-16BE with BOM": "%27%64",
-      "UTF-16LE with BOM": "%64%27",
-      [text]: "\u2764" // ❤
+      input: "%C3%A0",
+      output: "\u00E0" // à
     }, {
-      "UTF-8": "%30%F0%9F%9A%80",
-      "UTF-16BE": "%00%30%D8%3D%DE%80",
-      "UTF-16LE": "%30%00%3D%D8%80%DE",
-      [text]: "0\uD83D\uDE80" // 0🚀
+      input: "%E2%9D%A4",
+      output: "\u2764" // ❤
+    }, {
+      input: "%F0%9F%9A%80",
+      output: "\uD83D\uDE80" // 🚀
     }
   ];
 
-  const bom = {
-    "UTF-8": "%EF%BB%BF",
-    "UTF-16BE": "%FE%FF",
-    "UTF-16LE": "%FF%FE"
-  };
-
-  // Test double BOM.
-  tests.push(Object.entries(bom).reduce((obj, [prop, value]) => {
-    obj[prop + " with BOM"] = value;
-    return obj;
-  }, {[text]: "\uFEFF"}));
+  for (let {input, output} of tests) {
+    info("Test decoding of " + JSON.stringify(input) + ".");
 
-  for (let test of tests) {
-    let result = test[text];
-    for (let [encoding, data] of Object.entries(test)) {
-      info("Testing " + JSON.stringify(result) + " encoded in " + encoding + ".");
-
-      if (encoding.endsWith("BOM")) {
-        encoding = encoding.split(" ")[0];
-        data = bom[encoding] + data;
-      }
+    yield addJsonViewTab("data:application/json," + input);
+    yield selectJsonViewContentTab("rawdata");
 
-      yield addJsonViewTab("data:application/json," + data);
-      yield selectJsonViewContentTab("rawdata");
-
-      // Check encoding.
-      is(yield evalInContent("JSONView.encoding"), encoding, "Got the right encoding.");
-
-      // Check displayed data.
-      let output = yield getElementText(".textPanelBox .data");
-      is(output, result, "The right data has been received.");
-    }
+    // Check displayed data.
+    let data = yield getElementText(".textPanelBox .data");
+    is(data, output, "The right data has been received.");
   }
 });