Chromium Code Reviews| Index: sky/framework/xmlhttprequest.sky |
| diff --git a/sky/framework/xmlhttprequest.sky b/sky/framework/xmlhttprequest.sky |
| index fc688d431ef852cb60e8aaca2ef9b80d840a8b95..1f8f6ba2096c1f2aa94664472b1f4b92f7ab2728 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,15 @@ 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. |
| } |
| } |
| class XMLHttpRequest { |
| constructor() { |
| this[kPrivate] = new Private; |
| + this.responseType = 'text'; // Only text and arraybuffer support for now. |
|
ojan
2014/12/12 19:11:27
The default responseType is empty string: https://
|
| } |
| onload() { |
| @@ -32,9 +34,25 @@ class XMLHttpRequest { |
| } |
| get responseText() { |
| + if (this.responseType !== 'text') |
| + throw 'Wrong 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') |
| + return this.responseText; |
| + else if (this.responseType === 'arraybuffer') |
| + return this[kPrivate].responseArrayBuffer; |
| + throw 'Unknown responseType ' + this.responseType; |
|
ojan
2014/12/12 19:11:27
If the responseType is the empty string, then you'
|
| + } |
| + |
| open(method, url) { |
| var request = new loader.URLRequest(); |
| request.url = new URL(url, document.URL); |
| @@ -68,7 +86,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) { |