Index: components/cronet/android/java/src/org/chromium/net/CronetLibraryLoader.java |
diff --git a/components/cronet/android/java/src/org/chromium/net/CronetLibraryLoader.java b/components/cronet/android/java/src/org/chromium/net/CronetLibraryLoader.java |
index 7c358d97f422e6789123baa1af4ff5e380a1bcf7..0ce0b5430f2fcfe233de5cb98a9ce86e3ef0da91 100644 |
--- a/components/cronet/android/java/src/org/chromium/net/CronetLibraryLoader.java |
+++ b/components/cronet/android/java/src/org/chromium/net/CronetLibraryLoader.java |
@@ -17,27 +17,24 @@ import org.chromium.base.annotations.JNINamespace; |
*/ |
@JNINamespace("cronet") |
class CronetLibraryLoader { |
- // Synchronize initialization. |
+ /** |
+ * Synchronize access to sInitTaskPosted and initialization routine. |
+ */ |
private static final Object sLoadLock = new Object(); |
private static final String TAG = "CronetLibraryLoader"; |
- // Has library loading commenced? Setting guarded by sLoadLock. |
- private static volatile boolean sInitStarted = false; |
- // Has ensureMainThreadInitialized() completed? Only accessed on main thread. |
- private static boolean sMainThreadInitDone = false; |
+ private static boolean sInitTaskPosted = false; |
/** |
* Ensure that native library is loaded and initialized. Can be called from |
* any thread, the load and initialization is performed on main thread. |
*/ |
- static void ensureInitialized(final Context context, final CronetEngine.Builder builder) { |
+ public static void ensureInitialized( |
+ final Context context, final CronetEngine.Builder builder) { |
synchronized (sLoadLock) { |
- if (sInitStarted) { |
+ if (sInitTaskPosted) { |
return; |
} |
- sInitStarted = true; |
- ContextUtils.initApplicationContext(context.getApplicationContext()); |
builder.loadLibrary(); |
- ContextUtils.initApplicationContextForNative(); |
if (!Version.CRONET_VERSION.equals(nativeGetCronetVersion())) { |
throw new RuntimeException(String.format( |
"Expected Cronet version number %s, " |
@@ -47,11 +44,11 @@ class CronetLibraryLoader { |
} |
Log.i(TAG, "Cronet version: %s, arch: %s", |
Version.CRONET_VERSION, System.getProperty("os.arch")); |
+ ContextUtils.initApplicationContext(context.getApplicationContext()); |
// Init native Chromium CronetEngine on Main UI thread. |
Runnable task = new Runnable() { |
- @Override |
public void run() { |
- ensureInitializedOnMainThread(context); |
+ initOnMainThread(context); |
} |
}; |
// Run task immediately or post it to the UI thread. |
@@ -62,20 +59,11 @@ class CronetLibraryLoader { |
// to other tasks posted to the main thread. |
new Handler(Looper.getMainLooper()).post(task); |
} |
+ sInitTaskPosted = true; |
} |
} |
- /** |
- * Ensure that the main thread initialization has completed. Can only be called from |
- * the main thread. Ensures that the NetworkChangeNotifier is initialzied and the |
- * main thread native MessageLoop is initialized. |
- */ |
- static void ensureInitializedOnMainThread(Context context) { |
- assert sInitStarted; |
- assert Looper.getMainLooper() == Looper.myLooper(); |
- if (sMainThreadInitDone) { |
- return; |
- } |
+ private static void initOnMainThread(final Context context) { |
NetworkChangeNotifier.init(context); |
// Registers to always receive network notifications. Note |
// that this call is fine for Cronet because Cronet |
@@ -88,7 +76,6 @@ class CronetLibraryLoader { |
// the undesired initial network change observer notification, which |
// will cause active requests to fail with ERR_NETWORK_CHANGED. |
nativeCronetInitOnMainThread(); |
- sMainThreadInitDone = true; |
} |
// Native methods are implemented in cronet_library_loader.cc. |