| 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; |  | 
| 8 import android.os.Build; |  | 
| 9 import android.os.ConditionVariable; | 7 import android.os.ConditionVariable; | 
| 10 import android.os.Handler; | 8 import android.os.Handler; | 
| 11 import android.os.Looper; | 9 import android.os.Looper; | 
| 12 import android.os.Process; | 10 import android.os.Process; | 
| 13 import android.util.Log; | 11 import android.util.Log; | 
| 14 | 12 | 
| 15 import org.chromium.base.ObserverList; | 13 import org.chromium.base.ObserverList; | 
| 16 import org.chromium.base.VisibleForTesting; | 14 import org.chromium.base.VisibleForTesting; | 
| 17 import org.chromium.base.annotations.CalledByNative; | 15 import org.chromium.base.annotations.CalledByNative; | 
| 18 import org.chromium.base.annotations.JNINamespace; | 16 import org.chromium.base.annotations.JNINamespace; | 
| 19 import org.chromium.base.annotations.NativeClassQualifiedName; | 17 import org.chromium.base.annotations.NativeClassQualifiedName; | 
| 20 import org.chromium.base.annotations.UsedByReflection; | 18 import org.chromium.base.annotations.UsedByReflection; | 
| 21 import org.chromium.net.BidirectionalStream; | 19 import org.chromium.net.BidirectionalStream; | 
| 22 import org.chromium.net.CronetEngine; |  | 
| 23 import org.chromium.net.EffectiveConnectionType; | 20 import org.chromium.net.EffectiveConnectionType; | 
|  | 21 import org.chromium.net.ExperimentalBidirectionalStream; | 
| 24 import org.chromium.net.NetworkQualityRttListener; | 22 import org.chromium.net.NetworkQualityRttListener; | 
| 25 import org.chromium.net.NetworkQualityThroughputListener; | 23 import org.chromium.net.NetworkQualityThroughputListener; | 
| 26 import org.chromium.net.RequestFinishedInfo; | 24 import org.chromium.net.RequestFinishedInfo; | 
| 27 import org.chromium.net.RttThroughputValues; | 25 import org.chromium.net.RttThroughputValues; | 
| 28 import org.chromium.net.UrlRequest; | 26 import org.chromium.net.UrlRequest; | 
| 29 import org.chromium.net.urlconnection.CronetHttpURLConnection; | 27 import org.chromium.net.urlconnection.CronetHttpURLConnection; | 
| 30 import org.chromium.net.urlconnection.CronetURLStreamHandlerFactory; | 28 import org.chromium.net.urlconnection.CronetURLStreamHandlerFactory; | 
| 31 | 29 | 
| 32 import java.net.Proxy; | 30 import java.net.Proxy; | 
| 33 import java.net.URL; | 31 import java.net.URL; | 
| 34 import java.net.URLConnection; | 32 import java.net.URLConnection; | 
| 35 import java.net.URLStreamHandlerFactory; | 33 import java.net.URLStreamHandlerFactory; | 
| 36 import java.util.ArrayList; | 34 import java.util.ArrayList; | 
| 37 import java.util.Collection; | 35 import java.util.Collection; | 
| 38 import java.util.List; | 36 import java.util.List; | 
| 39 import java.util.Map; | 37 import java.util.Map; | 
| 40 import java.util.concurrent.Executor; | 38 import java.util.concurrent.Executor; | 
| 41 import java.util.concurrent.RejectedExecutionException; | 39 import java.util.concurrent.RejectedExecutionException; | 
| 42 import java.util.concurrent.atomic.AtomicInteger; | 40 import java.util.concurrent.atomic.AtomicInteger; | 
| 43 | 41 | 
| 44 import javax.annotation.concurrent.GuardedBy; | 42 import javax.annotation.concurrent.GuardedBy; | 
| 45 | 43 | 
| 46 /** | 44 /** | 
| 47  * CronetEngine using Chromium HTTP stack implementation. | 45  * CronetEngine using Chromium HTTP stack implementation. | 
| 48  */ | 46  */ | 
| 49 @JNINamespace("cronet") | 47 @JNINamespace("cronet") | 
| 50 @UsedByReflection("CronetEngine.java") | 48 @UsedByReflection("CronetEngine.java") | 
| 51 @VisibleForTesting | 49 @VisibleForTesting | 
| 52 public class CronetUrlRequestContext extends CronetEngine { | 50 public class CronetUrlRequestContext extends CronetEngineBase { | 
| 53     private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. | 51     private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. | 
| 54     private static final int LOG_DEBUG = -1; // LOG(FATAL...INFO), VLOG(1) | 52     private static final int LOG_DEBUG = -1; // LOG(FATAL...INFO), VLOG(1) | 
| 55     private static final int LOG_VERBOSE = -2; // LOG(FATAL...INFO), VLOG(2) | 53     private static final int LOG_VERBOSE = -2; // LOG(FATAL...INFO), VLOG(2) | 
| 56     static final String LOG_TAG = "ChromiumNetwork"; | 54     static final String LOG_TAG = "ChromiumNetwork"; | 
| 57 | 55 | 
| 58     /** | 56     /** | 
| 59      * Synchronize access to mUrlRequestContextAdapter and shutdown routine. | 57      * Synchronize access to mUrlRequestContextAdapter and shutdown routine. | 
| 60      */ | 58      */ | 
| 61     private final Object mLock = new Object(); | 59     private final Object mLock = new Object(); | 
| 62     private final ConditionVariable mInitCompleted = new ConditionVariable(false
     ); | 60     private final ConditionVariable mInitCompleted = new ConditionVariable(false
     ); | 
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 145     private boolean mIsLogging; | 143     private boolean mIsLogging; | 
| 146 | 144 | 
| 147     /** | 145     /** | 
| 148      * True if a NetLog observer that writes to disk with a bounded amount of sp
     ace has been | 146      * True if a NetLog observer that writes to disk with a bounded amount of sp
     ace has been | 
| 149      * activated by calling StartNetLogToDisk(). | 147      * activated by calling StartNetLogToDisk(). | 
| 150      */ | 148      */ | 
| 151     @GuardedBy("mLock") | 149     @GuardedBy("mLock") | 
| 152     private boolean mNetLogToDisk; | 150     private boolean mNetLogToDisk; | 
| 153 | 151 | 
| 154     @UsedByReflection("CronetEngine.java") | 152     @UsedByReflection("CronetEngine.java") | 
| 155     public CronetUrlRequestContext(final CronetEngine.Builder builder) { | 153     public CronetUrlRequestContext(final CronetEngineBuilderImpl builder) { | 
| 156         CronetLibraryLoader.ensureInitialized(builder.getContext(), builder); | 154         CronetLibraryLoader.ensureInitialized(builder.getContext(), builder); | 
| 157         nativeSetMinLogLevel(getLoggingLevel()); | 155         nativeSetMinLogLevel(getLoggingLevel()); | 
| 158         synchronized (mLock) { | 156         synchronized (mLock) { | 
| 159             mUrlRequestContextAdapter = nativeCreateRequestContextAdapter( | 157             mUrlRequestContextAdapter = | 
| 160                     createNativeUrlRequestContextConfig(builder.getContext(), bu
     ilder)); | 158                     nativeCreateRequestContextAdapter(createNativeUrlRequestCont
     extConfig(builder)); | 
| 161             if (mUrlRequestContextAdapter == 0) { | 159             if (mUrlRequestContextAdapter == 0) { | 
| 162                 throw new NullPointerException("Context Adapter creation failed.
     "); | 160                 throw new NullPointerException("Context Adapter creation failed.
     "); | 
| 163             } | 161             } | 
| 164             mNetworkQualityEstimatorEnabled = builder.networkQualityEstimatorEna
     bled(); | 162             mNetworkQualityEstimatorEnabled = builder.networkQualityEstimatorEna
     bled(); | 
| 165         } | 163         } | 
| 166 | 164 | 
| 167         // Init native Chromium URLRequestContext on main UI thread. | 165         // Init native Chromium URLRequestContext on main UI thread. | 
| 168         Runnable task = new Runnable() { | 166         Runnable task = new Runnable() { | 
| 169             @Override | 167             @Override | 
| 170             public void run() { | 168             public void run() { | 
| 171                 CronetLibraryLoader.ensureInitializedOnMainThread(builder.getCon
     text()); | 169                 CronetLibraryLoader.ensureInitializedOnMainThread(builder.getCon
     text()); | 
| 172                 synchronized (mLock) { | 170                 synchronized (mLock) { | 
| 173                     // mUrlRequestContextAdapter is guaranteed to exist until | 171                     // mUrlRequestContextAdapter is guaranteed to exist until | 
| 174                     // initialization on main and network threads completes and | 172                     // initialization on main and network threads completes and | 
| 175                     // initNetworkThread is called back on network thread. | 173                     // initNetworkThread is called back on network thread. | 
| 176                     nativeInitRequestContextOnMainThread(mUrlRequestContextAdapt
     er); | 174                     nativeInitRequestContextOnMainThread(mUrlRequestContextAdapt
     er); | 
| 177                 } | 175                 } | 
| 178             } | 176             } | 
| 179         }; | 177         }; | 
| 180         // Run task immediately or post it to the UI thread. | 178         // Run task immediately or post it to the UI thread. | 
| 181         if (Looper.getMainLooper() == Looper.myLooper()) { | 179         if (Looper.getMainLooper() == Looper.myLooper()) { | 
| 182             task.run(); | 180             task.run(); | 
| 183         } else { | 181         } else { | 
| 184             new Handler(Looper.getMainLooper()).post(task); | 182             new Handler(Looper.getMainLooper()).post(task); | 
| 185         } | 183         } | 
| 186     } | 184     } | 
| 187 | 185 | 
| 188     @VisibleForTesting | 186     @VisibleForTesting | 
| 189     public static long createNativeUrlRequestContextConfig( | 187     public static long createNativeUrlRequestContextConfig(CronetEngineBuilderIm
     pl builder) { | 
| 190             final Context context, CronetEngine.Builder builder) { |  | 
| 191         final long urlRequestContextConfig = nativeCreateRequestContextConfig( | 188         final long urlRequestContextConfig = nativeCreateRequestContextConfig( | 
| 192                 builder.getUserAgent(), builder.storagePath(), builder.quicEnabl
     ed(), | 189                 builder.getUserAgent(), builder.storagePath(), builder.quicEnabl
     ed(), | 
| 193                 builder.getDefaultQuicUserAgentId(context), builder.http2Enabled
     (), | 190                 builder.getDefaultQuicUserAgentId(), builder.http2Enabled(), bui
     lder.sdchEnabled(), | 
| 194                 builder.sdchEnabled(), builder.dataReductionProxyKey(), | 191                 builder.dataReductionProxyKey(), builder.dataReductionProxyPrima
     ryProxy(), | 
| 195                 builder.dataReductionProxyPrimaryProxy(), builder.dataReductionP
     roxyFallbackProxy(), | 192                 builder.dataReductionProxyFallbackProxy(), | 
| 196                 builder.dataReductionProxySecureProxyCheckUrl(), builder.cacheDi
     sabled(), | 193                 builder.dataReductionProxySecureProxyCheckUrl(), builder.cacheDi
     sabled(), | 
| 197                 builder.httpCacheMode(), builder.httpCacheMaxSize(), builder.exp
     erimentalOptions(), | 194                 builder.httpCacheMode(), builder.httpCacheMaxSize(), builder.exp
     erimentalOptions(), | 
| 198                 builder.mockCertVerifier(), builder.networkQualityEstimatorEnabl
     ed(), | 195                 builder.mockCertVerifier(), builder.networkQualityEstimatorEnabl
     ed(), | 
| 199                 builder.publicKeyPinningBypassForLocalTrustAnchorsEnabled(), | 196                 builder.publicKeyPinningBypassForLocalTrustAnchorsEnabled(), | 
| 200                 builder.certVerifierData()); | 197                 builder.certVerifierData()); | 
| 201         for (Builder.QuicHint quicHint : builder.quicHints()) { | 198         for (CronetEngineBuilderImpl.QuicHint quicHint : builder.quicHints()) { | 
| 202             nativeAddQuicHint(urlRequestContextConfig, quicHint.mHost, quicHint.
     mPort, | 199             nativeAddQuicHint(urlRequestContextConfig, quicHint.mHost, quicHint.
     mPort, | 
| 203                     quicHint.mAlternatePort); | 200                     quicHint.mAlternatePort); | 
| 204         } | 201         } | 
| 205         for (Builder.Pkp pkp : builder.publicKeyPins()) { | 202         for (CronetEngineBuilderImpl.Pkp pkp : builder.publicKeyPins()) { | 
| 206             nativeAddPkp(urlRequestContextConfig, pkp.mHost, pkp.mHashes, pkp.mI
     ncludeSubdomains, | 203             nativeAddPkp(urlRequestContextConfig, pkp.mHost, pkp.mHashes, pkp.mI
     ncludeSubdomains, | 
| 207                     pkp.mExpirationDate.getTime()); | 204                     pkp.mExpirationDate.getTime()); | 
| 208         } | 205         } | 
| 209         return urlRequestContextConfig; | 206         return urlRequestContextConfig; | 
| 210     } | 207     } | 
| 211 | 208 | 
| 212     @Override | 209     @Override | 
| 213     public UrlRequest createRequest(String url, UrlRequest.Callback callback, Ex
     ecutor executor, | 210     public ExperimentalBidirectionalStream.Builder newBidirectionalStreamBuilder
     ( | 
|  | 211             String url, BidirectionalStream.Callback callback, Executor executor
     ) { | 
|  | 212         return new BidirectionalStreamBuilderImpl(url, callback, executor, this)
     ; | 
|  | 213     } | 
|  | 214 | 
|  | 215     @Override | 
|  | 216     public UrlRequestBase createRequest(String url, UrlRequest.Callback callback
     , Executor executor, | 
| 214             int priority, Collection<Object> requestAnnotations, boolean disable
     Cache, | 217             int priority, Collection<Object> requestAnnotations, boolean disable
     Cache, | 
| 215             boolean disableConnectionMigration, boolean allowDirectExecutor) { | 218             boolean disableConnectionMigration, boolean allowDirectExecutor) { | 
| 216         synchronized (mLock) { | 219         synchronized (mLock) { | 
| 217             checkHaveAdapter(); | 220             checkHaveAdapter(); | 
| 218             return new CronetUrlRequest(this, url, priority, callback, executor,
      requestAnnotations, | 221             return new CronetUrlRequest(this, url, priority, callback, executor,
      requestAnnotations, | 
| 219                     disableCache, disableConnectionMigration, allowDirectExecuto
     r); | 222                     disableCache, disableConnectionMigration, allowDirectExecuto
     r); | 
| 220         } | 223         } | 
| 221     } | 224     } | 
| 222 | 225 | 
| 223     @Override | 226     @Override | 
| 224     public BidirectionalStream createBidirectionalStream(String url, | 227     protected ExperimentalBidirectionalStream createBidirectionalStream(String u
     rl, | 
| 225             BidirectionalStream.Callback callback, Executor executor, String htt
     pMethod, | 228             BidirectionalStream.Callback callback, Executor executor, String htt
     pMethod, | 
| 226             List<Map.Entry<String, String>> requestHeaders, | 229             List<Map.Entry<String, String>> requestHeaders, @StreamPriority int 
     priority, | 
| 227             @BidirectionalStream.Builder.StreamPriority int priority, |  | 
| 228             boolean delayRequestHeadersUntilFirstFlush, Collection<Object> reque
     stAnnotations) { | 230             boolean delayRequestHeadersUntilFirstFlush, Collection<Object> reque
     stAnnotations) { | 
| 229         synchronized (mLock) { | 231         synchronized (mLock) { | 
| 230             checkHaveAdapter(); | 232             checkHaveAdapter(); | 
| 231             return new CronetBidirectionalStream(this, url, priority, callback, 
     executor, | 233             return new CronetBidirectionalStream(this, url, priority, callback, 
     executor, | 
| 232                     httpMethod, requestHeaders, delayRequestHeadersUntilFirstFlu
     sh, | 234                     httpMethod, requestHeaders, delayRequestHeadersUntilFirstFlu
     sh, | 
| 233                     requestAnnotations); | 235                     requestAnnotations); | 
| 234         } | 236         } | 
| 235     } | 237     } | 
| 236 | 238 | 
| 237     @Override | 239     @Override | 
| 238     public boolean isEnabled() { |  | 
| 239         return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH; |  | 
| 240     } |  | 
| 241 |  | 
| 242     @Override |  | 
| 243     public String getVersionString() { | 240     public String getVersionString() { | 
| 244         return "Cronet/" + ImplVersion.getVersion(); | 241         return "Cronet/" + ImplVersion.getVersion(); | 
| 245     } | 242     } | 
| 246 | 243 | 
| 247     @Override | 244     @Override | 
| 248     public void shutdown() { | 245     public void shutdown() { | 
| 249         synchronized (mLock) { | 246         synchronized (mLock) { | 
| 250             checkHaveAdapter(); | 247             checkHaveAdapter(); | 
| 251             if (mActiveRequestCount.get() != 0) { | 248             if (mActiveRequestCount.get() != 0) { | 
| 252                 throw new IllegalStateException("Cannot shutdown with active req
     uests."); | 249                 throw new IllegalStateException("Cannot shutdown with active req
     uests."); | 
| (...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 698     @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 695     @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 
| 699     private native void nativeProvideRTTObservations(long nativePtr, boolean sho
     uld); | 696     private native void nativeProvideRTTObservations(long nativePtr, boolean sho
     uld); | 
| 700 | 697 | 
| 701     @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 698     @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 
| 702     private native void nativeProvideThroughputObservations(long nativePtr, bool
     ean should); | 699     private native void nativeProvideThroughputObservations(long nativePtr, bool
     ean should); | 
| 703 | 700 | 
| 704     public boolean isNetworkThread(Thread thread) { | 701     public boolean isNetworkThread(Thread thread) { | 
| 705         return thread == mNetworkThread; | 702         return thread == mNetworkThread; | 
| 706     } | 703     } | 
| 707 } | 704 } | 
| OLD | NEW | 
|---|