Chromium Code Reviews| 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.CalledByNative; | 15 import org.chromium.base.CalledByNative; |
| 16 import org.chromium.base.JNINamespace; | 16 import org.chromium.base.JNINamespace; |
| 17 import org.chromium.base.NativeClassQualifiedName; | 17 import org.chromium.base.NativeClassQualifiedName; |
| 18 import org.chromium.base.ObserverList; | |
| 18 import org.chromium.base.VisibleForTesting; | 19 import org.chromium.base.VisibleForTesting; |
| 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 /** |
| 25 * UrlRequest context using Chromium HTTP stack implementation. | 26 * UrlRequest context using Chromium HTTP stack implementation. |
| 26 */ | 27 */ |
| 27 @JNINamespace("cronet") | 28 @JNINamespace("cronet") |
| 28 @UsedByReflection("UrlRequestContext.java") | 29 @UsedByReflection("UrlRequestContext.java") |
| 29 public class CronetUrlRequestContext extends UrlRequestContext { | 30 public class CronetUrlRequestContext extends UrlRequestContext { |
| 30 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. | 31 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. |
| 31 private static final int LOG_DEBUG = -1; // LOG(FATAL...INFO), VLOG(1) | 32 private static final int LOG_DEBUG = -1; // LOG(FATAL...INFO), VLOG(1) |
| 32 private static final int LOG_VERBOSE = -2; // LOG(FATAL...INFO), VLOG(2) | 33 private static final int LOG_VERBOSE = -2; // LOG(FATAL...INFO), VLOG(2) |
| 33 static final String LOG_TAG = "ChromiumNetwork"; | 34 static final String LOG_TAG = "ChromiumNetwork"; |
| 34 | 35 |
| 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 ObserverList<RTTObserver> mRTTObserverList = | |
|
mef
2015/08/11 17:16:26
final
bengr
2015/08/25 23:43:34
Done.
| |
| 47 new ObserverList<RTTObserver>(); | |
| 48 private ObserverList<BandwidthObserver> mBandwidthObserverList = | |
| 49 new ObserverList<BandwidthObserver>(); | |
| 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 configureNetworkQualityEstimator( | |
| 159 boolean useLocalHostRequests, boolean useSmallerResponses) { | |
|
mef
2015/08/11 17:16:26
synchronized (mLock) { checkHaveAdapter(); ...
he
bengr
2015/08/25 23:43:34
Done.
| |
| 160 nativeConfigureNetworkQualityEstimator(mUrlRequestContextAdapter, | |
| 161 useLocalHostRequests, useSmallerResponses); | |
| 162 } | |
| 163 | |
| 164 @Override | |
| 165 public void addRTTObserver(RTTObserver observer) { | |
| 166 if (mRTTObserverList.isEmpty()) { | |
| 167 nativeProvideRTTObservations(mUrlRequestContextAdapter, true); | |
| 168 } | |
| 169 mRTTObserverList.addObserver(observer); | |
| 170 } | |
| 171 | |
| 172 @Override | |
| 173 public void removeRTTObserver(RTTObserver observer) { | |
| 174 mRTTObserverList.removeObserver(observer); | |
| 175 if (mRTTObserverList.isEmpty()) { | |
| 176 nativeProvideRTTObservations(mUrlRequestContextAdapter, false); | |
| 177 } | |
| 178 } | |
| 179 | |
| 180 @Override | |
| 181 public void addBandwidthObserver(BandwidthObserver observer) { | |
| 182 if (mBandwidthObserverList.isEmpty()) { | |
| 183 nativeProvideBandwidthObservations(mUrlRequestContextAdapter, true); | |
| 184 } | |
| 185 mBandwidthObserverList.addObserver(observer); | |
| 186 } | |
| 187 | |
| 188 @Override | |
| 189 public void removeBandwidthObserver(BandwidthObserver observer) { | |
| 190 mBandwidthObserverList.removeObserver(observer); | |
| 191 if (mBandwidthObserverList.isEmpty()) { | |
| 192 nativeProvideBandwidthObservations(mUrlRequestContextAdapter, | |
| 193 false); | |
| 194 } | |
| 195 | |
| 196 } | |
| 197 | |
| 151 /** | 198 /** |
| 152 * Mark request as started to prevent shutdown when there are active | 199 * Mark request as started to prevent shutdown when there are active |
| 153 * requests. | 200 * requests. |
| 154 */ | 201 */ |
| 155 void onRequestStarted(UrlRequest urlRequest) { | 202 void onRequestStarted(UrlRequest urlRequest) { |
| 156 mActiveRequestCount.incrementAndGet(); | 203 mActiveRequestCount.incrementAndGet(); |
| 157 } | 204 } |
| 158 | 205 |
| 159 /** | 206 /** |
| 160 * Mark request as completed to allow shutdown when there are no active | 207 * 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 | 249 @CalledByNative |
| 203 private void initNetworkThread() { | 250 private void initNetworkThread() { |
| 204 synchronized (mLock) { | 251 synchronized (mLock) { |
| 205 mNetworkThread = Thread.currentThread(); | 252 mNetworkThread = Thread.currentThread(); |
| 206 mInitCompleted.open(); | 253 mInitCompleted.open(); |
| 207 } | 254 } |
| 208 Thread.currentThread().setName("ChromiumNet"); | 255 Thread.currentThread().setName("ChromiumNet"); |
| 209 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); | 256 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); |
| 210 } | 257 } |
| 211 | 258 |
| 259 @SuppressWarnings("unused") | |
| 260 @CalledByNative | |
| 261 private void onRTTObservation(int value, int when, int source) { | |
| 262 for (RTTObserver observer : mRTTObserverList) { | |
|
mef
2015/08/11 17:16:26
protect the list?
bengr
2015/08/25 23:43:34
I don't understand.
| |
| 263 observer.onRTTObservation(value, when, source); | |
| 264 } | |
| 265 } | |
| 266 | |
| 267 @SuppressWarnings("unused") | |
| 268 @CalledByNative | |
| 269 private void onBandwidthObservation(int value, int when, int source) { | |
| 270 for (BandwidthObserver observer : mBandwidthObserverList) { | |
| 271 observer.onBandwidthObservation(value, when, source); | |
| 272 } | |
| 273 } | |
| 274 | |
| 212 // Native methods are implemented in cronet_url_request_context.cc. | 275 // Native methods are implemented in cronet_url_request_context.cc. |
| 213 private static native long nativeCreateRequestContextAdapter(String config); | 276 private static native long nativeCreateRequestContextAdapter(String config); |
| 214 | 277 |
| 215 private static native int nativeSetMinLogLevel(int loggingLevel); | 278 private static native int nativeSetMinLogLevel(int loggingLevel); |
| 216 | 279 |
| 217 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 280 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
| 218 private native void nativeDestroy(long nativePtr); | 281 private native void nativeDestroy(long nativePtr); |
| 219 | 282 |
| 220 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 283 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
| 221 private native void nativeStartNetLogToFile(long nativePtr, | 284 private native void nativeStartNetLogToFile(long nativePtr, |
| 222 String fileName, boolean logAll); | 285 String fileName, boolean logAll); |
| 223 | 286 |
| 224 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 287 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
| 225 private native void nativeStopNetLog(long nativePtr); | 288 private native void nativeStopNetLog(long nativePtr); |
| 226 | 289 |
| 227 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | 290 @NativeClassQualifiedName("CronetURLRequestContextAdapter") |
| 228 private native void nativeInitRequestContextOnMainThread(long nativePtr); | 291 private native void nativeInitRequestContextOnMainThread(long nativePtr); |
| 292 | |
| 293 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | |
| 294 private native void nativeConfigureNetworkQualityEstimator(long nativePtr, | |
| 295 boolean useLocalHostRequests, boolean useSmallerResponses); | |
| 296 | |
| 297 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | |
| 298 private native void nativeProvideRTTObservations(long nativePtr, | |
| 299 boolean should); | |
| 300 | |
| 301 @NativeClassQualifiedName("CronetURLRequestContextAdapter") | |
| 302 private native void nativeProvideBandwidthObservations(long nativePtr, | |
| 303 boolean should); | |
| 229 } | 304 } |
| OLD | NEW |