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); |
} |