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..0b8e48b80a0cec73531cb0c9d850f3c5dd0bc2d6 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,8 +20,11 @@ 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; |
+import javax.annotation.concurrent.GuardedBy; |
+ |
/** |
* UrlRequestContext using Chromium HTTP stack implementation. |
*/ |
@@ -42,6 +46,20 @@ class CronetUrlRequestContext extends UrlRequestContext { |
private long mUrlRequestContextAdapter = 0; |
private Thread mNetworkThread; |
+ private Executor mNetworkQualityExecutor; |
+ |
+ // Locks operations on network quality observers, because observer addition and removal may |
tbansal1
2015/09/15 17:10:24
May be javadoc style comments?
/**
*
*/
bengr
2015/09/16 15:32:38
Done.
|
+ // occur on a different thread from notification. |
+ private final Object mNetworkQualityLock = new Object(); |
+ |
+ @GuardedBy("mNetworkQualityLock") |
+ private final ObserverList<NetworkQualityRttObserver> mRttObserverList = |
+ new ObserverList<NetworkQualityRttObserver>(); |
+ |
+ @GuardedBy("mNetworkQualityLock") |
+ private final ObserverList<NetworkQualityThroughputObserver> mThroughputObserverList = |
+ new ObserverList<NetworkQualityThroughputObserver>(); |
+ |
@UsedByReflection("UrlRequestContext.java") |
public CronetUrlRequestContext(Context context, |
UrlRequestContextConfig config) { |
@@ -148,6 +166,75 @@ class CronetUrlRequestContext extends UrlRequestContext { |
} |
} |
+ @Override |
+ public void enableNetworkQualityEstimator(Executor executor) { |
+ enableNetworkQualityEstimator(false, false, executor); |
+ } |
+ |
+ @VisibleForTesting |
+ @Override |
+ void enableNetworkQualityEstimator( |
+ boolean useLocalHostRequests, boolean useSmallerResponses, Executor executor) { |
+ mNetworkQualityExecutor = executor; |
+ synchronized (mLock) { |
+ checkHaveAdapter(); |
+ nativeEnableNetworkQualityEstimator( |
+ mUrlRequestContextAdapter, useLocalHostRequests, useSmallerResponses); |
+ } |
+ } |
+ |
+ @Override |
+ public void addRttObserver(NetworkQualityRttObserver observer) { |
+ synchronized (mNetworkQualityLock) { |
+ if (mRttObserverList.isEmpty()) { |
+ synchronized (mLock) { |
+ checkHaveAdapter(); |
+ nativeProvideRTTObservations(mUrlRequestContextAdapter, true); |
+ } |
+ } |
+ 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) { |
+ synchronized (mNetworkQualityLock) { |
+ if (mThroughputObserverList.isEmpty()) { |
+ synchronized (mLock) { |
+ checkHaveAdapter(); |
+ nativeProvideThroughputObservations(mUrlRequestContextAdapter, true); |
+ } |
+ } |
+ 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 +296,45 @@ class CronetUrlRequestContext extends UrlRequestContext { |
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
} |
+ @SuppressWarnings("unused") |
tbansal1
2015/09/15 17:10:24
Why does it need SuppressWarnings. Looks like it i
bengr
2015/09/16 15:32:38
Following the pattern for other methods that are c
|
+ @CalledByNative |
+ private void onRttObservation(final int value, final long when, final int source) { |
tbansal1
2015/09/15 17:10:24
Why not whenMs here and below?
bengr
2015/09/16 15:32:38
Done.
|
+ 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 +352,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); |
} |