| 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.webapk.shell_apk; | 5 package org.chromium.webapk.shell_apk; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.content.SharedPreferences; | 8 import android.content.SharedPreferences; |
| 9 import android.content.pm.PackageManager; | 9 import android.content.pm.PackageManager; |
| 10 import android.os.Looper; | 10 import android.os.Looper; |
| 11 import android.util.Log; | 11 import android.util.Log; |
| 12 | 12 |
| 13 import org.chromium.webapk.lib.common.WebApkConstants; |
| 13 import org.chromium.webapk.lib.common.WebApkVersionUtils; | 14 import org.chromium.webapk.lib.common.WebApkVersionUtils; |
| 14 | 15 |
| 15 import java.io.File; | 16 import java.io.File; |
| 16 import java.util.Scanner; | 17 import java.util.Scanner; |
| 17 | 18 |
| 18 /** | 19 /** |
| 19 * Creates ClassLoader for WebAPK-specific dex file in Chrome APK's assets. | 20 * Creates ClassLoader for WebAPK-specific dex file in Chrome APK's assets. |
| 20 */ | 21 */ |
| 21 public class HostBrowserClassLoader { | 22 public class HostBrowserClassLoader { |
| 22 private static final String TAG = "cr_HostBrowserClassLoader"; | 23 private static final String TAG = "cr_HostBrowserClassLoader"; |
| 23 | 24 |
| 24 /** | 25 /** |
| 25 * Name of the shared preferences file. | |
| 26 */ | |
| 27 private static final String PREF_PACKAGE = "org.chromium.webapk.shell_apk"; | |
| 28 | |
| 29 /** | |
| 30 * Name of the shared preference for Chrome's version code. | 26 * Name of the shared preference for Chrome's version code. |
| 31 */ | 27 */ |
| 32 private static final String REMOTE_VERSION_CODE_PREF = | 28 private static final String REMOTE_VERSION_CODE_PREF = |
| 33 "org.chromium.webapk.shell_apk.version_code"; | 29 "org.chromium.webapk.shell_apk.version_code"; |
| 34 | 30 |
| 35 /** | 31 /** |
| 36 * Name of the shared preference for the version number of the dynamically l
oaded dex. | 32 * Name of the shared preference for the version number of the dynamically l
oaded dex. |
| 37 */ | 33 */ |
| 38 private static final String RUNTIME_DEX_VERSION_PREF = | 34 private static final String RUNTIME_DEX_VERSION_PREF = |
| 39 "org.chromium.webapk.shell_apk.dex_version"; | 35 "org.chromium.webapk.shell_apk.dex_version"; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 70 * Creates ClassLoader for WebAPK dex. | 66 * Creates ClassLoader for WebAPK dex. |
| 71 * @param context WebAPK's context. | 67 * @param context WebAPK's context. |
| 72 * @param remoteContext Host browser's context. | 68 * @param remoteContext Host browser's context. |
| 73 * @param canaryClassName Class to load to check that ClassLoader is valid. | 69 * @param canaryClassName Class to load to check that ClassLoader is valid. |
| 74 * @param dexLoader DexLoader for creating ClassLoader. | 70 * @param dexLoader DexLoader for creating ClassLoader. |
| 75 * @return The ClassLoader. | 71 * @return The ClassLoader. |
| 76 */ | 72 */ |
| 77 public static ClassLoader createClassLoader( | 73 public static ClassLoader createClassLoader( |
| 78 Context context, Context remoteContext, DexLoader dexLoader, String
canaryClassName) { | 74 Context context, Context remoteContext, DexLoader dexLoader, String
canaryClassName) { |
| 79 SharedPreferences preferences = | 75 SharedPreferences preferences = |
| 80 context.getSharedPreferences(PREF_PACKAGE, Context.MODE_PRIVATE)
; | 76 context.getSharedPreferences(WebApkConstants.PREF_PACKAGE, Conte
xt.MODE_PRIVATE); |
| 81 | 77 |
| 82 int runtimeDexVersion = preferences.getInt(RUNTIME_DEX_VERSION_PREF, -1)
; | 78 int runtimeDexVersion = preferences.getInt(RUNTIME_DEX_VERSION_PREF, -1)
; |
| 83 int newRuntimeDexVersion = checkForNewRuntimeDexVersion(preferences, rem
oteContext); | 79 int newRuntimeDexVersion = checkForNewRuntimeDexVersion(preferences, rem
oteContext); |
| 84 if (newRuntimeDexVersion == -1) { | 80 if (newRuntimeDexVersion == -1) { |
| 85 newRuntimeDexVersion = runtimeDexVersion; | 81 newRuntimeDexVersion = runtimeDexVersion; |
| 86 } | 82 } |
| 87 File localDexDir = context.getDir("dex", Context.MODE_PRIVATE); | 83 File localDexDir = context.getDir("dex", Context.MODE_PRIVATE); |
| 88 if (newRuntimeDexVersion != runtimeDexVersion) { | 84 if (newRuntimeDexVersion != runtimeDexVersion) { |
| 89 Log.w(TAG, "Delete cached dex files."); | 85 Log.w(TAG, "Delete cached dex files."); |
| 90 dexLoader.deleteCachedDexes(localDexDir); | 86 dexLoader.deleteCachedDexes(localDexDir); |
| 91 } | 87 } |
| 92 | 88 |
| 93 String dexAssetName = WebApkVersionUtils.getRuntimeDexName(newRuntimeDex
Version); | 89 String dexAssetName = WebApkVersionUtils.getRuntimeDexName(newRuntimeDex
Version); |
| 94 File remoteDexFile = | 90 File remoteDexFile = |
| 95 new File(remoteContext.getDir("dex", Context.MODE_PRIVATE), dexA
ssetName); | 91 new File(remoteContext.getDir("dex", Context.MODE_PRIVATE), dexA
ssetName); |
| 96 return dexLoader.load( | 92 return dexLoader.load( |
| 97 remoteContext, dexAssetName, canaryClassName, remoteDexFile, loc
alDexDir); | 93 remoteContext, dexAssetName, canaryClassName, remoteDexFile, loc
alDexDir); |
| 98 } | 94 } |
| 99 | 95 |
| 100 /** | 96 /** |
| 101 * Returns whether {@link sClassLoader} can be reused. | 97 * Returns whether {@link sClassLoader} can be reused. |
| 102 */ | 98 */ |
| 103 public static boolean canReuseClassLoaderInstance(Context context, Context r
emoteContext) { | 99 public static boolean canReuseClassLoaderInstance(Context context, Context r
emoteContext) { |
| 104 // WebAPK may still be running when the host browser gets upgraded. Prev
ent ClassLoader from | 100 // WebAPK may still be running when the host browser gets upgraded. Prev
ent ClassLoader from |
| 105 // getting reused in this scenario. | 101 // getting reused in this scenario. |
| 106 SharedPreferences preferences = | 102 SharedPreferences preferences = |
| 107 context.getSharedPreferences(PREF_PACKAGE, Context.MODE_PRIVATE)
; | 103 context.getSharedPreferences(WebApkConstants.PREF_PACKAGE, Conte
xt.MODE_PRIVATE); |
| 108 int cachedRemoteVersionCode = preferences.getInt(REMOTE_VERSION_CODE_PRE
F, -1); | 104 int cachedRemoteVersionCode = preferences.getInt(REMOTE_VERSION_CODE_PRE
F, -1); |
| 109 int remoteVersionCode = getVersionCode(remoteContext); | 105 int remoteVersionCode = getVersionCode(remoteContext); |
| 110 return remoteVersionCode == cachedRemoteVersionCode; | 106 return remoteVersionCode == cachedRemoteVersionCode; |
| 111 } | 107 } |
| 112 | 108 |
| 113 /** | 109 /** |
| 114 * Checks if there is a new "runtime dex" version number. If there is a new
version number, | 110 * Checks if there is a new "runtime dex" version number. If there is a new
version number, |
| 115 * updates SharedPreferences. | 111 * updates SharedPreferences. |
| 116 * @param preferences WebAPK's SharedPreferences. | 112 * @param preferences WebAPK's SharedPreferences. |
| 117 * @param remoteContext | 113 * @param remoteContext |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 return value; | 171 return value; |
| 176 } | 172 } |
| 177 | 173 |
| 178 /** | 174 /** |
| 179 * Asserts that current thread is the UI thread. | 175 * Asserts that current thread is the UI thread. |
| 180 */ | 176 */ |
| 181 private static void assertRunningOnUiThread() { | 177 private static void assertRunningOnUiThread() { |
| 182 assert Looper.getMainLooper().equals(Looper.myLooper()); | 178 assert Looper.getMainLooper().equals(Looper.myLooper()); |
| 183 } | 179 } |
| 184 } | 180 } |
| OLD | NEW |