Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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.base.library_loader; | 5 package org.chromium.base.library_loader; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.os.SystemClock; | 8 import android.os.SystemClock; |
| 9 import android.util.Log; | 9 import android.util.Log; |
| 10 | 10 |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 30 @JNINamespace("base::android") | 30 @JNINamespace("base::android") |
| 31 public class LibraryLoader { | 31 public class LibraryLoader { |
| 32 private static final String TAG = "LibraryLoader"; | 32 private static final String TAG = "LibraryLoader"; |
| 33 | 33 |
| 34 // Guards all access to the libraries | 34 // Guards all access to the libraries |
| 35 private static final Object sLock = new Object(); | 35 private static final Object sLock = new Object(); |
| 36 | 36 |
| 37 // One-way switch becomes true when the libraries are loaded. | 37 // One-way switch becomes true when the libraries are loaded. |
| 38 private static boolean sLoaded = false; | 38 private static boolean sLoaded = false; |
| 39 | 39 |
| 40 // One-way switch becomes true when the native command line is initialized ( | |
| 41 // by calling nativeInitCommandLine, which forwards to | |
| 42 // InitCommandLine(...) in library_loader_hooks.cc). | |
| 43 private static boolean sCommandLineInitialized = false; | |
| 44 | |
| 40 // One-way switch becomes true when the libraries are initialized ( | 45 // One-way switch becomes true when the libraries are initialized ( |
| 41 // by calling nativeLibraryLoaded, which forwards to LibraryLoaded(...) in | 46 // by calling nativeLibraryLoaded, which forwards to LibraryLoaded(...) in |
| 42 // library_loader_hooks.cc). | 47 // library_loader_hooks.cc). |
| 43 private static boolean sInitialized = false; | 48 private static boolean sInitialized = false; |
| 44 | 49 |
| 45 // One-way switch becomes true if the system library loading failed, | 50 // One-way switch becomes true if the system library loading failed, |
| 46 // and the right native library was found and loaded by the hack. | 51 // and the right native library was found and loaded by the hack. |
| 47 // The flag is used to report UMA stats later. | 52 // The flag is used to report UMA stats later. |
| 48 private static boolean sNativeLibraryHackWasUsed = false; | 53 private static boolean sNativeLibraryHackWasUsed = false; |
| 49 | 54 |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 76 */ | 81 */ |
| 77 public static void ensureInitialized( | 82 public static void ensureInitialized( |
| 78 Context context, boolean shouldDeleteOldWorkaroundLibraries) | 83 Context context, boolean shouldDeleteOldWorkaroundLibraries) |
| 79 throws ProcessInitException { | 84 throws ProcessInitException { |
| 80 synchronized (sLock) { | 85 synchronized (sLock) { |
| 81 if (sInitialized) { | 86 if (sInitialized) { |
| 82 // Already initialized, nothing to do. | 87 // Already initialized, nothing to do. |
| 83 return; | 88 return; |
| 84 } | 89 } |
| 85 loadAlreadyLocked(context, shouldDeleteOldWorkaroundLibraries); | 90 loadAlreadyLocked(context, shouldDeleteOldWorkaroundLibraries); |
| 86 initializeAlreadyLocked(CommandLine.getJavaSwitchesOrNull()); | 91 |
| 92 // We can only call getJavaSwitchesOrNull if we have not yet | |
| 93 // switched CommandLine to native. | |
| 94 String[] switches = null; | |
| 95 if (!sCommandLineInitialized) { | |
| 96 switches = CommandLine.getJavaSwitchesOrNull(); | |
| 97 } | |
| 98 initializeAlreadyLocked(switches); | |
| 87 } | 99 } |
| 88 } | 100 } |
| 89 | 101 |
| 90 /** | 102 /** |
| 91 * Checks if library is fully loaded and initialized. | 103 * Checks if library is fully loaded and initialized. |
| 92 */ | 104 */ |
| 93 public static boolean isInitialized() { | 105 public static boolean isInitialized() { |
| 94 synchronized (sLock) { | 106 synchronized (sLock) { |
| 95 return sInitialized; | 107 return sInitialized; |
| 96 } | 108 } |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 193 Log.i(TAG, String.format( | 205 Log.i(TAG, String.format( |
| 194 "Expected native library version number \"%s\"," + | 206 "Expected native library version number \"%s\"," + |
| 195 "actual native library version number \"%s\"", | 207 "actual native library version number \"%s\"", |
| 196 NativeLibraries.VERSION_NUMBER, | 208 NativeLibraries.VERSION_NUMBER, |
| 197 nativeGetVersionNumber())); | 209 nativeGetVersionNumber())); |
| 198 if (!NativeLibraries.VERSION_NUMBER.equals(nativeGetVersionNumber())) { | 210 if (!NativeLibraries.VERSION_NUMBER.equals(nativeGetVersionNumber())) { |
| 199 throw new ProcessInitException(LoaderErrors.LOADER_ERROR_NATIVE_LIBR ARY_WRONG_VERSION); | 211 throw new ProcessInitException(LoaderErrors.LOADER_ERROR_NATIVE_LIBR ARY_WRONG_VERSION); |
| 200 } | 212 } |
| 201 } | 213 } |
| 202 | 214 |
| 215 /** | |
| 216 * Set the native command line if it is has not been set yet. | |
| 217 * If the command line is set already the arguments are ignored. | |
| 218 * The library must have previously been loaded with loadNow. | |
| 219 */ | |
| 220 public static void ensureCommandLineInit(String[] initCommandLine) { | |
|
bulach
2014/05/21 11:33:49
hmm... this method seems dangerous, it "ensures" b
hjd
2014/05/22 11:09:10
Yeah I felt bad writing that name... I've changed
| |
| 221 synchronized (sLock) { | |
| 222 ensureCommandLineInitAlreadyLocked(initCommandLine); | |
| 223 } | |
| 224 } | |
| 225 | |
| 226 private static void ensureCommandLineInitAlreadyLocked(String[] initCommandL ine) { | |
| 227 if (sCommandLineInitialized) { | |
| 228 return; | |
| 229 } | |
| 230 nativeInitCommandLine(initCommandLine); | |
|
bulach
2014/05/21 11:33:49
calling an unbound method throws some fairly crypt
hjd
2014/05/22 11:09:10
I added a lot of extra LoaderErrors and checked th
| |
| 231 CommandLine.enableNativeProxy(); | |
| 232 sCommandLineInitialized = true; | |
| 233 } | |
| 234 | |
| 203 // Invoke base::android::LibraryLoaded in library_loader_hooks.cc | 235 // Invoke base::android::LibraryLoaded in library_loader_hooks.cc |
| 204 private static void initializeAlreadyLocked(String[] initCommandLine) | 236 private static void initializeAlreadyLocked(String[] initCommandLine) |
| 205 throws ProcessInitException { | 237 throws ProcessInitException { |
| 206 if (sInitialized) { | 238 if (sInitialized) { |
| 207 return; | 239 return; |
| 208 } | 240 } |
| 209 if (!nativeLibraryLoaded(initCommandLine)) { | 241 |
| 242 ensureCommandLineInitAlreadyLocked(initCommandLine); | |
| 243 | |
| 244 if (!nativeLibraryLoaded()) { | |
| 210 Log.e(TAG, "error calling nativeLibraryLoaded"); | 245 Log.e(TAG, "error calling nativeLibraryLoaded"); |
| 211 throw new ProcessInitException(LoaderErrors.LOADER_ERROR_FAILED_TO_R EGISTER_JNI); | 246 throw new ProcessInitException(LoaderErrors.LOADER_ERROR_FAILED_TO_R EGISTER_JNI); |
| 212 } | 247 } |
| 213 // From this point on, native code is ready to use and checkIsReady() | 248 // From this point on, native code is ready to use and checkIsReady() |
| 214 // shouldn't complain from now on (and in fact, it's used by the | 249 // shouldn't complain from now on (and in fact, it's used by the |
| 215 // following calls). | 250 // following calls). |
| 216 sInitialized = true; | 251 sInitialized = true; |
| 217 CommandLine.enableNativeProxy(); | |
| 218 | 252 |
| 219 // From now on, keep tracing in sync with native. | 253 // From now on, keep tracing in sync with native. |
| 220 TraceEvent.registerNativeEnabledObserver(); | 254 TraceEvent.registerNativeEnabledObserver(); |
| 221 | 255 |
| 222 // Record histogram for the Chromium linker. | 256 // Record histogram for the Chromium linker. |
| 223 if (Linker.isUsed()) { | 257 if (Linker.isUsed()) { |
| 224 nativeRecordChromiumAndroidLinkerHistogram(Linker.loadAtFixedAddress Failed(), | 258 nativeRecordChromiumAndroidLinkerHistogram(Linker.loadAtFixedAddress Failed(), |
| 225 SysUtils.isLowEndDevice()); | 259 SysUtils.isLowEndDevice()); |
| 226 } | 260 } |
| 227 | 261 |
| 228 nativeRecordNativeLibraryHack(sNativeLibraryHackWasUsed); | 262 nativeRecordNativeLibraryHack(sNativeLibraryHackWasUsed); |
| 229 } | 263 } |
| 230 | 264 |
| 265 // Invokes InitCommandLine in library_loader_hooks.cc to set the native comm and line. | |
| 266 private static native void nativeInitCommandLine(String[] commandLine); | |
| 267 | |
| 231 // Only methods needed before or during normal JNI registration are during S ystem.OnLoad. | 268 // Only methods needed before or during normal JNI registration are during S ystem.OnLoad. |
| 232 // nativeLibraryLoaded is then called to register everything else. This pro cess is called | 269 // nativeLibraryLoaded is then called to register everything else. This pro cess is called |
| 233 // "initialization". This method will be mapped (by generated code) to the LibraryLoaded | 270 // "initialization". This method will be mapped (by generated code) to the LibraryLoaded |
| 234 // definition in base/android/library_loader/library_loader_hooks.cc. | 271 // definition in base/android/library_loader/library_loader_hooks.cc. |
| 235 // | 272 // |
| 236 // Return true on success and false on failure. | 273 // Return true on success and false on failure. |
| 237 private static native boolean nativeLibraryLoaded(String[] initCommandLine); | 274 private static native boolean nativeLibraryLoaded(); |
| 238 | 275 |
| 239 // Method called to record statistics about the Chromium linker operation, | 276 // Method called to record statistics about the Chromium linker operation, |
| 240 // i.e. whether the library failed to be loaded at a fixed address, and | 277 // i.e. whether the library failed to be loaded at a fixed address, and |
| 241 // whether the device is 'low-memory'. | 278 // whether the device is 'low-memory'. |
| 242 private static native void nativeRecordChromiumAndroidLinkerHistogram( | 279 private static native void nativeRecordChromiumAndroidLinkerHistogram( |
| 243 boolean loadedAtFixedAddressFailed, | 280 boolean loadedAtFixedAddressFailed, |
| 244 boolean isLowMemoryDevice); | 281 boolean isLowMemoryDevice); |
| 245 | 282 |
| 246 // Get the version of the native library. This is needed so that we can chec k we | 283 // Get the version of the native library. This is needed so that we can chec k we |
| 247 // have the right version before initializing the (rest of the) JNI. | 284 // have the right version before initializing the (rest of the) JNI. |
| 248 private static native String nativeGetVersionNumber(); | 285 private static native String nativeGetVersionNumber(); |
| 249 | 286 |
| 250 private static native void nativeRecordNativeLibraryHack(boolean usedHack); | 287 private static native void nativeRecordNativeLibraryHack(boolean usedHack); |
| 251 } | 288 } |
| OLD | NEW |