OLD | NEW |
| (Empty) |
1 #!mojo mojo:sky_viewer | |
2 <sky> | |
3 <import src="dart-library.sky" as="library" /> | |
4 <script> | |
5 import 'dart:async'; | |
6 import '/mojo/public/dart/application.dart'; | |
7 import 'dart:typed_data'; | |
8 import 'dart:sky.internals' as internals; | |
9 import 'mojo:bindings'; | |
10 import 'mojo:core'; | |
11 | |
12 import 'package:mojo/services/network/public/interfaces/network_service.mojom.da
rt'; | |
13 import 'package:mojo/services/network/public/interfaces/url_loader.mojom.dart'; | |
14 import 'package:mojo/public/interfaces/application/shell.mojom.dart' as shell_mo
jom; | |
15 | |
16 class WGet extends Application { | |
17 NetworkServiceProxy _networkService; | |
18 UrlLoaderProxy _urlLoaderProxy; | |
19 | |
20 WGet.fromHandle(MojoHandle handle) : super.fromHandle(handle); | |
21 WGet(MojoMessagePipeEndpoint endpoint) : super(endpoint); | |
22 | |
23 void initialize(List<String> args, String url) { | |
24 run(args); | |
25 } | |
26 | |
27 run(List<String> args) async { | |
28 if (args == null || args.length != 2) { | |
29 throw "Expected URL argument"; | |
30 } | |
31 | |
32 ByteData bodyData = await _getUrl(args[1]); | |
33 print("read ${bodyData.lengthInBytes} bytes"); | |
34 | |
35 _closeProxies(); | |
36 close(); | |
37 } | |
38 | |
39 Future<ByteData> _getUrl(String url) async { | |
40 _initProxiesIfNeeded(); | |
41 | |
42 var urlRequest = new UrlRequest() | |
43 ..url = url | |
44 ..autoFollowRedirects = true; | |
45 | |
46 var urlResponse = await _urlLoaderProxy.start(urlRequest); | |
47 print("url => ${urlResponse.response.url}"); | |
48 print("status_line => ${urlResponse.response.statusLine}"); | |
49 print("mime_type => ${urlResponse.response.mimeType}"); | |
50 | |
51 return DataPipeDrainer.drainHandle(urlResponse.response.body); | |
52 } | |
53 | |
54 void _initProxiesIfNeeded() { | |
55 if (_networkService == null) { | |
56 _networkService = new NetworkServiceProxy.unbound(); | |
57 connectToService("mojo:network_service", _networkService); | |
58 } | |
59 if (_urlLoaderProxy == null) { | |
60 _urlLoaderProxy = new UrlLoaderProxy.unbound(); | |
61 _networkService.createUrlLoader(_urlLoaderProxy); | |
62 } | |
63 } | |
64 | |
65 void _closeProxies() { | |
66 _urlLoaderProxy.close(); | |
67 _networkService.close(); | |
68 _urlLoaderProxy = null; | |
69 _networkService = null; | |
70 } | |
71 } | |
72 | |
73 main() { | |
74 var messagePipe = new MojoMessagePipe(); | |
75 var wget = new WGet(messagePipe.endpoints[1]); | |
76 wget.listen(); | |
77 var shellProxy = new shell_mojom.ShellProxy.fromHandle(new MojoHandle(internal
s.takeShellProxyHandle())); | |
78 wget.initializeFromShellProxy(shellProxy, ["mojo:wget", "http://www.google.com
"], "mojo:wget"); | |
79 } | |
80 | |
81 </script> | |
82 </sky> | |
OLD | NEW |