OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 import 'common.dart'; | |
6 import 'elements/elements.dart' show LibraryElement; | |
7 | |
8 /// API used by the library loader to translate internal SDK URIs into file | |
9 /// system readable URIs. | |
10 abstract class ResolvedUriTranslator { | |
11 factory ResolvedUriTranslator( | |
12 Map<String, Uri> sdkLibraries, DiagnosticReporter reporter) = | |
13 _ResolvedUriTranslator; | |
14 | |
15 /// The set of platform libraries reported as unsupported. | |
16 /// | |
17 /// For instance when importing 'dart:io' without '--categories=Server'. | |
18 Set<Uri> get disallowedLibraryUris; | |
19 | |
20 /// Whether or not a mockable library has been translated. | |
21 bool get mockableLibraryUsed; | |
22 | |
23 /// A mapping from dart: library names to their location. | |
24 Map<String, Uri> get sdkLibraries; | |
25 | |
26 /// Translates the resolved [uri] into a readable URI. | |
27 /// | |
28 /// The [importingLibrary] holds the library importing [uri] or | |
29 /// [:null:] if [uri] is loaded as the main library. The | |
Johnni Winther
2016/04/13 08:21:43
[:null:] -> `null`
The [: ... :] notation is arch
Harry Terkelsen
2016/04/13 20:23:40
Done.
| |
30 /// [importingLibrary] is used to grant access to internal libraries from | |
31 /// platform libraries and patch libraries. | |
32 /// | |
33 /// If the [uri] is not accessible from [importingLibrary], this method | |
34 /// is responsible for reporting errors. | |
35 /// | |
36 /// See [LibraryLoader] for terminology on URIs. | |
37 Uri translate(LibraryElement importingLibrary, Uri uri, | |
38 [Spannable spannable]); | |
39 } | |
40 | |
41 class _ResolvedUriTranslator implements ResolvedUriTranslator { | |
42 final Map<String, Uri> _sdkLibraries; | |
43 final DiagnosticReporter _reporter; | |
44 | |
45 Set<Uri> disallowedLibraryUris = new Set<Uri>(); | |
46 bool mockableLibraryUsed = false; | |
47 | |
48 _ResolvedUriTranslator(this._sdkLibraries, this._reporter); | |
49 | |
50 Map<String, Uri> get sdkLibraries => _sdkLibraries; | |
51 | |
52 @override | |
53 Uri translate(LibraryElement importingLibrary, Uri uri, | |
54 [Spannable spannable]) { | |
55 if (uri.scheme == 'dart') { | |
56 return translateDartUri(importingLibrary, uri, spannable); | |
57 } | |
58 return uri; | |
59 } | |
60 | |
61 /// Translates "resolvedUri" with scheme "dart" to a [uri] resolved relative | |
62 /// to `options.platformConfigUri` according to the information in the file at | |
63 /// `options.platformConfigUri`. | |
64 /// | |
65 /// Returns null and emits an error if the library could not be found or | |
66 /// imported into [importingLibrary]. | |
67 /// | |
68 /// Internal libraries (whose name starts with '_') can be only resolved if | |
69 /// [importingLibrary] is a platform or patch library. | |
70 Uri translateDartUri( | |
71 LibraryElement importingLibrary, Uri resolvedUri, Spannable spannable) { | |
72 Uri location = lookupLibraryUri(resolvedUri.path); | |
73 | |
74 if (location == null) { | |
75 _reporter.reportErrorMessage(spannable, MessageKind.LIBRARY_NOT_FOUND, | |
76 {'resolvedUri': resolvedUri}); | |
77 return null; | |
78 } | |
79 | |
80 if (resolvedUri.path.startsWith('_')) { | |
81 bool allowInternalLibraryAccess = importingLibrary != null && | |
82 (importingLibrary.isPlatformLibrary || | |
83 importingLibrary.isPatch || | |
84 importingLibrary.canonicalUri.path | |
85 .contains('sdk/tests/compiler/dart2js_native')); | |
86 | |
87 if (!allowInternalLibraryAccess) { | |
88 if (importingLibrary != null) { | |
89 _reporter.reportErrorMessage( | |
90 spannable, MessageKind.INTERNAL_LIBRARY_FROM, { | |
91 'resolvedUri': resolvedUri, | |
92 'importingUri': importingLibrary.canonicalUri | |
93 }); | |
94 } else { | |
95 _reporter.reportErrorMessage(spannable, MessageKind.INTERNAL_LIBRARY, | |
96 {'resolvedUri': resolvedUri}); | |
97 registerDisallowedLibraryUse(resolvedUri); | |
98 } | |
99 return null; | |
100 } | |
101 } | |
102 | |
103 if (location.scheme == "unsupported") { | |
104 _reporter.reportErrorMessage(spannable, MessageKind.LIBRARY_NOT_SUPPORTED, | |
105 {'resolvedUri': resolvedUri}); | |
106 registerDisallowedLibraryUse(resolvedUri); | |
107 return null; | |
108 } | |
109 | |
110 if (resolvedUri.path == 'html' || resolvedUri.path == 'io') { | |
111 // TODO(ahe): Get rid of mockableLibraryUsed when test.dart | |
112 // supports this use case better. | |
113 mockableLibraryUsed = true; | |
114 } | |
115 return location; | |
116 } | |
117 | |
118 void registerDisallowedLibraryUse(Uri uri) { | |
119 disallowedLibraryUris.add(uri); | |
120 } | |
121 | |
122 Uri lookupLibraryUri(String libraryName) { | |
123 return _sdkLibraries[libraryName]; | |
124 } | |
125 } | |
OLD | NEW |