| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.blimp; | |
| 6 | |
| 7 import android.os.Handler; | |
| 8 | |
| 9 import org.chromium.base.ObserverList; | |
| 10 import org.chromium.base.ResourceExtractor; | |
| 11 import org.chromium.base.ThreadUtils; | |
| 12 import org.chromium.base.annotations.JNINamespace; | |
| 13 import org.chromium.base.library_loader.LibraryLoader; | |
| 14 import org.chromium.base.library_loader.LibraryProcessType; | |
| 15 import org.chromium.base.library_loader.ProcessInitException; | |
| 16 | |
| 17 /** | |
| 18 * Asynchronously loads and registers the native libraries associated with Blimp
. | |
| 19 */ | |
| 20 @JNINamespace("blimp::client") | |
| 21 public final class BlimpLibraryLoader { | |
| 22 /** | |
| 23 * A callback interface that is notified with the native library load result
s. | |
| 24 */ | |
| 25 public interface Callback { | |
| 26 /** | |
| 27 * Called when the load attempt is finished (regardless of whether or no
t it was | |
| 28 * successful). | |
| 29 * @param success Whether or not the native library was successfully loa
ded. | |
| 30 */ | |
| 31 void onStartupComplete(boolean success); | |
| 32 } | |
| 33 | |
| 34 /** | |
| 35 * Whether or not a call to {@link #startAsync(Callback)} is/has actually at
tempted to | |
| 36 * load the native library. | |
| 37 */ | |
| 38 private static boolean sLoadAttempted = false; | |
| 39 | |
| 40 /** If not {@code null} the result of a load attempt. */ | |
| 41 private static Boolean sLibraryLoadResult; | |
| 42 | |
| 43 /** | |
| 44 * A list of {@link Callback} instances that still need to be notified of th
e result of the | |
| 45 * initial call to {@link #startAsync(Callback)}. | |
| 46 */ | |
| 47 private static ObserverList<Callback> sOutstandingCallbacks = new ObserverLi
st<>(); | |
| 48 | |
| 49 /** | |
| 50 * Disallow instantiation of this class. | |
| 51 */ | |
| 52 private BlimpLibraryLoader() {} | |
| 53 | |
| 54 /** | |
| 55 * Starts asynchronously loading and registering the native libraries. If t
his is called more | |
| 56 * than once, only the first caller will actually load the library. The sub
sequent calls will | |
| 57 * wait for the first call to finish and notify their {@link BlimpLibraryLoa
der.Callback} | |
| 58 * instances accordingly. Any calls to this after the library has finished
loading will just | |
| 59 * have the initial load result posted back to {@code callback}. | |
| 60 * @param callback A {@link BlimpLibraryLoader.Callback} to be
notified upon | |
| 61 * completion. | |
| 62 * @throws ProcessInitException | |
| 63 */ | |
| 64 public static void startAsync(final Callback callback) throws ProcessInitExc
eption { | |
| 65 ThreadUtils.assertOnUiThread(); | |
| 66 | |
| 67 // Save the callback to be notified once loading and initializiation is
one. | |
| 68 sOutstandingCallbacks.addObserver(callback); | |
| 69 | |
| 70 if (sLibraryLoadResult != null) { | |
| 71 // The library is already loaded, notify {@code callback} and skip t
he rest of the | |
| 72 // loading steps. | |
| 73 notifyCallbacksAndClear(); | |
| 74 return; | |
| 75 } | |
| 76 | |
| 77 // If we're already in the process of loading, skip this call. Otherwis
e mark that we are | |
| 78 // loading and do the actual load. Subsequent calls won't run the load
steps, but will wait | |
| 79 // for this load to finish. | |
| 80 if (sLoadAttempted) return; | |
| 81 sLoadAttempted = true; | |
| 82 | |
| 83 ResourceExtractor extractor = ResourceExtractor.get(); | |
| 84 extractor.startExtractingResources(); | |
| 85 LibraryLoader.get(LibraryProcessType.PROCESS_BROWSER).ensureInitialized(
); | |
| 86 | |
| 87 extractor.addCompletionCallback(new Runnable() { | |
| 88 @Override | |
| 89 public void run() { | |
| 90 new Handler().post(new Runnable() { | |
| 91 @Override | |
| 92 public void run() { | |
| 93 // Only run nativeStartBlimp if we properly initialized
native. | |
| 94 boolean startResult = nativeStartBlimp(); | |
| 95 sLibraryLoadResult = Boolean.valueOf(startResult); | |
| 96 | |
| 97 // Notify any oustanding callers to #startAsync(). | |
| 98 notifyCallbacksAndClear(); | |
| 99 } | |
| 100 }); | |
| 101 } | |
| 102 }); | |
| 103 } | |
| 104 | |
| 105 private static void notifyCallbacksAndClear() { | |
| 106 for (Callback callback : sOutstandingCallbacks) { | |
| 107 notifyCallback(callback); | |
| 108 } | |
| 109 | |
| 110 // Clear the callback list so we don't hold onto references to callers. | |
| 111 sOutstandingCallbacks.clear(); | |
| 112 } | |
| 113 | |
| 114 private static void notifyCallback(final Callback callback) { | |
| 115 new Handler().post(new Runnable() { | |
| 116 @Override | |
| 117 public void run() { | |
| 118 ThreadUtils.assertOnUiThread(); | |
| 119 callback.onStartupComplete(sLibraryLoadResult); | |
| 120 } | |
| 121 }); | |
| 122 } | |
| 123 | |
| 124 // Native methods. | |
| 125 private static native boolean nativeStartBlimp(); | |
| 126 } | |
| OLD | NEW |