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 24 matching lines...) Expand all Loading... | |
| 35 public class LibraryLoader { | 35 public class LibraryLoader { |
| 36 private static final String TAG = "LibraryLoader"; | 36 private static final String TAG = "LibraryLoader"; |
| 37 | 37 |
| 38 // Set to true to enable debug logs. | 38 // Set to true to enable debug logs. |
| 39 private static final boolean DEBUG = false; | 39 private static final boolean DEBUG = false; |
| 40 | 40 |
| 41 // Guards all access to the libraries | 41 // Guards all access to the libraries |
| 42 private static final Object sLock = new Object(); | 42 private static final Object sLock = new Object(); |
| 43 | 43 |
| 44 // The singleton instance of LibraryLoader. | 44 // The singleton instance of LibraryLoader. |
| 45 private static LibraryLoader sInstance; | 45 private static volatile LibraryLoader sInstance; |
| 46 | 46 |
| 47 // One-way switch becomes true when the libraries are loaded. | 47 // One-way switch becomes true when the libraries are loaded. |
| 48 private boolean mLoaded; | 48 private boolean mLoaded; |
| 49 | 49 |
| 50 // One-way switch becomes true when the Java command line is switched to | 50 // One-way switch becomes true when the Java command line is switched to |
| 51 // native. | 51 // native. |
| 52 private boolean mCommandLineSwitched; | 52 private boolean mCommandLineSwitched; |
| 53 | 53 |
| 54 // One-way switch becomes true when the libraries are initialized ( | 54 // One-way switch becomes true when the libraries are initialized ( |
| 55 // by calling nativeLibraryLoaded, which forwards to LibraryLoaded(...) in | 55 // by calling nativeLibraryLoaded, which forwards to LibraryLoaded(...) in |
| 56 // library_loader_hooks.cc). | 56 // library_loader_hooks.cc). |
| 57 private boolean mInitialized; | 57 private volatile boolean mInitialized; |
| 58 | 58 |
| 59 // One-way switches recording attempts to use Relro sharing in the browser. | 59 // One-way switches recording attempts to use Relro sharing in the browser. |
| 60 // The flags are used to report UMA stats later. | 60 // The flags are used to report UMA stats later. |
| 61 private boolean mIsUsingBrowserSharedRelros; | 61 private boolean mIsUsingBrowserSharedRelros; |
| 62 private boolean mLoadAtFixedAddressFailed; | 62 private boolean mLoadAtFixedAddressFailed; |
| 63 | 63 |
| 64 // One-way switch becomes true if the device supports memory mapping the | 64 // One-way switch becomes true if the device supports memory mapping the |
| 65 // APK file with executable permissions. | 65 // APK file with executable permissions. |
| 66 private boolean mMapApkWithExecPermission; | 66 private boolean mMapApkWithExecPermission; |
| 67 | 67 |
| 68 // One-way switch to indicate whether we probe for memory mapping the APK | 68 // One-way switch to indicate whether we probe for memory mapping the APK |
| 69 // file with executable permissions. We suppress the probe under some | 69 // file with executable permissions. We suppress the probe under some |
| 70 // conditions. | 70 // conditions. |
| 71 // For more, see: | 71 // For more, see: |
| 72 // https://code.google.com/p/chromium/issues/detail?id=448084 | 72 // https://code.google.com/p/chromium/issues/detail?id=448084 |
| 73 private boolean mProbeMapApkWithExecPermission = true; | 73 private boolean mProbeMapApkWithExecPermission = true; |
| 74 | 74 |
| 75 // One-way switch becomes true if the Chromium library was loaded from the | 75 // One-way switch becomes true if the Chromium library was loaded from the |
| 76 // APK file directly. | 76 // APK file directly. |
| 77 private boolean mLibraryWasLoadedFromApk; | 77 private boolean mLibraryWasLoadedFromApk; |
| 78 | 78 |
| 79 // One-way switch becomes false if the Chromium library should be loaded | 79 // One-way switch becomes false if the Chromium library should be loaded |
| 80 // directly from the APK file but it was compressed or not aligned. | 80 // directly from the APK file but it was compressed or not aligned. |
| 81 private boolean mLibraryIsMappableInApk = true; | 81 private boolean mLibraryIsMappableInApk = true; |
| 82 | 82 |
| 83 // The type of process the shared library is loaded in. | 83 // The type of process the shared library is loaded in. |
| 84 private int mLibraryProcessType; | 84 private volatile int mLibraryProcessType; |
|
michaelbai
2015/02/25 04:57:12
You probably should use final instead of volatile
ripp
2015/02/25 08:32:23
Done.
| |
| 85 | 85 |
| 86 /** | 86 /** |
| 87 * @param libraryProcessType the process the shared library is loaded in. re fer to | 87 * @param libraryProcessType the process the shared library is loaded in. re fer to |
| 88 * LibraryProcessType for possible values. | 88 * LibraryProcessType for possible values. |
| 89 * @return LibraryLoader if existing, otherwise create a new one. | 89 * @return LibraryLoader if existing, otherwise create a new one. |
| 90 */ | 90 */ |
| 91 public static LibraryLoader get(int libraryProcessType) throws ProcessInitEx ception { | 91 public static LibraryLoader get(int libraryProcessType) throws ProcessInitEx ception { |
| 92 synchronized (sLock) { | 92 synchronized (sLock) { |
| 93 if (sInstance != null) { | 93 if (sInstance != null) { |
| 94 if (sInstance.mLibraryProcessType == libraryProcessType) return sInstance; | 94 if (sInstance.mLibraryProcessType == libraryProcessType) return sInstance; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 } | 134 } |
| 135 loadAlreadyLocked(context, shouldDeleteFallbackLibraries); | 135 loadAlreadyLocked(context, shouldDeleteFallbackLibraries); |
| 136 initializeAlreadyLocked(); | 136 initializeAlreadyLocked(); |
| 137 } | 137 } |
| 138 } | 138 } |
| 139 | 139 |
| 140 /** | 140 /** |
| 141 * Checks if library is fully loaded and initialized. | 141 * Checks if library is fully loaded and initialized. |
| 142 */ | 142 */ |
| 143 public static boolean isInitialized() { | 143 public static boolean isInitialized() { |
| 144 synchronized (sLock) { | 144 return sInstance != null && sInstance.mInitialized; |
| 145 return sInstance != null && sInstance.mInitialized; | |
| 146 } | |
| 147 } | 145 } |
| 148 | 146 |
| 149 /** | 147 /** |
| 150 * The same as loadNow(null, false), should only be called by | 148 * The same as loadNow(null, false), should only be called by |
| 151 * non-browser process. | 149 * non-browser process. |
| 152 * | 150 * |
| 153 * @throws ProcessInitException | 151 * @throws ProcessInitException |
| 154 */ | 152 */ |
| 155 public void loadNow() throws ProcessInitException { | 153 public void loadNow() throws ProcessInitException { |
| 156 loadNow(null, false); | 154 loadNow(null, false); |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 370 | 368 |
| 371 // Setup the native command line if necessary. | 369 // Setup the native command line if necessary. |
| 372 if (!mCommandLineSwitched) { | 370 if (!mCommandLineSwitched) { |
| 373 nativeInitCommandLine(CommandLine.getJavaSwitchesOrNull()); | 371 nativeInitCommandLine(CommandLine.getJavaSwitchesOrNull()); |
| 374 } | 372 } |
| 375 | 373 |
| 376 if (!nativeLibraryLoaded()) { | 374 if (!nativeLibraryLoaded()) { |
| 377 Log.e(TAG, "error calling nativeLibraryLoaded"); | 375 Log.e(TAG, "error calling nativeLibraryLoaded"); |
| 378 throw new ProcessInitException(LoaderErrors.LOADER_ERROR_FAILED_TO_R EGISTER_JNI); | 376 throw new ProcessInitException(LoaderErrors.LOADER_ERROR_FAILED_TO_R EGISTER_JNI); |
| 379 } | 377 } |
| 380 // From this point on, native code is ready to use and checkIsReady() | |
| 381 // shouldn't complain from now on (and in fact, it's used by the | |
| 382 // following calls). | |
| 383 mInitialized = true; | |
| 384 | 378 |
| 385 // The Chrome JNI is registered by now so we can switch the Java | 379 // The Chrome JNI is registered by now so we can switch the Java |
| 386 // command line over to delegating to native if it's necessary. | 380 // command line over to delegating to native if it's necessary. |
| 387 if (!mCommandLineSwitched) { | 381 if (!mCommandLineSwitched) { |
| 388 CommandLine.enableNativeProxy(); | 382 CommandLine.enableNativeProxy(); |
| 389 mCommandLineSwitched = true; | 383 mCommandLineSwitched = true; |
| 390 } | 384 } |
| 391 | 385 |
| 392 // From now on, keep tracing in sync with native. | 386 // From now on, keep tracing in sync with native. |
| 393 TraceEvent.registerNativeEnabledObserver(); | 387 TraceEvent.registerNativeEnabledObserver(); |
| 388 | |
| 389 // From this point on, native code is ready to use and checkIsReady() | |
| 390 // shouldn't complain from now on (and in fact, it's used by the | |
| 391 // following calls). | |
| 392 // Note that this flag can be accessed asynchronously, so any initializa tion | |
| 393 // must be performed before. | |
| 394 mInitialized = true; | |
| 394 } | 395 } |
| 395 | 396 |
| 396 // Called after all native initializations are complete. | 397 // Called after all native initializations are complete. |
| 397 public void onNativeInitializationComplete(Context context) { | 398 public void onNativeInitializationComplete(Context context) { |
| 398 recordBrowserProcessHistogram(context); | 399 recordBrowserProcessHistogram(context); |
| 399 } | 400 } |
| 400 | 401 |
| 401 // Record Chromium linker histogram state for the main browser process. Call ed from | 402 // Record Chromium linker histogram state for the main browser process. Call ed from |
| 402 // onNativeInitializationComplete(). | 403 // onNativeInitializationComplete(). |
| 403 private void recordBrowserProcessHistogram(Context context) { | 404 private void recordBrowserProcessHistogram(Context context) { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 448 loadAtFixedAddr essFailed); | 449 loadAtFixedAddr essFailed); |
| 449 } | 450 } |
| 450 } | 451 } |
| 451 | 452 |
| 452 /** | 453 /** |
| 453 * @return the process the shared library is loaded in, see the LibraryProce ssType | 454 * @return the process the shared library is loaded in, see the LibraryProce ssType |
| 454 * for possible values. | 455 * for possible values. |
| 455 */ | 456 */ |
| 456 @CalledByNative | 457 @CalledByNative |
| 457 public static int getLibraryProcessType() { | 458 public static int getLibraryProcessType() { |
| 458 synchronized (sLock) { | 459 if (sInstance == null) return LibraryProcessType.PROCESS_UNINITIALIZED; |
| 459 if (sInstance == null) return LibraryProcessType.PROCESS_UNINITIALIZ ED; | 460 return sInstance.mLibraryProcessType; |
| 460 return sInstance.mLibraryProcessType; | |
| 461 } | |
| 462 } | 461 } |
| 463 | 462 |
| 464 private native void nativeInitCommandLine(String[] initCommandLine); | 463 private native void nativeInitCommandLine(String[] initCommandLine); |
| 465 | 464 |
| 466 // Only methods needed before or during normal JNI registration are during S ystem.OnLoad. | 465 // Only methods needed before or during normal JNI registration are during S ystem.OnLoad. |
| 467 // nativeLibraryLoaded is then called to register everything else. This pro cess is called | 466 // nativeLibraryLoaded is then called to register everything else. This pro cess is called |
| 468 // "initialization". This method will be mapped (by generated code) to the LibraryLoaded | 467 // "initialization". This method will be mapped (by generated code) to the LibraryLoaded |
| 469 // definition in base/android/library_loader/library_loader_hooks.cc. | 468 // definition in base/android/library_loader/library_loader_hooks.cc. |
| 470 // | 469 // |
| 471 // Return true on success and false on failure. | 470 // Return true on success and false on failure. |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 484 // operation for a renderer process. Indicates whether the linker attempted relro sharing, | 483 // operation for a renderer process. Indicates whether the linker attempted relro sharing, |
| 485 // and if it did, whether the library failed to load at a fixed address. | 484 // and if it did, whether the library failed to load at a fixed address. |
| 486 private native void nativeRegisterChromiumAndroidLinkerRendererHistogram( | 485 private native void nativeRegisterChromiumAndroidLinkerRendererHistogram( |
| 487 boolean requestedSharedRelro, | 486 boolean requestedSharedRelro, |
| 488 boolean loadAtFixedAddressFailed); | 487 boolean loadAtFixedAddressFailed); |
| 489 | 488 |
| 490 // Get the version of the native library. This is needed so that we can chec k we | 489 // Get the version of the native library. This is needed so that we can chec k we |
| 491 // have the right version before initializing the (rest of the) JNI. | 490 // have the right version before initializing the (rest of the) JNI. |
| 492 private native String nativeGetVersionNumber(); | 491 private native String nativeGetVersionNumber(); |
| 493 } | 492 } |
| OLD | NEW |