| 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.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.annotations.JNINamespace; | 14 import org.chromium.base.annotations.JNINamespace; |
| 15 import org.chromium.net.CronetEngine; |
| 16 import org.chromium.net.NetworkChangeNotifier; |
| 14 | 17 |
| 15 /** | 18 /** |
| 16 * CronetLibraryLoader loads and initializes native library on main thread. | 19 * CronetLibraryLoader loads and initializes native library on main thread. |
| 17 */ | 20 */ |
| 18 @JNINamespace("cronet") | 21 @JNINamespace("cronet") |
| 19 class CronetLibraryLoader { | 22 @VisibleForTesting |
| 23 public class CronetLibraryLoader { |
| 20 // Synchronize initialization. | 24 // Synchronize initialization. |
| 21 private static final Object sLoadLock = new Object(); | 25 private static final Object sLoadLock = new Object(); |
| 22 private static final String TAG = "CronetLibraryLoader"; | 26 private static final String TAG = "CronetLibraryLoader"; |
| 23 // Has library loading commenced? Setting guarded by sLoadLock. | 27 // Has library loading commenced? Setting guarded by sLoadLock. |
| 24 private static volatile boolean sInitStarted = false; | 28 private static volatile boolean sInitStarted = false; |
| 25 // Has ensureMainThreadInitialized() completed? Only accessed on main threa
d. | 29 // Has ensureMainThreadInitialized() completed? Only accessed on main threa
d. |
| 26 private static boolean sMainThreadInitDone = false; | 30 private static boolean sMainThreadInitDone = false; |
| 27 | 31 |
| 28 /** | 32 /** |
| 29 * 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 |
| 30 * any thread, the load and initialization is performed on main thread. | 34 * any thread, the load and initialization is performed on main thread. |
| 31 */ | 35 */ |
| 32 static void ensureInitialized(final Context context, final CronetEngine.Buil
der builder) { | 36 public static void ensureInitialized( |
| 37 final Context context, final CronetEngine.Builder builder) { |
| 33 synchronized (sLoadLock) { | 38 synchronized (sLoadLock) { |
| 34 if (sInitStarted) { | 39 if (sInitStarted) { |
| 35 return; | 40 return; |
| 36 } | 41 } |
| 37 sInitStarted = true; | 42 sInitStarted = true; |
| 38 ContextUtils.initApplicationContext(context.getApplicationContext())
; | 43 ContextUtils.initApplicationContext(context.getApplicationContext())
; |
| 39 builder.loadLibrary(); | 44 builder.loadLibrary(); |
| 40 ContextUtils.initApplicationContextForNative(); | 45 ContextUtils.initApplicationContextForNative(); |
| 41 if (!Version.CRONET_VERSION.equals(nativeGetCronetVersion())) { | 46 if (!ImplVersion.CRONET_VERSION.equals(nativeGetCronetVersion())) { |
| 42 throw new RuntimeException(String.format( | 47 throw new RuntimeException(String.format("Expected Cronet versio
n number %s, " |
| 43 "Expected Cronet version number %s, " | 48 + "actual version number %s.", |
| 44 + "actual version number %s.", | 49 ImplVersion.CRONET_VERSION, nativeGetCronetVersion())); |
| 45 Version.CRONET_VERSION, | |
| 46 nativeGetCronetVersion())); | |
| 47 } | 50 } |
| 48 Log.i(TAG, "Cronet version: %s, arch: %s", | 51 Log.i(TAG, "Cronet version: %s, arch: %s", ImplVersion.CRONET_VERSIO
N, |
| 49 Version.CRONET_VERSION, System.getProperty("os.arch")); | 52 System.getProperty("os.arch")); |
| 50 // Init native Chromium CronetEngine on Main UI thread. | 53 // Init native Chromium CronetEngine on Main UI thread. |
| 51 Runnable task = new Runnable() { | 54 Runnable task = new Runnable() { |
| 52 @Override | 55 @Override |
| 53 public void run() { | 56 public void run() { |
| 54 ensureInitializedOnMainThread(context); | 57 ensureInitializedOnMainThread(context); |
| 55 } | 58 } |
| 56 }; | 59 }; |
| 57 // Run task immediately or post it to the UI thread. | 60 // Run task immediately or post it to the UI thread. |
| 58 if (Looper.getMainLooper() == Looper.myLooper()) { | 61 if (Looper.getMainLooper() == Looper.myLooper()) { |
| 59 task.run(); | 62 task.run(); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 88 // the undesired initial network change observer notification, which | 91 // the undesired initial network change observer notification, which |
| 89 // will cause active requests to fail with ERR_NETWORK_CHANGED. | 92 // will cause active requests to fail with ERR_NETWORK_CHANGED. |
| 90 nativeCronetInitOnMainThread(); | 93 nativeCronetInitOnMainThread(); |
| 91 sMainThreadInitDone = true; | 94 sMainThreadInitDone = true; |
| 92 } | 95 } |
| 93 | 96 |
| 94 // Native methods are implemented in cronet_library_loader.cc. | 97 // Native methods are implemented in cronet_library_loader.cc. |
| 95 private static native void nativeCronetInitOnMainThread(); | 98 private static native void nativeCronetInitOnMainThread(); |
| 96 private static native String nativeGetCronetVersion(); | 99 private static native String nativeGetCronetVersion(); |
| 97 } | 100 } |
| OLD | NEW |