| Index: base/android/java/src/org/chromium/base/ResourceExtractor.java
|
| diff --git a/base/android/java/src/org/chromium/base/ResourceExtractor.java b/base/android/java/src/org/chromium/base/ResourceExtractor.java
|
| index d44f2fc7c22bdc1697571606c3465a0bc902f3f4..030dcf9609f03a7169d685bd895ecbdd5afe22eb 100644
|
| --- a/base/android/java/src/org/chromium/base/ResourceExtractor.java
|
| +++ b/base/android/java/src/org/chromium/base/ResourceExtractor.java
|
| @@ -6,18 +6,17 @@ package org.chromium.base;
|
|
|
| import android.annotation.TargetApi;
|
| import android.content.Context;
|
| -import android.content.SharedPreferences;
|
| import android.content.pm.PackageInfo;
|
| import android.content.pm.PackageManager;
|
| -import android.content.res.AssetManager;
|
| import android.os.AsyncTask;
|
| import android.os.Build;
|
| import android.os.Handler;
|
| import android.os.Looper;
|
| import android.os.Trace;
|
| -import android.preference.PreferenceManager;
|
| import android.util.Log;
|
|
|
| +import org.chromium.base.annotations.SuppressFBWarnings;
|
| +
|
| import java.io.File;
|
| import java.io.FileOutputStream;
|
| import java.io.FilenameFilter;
|
| @@ -28,7 +27,6 @@ import java.util.ArrayList;
|
| import java.util.List;
|
| import java.util.concurrent.CancellationException;
|
| import java.util.concurrent.ExecutionException;
|
| -import java.util.regex.Pattern;
|
|
|
| /**
|
| * Handles extracting the necessary resources bundled in an APK and moving them to a location on
|
| @@ -37,23 +35,25 @@ import java.util.regex.Pattern;
|
| public class ResourceExtractor {
|
|
|
| private static final String LOGTAG = "ResourceExtractor";
|
| - private static final String LAST_LANGUAGE = "Last language";
|
| - private static final String PAK_FILENAMES_LEGACY_NOREUSE = "Pak filenames";
|
| private static final String ICU_DATA_FILENAME = "icudtl.dat";
|
| private static final String V8_NATIVES_DATA_FILENAME = "natives_blob.bin";
|
| private static final String V8_SNAPSHOT_DATA_FILENAME = "snapshot_blob.bin";
|
|
|
| - private static String[] sMandatoryPaks = null;
|
| -
|
| - // By default, we attempt to extract a pak file for the users
|
| - // current device locale. Use setExtractImplicitLocale() to
|
| - // change this behavior.
|
| - private static boolean sExtractImplicitLocalePak = true;
|
| + private static ResourceEntry[] sResourcesToExtract = new ResourceEntry[0];
|
|
|
| - private static boolean isAppDataFile(String file) {
|
| - return ICU_DATA_FILENAME.equals(file)
|
| - || V8_NATIVES_DATA_FILENAME.equals(file)
|
| - || V8_SNAPSHOT_DATA_FILENAME.equals(file);
|
| + /**
|
| + * Holds information about a res/raw file (e.g. locale .pak files).
|
| + */
|
| + public static final class ResourceEntry {
|
| + public final int resourceId;
|
| + public final String pathWithinApk;
|
| + public final String extractedFileName;
|
| +
|
| + public ResourceEntry(int resourceId, String pathWithinApk, String extractedFileName) {
|
| + this.resourceId = resourceId;
|
| + this.pathWithinApk = pathWithinApk;
|
| + this.extractedFileName = extractedFileName;
|
| + }
|
| }
|
|
|
| private class ExtractTask extends AsyncTask<Void, Void, Void> {
|
| @@ -61,12 +61,38 @@ public class ResourceExtractor {
|
|
|
| private final List<Runnable> mCompletionCallbacks = new ArrayList<Runnable>();
|
|
|
| - public ExtractTask() {
|
| + private void extractResourceHelper(InputStream is, File outFile, byte[] buffer)
|
| + throws IOException {
|
| + OutputStream os = null;
|
| + try {
|
| + os = new FileOutputStream(outFile);
|
| + Log.i(LOGTAG, "Extracting resource " + outFile);
|
| +
|
| + int count = 0;
|
| + while ((count = is.read(buffer, 0, BUFFER_SIZE)) != -1) {
|
| + os.write(buffer, 0, count);
|
| + }
|
| + os.flush();
|
| +
|
| + // Ensure something reasonable was written.
|
| + if (outFile.length() == 0) {
|
| + throw new IOException(outFile + " extracted with 0 length!");
|
| + }
|
| + } finally {
|
| + try {
|
| + if (is != null) {
|
| + is.close();
|
| + }
|
| + } finally {
|
| + if (os != null) {
|
| + os.close();
|
| + }
|
| + }
|
| + }
|
| }
|
|
|
| private void doInBackgroundImpl() {
|
| final File outputDir = getOutputDir();
|
| - final File appDataDir = getAppDataDir();
|
| if (!outputDir.exists() && !outputDir.mkdirs()) {
|
| Log.e(LOGTAG, "Unable to create pak resources directory!");
|
| return;
|
| @@ -83,97 +109,22 @@ public class ResourceExtractor {
|
| deleteFiles();
|
| }
|
|
|
| - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
|
| - String currentLocale = LocaleUtils.getDefaultLocale();
|
| - String currentLanguage = currentLocale.split("-", 2)[0];
|
| - // If everything we need is already there (and the locale hasn't
|
| - // changed), quick exit.
|
| - if (prefs.getString(LAST_LANGUAGE, "").equals(currentLanguage)) {
|
| - boolean filesPresent = true;
|
| - for (String file : sMandatoryPaks) {
|
| - File directory = isAppDataFile(file) ? appDataDir : outputDir;
|
| - if (!new File(directory, file).exists()) {
|
| - filesPresent = false;
|
| - break;
|
| - }
|
| - }
|
| - if (filesPresent) return;
|
| - } else {
|
| - prefs.edit().putString(LAST_LANGUAGE, currentLanguage).apply();
|
| - }
|
| -
|
| - StringBuilder p = new StringBuilder();
|
| - for (String mandatoryPak : sMandatoryPaks) {
|
| - if (p.length() > 0) p.append('|');
|
| - p.append("\\Q" + mandatoryPak + "\\E");
|
| - }
|
| -
|
| - if (sExtractImplicitLocalePak) {
|
| - if (p.length() > 0) p.append('|');
|
| - // As well as the minimum required set of .paks above, we'll
|
| - // also add all .paks that we have for the user's currently
|
| - // selected language.
|
| - p.append(currentLanguage);
|
| - p.append("(-\\w+)?\\.pak");
|
| - }
|
| -
|
| - Pattern paksToInstall = Pattern.compile(p.toString());
|
| -
|
| - AssetManager manager = mContext.getResources().getAssets();
|
| beginTraceSection("WalkAssets");
|
| + byte[] buffer = new byte[BUFFER_SIZE];
|
| try {
|
| - // Loop through every asset file that we have in the APK, and look for the
|
| - // ones that we need to extract by trying to match the Patterns that we
|
| - // created above.
|
| - byte[] buffer = null;
|
| - String[] files = manager.list("");
|
| - for (String file : files) {
|
| - if (!paksToInstall.matcher(file).matches()) {
|
| - continue;
|
| - }
|
| - File output = new File(isAppDataFile(file) ? appDataDir : outputDir, file);
|
| + // Extract all files that don't already exist.
|
| + for (ResourceEntry entry : sResourcesToExtract) {
|
| + File output = new File(outputDir, entry.extractedFileName);
|
| if (output.exists()) {
|
| continue;
|
| }
|
| -
|
| - InputStream is = null;
|
| - OutputStream os = null;
|
| beginTraceSection("ExtractResource");
|
| + InputStream inputStream =
|
| + mContext.getResources().openRawResource(entry.resourceId);
|
| try {
|
| - is = manager.open(file);
|
| - os = new FileOutputStream(output);
|
| - Log.i(LOGTAG, "Extracting resource " + file);
|
| - if (buffer == null) {
|
| - buffer = new byte[BUFFER_SIZE];
|
| - }
|
| -
|
| - int count = 0;
|
| - while ((count = is.read(buffer, 0, BUFFER_SIZE)) != -1) {
|
| - os.write(buffer, 0, count);
|
| - }
|
| - os.flush();
|
| -
|
| - // Ensure something reasonable was written.
|
| - if (output.length() == 0) {
|
| - throw new IOException(file + " extracted with 0 length!");
|
| - }
|
| -
|
| - if (isAppDataFile(file)) {
|
| - // icu and V8 data need to be accessed by a renderer
|
| - // process.
|
| - output.setReadable(true, false);
|
| - }
|
| + extractResourceHelper(inputStream, output, buffer);
|
| } finally {
|
| - try {
|
| - if (is != null) {
|
| - is.close();
|
| - }
|
| - } finally {
|
| - if (os != null) {
|
| - os.close();
|
| - }
|
| - endTraceSection(); // ExtractResource
|
| - }
|
| + endTraceSection(); // ExtractResource
|
| }
|
| }
|
| } catch (IOException e) {
|
| @@ -303,53 +254,19 @@ public class ResourceExtractor {
|
| }
|
|
|
| /**
|
| - * Specifies the .pak files that should be extracted from the APK's asset resources directory
|
| - * and moved to {@link #getOutputDirFromContext(Context)}.
|
| - * @param mandatoryPaks The list of pak files to be loaded. If no pak files are
|
| - * required, pass a single empty string.
|
| - */
|
| - public static void setMandatoryPaksToExtract(String... mandatoryPaks) {
|
| - assert (sInstance == null || sInstance.mExtractTask == null)
|
| - : "Must be called before startExtractingResources is called";
|
| - sMandatoryPaks = mandatoryPaks;
|
| -
|
| - }
|
| -
|
| - /**
|
| - * By default the ResourceExtractor will attempt to extract a pak resource for the users
|
| - * currently specified locale. This behavior can be changed with this function and is
|
| - * only needed by tests.
|
| - * @param extract False if we should not attempt to extract a pak file for
|
| - * the users currently selected locale and try to extract only the
|
| - * pak files specified in sMandatoryPaks.
|
| + * Specifies the files that should be extracted from the APK.
|
| + * and moved to {@link #getOutputDir()}.
|
| */
|
| - @VisibleForTesting
|
| - public static void setExtractImplicitLocaleForTesting(boolean extract) {
|
| + @SuppressFBWarnings("EI_EXPOSE_STATIC_REP2")
|
| + public static void setResourcesToExtract(ResourceEntry[] entries) {
|
| assert (sInstance == null || sInstance.mExtractTask == null)
|
| : "Must be called before startExtractingResources is called";
|
| - sExtractImplicitLocalePak = extract;
|
| + sResourcesToExtract = entries;
|
| }
|
|
|
| - /**
|
| - * Marks all the 'pak' resources, packaged as assets, for extraction during
|
| - * running the tests.
|
| - */
|
| - @VisibleForTesting
|
| - public void setExtractAllPaksAndV8SnapshotForTesting() {
|
| - List<String> pakAndSnapshotFileAssets = new ArrayList<String>();
|
| - AssetManager manager = mContext.getResources().getAssets();
|
| - try {
|
| - String[] files = manager.list("");
|
| - for (String file : files) {
|
| - if (file.endsWith(".pak")) pakAndSnapshotFileAssets.add(file);
|
| - }
|
| - } catch (IOException e) {
|
| - Log.w(LOGTAG, "Exception while accessing assets: " + e.getMessage(), e);
|
| - }
|
| - pakAndSnapshotFileAssets.add("natives_blob.bin");
|
| - pakAndSnapshotFileAssets.add("snapshot_blob.bin");
|
| - setMandatoryPaksToExtract(pakAndSnapshotFileAssets.toArray(
|
| - new String[pakAndSnapshotFileAssets.size()]));
|
| + // TODO(agrieve): Delete this method ones all usages of it are updated.
|
| + public static void setMandatoryPaksToExtract(String... paths) {
|
| + assert paths.length == 1 && "".equals(paths[0]);
|
| }
|
|
|
| private ResourceExtractor(Context context) {
|
| @@ -421,6 +338,10 @@ public class ResourceExtractor {
|
| return;
|
| }
|
|
|
| + // If a previous release extracted resources, and the current release does not,
|
| + // deleteFiles() will not run and some files will be left. This currently
|
| + // can happen for ContentShell, but not for Chrome proper, since we always extract
|
| + // locale pak files.
|
| if (shouldSkipPakExtraction()) {
|
| return;
|
| }
|
| @@ -473,12 +394,9 @@ public class ResourceExtractor {
|
| }
|
|
|
| /**
|
| - * Pak extraction not necessarily required by the embedder; we allow them to skip
|
| - * this process if they call setMandatoryPaksToExtract with a single empty String.
|
| + * Pak extraction not necessarily required by the embedder.
|
| */
|
| private static boolean shouldSkipPakExtraction() {
|
| - // Must call setMandatoryPaksToExtract before beginning resource extraction.
|
| - assert sMandatoryPaks != null;
|
| - return sMandatoryPaks.length == 1 && "".equals(sMandatoryPaks[0]);
|
| + return sResourcesToExtract.length == 0;
|
| }
|
| }
|
|
|