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. It's intended to |
7 // be run using the mojo_js content handler and assumes that this source file | 7 // be run using the mojo_js content handler and assumes that this source file |
8 // is specified as a URL. For example: | 8 // is specified as a URL. For example: |
9 // | 9 // |
10 // (cd YOUR_DIR/mojo/apps/js; python -m SimpleHTTPServer ) & | 10 // (cd YOUR_DIR/mojo/apps/js; python -m SimpleHTTPServer ) & |
11 // mojo_shell --content-handlers=application/javascript,mojo://mojo_js \ | 11 // mojo_shell --content-handlers=application/javascript,mojo://mojo_js \ |
12 // http://localhost:8000/test.js | 12 // http://localhost:8000/test.js |
13 | 13 |
14 define("test", [ | 14 define("test", [ |
15 "mojo/public/js/bindings/core", | 15 "mojo/public/js/bindings/core", |
16 "mojo/public/js/bindings/connection", | 16 "mojo/public/js/bindings/connection", |
17 "mojo/public/js/bindings/support", | |
17 "mojo/services/public/interfaces/network/network_service.mojom", | 18 "mojo/services/public/interfaces/network/network_service.mojom", |
18 "mojo/services/public/interfaces/network/url_loader.mojom", | 19 "mojo/services/public/interfaces/network/url_loader.mojom", |
19 "mojo", | 20 "mojo", |
20 "console" | 21 "console" |
21 ], function(core, connection, network, loader, mojo, console) { | 22 ], function(core, connection, support, net, loader, mojo, console) { |
22 | 23 |
23 function NetworkServiceImpl(remote) { } | 24 var netServiceHandle = mojo.connectToService( |
24 NetworkServiceImpl.prototype = | |
25 Object.create(network.NetworkServiceStub.prototype); | |
26 | |
27 function URLLoaderImpl(remote) { } | |
28 URLLoaderImpl.prototype = | |
29 Object.create(loader.URLLoaderStub.prototype); | |
30 | |
31 var networkServiceHandle = mojo.connectToService( | |
32 "mojo:mojo_network_service", "mojo::NetworkService"); | 25 "mojo:mojo_network_service", "mojo::NetworkService"); |
33 var networkConnection = new connection.Connection( | 26 var netConnection = new connection.Connection( |
34 networkServiceHandle, NetworkServiceImpl, network.NetworkServiceProxy); | 27 netServiceHandle, net.NetworkServiceStub, net.NetworkServiceProxy); |
35 | 28 |
36 var urlLoaderPipe = new core.createMessagePipe(); | 29 var urlLoaderPipe = new core.createMessagePipe(); |
37 networkConnection.remote.createURLLoader(urlLoaderPipe.handle1); | 30 netConnection.remote.createURLLoader(urlLoaderPipe.handle1); |
38 var urlLoaderConnection = new connection.Connection( | 31 var urlLoaderConnection = new connection.Connection( |
39 urlLoaderPipe.handle0, URLLoaderImpl, loader.URLLoaderProxy); | 32 urlLoaderPipe.handle0, loader.URLLoaderStub, loader.URLLoaderProxy); |
40 | 33 |
41 var urlRequest = new loader.URLRequest(); | 34 var urlRequest = new loader.URLRequest(); |
42 urlRequest.url = "http://www.cnn.com"; | 35 urlRequest.url = "http://www.cnn.com"; |
43 urlRequest.method = "GET"; | 36 urlRequest.method = "GET"; |
44 urlRequest.auto_follow_redirects = true; | 37 urlRequest.auto_follow_redirects = true; |
38 | |
45 var urlRequestPromise = urlLoaderConnection.remote.start(urlRequest); | 39 var urlRequestPromise = urlLoaderConnection.remote.start(urlRequest); |
46 urlRequestPromise.then( | 40 urlRequestPromise.then(function(result) { |
47 function(result) { | 41 for(var key in result.response) |
48 var body = core.readData(result.response.body, | 42 console.log(key + " => " + result.response[key]); |
49 core.READ_DATA_FLAG_ALL_OR_NONE); | 43 var drainDataPromise = core.drainData(result.response.body); |
Matt Perry
2014/09/24 00:39:41
Is this temporary necessary?
hansmuller
2014/09/24 17:47:53
No, I just thought it made the example a little ea
Matt Perry
2014/09/24 21:30:47
I think it's common in JS promises to just chain t
| |
50 if (body.result == core.RESULT_OK) | 44 drainDataPromise.then(function(result) { |
51 console.log("body.buffer.byteLength=" + body.buffer.byteLength); | 45 console.log("read " + result.buffer.byteLength + " bytes"); |
52 else | 46 }).then(function() { |
53 console.log("core.readData() failed err=" + body.result); | |
54 for(var key in result.response) | |
55 console.log(key + " => " + result.response[key]); | |
56 mojo.quit(); | |
57 }, | |
58 function(error) { | |
59 console.log("FAIL " + error.toString()); | |
60 mojo.quit(); | 47 mojo.quit(); |
61 }); | 48 }); |
62 | 49 }); |
63 }); | 50 }); |
OLD | NEW |