| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 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.app.Activity; | |
| 8 import android.content.Context; | |
| 9 import android.util.Log; | |
| 10 | |
| 11 import org.chromium.base.CalledByNative; | |
| 12 import org.chromium.base.JNINamespace; | |
| 13 | |
| 14 import java.io.File; | |
| 15 import java.util.ArrayList; | |
| 16 import java.util.Arrays; | |
| 17 import java.util.List; | |
| 18 | |
| 19 /** | |
| 20 * A placeholder class to call native functions. | |
| 21 **/ | |
| 22 @JNINamespace("mojo::shell") | |
| 23 public class MojoMain { | |
| 24 private static final String TAG = "MojoMain"; | |
| 25 | |
| 26 // Directory where applications bundled with the shell will be extracted. | |
| 27 private static final String LOCAL_APP_DIRECTORY = "local_apps"; | |
| 28 // Individual applications bundled with the shell as assets. | |
| 29 private static final String NETWORK_LIBRARY_APP = "network_service.mojo"; | |
| 30 // The mojo_shell library is also an executable run in forked processes when
running | |
| 31 // multi-process. | |
| 32 private static final String MOJO_SHELL_EXECUTABLE = "libmojo_shell.so"; | |
| 33 | |
| 34 /** | |
| 35 * A guard flag for calling nativeInit() only once. | |
| 36 **/ | |
| 37 private static boolean sInitialized = false; | |
| 38 | |
| 39 /** | |
| 40 * Initializes the native system. | |
| 41 **/ | |
| 42 static void ensureInitialized(Context applicationContext, String[] parameter
s) { | |
| 43 if (sInitialized) | |
| 44 return; | |
| 45 try { | |
| 46 FileHelper.extractFromAssets(applicationContext, NETWORK_LIBRARY_APP
, | |
| 47 getLocalAppsDir(applicationContext), false); | |
| 48 File mojoShell = new File(applicationContext.getApplicationInfo().na
tiveLibraryDir, | |
| 49 MOJO_SHELL_EXECUTABLE); | |
| 50 | |
| 51 List<String> parametersList = new ArrayList<String>(); | |
| 52 // Program name. | |
| 53 if (parameters != null) { | |
| 54 parametersList.addAll(Arrays.asList(parameters)); | |
| 55 } | |
| 56 | |
| 57 nativeInit(applicationContext, mojoShell.getAbsolutePath(), | |
| 58 parametersList.toArray(new String[parametersList.size()]), | |
| 59 getLocalAppsDir(applicationContext).getAbsolutePath(), | |
| 60 getTmpDir(applicationContext).getAbsolutePath()); | |
| 61 sInitialized = true; | |
| 62 } catch (Exception e) { | |
| 63 Log.e(TAG, "MojoMain initialization failed.", e); | |
| 64 throw new RuntimeException(e); | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 /** | |
| 69 * Starts the specified application in the specified context. | |
| 70 * | |
| 71 * @return <code>true</code> if an application has been launched. | |
| 72 **/ | |
| 73 static boolean start() { | |
| 74 return nativeStart(); | |
| 75 } | |
| 76 | |
| 77 /** | |
| 78 * Adds the given URL to the set of mojo applications to run on start. | |
| 79 */ | |
| 80 static void addApplicationURL(String url) { | |
| 81 nativeAddApplicationURL(url); | |
| 82 } | |
| 83 | |
| 84 private static File getLocalAppsDir(Context context) { | |
| 85 return context.getDir(LOCAL_APP_DIRECTORY, Context.MODE_PRIVATE); | |
| 86 } | |
| 87 | |
| 88 private static File getTmpDir(Context context) { | |
| 89 return new File(context.getCacheDir(), "tmp"); | |
| 90 } | |
| 91 | |
| 92 @CalledByNative | |
| 93 private static void finishActivity(Activity activity) { | |
| 94 activity.finish(); | |
| 95 } | |
| 96 | |
| 97 /** | |
| 98 * Initializes the native system. This API should be called only once per pr
ocess. | |
| 99 **/ | |
| 100 private static native void nativeInit(Context context, String mojoShellPath, | |
| 101 String[] parameters, String bundledAppsDirectory, String tmpDir); | |
| 102 | |
| 103 private static native boolean nativeStart(); | |
| 104 | |
| 105 private static native void nativeAddApplicationURL(String url); | |
| 106 } | |
| OLD | NEW |