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

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

Issue 1934623002: Revert of patch suspect of causing Android GPU builds to start failing (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 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
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; 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 // Synchronize initialization. 20 /**
21 * Synchronize access to sInitTaskPosted and initialization routine.
22 */
21 private static final Object sLoadLock = new Object(); 23 private static final Object sLoadLock = new Object();
22 private static final String TAG = "CronetLibraryLoader"; 24 private static final String TAG = "CronetLibraryLoader";
23 // Has library loading commenced? Setting guarded by sLoadLock. 25 private static boolean sInitTaskPosted = false;
24 private static volatile boolean sInitStarted = false;
25 // Has ensureMainThreadInitialized() completed? Only accessed on main threa d.
26 private static boolean sMainThreadInitDone = false;
27 26
28 /** 27 /**
29 * Ensure that native library is loaded and initialized. Can be called from 28 * Ensure that native library is loaded and initialized. Can be called from
30 * any thread, the load and initialization is performed on main thread. 29 * any thread, the load and initialization is performed on main thread.
31 */ 30 */
32 static void ensureInitialized(final Context context, final CronetEngine.Buil der builder) { 31 public static void ensureInitialized(
32 final Context context, final CronetEngine.Builder builder) {
33 synchronized (sLoadLock) { 33 synchronized (sLoadLock) {
34 if (sInitStarted) { 34 if (sInitTaskPosted) {
35 return; 35 return;
36 } 36 }
37 sInitStarted = true;
38 ContextUtils.initApplicationContext(context.getApplicationContext()) ;
39 builder.loadLibrary(); 37 builder.loadLibrary();
40 ContextUtils.initApplicationContextForNative();
41 if (!Version.CRONET_VERSION.equals(nativeGetCronetVersion())) { 38 if (!Version.CRONET_VERSION.equals(nativeGetCronetVersion())) {
42 throw new RuntimeException(String.format( 39 throw new RuntimeException(String.format(
43 "Expected Cronet version number %s, " 40 "Expected Cronet version number %s, "
44 + "actual version number %s.", 41 + "actual version number %s.",
45 Version.CRONET_VERSION, 42 Version.CRONET_VERSION,
46 nativeGetCronetVersion())); 43 nativeGetCronetVersion()));
47 } 44 }
48 Log.i(TAG, "Cronet version: %s, arch: %s", 45 Log.i(TAG, "Cronet version: %s, arch: %s",
49 Version.CRONET_VERSION, System.getProperty("os.arch")); 46 Version.CRONET_VERSION, System.getProperty("os.arch"));
47 ContextUtils.initApplicationContext(context.getApplicationContext()) ;
50 // Init native Chromium CronetEngine on Main UI thread. 48 // Init native Chromium CronetEngine on Main UI thread.
51 Runnable task = new Runnable() { 49 Runnable task = new Runnable() {
52 @Override
53 public void run() { 50 public void run() {
54 ensureInitializedOnMainThread(context); 51 initOnMainThread(context);
55 } 52 }
56 }; 53 };
57 // Run task immediately or post it to the UI thread. 54 // Run task immediately or post it to the UI thread.
58 if (Looper.getMainLooper() == Looper.myLooper()) { 55 if (Looper.getMainLooper() == Looper.myLooper()) {
59 task.run(); 56 task.run();
60 } else { 57 } else {
61 // The initOnMainThread will complete on the main thread prior 58 // The initOnMainThread will complete on the main thread prior
62 // to other tasks posted to the main thread. 59 // to other tasks posted to the main thread.
63 new Handler(Looper.getMainLooper()).post(task); 60 new Handler(Looper.getMainLooper()).post(task);
64 } 61 }
62 sInitTaskPosted = true;
65 } 63 }
66 } 64 }
67 65
68 /** 66 private static void initOnMainThread(final Context context) {
69 * Ensure that the main thread initialization has completed. Can only be cal led from
70 * the main thread. Ensures that the NetworkChangeNotifier is initialzied an d the
71 * main thread native MessageLoop is initialized.
72 */
73 static void ensureInitializedOnMainThread(Context context) {
74 assert sInitStarted;
75 assert Looper.getMainLooper() == Looper.myLooper();
76 if (sMainThreadInitDone) {
77 return;
78 }
79 NetworkChangeNotifier.init(context); 67 NetworkChangeNotifier.init(context);
80 // Registers to always receive network notifications. Note 68 // Registers to always receive network notifications. Note
81 // that this call is fine for Cronet because Cronet 69 // that this call is fine for Cronet because Cronet
82 // embedders do not have API access to create network change 70 // embedders do not have API access to create network change
83 // observers. Existing observers in the net stack do not 71 // observers. Existing observers in the net stack do not
84 // perform expensive work. 72 // perform expensive work.
85 NetworkChangeNotifier.registerToReceiveNotificationsAlways(); 73 NetworkChangeNotifier.registerToReceiveNotificationsAlways();
86 // registerToReceiveNotificationsAlways() is called before the native 74 // registerToReceiveNotificationsAlways() is called before the native
87 // NetworkChangeNotifierAndroid is created, so as to avoid receiving 75 // NetworkChangeNotifierAndroid is created, so as to avoid receiving
88 // the undesired initial network change observer notification, which 76 // the undesired initial network change observer notification, which
89 // will cause active requests to fail with ERR_NETWORK_CHANGED. 77 // will cause active requests to fail with ERR_NETWORK_CHANGED.
90 nativeCronetInitOnMainThread(); 78 nativeCronetInitOnMainThread();
91 sMainThreadInitDone = true;
92 } 79 }
93 80
94 // Native methods are implemented in cronet_library_loader.cc. 81 // Native methods are implemented in cronet_library_loader.cc.
95 private static native void nativeCronetInitOnMainThread(); 82 private static native void nativeCronetInitOnMainThread();
96 private static native String nativeGetCronetVersion(); 83 private static native String nativeGetCronetVersion();
97 } 84 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698