Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/public/interfaces/network/network_service.mojom.sky" as="net" /> | 3 <import src="/mojo/services/public/interfaces/network/network_service.mojom.sky" as="net" /> |
| 4 <import src="/mojo/services/public/interfaces/network/url_loader.mojom.sky" as=" loader" /> | 4 <import src="/mojo/services/public/interfaces/network/url_loader.mojom.sky" as=" loader" /> |
| 5 <import src="shell.sky" as="shell" /> | 5 <import src="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 = null; | |
|
esprehn
2014/11/21 08:08:28
default is empty string not null in the real one.
| |
| 19 } | 20 } |
| 20 } | 21 } |
| 21 | 22 |
| 22 class XMLHttpRequest { | 23 class XMLHttpRequest { |
| 23 constructor() { | 24 constructor() { |
| 24 this[kPrivate] = new Private; | 25 this[kPrivate] = new Private; |
| 25 this.responseText = null; | |
| 26 } | 26 } |
| 27 | 27 |
| 28 onload() { | 28 onload() { |
| 29 } | 29 } |
| 30 | 30 |
| 31 onerror(error) { | 31 onerror(error) { |
| 32 } | 32 } |
| 33 | 33 |
| 34 get responseText() { | |
| 35 return this[kPrivate].responseText; | |
| 36 } | |
| 37 | |
| 34 open(method, url) { | 38 open(method, url) { |
| 35 var request = new loader.URLRequest(); | 39 var request = new loader.URLRequest(); |
| 36 request.url = new URL(url, document.URL); | 40 request.url = new URL(url, document.URL); |
| 37 request.method = method; | 41 request.method = method; |
| 38 request.auto_follow_redirects = true; | 42 request.auto_follow_redirects = true; |
| 39 | 43 |
| 40 var priv = this[kPrivate]; | 44 var priv = this[kPrivate]; |
| 41 priv.request = request; | 45 priv.request = request; |
| 42 priv.headers.clear(); | 46 priv.headers.clear(); |
| 43 } | 47 } |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 57 // FIXME: Factor this into the JS bindings. | 61 // FIXME: Factor this into the JS bindings. |
| 58 var pipe = new core.createMessagePipe(); | 62 var pipe = new core.createMessagePipe(); |
| 59 priv.networkService.createURLLoader(pipe.handle1); | 63 priv.networkService.createURLLoader(pipe.handle1); |
| 60 priv.loader = shell.wrapHandle(pipe.handle0, loader.URLLoader); | 64 priv.loader = shell.wrapHandle(pipe.handle0, loader.URLLoader); |
| 61 | 65 |
| 62 var self = this; | 66 var self = this; |
| 63 outstandingRequests.add(this); | 67 outstandingRequests.add(this); |
| 64 priv.loader.start(priv.request).then(function(result) { | 68 priv.loader.start(priv.request).then(function(result) { |
| 65 return core.drainData(result.response.body).then(function(result) { | 69 return core.drainData(result.response.body).then(function(result) { |
| 66 outstandingRequests.delete(self); | 70 outstandingRequests.delete(self); |
| 67 self.responseText = unicode.decodeUtf8String(new Uint8Array(result.buffe r)); | 71 priv.responseText = unicode.decodeUtf8String(new Uint8Array(result.buffe r)); |
| 68 self.onload(); | 72 self.onload(); |
| 69 }); | 73 }); |
| 70 }).catch(function(error) { | 74 }).catch(function(error) { |
| 71 outstandingRequests.delete(self); | 75 outstandingRequests.delete(self); |
| 72 self.onerror(error); | 76 self.onerror(error); |
| 73 }); | 77 }); |
| 74 } | 78 } |
| 75 } | 79 } |
| 76 | 80 |
| 77 module.exports = XMLHttpRequest; | 81 module.exports = XMLHttpRequest; |
| 78 </script> | 82 </script> |
| OLD | NEW |