OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 import '../shell.dart' as shell; | |
6 import 'dart:async'; | |
7 import 'dart:sky' as sky; | |
8 import 'dart:typed_data'; | |
9 import 'package:mojo/public/dart/core.dart' as core; | |
10 import 'package:mojom/mojo/network_service.mojom.dart'; | |
11 import 'package:mojom/mojo/url_loader.mojom.dart'; | |
12 | |
13 class Response { | |
14 ByteData body; | |
15 | |
16 Response(this.body); | |
17 | |
18 String bodyAsString() { | |
19 return new String.fromCharCodes(new Uint8List.view(body.buffer)); | |
20 } | |
21 } | |
22 | |
23 Future<Response> fetch(String relativeUrl) async { | |
24 String url = new sky.URL(relativeUrl, sky.document.baseURI).href; | |
25 | |
26 var net = new NetworkServiceProxy.unbound(); | |
27 shell.requestService("mojo:network_service", net); | |
28 | |
29 var loader = new UrlLoaderProxy.unbound(); | |
30 net.ptr.createUrlLoader(loader); | |
31 | |
32 var request = new UrlRequest() | |
33 ..url = url | |
34 ..autoFollowRedirects = true; | |
35 var response = (await loader.ptr.start(request)).response; | |
36 | |
37 loader.close(); | |
38 net.close(); | |
39 | |
40 if (response.body == null) return new Response(null); | |
41 | |
42 ByteData data = await core.DataPipeDrainer.drainHandle(response.body); | |
43 return new Response(data); | |
44 } | |
OLD | NEW |