Home | History | Annotate | Download | only in js
      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
      3 // found in the LICENSE file.
      4 
      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
      7 // be run using the mojo_js content handler and assumes that this source file
      8 // is specified as a URL. For example:
      9 //
     10 //   (cd YOUR_DIR/mojo/apps/js; python -m SimpleHTTPServer ) &
     11 //   mojo_shell --content-handlers=application/javascript,mojo://mojo_js \
     12 //       http://localhost:8000/test.js
     13 
     14 define("test", [
     15   "mojo/public/js/bindings/core",
     16   "mojo/public/js/bindings/connection",
     17   "mojo/public/js/bindings/support",
     18   "mojo/services/public/interfaces/network/network_service.mojom",
     19   "mojo/services/public/interfaces/network/url_loader.mojom",
     20   "mojo",
     21   "console"
     22 ], function(core, connection, support, net, loader, mojo, console) {
     23 
     24   var netServiceHandle = mojo.connectToService(
     25       "mojo:mojo_network_service", "mojo::NetworkService");
     26   var netConnection = new connection.Connection(
     27       netServiceHandle, net.NetworkServiceStub, net.NetworkServiceProxy);
     28 
     29   var urlLoaderPipe = new core.createMessagePipe();
     30   netConnection.remote.createURLLoader(urlLoaderPipe.handle1);
     31   var urlLoaderConnection = new connection.Connection(
     32       urlLoaderPipe.handle0, loader.URLLoaderStub, loader.URLLoaderProxy);
     33 
     34   var urlRequest = new loader.URLRequest();
     35   urlRequest.url = "http://www.cnn.com";
     36   urlRequest.method = "GET";
     37   urlRequest.auto_follow_redirects = true;
     38 
     39   var urlRequestPromise = urlLoaderConnection.remote.start(urlRequest);
     40   urlRequestPromise.then(function(result) {
     41     for(var key in result.response)
     42       console.log(key + " => " + result.response[key]);
     43     var drainDataPromise = core.drainData(result.response.body);
     44     drainDataPromise.then(function(result) {
     45       console.log("read " + result.buffer.byteLength + " bytes");
     46     }).then(function() {
     47       mojo.quit();
     48     });
     49   });
     50 });
     51