Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(369)

Unified Diff: sky/framework/xmlhttprequest.sky

Issue 805503002: Add responseType='arraybuffer' support to XHR (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Updated per ojan's review Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | sky/tests/framework/xmlhttprequest/empty-responseType.sky » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sky/framework/xmlhttprequest.sky
diff --git a/sky/framework/xmlhttprequest.sky b/sky/framework/xmlhttprequest.sky
index fc688d431ef852cb60e8aaca2ef9b80d840a8b95..897ad9773d5500c7f9cbd3926d04e5a3a504b220 100644
--- a/sky/framework/xmlhttprequest.sky
+++ b/sky/framework/xmlhttprequest.sky
@@ -2,7 +2,7 @@
<import src="/mojo/public/sky/unicode.sky" as="unicode" />
<import src="/mojo/services/network/public/interfaces/network_service.mojom.sky" as="net" />
<import src="/mojo/services/network/public/interfaces/url_loader.mojom.sky" as="loader" />
-<import src="shell.sky" as="shell" />
+<import src="/sky/framework/shell.sky" as="shell" />
<script>
// XHR keeps itself alive.
var outstandingRequests = new Set();
@@ -16,13 +16,16 @@ class Private {
this.request = null;
this.loader = null;
this.headers = new Map();
- this.responseText = "";
+ this.responseArrayBuffer = null;
+ this.responseText = null; // Cached to avoid re-decoding each access.
}
}
+// https://xhr.spec.whatwg.org
class XMLHttpRequest {
constructor() {
this[kPrivate] = new Private;
+ this.responseType = ''; // Only text and arraybuffer support for now.
}
onload() {
@@ -32,9 +35,25 @@ class XMLHttpRequest {
}
get responseText() {
+ if (this.responseType !== '' && this.responseType !== 'text')
+ throw 'Non-text responseType ' + this.responseType;
+ if (this[kPrivate].responseArrayBuffer === null)
+ return null;
+ if (this[kPrivate].responseText === null) {
+ var intArray = new Uint8Array(this[kPrivate].responseArrayBuffer);
+ this[kPrivate].responseText = unicode.decodeUtf8String(intArray);
+ }
return this[kPrivate].responseText;
}
+ get response() {
+ if (this.responseType === 'text' || this.responseType == '')
+ return this.responseText;
+ else if (this.responseType === 'arraybuffer')
+ return this[kPrivate].responseArrayBuffer;
+ throw 'Unknown responseType ' + this.responseType;
+ }
+
open(method, url) {
var request = new loader.URLRequest();
request.url = new URL(url, document.URL);
@@ -68,7 +87,8 @@ class XMLHttpRequest {
priv.loader.start(priv.request).then(function(result) {
return core.drainData(result.response.body).then(function(result) {
outstandingRequests.delete(self);
- priv.responseText = unicode.decodeUtf8String(new Uint8Array(result.buffer));
+ priv.responseArrayBuffer = result.buffer;
+ // FIXME: Catch exceptions during onload so they don't trip onerror.
self.onload();
});
}).catch(function(error) {
« no previous file with comments | « no previous file | sky/tests/framework/xmlhttprequest/empty-responseType.sky » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698