OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.net; | 5 package org.chromium.net; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.os.Handler; | 8 import android.os.Handler; |
9 import android.os.Looper; | 9 import android.os.Looper; |
10 | 10 |
11 import org.chromium.base.ContextUtils; | 11 import org.chromium.base.ContextUtils; |
12 import org.chromium.base.Log; | 12 import org.chromium.base.Log; |
13 import org.chromium.base.annotations.JNINamespace; | 13 import org.chromium.base.annotations.JNINamespace; |
14 | 14 |
15 /** | 15 /** |
16 * CronetLibraryLoader loads and initializes native library on main thread. | 16 * CronetLibraryLoader loads and initializes native library on main thread. |
17 */ | 17 */ |
18 @JNINamespace("cronet") | 18 @JNINamespace("cronet") |
19 class CronetLibraryLoader { | 19 class CronetLibraryLoader { |
20 /** | 20 // Synchronize initialization. |
21 * Synchronize access to sInitTaskPosted and initialization routine. | |
22 */ | |
23 private static final Object sLoadLock = new Object(); | 21 private static final Object sLoadLock = new Object(); |
24 private static final String TAG = "CronetLibraryLoader"; | 22 private static final String TAG = "CronetLibraryLoader"; |
25 private static boolean sInitTaskPosted = false; | 23 // Has library loading commenced? Setting guarded by sLoadLock. |
mef
2016/04/27 16:43:47
add @GuardedBy?
pauljensen
2016/04/27 16:56:42
I read it in an assert on another thread and I don
| |
24 private static volatile boolean sInitStarted = false; | |
25 // Has ensureMainThreadInitialized() completed? Only accessed on main threa d. | |
26 private static boolean sMainThreadInitDone = false; | |
mef
2016/04/27 16:43:47
Maybe call it sInitDone?
pauljensen
2016/04/27 16:56:42
I'd rather not as this variable is very specific t
| |
26 | 27 |
27 /** | 28 /** |
28 * Ensure that native library is loaded and initialized. Can be called from | 29 * Ensure that native library is loaded and initialized. Can be called from |
29 * any thread, the load and initialization is performed on main thread. | 30 * any thread, the load and initialization is performed on main thread. |
30 */ | 31 */ |
31 public static void ensureInitialized( | 32 public static void ensureInitialized( |
32 final Context context, final CronetEngine.Builder builder) { | 33 final Context context, final CronetEngine.Builder builder) { |
33 synchronized (sLoadLock) { | 34 synchronized (sLoadLock) { |
34 if (sInitTaskPosted) { | 35 if (sInitStarted) { |
35 return; | 36 return; |
36 } | 37 } |
38 sInitStarted = true; | |
37 builder.loadLibrary(); | 39 builder.loadLibrary(); |
38 if (!Version.CRONET_VERSION.equals(nativeGetCronetVersion())) { | 40 if (!Version.CRONET_VERSION.equals(nativeGetCronetVersion())) { |
39 throw new RuntimeException(String.format( | 41 throw new RuntimeException(String.format( |
40 "Expected Cronet version number %s, " | 42 "Expected Cronet version number %s, " |
41 + "actual version number %s.", | 43 + "actual version number %s.", |
42 Version.CRONET_VERSION, | 44 Version.CRONET_VERSION, |
43 nativeGetCronetVersion())); | 45 nativeGetCronetVersion())); |
44 } | 46 } |
45 Log.i(TAG, "Cronet version: %s, arch: %s", | 47 Log.i(TAG, "Cronet version: %s, arch: %s", |
46 Version.CRONET_VERSION, System.getProperty("os.arch")); | 48 Version.CRONET_VERSION, System.getProperty("os.arch")); |
47 ContextUtils.initApplicationContext(context.getApplicationContext()) ; | 49 ContextUtils.initApplicationContext(context.getApplicationContext()) ; |
48 // Init native Chromium CronetEngine on Main UI thread. | 50 // Init native Chromium CronetEngine on Main UI thread. |
49 Runnable task = new Runnable() { | 51 Runnable task = new Runnable() { |
50 public void run() { | 52 public void run() { |
51 initOnMainThread(context); | 53 ensureMainThreadInitialized(context); |
52 } | 54 } |
53 }; | 55 }; |
54 // Run task immediately or post it to the UI thread. | 56 // Run task immediately or post it to the UI thread. |
55 if (Looper.getMainLooper() == Looper.myLooper()) { | 57 if (Looper.getMainLooper() == Looper.myLooper()) { |
56 task.run(); | 58 task.run(); |
57 } else { | 59 } else { |
58 // The initOnMainThread will complete on the main thread prior | 60 // The initOnMainThread will complete on the main thread prior |
59 // to other tasks posted to the main thread. | 61 // to other tasks posted to the main thread. |
60 new Handler(Looper.getMainLooper()).post(task); | 62 new Handler(Looper.getMainLooper()).post(task); |
61 } | 63 } |
62 sInitTaskPosted = true; | |
63 } | 64 } |
64 } | 65 } |
65 | 66 |
66 private static void initOnMainThread(final Context context) { | 67 /** |
68 * Ensure that the main thread initialization has completed. Can only be cal led from | |
69 * the main thread. Ensures that the NetworkChangeNotifier is initialzied an d the | |
70 * main thread native MessageLoop is initialized. | |
71 */ | |
72 public static void ensureMainThreadInitialized(Context context) { | |
kapishnikov
2016/04/27 15:53:17
Don't need to be public.
mef
2016/04/27 16:43:47
Maybe call it 'ensureInitializedOnMainThread'?
It
pauljensen
2016/04/27 16:56:42
Done, also done for ensureLibraryLoaded.
pauljensen
2016/04/27 16:56:42
Done.
| |
73 assert sInitStarted; | |
74 assert Looper.getMainLooper() == Looper.myLooper(); | |
75 if (sMainThreadInitDone) { | |
76 return; | |
77 } | |
67 NetworkChangeNotifier.init(context); | 78 NetworkChangeNotifier.init(context); |
68 // Registers to always receive network notifications. Note | 79 // Registers to always receive network notifications. Note |
69 // that this call is fine for Cronet because Cronet | 80 // that this call is fine for Cronet because Cronet |
70 // embedders do not have API access to create network change | 81 // embedders do not have API access to create network change |
71 // observers. Existing observers in the net stack do not | 82 // observers. Existing observers in the net stack do not |
72 // perform expensive work. | 83 // perform expensive work. |
73 NetworkChangeNotifier.registerToReceiveNotificationsAlways(); | 84 NetworkChangeNotifier.registerToReceiveNotificationsAlways(); |
74 // registerToReceiveNotificationsAlways() is called before the native | 85 // registerToReceiveNotificationsAlways() is called before the native |
75 // NetworkChangeNotifierAndroid is created, so as to avoid receiving | 86 // NetworkChangeNotifierAndroid is created, so as to avoid receiving |
76 // the undesired initial network change observer notification, which | 87 // the undesired initial network change observer notification, which |
77 // will cause active requests to fail with ERR_NETWORK_CHANGED. | 88 // will cause active requests to fail with ERR_NETWORK_CHANGED. |
78 nativeCronetInitOnMainThread(); | 89 nativeCronetInitOnMainThread(); |
90 sMainThreadInitDone = true; | |
79 } | 91 } |
80 | 92 |
81 // Native methods are implemented in cronet_library_loader.cc. | 93 // Native methods are implemented in cronet_library_loader.cc. |
82 private static native void nativeCronetInitOnMainThread(); | 94 private static native void nativeCronetInitOnMainThread(); |
83 private static native String nativeGetCronetVersion(); | 95 private static native String nativeGetCronetVersion(); |
84 } | 96 } |
OLD | NEW |