OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.mojo.shell; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.util.Log; |
| 9 |
| 10 import dalvik.system.DexClassLoader; |
| 11 |
| 12 import org.chromium.base.CalledByNative; |
| 13 import org.chromium.base.JNINamespace; |
| 14 |
| 15 import java.io.File; |
| 16 import java.io.IOException; |
| 17 import java.lang.reflect.Constructor; |
| 18 |
| 19 /** |
| 20 * Content handler for archives containing native libraries bundled with Java co
de. |
| 21 * <p> |
| 22 * TODO(ppi): create a seperate instance for each application being bootstrapped
to keep track of |
| 23 * the temporary files and clean them up once the execution finishes. |
| 24 */ |
| 25 @JNINamespace("mojo::shell") |
| 26 public class AndroidHandler { |
| 27 private static final String TAG = "AndroidHandler"; |
| 28 |
| 29 // Bootstrap native and java libraries are packaged with the MojoShell APK a
s assets. |
| 30 private static final String BOOTSTRAP_JAVA_LIBRARY = "bootstrap_java.dex.jar
"; |
| 31 private static final String BOOTSTRAP_NATIVE_LIBRARY = "libbootstrap.so"; |
| 32 // Name of the bootstrapping runnable shipped in the packaged Java library. |
| 33 private static final String BOOTSTRAP_CLASS = "org.chromium.mojo.shell.Boots
trap"; |
| 34 |
| 35 // File extensions used to identify application libraries in the provided ar
chive. |
| 36 private static final String JAVA_LIBRARY_SUFFIX = ".dex.jar"; |
| 37 private static final String NATIVE_LIBRARY_SUFFIX = ".so"; |
| 38 // Filename sections used for naming temporary files holding application fil
es. |
| 39 private static final String ARCHIVE_PREFIX = "archive"; |
| 40 private static final String ARCHIVE_SUFFIX = ".zip"; |
| 41 |
| 42 // Directories used to hold temporary files. These are cleared when clearTem
poraryFiles() is |
| 43 // called. |
| 44 private static final String DEX_OUTPUT_DIRECTORY = "dex_output"; |
| 45 private static final String APP_DIRECTORY = "applications"; |
| 46 private static final String ASSET_DIRECTORY = "assets"; |
| 47 |
| 48 /** |
| 49 * Deletes directories holding the temporary files. This should be called ea
rly on shell startup |
| 50 * to clean up after the previous run. |
| 51 */ |
| 52 static void clearTemporaryFiles(Context context) { |
| 53 FileHelper.deleteRecursively(getDexOutputDir(context)); |
| 54 FileHelper.deleteRecursively(getAppDir(context)); |
| 55 FileHelper.deleteRecursively(getAssetDir(context)); |
| 56 } |
| 57 |
| 58 /** |
| 59 * Returns the path at which the native part should save the application arc
hive. |
| 60 */ |
| 61 @CalledByNative |
| 62 private static String getNewTempArchivePath(Context context) throws IOExcept
ion { |
| 63 return File.createTempFile(ARCHIVE_PREFIX, ARCHIVE_SUFFIX, |
| 64 getAppDir(context)).getAbsolutePath(); |
| 65 } |
| 66 |
| 67 /** |
| 68 * Extracts and runs the application libraries contained by the indicated ar
chive. |
| 69 * @param context the application context |
| 70 * @param archivePath the path of the archive containing the application to
be run |
| 71 * @param handle handle to the shell to be passed to the native application.
On the Java side |
| 72 * this is opaque payload. |
| 73 * @param runApplicationPtr pointer to the function that will set the native
thunks and call |
| 74 * into the application MojoMain. On the Java side
this is opaque |
| 75 * payload. |
| 76 */ |
| 77 @CalledByNative |
| 78 private static boolean bootstrap(Context context, String archivePath, int ha
ndle, |
| 79 long runApplicationPtr) { |
| 80 File bootstrap_java_library; |
| 81 File bootstrap_native_library; |
| 82 try { |
| 83 bootstrap_java_library = FileHelper.extractFromAssets(context, BOOTS
TRAP_JAVA_LIBRARY, |
| 84 getAssetDir(context), true); |
| 85 bootstrap_native_library = FileHelper.extractFromAssets(context, |
| 86 BOOTSTRAP_NATIVE_LIBRARY, getAssetDir(context), true); |
| 87 } catch (Exception e) { |
| 88 Log.e(TAG, "Extraction of bootstrap files from assets failed.", e); |
| 89 return false; |
| 90 } |
| 91 |
| 92 File application_java_library; |
| 93 File application_native_library; |
| 94 try { |
| 95 File archive = new File(archivePath); |
| 96 application_java_library = FileHelper.extractFromArchive(archive, JA
VA_LIBRARY_SUFFIX, |
| 97 getAppDir(context)); |
| 98 application_native_library = FileHelper.extractFromArchive(archive, |
| 99 NATIVE_LIBRARY_SUFFIX, getAppDir(context)); |
| 100 } catch (Exception e) { |
| 101 Log.e(TAG, "Extraction of application files from the archive failed.
", e); |
| 102 return false; |
| 103 } |
| 104 |
| 105 String dexPath = bootstrap_java_library.getAbsolutePath() + File.pathSep
arator |
| 106 + application_java_library.getAbsolutePath(); |
| 107 DexClassLoader bootstrapLoader = new DexClassLoader(dexPath, |
| 108 getDexOutputDir(context).getAbsolutePath(), null, |
| 109 ClassLoader.getSystemClassLoader()); |
| 110 |
| 111 try { |
| 112 Class<?> loadedClass = bootstrapLoader.loadClass(BOOTSTRAP_CLASS); |
| 113 Class<? extends Runnable> bootstrapClass = loadedClass.asSubclass(Ru
nnable.class); |
| 114 Constructor<? extends Runnable> constructor = bootstrapClass.getCons
tructor( |
| 115 Context.class, File.class, File.class, Integer.class, Long.c
lass); |
| 116 Runnable bootstrapRunnable = constructor.newInstance(context, bootst
rap_native_library, |
| 117 application_native_library, Integer.valueOf(handle), |
| 118 Long.valueOf(runApplicationPtr)); |
| 119 bootstrapRunnable.run(); |
| 120 } catch (Throwable t) { |
| 121 Log.e(TAG, "Running Bootstrap failed.", t); |
| 122 return false; |
| 123 } |
| 124 return true; |
| 125 } |
| 126 |
| 127 private static File getDexOutputDir(Context context) { |
| 128 return context.getDir(DEX_OUTPUT_DIRECTORY, Context.MODE_PRIVATE); |
| 129 } |
| 130 |
| 131 private static File getAppDir(Context context) { |
| 132 return context.getDir(APP_DIRECTORY, Context.MODE_PRIVATE); |
| 133 } |
| 134 |
| 135 private static File getAssetDir(Context context) { |
| 136 return context.getDir(ASSET_DIRECTORY, Context.MODE_PRIVATE); |
| 137 } |
| 138 } |
OLD | NEW |