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

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: Removed redundant if statement 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) {
40 return; 40 ContextUtils.initApplicationContext(applicationContext);
41 if (builder.libraryLoader() != null) {
42 builder.libraryLoader().loadLibrary(LIBRARY_NAME);
43 } else {
44 System.loadLibrary(LIBRARY_NAME);
45 }
46 ContextUtils.initApplicationContextForNative();
47 String implVersion = ImplVersion.getCronetVersion();
48 if (!implVersion.equals(nativeGetCronetVersion())) {
49 throw new RuntimeException(String.format("Expected Cronet ve rsion number %s, "
50 + "actual version number %s.",
51 implVersion, nativeGetCronetVersion()));
52 }
53 Log.i(TAG, "Cronet version: %s, arch: %s", implVersion,
54 System.getProperty("os.arch"));
55 sLibraryLoaded = true;
41 } 56 }
42 sInitStarted = true; 57
43 ContextUtils.initApplicationContext(applicationContext); 58 if (!sMainThreadInitDone) {
44 if (builder.libraryLoader() != null) { 59 // Init native Chromium CronetEngine on Main UI thread.
45 builder.libraryLoader().loadLibrary(LIBRARY_NAME); 60 Runnable task = new Runnable() {
46 } else { 61 @Override
47 System.loadLibrary(LIBRARY_NAME); 62 public void run() {
48 } 63 ensureInitializedOnMainThread(applicationContext);
49 ContextUtils.initApplicationContextForNative(); 64 }
50 String implVersion = ImplVersion.getCronetVersion(); 65 };
51 if (!implVersion.equals(nativeGetCronetVersion())) { 66 // Run task immediately or post it to the UI thread.
52 throw new RuntimeException(String.format("Expected Cronet versio n number %s, " 67 if (Looper.getMainLooper() == Looper.myLooper()) {
53 + "actual version number %s.", 68 task.run();
54 implVersion, nativeGetCronetVersion())); 69 } else {
55 } 70 // The initOnMainThread will complete on the main thread pri or
56 Log.i(TAG, "Cronet version: %s, arch: %s", implVersion, System.getPr operty("os.arch")); 71 // to other tasks posted to the main thread.
57 // Init native Chromium CronetEngine on Main UI thread. 72 new Handler(Looper.getMainLooper()).post(task);
58 Runnable task = new Runnable() {
59 @Override
60 public void run() {
61 ensureInitializedOnMainThread(applicationContext);
62 } 73 }
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 } 74 }
72 } 75 }
73 } 76 }
74 77
75 /** 78 /**
76 * Ensure that the main thread initialization has completed. Can only be cal led from 79 * 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 80 * the main thread. Ensures that the NetworkChangeNotifier is initialzied an d the
78 * main thread native MessageLoop is initialized. 81 * main thread native MessageLoop is initialized.
79 */ 82 */
80 static void ensureInitializedOnMainThread(Context context) { 83 static void ensureInitializedOnMainThread(Context context) {
81 assert sInitStarted; 84 assert sLibraryLoaded;
82 assert Looper.getMainLooper() == Looper.myLooper(); 85 assert Looper.getMainLooper() == Looper.myLooper();
83 if (sMainThreadInitDone) { 86 if (sMainThreadInitDone) {
84 return; 87 return;
85 } 88 }
86 NetworkChangeNotifier.init(context); 89 NetworkChangeNotifier.init(context);
87 // Registers to always receive network notifications. Note 90 // Registers to always receive network notifications. Note
88 // that this call is fine for Cronet because Cronet 91 // that this call is fine for Cronet because Cronet
89 // embedders do not have API access to create network change 92 // embedders do not have API access to create network change
90 // observers. Existing observers in the net stack do not 93 // observers. Existing observers in the net stack do not
91 // perform expensive work. 94 // perform expensive work.
92 NetworkChangeNotifier.registerToReceiveNotificationsAlways(); 95 NetworkChangeNotifier.registerToReceiveNotificationsAlways();
93 // registerToReceiveNotificationsAlways() is called before the native 96 // registerToReceiveNotificationsAlways() is called before the native
94 // NetworkChangeNotifierAndroid is created, so as to avoid receiving 97 // NetworkChangeNotifierAndroid is created, so as to avoid receiving
95 // the undesired initial network change observer notification, which 98 // the undesired initial network change observer notification, which
96 // will cause active requests to fail with ERR_NETWORK_CHANGED. 99 // will cause active requests to fail with ERR_NETWORK_CHANGED.
97 nativeCronetInitOnMainThread(); 100 nativeCronetInitOnMainThread();
98 sMainThreadInitDone = true; 101 sMainThreadInitDone = true;
99 } 102 }
100 103
101 // Native methods are implemented in cronet_library_loader.cc. 104 // Native methods are implemented in cronet_library_loader.cc.
102 private static native void nativeCronetInitOnMainThread(); 105 private static native void nativeCronetInitOnMainThread();
103 private static native String nativeGetCronetVersion(); 106 private static native String nativeGetCronetVersion();
104 } 107 }
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