OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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.mojo.shell; | 5 package org.chromium.mojo.shell; |
6 | 6 |
7 import android.app.Activity; | 7 import android.app.Activity; |
8 import android.content.Context; | 8 import android.content.Context; |
9 import android.content.pm.ApplicationInfo; | 9 import android.content.pm.ApplicationInfo; |
10 import android.content.pm.PackageManager; | 10 import android.content.pm.PackageManager; |
11 import android.os.Bundle; | 11 import android.os.Bundle; |
12 import android.util.Log; | 12 import android.util.Log; |
13 | 13 |
| 14 import org.chromium.base.ContextUtils; |
14 import org.chromium.base.annotations.CalledByNative; | 15 import org.chromium.base.annotations.CalledByNative; |
15 import org.chromium.base.annotations.JNINamespace; | 16 import org.chromium.base.annotations.JNINamespace; |
16 | 17 |
17 import java.io.File; | 18 import java.io.File; |
18 import java.util.ArrayList; | 19 import java.util.ArrayList; |
19 import java.util.Arrays; | 20 import java.util.Arrays; |
20 import java.util.List; | 21 import java.util.List; |
21 | 22 |
22 /** | 23 /** |
23 * A placeholder class to call native functions. | 24 * A placeholder class to call native functions. |
24 **/ | 25 **/ |
25 @JNINamespace("mojo::runner") | 26 @JNINamespace("mojo::runner") |
26 public class ShellMain { | 27 public class ShellMain { |
27 private static final String TAG = "ShellMain"; | 28 private static final String TAG = "ShellMain"; |
28 | 29 |
29 // The key to the library to run in forked processes when running multi-proc
ess. | 30 // The key to the library to run in forked processes when running multi-proc
ess. |
30 private static final String MOJO_LIB_KEY = "mojo_lib"; | 31 private static final String MOJO_LIB_KEY = "mojo_lib"; |
31 | 32 |
32 | 33 |
33 /** | 34 /** |
34 * A guard flag for calling nativeInit() only once. | 35 * A guard flag for calling nativeInit() only once. |
35 **/ | 36 **/ |
36 private static boolean sInitialized = false; | 37 private static boolean sInitialized = false; |
37 | 38 |
38 /** | 39 /** |
39 * Initializes the native system. | 40 * Initializes the native system. |
40 **/ | 41 **/ |
41 public static void ensureInitialized(Context applicationContext, String[] pa
rameters) { | 42 public static void ensureInitialized(Context context, String[] parameters) { |
42 if (sInitialized) return; | 43 if (sInitialized) return; |
43 File cachedAppsDir = getCachedAppsDir(applicationContext); | 44 File cachedAppsDir = getCachedAppsDir(context); |
44 try { | 45 try { |
45 final File timestamp = | 46 final File timestamp = FileHelper.prepareDirectoryForAssets(context,
cachedAppsDir); |
46 FileHelper.prepareDirectoryForAssets(applicationContext, cac
hedAppsDir); | 47 for (String assetPath : FileHelper.getAssetsList(context)) { |
47 for (String assetPath : FileHelper.getAssetsList(applicationContext)
) { | 48 FileHelper.extractFromAssets( |
48 FileHelper.extractFromAssets(applicationContext, assetPath, cach
edAppsDir, | 49 context, assetPath, cachedAppsDir, FileHelper.FileType.P
ERMANENT); |
49 FileHelper.FileType.PERMANENT); | |
50 } | 50 } |
51 ApplicationInfo ai = applicationContext.getPackageManager().getAppli
cationInfo( | 51 ApplicationInfo ai = context.getPackageManager().getApplicationInfo( |
52 applicationContext.getPackageName(), PackageManager.GET_META
_DATA); | 52 context.getPackageName(), PackageManager.GET_META_DATA); |
53 Bundle bundle = ai.metaData; | 53 Bundle bundle = ai.metaData; |
54 String mojo_lib = bundle.getString(MOJO_LIB_KEY); | 54 String mojo_lib = bundle.getString(MOJO_LIB_KEY); |
55 | 55 |
56 FileHelper.createTimestampIfNecessary(timestamp); | 56 FileHelper.createTimestampIfNecessary(timestamp); |
57 File mojoShell = | 57 File mojoShell = new File(context.getApplicationInfo().nativeLibrary
Dir, mojo_lib); |
58 new File(applicationContext.getApplicationInfo().nativeLibra
ryDir, mojo_lib); | |
59 | 58 |
60 List<String> parametersList = new ArrayList<String>(); | 59 List<String> parametersList = new ArrayList<String>(); |
61 // Program name. | 60 // Program name. |
62 if (parameters != null) { | 61 if (parameters != null) { |
63 parametersList.addAll(Arrays.asList(parameters)); | 62 parametersList.addAll(Arrays.asList(parameters)); |
64 } | 63 } |
65 | 64 |
66 nativeInit(applicationContext, mojoShell.getAbsolutePath(), | 65 ContextUtils.initApplicationContext(context.getApplicationContext())
; |
| 66 nativeInit(context, mojoShell.getAbsolutePath(), |
67 parametersList.toArray(new String[parametersList.size()]), | 67 parametersList.toArray(new String[parametersList.size()]), |
68 cachedAppsDir.getAbsolutePath(), | 68 cachedAppsDir.getAbsolutePath(), getTmpDir(context).getAbsol
utePath()); |
69 getTmpDir(applicationContext).getAbsolutePath()); | |
70 sInitialized = true; | 69 sInitialized = true; |
71 } catch (Exception e) { | 70 } catch (Exception e) { |
72 Log.e(TAG, "ShellMain initialization failed.", e); | 71 Log.e(TAG, "ShellMain initialization failed.", e); |
73 throw new RuntimeException(e); | 72 throw new RuntimeException(e); |
74 } | 73 } |
75 } | 74 } |
76 | 75 |
77 /** | 76 /** |
78 * Starts the specified application in the specified context. | 77 * Starts the specified application in the specified context. |
79 **/ | 78 **/ |
(...skipping 24 matching lines...) Expand all Loading... |
104 /** | 103 /** |
105 * Initializes the native system. This API should be called only once per pr
ocess. | 104 * Initializes the native system. This API should be called only once per pr
ocess. |
106 **/ | 105 **/ |
107 private static native void nativeInit(Context context, String mojoShellPath, | 106 private static native void nativeInit(Context context, String mojoShellPath, |
108 String[] parameters, String cachedAppsDirectory, String tmpDir); | 107 String[] parameters, String cachedAppsDirectory, String tmpDir); |
109 | 108 |
110 private static native void nativeStart(); | 109 private static native void nativeStart(); |
111 | 110 |
112 private static native void nativeAddApplicationURL(String url); | 111 private static native void nativeAddApplicationURL(String url); |
113 } | 112 } |
OLD | NEW |