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 { | |
18 /** | |
19 * Synchronize access to mInitComplete and initialization routine. | |
20 */ | |
21 private static final Object sLoadAndInitLock = new Object(); | |
22 private static boolean sLoadAndInitComplete = false; | |
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 (sLoadAndInitLock) { | |
31 if (sLoadAndInitComplete) { | |
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()) { | |
mmenke
2015/01/08 19:06:16
If we're on a random executor thread, what does Lo
mef
2015/01/08 21:17:02
Returns null if the calling thread is not associat
| |
43 task.run(); | |
44 } else { | |
45 new Handler(Looper.getMainLooper()).post(task); | |
46 } | |
47 } | |
48 } | |
49 | |
50 private static void initOnMainThread(final Context context) { | |
51 synchronized (sLoadAndInitLock) { | |
52 if (sLoadAndInitComplete) { | |
53 return; | |
54 } | |
55 NetworkChangeNotifier.init(context); | |
56 // Registers to always receive network notifications. Note | |
57 // that this call is fine for Cronet because Cronet | |
58 // embedders do not have API access to create network change | |
59 // observers. Existing observers in the net stack do not | |
60 // perform expensive work. | |
61 NetworkChangeNotifier.registerToReceiveNotificationsAlways(); | |
62 nativeCronetInitOnMainThread(context); | |
63 sLoadAndInitComplete = true; | |
mmenke
2015/01/08 19:06:16
IS there a reason we have to set load and init com
mef
2015/01/08 21:17:02
In order to set sLoadAndInitComplete in ensureInit
mmenke
2015/01/12 15:28:26
I don't follow. We're not blocking anywhere until
mef
2015/01/12 17:19:56
Done.
| |
64 } | |
65 } | |
66 | |
67 // Native methods are implemented in cronet_loader.cc. | |
68 private static native long nativeCronetInitOnMainThread(Context context); | |
69 } | |
OLD | NEW |