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

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

Issue 726013002: [Cronet] Hook up library loader, system proxy and network change notifier to async api. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address Matt's comments. Created 5 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: components/cronet/android/java/src/org/chromium/net/CronetUrlRequestContext.java
diff --git a/components/cronet/android/java/src/org/chromium/net/CronetUrlRequestContext.java b/components/cronet/android/java/src/org/chromium/net/CronetUrlRequestContext.java
index 4c7afc6c0f2278fc2be06da86220681d7d2ee5ac..165ba53ab9d8fc03d2c31ebf607cb562514af3d0 100644
--- a/components/cronet/android/java/src/org/chromium/net/CronetUrlRequestContext.java
+++ b/components/cronet/android/java/src/org/chromium/net/CronetUrlRequestContext.java
@@ -6,6 +6,8 @@ package org.chromium.net;
import android.content.Context;
import android.os.Build;
+import android.os.Handler;
+import android.os.Looper;
import android.os.Process;
import android.util.Log;
@@ -25,18 +27,38 @@ public class CronetUrlRequestContext extends UrlRequestContext {
private static final int LOG_VERBOSE = -2; // LOG(FATAL...INFO), VLOG(2)
static final String LOG_TAG = "ChromiumNetwork";
+ /**
+ * Synchronize access to mUrlRequestContextAdapter and shutdown routine.
+ */
+ private final Object mLock = new Object();
+
private long mUrlRequestContextAdapter = 0;
private Thread mNetworkThread;
private AtomicInteger mActiveRequestCount = new AtomicInteger(0);
public CronetUrlRequestContext(Context context,
UrlRequestContextConfig config) {
+ CronetLibraryLoader.ensureInitialized(context, config);
nativeSetMinLogLevel(getLoggingLevel());
+ final String configString = config.toString();
xunjieli 2015/01/12 15:12:34 nit: could you make config.toString() inline?
mef 2015/01/12 17:19:56 Done.
mUrlRequestContextAdapter = nativeCreateRequestContextAdapter(
- context, config.toString());
+ context, configString);
if (mUrlRequestContextAdapter == 0) {
throw new NullPointerException("Context Adapter creation failed");
}
+ // Post a task to UI thread to init native Chromium URLRequestContext.
+ // TODO(xunjieli): This constructor is not supposed to be invoked on
+ // the main thread. Consider making the following code into a blocking
+ // API to handle the case where we are already on main thread.
+ Runnable task = new Runnable() {
+ public void run() {
+ // mUrlRequestContextAdapter is guaranteed to exist until
+ // initialization on main and network threads completes and
+ // initNetworkThread is called back on network thread.
+ nativeInitRequestContextOnMainThread(mUrlRequestContextAdapter);
+ }
+ };
+ new Handler(Looper.getMainLooper()).post(task);
}
@Override
@@ -62,18 +84,28 @@ public class CronetUrlRequestContext extends UrlRequestContext {
@Override
public void shutdown() {
- if (mActiveRequestCount.get() != 0) {
- throw new IllegalStateException(
- "Cannot shutdown with active requests.");
+ synchronized (mLock) {
+ if (mNetworkThread == null) {
+ throw new IllegalStateException(
+ "Context is not fully initialized.");
+ }
+ if (mUrlRequestContextAdapter == 0) {
+ throw new IllegalStateException(
+ "Context is already shutdown.");
+ }
+ if (mActiveRequestCount.get() != 0) {
+ throw new IllegalStateException(
+ "Cannot shutdown with active requests.");
+ }
+ // Destroying adapter stops the network thread, so it cannot be
+ // called on network thread.
+ if (Thread.currentThread() == mNetworkThread) {
+ throw new IllegalThreadStateException(
+ "Cannot shutdown from network thread.");
+ }
+ nativeDestroyRequestContextAdapter(mUrlRequestContextAdapter);
+ mUrlRequestContextAdapter = 0;
}
- // Destroying adapter stops the network thread, so it cannot be called
- // on network thread.
- if (Thread.currentThread() == mNetworkThread) {
- throw new IllegalThreadStateException(
- "Cannot shutdown from network thread.");
- }
- nativeDestroyRequestContextAdapter(mUrlRequestContextAdapter);
- mUrlRequestContextAdapter = 0;
}
@Override
@@ -146,4 +178,7 @@ public class CronetUrlRequestContext extends UrlRequestContext {
private native void nativeStopNetLog(long urlRequestContextAdapter);
private native int nativeSetMinLogLevel(int loggingLevel);
+
+ private native void nativeInitRequestContextOnMainThread(
+ long urlRequestContextAdapter);
}

Powered by Google App Engine
This is Rietveld 408576698