| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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.content.Context; | |
| 8 import android.util.Log; | |
| 9 | |
| 10 import java.lang.reflect.Constructor; | |
| 11 import java.util.concurrent.Executor; | |
| 12 | |
| 13 /** | |
| 14 * A context for {@link UrlRequest}'s, which uses the best HTTP stack | |
| 15 * available on the current platform. | |
| 16 */ | |
| 17 public abstract class UrlRequestContext { | |
| 18 private static final String TAG = "UrlRequestFactory"; | |
| 19 private static final String CRONET_URL_REQUEST_CONTEXT = | |
| 20 "org.chromium.net.CronetUrlRequestContext"; | |
| 21 | |
| 22 /** | |
| 23 * Creates a {@link UrlRequest} object. All callbacks will | |
| 24 * be called on {@code executor}'s thread. {@code executor} must not run | |
| 25 * tasks on the current thread to prevent blocking networking operations | |
| 26 * and causing exceptions during shutdown. Request is given medium priority, | |
| 27 * see {@link UrlRequest#REQUEST_PRIORITY_MEDIUM}. To specify other | |
| 28 * priorities see {@link #createRequest(String, UrlRequestListener, | |
| 29 * Executor, int priority)}. | |
| 30 * | |
| 31 * @param url {@link java.net.URL} for the request. | |
| 32 * @param listener callback class that gets called on different events. | |
| 33 * @param executor {@link Executor} on which all callbacks will be called. | |
| 34 * @return new request. | |
| 35 */ | |
| 36 public abstract UrlRequest createRequest(String url, | |
| 37 UrlRequestListener listener, Executor executor); | |
| 38 | |
| 39 /** | |
| 40 * Creates a {@link UrlRequest} object. All callbacks will | |
| 41 * be called on {@code executor}'s thread. {@code executor} must not run | |
| 42 * tasks on the current thread to prevent blocking networking operations | |
| 43 * and causing exceptions during shutdown. | |
| 44 * | |
| 45 * @param url {@link java.net.URL} for the request. | |
| 46 * @param listener callback class that gets called on different events. | |
| 47 * @param executor {@link Executor} on which all callbacks will be called. | |
| 48 * @param priority priority of the request which should be one of the | |
| 49 * {@link UrlRequest#REQUEST_PRIORITY_IDLE REQUEST_PRIORITY_*} | |
| 50 * values. | |
| 51 * @return new request. | |
| 52 */ | |
| 53 public abstract UrlRequest createRequest(String url, | |
| 54 UrlRequestListener listener, Executor executor, int priority); | |
| 55 | |
| 56 /** | |
| 57 * @return {@code true} if the context is enabled. | |
| 58 */ | |
| 59 abstract boolean isEnabled(); | |
| 60 | |
| 61 /** | |
| 62 * @return a human-readable version string of the context. | |
| 63 */ | |
| 64 public abstract String getVersionString(); | |
| 65 | |
| 66 /** | |
| 67 * Shuts down the {@link UrlRequestContext} if there are no active requests, | |
| 68 * otherwise throws an exception. | |
| 69 * | |
| 70 * Cannot be called on network thread - the thread Cronet calls into | |
| 71 * Executor on (which is different from the thread the Executor invokes | |
| 72 * callbacks on). May block until all the {@code UrlRequestContext}'s | |
| 73 * resources have been cleaned up. | |
| 74 */ | |
| 75 public abstract void shutdown(); | |
| 76 | |
| 77 /** | |
| 78 * Starts NetLog logging to a file. The NetLog is useful for debugging. | |
| 79 * The file can be viewed using a Chrome browser navigated to | |
| 80 * chrome://net-internals/#import | |
| 81 * @param fileName the complete file path. It must not be empty. If the file | |
| 82 * exists, it is truncated before starting. If actively logging, | |
| 83 * this method is ignored. | |
| 84 * @param logAll {@code true} to include basic events, user cookies, | |
| 85 * credentials and all transferred bytes in the log. | |
| 86 * {@code false} to just include basic events. | |
| 87 */ | |
| 88 public abstract void startNetLogToFile(String fileName, boolean logAll); | |
| 89 | |
| 90 /** | |
| 91 * Stops NetLog logging and flushes file to disk. If a logging session is | |
| 92 * not in progress, this call is ignored. | |
| 93 */ | |
| 94 public abstract void stopNetLog(); | |
| 95 | |
| 96 /** | |
| 97 * Enables the network quality estimator, which collects and reports | |
| 98 * measurements of round trip time (RTT) and downstream throughput at | |
| 99 * various layers of the network stack. After enabling the estimator, | |
| 100 * listeners of RTT and throughput can be added with | |
| 101 * {@link #addRttListener} and {@link #addThroughputListener} and | |
| 102 * removed with {@link #removeRttListener} and | |
| 103 * {@link #removeThroughputListener}. The estimator uses memory and CPU | |
| 104 * only when enabled. | |
| 105 * @param executor an executor that will be used to notified all | |
| 106 * added RTT and throughput listeners. | |
| 107 * @deprecated not really deprecated but hidden for now as it's a prototype. | |
| 108 */ | |
| 109 @Deprecated public abstract void enableNetworkQualityEstimator(Executor exec
utor); | |
| 110 | |
| 111 /** | |
| 112 * Enables the network quality estimator for testing. This must be called | |
| 113 * before round trip time and throughput listeners are added. Set both | |
| 114 * boolean parameters to false for default behavior. | |
| 115 * @param useLocalHostRequests include requests to localhost in estimates. | |
| 116 * @param useSmallerResponses include small responses in throughput estimate
s. | |
| 117 * @param executor an {@link java.util.concurrent.Executor} on which all | |
| 118 * listeners will be called. | |
| 119 * @deprecated not really deprecated but hidden for now as it's a prototype. | |
| 120 */ | |
| 121 @Deprecated | |
| 122 abstract void enableNetworkQualityEstimatorForTesting( | |
| 123 boolean useLocalHostRequests, boolean useSmallerResponses, Executor
executor); | |
| 124 | |
| 125 /** | |
| 126 * Registers a listener that gets called whenever the network quality | |
| 127 * estimator witnesses a sample round trip time. This must be called | |
| 128 * after {@link #enableNetworkQualityEstimator}, and with throw an | |
| 129 * exception otherwise. Round trip times may be recorded at various layers | |
| 130 * of the network stack, including TCP, QUIC, and at the URL request layer. | |
| 131 * The listener is called on the {@link java.util.concurrent.Executor} that | |
| 132 * is passed to {@link #enableNetworkQualityEstimator}. | |
| 133 * @param listener the listener of round trip times. | |
| 134 * @deprecated not really deprecated but hidden for now as it's a prototype. | |
| 135 */ | |
| 136 @Deprecated public abstract void addRttListener(NetworkQualityRttListener li
stener); | |
| 137 | |
| 138 /** | |
| 139 * Removes a listener of round trip times if previously registered with | |
| 140 * {@link #addRttListener}. This should be called after a | |
| 141 * {@link NetworkQualityRttListener} is added in order to stop receiving | |
| 142 * observations. | |
| 143 * @param listener the listener of round trip times. | |
| 144 * @deprecated not really deprecated but hidden for now as it's a prototype. | |
| 145 */ | |
| 146 @Deprecated public abstract void removeRttListener(NetworkQualityRttListener
listener); | |
| 147 | |
| 148 /** | |
| 149 * Registers a listener that gets called whenever the network quality | |
| 150 * estimator witnesses a sample throughput measurement. This must be called | |
| 151 * after {@link #enableNetworkQualityEstimator}. Throughput observations | |
| 152 * are computed by measuring bytes read over the active network interface | |
| 153 * at times when at least one URL response is being received. The listener | |
| 154 * is called on the {@link java.util.concurrent.Executor} that is passed to | |
| 155 * {@link #enableNetworkQualityEstimator}. | |
| 156 * @param listener the listener of throughput. | |
| 157 * @deprecated not really deprecated but hidden for now as it's a prototype. | |
| 158 */ | |
| 159 @Deprecated | |
| 160 public abstract void addThroughputListener(NetworkQualityThroughputListener
listener); | |
| 161 | |
| 162 /** | |
| 163 * Removes a listener of throughput. This should be called after a | |
| 164 * {@link NetworkQualityThroughputListener} is added with | |
| 165 * {@link #addThroughputListener} in order to stop receiving observations. | |
| 166 * @param listener the listener of throughput. | |
| 167 * @deprecated not really deprecated but hidden for now as it's a prototype. | |
| 168 */ | |
| 169 @Deprecated | |
| 170 public abstract void removeThroughputListener(NetworkQualityThroughputListen
er listener); | |
| 171 | |
| 172 /** | |
| 173 * Creates a {@link UrlRequestContext} with the given | |
| 174 * {@link UrlRequestContextConfig}. | |
| 175 * @param context Android {@link Context}. | |
| 176 * @param config context configuration. | |
| 177 */ | |
| 178 public static UrlRequestContext createContext(Context context, | |
| 179 UrlRequestContextConfig config) { | |
| 180 UrlRequestContext urlRequestContext = null; | |
| 181 if (config.userAgent().isEmpty()) { | |
| 182 config.setUserAgent(UserAgent.from(context)); | |
| 183 } | |
| 184 if (!config.legacyMode()) { | |
| 185 urlRequestContext = createCronetContext(context, config); | |
| 186 } | |
| 187 if (urlRequestContext == null) { | |
| 188 // TODO(mef): Fallback to stub implementation. Once stub | |
| 189 // implementation is available merge with createCronetFactory. | |
| 190 urlRequestContext = createCronetContext(context, config); | |
| 191 } | |
| 192 Log.i(TAG, "Using network stack: " | |
| 193 + urlRequestContext.getVersionString()); | |
| 194 return urlRequestContext; | |
| 195 } | |
| 196 | |
| 197 private static UrlRequestContext createCronetContext(Context context, | |
| 198 UrlRequestContextConfig config) { | |
| 199 UrlRequestContext urlRequestContext = null; | |
| 200 try { | |
| 201 Class<? extends UrlRequestContext> contextClass = | |
| 202 UrlRequestContext.class.getClassLoader() | |
| 203 .loadClass(CRONET_URL_REQUEST_CONTEXT) | |
| 204 .asSubclass(UrlRequestContext.class); | |
| 205 Constructor<? extends UrlRequestContext> constructor = | |
| 206 contextClass.getConstructor( | |
| 207 Context.class, UrlRequestContextConfig.class); | |
| 208 UrlRequestContext cronetContext = | |
| 209 constructor.newInstance(context, config); | |
| 210 if (cronetContext.isEnabled()) { | |
| 211 urlRequestContext = cronetContext; | |
| 212 } | |
| 213 } catch (ClassNotFoundException e) { | |
| 214 // Leave as null. | |
| 215 } catch (Exception e) { | |
| 216 throw new IllegalStateException( | |
| 217 "Cannot instantiate: " + CRONET_URL_REQUEST_CONTEXT, | |
| 218 e); | |
| 219 } | |
| 220 return urlRequestContext; | |
| 221 } | |
| 222 } | |
| OLD | NEW |