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