Bug 1658048 - Remove obsolete array buffer utilities for chat code. r=khushil
This commit is contained in:
Родитель
879d82a4d8
Коммит
a046c3c4ab
|
@ -1,74 +0,0 @@
|
|||
/* 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/. */
|
||||
|
||||
/*
|
||||
* JavaScript ArrayBuffers are missing a variety of useful methods which are
|
||||
* provided by this module.
|
||||
*/
|
||||
|
||||
const EXPORTED_SYMBOLS = [
|
||||
"copyBytes",
|
||||
"ArrayBufferToBytes",
|
||||
"BytesToArrayBuffer",
|
||||
"StringToBytes",
|
||||
"StringToArrayBuffer",
|
||||
"ArrayBufferToString",
|
||||
"ArrayBufferToHexString",
|
||||
];
|
||||
|
||||
/*
|
||||
* aTarget / aSource are ArrayBuffers.
|
||||
*
|
||||
* Note that this is very similar to ArrayBuffer.slice except that it allows
|
||||
* for an offset in the target as well as the source.
|
||||
*/
|
||||
function copyBytes(
|
||||
aTarget,
|
||||
aSource,
|
||||
aTargetOffset = 0,
|
||||
aSourceOffset = 0,
|
||||
aLength = aSource.byteLength
|
||||
) {
|
||||
// The rest just gets the data copied into it.
|
||||
let view = new Uint8Array(aTarget, aTargetOffset);
|
||||
view.set(new Uint8Array(aSource, aSourceOffset, aLength));
|
||||
}
|
||||
|
||||
function ArrayBufferToBytes(aBuf) {
|
||||
return [...new Uint8Array(aBuf)];
|
||||
}
|
||||
|
||||
function BytesToArrayBuffer(aBytes = []) {
|
||||
let buf = new ArrayBuffer(aBytes.length);
|
||||
let view = new Uint8Array(buf);
|
||||
view.set(aBytes);
|
||||
return buf;
|
||||
}
|
||||
|
||||
function StringToBytes(aString) {
|
||||
let bytes = [];
|
||||
for (let i in aString) {
|
||||
bytes.push(aString.charCodeAt(i));
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
function StringToArrayBuffer(aString) {
|
||||
return BytesToArrayBuffer(StringToBytes(aString));
|
||||
}
|
||||
|
||||
function ArrayBufferToString(aData) {
|
||||
return Array.from(new Uint8Array(aData))
|
||||
.map(b => String.fromCharCode(b))
|
||||
.join("");
|
||||
}
|
||||
|
||||
function ArrayBufferToHexString(aData) {
|
||||
return (
|
||||
"0x" +
|
||||
Array.from(new Uint8Array(aData))
|
||||
.map(b => ("0" + b.toString(16)).slice(-2))
|
||||
.join(" ")
|
||||
);
|
||||
}
|
|
@ -6,7 +6,6 @@
|
|||
XPCSHELL_TESTS_MANIFESTS += ['test/xpcshell.ini']
|
||||
|
||||
EXTRA_JS_MODULES += [
|
||||
'ArrayBufferUtils.jsm',
|
||||
'CLib.jsm',
|
||||
'hiddenWindow.jsm',
|
||||
'imContentSink.jsm',
|
||||
|
|
|
@ -4,14 +4,13 @@
|
|||
|
||||
/*
|
||||
* Combines a lot of the Mozilla networking interfaces into a sane interface for
|
||||
* simple(r) use of sockets code.
|
||||
* simple(r) handling of a low-level socket which sends text content.
|
||||
*
|
||||
* This implements nsIServerSocketListener, nsIStreamListener,
|
||||
* nsIRequestObserver, nsITransportEventSink and nsIProtocolProxyCallback.
|
||||
* This implements nsIStreamListener, nsIRequestObserver, nsITransportEventSink
|
||||
* and nsIProtocolProxyCallback.
|
||||
*
|
||||
* This uses nsIRoutedSocketTransportService, nsIServerSocket, nsIThreadManager,
|
||||
* nsIBinaryInputStream, nsIScriptableInputStream, nsIInputStreamPump,
|
||||
* nsIProxyService, nsIProxyInfo.
|
||||
* nsIScriptableInputStream, nsIInputStreamPump, nsIProxyService, nsIProxyInfo.
|
||||
*
|
||||
* High-level methods:
|
||||
* connect(<originHost>, <originPort>[, ("starttls" | "ssl" | "udp")
|
||||
|
@ -19,13 +18,11 @@
|
|||
* disconnect()
|
||||
* sendData(String <data>[, <logged data>])
|
||||
* sendString(String <data>[, <encoding>[, <logged data>]])
|
||||
* sendBinaryData(<arraybuffer data>)
|
||||
* startTLS()
|
||||
* resetPingTimer()
|
||||
* cancelDisconnectTimer()
|
||||
*
|
||||
* High-level properties:
|
||||
* binaryMode
|
||||
* delimiter
|
||||
* inputSegmentSize
|
||||
* outputSegmentSize
|
||||
|
@ -44,7 +41,6 @@
|
|||
* onBadCertificate(boolean aIsSslError, AString aNSSErrorMessage)
|
||||
* onConnectionClosed()
|
||||
* onDataReceived(String <data>)
|
||||
* <length handled> = onBinaryDataReceived(ArrayBuffer <data>)
|
||||
* onTransportStatus(nsISocketTransport <transport>, nsresult <status>,
|
||||
* unsigned long <progress>, unsigned long <progress max>)
|
||||
* sendPing()
|
||||
|
@ -76,9 +72,6 @@
|
|||
const EXPORTED_SYMBOLS = ["Socket"];
|
||||
|
||||
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
||||
var { ArrayBufferToBytes, ArrayBufferToHexString } = ChromeUtils.import(
|
||||
"resource:///modules/ArrayBufferUtils.jsm"
|
||||
);
|
||||
var { setTimeout, clearTimeout, executeSoon } = ChromeUtils.import(
|
||||
"resource:///modules/imXPCOMUtils.jsm"
|
||||
);
|
||||
|
@ -91,16 +84,6 @@ var NS_ERROR_MODULE_NETWORK = 2152398848;
|
|||
var NS_ERROR_NET_TIMEOUT = NS_ERROR_MODULE_NETWORK + 14;
|
||||
var NS_ERROR_NET_RESET = NS_ERROR_MODULE_NETWORK + 20;
|
||||
|
||||
var BinaryInputStream = Components.Constructor(
|
||||
"@mozilla.org/binaryinputstream;1",
|
||||
"nsIBinaryInputStream",
|
||||
"setInputStream"
|
||||
);
|
||||
var BinaryOutputStream = Components.Constructor(
|
||||
"@mozilla.org/binaryoutputstream;1",
|
||||
"nsIBinaryOutputStream",
|
||||
"setOutputStream"
|
||||
);
|
||||
var ScriptableInputStream = Components.Constructor(
|
||||
"@mozilla.org/scriptableinputstream;1",
|
||||
"nsIScriptableInputStream",
|
||||
|
@ -117,9 +100,6 @@ var ScriptableUnicodeConverter = Components.Constructor(
|
|||
);
|
||||
|
||||
var Socket = {
|
||||
// Use this to use binary mode for the
|
||||
binaryMode: false,
|
||||
|
||||
// Set this for non-binary mode to automatically parse the stream into chunks
|
||||
// separated by delimiter.
|
||||
delimiter: "",
|
||||
|
@ -274,21 +254,6 @@ var Socket = {
|
|||
}
|
||||
},
|
||||
|
||||
sendBinaryData(/* ArrayBuffer */ aData, aLoggedData) {
|
||||
this.LOG(
|
||||
"Sending binary data:\n" +
|
||||
(aLoggedData || "<" + ArrayBufferToHexString(aData) + ">")
|
||||
);
|
||||
|
||||
let byteArray = ArrayBufferToBytes(aData);
|
||||
try {
|
||||
// Send the data as a byte array
|
||||
this._binaryOutputStream.writeByteArray(byteArray, byteArray.length);
|
||||
} catch (e) {
|
||||
Cu.reportError(e);
|
||||
}
|
||||
},
|
||||
|
||||
disconnected: true,
|
||||
|
||||
startTLS() {
|
||||
|
@ -407,47 +372,25 @@ var Socket = {
|
|||
return;
|
||||
}
|
||||
this._lastAliveTime = Date.now();
|
||||
if (this.binaryMode) {
|
||||
// Load the data from the stream
|
||||
this._incomingDataBuffer = this._incomingDataBuffer.concat(
|
||||
this._binaryInputStream.readByteArray(aCount)
|
||||
);
|
||||
|
||||
let size = this._incomingDataBuffer.length;
|
||||
if (this.delimiter) {
|
||||
// Load the data from the stream.
|
||||
this._incomingDataBuffer += this._scriptableInputStream.read(aCount);
|
||||
let data = this._incomingDataBuffer.split(this.delimiter);
|
||||
|
||||
// Create a new ArrayBuffer.
|
||||
let buffer = new ArrayBuffer(size);
|
||||
let uintArray = new Uint8Array(buffer);
|
||||
|
||||
// Set the data into the array while saving the extra data.
|
||||
uintArray.set(this._incomingDataBuffer);
|
||||
|
||||
// Notify we've received data.
|
||||
// Variable data size, the callee must return how much data was handled.
|
||||
size = this.onBinaryDataReceived(buffer);
|
||||
|
||||
// Remove the handled data.
|
||||
this._incomingDataBuffer.splice(0, size);
|
||||
} else {
|
||||
if (this.delimiter) {
|
||||
// Load the data from the stream.
|
||||
this._incomingDataBuffer += this._scriptableInputStream.read(aCount);
|
||||
let data = this._incomingDataBuffer.split(this.delimiter);
|
||||
|
||||
// Store the (possibly) incomplete part.
|
||||
this._incomingDataBuffer = data.pop();
|
||||
if (!data.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Add the strings to the queue.
|
||||
this._pendingData = this._pendingData.concat(data);
|
||||
} else {
|
||||
// Add the whole string to the queue.
|
||||
this._pendingData.push(this._scriptableInputStream.read(aCount));
|
||||
// Store the (possibly) incomplete part.
|
||||
this._incomingDataBuffer = data.pop();
|
||||
if (!data.length) {
|
||||
return;
|
||||
}
|
||||
this._activateQueue();
|
||||
|
||||
// Add the strings to the queue.
|
||||
this._pendingData = this._pendingData.concat(data);
|
||||
} else {
|
||||
// Add the whole string to the queue.
|
||||
this._pendingData.push(this._scriptableInputStream.read(aCount));
|
||||
}
|
||||
this._activateQueue();
|
||||
},
|
||||
|
||||
_pendingData: [],
|
||||
|
@ -585,7 +528,7 @@ var Socket = {
|
|||
*****************************************************************************
|
||||
*/
|
||||
_resetBuffers() {
|
||||
this._incomingDataBuffer = this.binaryMode ? [] : "";
|
||||
this._incomingDataBuffer = "";
|
||||
this._outgoingDataBuffer = [];
|
||||
},
|
||||
|
||||
|
@ -654,16 +597,8 @@ var Socket = {
|
|||
throw new Error("Error getting input stream.");
|
||||
}
|
||||
|
||||
if (this.binaryMode) {
|
||||
// Handle binary mode
|
||||
this._binaryInputStream = new BinaryInputStream(this._inputStream);
|
||||
this._binaryOutputStream = new BinaryOutputStream(this._outputStream);
|
||||
} else {
|
||||
// Handle character mode
|
||||
this._scriptableInputStream = new ScriptableInputStream(
|
||||
this._inputStream
|
||||
);
|
||||
}
|
||||
// Handle character mode
|
||||
this._scriptableInputStream = new ScriptableInputStream(this._inputStream);
|
||||
|
||||
this.pump = new InputStreamPump(
|
||||
this._inputStream, // Data to read
|
||||
|
@ -708,9 +643,6 @@ var Socket = {
|
|||
// Called when ASCII data is available.
|
||||
onDataReceived(/* string */ aData) {},
|
||||
|
||||
// Called when binary data is available.
|
||||
onBinaryDataReceived(/* ArrayBuffer */ aData) {},
|
||||
|
||||
// If using the ping functionality, this is called when a new ping message
|
||||
// should be sent on the socket.
|
||||
sendPing() {},
|
||||
|
|
|
@ -1,109 +0,0 @@
|
|||
/* Any copyright is dedicated to the Public Domain.
|
||||
* http://creativecommons.org/publicdomain/zero/1.0/ */
|
||||
|
||||
var {
|
||||
ArrayBufferToBytes,
|
||||
BytesToArrayBuffer,
|
||||
StringToBytes,
|
||||
ArrayBufferToString,
|
||||
ArrayBufferToHexString,
|
||||
} = ChromeUtils.import("resource:///modules/ArrayBufferUtils.jsm");
|
||||
|
||||
function do_check_arraybuffer_eq(a, b) {
|
||||
let viewA = new Uint8Array(a);
|
||||
let viewB = new Uint8Array(b);
|
||||
|
||||
let res = a.byteLength == b.byteLength;
|
||||
for (let i = 0; i < viewA.byteLength; ++i) {
|
||||
res = res && viewA[i] == viewB[i];
|
||||
}
|
||||
|
||||
Assert.ok(res);
|
||||
}
|
||||
|
||||
function do_check_array_eq(a, b) {
|
||||
let res = a.length == b.length;
|
||||
for (let i = 0; i < a.length; ++i) {
|
||||
res = res && a[i] == b[i];
|
||||
}
|
||||
|
||||
Assert.ok(res);
|
||||
}
|
||||
|
||||
function test_ArrayBufferToBytes() {
|
||||
let expectedBytes = [0, 1, 0, 10, 0, 100, 3, 232];
|
||||
let expectedBuf = new ArrayBuffer(8);
|
||||
let view = new DataView(expectedBuf);
|
||||
view.setUint16(0, 1);
|
||||
view.setUint16(2, 10);
|
||||
view.setUint16(4, 100);
|
||||
view.setUint16(6, 1000);
|
||||
|
||||
let bytes = ArrayBufferToBytes(expectedBuf);
|
||||
do_check_array_eq(bytes, expectedBytes);
|
||||
|
||||
run_next_test();
|
||||
}
|
||||
|
||||
function test_BytesToArrayBuffer() {
|
||||
let expectedBytes = [0, 1, 0, 10, 0, 100, 3, 232];
|
||||
let expectedBuf = new ArrayBuffer(8);
|
||||
let view = new DataView(expectedBuf);
|
||||
view.setUint16(0, 1);
|
||||
view.setUint16(2, 10);
|
||||
view.setUint16(4, 100);
|
||||
view.setUint16(6, 1000);
|
||||
|
||||
let buf = BytesToArrayBuffer(expectedBytes);
|
||||
do_check_arraybuffer_eq(buf, expectedBuf);
|
||||
|
||||
run_next_test();
|
||||
}
|
||||
|
||||
function test_StringToBytes() {
|
||||
let expectedBytes = [72, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33];
|
||||
let bytes = StringToBytes("Hello world!");
|
||||
do_check_array_eq(bytes, expectedBytes);
|
||||
|
||||
run_next_test();
|
||||
}
|
||||
|
||||
function test_ArrayBufferToString() {
|
||||
let testString = "Hello world!";
|
||||
let byteString = StringToBytes(testString);
|
||||
|
||||
let buf = new ArrayBuffer(byteString.length);
|
||||
let view = new DataView(buf);
|
||||
for (let i = 0; i < byteString.length; ++i) {
|
||||
view.setUint8(i, byteString[i]);
|
||||
}
|
||||
|
||||
let str = ArrayBufferToString(buf);
|
||||
Assert.equal(str, testString);
|
||||
|
||||
run_next_test();
|
||||
}
|
||||
|
||||
function test_ArrayBufferToHexString() {
|
||||
let buf = new ArrayBuffer(4);
|
||||
let view = new DataView(buf);
|
||||
view.setUint8(0, 0x00);
|
||||
view.setUint8(1, 0x10);
|
||||
view.setUint8(2, 0x01);
|
||||
view.setUint8(3, 0x11);
|
||||
|
||||
let str = ArrayBufferToHexString(buf);
|
||||
Assert.equal(str, "0x00 10 01 11");
|
||||
|
||||
run_next_test();
|
||||
}
|
||||
|
||||
function run_test() {
|
||||
add_test(test_ArrayBufferToBytes);
|
||||
add_test(test_BytesToArrayBuffer);
|
||||
add_test(test_StringToBytes);
|
||||
add_test(test_ArrayBufferToString);
|
||||
add_test(test_ArrayBufferToHexString);
|
||||
|
||||
run_next_test();
|
||||
}
|
|
@ -2,6 +2,5 @@
|
|||
head =
|
||||
tail =
|
||||
|
||||
[test_ArrayBufferUtils.js]
|
||||
[test_filtering.js]
|
||||
[test_NormalizedMap.js]
|
||||
|
|
Загрузка…
Ссылка в новой задаче