Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(57)

Side by Side Diff: components/cronet/android/java/src/org/chromium/net/impl/CronetLibraryLoader.java

Issue 2726613002: Fix ensureInitialized() to try if the previous attempt failed (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.impl; 5 package org.chromium.net.impl;
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.VisibleForTesting; 13 import org.chromium.base.VisibleForTesting;
14 import org.chromium.base.annotations.JNINamespace; 14 import org.chromium.base.annotations.JNINamespace;
15 import org.chromium.net.NetworkChangeNotifier; 15 import org.chromium.net.NetworkChangeNotifier;
16 16
17 /** 17 /**
18 * CronetLibraryLoader loads and initializes native library on main thread. 18 * CronetLibraryLoader loads and initializes native library on main thread.
19 */ 19 */
20 @JNINamespace("cronet") 20 @JNINamespace("cronet")
21 @VisibleForTesting 21 @VisibleForTesting
22 public class CronetLibraryLoader { 22 public class CronetLibraryLoader {
23 // Synchronize initialization. 23 // Synchronize initialization.
24 private static final Object sLoadLock = new Object(); 24 private static final Object sLoadLock = new Object();
25 private static final String LIBRARY_NAME = "cronet"; 25 private static final String LIBRARY_NAME = "cronet";
26 private static final String TAG = CronetLibraryLoader.class.getSimpleName(); 26 private static final String TAG = CronetLibraryLoader.class.getSimpleName();
27 // Has library loading commenced? Setting guarded by sLoadLock. 27 // Has library loading commenced? Setting guarded by sLoadLock.
28 private static volatile boolean sInitStarted = false; 28 private static volatile boolean sLibraryLoaded = false;
29 // Has ensureMainThreadInitialized() completed? Only accessed on main threa d. 29 // Has ensureMainThreadInitialized() completed? Only accessed on main threa d.
30 private static boolean sMainThreadInitDone = false; 30 private static volatile boolean sMainThreadInitDone = false;
31 31
32 /** 32 /**
33 * Ensure that native library is loaded and initialized. Can be called from 33 * Ensure that native library is loaded and initialized. Can be called from
34 * any thread, the load and initialization is performed on main thread. 34 * any thread, the load and initialization is performed on main thread.
35 */ 35 */
36 public static void ensureInitialized( 36 public static void ensureInitialized(
37 final Context applicationContext, final CronetEngineBuilderImpl buil der) { 37 final Context applicationContext, final CronetEngineBuilderImpl buil der) {
38 synchronized (sLoadLock) { 38 synchronized (sLoadLock) {
39 if (sInitStarted) { 39 if (sLibraryLoaded && sMainThreadInitDone) {
40 return; 40 return;
41 } 41 }
pauljensen 2017/02/28 19:07:52 nit: remove if-statement as it's redundant with if
42 sInitStarted = true; 42
43 ContextUtils.initApplicationContext(applicationContext); 43 if (!sLibraryLoaded) {
44 if (builder.libraryLoader() != null) { 44 ContextUtils.initApplicationContext(applicationContext);
45 builder.libraryLoader().loadLibrary(LIBRARY_NAME); 45 if (builder.libraryLoader() != null) {
46 } else { 46 builder.libraryLoader().loadLibrary(LIBRARY_NAME);
47 System.loadLibrary(LIBRARY_NAME); 47 } else {
48 System.loadLibrary(LIBRARY_NAME);
49 }
50 ContextUtils.initApplicationContextForNative();
51 String implVersion = ImplVersion.getCronetVersion();
52 if (!implVersion.equals(nativeGetCronetVersion())) {
53 throw new RuntimeException(String.format("Expected Cronet ve rsion number %s, "
54 + "actual version number %s.",
55 implVersion, nativeGetCronetVersion()));
56 }
57 Log.i(TAG, "Cronet version: %s, arch: %s", implVersion,
58 System.getProperty("os.arch"));
59 sLibraryLoaded = true;
48 } 60 }
49 ContextUtils.initApplicationContextForNative(); 61
50 String implVersion = ImplVersion.getCronetVersion(); 62 if (!sMainThreadInitDone) {
51 if (!implVersion.equals(nativeGetCronetVersion())) { 63 // Init native Chromium CronetEngine on Main UI thread.
52 throw new RuntimeException(String.format("Expected Cronet versio n number %s, " 64 Runnable task = new Runnable() {
53 + "actual version number %s.", 65 @Override
54 implVersion, nativeGetCronetVersion())); 66 public void run() {
55 } 67 ensureInitializedOnMainThread(applicationContext);
56 Log.i(TAG, "Cronet version: %s, arch: %s", implVersion, System.getPr operty("os.arch")); 68 }
57 // Init native Chromium CronetEngine on Main UI thread. 69 };
58 Runnable task = new Runnable() { 70 // Run task immediately or post it to the UI thread.
59 @Override 71 if (Looper.getMainLooper() == Looper.myLooper()) {
60 public void run() { 72 task.run();
61 ensureInitializedOnMainThread(applicationContext); 73 } else {
74 // The initOnMainThread will complete on the main thread pri or
75 // to other tasks posted to the main thread.
76 new Handler(Looper.getMainLooper()).post(task);
62 } 77 }
63 };
64 // Run task immediately or post it to the UI thread.
65 if (Looper.getMainLooper() == Looper.myLooper()) {
66 task.run();
67 } else {
68 // The initOnMainThread will complete on the main thread prior
69 // to other tasks posted to the main thread.
70 new Handler(Looper.getMainLooper()).post(task);
71 } 78 }
72 } 79 }
73 } 80 }
74 81
75 /** 82 /**
76 * Ensure that the main thread initialization has completed. Can only be cal led from 83 * Ensure that the main thread initialization has completed. Can only be cal led from
77 * the main thread. Ensures that the NetworkChangeNotifier is initialzied an d the 84 * the main thread. Ensures that the NetworkChangeNotifier is initialzied an d the
78 * main thread native MessageLoop is initialized. 85 * main thread native MessageLoop is initialized.
79 */ 86 */
80 static void ensureInitializedOnMainThread(Context context) { 87 static void ensureInitializedOnMainThread(Context context) {
81 assert sInitStarted; 88 assert sLibraryLoaded;
82 assert Looper.getMainLooper() == Looper.myLooper(); 89 assert Looper.getMainLooper() == Looper.myLooper();
83 if (sMainThreadInitDone) { 90 if (sMainThreadInitDone) {
84 return; 91 return;
85 } 92 }
86 NetworkChangeNotifier.init(context); 93 NetworkChangeNotifier.init(context);
87 // Registers to always receive network notifications. Note 94 // Registers to always receive network notifications. Note
88 // that this call is fine for Cronet because Cronet 95 // that this call is fine for Cronet because Cronet
89 // embedders do not have API access to create network change 96 // embedders do not have API access to create network change
90 // observers. Existing observers in the net stack do not 97 // observers. Existing observers in the net stack do not
91 // perform expensive work. 98 // perform expensive work.
92 NetworkChangeNotifier.registerToReceiveNotificationsAlways(); 99 NetworkChangeNotifier.registerToReceiveNotificationsAlways();
93 // registerToReceiveNotificationsAlways() is called before the native 100 // registerToReceiveNotificationsAlways() is called before the native
94 // NetworkChangeNotifierAndroid is created, so as to avoid receiving 101 // NetworkChangeNotifierAndroid is created, so as to avoid receiving
95 // the undesired initial network change observer notification, which 102 // the undesired initial network change observer notification, which
96 // will cause active requests to fail with ERR_NETWORK_CHANGED. 103 // will cause active requests to fail with ERR_NETWORK_CHANGED.
97 nativeCronetInitOnMainThread(); 104 nativeCronetInitOnMainThread();
98 sMainThreadInitDone = true; 105 sMainThreadInitDone = true;
99 } 106 }
100 107
101 // Native methods are implemented in cronet_library_loader.cc. 108 // Native methods are implemented in cronet_library_loader.cc.
102 private static native void nativeCronetInitOnMainThread(); 109 private static native void nativeCronetInitOnMainThread();
103 private static native String nativeGetCronetVersion(); 110 private static native String nativeGetCronetVersion();
104 } 111 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698