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, appModule, coreModule, netModule, loaderModule) { |
16 | 16 |
17 class WGet extends appModule.Application { | 17 class WGet extends appModule.Application { |
18 initialize(args) { | 18 initialize(args) { |
19 if (args.length != 2) { | 19 if (args.length != 2) { |
20 console.log("Expected URL argument"); | 20 console.log("Expected URL argument"); |
21 return; | 21 return; |
22 } | 22 } |
23 | 23 |
24 var netService = this.shell.connectToService( | 24 var netService = this.shell.connectToService( |
25 "mojo:network_service", netModule.NetworkService); | 25 "mojo:network_service", netModule.NetworkService); |
26 | 26 |
27 var urlLoaderClient = {}; | 27 var urlLoader; |
28 netService.createURLLoader(urlLoaderClient); | 28 netService.createURLLoader(function(x){urlLoader = x;}); |
29 var urlLoader = urlLoaderClient.remote$; | |
30 | 29 |
31 var urlRequest = new loaderModule.URLRequest({ | 30 var urlRequest = new loaderModule.URLRequest({ |
32 url: args[1], | 31 url: args[1], |
33 method: "GET", | 32 method: "GET", |
34 auto_follow_redirects: true | 33 auto_follow_redirects: true |
35 }); | 34 }); |
36 | 35 |
37 var app = this; | 36 var app = this; |
38 urlLoader.start(urlRequest).then(function(result) { | 37 urlLoader.start(urlRequest).then(function(result) { |
39 console.log("url => " + result.response["url"]); | 38 console.log("url => " + result.response["url"]); |
40 console.log("status_line => " + result.response["status_line"]); | 39 console.log("status_line => " + result.response["status_line"]); |
41 console.log("mime_type => " + result.response["mime_type"]); | 40 console.log("mime_type => " + result.response["mime_type"]); |
42 | 41 |
43 coreModule.drainData(result.response.body).then( | 42 coreModule.drainData(result.response.body).then( |
44 function(result) { | 43 function(result) { |
45 console.log("read " + result.buffer.byteLength + " bytes"); | 44 console.log("read " + result.buffer.byteLength + " bytes"); |
46 }) | 45 }) |
47 .then(function() { | 46 .then(function() { |
48 app.quit(); | 47 app.quit(); |
49 }); | 48 }); |
50 }); | 49 }); |
51 } | 50 } |
52 } | 51 } |
53 | 52 |
54 return WGet; | 53 return WGet; |
55 }); | 54 }); |
OLD | NEW |