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; | 5 package org.chromium.net; |
6 | 6 |
7 import android.content.Context; | |
8 import android.os.Build; | 7 import android.os.Build; |
9 import android.os.ConditionVariable; | 8 import android.os.ConditionVariable; |
10 import android.os.Handler; | 9 import android.os.Handler; |
11 import android.os.Looper; | 10 import android.os.Looper; |
12 import android.os.Process; | 11 import android.os.Process; |
13 import android.util.Log; | 12 import android.util.Log; |
14 | 13 |
15 import org.chromium.base.ObserverList; | 14 import org.chromium.base.ObserverList; |
16 import org.chromium.base.VisibleForTesting; | 15 import org.chromium.base.VisibleForTesting; |
17 import org.chromium.base.annotations.CalledByNative; | 16 import org.chromium.base.annotations.CalledByNative; |
18 import org.chromium.base.annotations.JNINamespace; | 17 import org.chromium.base.annotations.JNINamespace; |
19 import org.chromium.base.annotations.NativeClassQualifiedName; | 18 import org.chromium.base.annotations.NativeClassQualifiedName; |
20 import org.chromium.base.annotations.UsedByReflection; | 19 import org.chromium.base.annotations.UsedByReflection; |
21 | 20 |
22 import java.util.concurrent.Executor; | 21 import java.util.concurrent.Executor; |
23 import java.util.concurrent.RejectedExecutionException; | 22 import java.util.concurrent.RejectedExecutionException; |
24 import java.util.concurrent.atomic.AtomicInteger; | 23 import java.util.concurrent.atomic.AtomicInteger; |
25 | 24 |
26 import javax.annotation.concurrent.GuardedBy; | 25 import javax.annotation.concurrent.GuardedBy; |
27 | 26 |
28 /** | 27 /** |
29 * UrlRequestContext using Chromium HTTP stack implementation. | 28 * CronetEngine using Chromium HTTP stack implementation. |
30 */ | 29 */ |
31 @JNINamespace("cronet") | 30 @JNINamespace("cronet") |
32 @UsedByReflection("UrlRequestContext.java") | 31 @UsedByReflection("CronetEngine.java") |
33 class CronetUrlRequestContext extends UrlRequestContext { | 32 class CronetUrlRequestContext extends CronetEngine { |
34 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. | 33 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. |
35 private static final int LOG_DEBUG = -1; // LOG(FATAL...INFO), VLOG(1) | 34 private static final int LOG_DEBUG = -1; // LOG(FATAL...INFO), VLOG(1) |
36 private static final int LOG_VERBOSE = -2; // LOG(FATAL...INFO), VLOG(2) | 35 private static final int LOG_VERBOSE = -2; // LOG(FATAL...INFO), VLOG(2) |
37 static final String LOG_TAG = "ChromiumNetwork"; | 36 static final String LOG_TAG = "ChromiumNetwork"; |
38 | 37 |
39 /** | 38 /** |
40 * Synchronize access to mUrlRequestContextAdapter and shutdown routine. | 39 * Synchronize access to mUrlRequestContextAdapter and shutdown routine. |
41 */ | 40 */ |
42 private final Object mLock = new Object(); | 41 private final Object mLock = new Object(); |
43 private final ConditionVariable mInitCompleted = new ConditionVariable(false
); | 42 private final ConditionVariable mInitCompleted = new ConditionVariable(false
); |
(...skipping 11 matching lines...) Expand all Loading... |
55 private final Object mNetworkQualityLock = new Object(); | 54 private final Object mNetworkQualityLock = new Object(); |
56 | 55 |
57 @GuardedBy("mNetworkQualityLock") | 56 @GuardedBy("mNetworkQualityLock") |
58 private final ObserverList<NetworkQualityRttListener> mRttListenerList = | 57 private final ObserverList<NetworkQualityRttListener> mRttListenerList = |
59 new ObserverList<NetworkQualityRttListener>(); | 58 new ObserverList<NetworkQualityRttListener>(); |
60 | 59 |
61 @GuardedBy("mNetworkQualityLock") | 60 @GuardedBy("mNetworkQualityLock") |
62 private final ObserverList<NetworkQualityThroughputListener> mThroughputList
enerList = | 61 private final ObserverList<NetworkQualityThroughputListener> mThroughputList
enerList = |
63 new ObserverList<NetworkQualityThroughputListener>(); | 62 new ObserverList<NetworkQualityThroughputListener>(); |
64 | 63 |
65 @UsedByReflection("UrlRequestContext.java") | 64 @UsedByReflection("CronetEngine.java") |
66 public CronetUrlRequestContext(Context context, | 65 public CronetUrlRequestContext(CronetEngine.Builder builder) { |
67 UrlRequestContextConfig config) { | 66 CronetLibraryLoader.ensureInitialized(builder.getContext(), builder); |
68 CronetLibraryLoader.ensureInitialized(context, config); | |
69 nativeSetMinLogLevel(getLoggingLevel()); | 67 nativeSetMinLogLevel(getLoggingLevel()); |
70 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter(config.toS
tring()); | 68 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter(builder.to
String()); |
71 if (mUrlRequestContextAdapter == 0) { | 69 if (mUrlRequestContextAdapter == 0) { |
72 throw new NullPointerException("Context Adapter creation failed."); | 70 throw new NullPointerException("Context Adapter creation failed."); |
73 } | 71 } |
74 | 72 |
75 // Init native Chromium URLRequestContext on main UI thread. | 73 // Init native Chromium URLRequestContext on main UI thread. |
76 Runnable task = new Runnable() { | 74 Runnable task = new Runnable() { |
77 @Override | 75 @Override |
78 public void run() { | 76 public void run() { |
79 synchronized (mLock) { | 77 synchronized (mLock) { |
80 // mUrlRequestContextAdapter is guaranteed to exist until | 78 // mUrlRequestContextAdapter is guaranteed to exist until |
(...skipping 10 matching lines...) Expand all Loading... |
91 new Handler(Looper.getMainLooper()).post(task); | 89 new Handler(Looper.getMainLooper()).post(task); |
92 } | 90 } |
93 } | 91 } |
94 | 92 |
95 @Override | 93 @Override |
96 public UrlRequest createRequest(String url, UrlRequestListener listener, | 94 public UrlRequest createRequest(String url, UrlRequestListener listener, |
97 Executor executor) { | 95 Executor executor) { |
98 synchronized (mLock) { | 96 synchronized (mLock) { |
99 checkHaveAdapter(); | 97 checkHaveAdapter(); |
100 return new CronetUrlRequest(this, mUrlRequestContextAdapter, url, | 98 return new CronetUrlRequest(this, mUrlRequestContextAdapter, url, |
101 UrlRequest.REQUEST_PRIORITY_MEDIUM, listener, executor); | 99 UrlRequest.Builder.REQUEST_PRIORITY_MEDIUM, listener, execut
or); |
102 } | 100 } |
103 } | 101 } |
104 | 102 |
105 @Override | 103 @Override |
106 public UrlRequest createRequest(String url, UrlRequestListener listener, | 104 public UrlRequest createRequest(String url, UrlRequestListener listener, |
107 Executor executor, int priority) { | 105 Executor executor, int priority) { |
108 synchronized (mLock) { | 106 synchronized (mLock) { |
109 checkHaveAdapter(); | 107 checkHaveAdapter(); |
110 return new CronetUrlRequest(this, mUrlRequestContextAdapter, url, | 108 return new CronetUrlRequest(this, mUrlRequestContextAdapter, url, |
111 priority, listener, executor); | 109 priority, listener, executor); |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 @VisibleForTesting | 273 @VisibleForTesting |
276 long getUrlRequestContextAdapter() { | 274 long getUrlRequestContextAdapter() { |
277 synchronized (mLock) { | 275 synchronized (mLock) { |
278 checkHaveAdapter(); | 276 checkHaveAdapter(); |
279 return mUrlRequestContextAdapter; | 277 return mUrlRequestContextAdapter; |
280 } | 278 } |
281 } | 279 } |
282 | 280 |
283 private void checkHaveAdapter() throws IllegalStateException { | 281 private void checkHaveAdapter() throws IllegalStateException { |
284 if (!haveRequestContextAdapter()) { | 282 if (!haveRequestContextAdapter()) { |
285 throw new IllegalStateException("Context is shut down."); | 283 throw new IllegalStateException("Engine is shut down."); |
286 } | 284 } |
287 } | 285 } |
288 | 286 |
289 private boolean haveRequestContextAdapter() { | 287 private boolean haveRequestContextAdapter() { |
290 return mUrlRequestContextAdapter != 0; | 288 return mUrlRequestContextAdapter != 0; |
291 } | 289 } |
292 | 290 |
293 /** | 291 /** |
294 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and | 292 * @return loggingLevel see {@link #LOG_NONE}, {@link #LOG_DEBUG} and |
295 * {@link #LOG_VERBOSE}. | 293 * {@link #LOG_VERBOSE}. |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 378 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
381 private native void nativeEnableNetworkQualityEstimator( | 379 private native void nativeEnableNetworkQualityEstimator( |
382 long nativePtr, boolean useLocalHostRequests, boolean useSmallerResp
onses); | 380 long nativePtr, boolean useLocalHostRequests, boolean useSmallerResp
onses); |
383 | 381 |
384 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 382 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
385 private native void nativeProvideRTTObservations(long nativePtr, boolean sho
uld); | 383 private native void nativeProvideRTTObservations(long nativePtr, boolean sho
uld); |
386 | 384 |
387 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 385 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
388 private native void nativeProvideThroughputObservations(long nativePtr, bool
ean should); | 386 private native void nativeProvideThroughputObservations(long nativePtr, bool
ean should); |
389 } | 387 } |
OLD | NEW |