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

Unified Diff: sdk/lib/html/dartium/html_dartium.dart

Issue 2042033002: Add zone task support to http-requests. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 6 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:
Download patch
Index: sdk/lib/html/dartium/html_dartium.dart
diff --git a/sdk/lib/html/dartium/html_dartium.dart b/sdk/lib/html/dartium/html_dartium.dart
index b3300761e306a1a597d3485c127108009570c54c..0bc2baaddc74e5555c03ddfa6e79650b32fde4aa 100644
--- a/sdk/lib/html/dartium/html_dartium.dart
+++ b/sdk/lib/html/dartium/html_dartium.dart
@@ -20997,6 +20997,67 @@ class HtmlOptionsCollection extends HtmlCollection {
// BSD-style license that can be found in the LICENSE file.
+/**
+ * A task specification for http requests.
+ *
+ * This specification is not available when an http request is sent through
+ * direct use of [HttpRequest.send]. See [HttpRequestSendTaskSpecification].
+ *
+ * A task created by this specification is a `Future<HttpRequest>`.
+ */
+class HttpRequestTaskSpecification extends TaskSpecification {
+ final String url;
+ final String method;
+ final bool withCredentials;
+ final String responseType;
+ final String mimeType;
+ final Map<String, String> requestHeaders;
+ final dynamic sendData;
+ /**
+ * The function that is invoked on progress updates. This function is *not*
+ * executed as part of the http-request task, but as an event callback.
+ *
+ * Creating an http-request automatically registers the on-progress listener.
+ */
+ final ZoneUnaryCallback<dynamic, ProgressEvent> onProgress;
+
+ HttpRequestTaskSpecification(this.url,
+ {String this.method, bool this.withCredentials, String this.responseType,
+ String this.mimeType, Map<String, String> this.requestHeaders,
+ this.sendData,
+ void this.onProgress(ProgressEvent e)});
+
+ String get name => "dart.html.http-request";
+ bool get isOneShot => true;
+}
+
+/**
+ * A task specification for http requests that are initiated through a direct
+ * invocation of [HttpRequest.send].
+ *
+ * This specification serves as signal to zones that an http request has been
+ * initiated. The created task is the [request] object itself, and
+ * no callback is ever executed in this task. When the http request gets
+ * data event-listeners that have the request object as event target are
+ * executed.
+ *
+ * Http requests that are initiated through `request` methods don't use
+ * this class but use [HttpRequestTaskSpecification].
+ */
+class HttpRequestSendTaskSpecification extends TaskSpecification {
+ final HttpRequest request;
+ final dynamic sendData;
+
+ HttpRequestSendTaskSpecification(this.request, this.sendData);
+
+ String get name => "dart.html.http-request-send";
+
+ /**
+ * No callback is ever executed in an http-request send task.
+ */
+ bool get isOneShot => false;
+}
+
/**
* A client-side XHR request for getting data from a URL,
* formally known as XMLHttpRequest.
@@ -21184,7 +21245,34 @@ class HttpRequest extends HttpRequestEventTarget {
{String method, bool withCredentials, String responseType,
String mimeType, Map<String, String> requestHeaders, sendData,
void onProgress(ProgressEvent e)}) {
+ var spec = new HttpRequestTaskSpecification(
+ url, method: method,
+ withCredentials: withCredentials,
+ responseType: responseType,
+ mimeType: mimeType,
+ requestHeaders: requestHeaders,
+ sendData: sendData,
+ onProgress: onProgress);
+
+ if (identical(Zone.current, Zone.ROOT)) {
+ return _createHttpRequestTask(spec, null);
+ }
+ return Zone.current.createTask(_createHttpRequestTask, spec);
+ }
+
+ static Future<HttpRequest> _createHttpRequestTask(
+ HttpRequestTaskSpecification spec, Zone zone) {
+ String url = spec.url;
+ String method = spec.method;
+ bool withCredentials = spec.withCredentials;
+ String responseType = spec.responseType;
+ String mimeType = spec.mimeType;
+ Map<String, String> requestHeaders = spec.requestHeaders;
+ var sendData = spec.sendData;
+ var onProgress = spec.onProgress;
+
var completer = new Completer<HttpRequest>();
+ var task = completer.future;
var xhr = new HttpRequest();
if (method == null) {
@@ -21224,23 +21312,42 @@ class HttpRequest extends HttpRequestEventTarget {
// redirect case will be handled by the browser before it gets to us,
// so if we see it we should pass it through to the user.
var unknownRedirect = xhr.status > 307 && xhr.status < 400;
-
- if (accepted || fileUri || notModified || unknownRedirect) {
+
+ var isSuccessful = accepted || fileUri || notModified || unknownRedirect;
+
+ if (zone == null && isSuccessful) {
completer.complete(xhr);
- } else {
+ } else if (zone == null) {
completer.completeError(e);
+ } else if (isSuccessful) {
+ zone.runTask((task, value) {
+ completer.complete(value);
+ }, task, xhr);
+ } else {
+ zone.runTask((task, error) {
+ completer.completeError(error);
+ }, task, e);
}
});
- xhr.onError.listen(completer.completeError);
+ if (zone == null) {
+ xhr.onError.listen(completer.completeError);
+ } else {
+ xhr.onError.listen((error) {
+ zone.runTask((task, error) {
+ completer.completeError(error);
+ }, task, error);
+ });
+ }
if (sendData != null) {
- xhr.send(sendData);
+ // TODO(floitsch): should we go through 'send()' and have nested tasks?
+ xhr._send(sendData);
} else {
- xhr.send();
+ xhr._send();
}
- return completer.future;
+ return task;
}
/**
@@ -21290,6 +21397,9 @@ class HttpRequest extends HttpRequestEventTarget {
return xhr.responseText;
});
}
+ // TODO(floitsch): the following code doesn't go through task zones.
+ // Since 'XDomainRequest' is an IE9 feature we should probably just remove
+ // it.
}
/**
@@ -21340,7 +21450,7 @@ class HttpRequest extends HttpRequestEventTarget {
*
* Note: Most simple HTTP requests can be accomplished using the [getString],
* [request], [requestCrossOrigin], or [postFormData] methods. Use of this
- * `open` method is intended only for more complext HTTP requests where
+ * `open` method is intended only for more complex HTTP requests where
* finer-grained control is needed.
*/
@DomName('XMLHttpRequest.open')
@@ -21353,6 +21463,36 @@ class HttpRequest extends HttpRequestEventTarget {
}
}
+ /**
+ * Sends the request with any given `data`.
+ *
+ * Note: Most simple HTTP requests can be accomplished using the [getString],
+ * [request], [requestCrossOrigin], or [postFormData] methods. Use of this
+ * `send` method is intended only for more complex HTTP requests where
+ * finer-grained control is needed.
+ *
+ * ## Other resources
+ *
+ * * [XMLHttpRequest.send](https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#send%28%29)
+ * from MDN.
+ */
+ @DomName('XMLHttpRequest.send')
+ @DocsEditable()
+ void send([body_OR_data]) {
+ if (identical(Zone.current, Zone.ROOT)) {
+ _send(body_OR_data);
+ } else {
+ Zone.current.createTask(_createHttpRequestSendTask,
+ new HttpRequestSendTaskSpecification(this, body_OR_data));
+ }
+ }
+
+ static HttpRequest _createHttpRequestSendTask(
+ HttpRequestSendTaskSpecification spec, Zone zone) {
+ spec.request._send(spec.sendData);
+ return spec.request;
+ }
+
// To suppress missing implicit constructor warnings.
factory HttpRequest._() { throw new UnsupportedError("Not supported"); }
@@ -21656,7 +21796,7 @@ class HttpRequest extends HttpRequestEventTarget {
@SupportedBrowser(SupportedBrowser.SAFARI)
void overrideMimeType(String mime) => _blink.BlinkXMLHttpRequest.instance.overrideMimeType_Callback_1_(this, mime);
- void send([body_OR_data]) {
+ void _send([body_OR_data]) {
if (body_OR_data != null) {
_blink.BlinkXMLHttpRequest.instance.send_Callback_1_(this, body_OR_data);
return;

Powered by Google App Engine
This is Rietveld 408576698