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

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

Issue 1273173002: Added Network Quality Estimator Real-time interface to Cronet (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed remaining comments Created 5 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; 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;
23 import java.util.concurrent.RejectedExecutionException;
22 import java.util.concurrent.atomic.AtomicInteger; 24 import java.util.concurrent.atomic.AtomicInteger;
23 25
26 import javax.annotation.concurrent.GuardedBy;
27
24 /** 28 /**
25 * UrlRequestContext using Chromium HTTP stack implementation. 29 * UrlRequestContext using Chromium HTTP stack implementation.
26 */ 30 */
27 @JNINamespace("cronet") 31 @JNINamespace("cronet")
28 @UsedByReflection("UrlRequestContext.java") 32 @UsedByReflection("UrlRequestContext.java")
29 class CronetUrlRequestContext extends UrlRequestContext { 33 class CronetUrlRequestContext extends UrlRequestContext {
30 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG. 34 private static final int LOG_NONE = 3; // LOG(FATAL), no VLOG.
31 private static final int LOG_DEBUG = -1; // LOG(FATAL...INFO), VLOG(1) 35 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) 36 private static final int LOG_VERBOSE = -2; // LOG(FATAL...INFO), VLOG(2)
33 static final String LOG_TAG = "ChromiumNetwork"; 37 static final String LOG_TAG = "ChromiumNetwork";
34 38
35 /** 39 /**
36 * Synchronize access to mUrlRequestContextAdapter and shutdown routine. 40 * Synchronize access to mUrlRequestContextAdapter and shutdown routine.
37 */ 41 */
38 private final Object mLock = new Object(); 42 private final Object mLock = new Object();
39 private final ConditionVariable mInitCompleted = new ConditionVariable(false ); 43 private final ConditionVariable mInitCompleted = new ConditionVariable(false );
40 private final AtomicInteger mActiveRequestCount = new AtomicInteger(0); 44 private final AtomicInteger mActiveRequestCount = new AtomicInteger(0);
41 45
42 private long mUrlRequestContextAdapter = 0; 46 private long mUrlRequestContextAdapter = 0;
43 private Thread mNetworkThread; 47 private Thread mNetworkThread;
44 48
49 private Executor mNetworkQualityExecutor;
50
51 // Locks operations on network quality observers, because observer addition and removal may
52 // occur on a different thread from notification.
53 private final Object mNetworkQualityLock = new Object();
54
55 @GuardedBy("mNetworkQualityLock")
56 private final ObserverList<NetworkQualityRttObserver> mRttObserverList =
57 new ObserverList<NetworkQualityRttObserver>();
58
59 @GuardedBy("mNetworkQualityLock")
60 private final ObserverList<NetworkQualityThroughputObserver> mThroughputObse rverList =
61 new ObserverList<NetworkQualityThroughputObserver>();
62
45 @UsedByReflection("UrlRequestContext.java") 63 @UsedByReflection("UrlRequestContext.java")
46 public CronetUrlRequestContext(Context context, 64 public CronetUrlRequestContext(Context context,
47 UrlRequestContextConfig config) { 65 UrlRequestContextConfig config) {
48 CronetLibraryLoader.ensureInitialized(context, config); 66 CronetLibraryLoader.ensureInitialized(context, config);
49 nativeSetMinLogLevel(getLoggingLevel()); 67 nativeSetMinLogLevel(getLoggingLevel());
50 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter(config.toS tring()); 68 mUrlRequestContextAdapter = nativeCreateRequestContextAdapter(config.toS tring());
51 if (mUrlRequestContextAdapter == 0) { 69 if (mUrlRequestContextAdapter == 0) {
52 throw new NullPointerException("Context Adapter creation failed."); 70 throw new NullPointerException("Context Adapter creation failed.");
53 } 71 }
54 72
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 } 159 }
142 160
143 @Override 161 @Override
144 public void stopNetLog() { 162 public void stopNetLog() {
145 synchronized (mLock) { 163 synchronized (mLock) {
146 checkHaveAdapter(); 164 checkHaveAdapter();
147 nativeStopNetLog(mUrlRequestContextAdapter); 165 nativeStopNetLog(mUrlRequestContextAdapter);
148 } 166 }
149 } 167 }
150 168
169 @Override
170 public void enableNetworkQualityEstimator(Executor executor) {
171 enableNetworkQualityEstimator(false, false, executor);
172 }
173
174 @VisibleForTesting
175 @Override
176 void enableNetworkQualityEstimator(
177 boolean useLocalHostRequests, boolean useSmallerResponses, Executor executor) {
178 mNetworkQualityExecutor = executor;
179 synchronized (mLock) {
180 checkHaveAdapter();
181 nativeEnableNetworkQualityEstimator(
182 mUrlRequestContextAdapter, useLocalHostRequests, useSmallerR esponses);
183 }
184 }
185
186 @Override
187 public void addRttObserver(NetworkQualityRttObserver observer) {
188 synchronized (mNetworkQualityLock) {
189 if (mRttObserverList.isEmpty()) {
190 synchronized (mLock) {
191 checkHaveAdapter();
192 nativeProvideRTTObservations(mUrlRequestContextAdapter, true );
193 }
194 }
195 mRttObserverList.addObserver(observer);
196 }
197 }
198
199 @Override
200 public void removeRttObserver(NetworkQualityRttObserver observer) {
201 synchronized (mNetworkQualityLock) {
202 mRttObserverList.removeObserver(observer);
203 if (mRttObserverList.isEmpty()) {
204 synchronized (mLock) {
205 checkHaveAdapter();
206 nativeProvideRTTObservations(mUrlRequestContextAdapter, fals e);
207 }
208 }
209 }
210 }
211
212 @Override
213 public void addThroughputObserver(NetworkQualityThroughputObserver observer) {
214 synchronized (mNetworkQualityLock) {
215 if (mThroughputObserverList.isEmpty()) {
216 synchronized (mLock) {
217 checkHaveAdapter();
218 nativeProvideThroughputObservations(mUrlRequestContextAdapte r, true);
219 }
220 }
221 mThroughputObserverList.addObserver(observer);
222 }
223 }
224
225 @Override
226 public void removeThroughputObserver(NetworkQualityThroughputObserver observ er) {
227 synchronized (mNetworkQualityLock) {
228 mThroughputObserverList.removeObserver(observer);
229 if (mThroughputObserverList.isEmpty()) {
230 synchronized (mLock) {
231 checkHaveAdapter();
232 nativeProvideThroughputObservations(mUrlRequestContextAdapte r, false);
233 }
234 }
235 }
236 }
237
151 /** 238 /**
152 * Mark request as started to prevent shutdown when there are active 239 * Mark request as started to prevent shutdown when there are active
153 * requests. 240 * requests.
154 */ 241 */
155 void onRequestStarted(UrlRequest urlRequest) { 242 void onRequestStarted(UrlRequest urlRequest) {
156 mActiveRequestCount.incrementAndGet(); 243 mActiveRequestCount.incrementAndGet();
157 } 244 }
158 245
159 /** 246 /**
160 * Mark request as completed to allow shutdown when there are no active 247 * Mark request as completed to allow shutdown when there are no active
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 @CalledByNative 289 @CalledByNative
203 private void initNetworkThread() { 290 private void initNetworkThread() {
204 synchronized (mLock) { 291 synchronized (mLock) {
205 mNetworkThread = Thread.currentThread(); 292 mNetworkThread = Thread.currentThread();
206 mInitCompleted.open(); 293 mInitCompleted.open();
207 } 294 }
208 Thread.currentThread().setName("ChromiumNet"); 295 Thread.currentThread().setName("ChromiumNet");
209 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); 296 Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
210 } 297 }
211 298
299 @SuppressWarnings("unused")
300 @CalledByNative
301 private void onRttObservation(final int value, final long when, final int so urce) {
302 Runnable task = new Runnable() {
303 @Override
304 public void run() {
305 for (NetworkQualityRttObserver observer : mRttObserverList) {
306 observer.onRttObservation(value, when, source);
307 }
308 }
309 };
310 postObservationTaskToExecutor(task);
311 }
312
313 @SuppressWarnings("unused")
314 @CalledByNative
315 private void onThroughputObservation(final int value, final long when, final int source) {
316 Runnable task = new Runnable() {
317 @Override
318 public void run() {
319 for (NetworkQualityThroughputObserver observer : mThroughputObse rverList) {
320 observer.onThroughputObservation(value, when, source);
321 }
322 }
323 };
324 postObservationTaskToExecutor(task);
325 }
326
327 void postObservationTaskToExecutor(Runnable task) {
328 try {
329 synchronized (mNetworkQualityLock) {
330 mNetworkQualityExecutor.execute(task);
331 }
332 } catch (RejectedExecutionException failException) {
333 Log.e(CronetUrlRequestContext.LOG_TAG, "Exception posting task to ex ecutor",
334 failException);
335 }
336 }
337
212 // Native methods are implemented in cronet_url_request_context.cc. 338 // Native methods are implemented in cronet_url_request_context.cc.
213 private static native long nativeCreateRequestContextAdapter(String config); 339 private static native long nativeCreateRequestContextAdapter(String config);
214 340
215 private static native int nativeSetMinLogLevel(int loggingLevel); 341 private static native int nativeSetMinLogLevel(int loggingLevel);
216 342
217 @NativeClassQualifiedName("CronetURLRequestContextAdapter") 343 @NativeClassQualifiedName("CronetURLRequestContextAdapter")
218 private native void nativeDestroy(long nativePtr); 344 private native void nativeDestroy(long nativePtr);
219 345
220 @NativeClassQualifiedName("CronetURLRequestContextAdapter") 346 @NativeClassQualifiedName("CronetURLRequestContextAdapter")
221 private native void nativeStartNetLogToFile(long nativePtr, 347 private native void nativeStartNetLogToFile(long nativePtr,
222 String fileName, boolean logAll); 348 String fileName, boolean logAll);
223 349
224 @NativeClassQualifiedName("CronetURLRequestContextAdapter") 350 @NativeClassQualifiedName("CronetURLRequestContextAdapter")
225 private native void nativeStopNetLog(long nativePtr); 351 private native void nativeStopNetLog(long nativePtr);
226 352
227 @NativeClassQualifiedName("CronetURLRequestContextAdapter") 353 @NativeClassQualifiedName("CronetURLRequestContextAdapter")
228 private native void nativeInitRequestContextOnMainThread(long nativePtr); 354 private native void nativeInitRequestContextOnMainThread(long nativePtr);
355
356 @NativeClassQualifiedName("CronetURLRequestContextAdapter")
357 private native void nativeEnableNetworkQualityEstimator(
358 long nativePtr, boolean useLocalHostRequests, boolean useSmallerResp onses);
359
360 @NativeClassQualifiedName("CronetURLRequestContextAdapter")
361 private native void nativeProvideRTTObservations(long nativePtr, boolean sho uld);
362
363 @NativeClassQualifiedName("CronetURLRequestContextAdapter")
364 private native void nativeProvideThroughputObservations(long nativePtr, bool ean should);
229 } 365 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698