Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2014, 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 /// Contains logic to initialize web_components apps during development. This | |
| 6 /// implementation uses dart:mirrors to load each library as they are discovered | |
| 7 /// through HTML imports. This is only meant to be used during development in | |
| 8 /// dartium, and the web_components transformers replace this implementation | |
| 9 /// for deployment. | |
| 10 library web_components.src.mirror_initializer; | |
| 11 | |
| 12 import 'dart:async'; | |
| 13 import 'dart:collection' show LinkedHashMap; | |
| 14 import 'dart:mirrors'; | |
| 15 import 'dart:html'; | |
| 16 import 'package:initialize/initialize.dart' as init; | |
| 17 | |
| 18 Future run({List<Type> typeFilter, init.InitializerFilter customFilter}) async { | |
| 19 var libraryUris = _discoverLibrariesToLoad(document, window.location.href) | |
| 20 .map(Uri.parse); | |
| 21 | |
| 22 for (var uri in libraryUris) { | |
| 23 await init.run( | |
| 24 typeFilter: typeFilter, customFilter: customFilter, from: uri); | |
| 25 } | |
| 26 return null; | |
| 27 } | |
| 28 | |
| 29 /// Walks the HTML import structure to discover all script tags that are | |
| 30 /// implicitly loaded. This code is only used in Dartium and should only be | |
| 31 /// called after all HTML imports are resolved. Polymer ensures this by asking | |
| 32 /// users to put their Dart script tags after all HTML imports (this is checked | |
| 33 /// by the linter, and Dartium will otherwise show an error message). | |
| 34 Iterable<_ScriptInfo> _discoverScripts( | |
| 35 Document doc, String baseUri, [_State state]) { | |
| 36 if (state == null) state = new _State(); | |
| 37 if (doc == null) { | |
| 38 print('warning: $baseUri not found.'); | |
| 39 return state.scripts.values; | |
| 40 } | |
| 41 if (!state.seen.add(doc)) return state.scripts.values; | |
| 42 | |
| 43 for (var node in doc.querySelectorAll('script,link[rel="import"]')) { | |
| 44 if (node is LinkElement) { | |
| 45 _discoverScripts(node.import, node.href, state); | |
| 46 } else if (node is ScriptElement && node.type == 'application/dart') { | |
| 47 var info = _scriptInfoFor(node, baseUri); | |
| 48 if (state.scripts.containsKey(info.resolvedUrl)) { | |
| 49 print('warning: Script `${info.resolvedUrl}` included more than once. ' | |
| 50 'See http://goo.gl/5HPeuP#polymer_44 for more details.'); | |
|
Siggi Cherem (dart-lang)
2015/03/23 22:16:26
we might want to relabel this warning in the futur
jakemac
2015/03/23 22:32:41
Acknowledged.
| |
| 51 } else { | |
| 52 state.scripts[info.resolvedUrl] = info; | |
| 53 } | |
| 54 } | |
| 55 } | |
| 56 return state.scripts.values; | |
| 57 } | |
| 58 | |
| 59 /// Internal state used in [_discoverScripts]. | |
| 60 class _State { | |
| 61 /// Documents that we have visited thus far. | |
| 62 final Set<Document> seen = new Set(); | |
| 63 | |
| 64 /// Scripts that have been discovered, in tree order. | |
| 65 final LinkedHashMap<String, _ScriptInfo> scripts = {}; | |
| 66 } | |
| 67 | |
| 68 /// Holds information about a Dart script tag. | |
| 69 class _ScriptInfo { | |
| 70 /// The original URL seen in the tag fully resolved. | |
| 71 final String resolvedUrl; | |
| 72 | |
| 73 /// Whether there was no URL, but code inlined instead. | |
| 74 bool get isInline => text != null; | |
| 75 | |
| 76 /// Whether it seems to be a 'package:' URL (starts with the package-root). | |
| 77 bool get isPackage => packageUrl != null; | |
| 78 | |
| 79 /// The equivalent 'package:' URL, if any. | |
| 80 final String packageUrl; | |
| 81 | |
| 82 /// The inlined text, if any. | |
| 83 final String text; | |
| 84 | |
| 85 /// Returns a base64 `data:` uri with the contents of [text]. | |
| 86 // TODO(sigmund): change back to application/dart: using text/javascript seems | |
| 87 // wrong but it hides a warning in Dartium (dartbug.com/18000). | |
| 88 String get dataUrl => 'data:text/javascript;base64,${window.btoa(text)}'; | |
| 89 | |
| 90 /// URL to import the contents of the original script from Dart. This is | |
| 91 /// either the source URL if the script tag had a `src` attribute, or a base64 | |
| 92 /// encoded `data:` URL if the script contents are inlined, or a `package:` | |
| 93 /// URL if the script can be resolved via a package URL. | |
| 94 String get importUrl => | |
| 95 isInline ? dataUrl : (isPackage ? packageUrl : resolvedUrl); | |
| 96 | |
| 97 _ScriptInfo(this.resolvedUrl, {this.packageUrl, this.text}); | |
| 98 } | |
| 99 | |
| 100 | |
| 101 // TODO(sigmund): explore other (cheaper) ways to resolve URIs relative to the | |
| 102 // root library (see dartbug.com/12612) | |
| 103 final _rootUri = currentMirrorSystem().isolate.rootLibrary.uri; | |
| 104 | |
| 105 /// Returns [_ScriptInfo] for [script] which was seen in [baseUri]. | |
| 106 _ScriptInfo _scriptInfoFor(script, baseUri) { | |
| 107 var uriString = script.src; | |
| 108 if (uriString != '') { | |
| 109 var uri = _rootUri.resolve(uriString); | |
| 110 if (!_isHttpStylePackageUrl(uri)) return new _ScriptInfo('$uri'); | |
| 111 // Use package: urls if available. This rule here is more permissive than | |
| 112 // how we translate urls in polymer-build, but we expect Dartium to limit | |
| 113 // the cases where there are differences. The polymer-build issues an error | |
| 114 // when using packages/ inside lib without properly stepping out all the way | |
| 115 // to the packages folder. If users don't create symlinks in the source | |
| 116 // tree, then Dartium will also complain because it won't find the file seen | |
| 117 // in an HTML import. | |
| 118 var packagePath = uri.path.substring( | |
| 119 uri.path.lastIndexOf('packages/') + 'packages/'.length); | |
| 120 return new _ScriptInfo('$uri', packageUrl: 'package:$packagePath'); | |
| 121 } | |
| 122 | |
| 123 return new _ScriptInfo(baseUri, text: script.text); | |
| 124 } | |
| 125 | |
| 126 /// Whether [uri] is an http URI that contains a 'packages' segment, and | |
| 127 /// therefore could be converted into a 'package:' URI. | |
| 128 bool _isHttpStylePackageUrl(Uri uri) { | |
| 129 var uriPath = uri.path; | |
| 130 return uri.scheme == _rootUri.scheme && | |
| 131 // Don't process cross-domain uris. | |
| 132 uri.authority == _rootUri.authority && | |
| 133 uriPath.endsWith('.dart') && | |
| 134 (uriPath.contains('/packages/') || uriPath.startsWith('packages/')); | |
| 135 } | |
| 136 | |
| 137 Iterable<String> _discoverLibrariesToLoad(Document doc, String baseUri) => | |
| 138 _discoverScripts(doc, baseUri).map( | |
| 139 (info) => _packageUrlExists(info) ? info.packageUrl : info.resolvedUrl); | |
|
Siggi Cherem (dart-lang)
2015/03/23 22:16:26
I think that to support inline Dart script tags, w
jakemac
2015/03/23 22:32:41
worked on this together, and stripped out some mor
| |
| 140 | |
| 141 /// All libraries in the current isolate. | |
| 142 final _libs = currentMirrorSystem().libraries; | |
| 143 | |
| 144 bool _packageUrlExists(_ScriptInfo info) => | |
| 145 info.isPackage && _libs[Uri.parse(info.packageUrl)] != null; | |
| OLD | NEW |