OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This trivial app just loads "cnn.com" using the Mojo Network and URLLoader | 5 // This trivial app just loads "cnn.com" using the Mojo Network and URLLoader |
6 // services and then prints a brief summary of the response. It's intended to | 6 // services and then prints a brief summary of the response. |
7 // be run using the mojo_js content handler and assumes that this source file | |
8 // is specified as a URL. For example: | |
9 // | 7 // |
| 8 // To run it using mojo_js_standalone (don't forget the quotes): |
| 9 // mojo_shell 'mojo://mojo_js_standalone THIS_DIR/main.js' |
| 10 // |
| 11 // To run it using mojo_js_content handler this file must be specified as |
| 12 // a URL. For example: |
10 // (cd YOUR_DIR/mojo/apps/js; python -m SimpleHTTPServer ) & | 13 // (cd YOUR_DIR/mojo/apps/js; python -m SimpleHTTPServer ) & |
11 // mojo_shell --content-handlers=application/javascript,mojo://mojo_js \ | 14 // mojo_shell \ |
12 // http://localhost:8000/test.js | 15 // --content-handlers=application/javascript,mojo://mojo_js_content_handler \ |
| 16 // http://localhost:8000/test.js |
13 | 17 |
14 define("test", [ | 18 define("test", [ |
| 19 "mojo/apps/js/mojo", |
15 "mojo/public/js/bindings/core", | 20 "mojo/public/js/bindings/core", |
16 "mojo/public/js/bindings/connection", | 21 "mojo/public/js/bindings/connection", |
17 "mojo/public/js/bindings/support", | 22 "mojo/public/js/bindings/support", |
18 "mojo/services/public/interfaces/network/network_service.mojom", | 23 "mojo/services/public/interfaces/network/network_service.mojom", |
19 "mojo/services/public/interfaces/network/url_loader.mojom", | 24 "mojo/services/public/interfaces/network/url_loader.mojom", |
20 "mojo", | |
21 "console" | 25 "console" |
22 ], function(core, connection, support, net, loader, mojo, console) { | 26 ], function(mojo, core, connection, support, net, loader, console) { |
23 | 27 |
24 var netServiceHandle = mojo.connectToService( | 28 var networkService = mojo.connectToService( |
25 "mojo:mojo_network_service", net.NetworkService.name); | 29 "mojo:mojo_network_service", net.NetworkService); |
26 var netConnection = new connection.Connection( | |
27 netServiceHandle, | |
28 net.NetworkService.stubClass, | |
29 net.NetworkService.proxyClass); | |
30 | 30 |
31 var urlLoaderPipe = core.createMessagePipe(); | 31 var urlLoaderPipe = core.createMessagePipe(); |
32 netConnection.remote.createURLLoader(urlLoaderPipe.handle1); | 32 networkService.createURLLoader(urlLoaderPipe.handle1); |
33 var urlLoaderConnection = new connection.Connection( | 33 var urlLoaderConnection = new connection.Connection( |
34 urlLoaderPipe.handle0, | 34 urlLoaderPipe.handle0, |
35 loader.URLLoader.stubClass, | 35 function(){}, |
36 loader.URLLoader.proxyClass); | 36 loader.URLLoader.proxyClass); |
37 | 37 |
38 var urlRequest = new loader.URLRequest({ | 38 var urlRequest = new loader.URLRequest({ |
39 url: "http://www.cnn.com", | 39 url: "http://www.cnn.com", |
40 method: "GET", | 40 method: "GET", |
41 auto_follow_redirects: true | 41 auto_follow_redirects: true |
42 }); | 42 }); |
43 | 43 |
44 var urlRequestPromise = urlLoaderConnection.remote.start(urlRequest); | 44 var urlRequestPromise = urlLoaderConnection.remote.start(urlRequest); |
45 urlRequestPromise.then(function(result) { | 45 urlRequestPromise.then(function(result) { |
46 for(var key in result.response) | 46 for(var key in result.response) |
47 console.log(key + " => " + result.response[key]); | 47 console.log(key + " => " + result.response[key]); |
48 var drainDataPromise = core.drainData(result.response.body); | 48 var drainDataPromise = core.drainData(result.response.body); |
49 drainDataPromise.then(function(result) { | 49 drainDataPromise.then(function(result) { |
50 console.log("read " + result.buffer.byteLength + " bytes"); | 50 console.log("read " + result.buffer.byteLength + " bytes"); |
51 }).then(function() { | 51 }).then(function() { |
52 mojo.quit(); | 52 mojo.quit(); |
53 }); | 53 }); |
54 }); | 54 }); |
55 }); | 55 }); |
OLD | NEW |