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 // Run with, e.g.: | 5 // Run with, e.g.: |
6 // mojo_shell "mojo:dart_wget http://www.google.com" | 6 // mojo_shell "mojo:dart_wget http://www.google.com" |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:typed_data'; | 9 import 'dart:typed_data'; |
10 import 'mojo:application'; | 10 import 'mojo:application'; |
11 import 'mojo:bindings'; | 11 import 'mojo:bindings'; |
12 import 'mojo:core'; | 12 import 'mojo:core'; |
13 | 13 |
14 import 'package:mojo/services/network/public/interfaces/network_service.mojom.da
rt'; | 14 import 'package:mojo/services/network/public/interfaces/network_service.mojom.da
rt'; |
15 import 'package:mojo/services/network/public/interfaces/url_loader.mojom.dart'; | 15 import 'package:mojo/services/network/public/interfaces/url_loader.mojom.dart'; |
16 | 16 |
17 class WGet extends Application { | 17 class WGet extends Application { |
18 NetworkServiceProxy _networkService; | 18 NetworkServiceProxy _networkServiceProxy; |
| 19 NetworkService _networkService; |
19 UrlLoaderProxy _urlLoaderProxy; | 20 UrlLoaderProxy _urlLoaderProxy; |
| 21 UrlLoader _urlLoader; |
20 | 22 |
21 WGet.fromHandle(MojoHandle handle) : super.fromHandle(handle); | 23 WGet.fromHandle(MojoHandle handle) : super.fromHandle(handle); |
22 | 24 |
23 void initialize(List<String> args, String url) { | 25 void initialize(List<String> args, String url) { |
24 run(args); | 26 run(args); |
25 } | 27 } |
26 | 28 |
27 run(List<String> args) async { | 29 run(List<String> args) async { |
28 if (args == null || args.length != 2) { | 30 if (args == null || args.length != 2) { |
29 throw "Expected URL argument"; | 31 throw "Expected URL argument"; |
30 } | 32 } |
31 | 33 |
32 ByteData bodyData = await _getUrl(args[1]); | 34 ByteData bodyData = await _getUrl(args[1]); |
33 print(">>> Body <<<"); | 35 print(">>> Body <<<"); |
34 print(new String.fromCharCodes(bodyData.buffer.asUint8List())); | 36 print(new String.fromCharCodes(bodyData.buffer.asUint8List())); |
35 print(">>> EOF <<<"); | 37 print(">>> EOF <<<"); |
36 | 38 |
37 _closeProxies(); | 39 _closeProxies(); |
38 close(); | 40 close(); |
39 } | 41 } |
40 | 42 |
41 Future<ByteData> _getUrl(String url) async { | 43 Future<ByteData> _getUrl(String url) async { |
42 _initProxiesIfNeeded(); | 44 _initProxiesIfNeeded(); |
43 | 45 |
44 var urlRequest = new UrlRequest() | 46 var urlRequest = new UrlRequest() |
45 ..url = url | 47 ..url = url |
46 ..autoFollowRedirects = true; | 48 ..autoFollowRedirects = true; |
47 | 49 |
48 var urlResponse = await _urlLoaderProxy.start(urlRequest); | 50 var urlResponse = await _urlLoader.start(urlRequest); |
49 print(">>> Headers <<<"); | 51 print(">>> Headers <<<"); |
50 print(urlResponse.response.headers.join('\n')); | 52 print(urlResponse.response.headers.join('\n')); |
51 | 53 |
52 return DataPipeDrainer.drainHandle(urlResponse.response.body); | 54 return DataPipeDrainer.drainHandle(urlResponse.response.body); |
53 } | 55 } |
54 | 56 |
55 void _initProxiesIfNeeded() { | 57 void _initProxiesIfNeeded() { |
56 if (_networkService == null) { | 58 if (_networkService == null) { |
57 _networkService = new NetworkServiceProxy.unbound(); | 59 _networkServiceProxy = new NetworkServiceProxy.unbound(); |
58 connectToService("mojo:network_service", _networkService); | 60 _networkService = _networkServiceProxy.interface; |
| 61 connectToService("mojo:network_service", _networkServiceProxy); |
59 } | 62 } |
60 if (_urlLoaderProxy == null) { | 63 if (_urlLoaderProxy == null) { |
61 _urlLoaderProxy = new UrlLoaderProxy.unbound(); | 64 _urlLoaderProxy = new UrlLoaderProxy.unbound(); |
| 65 _urlLoader = _urlLoaderProxy.interface; |
62 _networkService.createUrlLoader(_urlLoaderProxy); | 66 _networkService.createUrlLoader(_urlLoaderProxy); |
63 } | 67 } |
64 } | 68 } |
65 | 69 |
66 void _closeProxies() { | 70 void _closeProxies() { |
67 _urlLoaderProxy.close(); | 71 _urlLoaderProxy.impl.close(); |
68 _networkService.close(); | |
69 _urlLoaderProxy = null; | 72 _urlLoaderProxy = null; |
| 73 _urlLoader = null; |
| 74 _networkServiceProxy.impl.close(); |
| 75 _networkServiceProxy = null; |
70 _networkService = null; | 76 _networkService = null; |
71 } | 77 } |
72 } | 78 } |
73 | 79 |
74 main(List args) { | 80 main(List args) { |
75 MojoHandle appHandle = new MojoHandle(args[0]); | 81 MojoHandle appHandle = new MojoHandle(args[0]); |
76 var wget = new WGet.fromHandle(appHandle); | 82 new WGet.fromHandle(appHandle); |
77 wget.listen(); | |
78 } | 83 } |
OLD | NEW |