| Index: mojo/runner/android/apk/src/org/chromium/mojo/shell/AndroidHandler.java
|
| diff --git a/mojo/runner/android/apk/src/org/chromium/mojo/shell/AndroidHandler.java b/mojo/runner/android/apk/src/org/chromium/mojo/shell/AndroidHandler.java
|
| index 0ccc2ad18df6d986292d065f57456c15634fd042..1b730214f1d6aeaf18b73f3b6d1ea4a9aadd31b1 100644
|
| --- a/mojo/runner/android/apk/src/org/chromium/mojo/shell/AndroidHandler.java
|
| +++ b/mojo/runner/android/apk/src/org/chromium/mojo/shell/AndroidHandler.java
|
| @@ -5,14 +5,15 @@
|
| package org.chromium.mojo.shell;
|
|
|
| import android.content.Context;
|
| -import android.util.Log;
|
|
|
| import dalvik.system.DexClassLoader;
|
|
|
| import org.chromium.base.CalledByNative;
|
| import org.chromium.base.JNINamespace;
|
| +import org.chromium.base.Log;
|
|
|
| import java.io.File;
|
| +import java.io.FilenameFilter;
|
| import java.io.IOException;
|
| import java.lang.reflect.Constructor;
|
|
|
| @@ -45,6 +46,10 @@ public class AndroidHandler {
|
| private static final String APP_DIRECTORY = "applications";
|
| private static final String ASSET_DIRECTORY = "assets";
|
|
|
| + private static final String INTERNAL_DIRECTORY = "internal";
|
| +
|
| + private enum AppType { CACHED, UNCACHED }
|
| +
|
| /**
|
| * Deletes directories holding the temporary files. This should be called early on shell startup
|
| * to clean up after the previous run.
|
| @@ -77,45 +82,158 @@ public class AndroidHandler {
|
| @CalledByNative
|
| private static boolean bootstrap(Context context, String archivePath, int handle,
|
| long runApplicationPtr) {
|
| - File bootstrap_java_library;
|
| - File bootstrap_native_library;
|
| + File bootstrapJavaLibrary;
|
| + File bootstrapNativeLibrary;
|
| try {
|
| - bootstrap_java_library = FileHelper.extractFromAssets(context, BOOTSTRAP_JAVA_LIBRARY,
|
| - getAssetDir(context), true);
|
| - bootstrap_native_library = FileHelper.extractFromAssets(context,
|
| - BOOTSTRAP_NATIVE_LIBRARY, getAssetDir(context), true);
|
| + bootstrapJavaLibrary = FileHelper.extractFromAssets(context, BOOTSTRAP_JAVA_LIBRARY,
|
| + getAssetDir(context), FileHelper.FileType.TEMPORARY);
|
| + bootstrapNativeLibrary = FileHelper.extractFromAssets(context, BOOTSTRAP_NATIVE_LIBRARY,
|
| + getAssetDir(context), FileHelper.FileType.TEMPORARY);
|
| } catch (Exception e) {
|
| Log.e(TAG, "Extraction of bootstrap files from assets failed.", e);
|
| return false;
|
| }
|
|
|
| - File application_java_library;
|
| - File application_native_library;
|
| + File applicationJavaLibrary;
|
| + File applicationNativeLibrary;
|
| try {
|
| File archive = new File(archivePath);
|
| - application_java_library = FileHelper.extractFromArchive(archive, JAVA_LIBRARY_SUFFIX,
|
| - getAppDir(context));
|
| - application_native_library = FileHelper.extractFromArchive(archive,
|
| - NATIVE_LIBRARY_SUFFIX, getAppDir(context));
|
| + applicationJavaLibrary =
|
| + FileHelper.extractFromArchive(archive, JAVA_LIBRARY_SUFFIX, getAppDir(context),
|
| + FileHelper.FileType.TEMPORARY, FileHelper.ArchiveType.NORMAL);
|
| + applicationNativeLibrary = FileHelper.extractFromArchive(archive, NATIVE_LIBRARY_SUFFIX,
|
| + getAppDir(context), FileHelper.FileType.TEMPORARY,
|
| + FileHelper.ArchiveType.NORMAL);
|
| } catch (Exception e) {
|
| Log.e(TAG, "Extraction of application files from the archive failed.", e);
|
| return false;
|
| }
|
|
|
| - String dexPath = bootstrap_java_library.getAbsolutePath() + File.pathSeparator
|
| - + application_java_library.getAbsolutePath();
|
| - DexClassLoader bootstrapLoader = new DexClassLoader(dexPath,
|
| - getDexOutputDir(context).getAbsolutePath(), null,
|
| - ClassLoader.getSystemClassLoader());
|
| + return runApp(context, getDexOutputDir(context), applicationJavaLibrary,
|
| + applicationNativeLibrary, bootstrapJavaLibrary, bootstrapNativeLibrary, handle,
|
| + runApplicationPtr, AppType.UNCACHED);
|
| + }
|
| +
|
| + private static File findFileInDirectoryMatchingSuffix(
|
| + File dir, final String suffix, final String ignore) {
|
| + File[] matchingFiles = dir.listFiles(new FilenameFilter() {
|
| + public boolean accept(File dir, String name) {
|
| + return !name.equals(ignore) && !name.equals(BOOTSTRAP_JAVA_LIBRARY)
|
| + && !name.equals(BOOTSTRAP_NATIVE_LIBRARY) && name.endsWith(suffix);
|
| + }
|
| + });
|
| + return matchingFiles != null && matchingFiles.length == 1 ? matchingFiles[0] : null;
|
| + }
|
| +
|
| + /**
|
| + * Extracts and runs a cached application.
|
| + *
|
| + * @param context the application context
|
| + * @param archivePath the path of the archive containing the application to be run
|
| + * @param appPathString path where the cached app's resources and other files are
|
| + * @param handle handle to the shell to be passed to the native application. On the Java side
|
| + * this is opaque payload.
|
| + * @param runApplicationPtr pointer to the function that will set the native thunks and call
|
| + * into the application MojoMain. On the Java side this is opaque
|
| + * payload.
|
| + */
|
| + @CalledByNative
|
| + private static boolean bootstrapCachedApp(Context context, String archivePath,
|
| + String appPathString, int handle, long runApplicationPtr) {
|
| + final String appName = new File(appPathString).getName();
|
| + final File internalDir = new File(new File(appPathString), INTERNAL_DIRECTORY);
|
| + if (!internalDir.exists() && !internalDir.mkdirs()) {
|
| + Log.e(TAG, "Unable to create output dir " + internalDir.getAbsolutePath());
|
| + return false;
|
| + }
|
| + final File timestamp = FileHelper.prepareDirectoryForAssets(context, internalDir);
|
| + // We make the bootstrap library have a unique name on disk as otherwise we end up sharing
|
| + // bootstrap.so,
|
| + // which doesn't work.
|
| + // TODO(sky): figure out why android is caching the names.
|
| + final String bootstrapNativeLibraryName = appName + "-" + BOOTSTRAP_NATIVE_LIBRARY;
|
| + File bootstrapJavaLibrary = new File(internalDir, BOOTSTRAP_JAVA_LIBRARY);
|
| + File bootstrapNativeLibrary = new File(internalDir, bootstrapNativeLibraryName);
|
| + try {
|
| + // Use the files on disk if we have them, if not extract from the archive.
|
| + if (!bootstrapJavaLibrary.exists()) {
|
| + bootstrapJavaLibrary = FileHelper.extractFromAssets(context, BOOTSTRAP_JAVA_LIBRARY,
|
| + internalDir, FileHelper.FileType.PERMANENT);
|
| + }
|
| + if (!bootstrapNativeLibrary.exists()) {
|
| + final File extractedBootstrapNativeLibrary = FileHelper.extractFromAssets(context,
|
| + BOOTSTRAP_NATIVE_LIBRARY, internalDir, FileHelper.FileType.PERMANENT);
|
| + if (!extractedBootstrapNativeLibrary.renameTo(bootstrapNativeLibrary)) {
|
| + Log.e(TAG, "Unable to rename bootstrap library "
|
| + + bootstrapNativeLibrary.getAbsolutePath());
|
| + return false;
|
| + }
|
| + }
|
| + } catch (Exception e) {
|
| + Log.e(TAG, "Extraction of bootstrap files from assets failed.", e);
|
| + return false;
|
| + }
|
| +
|
| + // Because we allow the .so and .dex.jar to be anything we have to search in the internal
|
| + // directory for matching files.
|
| + // If we find one, we assume it's the one we want.
|
| + File applicationJavaLibrary =
|
| + findFileInDirectoryMatchingSuffix(internalDir, JAVA_LIBRARY_SUFFIX, "");
|
| + File applicationNativeLibrary = findFileInDirectoryMatchingSuffix(
|
| + internalDir, NATIVE_LIBRARY_SUFFIX, bootstrapNativeLibraryName);
|
| + try {
|
| + File archive = new File(archivePath);
|
| + if (applicationJavaLibrary == null) {
|
| + applicationJavaLibrary = FileHelper.extractFromArchive(archive, JAVA_LIBRARY_SUFFIX,
|
| + internalDir, FileHelper.FileType.PERMANENT,
|
| + FileHelper.ArchiveType.CONTENT_HANDLER);
|
| + }
|
| + if (applicationNativeLibrary == null) {
|
| + applicationNativeLibrary = FileHelper.extractFromArchive(archive,
|
| + NATIVE_LIBRARY_SUFFIX, internalDir, FileHelper.FileType.PERMANENT,
|
| + FileHelper.ArchiveType.CONTENT_HANDLER);
|
| + }
|
| + } catch (Exception e) {
|
| + Log.e(TAG, "Extraction of application files from the archive failed.", e);
|
| + return false;
|
| + }
|
| +
|
| + FileHelper.createTimestampIfNecessary(timestamp);
|
| +
|
| + return runApp(context, new File(internalDir, DEX_OUTPUT_DIRECTORY), applicationJavaLibrary,
|
| + applicationNativeLibrary, bootstrapJavaLibrary, bootstrapNativeLibrary, handle,
|
| + runApplicationPtr, AppType.CACHED);
|
| + }
|
| +
|
| + /**
|
| + * Creates the class loader containing the bootstrap classes and runs it.
|
| + *
|
| + * @return true if successfully ran, false if encounteres some sort of error.
|
| + */
|
| + private static boolean runApp(Context context, File dexOutputDir, File applicationJavaLibrary,
|
| + File applicationNativeLibrary, File bootstrapJavaLibrary, File bootstrapNativeLibrary,
|
| + int handle, long runApplicationPtr, AppType appType) {
|
| + final String dexPath = bootstrapJavaLibrary.getAbsolutePath() + File.pathSeparator
|
| + + applicationJavaLibrary.getAbsolutePath();
|
| + if (!dexOutputDir.exists() && !dexOutputDir.mkdirs()) {
|
| + Log.e(TAG, "Unable to create dex output dir " + dexOutputDir.getAbsolutePath());
|
| + return false;
|
| + }
|
| + // TODO(sky): third arg is a path, but appears to have no effect, figure out if this relates
|
| + // to weird caching
|
| + // logic mentioned above.
|
| + DexClassLoader bootstrapLoader = new DexClassLoader(
|
| + dexPath, dexOutputDir.getAbsolutePath(), null, ClassLoader.getSystemClassLoader());
|
|
|
| try {
|
| Class<?> loadedClass = bootstrapLoader.loadClass(BOOTSTRAP_CLASS);
|
| Class<? extends Runnable> bootstrapClass = loadedClass.asSubclass(Runnable.class);
|
| - Constructor<? extends Runnable> constructor = bootstrapClass.getConstructor(
|
| - Context.class, File.class, File.class, Integer.class, Long.class);
|
| - Runnable bootstrapRunnable = constructor.newInstance(context, bootstrap_native_library,
|
| - application_native_library, Integer.valueOf(handle),
|
| - Long.valueOf(runApplicationPtr));
|
| + Constructor<? extends Runnable> constructor =
|
| + bootstrapClass.getConstructor(Context.class, File.class, File.class,
|
| + Integer.class, Long.class, Boolean.class);
|
| + Runnable bootstrapRunnable = constructor.newInstance(context, bootstrapNativeLibrary,
|
| + applicationNativeLibrary, Integer.valueOf(handle),
|
| + Long.valueOf(runApplicationPtr), appType == AppType.CACHED);
|
| bootstrapRunnable.run();
|
| } catch (Throwable t) {
|
| Log.e(TAG, "Running Bootstrap failed.", t);
|
| @@ -124,6 +242,11 @@ public class AndroidHandler {
|
| return true;
|
| }
|
|
|
| + @CalledByNative
|
| + static String getLocalAppsDir(Context context) {
|
| + return ShellMain.getLocalAppsDir(context).getAbsolutePath();
|
| + }
|
| +
|
| private static File getDexOutputDir(Context context) {
|
| return context.getDir(DEX_OUTPUT_DIRECTORY, Context.MODE_PRIVATE);
|
| }
|
|
|