Commit 5a4d8e88 authored by dmurph@chromium.org's avatar dmurph@chromium.org

Adding ArrayBufferView as a constructor type to Response

BUG=412027

Review URL: https://codereview.chromium.org/558793002

git-svn-id: svn://svn.chromium.org/blink/trunk@181762 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent bd8429d0
...@@ -14,7 +14,7 @@ promise_test(function() { ...@@ -14,7 +14,7 @@ promise_test(function() {
}, 'Behavior of Response with string content.'); }, 'Behavior of Response with string content.');
promise_test(function() { promise_test(function() {
var intView = new Int32Array([0, 1, 2, 3, 4, 55, 6, 7, 8, 9]); var intView = new Int32Array([0, 1, 2, 3, 4, 55, 6, 7, 8, 9]);
var buffer = intView.buffer; var buffer = intView.buffer;
var response = new Response(buffer); var response = new Response(buffer);
...@@ -28,4 +28,38 @@ promise_test(function() { ...@@ -28,4 +28,38 @@ promise_test(function() {
'Response body ArrayBuffer should match ArrayBuffer ' + 'Response body ArrayBuffer should match ArrayBuffer ' +
'it was constructed with.'); 'it was constructed with.');
}); });
}, 'Behavior of Response with arraybuffer content.'); }, 'Behavior of Response with ArrayBuffer content.');
\ No newline at end of file
promise_test(function() {
var intView = new Int32Array([0, 1, 2, 3, 4, 55, 6, 7, 8, 9]);
var response = new Response(intView);
assert_false(response.headers.has('Content-Type'),
'A Response constructed with ArrayBufferView ' +
'should not have a content type.');
return response.body.asArrayBuffer()
.then(function(buffer) {
var resultIntView = new Int32Array(buffer);
assert_array_equals(
resultIntView, [0, 1, 2, 3, 4, 55, 6, 7, 8, 9],
'Response body ArrayBuffer should match ArrayBufferView ' +
'it was constructed with.');
});
}, 'Behavior of Response with ArrayBufferView content without a slice.');
promise_test(function() {
var intView = new Int32Array([0, 1, 2, 3, 4, 55, 6, 7, 8, 9]);
var slice = intView.subarray(1, 4); // Should be [1, 2, 3]
var response = new Response(slice);
assert_false(response.headers.has('Content-Type'),
'A Response constructed with ArrayBufferView ' +
'should not have a content type.');
return response.body.asArrayBuffer()
.then(function(buffer) {
var resultIntView = new Int32Array(buffer);
assert_array_equals(
resultIntView, [1, 2, 3],
'Response body ArrayBuffer should match ArrayBufferView ' +
'slice it was constructed with.');
});
}, 'Behavior of Response with ArrayBufferView content with a slice.');
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
#include "modules/serviceworkers/ResponseInit.h" #include "modules/serviceworkers/ResponseInit.h"
#include "public/platform/WebServiceWorkerResponse.h" #include "public/platform/WebServiceWorkerResponse.h"
#include "wtf/ArrayBuffer.h" #include "wtf/ArrayBuffer.h"
#include "wtf/ArrayBufferView.h"
#include "wtf/RefPtr.h"
namespace blink { namespace blink {
...@@ -70,6 +72,15 @@ Response* Response::create(const ArrayBuffer* body, const Dictionary& responseIn ...@@ -70,6 +72,15 @@ Response* Response::create(const ArrayBuffer* body, const Dictionary& responseIn
return create(blob.get(), ResponseInit(responseInit), exceptionState); return create(blob.get(), ResponseInit(responseInit), exceptionState);
} }
Response* Response::create(const ArrayBufferView* body, const Dictionary& responseInit, ExceptionState& exceptionState)
{
OwnPtr<BlobData> blobData = BlobData::create();
blobData->appendArrayBufferView(body);
const long long length = blobData->length();
RefPtrWillBeRawPtr<Blob> blob = Blob::create(BlobDataHandle::create(blobData.release(), length));
return create(blob.get(), ResponseInit(responseInit), exceptionState);
}
Response* Response::create(Blob* body, const ResponseInit& responseInit, ExceptionState& exceptionState) Response* Response::create(Blob* body, const ResponseInit& responseInit, ExceptionState& exceptionState)
{ {
// "1. If |init|'s status member is not in the range 200 to 599, throw a // "1. If |init|'s status member is not in the range 200 to 599, throw a
......
...@@ -12,8 +12,7 @@ ...@@ -12,8 +12,7 @@
#include "modules/serviceworkers/Headers.h" #include "modules/serviceworkers/Headers.h"
#include "platform/blob/BlobData.h" #include "platform/blob/BlobData.h"
#include "platform/heap/Handle.h" #include "platform/heap/Handle.h"
#include "wtf/RefPtr.h" #include "wtf/Forward.h"
#include "wtf/text/WTFString.h"
namespace blink { namespace blink {
...@@ -28,6 +27,7 @@ public: ...@@ -28,6 +27,7 @@ public:
static Response* create(Blob*, const Dictionary&, ExceptionState&); static Response* create(Blob*, const Dictionary&, ExceptionState&);
static Response* create(const String&, const Dictionary&, ExceptionState&); static Response* create(const String&, const Dictionary&, ExceptionState&);
static Response* create(const ArrayBuffer*, const Dictionary&, ExceptionState&); static Response* create(const ArrayBuffer*, const Dictionary&, ExceptionState&);
static Response* create(const ArrayBufferView*, const Dictionary&, ExceptionState&);
static Response* create(Blob*, const ResponseInit&, ExceptionState&); static Response* create(Blob*, const ResponseInit&, ExceptionState&);
static Response* create(FetchResponseData*); static Response* create(FetchResponseData*);
static Response* create(const WebServiceWorkerResponse&); static Response* create(const WebServiceWorkerResponse&);
......
...@@ -7,11 +7,11 @@ ...@@ -7,11 +7,11 @@
enum ResponseType { "basic", "cors", "default", "error", "opaque" }; enum ResponseType { "basic", "cors", "default", "error", "opaque" };
[ [
// FIXME: Add ctors for ArrayBufferView, FormData and URLSearchParams // FIXME: Add ctors for FormData and URLSearchParams response bodies.
// response bodies.
Constructor(ScalarValueString body, optional Dictionary responseInitDict), Constructor(ScalarValueString body, optional Dictionary responseInitDict),
Constructor(Blob? body, optional Dictionary responseInitDict), Constructor(Blob? body, optional Dictionary responseInitDict),
Constructor(ArrayBuffer input, optional Dictionary requestInitDict), Constructor(ArrayBuffer input, optional Dictionary requestInitDict),
Constructor(ArrayBufferView input, optional Dictionary requestInitDict),
RuntimeEnabled=ServiceWorker, RuntimeEnabled=ServiceWorker,
Exposed=ServiceWorker, Exposed=ServiceWorker,
RaisesException=Constructor, RaisesException=Constructor,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment