| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.net; | 5 package org.chromium.net; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.os.Build; | 8 import android.os.Build; |
| 9 import android.os.ConditionVariable; | 9 import android.os.ConditionVariable; |
| 10 import android.os.Handler; | 10 import android.os.Handler; |
| 11 import android.os.Looper; | 11 import android.os.Looper; |
| 12 import android.os.Process; | 12 import android.os.Process; |
| 13 import android.util.Log; | 13 import android.util.Log; |
| 14 | 14 |
| 15 import org.chromium.base.ObserverList; |
| 15 import org.chromium.base.VisibleForTesting; | 16 import org.chromium.base.VisibleForTesting; |
| 16 import org.chromium.base.annotations.CalledByNative; | 17 import org.chromium.base.annotations.CalledByNative; |
| 17 import org.chromium.base.annotations.JNINamespace; | 18 import org.chromium.base.annotations.JNINamespace; |
| 18 import org.chromium.base.annotations.NativeClassQualifiedName; | 19 import org.chromium.base.annotations.NativeClassQualifiedName; |
| 19 import org.chromium.base.annotations.UsedByReflection; | 20 import org.chromium.base.annotations.UsedByReflection; |
| 20 | 21 |
| 21 import java.util.concurrent.Executor; | 22 import java.util.concurrent.Executor; |
| 23 import java.util.concurrent.RejectedExecutionException; |
| 22 import java.util.concurrent.atomic.AtomicInteger; | 24 import java.util.concurrent.atomic.AtomicInteger; |
| 23 | 25 |
| 26 import javax.annotation.concurrent.GuardedBy; |
| 27 |
| 24 /** | 28 /** |
| 25 * UrlRequestContext using Chromium HTTP stack implementation. | 29 * UrlRequestContext using Chromium HTTP stack implementation. |
| 26 */ | 30 */ |
| 27 @JNINamespace("cronet") | 31 @JNINamespace("cronet") |
| 28 @UsedByReflection("UrlRequestContext.java") | 32 @UsedByReflection("UrlRequestContext.java") |
| 29 class CronetUrlRequestContext extends UrlRequestContext { | 33 class CronetUrlRequestContext extends UrlRequestContext { |
| 30 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. | 34 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. |
| 31 private static final int LOG_DEBUG = -1; // LOG(FATAL...INFO), VLOG(1) | 35 private static final int LOG_DEBUG = -1; // LOG(FATAL...INFO), VLOG(1) |
| 32 private static final int LOG_VERBOSE = -2; // LOG(FATAL...INFO), VLOG(2) | 36 private static final int LOG_VERBOSE = -2; // LOG(FATAL...INFO), VLOG(2) |
| 33 static final String LOG_TAG = "ChromiumNetwork"; | 37 static final String LOG_TAG = "ChromiumNetwork"; |
| 34 | 38 |
| 35 /** | 39 /** |
| 36 * Synchronize access to mUrlRequestContextAdapter and shutdown routine. | 40 * Synchronize access to mUrlRequestContextAdapter and shutdown routine. |
| 37 */ | 41 */ |
| 38 private final Object mLock = new Object(); | 42 private final Object mLock = new Object(); |
| 39 private final ConditionVariable mInitCompleted = new ConditionVariable(false
); | 43 private final ConditionVariable mInitCompleted = new ConditionVariable(false
); |
| 40 private final AtomicInteger mActiveRequestCount = new AtomicInteger(0); | 44 private final AtomicInteger mActiveRequestCount = new AtomicInteger(0); |
| 41 | 45 |
| 42 private long mUrlRequestContextAdapter = 0; | 46 private long mUrlRequestContextAdapter = 0; |
| 43 private Thread mNetworkThread; | 47 private Thread mNetworkThread; |
| 44 | 48 |
| 49 private Executor mNetworkQualityExecutor; |
| 50 |
| 51 /** Locks operations on network quality observers, because observer |
| 52 * addition and removal may occur on a different thread from notification. |
| 53 */ |
| 54 private final Object mNetworkQualityLock = new Object(); |
| 55 |
| 56 @GuardedBy("mNetworkQualityLock") |
| 57 private final ObserverList<NetworkQualityRttObserver> mRttObserverList = |
| 58 new ObserverList<NetworkQualityRttObserver>(); |
| 59 |
| 60 @GuardedBy("mNetworkQualityLock") |
| 61 private final ObserverList<NetworkQualityThroughputObserver> mThroughputObse
rverList = |
| 62 new ObserverList<NetworkQualityThroughputObserver>(); |
| 63 |
| 45 @UsedByReflection("UrlRequestContext.java") | 64 @UsedByReflection("UrlRequestContext.java") |
| 46 public CronetUrlRequestContext(Context context, | 65 public CronetUrlRequestContext(Context context, |
| 47 UrlRequestContextConfig config) { | 66 UrlRequestContextConfig config) { |
| 48 CronetLibraryLoader.ensureInitialized(context, config); | 67 CronetLibraryLoader.ensureInitialized(context, config); |
| 49 nativeSetMinLogLevel(getLoggingLevel()); | 68 nativeSetMinLogLevel(getLoggingLevel()); |
| 50 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter(config.toS
tring()); | 69 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter(config.toS
tring()); |
| 51 if (mUrlRequestContextAdapter == 0) { | 70 if (mUrlRequestContextAdapter == 0) { |
| 52 throw new NullPointerException("Context Adapter creation failed."); | 71 throw new NullPointerException("Context Adapter creation failed."); |
| 53 } | 72 } |
| 54 | 73 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 } | 160 } |
| 142 | 161 |
| 143 @Override | 162 @Override |
| 144 public void stopNetLog() { | 163 public void stopNetLog() { |
| 145 synchronized (mLock) { | 164 synchronized (mLock) { |
| 146 checkHaveAdapter(); | 165 checkHaveAdapter(); |
| 147 nativeStopNetLog(mUrlRequestContextAdapter); | 166 nativeStopNetLog(mUrlRequestContextAdapter); |
| 148 } | 167 } |
| 149 } | 168 } |
| 150 | 169 |
| 170 @Override |
| 171 public void enableNetworkQualityEstimator(Executor executor) { |
| 172 enableNetworkQualityEstimator(false, false, executor); |
| 173 } |
| 174 |
| 175 @VisibleForTesting |
| 176 @Override |
| 177 void enableNetworkQualityEstimator( |
| 178 boolean useLocalHostRequests, boolean useSmallerResponses, Executor
executor) { |
| 179 mNetworkQualityExecutor = executor; |
| 180 synchronized (mLock) { |
| 181 checkHaveAdapter(); |
| 182 nativeEnableNetworkQualityEstimator( |
| 183 mUrlRequestContextAdapter, useLocalHostRequests, useSmallerR
esponses); |
| 184 } |
| 185 } |
| 186 |
| 187 @Override |
| 188 public void addRttObserver(NetworkQualityRttObserver observer) { |
| 189 synchronized (mNetworkQualityLock) { |
| 190 if (mRttObserverList.isEmpty()) { |
| 191 synchronized (mLock) { |
| 192 checkHaveAdapter(); |
| 193 nativeProvideRTTObservations(mUrlRequestContextAdapter, true
); |
| 194 } |
| 195 } |
| 196 mRttObserverList.addObserver(observer); |
| 197 } |
| 198 } |
| 199 |
| 200 @Override |
| 201 public void removeRttObserver(NetworkQualityRttObserver observer) { |
| 202 synchronized (mNetworkQualityLock) { |
| 203 mRttObserverList.removeObserver(observer); |
| 204 if (mRttObserverList.isEmpty()) { |
| 205 synchronized (mLock) { |
| 206 checkHaveAdapter(); |
| 207 nativeProvideRTTObservations(mUrlRequestContextAdapter, fals
e); |
| 208 } |
| 209 } |
| 210 } |
| 211 } |
| 212 |
| 213 @Override |
| 214 public void addThroughputObserver(NetworkQualityThroughputObserver observer)
{ |
| 215 synchronized (mNetworkQualityLock) { |
| 216 if (mThroughputObserverList.isEmpty()) { |
| 217 synchronized (mLock) { |
| 218 checkHaveAdapter(); |
| 219 nativeProvideThroughputObservations(mUrlRequestContextAdapte
r, true); |
| 220 } |
| 221 } |
| 222 mThroughputObserverList.addObserver(observer); |
| 223 } |
| 224 } |
| 225 |
| 226 @Override |
| 227 public void removeThroughputObserver(NetworkQualityThroughputObserver observ
er) { |
| 228 synchronized (mNetworkQualityLock) { |
| 229 mThroughputObserverList.removeObserver(observer); |
| 230 if (mThroughputObserverList.isEmpty()) { |
| 231 synchronized (mLock) { |
| 232 checkHaveAdapter(); |
| 233 nativeProvideThroughputObservations(mUrlRequestContextAdapte
r, false); |
| 234 } |
| 235 } |
| 236 } |
| 237 } |
| 238 |
| 151 /** | 239 /** |
| 152 * Mark request as started to prevent shutdown when there are active | 240 * Mark request as started to prevent shutdown when there are active |
| 153 * requests. | 241 * requests. |
| 154 */ | 242 */ |
| 155 void onRequestStarted(UrlRequest urlRequest) { | 243 void onRequestStarted(UrlRequest urlRequest) { |
| 156 mActiveRequestCount.incrementAndGet(); | 244 mActiveRequestCount.incrementAndGet(); |
| 157 } | 245 } |
| 158 | 246 |
| 159 /** | 247 /** |
| 160 * Mark request as completed to allow shutdown when there are no active | 248 * Mark request as completed to allow shutdown when there are no active |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 @CalledByNative | 290 @CalledByNative |
| 203 private void initNetworkThread() { | 291 private void initNetworkThread() { |
| 204 synchronized (mLock) { | 292 synchronized (mLock) { |
| 205 mNetworkThread = Thread.currentThread(); | 293 mNetworkThread = Thread.currentThread(); |
| 206 mInitCompleted.open(); | 294 mInitCompleted.open(); |
| 207 } | 295 } |
| 208 Thread.currentThread().setName("ChromiumNet"); | 296 Thread.currentThread().setName("ChromiumNet"); |
| 209 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); | 297 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 210 } | 298 } |
| 211 | 299 |
| 300 @SuppressWarnings("unused") |
| 301 @CalledByNative |
| 302 private void onRttObservation(final int rttMs, final long whenMs, final int
source) { |
| 303 Runnable task = new Runnable() { |
| 304 @Override |
| 305 public void run() { |
| 306 for (NetworkQualityRttObserver observer : mRttObserverList) { |
| 307 observer.onRttObservation(rttMs, whenMs, source); |
| 308 } |
| 309 } |
| 310 }; |
| 311 postObservationTaskToExecutor(task); |
| 312 } |
| 313 |
| 314 @SuppressWarnings("unused") |
| 315 @CalledByNative |
| 316 private void onThroughputObservation(final int throughputKbps, final long wh
enMs, |
| 317 final int source) { |
| 318 Runnable task = new Runnable() { |
| 319 @Override |
| 320 public void run() { |
| 321 for (NetworkQualityThroughputObserver observer : mThroughputObse
rverList) { |
| 322 observer.onThroughputObservation(throughputKbps, whenMs, sou
rce); |
| 323 } |
| 324 } |
| 325 }; |
| 326 postObservationTaskToExecutor(task); |
| 327 } |
| 328 |
| 329 void postObservationTaskToExecutor(Runnable task) { |
| 330 try { |
| 331 synchronized (mNetworkQualityLock) { |
| 332 mNetworkQualityExecutor.execute(task); |
| 333 } |
| 334 } catch (RejectedExecutionException failException) { |
| 335 Log.e(CronetUrlRequestContext.LOG_TAG, "Exception posting task to ex
ecutor", |
| 336 failException); |
| 337 } |
| 338 } |
| 339 |
| 212 // Native methods are implemented in cronet_url_request_context.cc. | 340 // Native methods are implemented in cronet_url_request_context.cc. |
| 213 private static native long nativeCreateRequestContextAdapter(String config); | 341 private static native long nativeCreateRequestContextAdapter(String config); |
| 214 | 342 |
| 215 private static native int nativeSetMinLogLevel(int loggingLevel); | 343 private static native int nativeSetMinLogLevel(int loggingLevel); |
| 216 | 344 |
| 217 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 345 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
| 218 private native void nativeDestroy(long nativePtr); | 346 private native void nativeDestroy(long nativePtr); |
| 219 | 347 |
| 220 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 348 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
| 221 private native void nativeStartNetLogToFile(long nativePtr, | 349 private native void nativeStartNetLogToFile(long nativePtr, |
| 222 String fileName, boolean logAll); | 350 String fileName, boolean logAll); |
| 223 | 351 |
| 224 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 352 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
| 225 private native void nativeStopNetLog(long nativePtr); | 353 private native void nativeStopNetLog(long nativePtr); |
| 226 | 354 |
| 227 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 355 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
| 228 private native void nativeInitRequestContextOnMainThread(long nativePtr); | 356 private native void nativeInitRequestContextOnMainThread(long nativePtr); |
| 357 |
| 358 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
| 359 private native void nativeEnableNetworkQualityEstimator( |
| 360 long nativePtr, boolean useLocalHostRequests, boolean useSmallerResp
onses); |
| 361 |
| 362 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
| 363 private native void nativeProvideRTTObservations(long nativePtr, boolean sho
uld); |
| 364 |
| 365 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
| 366 private native void nativeProvideThroughputObservations(long nativePtr, bool
ean should); |
| 229 } | 367 } |
| OLD | NEW |