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; | |
23 import org.chromium.net.EffectiveConnectionType; | 22 import org.chromium.net.EffectiveConnectionType; |
24 import org.chromium.net.NetworkQualityRttListener; | 23 import org.chromium.net.NetworkQualityRttListener; |
25 import org.chromium.net.NetworkQualityThroughputListener; | 24 import org.chromium.net.NetworkQualityThroughputListener; |
26 import org.chromium.net.RequestFinishedInfo; | 25 import org.chromium.net.RequestFinishedInfo; |
27 import org.chromium.net.UrlRequest; | 26 import org.chromium.net.UrlRequest; |
28 import org.chromium.net.urlconnection.CronetHttpURLConnection; | 27 import org.chromium.net.urlconnection.CronetHttpURLConnection; |
29 import org.chromium.net.urlconnection.CronetURLStreamHandlerFactory; | 28 import org.chromium.net.urlconnection.CronetURLStreamHandlerFactory; |
30 | 29 |
31 import java.net.Proxy; | 30 import java.net.Proxy; |
32 import java.net.URL; | 31 import java.net.URL; |
33 import java.net.URLConnection; | 32 import java.net.URLConnection; |
34 import java.net.URLStreamHandlerFactory; | 33 import java.net.URLStreamHandlerFactory; |
35 import java.util.ArrayList; | 34 import java.util.ArrayList; |
36 import java.util.Collection; | 35 import java.util.Collection; |
37 import java.util.List; | 36 import java.util.List; |
38 import java.util.Map; | 37 import java.util.Map; |
39 import java.util.concurrent.Executor; | 38 import java.util.concurrent.Executor; |
40 import java.util.concurrent.RejectedExecutionException; | 39 import java.util.concurrent.RejectedExecutionException; |
41 import java.util.concurrent.atomic.AtomicInteger; | 40 import java.util.concurrent.atomic.AtomicInteger; |
42 | 41 |
43 import javax.annotation.concurrent.GuardedBy; | 42 import javax.annotation.concurrent.GuardedBy; |
44 | 43 |
45 /** | 44 /** |
46 * CronetEngine using Chromium HTTP stack implementation. | 45 * CronetEngine using Chromium HTTP stack implementation. |
47 */ | 46 */ |
48 @JNINamespace("cronet") | 47 @JNINamespace("cronet") |
49 @UsedByReflection("CronetEngine.java") | 48 @UsedByReflection("CronetEngine.java") |
50 @VisibleForTesting | 49 @VisibleForTesting |
51 public class CronetUrlRequestContext extends CronetEngine { | 50 public class CronetUrlRequestContext extends CronetEngineBase { |
52 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. | 51 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. |
53 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) |
54 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) |
55 static final String LOG_TAG = "ChromiumNetwork"; | 54 static final String LOG_TAG = "ChromiumNetwork"; |
56 | 55 |
57 /** | 56 /** |
58 * Synchronize access to mUrlRequestContextAdapter and shutdown routine. | 57 * Synchronize access to mUrlRequestContextAdapter and shutdown routine. |
59 */ | 58 */ |
60 private final Object mLock = new Object(); | 59 private final Object mLock = new Object(); |
61 private final ConditionVariable mInitCompleted = new ConditionVariable(false
); | 60 private final ConditionVariable mInitCompleted = new ConditionVariable(false
); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 private boolean mIsLogging; | 121 private boolean mIsLogging; |
123 | 122 |
124 /** | 123 /** |
125 * True if a NetLog observer that writes to disk with a bounded amount of sp
ace has been | 124 * True if a NetLog observer that writes to disk with a bounded amount of sp
ace has been |
126 * activated by calling StartNetLogToDisk(). | 125 * activated by calling StartNetLogToDisk(). |
127 */ | 126 */ |
128 @GuardedBy("mLock") | 127 @GuardedBy("mLock") |
129 private boolean mNetLogToDisk; | 128 private boolean mNetLogToDisk; |
130 | 129 |
131 @UsedByReflection("CronetEngine.java") | 130 @UsedByReflection("CronetEngine.java") |
132 public CronetUrlRequestContext(final CronetEngine.Builder builder) { | 131 public CronetUrlRequestContext(final CronetEngineBuilderImpl builder) { |
133 CronetLibraryLoader.ensureInitialized(builder.getContext(), builder); | 132 CronetLibraryLoader.ensureInitialized(builder.getContext(), builder); |
134 nativeSetMinLogLevel(getLoggingLevel()); | 133 nativeSetMinLogLevel(getLoggingLevel()); |
135 synchronized (mLock) { | 134 synchronized (mLock) { |
136 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter( | 135 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter( |
137 createNativeUrlRequestContextConfig(builder.getContext(), bu
ilder)); | 136 createNativeUrlRequestContextConfig(builder.getContext(), bu
ilder)); |
138 if (mUrlRequestContextAdapter == 0) { | 137 if (mUrlRequestContextAdapter == 0) { |
139 throw new NullPointerException("Context Adapter creation failed.
"); | 138 throw new NullPointerException("Context Adapter creation failed.
"); |
140 } | 139 } |
141 mNetworkQualityEstimatorEnabled = builder.networkQualityEstimatorEna
bled(); | 140 mNetworkQualityEstimatorEnabled = builder.networkQualityEstimatorEna
bled(); |
142 } | 141 } |
(...skipping 14 matching lines...) Expand all Loading... |
157 // Run task immediately or post it to the UI thread. | 156 // Run task immediately or post it to the UI thread. |
158 if (Looper.getMainLooper() == Looper.myLooper()) { | 157 if (Looper.getMainLooper() == Looper.myLooper()) { |
159 task.run(); | 158 task.run(); |
160 } else { | 159 } else { |
161 new Handler(Looper.getMainLooper()).post(task); | 160 new Handler(Looper.getMainLooper()).post(task); |
162 } | 161 } |
163 } | 162 } |
164 | 163 |
165 @VisibleForTesting | 164 @VisibleForTesting |
166 public static long createNativeUrlRequestContextConfig( | 165 public static long createNativeUrlRequestContextConfig( |
167 final Context context, CronetEngine.Builder builder) { | 166 final Context context, CronetEngineBuilderImpl builder) { |
168 final long urlRequestContextConfig = nativeCreateRequestContextConfig( | 167 final long urlRequestContextConfig = nativeCreateRequestContextConfig( |
169 builder.getUserAgent(), builder.storagePath(), builder.quicEnabl
ed(), | 168 builder.getUserAgent(), builder.storagePath(), builder.quicEnabl
ed(), |
170 builder.getDefaultQuicUserAgentId(context), builder.http2Enabled
(), | 169 builder.getDefaultQuicUserAgentId(context), builder.http2Enabled
(), |
171 builder.sdchEnabled(), builder.dataReductionProxyKey(), | 170 builder.sdchEnabled(), builder.dataReductionProxyKey(), |
172 builder.dataReductionProxyPrimaryProxy(), builder.dataReductionP
roxyFallbackProxy(), | 171 builder.dataReductionProxyPrimaryProxy(), builder.dataReductionP
roxyFallbackProxy(), |
173 builder.dataReductionProxySecureProxyCheckUrl(), builder.cacheDi
sabled(), | 172 builder.dataReductionProxySecureProxyCheckUrl(), builder.cacheDi
sabled(), |
174 builder.httpCacheMode(), builder.httpCacheMaxSize(), builder.exp
erimentalOptions(), | 173 builder.httpCacheMode(), builder.httpCacheMaxSize(), builder.exp
erimentalOptions(), |
175 builder.mockCertVerifier(), builder.networkQualityEstimatorEnabl
ed(), | 174 builder.mockCertVerifier(), builder.networkQualityEstimatorEnabl
ed(), |
176 builder.publicKeyPinningBypassForLocalTrustAnchorsEnabled(), | 175 builder.publicKeyPinningBypassForLocalTrustAnchorsEnabled(), |
177 builder.certVerifierData()); | 176 builder.certVerifierData()); |
178 for (Builder.QuicHint quicHint : builder.quicHints()) { | 177 for (CronetEngineBuilderImpl.QuicHint quicHint : builder.quicHints()) { |
179 nativeAddQuicHint(urlRequestContextConfig, quicHint.mHost, quicHint.
mPort, | 178 nativeAddQuicHint(urlRequestContextConfig, quicHint.mHost, quicHint.
mPort, |
180 quicHint.mAlternatePort); | 179 quicHint.mAlternatePort); |
181 } | 180 } |
182 for (Builder.Pkp pkp : builder.publicKeyPins()) { | 181 for (CronetEngineBuilderImpl.Pkp pkp : builder.publicKeyPins()) { |
183 nativeAddPkp(urlRequestContextConfig, pkp.mHost, pkp.mHashes, pkp.mI
ncludeSubdomains, | 182 nativeAddPkp(urlRequestContextConfig, pkp.mHost, pkp.mHashes, pkp.mI
ncludeSubdomains, |
184 pkp.mExpirationDate.getTime()); | 183 pkp.mExpirationDate.getTime()); |
185 } | 184 } |
186 return urlRequestContextConfig; | 185 return urlRequestContextConfig; |
187 } | 186 } |
188 | 187 |
189 @Override | 188 @Override |
190 public UrlRequest createRequest(String url, UrlRequest.Callback callback, Ex
ecutor executor, | 189 public BidirectionalStream.Builder newBidirectionalStreamBuilder( |
| 190 String url, BidirectionalStream.Callback callback, Executor executor
) { |
| 191 return new BidirectionalStreamBuilderImpl(url, callback, executor, this)
; |
| 192 } |
| 193 |
| 194 @Override |
| 195 public UrlRequestBase createRequest(String url, UrlRequest.Callback callback
, Executor executor, |
191 int priority, Collection<Object> requestAnnotations, boolean disable
Cache, | 196 int priority, Collection<Object> requestAnnotations, boolean disable
Cache, |
192 boolean disableConnectionMigration, boolean allowDirectExecutor) { | 197 boolean disableConnectionMigration, boolean allowDirectExecutor) { |
193 synchronized (mLock) { | 198 synchronized (mLock) { |
194 checkHaveAdapter(); | 199 checkHaveAdapter(); |
195 boolean metricsCollectionEnabled = false; | 200 boolean metricsCollectionEnabled = false; |
196 synchronized (mFinishedListenerLock) { | 201 synchronized (mFinishedListenerLock) { |
197 metricsCollectionEnabled = !mFinishedListenerList.isEmpty(); | 202 metricsCollectionEnabled = !mFinishedListenerList.isEmpty(); |
198 } | 203 } |
199 return new CronetUrlRequest(this, url, priority, callback, executor,
requestAnnotations, | 204 return new CronetUrlRequest(this, url, priority, callback, executor,
requestAnnotations, |
200 metricsCollectionEnabled, disableCache, disableConnectionMig
ration, | 205 metricsCollectionEnabled, disableCache, disableConnectionMig
ration, |
201 allowDirectExecutor); | 206 allowDirectExecutor); |
202 } | 207 } |
203 } | 208 } |
204 | 209 |
| 210 /** |
| 211 * Creates a {@link BidirectionalStream} object. {@code callback} methods wi
ll |
| 212 * be invoked on {@code executor}. {@code executor} must not run |
| 213 * tasks on the current thread to prevent blocking networking operations |
| 214 * and causing exceptions during shutdown. |
| 215 * |
| 216 * @param url the URL for the stream |
| 217 * @param callback the object whose methods get invoked upon different event
s |
| 218 * @param executor the {@link Executor} on which all callbacks will be calle
d |
| 219 * @param httpMethod the HTTP method to use for the stream |
| 220 * @param requestHeaders the list of request headers |
| 221 * @param priority priority of the stream which should be one of the |
| 222 * {@link BidirectionalStream.Builder#STREAM_PRIORITY_IDLE STREAM_PR
IORITY_*} |
| 223 * values. |
| 224 * @param delayRequestHeadersUntilFirstFlush whether to delay sending reques
t |
| 225 * headers until flush() is called, and try to combine them |
| 226 * with the next data frame. |
| 227 * @return a new stream. |
| 228 */ |
205 @Override | 229 @Override |
206 public BidirectionalStream createBidirectionalStream(String url, | 230 public BidirectionalStream createBidirectionalStream(String url, |
207 BidirectionalStream.Callback callback, Executor executor, String htt
pMethod, | 231 BidirectionalStream.Callback callback, Executor executor, String htt
pMethod, |
208 List<Map.Entry<String, String>> requestHeaders, | 232 List<Map.Entry<String, String>> requestHeaders, |
209 @BidirectionalStream.Builder.StreamPriority int priority, | 233 @BidirectionalStreamBuilderImpl.StreamPriority int priority, |
210 boolean delayRequestHeadersUntilFirstFlush) { | 234 boolean delayRequestHeadersUntilFirstFlush) { |
211 synchronized (mLock) { | 235 synchronized (mLock) { |
212 checkHaveAdapter(); | 236 checkHaveAdapter(); |
213 return new CronetBidirectionalStream(this, url, priority, callback,
executor, | 237 return new CronetBidirectionalStream(this, url, priority, callback,
executor, |
214 httpMethod, requestHeaders, delayRequestHeadersUntilFirstFlu
sh); | 238 httpMethod, requestHeaders, delayRequestHeadersUntilFirstFlu
sh); |
215 } | 239 } |
216 } | 240 } |
217 | 241 |
218 @Override | 242 @Override |
219 public boolean isEnabled() { | 243 public boolean isEnabled() { |
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
634 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 658 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
635 private native void nativeProvideRTTObservations(long nativePtr, boolean sho
uld); | 659 private native void nativeProvideRTTObservations(long nativePtr, boolean sho
uld); |
636 | 660 |
637 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 661 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
638 private native void nativeProvideThroughputObservations(long nativePtr, bool
ean should); | 662 private native void nativeProvideThroughputObservations(long nativePtr, bool
ean should); |
639 | 663 |
640 public boolean isNetworkThread(Thread thread) { | 664 public boolean isNetworkThread(Thread thread) { |
641 return thread == mNetworkThread; | 665 return thread == mNetworkThread; |
642 } | 666 } |
643 } | 667 } |
OLD | NEW |