OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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.minting; | 5 package org.chromium.minting; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.content.pm.PackageManager.NameNotFoundException; | 8 import android.content.pm.PackageManager.NameNotFoundException; |
9 import android.util.Log; | 9 import android.util.Log; |
10 | 10 |
11 import dalvik.system.BaseDexClassLoader; | 11 import dalvik.system.BaseDexClassLoader; |
12 | 12 |
13 import java.io.File; | 13 import java.io.File; |
14 import java.io.FileOutputStream; | 14 import java.io.FileOutputStream; |
15 import java.io.InputStream; | 15 import java.io.InputStream; |
16 | 16 |
17 /** | 17 /** |
18 * Creates ClassLoader for .dex file in a remote Context's APK. | 18 * Creates ClassLoader for .dex file in a remote Context's APK. |
19 */ | 19 */ |
20 public class DexLoader { | 20 public class DexLoader { |
21 private static final int BUFFER_SIZE = 16 * 1024; | 21 private static final int BUFFER_SIZE = 16 * 1024; |
22 private static final String TAG = "cr.DexLoader"; | 22 private static final String TAG = "cr.DexLoader"; |
23 | 23 |
24 /** | 24 /** |
25 * Returns context for {@link remotePackageName} which is compatible with {@
link #load()} | |
26 * @param context A context. | |
27 * @param remotePackageName The package name of the desired context. | |
28 * @return The remote context. Returns null on an error. | |
29 */ | |
30 public static Context getRemoteContext(Context context, String remotePackage
Name) { | |
31 try { | |
32 return context.getApplicationContext().createPackageContext(remotePa
ckageName, | |
33 Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CO
DE); | |
34 } catch (NameNotFoundException e) { | |
35 return null; | |
36 } | |
37 } | |
38 | |
39 /** | |
40 * Creates ClassLoader for .dex file in {@link remoteContext}'s APK. | 25 * Creates ClassLoader for .dex file in {@link remoteContext}'s APK. |
41 * @param remoteContext The context with the APK with the .dex file. | 26 * @param remoteContext The context with the APK with the .dex file. |
42 * @param dexName The name of the .dex file in the APK. | 27 * @param dexName The name of the .dex file in the APK. |
43 * @param canaryClassName Name of class in the .dex file. Used for testing t
he ClassLoader | 28 * @param canaryClassName Name of class in the .dex file. Used for testing t
he ClassLoader |
44 * before returning it. | 29 * before returning it. |
45 * @param remoteDexFile Location of already extracted .dex file from APK. | 30 * @param remoteDexFile Location of already extracted .dex file from APK. |
46 * @param localDexDir Writable directory for caching data to speed up future
calls to | 31 * @param localDexDir Writable directory for caching data to speed up future
calls to |
47 * {@link #load()}. | 32 * {@link #load()}. |
48 * @return The ClassLoader. Returns null on an error. | 33 * @return The ClassLoader. Returns null on an error. |
49 */ | 34 */ |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 return loader; | 114 return loader; |
130 } catch (Exception e) { | 115 } catch (Exception e) { |
131 String optimizedDirPath = (optimizedDir == null) ? null : optimizedD
ir.getPath(); | 116 String optimizedDirPath = (optimizedDir == null) ? null : optimizedD
ir.getPath(); |
132 Log.w(TAG, "Could not load dex from " + dexFile.getPath() + " with o
ptimized directory " | 117 Log.w(TAG, "Could not load dex from " + dexFile.getPath() + " with o
ptimized directory " |
133 + optimizedDirPath); | 118 + optimizedDirPath); |
134 e.printStackTrace(); | 119 e.printStackTrace(); |
135 return null; | 120 return null; |
136 } | 121 } |
137 } | 122 } |
138 } | 123 } |
OLD | NEW |