| 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; | 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.VisibleForTesting; | 16 import org.chromium.base.VisibleForTesting; |
| 16 import org.chromium.base.annotations.CalledByNative; | 17 import org.chromium.base.annotations.CalledByNative; |
| 17 import org.chromium.base.annotations.JNINamespace; | 18 import org.chromium.base.annotations.JNINamespace; |
| 18 import org.chromium.base.annotations.NativeClassQualifiedName; | 19 import org.chromium.base.annotations.NativeClassQualifiedName; |
| 19 import org.chromium.base.annotations.UsedByReflection; | 20 import org.chromium.base.annotations.UsedByReflection; |
| 20 | 21 |
| 21 import java.util.concurrent.Executor; | 22 import java.util.concurrent.Executor; |
| 22 import java.util.concurrent.atomic.AtomicInteger; | 23 import java.util.concurrent.atomic.AtomicInteger; |
| 23 | 24 |
| 24 /** | 25 /** |
| (...skipping 10 matching lines...) Expand all Loading... |
| 35 /** | 36 /** |
| 36 * Synchronize access to mUrlRequestContextAdapter and shutdown routine. | 37 * Synchronize access to mUrlRequestContextAdapter and shutdown routine. |
| 37 */ | 38 */ |
| 38 private final Object mLock = new Object(); | 39 private final Object mLock = new Object(); |
| 39 private final ConditionVariable mInitCompleted = new ConditionVariable(false
); | 40 private final ConditionVariable mInitCompleted = new ConditionVariable(false
); |
| 40 private final AtomicInteger mActiveRequestCount = new AtomicInteger(0); | 41 private final AtomicInteger mActiveRequestCount = new AtomicInteger(0); |
| 41 | 42 |
| 42 private long mUrlRequestContextAdapter = 0; | 43 private long mUrlRequestContextAdapter = 0; |
| 43 private Thread mNetworkThread; | 44 private Thread mNetworkThread; |
| 44 | 45 |
| 46 private final ObserverList<NetworkQualityRTTObserver> mRTTObserverList = |
| 47 new ObserverList<NetworkQualityRTTObserver>(); |
| 48 private final ObserverList<NetworkQualityThroughputObserver> mThroughputObse
rverList = |
| 49 new ObserverList<NetworkQualityThroughputObserver>(); |
| 50 |
| 45 @UsedByReflection("UrlRequestContext.java") | 51 @UsedByReflection("UrlRequestContext.java") |
| 46 public CronetUrlRequestContext(Context context, | 52 public CronetUrlRequestContext(Context context, |
| 47 UrlRequestContextConfig config) { | 53 UrlRequestContextConfig config) { |
| 48 CronetLibraryLoader.ensureInitialized(context, config); | 54 CronetLibraryLoader.ensureInitialized(context, config); |
| 49 nativeSetMinLogLevel(getLoggingLevel()); | 55 nativeSetMinLogLevel(getLoggingLevel()); |
| 50 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter(config.toS
tring()); | 56 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter(config.toS
tring()); |
| 51 if (mUrlRequestContextAdapter == 0) { | 57 if (mUrlRequestContextAdapter == 0) { |
| 52 throw new NullPointerException("Context Adapter creation failed."); | 58 throw new NullPointerException("Context Adapter creation failed."); |
| 53 } | 59 } |
| 54 | 60 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 } | 147 } |
| 142 | 148 |
| 143 @Override | 149 @Override |
| 144 public void stopNetLog() { | 150 public void stopNetLog() { |
| 145 synchronized (mLock) { | 151 synchronized (mLock) { |
| 146 checkHaveAdapter(); | 152 checkHaveAdapter(); |
| 147 nativeStopNetLog(mUrlRequestContextAdapter); | 153 nativeStopNetLog(mUrlRequestContextAdapter); |
| 148 } | 154 } |
| 149 } | 155 } |
| 150 | 156 |
| 157 @Override |
| 158 public void enableNetworkQualityEstimator( |
| 159 boolean useLocalHostRequests, boolean useSmallerResponses) { |
| 160 synchronized (mLock) { |
| 161 checkHaveAdapter(); |
| 162 nativeEnableNetworkQualityEstimator( |
| 163 mUrlRequestContextAdapter, useLocalHostRequests, useSmallerR
esponses); |
| 164 } |
| 165 } |
| 166 |
| 167 @Override |
| 168 public void addRTTObserver(NetworkQualityRTTObserver observer) { |
| 169 if (mRTTObserverList.isEmpty()) { |
| 170 synchronized (mLock) { |
| 171 checkHaveAdapter(); |
| 172 nativeProvideRTTObservations(mUrlRequestContextAdapter, true); |
| 173 } |
| 174 } |
| 175 mRTTObserverList.addObserver(observer); |
| 176 } |
| 177 |
| 178 @Override |
| 179 public void removeRTTObserver(NetworkQualityRTTObserver observer) { |
| 180 mRTTObserverList.removeObserver(observer); |
| 181 if (mRTTObserverList.isEmpty()) { |
| 182 synchronized (mLock) { |
| 183 checkHaveAdapter(); |
| 184 nativeProvideRTTObservations(mUrlRequestContextAdapter, false); |
| 185 } |
| 186 } |
| 187 } |
| 188 |
| 189 @Override |
| 190 public void addThroughputObserver(NetworkQualityThroughputObserver observer)
{ |
| 191 if (mThroughputObserverList.isEmpty()) { |
| 192 synchronized (mLock) { |
| 193 checkHaveAdapter(); |
| 194 nativeProvideThroughputObservations(mUrlRequestContextAdapter, t
rue); |
| 195 } |
| 196 } |
| 197 mThroughputObserverList.addObserver(observer); |
| 198 } |
| 199 |
| 200 @Override |
| 201 public void removeThroughputObserver(NetworkQualityThroughputObserver observ
er) { |
| 202 mThroughputObserverList.removeObserver(observer); |
| 203 if (mThroughputObserverList.isEmpty()) { |
| 204 synchronized (mLock) { |
| 205 checkHaveAdapter(); |
| 206 nativeProvideThroughputObservations(mUrlRequestContextAdapter, f
alse); |
| 207 } |
| 208 } |
| 209 } |
| 210 |
| 151 /** | 211 /** |
| 152 * Mark request as started to prevent shutdown when there are active | 212 * Mark request as started to prevent shutdown when there are active |
| 153 * requests. | 213 * requests. |
| 154 */ | 214 */ |
| 155 void onRequestStarted(UrlRequest urlRequest) { | 215 void onRequestStarted(UrlRequest urlRequest) { |
| 156 mActiveRequestCount.incrementAndGet(); | 216 mActiveRequestCount.incrementAndGet(); |
| 157 } | 217 } |
| 158 | 218 |
| 159 /** | 219 /** |
| 160 * Mark request as completed to allow shutdown when there are no active | 220 * Mark request as completed to allow shutdown when there are no active |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 @CalledByNative | 262 @CalledByNative |
| 203 private void initNetworkThread() { | 263 private void initNetworkThread() { |
| 204 synchronized (mLock) { | 264 synchronized (mLock) { |
| 205 mNetworkThread = Thread.currentThread(); | 265 mNetworkThread = Thread.currentThread(); |
| 206 mInitCompleted.open(); | 266 mInitCompleted.open(); |
| 207 } | 267 } |
| 208 Thread.currentThread().setName("ChromiumNet"); | 268 Thread.currentThread().setName("ChromiumNet"); |
| 209 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); | 269 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 210 } | 270 } |
| 211 | 271 |
| 272 @SuppressWarnings("unused") |
| 273 @CalledByNative |
| 274 private void onRTTObservation(int value, long when, int source) { |
| 275 for (NetworkQualityRTTObserver observer : mRTTObserverList) { |
| 276 observer.onRTTObservation(value, when, source); |
| 277 } |
| 278 } |
| 279 |
| 280 @SuppressWarnings("unused") |
| 281 @CalledByNative |
| 282 private void onThroughputObservation(int value, long when, int source) { |
| 283 for (NetworkQualityThroughputObserver observer : mThroughputObserverList
) { |
| 284 observer.onThroughputObservation(value, when, source); |
| 285 } |
| 286 } |
| 287 |
| 212 // Native methods are implemented in cronet_url_request_context.cc. | 288 // Native methods are implemented in cronet_url_request_context.cc. |
| 213 private static native long nativeCreateRequestContextAdapter(String config); | 289 private static native long nativeCreateRequestContextAdapter(String config); |
| 214 | 290 |
| 215 private static native int nativeSetMinLogLevel(int loggingLevel); | 291 private static native int nativeSetMinLogLevel(int loggingLevel); |
| 216 | 292 |
| 217 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 293 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
| 218 private native void nativeDestroy(long nativePtr); | 294 private native void nativeDestroy(long nativePtr); |
| 219 | 295 |
| 220 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 296 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
| 221 private native void nativeStartNetLogToFile(long nativePtr, | 297 private native void nativeStartNetLogToFile(long nativePtr, |
| 222 String fileName, boolean logAll); | 298 String fileName, boolean logAll); |
| 223 | 299 |
| 224 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 300 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
| 225 private native void nativeStopNetLog(long nativePtr); | 301 private native void nativeStopNetLog(long nativePtr); |
| 226 | 302 |
| 227 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 303 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
| 228 private native void nativeInitRequestContextOnMainThread(long nativePtr); | 304 private native void nativeInitRequestContextOnMainThread(long nativePtr); |
| 305 |
| 306 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
| 307 private native void nativeEnableNetworkQualityEstimator( |
| 308 long nativePtr, boolean useLocalHostRequests, boolean useSmallerResp
onses); |
| 309 |
| 310 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
| 311 private native void nativeProvideRTTObservations(long nativePtr, boolean sho
uld); |
| 312 |
| 313 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
| 314 private native void nativeProvideThroughputObservations(long nativePtr, bool
ean should); |
| 229 } | 315 } |
| OLD | NEW |