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.AsyncTask; | 8 import android.os.AsyncTask; |
9 import android.os.SystemClock; | 9 import android.os.SystemClock; |
10 | 10 |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
122 } | 122 } |
123 } | 123 } |
124 | 124 |
125 private LibraryLoader(int libraryProcessType) { | 125 private LibraryLoader(int libraryProcessType) { |
126 mLibraryProcessType = libraryProcessType; | 126 mLibraryProcessType = libraryProcessType; |
127 mPrefetchLibraryHasBeenCalled = new AtomicBoolean(); | 127 mPrefetchLibraryHasBeenCalled = new AtomicBoolean(); |
128 } | 128 } |
129 | 129 |
130 /** | 130 /** |
131 * This method blocks until the library is fully loaded and initialized. | 131 * This method blocks until the library is fully loaded and initialized. |
132 * | |
133 * @param context The context in which the method is called. | |
134 */ | 132 */ |
135 public void ensureInitialized(Context context) throws ProcessInitException { | 133 public void ensureInitialized() throws ProcessInitException { |
136 // TODO(wnwen): Move this call appropriately down to the tests that need it. | |
137 ContextUtils.initApplicationContext(context.getApplicationContext()); | |
138 synchronized (sLock) { | 134 synchronized (sLock) { |
139 if (mInitialized) { | 135 if (mInitialized) { |
140 // Already initialized, nothing to do. | 136 // Already initialized, nothing to do. |
141 return; | 137 return; |
142 } | 138 } |
143 loadAlreadyLocked(context); | 139 loadAlreadyLocked(ContextUtils.getApplicationContext()); |
144 initializeAlreadyLocked(); | 140 initializeAlreadyLocked(); |
145 } | 141 } |
146 } | 142 } |
147 | 143 |
148 /** | 144 /** |
149 * Checks if library is fully loaded and initialized. | 145 * Checks if library is fully loaded and initialized. |
150 */ | 146 */ |
151 public static boolean isInitialized() { | 147 public static boolean isInitialized() { |
152 return sInstance != null && sInstance.mInitialized; | 148 return sInstance != null && sInstance.mInitialized; |
153 } | 149 } |
154 | 150 |
155 /** | 151 /** |
156 * Loads the library and blocks until the load completes. The caller is resp onsible | 152 * Loads the library and blocks until the load completes. The caller is resp onsible |
157 * for subsequently calling ensureInitialized(). | 153 * for subsequently calling ensureInitialized(). |
158 * May be called on any thread, but should only be called once. Note the thr ead | 154 * May be called on any thread, but should only be called once. Note the thr ead |
159 * this is called on will be the thread that runs the native code's static i nitializers. | 155 * this is called on will be the thread that runs the native code's static i nitializers. |
160 * See the comment in doInBackground() for more considerations on this. | 156 * See the comment in doInBackground() for more considerations on this. |
161 * | 157 * |
162 * @param context The context the code is running. | 158 * @throws ProcessInitException if the native library failed to load. |
159 */ | |
160 public void loadNow() throws ProcessInitException { | |
161 loadNow(ContextUtils.getApplicationContext()); | |
162 } | |
163 | |
164 /** | |
165 * Override kept for callers that need to load from a different app context. | |
163 * | 166 * |
164 * @throws ProcessInitException if the native library failed to load. | 167 * @param context The overriding context to be used to load libraries. |
168 * @throws ProcessInitException if the native library failed to load with th is context. | |
165 */ | 169 */ |
166 public void loadNow(Context context) throws ProcessInitException { | 170 public void loadNow(Context context) throws ProcessInitException { |
Ted C
2016/09/26 20:49:15
We should probably name this more explicit as well
Peter Wen
2016/09/28 15:55:17
Done.
| |
167 synchronized (sLock) { | 171 synchronized (sLock) { |
Ted C
2016/09/26 20:49:15
If we call this with something other than the appl
Peter Wen
2016/09/28 15:55:17
Agreed, done.
| |
168 loadAlreadyLocked(context); | 172 loadAlreadyLocked(context); |
169 } | 173 } |
170 } | 174 } |
171 | 175 |
172 /** | 176 /** |
173 * initializes the library here and now: must be called on the thread that t he | 177 * initializes the library here and now: must be called on the thread that t he |
174 * native will call its "main" thread. The library must have previously been | 178 * native will call its "main" thread. The library must have previously been |
175 * loaded with loadNow. | 179 * loaded with loadNow. |
176 */ | 180 */ |
177 public void initialize() throws ProcessInitException { | 181 public void initialize() throws ProcessInitException { |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
371 | 375 |
372 // From this point on, native code is ready to use and checkIsReady() | 376 // From this point on, native code is ready to use and checkIsReady() |
373 // shouldn't complain from now on (and in fact, it's used by the | 377 // shouldn't complain from now on (and in fact, it's used by the |
374 // following calls). | 378 // following calls). |
375 // Note that this flag can be accessed asynchronously, so any initializa tion | 379 // Note that this flag can be accessed asynchronously, so any initializa tion |
376 // must be performed before. | 380 // must be performed before. |
377 mInitialized = true; | 381 mInitialized = true; |
378 } | 382 } |
379 | 383 |
380 // Called after all native initializations are complete. | 384 // Called after all native initializations are complete. |
381 public void onNativeInitializationComplete(Context context) { | 385 public void onNativeInitializationComplete() { |
382 recordBrowserProcessHistogram(context); | 386 recordBrowserProcessHistogram(); |
383 } | 387 } |
384 | 388 |
385 // Record Chromium linker histogram state for the main browser process. Call ed from | 389 // Record Chromium linker histogram state for the main browser process. Call ed from |
386 // onNativeInitializationComplete(). | 390 // onNativeInitializationComplete(). |
387 private void recordBrowserProcessHistogram(Context context) { | 391 private void recordBrowserProcessHistogram() { |
388 if (Linker.getInstance().isUsed()) { | 392 if (Linker.getInstance().isUsed()) { |
389 nativeRecordChromiumAndroidLinkerBrowserHistogram(mIsUsingBrowserSha redRelros, | 393 nativeRecordChromiumAndroidLinkerBrowserHistogram( |
390 mLoadAtFixedAddres sFailed, | 394 mIsUsingBrowserSharedRelros, |
391 getLibraryLoadFrom ApkStatus(context), | 395 mLoadAtFixedAddressFailed, |
392 mLibraryLoadTimeMs ); | 396 getLibraryLoadFromApkStatus(), |
397 mLibraryLoadTimeMs); | |
393 } | 398 } |
394 if (sLibraryPreloader != null) { | 399 if (sLibraryPreloader != null) { |
395 nativeRecordLibraryPreloaderBrowserHistogram(mLibraryPreloaderStatus ); | 400 nativeRecordLibraryPreloaderBrowserHistogram(mLibraryPreloaderStatus ); |
396 } | 401 } |
397 } | 402 } |
398 | 403 |
399 // Returns the device's status for loading a library directly from the APK f ile. | 404 // Returns the device's status for loading a library directly from the APK f ile. |
400 // This method can only be called when the Chromium linker is used. | 405 // This method can only be called when the Chromium linker is used. |
401 private int getLibraryLoadFromApkStatus(Context context) { | 406 private int getLibraryLoadFromApkStatus() { |
402 assert Linker.getInstance().isUsed(); | 407 assert Linker.getInstance().isUsed(); |
403 | 408 |
404 if (mLibraryWasLoadedFromApk) { | 409 if (mLibraryWasLoadedFromApk) { |
405 return LibraryLoadFromApkStatusCodes.SUCCESSFUL; | 410 return LibraryLoadFromApkStatusCodes.SUCCESSFUL; |
406 } | 411 } |
407 | 412 |
408 // There were no libraries to be loaded directly from the APK file. | 413 // There were no libraries to be loaded directly from the APK file. |
409 return LibraryLoadFromApkStatusCodes.UNKNOWN; | 414 return LibraryLoadFromApkStatusCodes.UNKNOWN; |
410 } | 415 } |
411 | 416 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
479 | 484 |
480 // Finds the ranges corresponding to the native library pages, forks a new | 485 // Finds the ranges corresponding to the native library pages, forks a new |
481 // process to prefetch these pages and waits for it. The new process then | 486 // process to prefetch these pages and waits for it. The new process then |
482 // terminates. This is blocking. | 487 // terminates. This is blocking. |
483 private static native boolean nativeForkAndPrefetchNativeLibrary(); | 488 private static native boolean nativeForkAndPrefetchNativeLibrary(); |
484 | 489 |
485 // Returns the percentage of the native library code page that are currently reseident in | 490 // Returns the percentage of the native library code page that are currently reseident in |
486 // memory. | 491 // memory. |
487 private static native int nativePercentageOfResidentNativeLibraryCode(); | 492 private static native int nativePercentageOfResidentNativeLibraryCode(); |
488 } | 493 } |
OLD | NEW |