OLD | NEW |
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 part of vmservice_io; | 5 part of vmservice_io; |
6 | 6 |
| 7 _sanitizeWindowsPath(path) { |
| 8 // For Windows we need to massage the paths a bit according to |
| 9 // http://blogs.msdn.com/b/ie/archive/2006/12/06/file-uris-in-windows.aspx |
| 10 // |
| 11 // Convert |
| 12 // C:\one\two\three |
| 13 // to |
| 14 // /C:/one/two/three |
| 15 |
| 16 if (_isWindows == false) { |
| 17 // Do nothing when not running Windows. |
| 18 return path; |
| 19 } |
| 20 |
| 21 var fixedPath = "${path.replaceAll('\\', '/')}"; |
| 22 |
| 23 if ((path.length > 2) && (path[1] == ':')) { |
| 24 // Path begins with a drive letter. |
| 25 return '/$fixedPath'; |
| 26 } |
| 27 |
| 28 return fixedPath; |
| 29 } |
| 30 |
| 31 _trimWindowsPath(path) { |
| 32 // Convert /X:/ to X:/. |
| 33 if (_isWindows == false) { |
| 34 // Do nothing when not running Windows. |
| 35 return path; |
| 36 } |
| 37 if (!path.startsWith('/') || (path.length < 3)) { |
| 38 return path; |
| 39 } |
| 40 // Match '/?:'. |
| 41 if ((path[0] == '/') && (path[2] == ':')) { |
| 42 // Remove leading '/'. |
| 43 return path.substring(1); |
| 44 } |
| 45 return path; |
| 46 } |
| 47 |
| 48 // Ensure we have a trailing slash character. |
| 49 _enforceTrailingSlash(uri) { |
| 50 if (!uri.endsWith('/')) { |
| 51 return '$uri/'; |
| 52 } |
| 53 return uri; |
| 54 } |
| 55 |
| 56 // State associated with the isolate that is used for loading. |
| 57 class IsolateLoaderState { |
| 58 IsolateLoaderState(this.isolateId); |
| 59 |
| 60 final int isolateId; |
| 61 |
| 62 SendPort sp; |
| 63 |
| 64 void init(String packageRootFlag, |
| 65 String packagesConfigFlag, |
| 66 String workingDirectory) { |
| 67 // _workingDirectory must be set first. |
| 68 _workingDirectory = new Uri.directory(workingDirectory); |
| 69 // If the --package-root flag was passed. |
| 70 if (packageRootFlag != null) { |
| 71 _setPackageRoot(packageRootFlag); |
| 72 } |
| 73 // If the --packages flag was passed. |
| 74 if (packagesConfigFlag != null) { |
| 75 _setPackagesConfig(packagesConfigFlag); |
| 76 } |
| 77 } |
| 78 |
| 79 // The working directory when the embedder started. |
| 80 Uri _workingDirectory; |
| 81 |
| 82 // The root script's uri. |
| 83 Uri _rootScript; |
| 84 |
| 85 bool _traceLoading = false; |
| 86 |
| 87 // Packages are either resolved looking up in a map or resolved from within a |
| 88 // package root. |
| 89 bool get _packagesReady => (_packageRoot != null) || |
| 90 (_packageMap != null) || |
| 91 (_packageError != null); |
| 92 |
| 93 // Error string set if there was an error resolving package configuration. |
| 94 // For example not finding a .packages file or packages/ directory, malformed |
| 95 // .packages file or any other related error. |
| 96 String _packageError = null; |
| 97 |
| 98 // The directory to look in to resolve "package:" scheme URIs. By default it |
| 99 // is the 'packages' directory right next to the script. |
| 100 Uri _packageRoot = null; |
| 101 |
| 102 // The map describing how certain package names are mapped to Uris. |
| 103 Uri _packageConfig = null; |
| 104 Map<String, Uri> _packageMap = null; |
| 105 |
| 106 _setPackageRoot(String packageRoot) { |
| 107 packageRoot = _enforceTrailingSlash(packageRoot); |
| 108 if (packageRoot.startsWith('file:') || |
| 109 packageRoot.startsWith('http:') || |
| 110 packageRoot.startsWith('https:')) { |
| 111 _packageRoot = _workingDirectory.resolve(packageRoot); |
| 112 } else { |
| 113 packageRoot = _sanitizeWindowsPath(packageRoot); |
| 114 packageRoot = _trimWindowsPath(packageRoot); |
| 115 _packageRoot = _workingDirectory.resolveUri(new Uri.file(packageRoot)); |
| 116 } |
| 117 } |
| 118 |
| 119 _setPackagesConfig(String packagesParam) { |
| 120 var packagesName = _sanitizeWindowsPath(packagesParam); |
| 121 var packagesUri = Uri.parse(packagesName); |
| 122 if (packagesUri.scheme == '') { |
| 123 // Script does not have a scheme, assume that it is a path, |
| 124 // resolve it against the working directory. |
| 125 packagesUri = _workingDirectory.resolveUri(packagesUri); |
| 126 } |
| 127 _requestPackagesMap(packagesUri); |
| 128 } |
| 129 |
| 130 // Handling of access to the package root or package map from user code. |
| 131 _triggerPackageResolution(action) { |
| 132 if (_packagesReady) { |
| 133 // Packages are ready. Execute the action now. |
| 134 action(); |
| 135 } else { |
| 136 if (_pendingPackageLoads.isEmpty) { |
| 137 // Package resolution has not been setup yet, and this is the first |
| 138 // request for package resolution & loading. |
| 139 _requestPackagesMap(); |
| 140 } |
| 141 // Register the action for when the package resolution is ready. |
| 142 _pendingPackageLoads.add(action); |
| 143 } |
| 144 } |
| 145 |
| 146 // A list of callbacks which should be invoked after the package map has been |
| 147 // loaded. |
| 148 List<Function> _pendingPackageLoads = []; |
| 149 |
| 150 // Given a uri with a 'package' scheme, return a Uri that is prefixed with |
| 151 // the package root. |
| 152 Uri _resolvePackageUri(Uri uri) { |
| 153 assert(uri.scheme == "package"); |
| 154 assert(_packagesReady); |
| 155 |
| 156 if (!uri.host.isEmpty) { |
| 157 var path = '${uri.host}${uri.path}'; |
| 158 var right = 'package:$path'; |
| 159 var wrong = 'package://$path'; |
| 160 |
| 161 throw "URIs using the 'package:' scheme should look like " |
| 162 "'$right', not '$wrong'."; |
| 163 } |
| 164 |
| 165 if (_traceLoading) { |
| 166 _log('Resolving package with uri path: ${uri.path}'); |
| 167 } |
| 168 var resolvedUri; |
| 169 if (_packageError != null) { |
| 170 if (_traceLoading) { |
| 171 _log("Resolving package with pending resolution error: $_packageError"); |
| 172 } |
| 173 throw _packageError; |
| 174 } else if (_packageRoot != null) { |
| 175 resolvedUri = _packageRoot.resolve(uri.path); |
| 176 } else { |
| 177 var packageName = uri.pathSegments[0]; |
| 178 var mapping = _packageMap[packageName]; |
| 179 if (_traceLoading) { |
| 180 _log("Mapped '$packageName' package to '$mapping'"); |
| 181 } |
| 182 if (mapping == null) { |
| 183 throw "No mapping for '$packageName' package when resolving '$uri'."; |
| 184 } |
| 185 var path; |
| 186 if (uri.path.length > packageName.length) { |
| 187 path = uri.path.substring(packageName.length + 1); |
| 188 } else { |
| 189 // Handle naked package resolution to the default package name: |
| 190 // package:foo is equivalent to package:foo/foo.dart |
| 191 assert(uri.path.length == packageName.length); |
| 192 path = "$packageName.dart"; |
| 193 } |
| 194 if (_traceLoading) { |
| 195 _log("Path to be resolved in package: $path"); |
| 196 } |
| 197 resolvedUri = mapping.resolve(path); |
| 198 } |
| 199 if (_traceLoading) { |
| 200 _log("Resolved '$uri' to '$resolvedUri'."); |
| 201 } |
| 202 return resolvedUri; |
| 203 } |
| 204 |
| 205 RawReceivePort _packagesPort; |
| 206 |
| 207 void _requestPackagesMap([Uri packageConfig]) { |
| 208 assert(_packagesPort == null); |
| 209 assert(_rootScript != null); |
| 210 // Create a port to receive the packages map on. |
| 211 _packagesPort = new RawReceivePort(_handlePackagesReply); |
| 212 var sp = _packagesPort.sendPort; |
| 213 |
| 214 if (packageConfig != null) { |
| 215 // Explicitly specified .packages path. |
| 216 _handlePackagesRequest(sp, |
| 217 _traceLoading, |
| 218 -2, |
| 219 packageConfig); |
| 220 } else { |
| 221 // Search for .packages or packages/ starting at the root script. |
| 222 _handlePackagesRequest(sp, |
| 223 _traceLoading, |
| 224 -1, |
| 225 _rootScript); |
| 226 } |
| 227 |
| 228 if (_traceLoading) { |
| 229 _log("Requested packages map for '$_rootScript'."); |
| 230 } |
| 231 } |
| 232 |
| 233 void _handlePackagesReply(msg) { |
| 234 // Make sure to close the _packagePort before any other action. |
| 235 _packagesPort.close(); |
| 236 _packagesPort = null; |
| 237 |
| 238 if (_traceLoading) { |
| 239 _log("Got packages reply: $msg"); |
| 240 } |
| 241 if (msg is String) { |
| 242 if (_traceLoading) { |
| 243 _log("Got failure response on package port: '$msg'"); |
| 244 } |
| 245 // Remember the error message. |
| 246 _packageError = msg; |
| 247 } else if (msg is List) { |
| 248 if (msg.length == 1) { |
| 249 if (_traceLoading) { |
| 250 _log("Received package root: '${msg[0]}'"); |
| 251 } |
| 252 _packageRoot = Uri.parse(msg[0]); |
| 253 } else { |
| 254 // First entry contains the location of the loaded .packages file. |
| 255 assert((msg.length % 2) == 0); |
| 256 assert(msg.length >= 2); |
| 257 assert(msg[1] == null); |
| 258 _packageConfig = Uri.parse(msg[0]); |
| 259 _packageMap = new Map<String, Uri>(); |
| 260 for (var i = 2; i < msg.length; i+=2) { |
| 261 // TODO(iposva): Complain about duplicate entries. |
| 262 _packageMap[msg[i]] = Uri.parse(msg[i+1]); |
| 263 } |
| 264 if (_traceLoading) { |
| 265 _log("Setup package map: $_packageMap"); |
| 266 } |
| 267 } |
| 268 } else { |
| 269 _packageError = "Bad type of packages reply: ${msg.runtimeType}"; |
| 270 if (_traceLoading) { |
| 271 _log(_packageError); |
| 272 } |
| 273 } |
| 274 |
| 275 // Resolve all pending package loads now that we know how to resolve them. |
| 276 while (_pendingPackageLoads.length > 0) { |
| 277 // Order does not matter as we queue all of the requests up right now. |
| 278 var req = _pendingPackageLoads.removeLast(); |
| 279 // Call the registered closure, to handle the delayed action. |
| 280 req(); |
| 281 } |
| 282 // Reset the pending package loads to empty. So that we eventually can |
| 283 // finish loading. |
| 284 _pendingPackageLoads = []; |
| 285 } |
| 286 |
| 287 } |
| 288 |
7 _log(msg) { | 289 _log(msg) { |
8 print("% $msg"); | 290 print("% $msg"); |
9 } | 291 } |
10 | 292 |
11 var _httpClient; | 293 var _httpClient; |
12 | 294 |
13 // Send a response to the requesting isolate. | 295 // Send a response to the requesting isolate. |
14 void _sendResourceResponse(SendPort sp, int id, dynamic data) { | 296 void _sendResourceResponse(SendPort sp, |
| 297 int tag, |
| 298 Uri uri, |
| 299 String libraryUrl, |
| 300 dynamic data) { |
15 assert((data is List<int>) || (data is String)); | 301 assert((data is List<int>) || (data is String)); |
16 var msg = new List(2); | 302 var msg = new List(4); |
17 msg[0] = id; | 303 if (data is String) { |
18 msg[1] = data; | 304 // We encountered an error, flip the sign of the tag to indicate that. |
| 305 tag = -tag; |
| 306 if (libraryUrl == null) { |
| 307 data = 'Could not load "$uri": $data'; |
| 308 } else { |
| 309 data = 'Could not import "$uri" from "$libraryUrl": $data'; |
| 310 } |
| 311 } |
| 312 msg[0] = tag; |
| 313 msg[1] = uri.toString(); |
| 314 msg[2] = libraryUrl; |
| 315 msg[3] = data; |
19 sp.send(msg); | 316 sp.send(msg); |
20 } | 317 } |
21 | 318 |
22 void _loadHttp(SendPort sp, int id, Uri uri) { | 319 void _loadHttp(SendPort sp, |
| 320 int tag, |
| 321 Uri uri, |
| 322 Uri resolvedUri, |
| 323 String libraryUrl) { |
23 if (_httpClient == null) { | 324 if (_httpClient == null) { |
24 _httpClient = new HttpClient()..maxConnectionsPerHost = 6; | 325 _httpClient = new HttpClient()..maxConnectionsPerHost = 6; |
25 } | 326 } |
26 _httpClient.getUrl(uri) | 327 _httpClient.getUrl(resolvedUri) |
27 .then((HttpClientRequest request) => request.close()) | 328 .then((HttpClientRequest request) => request.close()) |
28 .then((HttpClientResponse response) { | 329 .then((HttpClientResponse response) { |
29 var builder = new BytesBuilder(copy: false); | 330 var builder = new BytesBuilder(copy: false); |
30 response.listen( | 331 response.listen( |
31 builder.add, | 332 builder.add, |
32 onDone: () { | 333 onDone: () { |
33 if (response.statusCode != 200) { | 334 if (response.statusCode != 200) { |
34 var msg = "Failure getting $uri:\n" | 335 var msg = "Failure getting $resolvedUri:\n" |
35 " ${response.statusCode} ${response.reasonPhrase}"; | 336 " ${response.statusCode} ${response.reasonPhrase}"; |
36 _sendResourceResponse(sp, id, msg); | 337 _sendResourceResponse(sp, tag, uri, libraryUrl, msg); |
37 } else { | 338 } else { |
38 _sendResourceResponse(sp, id, builder.takeBytes()); | 339 _sendResourceResponse(sp, tag, uri, libraryUrl, |
| 340 builder.takeBytes()); |
39 } | 341 } |
40 }, | 342 }, |
41 onError: (e) { | 343 onError: (e) { |
42 _sendResourceResponse(sp, id, e.toString()); | 344 _sendResourceResponse(sp, tag, uri, libraryUrl, e.toString()); |
43 }); | 345 }); |
44 }) | 346 }) |
45 .catchError((e) { | 347 .catchError((e) { |
46 _sendResourceResponse(sp, id, e.toString()); | 348 _sendResourceResponse(sp, tag, uri, libraryUrl, e.toString()); |
47 }); | 349 }); |
48 // It's just here to push an event on the event loop so that we invoke the | 350 // It's just here to push an event on the event loop so that we invoke the |
49 // scheduled microtasks. | 351 // scheduled microtasks. |
50 Timer.run(() {}); | 352 Timer.run(() {}); |
51 } | 353 } |
52 | 354 |
53 void _loadFile(SendPort sp, int id, Uri uri) { | 355 void _loadFile(SendPort sp, |
54 var path = uri.toFilePath(); | 356 int tag, |
| 357 Uri uri, |
| 358 Uri resolvedUri, |
| 359 String libraryUrl) { |
| 360 var path = resolvedUri.toFilePath(); |
55 var sourceFile = new File(path); | 361 var sourceFile = new File(path); |
56 sourceFile.readAsBytes().then((data) { | 362 sourceFile.readAsBytes().then((data) { |
57 _sendResourceResponse(sp, id, data); | 363 _sendResourceResponse(sp, tag, uri, libraryUrl, data); |
58 }, | 364 }, |
59 onError: (e) { | 365 onError: (e) { |
60 _sendResourceResponse(sp, id, e.toString()); | 366 _sendResourceResponse(sp, tag, uri, libraryUrl, e.toString()); |
61 }); | 367 }); |
62 } | 368 } |
63 | 369 |
64 void _loadDataUri(SendPort sp, int id, Uri uri) { | 370 void _loadDataUri(SendPort sp, |
| 371 int tag, |
| 372 Uri uri, |
| 373 Uri resolvedUri, |
| 374 String libraryUrl) { |
65 try { | 375 try { |
66 var mime = uri.data.mimeType; | 376 var mime = uri.data.mimeType; |
67 if ((mime != "application/dart") && | 377 if ((mime != "application/dart") && |
68 (mime != "text/plain")) { | 378 (mime != "text/plain")) { |
69 throw "MIME-type must be application/dart or text/plain: $mime given."; | 379 throw "MIME-type must be application/dart or text/plain: $mime given."; |
70 } | 380 } |
71 var charset = uri.data.charset; | 381 var charset = uri.data.charset; |
72 if ((charset != "utf-8") && | 382 if ((charset != "utf-8") && |
73 (charset != "US-ASCII")) { | 383 (charset != "US-ASCII")) { |
74 // The C++ portion of the embedder assumes UTF-8. | 384 // The C++ portion of the embedder assumes UTF-8. |
75 throw "Only utf-8 or US-ASCII encodings are supported: $charset given."; | 385 throw "Only utf-8 or US-ASCII encodings are supported: $charset given."; |
76 } | 386 } |
77 _sendResourceResponse(sp, id, uri.data.contentAsBytes()); | 387 _sendResourceResponse(sp, tag, uri, libraryUrl, uri.data.contentAsBytes()); |
78 } catch (e) { | 388 } catch (e) { |
79 _sendResourceResponse(sp, id, "Invalid data uri ($uri):\n $e"); | 389 _sendResourceResponse(sp, tag, uri, libraryUrl, |
| 390 "Invalid data uri ($uri):\n $e"); |
80 } | 391 } |
81 } | 392 } |
82 | 393 |
83 _handleResourceRequest(SendPort sp, bool traceLoading, int id, Uri resource) { | 394 // Loading a package URI needs to first map the package name to a loadable |
84 if (resource.scheme == 'file') { | 395 // URI. |
85 _loadFile(sp, id, resource); | 396 _loadPackage(IsolateLoaderState loaderState, |
86 } else if ((resource.scheme == 'http') || (resource.scheme == 'https')) { | 397 SendPort sp, |
87 _loadHttp(sp, id, resource); | 398 bool traceLoading, |
88 } else if ((resource.scheme == 'data')) { | 399 int tag, |
89 _loadDataUri(sp, id, resource); | 400 Uri uri, |
| 401 Uri resolvedUri, |
| 402 String libraryUrl) { |
| 403 if (loaderState._packagesReady) { |
| 404 var resolvedUri; |
| 405 try { |
| 406 resolvedUri = loaderState._resolvePackageUri(uri); |
| 407 } catch (e, s) { |
| 408 if (traceLoading) { |
| 409 _log("Exception ($e) when resolving package URI: $uri"); |
| 410 } |
| 411 // Report error. |
| 412 _sendResourceResponse(sp, |
| 413 tag, |
| 414 uri, |
| 415 libraryUrl, |
| 416 e.toString()); |
| 417 return; |
| 418 } |
| 419 // Recursively call with the new resolved uri. |
| 420 _handleResourceRequest(loaderState, |
| 421 sp, |
| 422 traceLoading, |
| 423 tag, |
| 424 uri, |
| 425 resolvedUri, |
| 426 libraryUrl); |
90 } else { | 427 } else { |
91 _sendResourceResponse(sp, id, | 428 if (loaderState._pendingPackageLoads.isEmpty) { |
92 'Unknown scheme (${resource.scheme}) for $resource'); | 429 // Package resolution has not been setup yet, and this is the first |
| 430 // request for package resolution & loading. |
| 431 loaderState._requestPackagesMap(); |
| 432 } |
| 433 // Register the action of loading this package once the package resolution |
| 434 // is ready. |
| 435 loaderState._pendingPackageLoads.add(() { |
| 436 _handleResourceRequest(loaderState, |
| 437 sp, |
| 438 traceLoading, |
| 439 tag, |
| 440 uri, |
| 441 uri, |
| 442 libraryUrl); |
| 443 }); |
| 444 if (traceLoading) { |
| 445 _log("Pending package load of '$uri': " |
| 446 "${loaderState._pendingPackageLoads.length} pending"); |
| 447 } |
93 } | 448 } |
94 } | 449 } |
95 | 450 |
| 451 // TODO(johnmccutchan): This and most other top level functions in this file |
| 452 // should be turned into methods on the IsolateLoaderState class. |
| 453 _handleResourceRequest(IsolateLoaderState loaderState, |
| 454 SendPort sp, |
| 455 bool traceLoading, |
| 456 int tag, |
| 457 Uri uri, |
| 458 Uri resolvedUri, |
| 459 String libraryUrl) { |
| 460 if (resolvedUri.scheme == 'file') { |
| 461 _loadFile(sp, tag, uri, resolvedUri, libraryUrl); |
| 462 } else if ((resolvedUri.scheme == 'http') || |
| 463 (resolvedUri.scheme == 'https')) { |
| 464 _loadHttp(sp, tag, uri, resolvedUri, libraryUrl); |
| 465 } else if ((resolvedUri.scheme == 'data')) { |
| 466 _loadDataUri(sp, tag, uri, resolvedUri, libraryUrl); |
| 467 } else if ((resolvedUri.scheme == 'package')) { |
| 468 _loadPackage(loaderState, |
| 469 sp, |
| 470 traceLoading, |
| 471 tag, |
| 472 uri, |
| 473 resolvedUri, |
| 474 libraryUrl); |
| 475 } else { |
| 476 _sendResourceResponse(sp, tag, |
| 477 uri, |
| 478 libraryUrl, |
| 479 'Unknown scheme (${resolvedUri.scheme}) for ' |
| 480 '$resolvedUri'); |
| 481 } |
| 482 } |
96 | 483 |
97 // Handling of packages requests. Finding and parsing of .packages file or | 484 // Handling of packages requests. Finding and parsing of .packages file or |
98 // packages/ directories. | 485 // packages/ directories. |
99 const _LF = 0x0A; | 486 const _LF = 0x0A; |
100 const _CR = 0x0D; | 487 const _CR = 0x0D; |
101 const _SPACE = 0x20; | 488 const _SPACE = 0x20; |
102 const _HASH = 0x23; | 489 const _HASH = 0x23; |
103 const _DOT = 0x2E; | 490 const _DOT = 0x2E; |
104 const _COLON = 0x3A; | 491 const _COLON = 0x3A; |
105 const _DEL = 0x7F; | 492 const _DEL = 0x7F; |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 } | 709 } |
323 sp.send("Could not resolve a package location for base at $base"); | 710 sp.send("Could not resolve a package location for base at $base"); |
324 } catch (e, s) { | 711 } catch (e, s) { |
325 if (traceLoading) { | 712 if (traceLoading) { |
326 _log("Error loading packages: $e\n$s"); | 713 _log("Error loading packages: $e\n$s"); |
327 } | 714 } |
328 sp.send("Uncaught error ($e) loading packages file."); | 715 sp.send("Uncaught error ($e) loading packages file."); |
329 } | 716 } |
330 } | 717 } |
331 | 718 |
332 | |
333 Future<bool> _loadHttpPackagesFile(SendPort sp, | 719 Future<bool> _loadHttpPackagesFile(SendPort sp, |
334 bool traceLoading, | 720 bool traceLoading, |
335 Uri resource) async { | 721 Uri resource) async { |
336 try { | 722 try { |
337 if (_httpClient == null) { | 723 if (_httpClient == null) { |
338 _httpClient = new HttpClient()..maxConnectionsPerHost = 6; | 724 _httpClient = new HttpClient()..maxConnectionsPerHost = 6; |
339 } | 725 } |
340 if (traceLoading) { | 726 if (traceLoading) { |
341 _log("Fetching packages file from '$resource'."); | 727 _log("Fetching packages file from '$resource'."); |
342 } | 728 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
379 (charset != "US-ASCII")) { | 765 (charset != "US-ASCII")) { |
380 // The C++ portion of the embedder assumes UTF-8. | 766 // The C++ portion of the embedder assumes UTF-8. |
381 throw "Only utf-8 or US-ASCII encodings are supported: $charset given."; | 767 throw "Only utf-8 or US-ASCII encodings are supported: $charset given."; |
382 } | 768 } |
383 _parsePackagesFile(sp, traceLoading, resource, data.contentAsBytes()); | 769 _parsePackagesFile(sp, traceLoading, resource, data.contentAsBytes()); |
384 } catch (e) { | 770 } catch (e) { |
385 sp.send("Uncaught error ($e) loading packages data."); | 771 sp.send("Uncaught error ($e) loading packages data."); |
386 } | 772 } |
387 } | 773 } |
388 | 774 |
389 | 775 // This code used to exist in a second isolate and so it uses a SendPort to |
| 776 // report it's return value. This could be refactored so that it returns it's |
| 777 // value and the caller could wait on the future rather than a message on |
| 778 // SendPort. |
390 _handlePackagesRequest(SendPort sp, | 779 _handlePackagesRequest(SendPort sp, |
391 bool traceLoading, | 780 bool traceLoading, |
392 int id, | 781 int tag, |
393 Uri resource) async { | 782 Uri resource) async { |
394 try { | 783 try { |
395 if (id == -1) { | 784 if (tag == -1) { |
396 if (resource.scheme == 'file') { | 785 if (resource.scheme == 'file') { |
397 _findPackagesFile(sp, traceLoading, resource); | 786 _findPackagesFile(sp, traceLoading, resource); |
398 } else if ((resource.scheme == 'http') || (resource.scheme == 'https')) { | 787 } else if ((resource.scheme == 'http') || (resource.scheme == 'https')) { |
399 // Try to load the .packages file next to the resource. | 788 // Try to load the .packages file next to the resource. |
400 var packagesUri = resource.resolve(".packages"); | 789 var packagesUri = resource.resolve(".packages"); |
401 var exists = await _loadHttpPackagesFile(sp, traceLoading, packagesUri); | 790 var exists = await _loadHttpPackagesFile(sp, traceLoading, packagesUri); |
402 if (!exists) { | 791 if (!exists) { |
403 // If the loading of the .packages file failed for http/https based | 792 // If the loading of the .packages file failed for http/https based |
404 // scripts then setup the package root. | 793 // scripts then setup the package root. |
405 var packageRoot = resource.resolve('packages/'); | 794 var packageRoot = resource.resolve('packages/'); |
406 sp.send([packageRoot.toString()]); | 795 sp.send([packageRoot.toString()]); |
407 } | 796 } |
408 } else { | 797 } else { |
409 sp.send("Unsupported scheme used to locate .packages file: " | 798 sp.send("Unsupported scheme used to locate .packages file: " |
410 "'$resource'."); | 799 "'$resource'."); |
411 } | 800 } |
412 } else if (id == -2) { | 801 } else if (tag == -2) { |
413 if (traceLoading) { | 802 if (traceLoading) { |
414 _log("Handling load of packages map: '$resource'."); | 803 _log("Handling load of packages map: '$resource'."); |
415 } | 804 } |
416 if (resource.scheme == 'file') { | 805 if (resource.scheme == 'file') { |
417 var exists = await new File.fromUri(resource).exists(); | 806 var exists = await new File.fromUri(resource).exists(); |
418 if (exists) { | 807 if (exists) { |
419 _loadPackagesFile(sp, traceLoading, resource); | 808 _loadPackagesFile(sp, traceLoading, resource); |
420 } else { | 809 } else { |
421 sp.send("Packages file '$resource' not found."); | 810 sp.send("Packages file '$resource' not found."); |
422 } | 811 } |
423 } else if ((resource.scheme == 'http') || (resource.scheme == 'https')) { | 812 } else if ((resource.scheme == 'http') || (resource.scheme == 'https')) { |
424 var exists = await _loadHttpPackagesFile(sp, traceLoading, resource); | 813 var exists = await _loadHttpPackagesFile(sp, traceLoading, resource); |
425 if (!exists) { | 814 if (!exists) { |
426 sp.send("Packages file '$resource' not found."); | 815 sp.send("Packages file '$resource' not found."); |
427 } | 816 } |
428 } else if (resource.scheme == 'data') { | 817 } else if (resource.scheme == 'data') { |
429 _loadPackagesData(sp, traceLoading, resource); | 818 _loadPackagesData(sp, traceLoading, resource); |
430 } else { | 819 } else { |
431 sp.send("Unknown scheme (${resource.scheme}) for package file at " | 820 sp.send("Unknown scheme (${resource.scheme}) for package file at " |
432 "'$resource'."); | 821 "'$resource'."); |
433 } | 822 } |
434 } else { | 823 } else { |
435 sp.send("Unknown packages request id: $id for '$resource'."); | 824 sp.send("Unknown packages request tag: $tag for '$resource'."); |
436 } | 825 } |
437 } catch (e, s) { | 826 } catch (e, s) { |
438 if (traceLoading) { | 827 if (traceLoading) { |
439 _log("Error handling packages request: $e\n$s"); | 828 _log("Error handling packages request: $e\n$s"); |
440 } | 829 } |
441 sp.send("Uncaught error ($e) handling packages request."); | 830 sp.send("Uncaught error ($e) handling packages request."); |
442 } | 831 } |
443 } | 832 } |
444 | 833 |
| 834 // Shutdown all active loaders by sending an error message. |
| 835 void shutdownLoaders() { |
| 836 String message = 'Service shutdown'; |
| 837 isolateEmbedderData.values.toList().forEach((IsolateLoaderState ils) { |
| 838 assert(ils.sp != null); |
| 839 _sendResourceResponse(ils.sp, 1, null, null, message); |
| 840 }); |
| 841 } |
| 842 |
| 843 // See Dart_LibraryTag in dart_api.h |
| 844 const _Dart_kCanonicalizeUrl = 0; // Canonicalize the URL. |
| 845 const _Dart_kScriptTag = 1; // Load the root script. |
| 846 const _Dart_kSourceTag = 2; // Load a part source. |
| 847 const _Dart_kImportTag = 3; // Import a library. |
| 848 |
| 849 // Extra requests. Keep these in sync between loader.dart and builtin.dart. |
| 850 const _Dart_kInitLoader = 4; // Initialize the loader. |
| 851 const _Dart_kResourceLoad = 5; // Resource class support. |
| 852 const _Dart_kGetPackageRootUri = 6; // Uri of the packages/ directory. |
| 853 const _Dart_kGetPackageConfigUri = 7; // Uri of the .packages file. |
| 854 const _Dart_kResolvePackageUri = 8; // Resolve a package: uri. |
445 | 855 |
446 // External entry point for loader requests. | 856 // External entry point for loader requests. |
447 _processLoadRequest(request) { | 857 _processLoadRequest(request) { |
448 SendPort sp = request[0]; | 858 assert(request is List); |
449 assert(sp != null); | 859 assert(request.length > 4); |
450 bool traceLoading = request[1]; | 860 |
451 assert(traceLoading != null); | 861 // Should we trace loading? |
452 int id = request[2]; | 862 bool traceLoading = request[0]; |
453 assert(id != null); | 863 |
454 String resource = request[3]; | 864 // This is the sending isolate's Dart_GetMainPortId(). |
455 assert(resource != null); | 865 int isolateId = request[1]; |
456 var uri = Uri.parse(resource); | 866 |
457 if (id >= 0) { | 867 // The tag describing the operation. |
458 _handleResourceRequest(sp, traceLoading, id, uri); | 868 int tag = request[2]; |
459 } else { | 869 |
460 _handlePackagesRequest(sp, traceLoading, id, uri); | 870 // The send port to send the response on. |
| 871 SendPort sp = request[3]; |
| 872 |
| 873 // Grab the loader state for the requesting isolate. |
| 874 IsolateLoaderState loaderState = isolateEmbedderData[isolateId]; |
| 875 |
| 876 // We are either about to initialize the loader, or, we already have. |
| 877 assert((tag == _Dart_kInitLoader) || (loaderState != null)); |
| 878 |
| 879 // Handle the request specified in the tag. |
| 880 switch (tag) { |
| 881 case _Dart_kScriptTag: { |
| 882 Uri uri = Uri.parse(request[4]); |
| 883 // Remember the root script. |
| 884 loaderState._rootScript = uri; |
| 885 _handleResourceRequest(loaderState, |
| 886 sp, |
| 887 traceLoading, |
| 888 tag, |
| 889 uri, |
| 890 uri, |
| 891 null); |
| 892 } |
| 893 break; |
| 894 case _Dart_kSourceTag: |
| 895 case _Dart_kImportTag: { |
| 896 // The url of the file being loaded. |
| 897 var uri = Uri.parse(request[4]); |
| 898 // The library that is importing/parting the file. |
| 899 String libraryUrl = request[5]; |
| 900 _handleResourceRequest(loaderState, |
| 901 sp, |
| 902 traceLoading, |
| 903 tag, |
| 904 uri, |
| 905 uri, |
| 906 libraryUrl); |
| 907 } |
| 908 break; |
| 909 case _Dart_kInitLoader: { |
| 910 String packageRoot = request[4]; |
| 911 String packagesFile = request[5]; |
| 912 String workingDirectory = request[6]; |
| 913 if (loaderState == null) { |
| 914 loaderState = new IsolateLoaderState(isolateId); |
| 915 loaderState.sp = sp; |
| 916 isolateEmbedderData[isolateId] = loaderState; |
| 917 } |
| 918 loaderState.init(packageRoot, packagesFile, workingDirectory); |
| 919 assert(isolateEmbedderData[isolateId] == loaderState); |
| 920 } |
| 921 break; |
| 922 case _Dart_kResourceLoad: { |
| 923 Uri uri = Uri.parse(request[4]); |
| 924 _handleResourceRequest(loaderState, |
| 925 sp, |
| 926 traceLoading, |
| 927 tag, |
| 928 uri, |
| 929 uri, |
| 930 null); |
| 931 } |
| 932 break; |
| 933 case _Dart_kGetPackageRootUri: |
| 934 loaderState._triggerPackageResolution(() { |
| 935 // Respond with the package root (if any) after package resolution. |
| 936 sp.send(loaderState._packageRoot); |
| 937 }); |
| 938 break; |
| 939 case _Dart_kGetPackageConfigUri: |
| 940 loaderState._triggerPackageResolution(() { |
| 941 // Respond with the packages config (if any) after package resolution. |
| 942 sp.send(loaderState._packageConfig); |
| 943 }); |
| 944 break; |
| 945 case _Dart_kResolvePackageUri: |
| 946 Uri uri = Uri.parse(request[4]); |
| 947 loaderState._triggerPackageResolution(() { |
| 948 // Respond with the resolved package uri after package resolution. |
| 949 Uri resolvedUri; |
| 950 try { |
| 951 resolvedUri = loaderState._resolvePackageUri(uri); |
| 952 } catch (e, s) { |
| 953 if (traceLoading) { |
| 954 _log("Exception ($e) when resolving package URI: $uri"); |
| 955 } |
| 956 resolvedUri = null; |
| 957 } |
| 958 sp.send(resolvedUri); |
| 959 }); |
| 960 break; |
| 961 default: |
| 962 _log('Unknown loader request tag=$tag from $isolateId'); |
461 } | 963 } |
462 } | 964 } |
OLD | NEW |