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