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..424447b94b5f258f3e9dd1ad6ac6f440f9320c4d |
| --- /dev/null |
| +++ b/components/cronet/android/java/src/org/chromium/net/CronetLibraryLoader.java |
| @@ -0,0 +1,61 @@ |
| +// Copyright 2014 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.ConditionVariable; |
| +import android.os.Handler; |
| +import android.os.Looper; |
| + |
| +import org.chromium.base.JNINamespace; |
| + |
| +/** |
| + * CronetLibraryLoader loads and initializes native library on main thread. |
| + */ |
| +@JNINamespace("cronet") |
| +public class CronetLibraryLoader { |
| + /** |
| + * Synchronize access to mInitComplete and initialization routine. |
| + */ |
| + private static final Object sLoadAndInitLock = new Object(); |
| + private static boolean sLoadAndInitComplete = false; |
| + private static final ConditionVariable sInitialized = new ConditionVariable(); |
| + |
| + public static void loadAndInitialize( |
| + final Context context, final UrlRequestContextConfig config) { |
| + synchronized (sLoadAndInitLock) { |
| + if (sLoadAndInitComplete) { |
| + return; |
| + } |
| + System.loadLibrary(config.libraryName()); |
| + // Init native Chromium URLRequestContext on Main UI thread. |
| + Runnable task = new Runnable() { |
| + public void run() { |
| + 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/01/05 14:48:03
I wonder whether we should do "nativeCronetInitOnM
mef
2015/01/05 16:59:21
Good question. I've copied it from ChromiumUrlRequ
pauljensen
2015/01/06 17:38:40
I don't think the order matters. The only advanta
|
| + sInitialized.open(); |
| + } |
| + }; |
| + // Run task immediately or post it to the UI thread. |
| + if (Looper.getMainLooper() == Looper.myLooper()) { |
| + task.run(); |
| + } else { |
| + new Handler(Looper.getMainLooper()).post(task); |
| + } |
| + // Wait for initialization to complete on the main thread. |
| + sInitialized.block(5000); |
|
xunjieli
2015/01/05 14:48:03
Why do we need to wait for initialization to compl
mef
2015/01/05 16:59:21
The goal is to prevent race in case of concurrent
|
| + sLoadAndInitComplete = true; |
| + } |
| + } |
| + |
| + // Native methods are implemented in cronet_loader.cc. |
| + private static native long nativeCronetInitOnMainThread(Context context); |
| +} |