Chromium Code Reviews| 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.impl; | 5 package org.chromium.net.impl; |
| 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.ObserverList; |
| 16 import org.chromium.base.VisibleForTesting; | 16 import org.chromium.base.VisibleForTesting; |
| 17 import org.chromium.base.annotations.CalledByNative; | 17 import org.chromium.base.annotations.CalledByNative; |
| 18 import org.chromium.base.annotations.JNINamespace; | 18 import org.chromium.base.annotations.JNINamespace; |
| 19 import org.chromium.base.annotations.NativeClassQualifiedName; | 19 import org.chromium.base.annotations.NativeClassQualifiedName; |
| 20 import org.chromium.base.annotations.UsedByReflection; | 20 import org.chromium.base.annotations.UsedByReflection; |
| 21 import org.chromium.net.BidirectionalStream; | 21 import org.chromium.net.BidirectionalStream; |
| 22 import org.chromium.net.CronetEngine; | 22 import org.chromium.net.CronetEngine; |
| 23 import org.chromium.net.NetworkQualityRttListener; | 23 import org.chromium.net.NetworkQualityRttListener; |
| 24 import org.chromium.net.NetworkQualityThroughputListener; | 24 import org.chromium.net.NetworkQualityThroughputListener; |
| 25 import org.chromium.net.RequestFinishedListener; | |
| 25 import org.chromium.net.UrlRequest; | 26 import org.chromium.net.UrlRequest; |
| 26 import org.chromium.net.urlconnection.CronetHttpURLConnection; | 27 import org.chromium.net.urlconnection.CronetHttpURLConnection; |
| 27 import org.chromium.net.urlconnection.CronetURLStreamHandlerFactory; | 28 import org.chromium.net.urlconnection.CronetURLStreamHandlerFactory; |
| 28 | 29 |
| 29 import java.net.Proxy; | 30 import java.net.Proxy; |
| 30 import java.net.URL; | 31 import java.net.URL; |
| 31 import java.net.URLConnection; | 32 import java.net.URLConnection; |
| 32 import java.net.URLStreamHandlerFactory; | 33 import java.net.URLStreamHandlerFactory; |
| 33 import java.util.Collection; | 34 import java.util.Collection; |
| 34 import java.util.List; | 35 import java.util.List; |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 61 private long mUrlRequestContextAdapter = 0; | 62 private long mUrlRequestContextAdapter = 0; |
| 62 private Thread mNetworkThread; | 63 private Thread mNetworkThread; |
| 63 | 64 |
| 64 private Executor mNetworkQualityExecutor; | 65 private Executor mNetworkQualityExecutor; |
| 65 private boolean mNetworkQualityEstimatorEnabled; | 66 private boolean mNetworkQualityEstimatorEnabled; |
| 66 | 67 |
| 67 /** Locks operations on network quality listeners, because listener | 68 /** Locks operations on network quality listeners, because listener |
| 68 * addition and removal may occur on a different thread from notification. | 69 * addition and removal may occur on a different thread from notification. |
| 69 */ | 70 */ |
| 70 private final Object mNetworkQualityLock = new Object(); | 71 private final Object mNetworkQualityLock = new Object(); |
| 72 private final Object mFinishedListenerLock = new Object(); | |
|
tbansal1
2016/07/26 20:37:26
May be add a separate comment block for this.
xunjieli
2016/07/26 21:11:09
Can you add a comment here?
mgersh
2016/07/27 20:54:12
Done.
mgersh
2016/07/27 20:54:12
Done.
| |
| 71 | 73 |
| 72 @GuardedBy("mNetworkQualityLock") | 74 @GuardedBy("mNetworkQualityLock") |
| 73 private final ObserverList<NetworkQualityRttListener> mRttListenerList = | 75 private final ObserverList<NetworkQualityRttListener> mRttListenerList = |
| 74 new ObserverList<NetworkQualityRttListener>(); | 76 new ObserverList<NetworkQualityRttListener>(); |
| 75 | 77 |
| 76 @GuardedBy("mNetworkQualityLock") | 78 @GuardedBy("mNetworkQualityLock") |
| 77 private final ObserverList<NetworkQualityThroughputListener> mThroughputList enerList = | 79 private final ObserverList<NetworkQualityThroughputListener> mThroughputList enerList = |
| 78 new ObserverList<NetworkQualityThroughputListener>(); | 80 new ObserverList<NetworkQualityThroughputListener>(); |
| 79 | 81 |
| 80 @GuardedBy("mNetworkQualityLock") | 82 @GuardedBy("mFinishedListenerLock") |
| 81 private final ObserverList<RequestFinishedListener> mFinishedListenerList = | 83 private final ObserverList<RequestFinishedListener> mFinishedListenerList = |
| 82 new ObserverList<RequestFinishedListener>(); | 84 new ObserverList<RequestFinishedListener>(); |
| 83 | 85 |
| 84 /** | 86 /** |
| 85 * Synchronize access to mCertVerifierData. | 87 * Synchronize access to mCertVerifierData. |
| 86 */ | 88 */ |
| 87 private ConditionVariable mWaitGetCertVerifierDataComplete = new ConditionVa riable(); | 89 private ConditionVariable mWaitGetCertVerifierDataComplete = new ConditionVa riable(); |
| 88 | 90 |
| 89 /** Holds CertVerifier data. */ | 91 /** Holds CertVerifier data. */ |
| 90 private String mCertVerifierData; | 92 private String mCertVerifierData; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 146 } | 148 } |
| 147 return urlRequestContextConfig; | 149 return urlRequestContextConfig; |
| 148 } | 150 } |
| 149 | 151 |
| 150 @Override | 152 @Override |
| 151 public UrlRequest createRequest(String url, UrlRequest.Callback callback, Ex ecutor executor, | 153 public UrlRequest createRequest(String url, UrlRequest.Callback callback, Ex ecutor executor, |
| 152 int priority, Collection<Object> requestAnnotations, boolean disable Cache, | 154 int priority, Collection<Object> requestAnnotations, boolean disable Cache, |
| 153 boolean disableConnectionMigration) { | 155 boolean disableConnectionMigration) { |
| 154 synchronized (mLock) { | 156 synchronized (mLock) { |
| 155 checkHaveAdapter(); | 157 checkHaveAdapter(); |
| 156 boolean metricsCollectionEnabled = mNetworkQualityEstimatorEnabled; | 158 boolean metricsCollectionEnabled = false; |
| 157 if (metricsCollectionEnabled) { // Collect metrics only if someone i s listening. | 159 synchronized (mFinishedListenerLock) { |
| 158 synchronized (mNetworkQualityLock) { | 160 metricsCollectionEnabled = !mFinishedListenerList.isEmpty(); |
| 159 metricsCollectionEnabled = !mFinishedListenerList.isEmpty(); | |
| 160 } | |
| 161 } | 161 } |
| 162 return new CronetUrlRequest(this, url, priority, callback, executor, requestAnnotations, | 162 return new CronetUrlRequest(this, url, priority, callback, executor, requestAnnotations, |
| 163 metricsCollectionEnabled, disableCache, disableConnectionMig ration); | 163 metricsCollectionEnabled, disableCache, disableConnectionMig ration); |
| 164 } | 164 } |
| 165 } | 165 } |
| 166 | 166 |
| 167 @Override | 167 @Override |
| 168 public BidirectionalStream createBidirectionalStream(String url, | 168 public BidirectionalStream createBidirectionalStream(String url, |
| 169 BidirectionalStream.Callback callback, Executor executor, String htt pMethod, | 169 BidirectionalStream.Callback callback, Executor executor, String htt pMethod, |
| 170 List<Map.Entry<String, String>> requestHeaders, | 170 List<Map.Entry<String, String>> requestHeaders, |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 372 } | 372 } |
| 373 } | 373 } |
| 374 | 374 |
| 375 /** | 375 /** |
| 376 * TODO(tbansal): http://crbug.com/618034 Remove this API once all | 376 * TODO(tbansal): http://crbug.com/618034 Remove this API once all |
| 377 * embedders have switched to using a request finished listener that | 377 * embedders have switched to using a request finished listener that |
| 378 * provides its own executor. | 378 * provides its own executor. |
| 379 */ | 379 */ |
| 380 @Override | 380 @Override |
| 381 public void addRequestFinishedListener(RequestFinishedListener listener) { | 381 public void addRequestFinishedListener(RequestFinishedListener listener) { |
| 382 if (!mNetworkQualityEstimatorEnabled) { | 382 synchronized (mFinishedListenerLock) { |
| 383 throw new IllegalStateException("Network quality estimator must be e nabled"); | |
| 384 } | |
| 385 // RequestFinishedListener does not provide its own executor. | |
| 386 if (mNetworkQualityExecutor == null) { | |
| 387 throw new IllegalStateException("Executor must not be null"); | |
| 388 } | |
| 389 synchronized (mNetworkQualityLock) { | |
| 390 mFinishedListenerList.addObserver(listener); | 383 mFinishedListenerList.addObserver(listener); |
| 391 } | 384 } |
| 392 } | 385 } |
| 393 | 386 |
| 394 /** | 387 /** |
| 395 * TODO(tbansal): http://crbug.com/618034 Remove this API. | 388 * TODO(tbansal): http://crbug.com/618034 Remove this API. |
| 396 */ | 389 */ |
| 397 @Override | 390 @Override |
| 398 public void removeRequestFinishedListener(RequestFinishedListener listener) { | 391 public void removeRequestFinishedListener(RequestFinishedListener listener) { |
| 399 if (!mNetworkQualityEstimatorEnabled) { | 392 synchronized (mFinishedListenerLock) { |
| 400 throw new IllegalStateException("Network quality estimator must be e nabled"); | |
| 401 } | |
| 402 synchronized (mNetworkQualityLock) { | |
| 403 mFinishedListenerList.removeObserver(listener); | 393 mFinishedListenerList.removeObserver(listener); |
| 404 } | 394 } |
| 405 } | 395 } |
| 406 | 396 |
| 407 @Override | 397 @Override |
| 408 public URLConnection openConnection(URL url) { | 398 public URLConnection openConnection(URL url) { |
| 409 return openConnection(url, Proxy.NO_PROXY); | 399 return openConnection(url, Proxy.NO_PROXY); |
| 410 } | 400 } |
| 411 | 401 |
| 412 @Override | 402 @Override |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 521 } | 511 } |
| 522 | 512 |
| 523 @SuppressWarnings("unused") | 513 @SuppressWarnings("unused") |
| 524 @CalledByNative | 514 @CalledByNative |
| 525 private void onGetCertVerifierData(String certVerifierData) { | 515 private void onGetCertVerifierData(String certVerifierData) { |
| 526 mCertVerifierData = certVerifierData; | 516 mCertVerifierData = certVerifierData; |
| 527 mWaitGetCertVerifierDataComplete.open(); | 517 mWaitGetCertVerifierDataComplete.open(); |
| 528 } | 518 } |
| 529 | 519 |
| 530 void reportFinished(final CronetUrlRequest request) { | 520 void reportFinished(final CronetUrlRequest request) { |
| 531 if (!mNetworkQualityEstimatorEnabled) { | 521 final UrlRequestInfo requestInfo = request.getRequestInfo(); |
| 532 return; | 522 synchronized (mFinishedListenerLock) { |
| 533 } | 523 for (final RequestFinishedListener listener : mFinishedListenerList) { |
| 534 // If no request finished listener has been added, then mNetworkQualityE xecutor may be | 524 Runnable task = new Runnable() { |
| 535 // null. Exit early to avoid posting to a null executor. | 525 @Override |
| 536 synchronized (mNetworkQualityLock) { | 526 public void run() { |
| 537 if (mFinishedListenerList.isEmpty()) { | 527 listener.onRequestFinished(requestInfo); |
| 538 return; | 528 } |
| 529 }; | |
| 530 postObservationTaskToExecutor(listener.getExecutor(), task); | |
|
xunjieli
2016/07/26 21:11:09
We shouldn't call into the executor, which might h
mgersh
2016/07/27 20:54:12
Done. I also changed mFinishedListenerList from an
xunjieli
2016/07/29 16:35:24
Acknowledged. I agree with you.
| |
| 539 } | 531 } |
| 540 } | 532 } |
| 541 Runnable task = new Runnable() { | |
| 542 @Override | |
| 543 public void run() { | |
| 544 synchronized (mNetworkQualityLock) { | |
| 545 UrlRequestInfo requestInfo = request.getRequestInfo(); | |
| 546 for (RequestFinishedListener listener : mFinishedListenerLis t) { | |
| 547 listener.onRequestFinished(requestInfo); | |
| 548 } | |
| 549 } | |
| 550 } | |
| 551 }; | |
| 552 // Use {@link mNetworkQualityExecutor} since | |
| 553 // RequestFInishedListeners do not provide an executor. | |
| 554 postObservationTaskToExecutor(mNetworkQualityExecutor, task); | |
| 555 } | 533 } |
| 556 | 534 |
| 557 private static void postObservationTaskToExecutor(Executor executor, Runnabl e task) { | 535 private static void postObservationTaskToExecutor(Executor executor, Runnabl e task) { |
| 558 try { | 536 try { |
| 559 executor.execute(task); | 537 executor.execute(task); |
| 560 } catch (RejectedExecutionException failException) { | 538 } catch (RejectedExecutionException failException) { |
| 561 Log.e(CronetUrlRequestContext.LOG_TAG, "Exception posting task to ex ecutor", | 539 Log.e(CronetUrlRequestContext.LOG_TAG, "Exception posting task to ex ecutor", |
| 562 failException); | 540 failException); |
| 563 } | 541 } |
| 564 } | 542 } |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 606 | 584 |
| 607 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 585 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
| 608 private native void nativeEnableNetworkQualityEstimator(long nativePtr); | 586 private native void nativeEnableNetworkQualityEstimator(long nativePtr); |
| 609 | 587 |
| 610 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 588 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
| 611 private native void nativeProvideRTTObservations(long nativePtr, boolean sho uld); | 589 private native void nativeProvideRTTObservations(long nativePtr, boolean sho uld); |
| 612 | 590 |
| 613 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 591 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
| 614 private native void nativeProvideThroughputObservations(long nativePtr, bool ean should); | 592 private native void nativeProvideThroughputObservations(long nativePtr, bool ean should); |
| 615 } | 593 } |
| OLD | NEW |