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.os.ConditionVariable; | 7 import android.os.ConditionVariable; |
8 import android.os.Handler; | |
9 import android.os.Looper; | |
10 import android.os.Process; | 8 import android.os.Process; |
11 | 9 |
12 import org.chromium.base.Log; | 10 import org.chromium.base.Log; |
13 import org.chromium.base.ObserverList; | 11 import org.chromium.base.ObserverList; |
14 import org.chromium.base.VisibleForTesting; | 12 import org.chromium.base.VisibleForTesting; |
15 import org.chromium.base.annotations.CalledByNative; | 13 import org.chromium.base.annotations.CalledByNative; |
16 import org.chromium.base.annotations.JNINamespace; | 14 import org.chromium.base.annotations.JNINamespace; |
17 import org.chromium.base.annotations.NativeClassQualifiedName; | 15 import org.chromium.base.annotations.NativeClassQualifiedName; |
18 import org.chromium.base.annotations.UsedByReflection; | 16 import org.chromium.base.annotations.UsedByReflection; |
19 import org.chromium.net.BidirectionalStream; | 17 import org.chromium.net.BidirectionalStream; |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 nativeSetMinLogLevel(getLoggingLevel()); | 152 nativeSetMinLogLevel(getLoggingLevel()); |
155 synchronized (mLock) { | 153 synchronized (mLock) { |
156 mUrlRequestContextAdapter = | 154 mUrlRequestContextAdapter = |
157 nativeCreateRequestContextAdapter(createNativeUrlRequestCont
extConfig(builder)); | 155 nativeCreateRequestContextAdapter(createNativeUrlRequestCont
extConfig(builder)); |
158 if (mUrlRequestContextAdapter == 0) { | 156 if (mUrlRequestContextAdapter == 0) { |
159 throw new NullPointerException("Context Adapter creation failed.
"); | 157 throw new NullPointerException("Context Adapter creation failed.
"); |
160 } | 158 } |
161 mNetworkQualityEstimatorEnabled = builder.networkQualityEstimatorEna
bled(); | 159 mNetworkQualityEstimatorEnabled = builder.networkQualityEstimatorEna
bled(); |
162 } | 160 } |
163 | 161 |
164 // Init native Chromium URLRequestContext on main UI thread. | 162 // Init native Chromium URLRequestContext on init thread. |
165 Runnable task = new Runnable() { | 163 CronetLibraryLoader.postToInitThread(new Runnable() { |
166 @Override | 164 @Override |
167 public void run() { | 165 public void run() { |
168 CronetLibraryLoader.ensureInitializedOnMainThread(builder.getCon
text()); | 166 CronetLibraryLoader.ensureInitializedOnInitThread(builder.getCon
text()); |
169 synchronized (mLock) { | 167 synchronized (mLock) { |
170 // mUrlRequestContextAdapter is guaranteed to exist until | 168 // mUrlRequestContextAdapter is guaranteed to exist until |
171 // initialization on main and network threads completes and | 169 // initialization on init and network threads completes and |
172 // initNetworkThread is called back on network thread. | 170 // initNetworkThread is called back on network thread. |
173 nativeInitRequestContextOnMainThread(mUrlRequestContextAdapt
er); | 171 nativeInitRequestContextOnInitThread(mUrlRequestContextAdapt
er); |
174 } | 172 } |
175 } | 173 } |
176 }; | 174 }); |
177 // Run task immediately or post it to the UI thread. | |
178 if (Looper.getMainLooper() == Looper.myLooper()) { | |
179 task.run(); | |
180 } else { | |
181 new Handler(Looper.getMainLooper()).post(task); | |
182 } | |
183 } | 175 } |
184 | 176 |
185 @VisibleForTesting | 177 @VisibleForTesting |
186 public static long createNativeUrlRequestContextConfig(CronetEngineBuilderIm
pl builder) { | 178 public static long createNativeUrlRequestContextConfig(CronetEngineBuilderIm
pl builder) { |
187 final long urlRequestContextConfig = nativeCreateRequestContextConfig( | 179 final long urlRequestContextConfig = nativeCreateRequestContextConfig( |
188 builder.getUserAgent(), builder.storagePath(), builder.quicEnabl
ed(), | 180 builder.getUserAgent(), builder.storagePath(), builder.quicEnabl
ed(), |
189 builder.getDefaultQuicUserAgentId(), builder.http2Enabled(), bui
lder.sdchEnabled(), | 181 builder.getDefaultQuicUserAgentId(), builder.http2Enabled(), bui
lder.sdchEnabled(), |
190 builder.brotliEnabled(), builder.cacheDisabled(), builder.httpCa
cheMode(), | 182 builder.brotliEnabled(), builder.cacheDisabled(), builder.httpCa
cheMode(), |
191 builder.httpCacheMaxSize(), builder.experimentalOptions(), | 183 builder.httpCacheMaxSize(), builder.experimentalOptions(), |
192 builder.mockCertVerifier(), builder.networkQualityEstimatorEnabl
ed(), | 184 builder.mockCertVerifier(), builder.networkQualityEstimatorEnabl
ed(), |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 checkHaveAdapter(); | 236 checkHaveAdapter(); |
245 if (mActiveRequestCount.get() != 0) { | 237 if (mActiveRequestCount.get() != 0) { |
246 throw new IllegalStateException("Cannot shutdown with active req
uests."); | 238 throw new IllegalStateException("Cannot shutdown with active req
uests."); |
247 } | 239 } |
248 // Destroying adapter stops the network thread, so it cannot be | 240 // Destroying adapter stops the network thread, so it cannot be |
249 // called on network thread. | 241 // called on network thread. |
250 if (Thread.currentThread() == mNetworkThread) { | 242 if (Thread.currentThread() == mNetworkThread) { |
251 throw new IllegalThreadStateException("Cannot shutdown from netw
ork thread."); | 243 throw new IllegalThreadStateException("Cannot shutdown from netw
ork thread."); |
252 } | 244 } |
253 } | 245 } |
254 // Wait for init to complete on main and network thread (without lock, | 246 // Wait for init to complete on init and network thread (without lock, |
255 // so other thread could access it). | 247 // so other thread could access it). |
256 mInitCompleted.block(); | 248 mInitCompleted.block(); |
257 | 249 |
258 // If not logging, this is a no-op. | 250 // If not logging, this is a no-op. |
259 stopNetLog(); | 251 stopNetLog(); |
260 | 252 |
261 synchronized (mLock) { | 253 synchronized (mLock) { |
262 // It is possible that adapter is already destroyed on another threa
d. | 254 // It is possible that adapter is already destroyed on another threa
d. |
263 if (!haveRequestContextAdapter()) { | 255 if (!haveRequestContextAdapter()) { |
264 return; | 256 return; |
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
704 private native void nativeStartNetLogToDisk( | 696 private native void nativeStartNetLogToDisk( |
705 long nativePtr, String dirPath, boolean logAll, int maxSize); | 697 long nativePtr, String dirPath, boolean logAll, int maxSize); |
706 | 698 |
707 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 699 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
708 private native void nativeStopNetLog(long nativePtr); | 700 private native void nativeStopNetLog(long nativePtr); |
709 | 701 |
710 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 702 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
711 private native void nativeGetCertVerifierData(long nativePtr); | 703 private native void nativeGetCertVerifierData(long nativePtr); |
712 | 704 |
713 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 705 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
714 private native void nativeInitRequestContextOnMainThread(long nativePtr); | 706 private native void nativeInitRequestContextOnInitThread(long nativePtr); |
715 | 707 |
716 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 708 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
717 private native void nativeConfigureNetworkQualityEstimatorForTesting(long na
tivePtr, | 709 private native void nativeConfigureNetworkQualityEstimatorForTesting(long na
tivePtr, |
718 boolean useLocalHostRequests, boolean useSmallerResponses, boolean d
isableOfflineCheck); | 710 boolean useLocalHostRequests, boolean useSmallerResponses, boolean d
isableOfflineCheck); |
719 | 711 |
720 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 712 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
721 private native void nativeProvideRTTObservations(long nativePtr, boolean sho
uld); | 713 private native void nativeProvideRTTObservations(long nativePtr, boolean sho
uld); |
722 | 714 |
723 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 715 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
724 private native void nativeProvideThroughputObservations(long nativePtr, bool
ean should); | 716 private native void nativeProvideThroughputObservations(long nativePtr, bool
ean should); |
725 | 717 |
726 public boolean isNetworkThread(Thread thread) { | 718 public boolean isNetworkThread(Thread thread) { |
727 return thread == mNetworkThread; | 719 return thread == mNetworkThread; |
728 } | 720 } |
729 } | 721 } |
OLD | NEW |