Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(192)

Side by Side Diff: web_apks/minting_example/src/org/chromium/minting/MintingApplication.java

Issue 1953543002: Rename WebAPK related classes to be of the format *WebApk*.java (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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.minting;
6
7 import android.app.Application;
8 import android.content.Context;
9 import android.content.pm.PackageManager.NameNotFoundException;
10 import android.util.Log;
11
12 import org.chromium.minting.lib.common.WebAPKUtils;
13
14 import java.lang.reflect.Array;
15 import java.util.List;
16
17 /**
18 * Example application for a minted APK.
19 */
20 public class MintingApplication extends Application {
21 // Context of Chrome.
22 private Context mRemoteContext = null;
23
24 private static final String TAG = "cr.MintingApplication";
25 /**
26 * Copy all the objects from a specified field of the hostInstance to the sa me field of the
27 * mintInstance. As a result, the given field of the mintInstance will conta in all the
28 * objects from both instances.
29 * @param mintInstance the first instance which contains a field of the give n fieldName.
30 * @param fieldName the name of field to expand to an Object array.
31 * @param hostInstance the second instance which has a field of the given fi eldName.
32 * @throws NoSuchFieldException
33 * @throws IllegalArgumentException
34 * @throws IllegalAccessException
35 */
36 private static void expandField(Object mintInstance, String fieldName, Objec t hostInstance)
37 throws NoSuchFieldException, IllegalArgumentException,
38 IllegalAccessException {
39 try {
40 Object hostCurrentDirs = Reflect.getField(hostInstance, fieldName);
41 Object mintCurrentDirs = Reflect.getField(mintInstance, fieldName);
42 // Switched from an array to an ArrayList in Lollipop.
43 if (mintCurrentDirs instanceof List) {
44 List<Object> mintDirsAsList = (List<Object>) mintCurrentDirs;
45 concatAndRemoveEndDuplicates(mintDirsAsList, (List<Object>) host CurrentDirs);
46 } else {
47 Object[] mintDirsAsArray = (Object[]) mintCurrentDirs;
48 Object[] hostDirsAsArray = (Object[]) hostCurrentDirs;
49 Reflect.setField(mintInstance, fieldName,
50 concatAndRemoveEndDuplicates(mintDirsAsArray, hostDirsAs Array));
51 }
52 } catch (ReflectiveOperationException e) {
53 e.printStackTrace();
54 }
55 }
56
57 /**
58 * Combines two arrays into a new array.
59 * If the two arrays end with duplicated elements, keep one copy only. For e xample,
60 * the first array is [A, B, C, D], and the second array is [E, F, C, D], th e combined one is
61 * [A, B, E, F, C, D]. The unique elements are stored before the duplicates.
62 * The arrays must be of the same type.
63 * @param mintArrays: the array from a WebAPK
64 * @param hostArrays: the array from the host
65 * @return: a combined array consists of [WebAPK unique elements, host's uni que elements,
66 * duplicated one]
67 */
68 private static Object[] concatAndRemoveEndDuplicates(Object[] mintArrays, Ob ject[] hostArrays) {
69 int duplicate1 = mintArrays.length - 1;
70 int duplicate2 = hostArrays.length - 1;
71 int count = 0;
72 while (duplicate1 >= 0 && duplicate2 >= 0
73 && mintArrays[duplicate1].toString().equals(hostArrays[duplicate 2].toString())) {
74 --duplicate1;
75 --duplicate2;
76 ++count;
77 }
78 Object[] combined = (Object[]) Array.newInstance(mintArrays.getClass().g etComponentType(),
79 mintArrays.length + hostArrays.length - count);
80 if (mintArrays.length - count > 0) {
81 System.arraycopy(mintArrays, 0, combined, 0, mintArrays.length - cou nt);
82 }
83 System.arraycopy(hostArrays, 0, combined, mintArrays.length - count, hos tArrays.length);
84 return combined;
85 }
86
87 /**
88 * Add the unique elements of the second list to the first one.
89 * If the two lists end with duplicated elements, keep one copy only. For ex ample,
90 * the first list is [A, B, C, D], and the second list is [E, F, C, D], the combined one is
91 * [A, B, E, F, C, D]. The unique elements are store before the duplicates.
92 * The lists must be of the same type.
93 * @param mintList: the list from a WebAPK
94 * @param hostList: the list from the host
95 * @return: a combined list consists of [WebAPK unique elements, host's uniq ue elements,
96 * duplicated one]
97 */
98 private static void concatAndRemoveEndDuplicates(List<Object> mintList, List <Object> hostList) {
99 int duplicate1 = mintList.size() - 1;
100 int duplicate2 = hostList.size() - 1;
101 while (duplicate1 >= 0 && duplicate2 >= 0
102 && mintList.get(duplicate1).toString().equals(hostList.get(dupli cate2).toString()))
103 {
104 --duplicate1;
105 --duplicate2;
106 }
107 for (int i = 0; i < duplicate2 + 1; i++) {
108 mintList.add(duplicate1 + i, hostList.get(i));
109 }
110 }
111
112 private void addExternalLoader() throws ReflectiveOperationException {
113 if (mRemoteContext == null) {
114 Log.w(TAG, "Failed to add external loader since the remote context i s null.");
115 return;
116 }
117 ClassLoader externalLoader = mRemoteContext.getClassLoader();
118 ClassLoader mintLoader = getClassLoader();
119
120 Object extDexPathList = Reflect.getField(externalLoader, "pathList");
121 Object mintDexPathList = Reflect.getField(mintLoader, "pathList");
122 expandField(mintDexPathList, "dexElements", extDexPathList);
123 }
124
125 private void addNativeLibrarySearchPath() throws ReflectiveOperationExceptio n {
126 if (mRemoteContext == null) {
127 Log.w(TAG, "Failed to add external loader since the remote context i s null.");
128 return;
129 }
130 ClassLoader externalLoader = mRemoteContext.getClassLoader();
131 ClassLoader mintLoader = getClassLoader();
132
133 // Since both WebAPK and its host have the "system/lib" and "vendor/lib" at the end of
134 // the native library directory path list, we want to add host's directo ries before the
135 // system directory, and only keep one copy of the system directories.
136 // This is because when System.loadLibrary() is called, we want it searc h the host's
137 // native library directories first. If a ".so" file doesn't exit, then search /system/lib.
138 Object extDexPathList = Reflect.getField(externalLoader, "pathList");
139 Object mintDexPathList = Reflect.getField(mintLoader, "pathList");
140 expandField(mintDexPathList, "nativeLibraryDirectories", extDexPathList) ;
141 expandField(mintDexPathList, "nativeLibraryPathElements", extDexPathList );
142 }
143
144 @Override
145 public void onCreate() {
146 super.onCreate();
147 mRemoteContext = WebAPKUtils.getHostBrowserContext(this);
148 try {
149 addExternalLoader();
150 addNativeLibrarySearchPath();
151 } catch (ReflectiveOperationException e) {
152 e.printStackTrace();
153 }
154 Log.w(TAG, "Successfully add external loader.");
155 }
156 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698