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 29 matching lines...) Expand all Loading... | |
| 40 // One-way switch becomes true when the libraries are initialized ( | 40 // One-way switch becomes true when the libraries are initialized ( |
| 41 // by calling nativeLibraryLoaded, which forwards to LibraryLoaded(...) in | 41 // by calling nativeLibraryLoaded, which forwards to LibraryLoaded(...) in |
| 42 // library_loader_hooks.cc). | 42 // library_loader_hooks.cc). |
| 43 private static boolean sInitialized = false; | 43 private static boolean sInitialized = false; |
| 44 | 44 |
| 45 // One-way switch becomes true if the system library loading failed, | 45 // One-way switch becomes true if the system library loading failed, |
| 46 // and the right native library was found and loaded by the hack. | 46 // and the right native library was found and loaded by the hack. |
| 47 // The flag is used to report UMA stats later. | 47 // The flag is used to report UMA stats later. |
| 48 private static boolean sNativeLibraryHackWasUsed = false; | 48 private static boolean sNativeLibraryHackWasUsed = false; |
| 49 | 49 |
| 50 | 50 // TODO(fqian): Remove this method once downstream CL is |
| 51 /** | 51 // committed. |
| 52 * TODO: http://crbug.com/354655 | 52 public static void ensureInitialized(Context context) throws ProcessInitExce ption { |
| 53 * remove this method once WebViewChromiumFactoryProvider.java | 53 ensureInitialized(context, true); |
| 54 * changes the call to ensureInitialized(null). | |
| 55 */ | |
| 56 public static void ensureInitialized() throws ProcessInitException { | |
| 57 ensureInitialized(null); | |
| 58 } | 54 } |
| 59 | 55 |
| 60 /** | 56 /** |
| 57 * The same as ensureInitialized(null, false), should only be called | |
| 58 * by non-browser processes. | |
| 59 * | |
| 60 * @throws ProcessInitException | |
| 61 */ | |
| 62 public static void ensureInitialized() throws ProcessInitException { | |
| 63 ensureInitialized(null, false); | |
| 64 } | |
| 65 | |
| 66 /** | |
| 61 * This method blocks until the library is fully loaded and initialized. | 67 * This method blocks until the library is fully loaded and initialized. |
| 62 * | 68 * |
| 63 * @param context The context in which the method is called, the caller | 69 * @param context The context in which the method is called, the caller |
| 64 * may pass in a null context if it doesn't know in which context it | 70 * may pass in a null context if it doesn't know in which context it |
| 65 * is running, or it doesn't need to work around the issue | 71 * is running, or it doesn't need to work around the issue |
| 66 * http://b/13216167. | 72 * http://b/13216167. |
| 67 * | 73 * |
| 68 * When the context is not null and native library was not extracted | 74 * When the context is not null and native library was not extracted |
| 69 * by Android package manager, the LibraryLoader class | 75 * by Android package manager, the LibraryLoader class |
| 70 * will extract the native libraries from APK. This is a hack used to | 76 * will extract the native libraries from APK. This is a hack used to |
| 71 * work around some Sony devices with the following platform bug: | 77 * work around some Sony devices with the following platform bug: |
| 72 * http://b/13216167. | 78 * http://b/13216167. |
| 79 * | |
| 80 * @param inBrowserProcess The caller indicates it is running in the browse r | |
| 81 * process, so the code will perform browser-specific file cleanup. | |
| 73 */ | 82 */ |
| 74 public static void ensureInitialized(Context context) throws ProcessInitExce ption { | 83 public static void ensureInitialized(Context context, boolean inBrowserProce ss) |
| 84 throws ProcessInitException { | |
| 75 synchronized (sLock) { | 85 synchronized (sLock) { |
| 76 if (sInitialized) { | 86 if (sInitialized) { |
| 77 // Already initialized, nothing to do. | 87 // Already initialized, nothing to do. |
| 78 return; | 88 return; |
| 79 } | 89 } |
| 80 loadAlreadyLocked(context); | 90 loadAlreadyLocked(context, inBrowserProcess); |
| 81 initializeAlreadyLocked(CommandLine.getJavaSwitchesOrNull()); | 91 initializeAlreadyLocked(CommandLine.getJavaSwitchesOrNull()); |
| 82 } | 92 } |
| 83 } | 93 } |
| 84 | 94 |
| 85 /** | 95 /** |
| 86 * Checks if library is fully loaded and initialized. | 96 * Checks if library is fully loaded and initialized. |
| 87 */ | 97 */ |
| 88 public static boolean isInitialized() { | 98 public static boolean isInitialized() { |
| 89 synchronized (sLock) { | 99 synchronized (sLock) { |
| 90 return sInitialized; | 100 return sInitialized; |
| 91 } | 101 } |
| 92 } | 102 } |
| 93 | 103 |
| 94 /** | 104 /** |
| 105 * The same as loadNow(null, false), should only be called by | |
| 106 * non-browser process. | |
| 107 * | |
| 108 * @throws ProcessInitException | |
| 109 */ | |
| 110 public static void loadNow() throws ProcessInitException { | |
| 111 loadNow(null, false); | |
| 112 } | |
| 113 | |
| 114 /** | |
| 95 * Loads the library and blocks until the load completes. The caller is resp onsible | 115 * Loads the library and blocks until the load completes. The caller is resp onsible |
| 96 * for subsequently calling ensureInitialized(). | 116 * for subsequently calling ensureInitialized(). |
| 97 * May be called on any thread, but should only be called once. Note the thr ead | 117 * May be called on any thread, but should only be called once. Note the thr ead |
| 98 * this is called on will be the thread that runs the native code's static i nitializers. | 118 * this is called on will be the thread that runs the native code's static i nitializers. |
| 99 * See the comment in doInBackground() for more considerations on this. | 119 * See the comment in doInBackground() for more considerations on this. |
| 100 * | 120 * |
| 121 * @param context The context the code is running, or null if it doesn't hav e one. | |
| 122 * @param inBrowserProcess The flag tells whether the code is running in | |
| 123 * the browser process. | |
|
cjhopman
2014/04/02 22:24:24
It's unclear (from this documentation) what this f
Feng Qian
2014/04/03 00:50:42
ok, I am fine with renaming.
On 2014/04/02 22:24:
| |
| 124 * | |
| 101 * @throws ProcessInitException if the native library failed to load. | 125 * @throws ProcessInitException if the native library failed to load. |
| 102 */ | 126 */ |
| 103 public static void loadNow(Context context) throws ProcessInitException { | 127 public static void loadNow(Context context, boolean inBrowserProcess) |
| 128 throws ProcessInitException { | |
| 104 synchronized (sLock) { | 129 synchronized (sLock) { |
| 105 loadAlreadyLocked(context); | 130 loadAlreadyLocked(context, inBrowserProcess); |
| 106 } | 131 } |
| 107 } | 132 } |
| 108 | 133 |
| 109 /** | 134 /** |
| 110 * initializes the library here and now: must be called on the thread that t he | 135 * initializes the library here and now: must be called on the thread that t he |
| 111 * native will call its "main" thread. The library must have previously been | 136 * native will call its "main" thread. The library must have previously been |
| 112 * loaded with loadNow. | 137 * loaded with loadNow. |
| 113 * @param initCommandLine The command line arguments that native command lin e will | 138 * @param initCommandLine The command line arguments that native command lin e will |
| 114 * be initialized with. | 139 * be initialized with. |
| 115 */ | 140 */ |
| 116 public static void initialize(String[] initCommandLine) throws ProcessInitEx ception { | 141 public static void initialize(String[] initCommandLine) throws ProcessInitEx ception { |
| 117 synchronized (sLock) { | 142 synchronized (sLock) { |
| 118 initializeAlreadyLocked(initCommandLine); | 143 initializeAlreadyLocked(initCommandLine); |
| 119 } | 144 } |
| 120 } | 145 } |
| 121 | 146 |
| 122 // Invoke System.loadLibrary(...), triggering JNI_OnLoad in native code | 147 // Invoke System.loadLibrary(...), triggering JNI_OnLoad in native code |
| 123 private static void loadAlreadyLocked(Context context) throws ProcessInitExc eption { | 148 private static void loadAlreadyLocked(Context context, boolean inBrowserProc ess) |
| 149 throws ProcessInitException { | |
| 124 try { | 150 try { |
| 125 if (!sLoaded) { | 151 if (!sLoaded) { |
| 126 assert !sInitialized; | 152 assert !sInitialized; |
| 127 | 153 |
| 128 long startTime = SystemClock.uptimeMillis(); | 154 long startTime = SystemClock.uptimeMillis(); |
| 129 boolean useChromiumLinker = Linker.isUsed(); | 155 boolean useChromiumLinker = Linker.isUsed(); |
| 130 | 156 |
| 131 if (useChromiumLinker) Linker.prepareLibraryLoad(); | 157 if (useChromiumLinker) Linker.prepareLibraryLoad(); |
| 132 | 158 |
| 159 boolean workaroundLibrariesDeleted = false; | |
| 133 for (String library : NativeLibraries.LIBRARIES) { | 160 for (String library : NativeLibraries.LIBRARIES) { |
| 134 Log.i(TAG, "Loading: " + library); | 161 Log.i(TAG, "Loading: " + library); |
| 135 if (useChromiumLinker) { | 162 if (useChromiumLinker) { |
| 136 Linker.loadLibrary(library); | 163 Linker.loadLibrary(library); |
| 137 } else { | 164 } else { |
| 138 try { | 165 try { |
| 139 System.loadLibrary(library); | 166 System.loadLibrary(library); |
| 140 if (context != null) { | 167 if (context != null |
| 168 && inBrowserProcess | |
|
boliu
2014/04/03 01:03:22
Webview technically only has the browser process,
| |
| 169 && !workaroundLibrariesDeleted) { | |
| 141 LibraryLoaderHelper.deleteWorkaroundLibrariesAsy nchronously( | 170 LibraryLoaderHelper.deleteWorkaroundLibrariesAsy nchronously( |
| 142 context); | 171 context); |
| 172 workaroundLibrariesDeleted = true; | |
|
cjhopman
2014/04/02 22:24:24
Since we only want this to happen once, how about
Feng Qian
2014/04/03 00:50:42
The code is not just run when sNativeLibraryHackWa
| |
| 143 } | 173 } |
| 144 } catch (UnsatisfiedLinkError e) { | 174 } catch (UnsatisfiedLinkError e) { |
| 145 if (context != null | 175 if (context != null |
| 146 && LibraryLoaderHelper.tryLoadLibraryUsingWorkar ound(context, | 176 && LibraryLoaderHelper.tryLoadLibraryUsingWorkar ound(context, |
| 147 library)) { | 177 library)) { |
| 148 sNativeLibraryHackWasUsed = true; | 178 sNativeLibraryHackWasUsed = true; |
| 149 } else { | 179 } else { |
| 150 throw e; | 180 throw e; |
| 151 } | 181 } |
| 152 } | 182 } |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 213 private static native void nativeRecordChromiumAndroidLinkerHistogram( | 243 private static native void nativeRecordChromiumAndroidLinkerHistogram( |
| 214 boolean loadedAtFixedAddressFailed, | 244 boolean loadedAtFixedAddressFailed, |
| 215 boolean isLowMemoryDevice); | 245 boolean isLowMemoryDevice); |
| 216 | 246 |
| 217 // Get the version of the native library. This is needed so that we can chec k we | 247 // Get the version of the native library. This is needed so that we can chec k we |
| 218 // have the right version before initializing the (rest of the) JNI. | 248 // have the right version before initializing the (rest of the) JNI. |
| 219 private static native String nativeGetVersionNumber(); | 249 private static native String nativeGetVersionNumber(); |
| 220 | 250 |
| 221 private static native void nativeRecordNativeLibraryHack(boolean usedHack); | 251 private static native void nativeRecordNativeLibraryHack(boolean usedHack); |
| 222 } | 252 } |
| OLD | NEW |