Chromium Code Reviews| Index: runtime/bin/builtin.dart |
| =================================================================== |
| --- runtime/bin/builtin.dart (revision 36309) |
| +++ runtime/bin/builtin.dart (working copy) |
| @@ -5,6 +5,7 @@ |
| library builtin; |
| import 'dart:io'; |
| import 'dart:async'; |
| +import 'dart:convert'; |
| // import 'root_library'; happens here from C Code |
| // The root library (aka the script) is imported into this library. The |
| @@ -107,6 +108,47 @@ |
| } |
| +void _httpGet(Uri uri, loadCallback(List<int> data)) { |
| + var httpClient = new HttpClient(); |
| + try { |
| + httpClient.getUrl(uri) |
| + .then((HttpClientRequest request) { |
| + request.persistentConnection = false; |
| + return request.close(); |
| + }) |
| + .then((HttpClientResponse response) { |
| + // Only create a ByteBuilder if multiple chunks are received. |
| + var builder = new BytesBuilder(copy: false); |
| + response.listen( |
| + builder.add, |
| + onDone: () { |
| + if (response.statusCode != 200) { |
| + var msg = 'Failure getting $uri: ' |
| + '${response.statusCode} ${response.reasonPhrase}'; |
| + _asyncLoadError(uri.toString(), msg); |
| + } |
| + |
| + List<int> data = builder.takeBytes(); |
| + httpClient.close(); |
| + loadCallback(data); |
| + }, |
| + onError: (error) { |
| + _asyncLoadError(uri.toString(), error); |
| + }); |
| + }) |
| + .catchError((error) { |
| + _asyncLoadError(uri.toString(), error); |
| + }); |
| + } catch (error) { |
| + _asyncLoadError(uri.toString(), error); |
| + } |
| + // TODO(floitsch): remove this line. It's just here to push an event on the |
| + // event loop so that we invoke the scheduled microtasks. Also remove the |
| + // import of dart:async when this line is not needed anymore. |
| + Timer.run(() {}); |
| +} |
| + |
| + |
| // Are we running on Windows? |
| var _isWindows = false; |
| var _workingWindowsDrivePrefix; |
| @@ -266,6 +308,59 @@ |
| } |
| +void _loadScript(String uri, List<int> data) native "Builtin_LoadScript"; |
| + |
| +void _asyncLoadError(uri, error) native "Builtin_AsyncLoadError"; |
| + |
| + |
| +// Asynchronously loads script data (source or snapshot) through |
| +// an http or file uri. |
| +_loadDataAsync(String uri) { |
| + uri = _resolveScriptUri(uri); |
| + Uri sourceUri = Uri.parse(uri); |
| + if (sourceUri.scheme == 'http') { |
| + _httpGet(sourceUri, (data) { |
| + _loadScript(uri, data); |
| + }); |
| + } else { |
| + _loadDataFromFileAsync(uri);; |
| + } |
| +} |
| + |
| +_loadDataFromFileAsync(String uri) { |
| + var sourceFile = new File(_filePathFromUri(uri)); |
| + sourceFile.readAsBytes().then((data) { |
| + _loadScript(uri, data);//native |
|
Ivan Posva
2014/05/21 22:02:07
The comment here is unnecessary.
hausner
2014/05/21 23:53:08
Left-overs. Removed.
|
| + }, |
| + onError: (e) { |
| + _asyncLoadError(uri, e); |
| + }); |
| +} |
| + |
| + |
| +void _loadLibrarySource(tag, uri, libraryUri, text) |
|
Ivan Posva
2014/05/21 22:02:07
I am wondering why we do not unify the loading of
hausner
2014/05/21 23:53:08
This can be done, but on the C++ side we save noth
|
| + native "Builtin_LoadLibrarySource"; |
| + |
| +_loadSourceAsync(int tag, String uri, String libraryUri) { |
| + var filePath = _filePathFromUri(uri); |
| + Uri sourceUri = Uri.parse(filePath); |
| + if (sourceUri.scheme == 'http') { |
| + _httpGet(sourceUri, (data) { |
| + var text = UTF8.decode(data); |
| + _loadLibrarySource(tag, uri, libraryUri, text); |
| + }); |
| + } else { |
| + var sourceFile = new File(filePath); |
| + sourceFile.readAsString().then((text) { |
| + _loadLibrarySource(tag, uri, libraryUri, text);//native |
|
Ivan Posva
2014/05/21 22:02:07
ditto.
hausner
2014/05/21 23:53:08
Done.
|
| + }, |
| + onError: (e) { |
| + _asyncLoadError(uri, e); |
| + }); |
| + } |
| +} |
| + |
| + |
| // Returns the directory part, the filename part, and the name |
| // of a native extension URL as a list [directory, filename, name]. |
| // The directory part is either a file system path or an HTTP(S) URL. |