Bug 831107 - Part 1: Allow TCPSocket consumers to receive typed array data without converting it to a byte string first. r=vlad

This commit is contained in:
Josh Matthews 2013-04-19 11:25:33 +02:00
parent ee172b786c
commit e24e7ab8dd
3 changed files with 40 additions and 3 deletions

View File

@ -576,9 +576,9 @@ TCPSocket.prototype = {
// nsIStreamListener (Triggered by _inputStreamPump.asyncRead)
onDataAvailable: function ts_onDataAvailable(request, context, inputStream, offset, count) {
if (this._binaryType === "arraybuffer") {
let ua = this.useWin ? new this.useWin.Uint8Array(count)
: new Uint8Array(count);
ua.set(this._inputStreamBinary.readByteArray(count));
let buffer = this._inputStreamBinary.readArrayBuffer(count);
let constructor = this.useWin ? this.useWin.Uint8Array : Uint8Array;
let ua = new constructor(buffer);
this.callListener("data", ua);
} else {
this.callListener("data", this._inputStreamScriptable.read(count));

View File

@ -28,6 +28,9 @@
#include "nsComponentManagerUtils.h"
#include "nsIURI.h" // for NS_IURI_IID
#include "jsapi.h"
#include "jsfriendapi.h"
NS_IMPL_ISUPPORTS3(nsBinaryOutputStream, nsIObjectOutputStream, nsIBinaryOutputStream, nsIOutputStream)
NS_IMETHODIMP
@ -715,6 +718,30 @@ nsBinaryInputStream::ReadByteArray(uint32_t aLength, uint8_t* *_rval)
return ReadBytes(aLength, reinterpret_cast<char **>(_rval));
}
NS_IMETHODIMP
nsBinaryInputStream::ReadArrayBuffer(uint32_t aLength, JSContext* cx, JS::Value* _rval)
{
JSAutoRequest ar(cx);
JS::RootedObject buffer(cx, JS_NewArrayBuffer(cx, aLength));
if (!buffer) {
return NS_ERROR_FAILURE;
}
uint8_t* data = JS_GetArrayBufferData(buffer);
if (!data) {
return NS_ERROR_FAILURE;
}
uint32_t bytesRead;
nsresult rv = Read(reinterpret_cast<char*>(data), aLength, &bytesRead);
NS_ENSURE_SUCCESS(rv, rv);
if (bytesRead != aLength) {
return NS_ERROR_FAILURE;
}
*_rval = JS::ObjectValue(*buffer);
return NS_OK;
}
NS_IMETHODIMP
nsBinaryInputStream::ReadObject(bool aIsStrongRef, nsISupports* *aObject)
{

View File

@ -68,6 +68,16 @@ interface nsIBinaryInputStream : nsIInputStream {
*/
void readByteArray(in uint32_t aLength,
[array, size_is(aLength), retval] out uint8_t aBytes);
/**
* Read opaque bytes from the stream, storing the results in an ArrayBuffer.
*
* @param aLength the number of bytes that must be read
*
* @throws NS_ERROR_FAILURE if it can't read aLength bytes
*/
[implicit_jscontext]
jsval readArrayBuffer(in uint32_t aLength);
};
%{C++