| OLD | NEW |
| 1 #!mojo:js_content_handler | 1 #!mojo:js_content_handler |
| 2 // Demonstrate use of the Mojo network service to load a URL. To run this | 2 // Demonstrate use of the Mojo network service to load a URL. To run this |
| 3 // application with mojo_shell, set DIR to be the absolute path for this | 3 // application with mojo_shell, set DIR to be the absolute path for this |
| 4 // directory, then: | 4 // directory, then: |
| 5 // mojo_shell "file://$DIR/wget.js http://www.google.com" | 5 // mojo_shell "file://$DIR/wget.js http://www.google.com" |
| 6 // Substitute any URL for google.com. To keep the noise down, this app | 6 // Substitute any URL for google.com. To keep the noise down, this app |
| 7 // only displays the number of bytes read and a little http header info. | 7 // only displays the number of bytes read and a little http header info. |
| 8 | 8 |
| 9 define("main", [ | 9 define("main", [ |
| 10 "console", | 10 "console", |
| 11 "mojo/services/public/js/application", | 11 "mojo/services/public/js/application", |
| 12 "mojo/public/js/core", | 12 "mojo/public/js/core", |
| 13 "mojo/services/network/public/interfaces/network_service.mojom", | 13 "mojo/services/network/public/interfaces/network_service.mojom", |
| 14 "mojo/services/network/public/interfaces/url_loader.mojom", | 14 "mojo/services/network/public/interfaces/url_loader.mojom", |
| 15 ], function(console, appModule, coreModule, netModule, loaderModule) { | 15 ], function(console, application, core, network, loader) { |
| 16 | 16 |
| 17 class WGet extends appModule.Application { | 17 var Application = application.Application; |
| 18 var NetworkService = network.NetworkService; |
| 19 var URLRequest = loader.URLRequest; |
| 20 |
| 21 class WGet extends Application { |
| 18 initialize(args) { | 22 initialize(args) { |
| 19 if (args.length != 2) { | 23 if (args.length != 2) { |
| 20 console.log("Expected URL argument"); | 24 console.log("Expected URL argument"); |
| 21 return; | 25 return; |
| 22 } | 26 } |
| 23 | 27 |
| 24 var netService = this.shell.connectToService( | 28 var netService = this.shell.connectToService( |
| 25 "mojo:network_service", netModule.NetworkService); | 29 "mojo:network_service", NetworkService); |
| 26 | 30 |
| 27 var urlLoader; | 31 var urlLoader; |
| 28 netService.createURLLoader(function(x){urlLoader = x;}); | 32 netService.createURLLoader(function(x){urlLoader = x;}); |
| 29 | 33 |
| 30 var urlRequest = new loaderModule.URLRequest({ | 34 var urlRequest = new URLRequest({ |
| 31 url: args[1], | 35 url: args[1], |
| 32 method: "GET", | 36 method: "GET", |
| 33 auto_follow_redirects: true | 37 auto_follow_redirects: true |
| 34 }); | 38 }); |
| 35 | 39 |
| 36 var app = this; | 40 var app = this; |
| 37 urlLoader.start(urlRequest).then(function(result) { | 41 urlLoader.start(urlRequest).then(function(result) { |
| 38 console.log("url => " + result.response["url"]); | 42 console.log("url => " + result.response["url"]); |
| 39 console.log("status_line => " + result.response["status_line"]); | 43 console.log("status_line => " + result.response["status_line"]); |
| 40 console.log("mime_type => " + result.response["mime_type"]); | 44 console.log("mime_type => " + result.response["mime_type"]); |
| 41 | 45 |
| 42 coreModule.drainData(result.response.body).then( | 46 core.drainData(result.response.body).then( |
| 43 function(result) { | 47 function(result) { |
| 44 console.log("read " + result.buffer.byteLength + " bytes"); | 48 console.log("read " + result.buffer.byteLength + " bytes"); |
| 45 }) | 49 }) |
| 46 .then(function() { | 50 .then(function() { |
| 47 app.quit(); | 51 app.quit(); |
| 48 }); | 52 }); |
| 49 }); | 53 }); |
| 50 } | 54 } |
| 51 } | 55 } |
| 52 | 56 |
| 53 return WGet; | 57 return WGet; |
| 54 }); | 58 }); |
| OLD | NEW |