Chromium Code Reviews| 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 |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9a85f963ce9195fea82027aee8fed7fb6885936a |
| --- /dev/null |
| +++ b/components/cronet/android/java/src/org/chromium/net/CronetLibraryLoader.java |
| @@ -0,0 +1,66 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.net; |
| + |
| +import android.content.Context; |
| +import android.os.Handler; |
| +import android.os.Looper; |
| + |
| +import org.chromium.base.JNINamespace; |
| + |
| +/** |
| + * CronetLibraryLoader loads and initializes native library on main thread. |
| + */ |
| +@JNINamespace("cronet") |
| +class CronetLibraryLoader { |
| + /** |
| + * Synchronize access to sInitTaskPosted and initialization routine. |
| + */ |
| + private static final Object sLoadLock = new Object(); |
| + 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. |
| + */ |
| + public static void ensureInitialized( |
| + final Context context, final UrlRequestContextConfig config) { |
| + synchronized (sLoadLock) { |
| + if (sInitTaskPosted) { |
| + return; |
| + } |
| + System.loadLibrary(config.libraryName()); |
| + // Init native Chromium URLRequestContext on Main UI thread. |
| + Runnable task = new Runnable() { |
| + public void run() { |
| + initOnMainThread(context); |
| + } |
| + }; |
| + // Run task immediately or post it to the UI thread. |
| + if (Looper.getMainLooper() == Looper.myLooper()) { |
| + task.run(); |
| + } else { |
| + // The initOnMainThread will complete on the main thread prior |
| + // to other tasks posted to the main thread. |
| + new Handler(Looper.getMainLooper()).post(task); |
| + } |
| + sInitTaskPosted = true; |
| + } |
| + } |
| + |
| + 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 |
| + // embedders do not have API access to create network change |
| + // observers. Existing observers in the net stack do not |
| + // perform expensive work. |
| + NetworkChangeNotifier.registerToReceiveNotificationsAlways(); |
| + nativeCronetInitOnMainThread(context); |
|
xunjieli
2015/02/03 18:37:59
Maybe move nativeCronetInitOnMainThread(context) i
mef
2015/02/03 20:08:55
I'll be glad to do that as I was just copying what
xunjieli
2015/02/03 20:11:48
Yes, that should do it. Thanks!
mef
2015/02/03 22:09:26
Done.
mef
2015/02/05 19:28:56
This change is causing first request to fail with
xunjieli
2015/02/05 19:47:49
I don't understand why this happens. We post initO
mef
2015/02/05 20:31:06
Why was it originally done in this order here: htt
|
| + } |
| + |
| + // Native methods are implemented in cronet_loader.cc. |
| + private static native long nativeCronetInitOnMainThread(Context context); |
| +} |