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

Unified Diff: sdk/lib/_internal/js_runtime/lib/js_helper.dart

Issue 1308143002: dart2js: fetch http Resources (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sdk/lib/_internal/js_runtime/lib/core_patch.dart ('k') | sdk/lib/_internal/js_runtime/pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/_internal/js_runtime/lib/js_helper.dart
diff --git a/sdk/lib/_internal/js_runtime/lib/js_helper.dart b/sdk/lib/_internal/js_runtime/lib/js_helper.dart
index 6692d2723c426a4e4f984899aac1ac55fb05e77b..b76ac6c03ccfc146f5ca9bf74b2cbb272b751520 100644
--- a/sdk/lib/_internal/js_runtime/lib/js_helper.dart
+++ b/sdk/lib/_internal/js_runtime/lib/js_helper.dart
@@ -3903,6 +3903,58 @@ Future<Null> _loadHunk(String hunkName) {
return completer.future;
}
+// Performs an HTTP GET of the given URI and returns the response. The response
+// is either a String or a ByteBuffer.
+Future<dynamic> readHttp(String uri) {
+ Completer completer = new Completer();
+
+ void failure([error, StackTrace stackTrace]) {
+ completer.completeError(
+ new Exception("Loading $uri failed: $error"),
+ stackTrace);
+ }
+
+ enterJsAsync();
+ completer.future.whenComplete(leaveJsAsync);
+
+ var xhr = JS('var', 'new XMLHttpRequest()');
+ JS('void', '#.open("GET", #)', xhr, uri);
+ JS('void', '#.addEventListener("load", #, false)',
+ xhr, convertDartClosureToJS((event) {
+ int status = JS('int', '#.status', xhr);
+ if (status != 200) {
+ failure("Status code: $status");
+ return;
+ }
+ String responseType = JS('String', '#.responseType', xhr);
+ var data;
+ if (responseType.isEmpty || responseType == 'text') {
+ data = JS('String', '#.response', xhr);
+ completer.complete(data);
+ } else if (responseType == 'document' || responseType == 'json') {
+ data = JS('String', '#.responseText', xhr);
+ completer.complete(data);
+ } else if (responseType == 'arraybuffer') {
+ data = JS('var', '#.response', xhr);
+ completer.complete(data);
+ } else if (responseType == 'blob') {
+ var reader = JS('var', 'new FileReader()');
+ JS('void', '#.addEventListener("loadend", #, false)',
+ reader, convertDartClosureToJS((event) {
+ data = JS('var', '#.result', reader);
+ completer.complete(data);
+ }, 1));
+ } else {
+ failure('Result had unexpected type: $responseType');
+ }
+ }, 1));
+
+ JS('void', '#.addEventListener("error", #, false)', xhr, failure);
+ JS('void', '#.addEventListener("abort", #, false)', xhr, failure);
+ JS('void', '#.send()', xhr);
+ return completer.future;
+}
+
class MainError extends Error implements NoSuchMethodError {
final String _message;
« no previous file with comments | « sdk/lib/_internal/js_runtime/lib/core_patch.dart ('k') | sdk/lib/_internal/js_runtime/pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698