| OLD | NEW |
| 1 #!mojo mojo:dart_content_handler | 1 #!mojo mojo:dart_content_handler |
| 2 // Copyright 2014 The Chromium Authors. All rights reserved. | 2 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 // Use of this source code is governed by a BSD-style license that can be | 3 // Use of this source code is governed by a BSD-style license that can be |
| 4 // found in the LICENSE file. | 4 // found in the LICENSE file. |
| 5 | 5 |
| 6 // Run with, e.g.: | 6 // Run with, e.g.: |
| 7 // mojo_shell "mojo:dart_wget http://www.google.com" | 7 // mojo_shell "mojo:dart_wget http://www.google.com" |
| 8 | 8 |
| 9 import 'dart:async'; | 9 import 'dart:async'; |
| 10 import 'dart:typed_data'; | 10 import 'dart:typed_data'; |
| 11 | 11 |
| 12 import 'package:mojo/application.dart'; | 12 import 'package:mojo/application.dart'; |
| 13 import 'package:mojo/core.dart'; | 13 import 'package:mojo/core.dart'; |
| 14 import 'package:mojo/mojo/url_request.mojom.dart'; | 14 import 'package:mojo/mojo/url_request.mojom.dart'; |
| 15 import 'package:mojo_services/mojo/network_service.mojom.dart'; | 15 import 'package:mojo_services/mojo/network_service.mojom.dart'; |
| 16 import 'package:mojo_services/mojo/url_loader.mojom.dart'; | 16 import 'package:mojo_services/mojo/url_loader.mojom.dart'; |
| 17 | 17 |
| 18 class WGet extends Application { | 18 class WGet extends Application { |
| 19 NetworkServiceProxy _networkService; | 19 NetworkServiceInterfaceRequest _networkService; |
| 20 UrlLoaderProxy _urlLoader; | 20 UrlLoaderProxy _urlLoader; |
| 21 | 21 |
| 22 WGet.fromHandle(MojoHandle handle) : super.fromHandle(handle); | 22 WGet.fromHandle(MojoHandle handle) : super.fromHandle(handle); |
| 23 | 23 |
| 24 void initialize(List<String> args, String url) { | 24 void initialize(List<String> args, String url) { |
| 25 run(args); | 25 run(args); |
| 26 } | 26 } |
| 27 | 27 |
| 28 run(List<String> args) async { | 28 run(List<String> args) async { |
| 29 if (args == null || args.length != 2) { | 29 if (args == null || args.length != 2) { |
| 30 throw "Expected URL argument"; | 30 throw "Expected URL argument"; |
| 31 } | 31 } |
| 32 | 32 |
| 33 ByteData bodyData = await _getUrl(args[1]); | 33 ByteData bodyData = await _getUrl(args[1]); |
| 34 print(">>> Body <<<"); | 34 print(">>> Body <<<"); |
| 35 print(new String.fromCharCodes(bodyData.buffer.asUint8List())); | 35 print(new String.fromCharCodes(bodyData.buffer.asUint8List())); |
| 36 print(">>> EOF <<<"); | 36 print(">>> EOF <<<"); |
| 37 | 37 |
| 38 _closeProxies(); | 38 _closeInterfaces(); |
| 39 await close(); | 39 await close(); |
| 40 MojoHandle.reportLeakedHandles(); | 40 MojoHandle.reportLeakedHandles(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 Future<ByteData> _getUrl(String url) async { | 43 Future<ByteData> _getUrl(String url) async { |
| 44 _initProxiesIfNeeded(); | 44 _initInterfacesIfNeeded(); |
| 45 | 45 |
| 46 var urlRequest = new UrlRequest() | 46 var urlRequest = new UrlRequest() |
| 47 ..url = url | 47 ..url = url |
| 48 ..autoFollowRedirects = true; | 48 ..autoFollowRedirects = true; |
| 49 | 49 |
| 50 var urlResponse = await _urlLoader.start(urlRequest); | 50 var urlResponse = await _urlLoader.start(urlRequest); |
| 51 print(">>> Headers <<<"); | 51 print(">>> Headers <<<"); |
| 52 print(urlResponse.response.headers.join('\n')); | 52 print(urlResponse.response.headers.join('\n')); |
| 53 | 53 |
| 54 return DataPipeDrainer.drainHandle(urlResponse.response.body); | 54 return DataPipeDrainer.drainHandle(urlResponse.response.body); |
| 55 } | 55 } |
| 56 | 56 |
| 57 void _initProxiesIfNeeded() { | 57 void _initInterfacesIfNeeded() { |
| 58 if (_networkService == null) { | 58 if (_networkService == null) { |
| 59 _networkService = new NetworkServiceProxy.unbound(); | 59 _networkService = new NetworkServiceInterfaceRequest(); |
| 60 connectToService("mojo:network_service", _networkService); | 60 connectToService("mojo:network_service", _networkService); |
| 61 } | 61 } |
| 62 if (_urlLoader == null) { | 62 if (_urlLoader == null) { |
| 63 _urlLoader = new UrlLoaderProxy.unbound(); | 63 _urlLoader = new UrlLoaderInterfaceRequest(); |
| 64 _networkService.createUrlLoader(_urlLoader); | 64 _networkService.createUrlLoader(_urlLoader); |
| 65 } | 65 } |
| 66 } | 66 } |
| 67 | 67 |
| 68 void _closeProxies() { | 68 void _closeInterfaces() { |
| 69 _urlLoader.close(); | 69 _urlLoader.close(); |
| 70 _urlLoader = null; | 70 _urlLoader = null; |
| 71 _networkService.close(); | 71 _networkService.close(); |
| 72 _networkService = null; | 72 _networkService = null; |
| 73 } | 73 } |
| 74 } | 74 } |
| 75 | 75 |
| 76 main(List args, handleToken) { | 76 main(List args, handleToken) { |
| 77 MojoHandle appHandle = new MojoHandle(handleToken); | 77 MojoHandle appHandle = new MojoHandle(handleToken); |
| 78 new WGet.fromHandle(appHandle); | 78 new WGet.fromHandle(appHandle); |
| 79 } | 79 } |
| OLD | NEW |