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

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

Issue 2339223002: Cronet API Refactoring (Closed)
Patch Set: Rebase + conflict resolution Created 4 years, 2 months 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.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.UrlRequest; 25 import org.chromium.net.UrlRequest;
28 import org.chromium.net.urlconnection.CronetHttpURLConnection; 26 import org.chromium.net.urlconnection.CronetHttpURLConnection;
29 import org.chromium.net.urlconnection.CronetURLStreamHandlerFactory; 27 import org.chromium.net.urlconnection.CronetURLStreamHandlerFactory;
30 28
31 import java.net.Proxy; 29 import java.net.Proxy;
32 import java.net.URL; 30 import java.net.URL;
33 import java.net.URLConnection; 31 import java.net.URLConnection;
34 import java.net.URLStreamHandlerFactory; 32 import java.net.URLStreamHandlerFactory;
35 import java.util.ArrayList; 33 import java.util.ArrayList;
36 import java.util.Collection; 34 import java.util.Collection;
37 import java.util.List; 35 import java.util.List;
38 import java.util.Map; 36 import java.util.Map;
39 import java.util.concurrent.Executor; 37 import java.util.concurrent.Executor;
40 import java.util.concurrent.RejectedExecutionException; 38 import java.util.concurrent.RejectedExecutionException;
41 import java.util.concurrent.atomic.AtomicInteger; 39 import java.util.concurrent.atomic.AtomicInteger;
42 40
43 import javax.annotation.concurrent.GuardedBy; 41 import javax.annotation.concurrent.GuardedBy;
44 42
45 /** 43 /**
46 * CronetEngine using Chromium HTTP stack implementation. 44 * CronetEngine using Chromium HTTP stack implementation.
47 */ 45 */
48 @JNINamespace("cronet") 46 @JNINamespace("cronet")
49 @UsedByReflection("CronetEngine.java") 47 @UsedByReflection("CronetEngine.java")
50 @VisibleForTesting 48 @VisibleForTesting
51 public class CronetUrlRequestContext extends CronetEngine { 49 public class CronetUrlRequestContext extends CronetEngineBase {
52 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. 50 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG.
53 private static final int LOG_DEBUG = -1; // LOG(FATAL...INFO), VLOG(1) 51 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) 52 private static final int LOG_VERBOSE = -2; // LOG(FATAL...INFO), VLOG(2)
55 static final String LOG_TAG = "ChromiumNetwork"; 53 static final String LOG_TAG = "ChromiumNetwork";
56 54
57 /** 55 /**
58 * Synchronize access to mUrlRequestContextAdapter and shutdown routine. 56 * Synchronize access to mUrlRequestContextAdapter and shutdown routine.
59 */ 57 */
60 private final Object mLock = new Object(); 58 private final Object mLock = new Object();
61 private final ConditionVariable mInitCompleted = new ConditionVariable(false ); 59 private final ConditionVariable mInitCompleted = new ConditionVariable(false );
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 private boolean mIsLogging; 121 private boolean mIsLogging;
124 122
125 /** 123 /**
126 * 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
127 * activated by calling StartNetLogToDisk(). 125 * activated by calling StartNetLogToDisk().
128 */ 126 */
129 @GuardedBy("mLock") 127 @GuardedBy("mLock")
130 private boolean mNetLogToDisk; 128 private boolean mNetLogToDisk;
131 129
132 @UsedByReflection("CronetEngine.java") 130 @UsedByReflection("CronetEngine.java")
133 public CronetUrlRequestContext(final CronetEngine.Builder builder) { 131 public CronetUrlRequestContext(final CronetEngineBuilderImpl builder) {
134 CronetLibraryLoader.ensureInitialized(builder.getContext(), builder); 132 CronetLibraryLoader.ensureInitialized(builder.getContext(), builder);
135 nativeSetMinLogLevel(getLoggingLevel()); 133 nativeSetMinLogLevel(getLoggingLevel());
136 synchronized (mLock) { 134 synchronized (mLock) {
137 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter( 135 mUrlRequestContextAdapter =
138 createNativeUrlRequestContextConfig(builder.getContext(), bu ilder)); 136 nativeCreateRequestContextAdapter(createNativeUrlRequestCont extConfig(builder));
139 if (mUrlRequestContextAdapter == 0) { 137 if (mUrlRequestContextAdapter == 0) {
140 throw new NullPointerException("Context Adapter creation failed. "); 138 throw new NullPointerException("Context Adapter creation failed. ");
141 } 139 }
142 mNetworkQualityEstimatorEnabled = builder.networkQualityEstimatorEna bled(); 140 mNetworkQualityEstimatorEnabled = builder.networkQualityEstimatorEna bled();
143 } 141 }
144 142
145 // Init native Chromium URLRequestContext on main UI thread. 143 // Init native Chromium URLRequestContext on main UI thread.
146 Runnable task = new Runnable() { 144 Runnable task = new Runnable() {
147 @Override 145 @Override
148 public void run() { 146 public void run() {
149 CronetLibraryLoader.ensureInitializedOnMainThread(builder.getCon text()); 147 CronetLibraryLoader.ensureInitializedOnMainThread(builder.getCon text());
150 synchronized (mLock) { 148 synchronized (mLock) {
151 // mUrlRequestContextAdapter is guaranteed to exist until 149 // mUrlRequestContextAdapter is guaranteed to exist until
152 // initialization on main and network threads completes and 150 // initialization on main and network threads completes and
153 // initNetworkThread is called back on network thread. 151 // initNetworkThread is called back on network thread.
154 nativeInitRequestContextOnMainThread(mUrlRequestContextAdapt er); 152 nativeInitRequestContextOnMainThread(mUrlRequestContextAdapt er);
155 } 153 }
156 } 154 }
157 }; 155 };
158 // Run task immediately or post it to the UI thread. 156 // Run task immediately or post it to the UI thread.
159 if (Looper.getMainLooper() == Looper.myLooper()) { 157 if (Looper.getMainLooper() == Looper.myLooper()) {
160 task.run(); 158 task.run();
161 } else { 159 } else {
162 new Handler(Looper.getMainLooper()).post(task); 160 new Handler(Looper.getMainLooper()).post(task);
163 } 161 }
164 } 162 }
165 163
166 @VisibleForTesting 164 @VisibleForTesting
167 public static long createNativeUrlRequestContextConfig( 165 public static long createNativeUrlRequestContextConfig(CronetEngineBuilderIm pl builder) {
168 final Context context, CronetEngine.Builder builder) {
169 final long urlRequestContextConfig = nativeCreateRequestContextConfig( 166 final long urlRequestContextConfig = nativeCreateRequestContextConfig(
170 builder.getUserAgent(), builder.storagePath(), builder.quicEnabl ed(), 167 builder.getUserAgent(), builder.storagePath(), builder.quicEnabl ed(),
171 builder.getDefaultQuicUserAgentId(context), builder.http2Enabled (), 168 builder.getDefaultQuicUserAgentId(), builder.http2Enabled(), bui lder.sdchEnabled(),
172 builder.sdchEnabled(), builder.dataReductionProxyKey(), 169 builder.dataReductionProxyKey(), builder.dataReductionProxyPrima ryProxy(),
173 builder.dataReductionProxyPrimaryProxy(), builder.dataReductionP roxyFallbackProxy(), 170 builder.dataReductionProxyFallbackProxy(),
174 builder.dataReductionProxySecureProxyCheckUrl(), builder.cacheDi sabled(), 171 builder.dataReductionProxySecureProxyCheckUrl(), builder.cacheDi sabled(),
175 builder.httpCacheMode(), builder.httpCacheMaxSize(), builder.exp erimentalOptions(), 172 builder.httpCacheMode(), builder.httpCacheMaxSize(), builder.exp erimentalOptions(),
176 builder.mockCertVerifier(), builder.networkQualityEstimatorEnabl ed(), 173 builder.mockCertVerifier(), builder.networkQualityEstimatorEnabl ed(),
177 builder.publicKeyPinningBypassForLocalTrustAnchorsEnabled(), 174 builder.publicKeyPinningBypassForLocalTrustAnchorsEnabled(),
178 builder.certVerifierData()); 175 builder.certVerifierData());
179 for (Builder.QuicHint quicHint : builder.quicHints()) { 176 for (CronetEngineBuilderImpl.QuicHint quicHint : builder.quicHints()) {
180 nativeAddQuicHint(urlRequestContextConfig, quicHint.mHost, quicHint. mPort, 177 nativeAddQuicHint(urlRequestContextConfig, quicHint.mHost, quicHint. mPort,
181 quicHint.mAlternatePort); 178 quicHint.mAlternatePort);
182 } 179 }
183 for (Builder.Pkp pkp : builder.publicKeyPins()) { 180 for (CronetEngineBuilderImpl.Pkp pkp : builder.publicKeyPins()) {
184 nativeAddPkp(urlRequestContextConfig, pkp.mHost, pkp.mHashes, pkp.mI ncludeSubdomains, 181 nativeAddPkp(urlRequestContextConfig, pkp.mHost, pkp.mHashes, pkp.mI ncludeSubdomains,
185 pkp.mExpirationDate.getTime()); 182 pkp.mExpirationDate.getTime());
186 } 183 }
187 return urlRequestContextConfig; 184 return urlRequestContextConfig;
188 } 185 }
189 186
190 @Override 187 @Override
191 public UrlRequest createRequest(String url, UrlRequest.Callback callback, Ex ecutor executor, 188 public ExperimentalBidirectionalStream.Builder newBidirectionalStreamBuilder (
189 String url, BidirectionalStream.Callback callback, Executor executor ) {
190 return new BidirectionalStreamBuilderImpl(url, callback, executor, this) ;
191 }
192
193 @Override
194 public UrlRequestBase createRequest(String url, UrlRequest.Callback callback , Executor executor,
192 int priority, Collection<Object> requestAnnotations, boolean disable Cache, 195 int priority, Collection<Object> requestAnnotations, boolean disable Cache,
193 boolean disableConnectionMigration, boolean allowDirectExecutor) { 196 boolean disableConnectionMigration, boolean allowDirectExecutor) {
194 synchronized (mLock) { 197 synchronized (mLock) {
195 checkHaveAdapter(); 198 checkHaveAdapter();
196 return new CronetUrlRequest(this, url, priority, callback, executor, requestAnnotations, 199 return new CronetUrlRequest(this, url, priority, callback, executor, requestAnnotations,
197 disableCache, disableConnectionMigration, allowDirectExecuto r); 200 disableCache, disableConnectionMigration, allowDirectExecuto r);
198 } 201 }
199 } 202 }
200 203
201 @Override 204 @Override
202 public BidirectionalStream createBidirectionalStream(String url, 205 protected ExperimentalBidirectionalStream createBidirectionalStream(String u rl,
203 BidirectionalStream.Callback callback, Executor executor, String htt pMethod, 206 BidirectionalStream.Callback callback, Executor executor, String htt pMethod,
204 List<Map.Entry<String, String>> requestHeaders, 207 List<Map.Entry<String, String>> requestHeaders,
205 @BidirectionalStream.Builder.StreamPriority int priority, 208 @BidirectionalStreamBuilderImpl.StreamPriority int priority,
206 boolean delayRequestHeadersUntilFirstFlush, Collection<Object> reque stAnnotations) { 209 boolean delayRequestHeadersUntilFirstFlush, Collection<Object> reque stAnnotations) {
207 synchronized (mLock) { 210 synchronized (mLock) {
208 checkHaveAdapter(); 211 checkHaveAdapter();
209 return new CronetBidirectionalStream(this, url, priority, callback, executor, 212 return new CronetBidirectionalStream(this, url, priority, callback, executor,
210 httpMethod, requestHeaders, delayRequestHeadersUntilFirstFlu sh, 213 httpMethod, requestHeaders, delayRequestHeadersUntilFirstFlu sh,
211 requestAnnotations); 214 requestAnnotations);
212 } 215 }
213 } 216 }
214 217
215 @Override 218 @Override
216 public boolean isEnabled() {
217 return Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
218 }
219
220 @Override
221 public String getVersionString() { 219 public String getVersionString() {
222 return "Cronet/" + ImplVersion.getVersion(); 220 return "Cronet/" + ImplVersion.getVersion();
223 } 221 }
224 222
225 @Override 223 @Override
226 public void shutdown() { 224 public void shutdown() {
227 synchronized (mLock) { 225 synchronized (mLock) {
228 checkHaveAdapter(); 226 checkHaveAdapter();
229 if (mActiveRequestCount.get() != 0) { 227 if (mActiveRequestCount.get() != 0) {
230 throw new IllegalStateException("Cannot shutdown with active req uests."); 228 throw new IllegalStateException("Cannot shutdown with active req uests.");
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after
638 @NativeClassQualifiedName("CronetURLRequestContextAdapter") 636 @NativeClassQualifiedName("CronetURLRequestContextAdapter")
639 private native void nativeProvideRTTObservations(long nativePtr, boolean sho uld); 637 private native void nativeProvideRTTObservations(long nativePtr, boolean sho uld);
640 638
641 @NativeClassQualifiedName("CronetURLRequestContextAdapter") 639 @NativeClassQualifiedName("CronetURLRequestContextAdapter")
642 private native void nativeProvideThroughputObservations(long nativePtr, bool ean should); 640 private native void nativeProvideThroughputObservations(long nativePtr, bool ean should);
643 641
644 public boolean isNetworkThread(Thread thread) { 642 public boolean isNetworkThread(Thread thread) {
645 return thread == mNetworkThread; 643 return thread == mNetworkThread;
646 } 644 }
647 } 645 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698