| OLD | NEW |
| (Empty) | |
| 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 |
| 5 package org.chromium.net; |
| 6 |
| 7 import android.support.annotation.Nullable; |
| 8 |
| 9 import java.util.Collection; |
| 10 import java.util.concurrent.Executor; |
| 11 |
| 12 /** |
| 13 * Information about a finished request. Passed to {@link RequestFinishedInfo.Li
stener}. |
| 14 * |
| 15 * {@hide} as it's a prototype. |
| 16 */ |
| 17 public final class RequestFinishedInfo { |
| 18 /** |
| 19 * Listens for finished requests for the purpose of collecting metrics. |
| 20 * |
| 21 * {@hide} as it's a prototype. |
| 22 */ |
| 23 public abstract static class Listener { |
| 24 private final Executor mExecutor; |
| 25 |
| 26 public Listener(Executor executor) { |
| 27 if (executor == null) { |
| 28 throw new IllegalStateException("Executor must not be null"); |
| 29 } |
| 30 mExecutor = executor; |
| 31 } |
| 32 |
| 33 /** |
| 34 * Invoked with request info. Will be called in a task submitted to the |
| 35 * {@link java.util.concurrent.Executor} returned by {@link #getExecutor
}. |
| 36 * @param requestInfo {@link RequestFinishedInfo} for finished request. |
| 37 */ |
| 38 public abstract void onRequestFinished(RequestFinishedInfo requestInfo); |
| 39 |
| 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 /** |
| 50 * Metrics collected for a single request. |
| 51 * |
| 52 * {@hide} as it's a prototype. |
| 53 */ |
| 54 public static class Metrics { |
| 55 @Nullable |
| 56 private final Long mTtfbMs; |
| 57 @Nullable |
| 58 private final Long mTotalTimeMs; |
| 59 @Nullable |
| 60 private final Long mSentBytesCount; |
| 61 @Nullable |
| 62 private final Long mReceivedBytesCount; |
| 63 |
| 64 public Metrics(@Nullable Long ttfbMs, @Nullable Long totalTimeMs, |
| 65 @Nullable Long sentBytesCount, @Nullable Long receivedBytesCount
) { |
| 66 mTtfbMs = ttfbMs; |
| 67 mTotalTimeMs = totalTimeMs; |
| 68 mSentBytesCount = sentBytesCount; |
| 69 mReceivedBytesCount = receivedBytesCount; |
| 70 } |
| 71 |
| 72 /** |
| 73 * Returns milliseconds between request initiation and first byte of res
ponse headers, |
| 74 * or null if not collected. |
| 75 */ |
| 76 @Nullable |
| 77 public Long getTtfbMs() { |
| 78 return mTtfbMs; |
| 79 } |
| 80 |
| 81 /** |
| 82 * Returns milliseconds between request initiation and finish, |
| 83 * including a failure or cancellation, or null if not collected. |
| 84 */ |
| 85 @Nullable |
| 86 public Long getTotalTimeMs() { |
| 87 return mTotalTimeMs; |
| 88 } |
| 89 |
| 90 /** |
| 91 * Returns total bytes sent over the network transport layer, or null if
not collected. |
| 92 */ |
| 93 @Nullable |
| 94 public Long getSentBytesCount() { |
| 95 return mSentBytesCount; |
| 96 } |
| 97 |
| 98 /** |
| 99 * Returns total bytes received over the network transport layer, or nul
l if not collected. |
| 100 */ |
| 101 @Nullable |
| 102 public Long getReceivedBytesCount() { |
| 103 return mReceivedBytesCount; |
| 104 } |
| 105 } |
| 106 |
| 107 private final String mUrl; |
| 108 private final Collection<Object> mAnnotations; |
| 109 private final Metrics mMetrics; |
| 110 @Nullable |
| 111 private final UrlResponseInfo mResponseInfo; |
| 112 |
| 113 /** |
| 114 * @hide only used by internal implementation. |
| 115 */ |
| 116 public RequestFinishedInfo(String url, Collection<Object> annotations, Metri
cs metrics, |
| 117 @Nullable UrlResponseInfo responseInfo) { |
| 118 mUrl = url; |
| 119 mAnnotations = annotations; |
| 120 mMetrics = metrics; |
| 121 mResponseInfo = responseInfo; |
| 122 } |
| 123 |
| 124 /** Returns the request's original URL. */ |
| 125 public String getUrl() { |
| 126 return mUrl; |
| 127 } |
| 128 |
| 129 /** Returns the objects that the caller has supplied when initiating the req
uest. */ |
| 130 public Collection<Object> getAnnotations() { |
| 131 return mAnnotations; |
| 132 } |
| 133 |
| 134 // TODO(klm): Collect and return a chain of Metrics objects for redirect res
ponses. |
| 135 /** |
| 136 * Returns metrics collected for this request. |
| 137 * |
| 138 * <p>The reported times and bytes account for all redirects, i.e. |
| 139 * the TTFB is from the start of the original request to the ultimate respon
se headers, |
| 140 * the TTLB is from the start of the original request to the end of the ulti
mate response, |
| 141 * the received byte count is for all redirects and the ultimate response co
mbined. |
| 142 * These cumulative metric definitions are debatable, but are chosen to make
sense |
| 143 * for user-facing latency analysis. |
| 144 * |
| 145 * @return metrics collected for this request. |
| 146 */ |
| 147 public Metrics getMetrics() { |
| 148 return mMetrics; |
| 149 } |
| 150 |
| 151 /** |
| 152 * Returns a {@link UrlResponseInfo} for the request, if its response had st
arted. |
| 153 * @return {@link UrlResponseInfo} for the request, if its response had star
ted. |
| 154 */ |
| 155 @Nullable |
| 156 public UrlResponseInfo getResponseInfo() { |
| 157 return mResponseInfo; |
| 158 } |
| 159 } |
| OLD | NEW |