Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(170)

Side by Side Diff: lib/src/browser/loader.dart

Issue 1612003002: Add browser-compatible version. (Closed) Base URL: https://github.com/dart-lang/resource.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 import "dart:async" show Future, Stream; 5 import "dart:async" show Future, Stream;
6 import "dart:convert" show Encoding; 6 import "dart:convert" show Encoding;
7 import "dart:isolate" show Isolate; 7 import "dart:isolate" show Isolate;
8 import "io.dart" as io; // TODO: make this import configuration dependent. 8
9 import "html_io.dart" as io;
9 10
10 /// Resource loading strategy. 11 /// Resource loading strategy.
11 /// 12 ///
12 /// An abstraction of the functionality needed to load resources. 13 /// An abstraction of the functionality needed to load resources.
13 /// 14 ///
14 /// Implementations of this interface decide which URI schemes they support. 15 /// Implementations of this interface decide which URI schemes they support.
15 abstract class ResourceLoader { 16 abstract class ResourceLoader {
16 /// A resource loader that can load as many of the following URI 17 /// A resource loader that can load as many of the following URI
17 /// schemes as are supported by the platform: 18 /// schemes as are supported by the platform:
18 /// * file 19 /// * file
19 /// * http 20 /// * http
20 /// * https 21 /// * https
21 /// * data 22 /// * data
22 /// * package 23 /// * package
23 /// 24 ///
24 /// (For example, file: URIs are not supported in the browser). 25 /// (For example, file: URIs are not supported in the browser).
25 /// Relative URI references are accepted - they are resolved against 26 /// Relative URI references are accepted - they are resolved against
26 /// [Uri.base] before being loaded. 27 /// [Uri.base] before being loaded.
27 static const ResourceLoader defaultLoader = const PackageLoader(); 28 static ResourceLoader get defaultLoader => const DefaultLoader();
28 29
29 /// Reads the file located by [uri] as a stream of bytes. 30 /// Reads the file located by [uri] as a stream of bytes.
30 Stream<List<int>> openRead(Uri uri); 31 Stream<List<int>> openRead(Uri uri);
31 32
32 /// Reads the file located by [uri] as a list of bytes. 33 /// Reads the file located by [uri] as a list of bytes.
33 Future<List<int>> readAsBytes(Uri uri); 34 Future<List<int>> readAsBytes(Uri uri);
34 35
35 /// Reads the file located by [uri] as a [String]. 36 /// Reads the file located by [uri] as a [String].
36 /// 37 ///
37 /// The file bytes are decoded using [encoding], if provided. 38 /// The file bytes are decoded using [encoding], if provided.
38 /// 39 ///
39 /// If [encoding] is omitted, the default for the `file:` scheme is UTF-8. 40 /// If [encoding] is omitted, the default for the `file:` scheme is UTF-8.
40 /// For `http`, `https` and `data` URIs, the Content-Type header's charset 41 /// For `http`, `https` and `data` URIs, the Content-Type header's charset
41 /// is used, if available and recognized by [Encoding.getByName], 42 /// is used, if available and recognized by [Encoding.getByName],
42 /// otherwise it defaults to Latin-1 for `http` and `https` 43 /// otherwise it defaults to Latin-1 for `http` and `https`
43 /// and to ASCII for `data` URIs. 44 /// and to ASCII for `data` URIs.
44 Future<String> readAsString(Uri uri, { Encoding encoding }); 45 Future<String> readAsString(Uri uri, { Encoding encoding });
45 } 46 }
46 47
47 /// Default implementation of [ResourceLoader]. 48 /// Default implementation of [ResourceLoader]..
48 /// 49 ///
49 /// Uses the system's available loading functionality to implement the 50 /// Uses the system's available loading functionality to implement the
50 /// loading functions. 51 /// loading functions.
51 /// 52 ///
52 /// Supports `http:`, `https:`, `file:` and `data:` URIs. 53 /// Supports as many of `http:`, `https:`, `file:` and `data:` URIs as
54 /// possible.
53 class DefaultLoader implements ResourceLoader { 55 class DefaultLoader implements ResourceLoader {
54 const DefaultLoader(); 56 const DefaultLoader();
55 57
56 Stream<List<int>> openRead(Uri uri) => io.readAsStream(uri); 58 Stream<List<int>> openRead(Uri uri) => io.readAsStream(uri);
57 59
58 Future<List<int>> readAsBytes(Uri uri) => io.readAsBytes(uri); 60 Future<List<int>> readAsBytes(Uri uri) => io.readAsBytes(uri);
59 61
60 Future<String> readAsString(Uri uri, { Encoding encoding }) => 62 Future<String> readAsString(Uri uri, { Encoding encoding }) =>
61 io.readAsString(uri, encoding); 63 io.readAsString(uri, encoding);
62 } 64 }
63
64
65 /// Implementation of [ResourceLoader] that accepts relative and package: URIs.
66 ///
67 /// Like [DefaultLoader] except that it resolves package URIs and relative
68 /// URI references as well.
69 ///
70 /// This class may be useful when you don't want to bother creating a [Resource]
71 /// object, and just want to load a resource directly.
72 class PackageLoader implements ResourceLoader {
73 const PackageLoader();
74
75 Stream<List<int>> openRead(Uri uri) async* {
76 yield* io.readAsStream(await resolveUri(uri));
77 }
78
79 Future<List<int>> readAsBytes(Uri uri) async =>
80 io.readAsBytes(await resolveUri(uri));
81
82 Future<String> readAsString(Uri uri, { Encoding encoding }) async =>
83 io.readAsString(await resolveUri(uri), encoding);
84 }
85
86 /// Helper function for resolving to a non-relative, non-package URI.
87 Future<Uri> resolveUri(Uri uri) async {
88 if (uri.scheme == "package") {
89 return Isolate.resolvePackageUri(uri);
90 }
91 return Uri.base.resolveUri(uri);
92 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698