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 function XMLHttpRequest() { | 10 class XMLHttpRequest { |
| 11 this.networkService_ = shell.connectToService( | 11 constructor() { |
| 12 "mojo:network_service", net.NetworkService); | 12 this.networkService_ = shell.connectToService( |
| 13 this.request_ = null; | 13 "mojo:network_service", net.NetworkService); |
| 14 this.loader_ = null; | 14 this.request_ = null; |
| 15 this.responseText = null; | 15 this.loader_ = null; |
| 16 this.headers_ = new Map(); | 16 this.responseText = null; |
|
esprehn
2014/11/21 07:24:24
It might be nice to put public properties grouped.
| |
| 17 }; | 17 this.headers_ = new Map(); |
| 18 } | |
| 18 | 19 |
| 19 XMLHttpRequest.prototype.onload = function() { }; | 20 onload() { |
| 20 XMLHttpRequest.prototype.onerror = function(error) { }; | 21 } |
| 21 | 22 |
| 22 XMLHttpRequest.prototype.open = function(method, url) { | 23 onerror(error) { |
| 23 this.request_ = new loader.URLRequest(); | 24 } |
| 24 this.request_.url = new URL(url, document.URL); | |
| 25 this.request_.method = method; | |
| 26 this.request_.auto_follow_redirects = true; | |
| 27 this.headers_.clear(); | |
| 28 }; | |
| 29 | 25 |
| 30 XMLHttpRequest.prototype.setRequestHeader = function(header, value) { | 26 open(method, url) { |
| 31 this.headers_.set(header, value); | 27 this.request_ = new loader.URLRequest(); |
| 32 }; | 28 this.request_.url = new URL(url, document.URL); |
| 29 this.request_.method = method; | |
| 30 this.request_.auto_follow_redirects = true; | |
| 31 this.headers_.clear(); | |
| 32 } | |
| 33 | 33 |
| 34 XMLHttpRequest.prototype.send = function() { | 34 setRequestHeader(header, value) { |
| 35 var requestHeaders = []; | 35 this.headers_.set(header, value); |
| 36 this.headers_.forEach(function(value, key) { | 36 } |
| 37 requestHeaders.push(key + ': ' + value); | |
| 38 }); | |
| 39 this.request_.headers = requestHeaders; | |
| 40 | 37 |
| 41 // FIXME: Factor this into the JS bindings. | 38 send() { |
| 42 var pipe = new core.createMessagePipe(); | 39 var requestHeaders = []; |
| 43 this.networkService_.createURLLoader(pipe.handle1); | 40 this.headers_.forEach(function(value, key) { |
| 44 this.loader_ = shell.wrapHandle(pipe.handle0, loader.URLLoader); | 41 requestHeaders.push(key + ': ' + value); |
| 42 }); | |
| 43 this.request_.headers = requestHeaders; | |
| 45 | 44 |
| 46 var self = this; | 45 // FIXME: Factor this into the JS bindings. |
| 47 outstandingRequests.add(this); | 46 var pipe = new core.createMessagePipe(); |
| 48 this.loader_.start(this.request_).then(function(result) { | 47 this.networkService_.createURLLoader(pipe.handle1); |
| 49 return core.drainData(result.response.body).then(function(result) { | 48 this.loader_ = shell.wrapHandle(pipe.handle0, loader.URLLoader); |
| 49 | |
| 50 var self = this; | |
| 51 outstandingRequests.add(this); | |
| 52 this.loader_.start(this.request_).then(function(result) { | |
| 53 return core.drainData(result.response.body).then(function(result) { | |
| 54 outstandingRequests.delete(self); | |
| 55 self.responseText = unicode.decodeUtf8String(new Uint8Array(result.buffe r)); | |
| 56 self.onload(); | |
| 57 }); | |
| 58 }).catch(function(error) { | |
| 50 outstandingRequests.delete(self); | 59 outstandingRequests.delete(self); |
| 51 self.responseText = unicode.decodeUtf8String(new Uint8Array(result.buffer) ); | 60 self.onerror(error); |
| 52 self.onload(); | |
| 53 }); | 61 }); |
| 54 }).catch(function(error) { | 62 } |
| 55 outstandingRequests.delete(self); | 63 } |
| 56 self.onerror(error); | |
| 57 }); | |
| 58 }; | |
| 59 | 64 |
| 60 module.exports = XMLHttpRequest; | 65 module.exports = XMLHttpRequest; |
| 61 </script> | 66 </script> |
| OLD | NEW |