PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/components/cronet/android/api/src/org/chromium/net/RequestFinishedInfo.java

https://github.com/chromium/chromium
Java | 320 lines | 68 code | 32 blank | 220 comment | 2 complexity | 644d6d5652efb7b9ead5b95df982fa99 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. package org.chromium.net;
  5. import androidx.annotation.Nullable;
  6. import java.util.Collection;
  7. import java.util.Date;
  8. import java.util.concurrent.Executor;
  9. /**
  10. * Information about a finished request. Passed to {@link RequestFinishedInfo.Listener}.
  11. *
  12. * To associate the data with the original request, use
  13. * {@link ExperimentalUrlRequest.Builder#addRequestAnnotation} or
  14. * {@link ExperimentalBidirectionalStream.Builder#addRequestAnnotation} to add a unique identifier
  15. * when creating the request, and call {@link #getAnnotations} when the {@link RequestFinishedInfo}
  16. * is received to retrieve the identifier.
  17. *
  18. * {@hide} as it's a prototype.
  19. */
  20. public abstract class RequestFinishedInfo {
  21. /**
  22. * Listens for finished requests for the purpose of collecting metrics.
  23. *
  24. * {@hide} as it's a prototype.
  25. */
  26. public abstract static class Listener {
  27. private final Executor mExecutor;
  28. public Listener(Executor executor) {
  29. if (executor == null) {
  30. throw new IllegalStateException("Executor must not be null");
  31. }
  32. mExecutor = executor;
  33. }
  34. /**
  35. * Invoked with request info. Will be called in a task submitted to the
  36. * {@link java.util.concurrent.Executor} returned by {@link #getExecutor}.
  37. * @param requestInfo {@link RequestFinishedInfo} for finished request.
  38. */
  39. public abstract void onRequestFinished(RequestFinishedInfo requestInfo);
  40. /**
  41. * Returns this listener's executor. Can be called on any thread.
  42. * @return this listener's {@link java.util.concurrent.Executor}
  43. */
  44. public Executor getExecutor() {
  45. return mExecutor;
  46. }
  47. }
  48. /**
  49. * Metrics collected for a single request. Most of these metrics are timestamps for events
  50. * during the lifetime of the request, which can be used to build a detailed timeline for
  51. * investigating performance.
  52. *
  53. * Events happen in this order:
  54. * <ol>
  55. * <li>{@link #getRequestStart request start}</li>
  56. * <li>{@link #getDnsStart DNS start}</li>
  57. * <li>{@link #getDnsEnd DNS end}</li>
  58. * <li>{@link #getConnectStart connect start}</li>
  59. * <li>{@link #getSslStart SSL start}</li>
  60. * <li>{@link #getSslEnd SSL end}</li>
  61. * <li>{@link #getConnectEnd connect end}</li>
  62. * <li>{@link #getSendingStart sending start}</li>
  63. * <li>{@link #getSendingEnd sending end}</li>
  64. * <li>{@link #getResponseStart response start}</li>
  65. * <li>{@link #getRequestEnd request end}</li>
  66. * </ol>
  67. *
  68. * Start times are reported as the time when a request started blocking on event, not when the
  69. * event actually occurred, with the exception of push start and end. If a metric is not
  70. * meaningful or not available, including cases when a request finished before reaching that
  71. * stage, start and end times will be {@code null}. If no time was spent blocking on an event,
  72. * start and end will be the same time.
  73. *
  74. * If the system clock is adjusted during the request, some of the {@link java.util.Date} values
  75. * might not match it. Timestamps are recorded using a clock that is guaranteed not to run
  76. * backwards. All timestamps are correct relative to the system clock at the time of request
  77. * start, and taking the difference between two timestamps will give the correct difference
  78. * between the events. In order to preserve this property, timestamps for events other than
  79. * request start are not guaranteed to match the system clock at the times they represent.
  80. *
  81. * Most timing metrics are taken from
  82. * <a
  83. * href="https://cs.chromium.org/chromium/src/net/base/load_timing_info.h">LoadTimingInfo</a>,
  84. * which holds the information for <a href="http://w3c.github.io/navigation-timing/"></a> and
  85. * <a href="https://www.w3.org/TR/resource-timing/"></a>.
  86. *
  87. * {@hide} as it's a prototype.
  88. */
  89. public abstract static class Metrics {
  90. /**
  91. * Returns time when the request started.
  92. * @return {@link java.util.Date} representing when the native request actually started.
  93. * This timestamp will match the system clock at the time it represents.
  94. */
  95. @Nullable
  96. public abstract Date getRequestStart();
  97. /**
  98. * Returns time when DNS lookup started. This and {@link #getDnsEnd} will return non-null
  99. * values regardless of whether the result came from a DNS server or the local cache.
  100. * @return {@link java.util.Date} representing when DNS lookup started. {@code null} if the
  101. * socket was reused (see {@link #getSocketReused}).
  102. */
  103. @Nullable
  104. public abstract Date getDnsStart();
  105. /**
  106. * Returns time when DNS lookup finished. This and {@link #getDnsStart} will return non-null
  107. * values regardless of whether the result came from a DNS server or the local cache.
  108. * @return {@link java.util.Date} representing when DNS lookup finished. {@code null} if the
  109. * socket was reused (see {@link #getSocketReused}).
  110. */
  111. @Nullable
  112. public abstract Date getDnsEnd();
  113. /**
  114. * Returns time when connection establishment started.
  115. * @return {@link java.util.Date} representing when connection establishment started,
  116. * typically when DNS resolution finishes. {@code null} if the socket was reused (see
  117. * {@link #getSocketReused}).
  118. */
  119. @Nullable
  120. public abstract Date getConnectStart();
  121. /**
  122. * Returns time when connection establishment finished.
  123. * @return {@link java.util.Date} representing when connection establishment finished,
  124. * after TCP connection is established and, if using HTTPS, SSL handshake is completed.
  125. * For QUIC 0-RTT, this represents the time of handshake confirmation and might happen
  126. * later than {@link #getSendingStart}.
  127. * {@code null} if the socket was reused (see {@link #getSocketReused}).
  128. */
  129. @Nullable
  130. public abstract Date getConnectEnd();
  131. /**
  132. * Returns time when SSL handshake started. For QUIC, this will be the same time as
  133. * {@link #getConnectStart}.
  134. * @return {@link java.util.Date} representing when SSL handshake started. {@code null} if
  135. * SSL is not used or if the socket was reused (see {@link #getSocketReused}).
  136. */
  137. @Nullable
  138. public abstract Date getSslStart();
  139. /**
  140. * Returns time when SSL handshake finished. For QUIC, this will be the same time as
  141. * {@link #getConnectEnd}.
  142. * @return {@link java.util.Date} representing when SSL handshake finished. {@code null} if
  143. * SSL is not used or if the socket was reused (see {@link #getSocketReused}).
  144. */
  145. @Nullable
  146. public abstract Date getSslEnd();
  147. /**
  148. * Returns time when sending the request started.
  149. * @return {@link java.util.Date} representing when sending HTTP request headers started.
  150. */
  151. @Nullable
  152. public abstract Date getSendingStart();
  153. /**
  154. * Returns time when sending the request finished.
  155. * @return {@link java.util.Date} representing when sending HTTP request body finished.
  156. * (Sending request body happens after sending request headers.)
  157. */
  158. @Nullable
  159. public abstract Date getSendingEnd();
  160. /**
  161. * Returns time when first byte of HTTP/2 server push was received.
  162. * @return {@link java.util.Date} representing when the first byte of an HTTP/2 server push
  163. * was received. {@code null} if server push is not used.
  164. */
  165. @Nullable
  166. public abstract Date getPushStart();
  167. /**
  168. * Returns time when last byte of HTTP/2 server push was received.
  169. * @return {@link java.util.Date} representing when the last byte of an HTTP/2 server push
  170. * was received. {@code null} if server push is not used.
  171. */
  172. @Nullable
  173. public abstract Date getPushEnd();
  174. /**
  175. * Returns time when the end of the response headers was received.
  176. * @return {@link java.util.Date} representing when the end of the response headers was
  177. * received.
  178. */
  179. @Nullable
  180. public abstract Date getResponseStart();
  181. /**
  182. * Returns time when the request finished.
  183. * @return {@link java.util.Date} representing when the request finished.
  184. */
  185. @Nullable
  186. public abstract Date getRequestEnd();
  187. /**
  188. * Returns whether the socket was reused from a previous request. In HTTP/2 or QUIC, if
  189. * streams are multiplexed in a single connection, returns {@code true} for all streams
  190. * after the first.
  191. * @return whether this request reused a socket from a previous request. When {@code true},
  192. * DNS, connection, and SSL times will be {@code null}.
  193. */
  194. public abstract boolean getSocketReused();
  195. /**
  196. * Returns milliseconds between request initiation and first byte of response headers,
  197. * or {@code null} if not collected.
  198. * TODO(mgersh): Remove once new API works http://crbug.com/629194
  199. * {@hide}
  200. */
  201. @Nullable
  202. public abstract Long getTtfbMs();
  203. /**
  204. * Returns milliseconds between request initiation and finish,
  205. * including a failure or cancellation, or {@code null} if not collected.
  206. * TODO(mgersh): Remove once new API works http://crbug.com/629194
  207. * {@hide}
  208. */
  209. @Nullable
  210. public abstract Long getTotalTimeMs();
  211. /**
  212. * Returns total bytes sent over the network transport layer, or {@code null} if not
  213. * collected.
  214. */
  215. @Nullable
  216. public abstract Long getSentByteCount();
  217. /**
  218. * Returns total bytes received over the network transport layer, or {@code null} if not
  219. * collected. Number of bytes does not include any previous redirects.
  220. */
  221. @Nullable
  222. public abstract Long getReceivedByteCount();
  223. }
  224. /**
  225. * Reason value indicating that the request succeeded. Returned from {@link #getFinishedReason}.
  226. */
  227. public static final int SUCCEEDED = 0;
  228. /**
  229. * Reason value indicating that the request failed or returned an error. Returned from
  230. * {@link #getFinishedReason}.
  231. */
  232. public static final int FAILED = 1;
  233. /**
  234. * Reason value indicating that the request was canceled. Returned from
  235. * {@link #getFinishedReason}.
  236. */
  237. public static final int CANCELED = 2;
  238. /**
  239. * Returns the request's original URL.
  240. *
  241. * @return the request's original URL
  242. */
  243. public abstract String getUrl();
  244. /**
  245. * Returns the objects that the caller has supplied when initiating the request, using
  246. * {@link ExperimentalUrlRequest.Builder#addRequestAnnotation} or
  247. * {@link ExperimentalBidirectionalStream.Builder#addRequestAnnotation}.
  248. * Annotations can be used to associate a {@link RequestFinishedInfo} with the original request
  249. * or type of request.
  250. *
  251. * @return annotations supplied when creating the request
  252. */
  253. public abstract Collection<Object> getAnnotations();
  254. // TODO(klm): Collect and return a chain of Metrics objects for redirect responses.
  255. // TODO(mgersh): Update this javadoc when new metrics are fully implemented
  256. /**
  257. * Returns metrics collected for this request.
  258. *
  259. * <p>The reported times and bytes account for all redirects, i.e.
  260. * the TTFB is from the start of the original request to the ultimate response headers,
  261. * the TTLB is from the start of the original request to the end of the ultimate response,
  262. * the received byte count is for all redirects and the ultimate response combined.
  263. * These cumulative metric definitions are debatable, but are chosen to make sense
  264. * for user-facing latency analysis.
  265. *
  266. * @return metrics collected for this request.
  267. */
  268. public abstract Metrics getMetrics();
  269. /**
  270. * Returns the reason why the request finished.
  271. * @return one of {@link #SUCCEEDED}, {@link #FAILED}, or {@link #CANCELED}
  272. */
  273. public abstract int getFinishedReason();
  274. /**
  275. * Returns a {@link UrlResponseInfo} for the request, if its response had started.
  276. * @return {@link UrlResponseInfo} for the request, if its response had started.
  277. */
  278. @Nullable
  279. public abstract UrlResponseInfo getResponseInfo();
  280. /**
  281. * If the request failed, returns the same {@link CronetException} provided to
  282. * {@link UrlRequest.Callback#onFailed}.
  283. *
  284. * @return the request's {@link CronetException}, if the request failed
  285. */
  286. @Nullable
  287. public abstract CronetException getException();
  288. }