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. | |
| 8 var outstandingRequests = new Set(); | |
|
esprehn
2014/11/18 02:06:24
Why do you need this? I thought the Mojo JS stuff
abarth-chromium
2014/11/18 02:11:59
It doesn't. :(
We can make the waiting callbacks
esprehn
2014/11/18 02:15:49
Fixing this with one off Sets seems like a mistake
| |
| 9 | |
| 7 function XMLHttpRequest() { | 10 function XMLHttpRequest() { |
| 8 this.networkService_ = shell.connectToService( | 11 this.networkService_ = shell.connectToService( |
| 9 "mojo:network_service", net.NetworkService); | 12 "mojo:network_service", net.NetworkService); |
| 10 this.request_ = null; | 13 this.request_ = null; |
| 11 this.loader_ = null; | 14 this.loader_ = null; |
| 12 this.responseText = null; | 15 this.responseText = null; |
| 13 this.headers_ = new Map(); | 16 this.headers_ = new Map(); |
| 14 }; | 17 }; |
| 15 | 18 |
| 16 XMLHttpRequest.prototype.onload = function() { }; | 19 XMLHttpRequest.prototype.onload = function() { }; |
| 17 XMLHttpRequest.prototype.onerror = function(error) { }; | 20 XMLHttpRequest.prototype.onerror = function(error) { }; |
| 18 | 21 |
| 19 XMLHttpRequest.prototype.open = function(method, url) { | 22 XMLHttpRequest.prototype.open = function(method, url) { |
| 20 this.request_ = new loader.URLRequest(); | 23 this.request_ = new loader.URLRequest(); |
| 21 this.request_.url = url; | 24 this.request_.url = new URL(url, document.URL); |
| 22 this.request_.method = method; | 25 this.request_.method = method; |
| 23 this.request_.auto_follow_redirects = true; | 26 this.request_.auto_follow_redirects = true; |
| 24 this.headers_.clear(); | 27 this.headers_.clear(); |
| 25 }; | 28 }; |
| 26 | 29 |
| 27 XMLHttpRequest.prototype.setRequestHeader = function(header, value) { | 30 XMLHttpRequest.prototype.setRequestHeader = function(header, value) { |
| 28 this.headers_.set(header, value); | 31 this.headers_.set(header, value); |
| 29 }; | 32 }; |
| 30 | 33 |
| 31 XMLHttpRequest.prototype.send = function() { | 34 XMLHttpRequest.prototype.send = function() { |
| 32 var requestHeaders = []; | 35 var requestHeaders = []; |
| 33 this.headers_.forEach(function(value, key) { | 36 this.headers_.forEach(function(value, key) { |
| 34 requestHeaders.push(key + ': ' + value); | 37 requestHeaders.push(key + ': ' + value); |
| 35 }); | 38 }); |
| 36 this.request_.headers = requestHeaders; | 39 this.request_.headers = requestHeaders; |
| 37 | 40 |
| 38 // FIXME: Factor this into the JS bindings. | 41 // FIXME: Factor this into the JS bindings. |
| 39 var pipe = new core.createMessagePipe(); | 42 var pipe = new core.createMessagePipe(); |
| 40 this.networkService_.createURLLoader(pipe.handle1); | 43 this.networkService_.createURLLoader(pipe.handle1); |
| 41 this.loader_ = shell.wrapHandle(pipe.handle0, loader.URLLoader); | 44 this.loader_ = shell.wrapHandle(pipe.handle0, loader.URLLoader); |
| 42 | 45 |
| 43 var self = this; | 46 var self = this; |
| 47 outstandingRequests.add(this); | |
| 44 this.loader_.start(this.request_).then(function(result) { | 48 this.loader_.start(this.request_).then(function(result) { |
| 45 return core.drainData(result.response.body).then(function(result) { | 49 return core.drainData(result.response.body).then(function(result) { |
| 50 outstandingRequests.delete(self); | |
| 46 self.responseText = unicode.decodeUtf8String(new Uint8Array(result.buffer) ); | 51 self.responseText = unicode.decodeUtf8String(new Uint8Array(result.buffer) ); |
| 47 self.onload(); | 52 self.onload(); |
| 48 }); | 53 }); |
| 49 }).catch(function(error) { | 54 }).catch(function(error) { |
| 55 outstandingRequests.delete(self); | |
| 50 self.onerror(error); | 56 self.onerror(error); |
| 51 }); | 57 }); |
| 52 }; | 58 }; |
| 53 | 59 |
| 54 module.exports = XMLHttpRequest; | 60 module.exports = XMLHttpRequest; |
| 55 </script> | 61 </script> |
| OLD | NEW |