Chromium Code Reviews| Index: components/cronet/android/java/src/org/chromium/net/CronetUrlRequestContext.java |
| diff --git a/components/cronet/android/java/src/org/chromium/net/CronetUrlRequestContext.java b/components/cronet/android/java/src/org/chromium/net/CronetUrlRequestContext.java |
| index 7bae2883221d5340a4ddbb23c19c9bf4865d978a..22c7045957ded0bf8015e81daa22e1a74c5cf790 100644 |
| --- a/components/cronet/android/java/src/org/chromium/net/CronetUrlRequestContext.java |
| +++ b/components/cronet/android/java/src/org/chromium/net/CronetUrlRequestContext.java |
| @@ -12,6 +12,7 @@ import android.os.Looper; |
| import android.os.Process; |
| import android.util.Log; |
| +import org.chromium.base.ObserverList; |
| import org.chromium.base.VisibleForTesting; |
| import org.chromium.base.annotations.CalledByNative; |
| import org.chromium.base.annotations.JNINamespace; |
| @@ -19,6 +20,7 @@ import org.chromium.base.annotations.NativeClassQualifiedName; |
| import org.chromium.base.annotations.UsedByReflection; |
| import java.util.concurrent.Executor; |
| +import java.util.concurrent.RejectedExecutionException; |
| import java.util.concurrent.atomic.AtomicInteger; |
| /** |
| @@ -42,6 +44,14 @@ class CronetUrlRequestContext extends UrlRequestContext { |
| private long mUrlRequestContextAdapter = 0; |
| private Thread mNetworkThread; |
| + private Executor mNetworkQualityExecutor; |
| + private final Object mNetworkQualityLock = new Object(); |
|
pauljensen
2015/09/02 16:54:48
Please add a comment about what this is locking.
bengr
2015/09/14 21:04:35
Done.
|
| + |
| + private final ObserverList<NetworkQualityRTTObserver> mRTTObserverList = |
|
pauljensen
2015/09/02 16:54:48
Can we use @GuardedBy?
bengr
2015/09/14 21:04:35
Done.
|
| + new ObserverList<NetworkQualityRTTObserver>(); |
| + private final ObserverList<NetworkQualityThroughputObserver> mThroughputObserverList = |
| + new ObserverList<NetworkQualityThroughputObserver>(); |
| + |
| @UsedByReflection("UrlRequestContext.java") |
| public CronetUrlRequestContext(Context context, |
| UrlRequestContextConfig config) { |
| @@ -148,6 +158,74 @@ class CronetUrlRequestContext extends UrlRequestContext { |
| } |
| } |
| + @Override |
| + public void enableNetworkQualityEstimator(Executor executor) { |
| + enableNetworkQualityEstimator(false, false, executor); |
| + } |
| + |
| + @Override |
| + void enableNetworkQualityEstimator( |
| + boolean useLocalHostRequests, boolean useSmallerResponses, Executor executor) { |
| + mNetworkQualityExecutor = executor; |
| + synchronized (mLock) { |
| + checkHaveAdapter(); |
| + nativeEnableNetworkQualityEstimator( |
| + mUrlRequestContextAdapter, useLocalHostRequests, useSmallerResponses); |
| + } |
| + } |
| + |
| + @Override |
| + public void addRTTObserver(NetworkQualityRTTObserver observer) { |
| + if (mRTTObserverList.isEmpty()) { |
|
pauljensen
2015/09/02 16:54:48
You lock mRTTObserverList on line 186 but not here
bengr
2015/09/14 21:04:35
Good catch. Thanks.
|
| + synchronized (mLock) { |
| + checkHaveAdapter(); |
| + nativeProvideRTTObservations(mUrlRequestContextAdapter, true); |
| + } |
| + } |
| + synchronized (mNetworkQualityLock) { |
| + mRTTObserverList.addObserver(observer); |
| + } |
| + } |
| + |
| + @Override |
| + public void removeRTTObserver(NetworkQualityRTTObserver observer) { |
| + synchronized (mNetworkQualityLock) { |
| + mRTTObserverList.removeObserver(observer); |
| + } |
| + if (mRTTObserverList.isEmpty()) { |
| + synchronized (mLock) { |
| + checkHaveAdapter(); |
| + nativeProvideRTTObservations(mUrlRequestContextAdapter, false); |
| + } |
| + } |
| + } |
| + |
| + @Override |
| + public void addThroughputObserver(NetworkQualityThroughputObserver observer) { |
| + if (mThroughputObserverList.isEmpty()) { |
| + synchronized (mLock) { |
| + checkHaveAdapter(); |
| + nativeProvideThroughputObservations(mUrlRequestContextAdapter, true); |
| + } |
| + } |
| + synchronized (mNetworkQualityLock) { |
| + mThroughputObserverList.addObserver(observer); |
| + } |
| + } |
| + |
| + @Override |
| + public void removeThroughputObserver(NetworkQualityThroughputObserver observer) { |
| + synchronized (mNetworkQualityLock) { |
| + mThroughputObserverList.removeObserver(observer); |
| + } |
| + if (mThroughputObserverList.isEmpty()) { |
| + synchronized (mLock) { |
| + checkHaveAdapter(); |
| + nativeProvideThroughputObservations(mUrlRequestContextAdapter, false); |
| + } |
| + } |
| + } |
| + |
| /** |
| * Mark request as started to prevent shutdown when there are active |
| * requests. |
| @@ -209,6 +287,45 @@ class CronetUrlRequestContext extends UrlRequestContext { |
| Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| } |
| + @SuppressWarnings("unused") |
| + @CalledByNative |
| + private void onRTTObservation(final int value, final long when, final int source) { |
| + Runnable task = new Runnable() { |
| + @Override |
| + public void run() { |
| + for (NetworkQualityRTTObserver observer : mRTTObserverList) { |
| + observer.onRTTObservation(value, when, source); |
| + } |
| + } |
| + }; |
| + postObservationTaskToExecutor(task); |
| + } |
| + |
| + @SuppressWarnings("unused") |
| + @CalledByNative |
| + private void onThroughputObservation(final int value, final long when, final int source) { |
| + Runnable task = new Runnable() { |
| + @Override |
| + public void run() { |
| + for (NetworkQualityThroughputObserver observer : mThroughputObserverList) { |
| + observer.onThroughputObservation(value, when, source); |
| + } |
| + } |
| + }; |
| + postObservationTaskToExecutor(task); |
| + } |
| + |
| + void postObservationTaskToExecutor(Runnable task) { |
| + try { |
| + synchronized (mNetworkQualityLock) { |
| + mNetworkQualityExecutor.execute(task); |
| + } |
| + } catch (RejectedExecutionException failException) { |
| + Log.e(CronetUrlRequestContext.LOG_TAG, "Exception posting task to executor", |
| + failException); |
| + } |
| + } |
| + |
| // Native methods are implemented in cronet_url_request_context.cc. |
| private static native long nativeCreateRequestContextAdapter(String config); |
| @@ -226,4 +343,14 @@ class CronetUrlRequestContext extends UrlRequestContext { |
| @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
| private native void nativeInitRequestContextOnMainThread(long nativePtr); |
| + |
| + @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
| + private native void nativeEnableNetworkQualityEstimator( |
| + long nativePtr, boolean useLocalHostRequests, boolean useSmallerResponses); |
| + |
| + @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
| + private native void nativeProvideRTTObservations(long nativePtr, boolean should); |
| + |
| + @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
| + private native void nativeProvideThroughputObservations(long nativePtr, boolean should); |
| } |