| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 import '../shell.dart' as shell; | 5 // TODO(abarth): Remove this file once clients migrate to the new location. |
| 6 import 'dart:async'; | 6 export '../../mojo/net/fetch.dart'; |
| 7 import 'dart:typed_data'; | |
| 8 import 'package:mojo/core.dart' as core; | |
| 9 import 'package:mojom/mojo/network_service.mojom.dart'; | |
| 10 import 'package:mojom/mojo/url_loader.mojom.dart'; | |
| 11 import 'package:mojom/mojo/url_request.mojom.dart'; | |
| 12 import 'package:mojom/mojo/url_response.mojom.dart'; | |
| 13 | |
| 14 class Response { | |
| 15 ByteData body; | |
| 16 | |
| 17 Response(this.body); | |
| 18 | |
| 19 String bodyAsString() { | |
| 20 return new String.fromCharCodes(new Uint8List.view(body.buffer)); | |
| 21 } | |
| 22 } | |
| 23 | |
| 24 Future<UrlResponse> fetch(UrlRequest request) async { | |
| 25 NetworkServiceProxy net = new NetworkServiceProxy.unbound(); | |
| 26 shell.requestService("mojo:authenticated_network_service", net); | |
| 27 | |
| 28 UrlLoaderProxy loader = new UrlLoaderProxy.unbound(); | |
| 29 net.ptr.createUrlLoader(loader); | |
| 30 | |
| 31 UrlResponse response = (await loader.ptr.start(request)).response; | |
| 32 | |
| 33 loader.close(); | |
| 34 net.close(); | |
| 35 return response; | |
| 36 } | |
| 37 | |
| 38 Future<UrlResponse> fetchUrl(String relativeUrl) async { | |
| 39 String url = Uri.base.resolve(relativeUrl).toString(); | |
| 40 UrlRequest request = new UrlRequest() | |
| 41 ..url = url | |
| 42 ..autoFollowRedirects = true; | |
| 43 return fetch(request); | |
| 44 } | |
| 45 | |
| 46 Future<Response> fetchBody(String relativeUrl) async { | |
| 47 UrlResponse response = await fetchUrl(relativeUrl); | |
| 48 if (response.body == null) return new Response(null); | |
| 49 | |
| 50 ByteData data = await core.DataPipeDrainer.drainHandle(response.body); | |
| 51 return new Response(data); | |
| 52 } | |
| OLD | NEW |