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

Side by Side 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: 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 unified diff | Download patch
« no previous file with comments | « no previous file | sky/tests/framework/xmlhttprequest/resources/pass.txt » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 <import src="/mojo/public/sky/core.sky" as="core" /> 1 <import src="/mojo/public/sky/core.sky" as="core" />
2 <import src="/mojo/public/sky/unicode.sky" as="unicode" /> 2 <import src="/mojo/public/sky/unicode.sky" as="unicode" />
3 <import src="/mojo/services/network/public/interfaces/network_service.mojom.sky" as="net" /> 3 <import src="/mojo/services/network/public/interfaces/network_service.mojom.sky" as="net" />
4 <import src="/mojo/services/network/public/interfaces/url_loader.mojom.sky" as=" loader" /> 4 <import src="/mojo/services/network/public/interfaces/url_loader.mojom.sky" as=" loader" />
5 <import src="shell.sky" as="shell" /> 5 <import src="/sky/framework/shell.sky" as="shell" />
6 <script> 6 <script>
7 // XHR keeps itself alive. 7 // XHR keeps itself alive.
8 var outstandingRequests = new Set(); 8 var outstandingRequests = new Set();
9 9
10 const kPrivate = Symbol("XMLHttpRequestPrivate"); 10 const kPrivate = Symbol("XMLHttpRequestPrivate");
11 11
12 class Private { 12 class Private {
13 constructor() { 13 constructor() {
14 this.networkService = shell.connectToService( 14 this.networkService = shell.connectToService(
15 "mojo:network_service", net.NetworkService); 15 "mojo:network_service", net.NetworkService);
16 this.request = null; 16 this.request = null;
17 this.loader = null; 17 this.loader = null;
18 this.headers = new Map(); 18 this.headers = new Map();
19 this.responseText = ""; 19 this.responseArrayBuffer = null;
20 this.responseText = null; // Cached to avoid re-decoding each access.
20 } 21 }
21 } 22 }
22 23
23 class XMLHttpRequest { 24 class XMLHttpRequest {
24 constructor() { 25 constructor() {
25 this[kPrivate] = new Private; 26 this[kPrivate] = new Private;
27 this.responseType = 'text'; // Only text and arraybuffer support for now.
ojan 2014/12/12 19:11:27 The default responseType is empty string: https://
26 } 28 }
27 29
28 onload() { 30 onload() {
29 } 31 }
30 32
31 onerror(error) { 33 onerror(error) {
32 } 34 }
33 35
34 get responseText() { 36 get responseText() {
37 if (this.responseType !== 'text')
38 throw 'Wrong responseType';
39 if (this[kPrivate].responseArrayBuffer === null)
40 return null;
41 if (this[kPrivate].responseText === null) {
42 var intArray = new Uint8Array(this[kPrivate].responseArrayBuffer);
43 this[kPrivate].responseText = unicode.decodeUtf8String(intArray);
44 }
35 return this[kPrivate].responseText; 45 return this[kPrivate].responseText;
36 } 46 }
37 47
48 get response() {
49 if (this.responseType === 'text')
50 return this.responseText;
51 else if (this.responseType === 'arraybuffer')
52 return this[kPrivate].responseArrayBuffer;
53 throw 'Unknown responseType ' + this.responseType;
ojan 2014/12/12 19:11:27 If the responseType is the empty string, then you'
54 }
55
38 open(method, url) { 56 open(method, url) {
39 var request = new loader.URLRequest(); 57 var request = new loader.URLRequest();
40 request.url = new URL(url, document.URL); 58 request.url = new URL(url, document.URL);
41 request.method = method; 59 request.method = method;
42 request.auto_follow_redirects = true; 60 request.auto_follow_redirects = true;
43 61
44 var priv = this[kPrivate]; 62 var priv = this[kPrivate];
45 priv.request = request; 63 priv.request = request;
46 priv.headers.clear(); 64 priv.headers.clear();
47 } 65 }
(...skipping 13 matching lines...) Expand all
61 // FIXME: Factor this into the JS bindings. 79 // FIXME: Factor this into the JS bindings.
62 var pipe = new core.createMessagePipe(); 80 var pipe = new core.createMessagePipe();
63 priv.networkService.createURLLoader(pipe.handle1); 81 priv.networkService.createURLLoader(pipe.handle1);
64 priv.loader = shell.wrapHandle(pipe.handle0, loader.URLLoader); 82 priv.loader = shell.wrapHandle(pipe.handle0, loader.URLLoader);
65 83
66 var self = this; 84 var self = this;
67 outstandingRequests.add(this); 85 outstandingRequests.add(this);
68 priv.loader.start(priv.request).then(function(result) { 86 priv.loader.start(priv.request).then(function(result) {
69 return core.drainData(result.response.body).then(function(result) { 87 return core.drainData(result.response.body).then(function(result) {
70 outstandingRequests.delete(self); 88 outstandingRequests.delete(self);
71 priv.responseText = unicode.decodeUtf8String(new Uint8Array(result.buffe r)); 89 priv.responseArrayBuffer = result.buffer;
90 // FIXME: Catch exceptions during onload so they don't trip onerror.
72 self.onload(); 91 self.onload();
73 }); 92 });
74 }).catch(function(error) { 93 }).catch(function(error) {
75 outstandingRequests.delete(self); 94 outstandingRequests.delete(self);
76 self.onerror(error); 95 self.onerror(error);
77 }); 96 });
78 } 97 }
79 } 98 }
80 99
81 module.exports = XMLHttpRequest; 100 module.exports = XMLHttpRequest;
82 </script> 101 </script>
OLDNEW
« no previous file with comments | « no previous file | sky/tests/framework/xmlhttprequest/resources/pass.txt » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698