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

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

Issue 2283243002: Allow direct executors in cronet. (Closed)
Patch Set: Improve thread checking Created 4 years, 3 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; 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;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 );
62 private final AtomicInteger mActiveRequestCount = new AtomicInteger(0); 62 private final AtomicInteger mActiveRequestCount = new AtomicInteger(0);
63 63
64 @GuardedBy("mLock")
64 private long mUrlRequestContextAdapter = 0; 65 private long mUrlRequestContextAdapter = 0;
66 /**
67 * This field is accessed without synchronization, but only for the purposes of reference
68 * equality comparison with other threads. If such a comparison is performed on the network
69 * thread, then there is a happens-before edge between the write of this fie ld and the
70 * subsequent read; if it's performed on another thread, then observing a va lue of null won't
71 * change the result of the comparison.
72 */
65 private Thread mNetworkThread; 73 private Thread mNetworkThread;
66 74
67 private boolean mNetworkQualityEstimatorEnabled; 75 private boolean mNetworkQualityEstimatorEnabled;
68 76
69 /** 77 /**
70 * Locks operations on network quality listeners, because listener 78 * Locks operations on network quality listeners, because listener
71 * addition and removal may occur on a different thread from notification. 79 * addition and removal may occur on a different thread from notification.
72 */ 80 */
73 private final Object mNetworkQualityLock = new Object(); 81 private final Object mNetworkQualityLock = new Object();
74 82
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 for (Builder.Pkp pkp : builder.publicKeyPins()) { 183 for (Builder.Pkp pkp : builder.publicKeyPins()) {
176 nativeAddPkp(urlRequestContextConfig, pkp.mHost, pkp.mHashes, pkp.mI ncludeSubdomains, 184 nativeAddPkp(urlRequestContextConfig, pkp.mHost, pkp.mHashes, pkp.mI ncludeSubdomains,
177 pkp.mExpirationDate.getTime()); 185 pkp.mExpirationDate.getTime());
178 } 186 }
179 return urlRequestContextConfig; 187 return urlRequestContextConfig;
180 } 188 }
181 189
182 @Override 190 @Override
183 public UrlRequest createRequest(String url, UrlRequest.Callback callback, Ex ecutor executor, 191 public UrlRequest createRequest(String url, UrlRequest.Callback callback, Ex ecutor executor,
184 int priority, Collection<Object> requestAnnotations, boolean disable Cache, 192 int priority, Collection<Object> requestAnnotations, boolean disable Cache,
185 boolean disableConnectionMigration) { 193 boolean disableConnectionMigration, boolean allowDirectExecutor) {
186 synchronized (mLock) { 194 synchronized (mLock) {
187 checkHaveAdapter(); 195 checkHaveAdapter();
188 boolean metricsCollectionEnabled = false; 196 boolean metricsCollectionEnabled = false;
189 synchronized (mFinishedListenerLock) { 197 synchronized (mFinishedListenerLock) {
190 metricsCollectionEnabled = !mFinishedListenerList.isEmpty(); 198 metricsCollectionEnabled = !mFinishedListenerList.isEmpty();
191 } 199 }
192 return new CronetUrlRequest(this, url, priority, callback, executor, requestAnnotations, 200 return new CronetUrlRequest(this, url, priority, callback, executor, requestAnnotations,
193 metricsCollectionEnabled, disableCache, disableConnectionMig ration); 201 metricsCollectionEnabled, disableCache, disableConnectionMig ration,
202 allowDirectExecutor);
194 } 203 }
195 } 204 }
196 205
197 @Override 206 @Override
198 public BidirectionalStream createBidirectionalStream(String url, 207 public BidirectionalStream createBidirectionalStream(String url,
199 BidirectionalStream.Callback callback, Executor executor, String htt pMethod, 208 BidirectionalStream.Callback callback, Executor executor, String htt pMethod,
200 List<Map.Entry<String, String>> requestHeaders, 209 List<Map.Entry<String, String>> requestHeaders,
201 @BidirectionalStream.Builder.StreamPriority int priority, 210 @BidirectionalStream.Builder.StreamPriority int priority,
202 boolean delayRequestHeadersUntilFirstFlush) { 211 boolean delayRequestHeadersUntilFirstFlush) {
203 synchronized (mLock) { 212 synchronized (mLock) {
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 public byte[] getGlobalMetricsDeltas() { 322 public byte[] getGlobalMetricsDeltas() {
314 return nativeGetHistogramDeltas(); 323 return nativeGetHistogramDeltas();
315 } 324 }
316 325
317 @Override 326 @Override
318 public int getEffectiveConnectionType() { 327 public int getEffectiveConnectionType() {
319 if (!mNetworkQualityEstimatorEnabled) { 328 if (!mNetworkQualityEstimatorEnabled) {
320 throw new IllegalStateException("Network quality estimator must be e nabled"); 329 throw new IllegalStateException("Network quality estimator must be e nabled");
321 } 330 }
322 synchronized (mNetworkQualityLock) { 331 synchronized (mNetworkQualityLock) {
323 checkHaveAdapter(); 332 synchronized (mLock) {
333 checkHaveAdapter();
334 }
324 return mEffectiveConnectionType; 335 return mEffectiveConnectionType;
325 } 336 }
326 } 337 }
327 338
328 @VisibleForTesting 339 @VisibleForTesting
329 @Override 340 @Override
330 public void configureNetworkQualityEstimatorForTesting( 341 public void configureNetworkQualityEstimatorForTesting(
331 boolean useLocalHostRequests, boolean useSmallerResponses) { 342 boolean useLocalHostRequests, boolean useSmallerResponses) {
332 if (!mNetworkQualityEstimatorEnabled) { 343 if (!mNetworkQualityEstimatorEnabled) {
333 throw new IllegalStateException("Network quality estimator must be e nabled"); 344 throw new IllegalStateException("Network quality estimator must be e nabled");
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
456 } 467 }
457 468
458 @VisibleForTesting 469 @VisibleForTesting
459 public long getUrlRequestContextAdapter() { 470 public long getUrlRequestContextAdapter() {
460 synchronized (mLock) { 471 synchronized (mLock) {
461 checkHaveAdapter(); 472 checkHaveAdapter();
462 return mUrlRequestContextAdapter; 473 return mUrlRequestContextAdapter;
463 } 474 }
464 } 475 }
465 476
477 @GuardedBy("mLock")
466 private void checkHaveAdapter() throws IllegalStateException { 478 private void checkHaveAdapter() throws IllegalStateException {
467 if (!haveRequestContextAdapter()) { 479 if (!haveRequestContextAdapter()) {
468 throw new IllegalStateException("Engine is shut down."); 480 throw new IllegalStateException("Engine is shut down.");
469 } 481 }
470 } 482 }
471 483
484 @GuardedBy("mLock")
472 private boolean haveRequestContextAdapter() { 485 private boolean haveRequestContextAdapter() {
473 return mUrlRequestContextAdapter != 0; 486 return mUrlRequestContextAdapter != 0;
474 } 487 }
475 488
476 /** 489 /**
477 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and 490 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and
478 * {@link #LOG_VERBOSE}. 491 * {@link #LOG_VERBOSE}.
479 */ 492 */
480 private int getLoggingLevel() { 493 private int getLoggingLevel() {
481 int loggingLevel; 494 int loggingLevel;
482 if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) { 495 if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) {
483 loggingLevel = LOG_VERBOSE; 496 loggingLevel = LOG_VERBOSE;
484 } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) { 497 } else if (Log.isLoggable(LOG_TAG, Log.DEBUG)) {
485 loggingLevel = LOG_DEBUG; 498 loggingLevel = LOG_DEBUG;
486 } else { 499 } else {
487 loggingLevel = LOG_NONE; 500 loggingLevel = LOG_NONE;
488 } 501 }
489 return loggingLevel; 502 return loggingLevel;
490 } 503 }
491 504
492 @SuppressWarnings("unused") 505 @SuppressWarnings("unused")
493 @CalledByNative 506 @CalledByNative
494 private void initNetworkThread() { 507 private void initNetworkThread() {
495 synchronized (mLock) { 508 mNetworkThread = Thread.currentThread();
496 mNetworkThread = Thread.currentThread(); 509 mInitCompleted.open();
497 mInitCompleted.open();
498 }
499 Thread.currentThread().setName("ChromiumNet"); 510 Thread.currentThread().setName("ChromiumNet");
500 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); 511 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
501 } 512 }
502 513
503 @SuppressWarnings("unused") 514 @SuppressWarnings("unused")
504 @CalledByNative 515 @CalledByNative
505 private void onEffectiveConnectionTypeChanged(int effectiveConnectionType) { 516 private void onEffectiveConnectionTypeChanged(int effectiveConnectionType) {
506 synchronized (mNetworkQualityLock) { 517 synchronized (mNetworkQualityLock) {
507 // Convert the enum returned by the network quality estimator to an enum of type 518 // Convert the enum returned by the network quality estimator to an enum of type
508 // EffectiveConnectionType. 519 // EffectiveConnectionType.
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
619 630
620 @NativeClassQualifiedName("CronetURLRequestContextAdapter") 631 @NativeClassQualifiedName("CronetURLRequestContextAdapter")
621 private native void nativeConfigureNetworkQualityEstimatorForTesting( 632 private native void nativeConfigureNetworkQualityEstimatorForTesting(
622 long nativePtr, boolean useLocalHostRequests, boolean useSmallerResp onses); 633 long nativePtr, boolean useLocalHostRequests, boolean useSmallerResp onses);
623 634
624 @NativeClassQualifiedName("CronetURLRequestContextAdapter") 635 @NativeClassQualifiedName("CronetURLRequestContextAdapter")
625 private native void nativeProvideRTTObservations(long nativePtr, boolean sho uld); 636 private native void nativeProvideRTTObservations(long nativePtr, boolean sho uld);
626 637
627 @NativeClassQualifiedName("CronetURLRequestContextAdapter") 638 @NativeClassQualifiedName("CronetURLRequestContextAdapter")
628 private native void nativeProvideThroughputObservations(long nativePtr, bool ean should); 639 private native void nativeProvideThroughputObservations(long nativePtr, bool ean should);
640
641 public boolean isNetworkThread(Thread thread) {
642 return thread == mNetworkThread;
643 }
629 } 644 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698