| 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.chrome.browser.webapps; | 5 package org.chromium.chrome.browser.webapps; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.content.SharedPreferences; | 8 import android.content.SharedPreferences; |
| 9 | 9 |
| 10 import org.chromium.base.CommandLine; |
| 10 import org.chromium.base.ContextUtils; | 11 import org.chromium.base.ContextUtils; |
| 11 import org.chromium.base.FileUtils; | 12 import org.chromium.base.FileUtils; |
| 12 import org.chromium.base.ThreadUtils; | 13 import org.chromium.base.ThreadUtils; |
| 14 import org.chromium.chrome.browser.ChromeSwitches; |
| 13 import org.chromium.webapk.lib.client.DexOptimizer; | 15 import org.chromium.webapk.lib.client.DexOptimizer; |
| 16 import org.chromium.webapk.lib.client.WebApkVersion; |
| 17 import org.chromium.webapk.lib.common.WebApkUtils; |
| 14 | 18 |
| 15 import java.io.File; | 19 import java.io.File; |
| 16 | 20 |
| 17 /** | 21 /** |
| 18 * Updates installed WebAPKs after a Chrome update. | 22 * Updates installed WebAPKs after a Chrome update. |
| 19 */ | 23 */ |
| 20 public class WebApkVersionManager { | 24 public class WebApkVersionManager { |
| 21 /** | 25 /** |
| 22 * Name of the shared preference to store whether an attempt to extract the
WebAPK runtime | 26 * Name of the shared preference for the version number of the dynamically l
oaded dex. |
| 23 * library was made. | |
| 24 */ | 27 */ |
| 25 private static final String TRIED_EXTRACTING_DEX_PREF = | 28 private static final String EXTRACTED_DEX_VERSION_PREF = |
| 26 "org.chromium.chrome.browser.webapps.TRIED_EXTRACTING_DEX"; | 29 "org.chromium.chrome.browser.webapps.extracted_dex_version"; |
| 27 | 30 |
| 28 /** | 31 /** |
| 29 * Tries to extract the WebAPK runtime dex from the Chrome APK if it has not
tried already. | 32 * Tries to extract the WebAPK runtime dex from the Chrome APK if it has not
tried already. |
| 30 * Should not be called on UI thread. | 33 * Should not be called on UI thread. |
| 31 */ | 34 */ |
| 32 public static void updateWebApksIfNeeded() { | 35 public static void updateWebApksIfNeeded() { |
| 33 assert !ThreadUtils.runningOnUiThread(); | 36 assert !ThreadUtils.runningOnUiThread(); |
| 34 | 37 |
| 38 // TODO(pkotwicz|hanxi): Detect whether the manifest of installed APKs n
eeds to be updated. |
| 39 // (crbug.com/604513) |
| 40 |
| 35 SharedPreferences preferences = ContextUtils.getAppSharedPreferences(); | 41 SharedPreferences preferences = ContextUtils.getAppSharedPreferences(); |
| 36 if (preferences.getBoolean(TRIED_EXTRACTING_DEX_PREF, false)) { | 42 int extractedDexVersion = preferences.getInt(EXTRACTED_DEX_VERSION_PREF,
-1); |
| 43 if (!CommandLine.getInstance().hasSwitch( |
| 44 ChromeSwitches.ALWAYS_EXTRACT_WEBAPK_RUNTIME_DEX_ON_STARTUP) |
| 45 && extractedDexVersion == WebApkVersion.CURRENT_RUNTIME_DEX_VERS
ION) { |
| 37 return; | 46 return; |
| 38 } | 47 } |
| 39 | 48 |
| 40 preferences.edit().putBoolean(TRIED_EXTRACTING_DEX_PREF, true).apply(); | 49 SharedPreferences.Editor editor = preferences.edit(); |
| 50 editor.putInt(EXTRACTED_DEX_VERSION_PREF, WebApkVersion.CURRENT_RUNTIME_
DEX_VERSION); |
| 51 editor.apply(); |
| 41 | 52 |
| 42 Context context = ContextUtils.getApplicationContext(); | 53 Context context = ContextUtils.getApplicationContext(); |
| 43 File dexDir = context.getDir("dex", Context.MODE_PRIVATE); | 54 File dexDir = context.getDir("dex", Context.MODE_PRIVATE); |
| 44 File dexFile = new File(dexDir, "web_apk.dex"); | 55 FileUtils.recursivelyDeleteFile(dexDir); |
| 45 if (!FileUtils.extractAsset(context, "web_apk.dex", dexFile) | 56 |
| 46 || !DexOptimizer.optimize(dexFile)) { | 57 // Recreate world-executable directory using {@link Context#getDir}. |
| 58 dexDir = context.getDir("dex", Context.MODE_PRIVATE); |
| 59 |
| 60 String dexName = |
| 61 WebApkUtils.getRuntimeDexName(WebApkVersion.CURRENT_RUNTIME_DEX_
VERSION); |
| 62 File dexFile = new File(dexDir, dexName); |
| 63 if (!FileUtils.extractAsset(context, dexName, dexFile) || !DexOptimizer.
optimize(dexFile)) { |
| 47 return; | 64 return; |
| 48 } | 65 } |
| 49 | 66 |
| 50 // Make dex file world-readable so that WebAPK can use it. | 67 // Make dex file world-readable so that WebAPK can use it. |
| 51 dexFile.setReadable(true, false); | 68 dexFile.setReadable(true, false); |
| 52 } | 69 } |
| 53 } | 70 } |
| OLD | NEW |