Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(201)

Side by Side Diff: components/cronet/android/java/src/org/chromium/net/impl/CronetUrlRequestContext.java

Issue 2514783002: [Cronet] Add callback wrapper classes to enforce API version checking. (Closed)
Patch Set: fix JNIAdditionalImport Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.os.ConditionVariable; 7 import android.os.ConditionVariable;
8 import android.os.Handler; 8 import android.os.Handler;
9 import android.os.Looper; 9 import android.os.Looper;
10 import android.os.Process; 10 import android.os.Process;
(...skipping 15 matching lines...) Expand all
26 import org.chromium.net.UrlRequest; 26 import org.chromium.net.UrlRequest;
27 import org.chromium.net.urlconnection.CronetHttpURLConnection; 27 import org.chromium.net.urlconnection.CronetHttpURLConnection;
28 import org.chromium.net.urlconnection.CronetURLStreamHandlerFactory; 28 import org.chromium.net.urlconnection.CronetURLStreamHandlerFactory;
29 29
30 import java.net.Proxy; 30 import java.net.Proxy;
31 import java.net.URL; 31 import java.net.URL;
32 import java.net.URLConnection; 32 import java.net.URLConnection;
33 import java.net.URLStreamHandlerFactory; 33 import java.net.URLStreamHandlerFactory;
34 import java.util.ArrayList; 34 import java.util.ArrayList;
35 import java.util.Collection; 35 import java.util.Collection;
36 import java.util.HashMap;
36 import java.util.List; 37 import java.util.List;
37 import java.util.Map; 38 import java.util.Map;
38 import java.util.concurrent.Executor; 39 import java.util.concurrent.Executor;
39 import java.util.concurrent.RejectedExecutionException; 40 import java.util.concurrent.RejectedExecutionException;
40 import java.util.concurrent.atomic.AtomicInteger; 41 import java.util.concurrent.atomic.AtomicInteger;
41 42
42 import javax.annotation.concurrent.GuardedBy; 43 import javax.annotation.concurrent.GuardedBy;
43 44
44 /** 45 /**
45 * CronetEngine using Chromium HTTP stack implementation. 46 * CronetEngine using Chromium HTTP stack implementation.
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 private int mTransportRttMs = RttThroughputValues.INVALID_RTT_THROUGHPUT; 109 private int mTransportRttMs = RttThroughputValues.INVALID_RTT_THROUGHPUT;
109 110
110 /** 111 /**
111 * Current estimate of the downstream throughput (in kilobits per second) 112 * Current estimate of the downstream throughput (in kilobits per second)
112 * computed by the network quality estimator. 113 * computed by the network quality estimator.
113 */ 114 */
114 @GuardedBy("mNetworkQualityLock") 115 @GuardedBy("mNetworkQualityLock")
115 private int mDownstreamThroughputKbps = RttThroughputValues.INVALID_RTT_THRO UGHPUT; 116 private int mDownstreamThroughputKbps = RttThroughputValues.INVALID_RTT_THRO UGHPUT;
116 117
117 @GuardedBy("mNetworkQualityLock") 118 @GuardedBy("mNetworkQualityLock")
118 private final ObserverList<NetworkQualityRttListener> mRttListenerList = 119 private final ObserverList<VersionSafeCallbacks.NetworkQualityRttListenerWra pper>
119 new ObserverList<NetworkQualityRttListener>(); 120 mRttListenerList =
121 new ObserverList<VersionSafeCallbacks.NetworkQualityRttListe nerWrapper>();
120 122
121 @GuardedBy("mNetworkQualityLock") 123 @GuardedBy("mNetworkQualityLock")
122 private final ObserverList<NetworkQualityThroughputListener> mThroughputList enerList = 124 private final ObserverList<VersionSafeCallbacks.NetworkQualityThroughputList enerWrapper>
123 new ObserverList<NetworkQualityThroughputListener>(); 125 mThroughputListenerList =
126 new ObserverList<VersionSafeCallbacks
127 .NetworkQualityThroughputListenerWr apper>();
124 128
125 @GuardedBy("mFinishedListenerLock") 129 @GuardedBy("mFinishedListenerLock")
126 private final List<RequestFinishedInfo.Listener> mFinishedListenerList = 130 private final Map<RequestFinishedInfo.Listener,
127 new ArrayList<RequestFinishedInfo.Listener>(); 131 VersionSafeCallbacks.RequestFinishedInfoListener> mFinishedListenerM ap =
132 new HashMap<RequestFinishedInfo.Listener,
133 VersionSafeCallbacks.RequestFinishedInfoListener>();
128 134
129 /** 135 /**
130 * Synchronize access to mCertVerifierData. 136 * Synchronize access to mCertVerifierData.
131 */ 137 */
132 private ConditionVariable mWaitGetCertVerifierDataComplete = new ConditionVa riable(); 138 private ConditionVariable mWaitGetCertVerifierDataComplete = new ConditionVa riable();
133 139
134 /** Holds CertVerifier data. */ 140 /** Holds CertVerifier data. */
135 private String mCertVerifierData; 141 private String mCertVerifierData;
136 142
137 private ConditionVariable mStopNetLogCompleted; 143 private ConditionVariable mStopNetLogCompleted;
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 if (!mNetworkQualityEstimatorEnabled) { 411 if (!mNetworkQualityEstimatorEnabled) {
406 throw new IllegalStateException("Network quality estimator must be e nabled"); 412 throw new IllegalStateException("Network quality estimator must be e nabled");
407 } 413 }
408 synchronized (mNetworkQualityLock) { 414 synchronized (mNetworkQualityLock) {
409 if (mRttListenerList.isEmpty()) { 415 if (mRttListenerList.isEmpty()) {
410 synchronized (mLock) { 416 synchronized (mLock) {
411 checkHaveAdapter(); 417 checkHaveAdapter();
412 nativeProvideRTTObservations(mUrlRequestContextAdapter, true ); 418 nativeProvideRTTObservations(mUrlRequestContextAdapter, true );
413 } 419 }
414 } 420 }
415 mRttListenerList.addObserver(listener); 421 mRttListenerList.addObserver(
422 new VersionSafeCallbacks.NetworkQualityRttListenerWrapper(li stener));
416 } 423 }
417 } 424 }
418 425
419 @Override 426 @Override
420 public void removeRttListener(NetworkQualityRttListener listener) { 427 public void removeRttListener(NetworkQualityRttListener listener) {
421 if (!mNetworkQualityEstimatorEnabled) { 428 if (!mNetworkQualityEstimatorEnabled) {
422 throw new IllegalStateException("Network quality estimator must be e nabled"); 429 throw new IllegalStateException("Network quality estimator must be e nabled");
423 } 430 }
424 synchronized (mNetworkQualityLock) { 431 synchronized (mNetworkQualityLock) {
425 mRttListenerList.removeObserver(listener); 432 if (mRttListenerList.removeObserver(
426 if (mRttListenerList.isEmpty()) { 433 new VersionSafeCallbacks.NetworkQualityRttListenerWrappe r(listener))) {
427 synchronized (mLock) { 434 if (mRttListenerList.isEmpty()) {
428 checkHaveAdapter(); 435 synchronized (mLock) {
429 nativeProvideRTTObservations(mUrlRequestContextAdapter, fals e); 436 checkHaveAdapter();
437 nativeProvideRTTObservations(mUrlRequestContextAdapter, false);
438 }
430 } 439 }
431 } 440 }
432 } 441 }
433 } 442 }
434 443
435 @Override 444 @Override
436 public void addThroughputListener(NetworkQualityThroughputListener listener) { 445 public void addThroughputListener(NetworkQualityThroughputListener listener) {
437 if (!mNetworkQualityEstimatorEnabled) { 446 if (!mNetworkQualityEstimatorEnabled) {
438 throw new IllegalStateException("Network quality estimator must be e nabled"); 447 throw new IllegalStateException("Network quality estimator must be e nabled");
439 } 448 }
440 synchronized (mNetworkQualityLock) { 449 synchronized (mNetworkQualityLock) {
441 if (mThroughputListenerList.isEmpty()) { 450 if (mThroughputListenerList.isEmpty()) {
442 synchronized (mLock) { 451 synchronized (mLock) {
443 checkHaveAdapter(); 452 checkHaveAdapter();
444 nativeProvideThroughputObservations(mUrlRequestContextAdapte r, true); 453 nativeProvideThroughputObservations(mUrlRequestContextAdapte r, true);
445 } 454 }
446 } 455 }
447 mThroughputListenerList.addObserver(listener); 456 mThroughputListenerList.addObserver(
457 new VersionSafeCallbacks.NetworkQualityThroughputListenerWra pper(listener));
448 } 458 }
449 } 459 }
450 460
451 @Override 461 @Override
452 public void removeThroughputListener(NetworkQualityThroughputListener listen er) { 462 public void removeThroughputListener(NetworkQualityThroughputListener listen er) {
453 if (!mNetworkQualityEstimatorEnabled) { 463 if (!mNetworkQualityEstimatorEnabled) {
454 throw new IllegalStateException("Network quality estimator must be e nabled"); 464 throw new IllegalStateException("Network quality estimator must be e nabled");
455 } 465 }
456 synchronized (mNetworkQualityLock) { 466 synchronized (mNetworkQualityLock) {
457 mThroughputListenerList.removeObserver(listener); 467 if (mThroughputListenerList.removeObserver(
458 if (mThroughputListenerList.isEmpty()) { 468 new VersionSafeCallbacks.NetworkQualityThroughputListene rWrapper(
459 synchronized (mLock) { 469 listener))) {
460 checkHaveAdapter(); 470 if (mThroughputListenerList.isEmpty()) {
461 nativeProvideThroughputObservations(mUrlRequestContextAdapte r, false); 471 synchronized (mLock) {
472 checkHaveAdapter();
473 nativeProvideThroughputObservations(mUrlRequestContextAd apter, false);
474 }
462 } 475 }
463 } 476 }
464 } 477 }
465 } 478 }
466 479
467 @Override 480 @Override
468 public void addRequestFinishedListener(RequestFinishedInfo.Listener listener ) { 481 public void addRequestFinishedListener(RequestFinishedInfo.Listener listener ) {
469 synchronized (mFinishedListenerLock) { 482 synchronized (mFinishedListenerLock) {
470 mFinishedListenerList.add(listener); 483 mFinishedListenerMap.put(
484 listener, new VersionSafeCallbacks.RequestFinishedInfoListen er(listener));
471 } 485 }
472 } 486 }
473 487
474 @Override 488 @Override
475 public void removeRequestFinishedListener(RequestFinishedInfo.Listener liste ner) { 489 public void removeRequestFinishedListener(RequestFinishedInfo.Listener liste ner) {
476 synchronized (mFinishedListenerLock) { 490 synchronized (mFinishedListenerLock) {
477 mFinishedListenerList.remove(listener); 491 mFinishedListenerMap.remove(listener);
478 } 492 }
479 } 493 }
480 494
481 boolean hasRequestFinishedListener() { 495 boolean hasRequestFinishedListener() {
482 synchronized (mFinishedListenerLock) { 496 synchronized (mFinishedListenerLock) {
483 return !mFinishedListenerList.isEmpty(); 497 return !mFinishedListenerMap.isEmpty();
484 } 498 }
485 } 499 }
486 500
487 @Override 501 @Override
488 public URLConnection openConnection(URL url) { 502 public URLConnection openConnection(URL url) {
489 return openConnection(url, Proxy.NO_PROXY); 503 return openConnection(url, Proxy.NO_PROXY);
490 } 504 }
491 505
492 @Override 506 @Override
493 public URLConnection openConnection(URL url, Proxy proxy) { 507 public URLConnection openConnection(URL url, Proxy proxy) {
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
606 mHttpRttMs = httpRttMs; 620 mHttpRttMs = httpRttMs;
607 mTransportRttMs = transportRttMs; 621 mTransportRttMs = transportRttMs;
608 mDownstreamThroughputKbps = downstreamThroughputKbps; 622 mDownstreamThroughputKbps = downstreamThroughputKbps;
609 } 623 }
610 } 624 }
611 625
612 @SuppressWarnings("unused") 626 @SuppressWarnings("unused")
613 @CalledByNative 627 @CalledByNative
614 private void onRttObservation(final int rttMs, final long whenMs, final int source) { 628 private void onRttObservation(final int rttMs, final long whenMs, final int source) {
615 synchronized (mNetworkQualityLock) { 629 synchronized (mNetworkQualityLock) {
616 for (final NetworkQualityRttListener listener : mRttListenerList) { 630 for (final VersionSafeCallbacks.NetworkQualityRttListenerWrapper lis tener :
631 mRttListenerList) {
617 Runnable task = new Runnable() { 632 Runnable task = new Runnable() {
618 @Override 633 @Override
619 public void run() { 634 public void run() {
620 listener.onRttObservation(rttMs, whenMs, source); 635 listener.onRttObservation(rttMs, whenMs, source);
621 } 636 }
622 }; 637 };
623 postObservationTaskToExecutor(listener.getExecutor(), task); 638 postObservationTaskToExecutor(listener.getExecutor(), task);
624 } 639 }
625 } 640 }
626 } 641 }
627 642
628 @SuppressWarnings("unused") 643 @SuppressWarnings("unused")
629 @CalledByNative 644 @CalledByNative
630 private void onThroughputObservation( 645 private void onThroughputObservation(
631 final int throughputKbps, final long whenMs, final int source) { 646 final int throughputKbps, final long whenMs, final int source) {
632 synchronized (mNetworkQualityLock) { 647 synchronized (mNetworkQualityLock) {
633 for (final NetworkQualityThroughputListener listener : mThroughputLi stenerList) { 648 for (final VersionSafeCallbacks.NetworkQualityThroughputListenerWrap per listener :
649 mThroughputListenerList) {
634 Runnable task = new Runnable() { 650 Runnable task = new Runnable() {
635 @Override 651 @Override
636 public void run() { 652 public void run() {
637 listener.onThroughputObservation(throughputKbps, whenMs, source); 653 listener.onThroughputObservation(throughputKbps, whenMs, source);
638 } 654 }
639 }; 655 };
640 postObservationTaskToExecutor(listener.getExecutor(), task); 656 postObservationTaskToExecutor(listener.getExecutor(), task);
641 } 657 }
642 } 658 }
643 } 659 }
644 660
645 @SuppressWarnings("unused") 661 @SuppressWarnings("unused")
646 @CalledByNative 662 @CalledByNative
647 private void onGetCertVerifierData(String certVerifierData) { 663 private void onGetCertVerifierData(String certVerifierData) {
648 mCertVerifierData = certVerifierData; 664 mCertVerifierData = certVerifierData;
649 mWaitGetCertVerifierDataComplete.open(); 665 mWaitGetCertVerifierDataComplete.open();
650 } 666 }
651 667
652 void reportFinished(final RequestFinishedInfo requestInfo) { 668 void reportFinished(final RequestFinishedInfo requestInfo) {
653 ArrayList<RequestFinishedInfo.Listener> currentListeners; 669 ArrayList<VersionSafeCallbacks.RequestFinishedInfoListener> currentListe ners;
654 synchronized (mFinishedListenerLock) { 670 synchronized (mFinishedListenerLock) {
655 currentListeners = new ArrayList<RequestFinishedInfo.Listener>(mFini shedListenerList); 671 currentListeners = new ArrayList<VersionSafeCallbacks.RequestFinishe dInfoListener>(
672 mFinishedListenerMap.values());
656 } 673 }
657 for (final RequestFinishedInfo.Listener listener : currentListeners) { 674 for (final VersionSafeCallbacks.RequestFinishedInfoListener listener : c urrentListeners) {
658 Runnable task = new Runnable() { 675 Runnable task = new Runnable() {
659 @Override 676 @Override
660 public void run() { 677 public void run() {
661 listener.onRequestFinished(requestInfo); 678 listener.onRequestFinished(requestInfo);
662 } 679 }
663 }; 680 };
664 postObservationTaskToExecutor(listener.getExecutor(), task); 681 postObservationTaskToExecutor(listener.getExecutor(), task);
665 } 682 }
666 } 683 }
667 684
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
722 @NativeClassQualifiedName("CronetURLRequestContextAdapter") 739 @NativeClassQualifiedName("CronetURLRequestContextAdapter")
723 private native void nativeProvideRTTObservations(long nativePtr, boolean sho uld); 740 private native void nativeProvideRTTObservations(long nativePtr, boolean sho uld);
724 741
725 @NativeClassQualifiedName("CronetURLRequestContextAdapter") 742 @NativeClassQualifiedName("CronetURLRequestContextAdapter")
726 private native void nativeProvideThroughputObservations(long nativePtr, bool ean should); 743 private native void nativeProvideThroughputObservations(long nativePtr, bool ean should);
727 744
728 public boolean isNetworkThread(Thread thread) { 745 public boolean isNetworkThread(Thread thread) {
729 return thread == mNetworkThread; 746 return thread == mNetworkThread;
730 } 747 }
731 } 748 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698