OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.net; | |
6 | |
7 import android.content.Context; | |
8 import android.os.Handler; | |
9 import android.os.Looper; | |
10 | |
11 import org.chromium.base.JNINamespace; | |
12 | |
13 /** | |
14 * CronetLibraryLoader loads and initializes native library on main thread. | |
15 */ | |
16 @JNINamespace("cronet") | |
17 public class CronetLibraryLoader { | |
mmenke
2015/01/23 20:41:41
Do we need to expose this externally?
mef
2015/01/28 21:32:28
Good point. Done.
| |
18 /** | |
19 * Synchronize access to sLoadComplete and initialization routine. | |
20 */ | |
21 private static final Object sLoadLock = new Object(); | |
22 private static boolean sLoadComplete = false; | |
mmenke
2015/01/23 20:41:41
sLoadComplete doesn't seem like a great name. sI
mef
2015/01/28 21:32:28
Done.
| |
23 | |
24 /** | |
25 * Ensure that native library is loaded and initialized. Can be called from | |
26 * any thread, the load and initialization is performed on main thread. | |
27 */ | |
28 public static void ensureInitialized( | |
29 final Context context, final UrlRequestContextConfig config) { | |
30 synchronized (sLoadLock) { | |
31 if (sLoadComplete) { | |
32 return; | |
33 } | |
34 System.loadLibrary(config.libraryName()); | |
35 // Init native Chromium URLRequestContext on Main UI thread. | |
36 Runnable task = new Runnable() { | |
37 public void run() { | |
38 initOnMainThread(context); | |
39 } | |
40 }; | |
41 // Run task immediately or post it to the UI thread. | |
42 if (Looper.getMainLooper() == Looper.myLooper()) { | |
43 task.run(); | |
44 } else { | |
45 // The initOnMainThread will complete on the main thread prior | |
46 // to other tasks posted to the main thread. | |
47 new Handler(Looper.getMainLooper()).post(task); | |
48 } | |
49 sLoadComplete = true; | |
50 } | |
51 } | |
52 | |
53 private static void initOnMainThread(final Context context) { | |
54 NetworkChangeNotifier.init(context); | |
55 // Registers to always receive network notifications. Note | |
56 // that this call is fine for Cronet because Cronet | |
57 // embedders do not have API access to create network change | |
58 // observers. Existing observers in the net stack do not | |
59 // perform expensive work. | |
60 NetworkChangeNotifier.registerToReceiveNotificationsAlways(); | |
61 nativeCronetInitOnMainThread(context); | |
62 } | |
63 | |
64 // Native methods are implemented in cronet_loader.cc. | |
65 private static native long nativeCronetInitOnMainThread(Context context); | |
66 } | |
OLD | NEW |