Skip to content

Commit

Permalink
Reland chromium#2 of Move language pak files to assets
Browse files Browse the repository at this point in the history
Reverted in:
https://codereview.chromium.org/2406093002/
and https://codereview.chromium.org/2404043002

Reason for reland:
Now auto-detects whether locale paks are compressed or stored
(for chrome, but not webview)

TBR=michaelbai
BUG=653771,644377, 529604

Review-Url: https://codereview.chromium.org/2404253003
Cr-Commit-Position: refs/heads/master@{#425402}
  • Loading branch information
agrieve authored and Commit bot committed Oct 14, 2016
1 parent 633c166 commit 732f0d8
Show file tree
Hide file tree
Showing 25 changed files with 140 additions and 463 deletions.
5 changes: 3 additions & 2 deletions android_webview/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ webview_repack_locales("repack_locales") {
}
}

locale_pak_resources("locale_paks") {
android_assets("locale_pak_assets") {
disable_compression = true
sources = []
foreach(_locale, locales) {
sources += [ "$root_out_dir/android_webview/locales/$_locale.pak" ]
Expand Down Expand Up @@ -135,6 +136,7 @@ android_assets("monochrome_webview_assets") {

android_assets("assets") {
deps = [
":locale_pak_assets",
":monochrome_webview_assets",
":pak_file_assets",
]
Expand Down Expand Up @@ -164,7 +166,6 @@ android_resources("resources") {
resource_dirs = [ "java/res" ]
custom_package = "org.chromium.android_webview"
deps = [
":locale_paks",
":strings_grd",
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import org.chromium.android_webview.AwResource;
import org.chromium.android_webview.AwSettings;
import org.chromium.android_webview.HttpAuthDatabase;
import org.chromium.android_webview.R;
import org.chromium.android_webview.ResourcesContextWrapperFactory;
import org.chromium.base.BuildConfig;
import org.chromium.base.CommandLine;
Expand All @@ -62,7 +61,6 @@
import org.chromium.base.library_loader.ProcessInitException;
import org.chromium.content.browser.ContentViewStatics;
import org.chromium.net.NetworkChangeNotifier;
import org.chromium.ui.base.ResourceBundle;

import java.io.File;
import java.util.Queue;
Expand Down Expand Up @@ -392,7 +390,6 @@ private void startChromiumLocked() {
final String webViewPackageName = WebViewFactory.getLoadedPackageInfo().packageName;
Context context = ContextUtils.getApplicationContext();
setUpResources(webViewPackageName, context);
ResourceBundle.initializeLocalePaks(context, R.array.locale_paks);
initPlatSupportLibrary();
initNetworkChangeNotifier(context);
final int extraBindFlags = Context.BIND_EXTERNAL_SERVICE;
Expand Down
1 change: 0 additions & 1 deletion android_webview/system_webview_apk_tmpl.gni
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ template("system_webview_apk_tmpl") {
_native_lib_file =
rebase_path("$root_gen_dir/CHROME_VERSION.json", root_out_dir)
native_lib_version_arg = "@FileArg($_native_lib_file:full-quoted)"
extensions_to_not_compress = ".lpak,.pak,.bin,.dat"
if (build_apk_secondary_abi && android_64bit_target_cpu) {
secondary_abi_shared_libraries = [ "//android_webview:libwebviewchromium($android_secondary_abi_toolchain)" ]
}
Expand Down
3 changes: 1 addition & 2 deletions android_webview/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ android_apk("android_webview_apk") {
":android_webview_apk_assets",
":android_webview_apk_resources",
"//android_webview:android_webview_java",
"//android_webview:locale_pak_assets",
"//base:base_java",
"//base:base_java_test_support",
"//components/policy/android:policy_java_test_support",
Expand Down Expand Up @@ -51,8 +52,6 @@ android_apk("android_webview_apk") {
_native_lib_file =
rebase_path("$root_gen_dir/CHROME_VERSION.json", root_build_dir)
native_lib_version_arg = "@FileArg($_native_lib_file:full-quoted)"

extensions_to_not_compress = ".dat,.bin,.pak,.lpak"
}

android_resources("android_webview_apk_resources") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

package org.chromium.android_webview.shell;

import org.chromium.android_webview.R;
import org.chromium.base.CommandLine;
import org.chromium.base.annotations.SuppressFBWarnings;
import org.chromium.content.app.ContentApplication;
import org.chromium.ui.base.ResourceBundle;

/**
* The android_webview shell Application subclass.
Expand All @@ -18,16 +16,6 @@ public AwShellApplication() {
super(false /* mShouldInitializeApplicationStatusTracking */);
}

@Override
public void onCreate() {
super.onCreate();
}

@Override
protected void initializeLibraryDependencies() {
ResourceBundle.initializeLocalePaks(this, R.array.locale_paks);
}

@SuppressFBWarnings("DMI_HARDCODED_ABSOLUTE_FILENAME")
@Override
public void initCommandLine() {
Expand Down
26 changes: 17 additions & 9 deletions base/android/java/src/org/chromium/base/LocaleUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,23 @@ private LocaleUtils() {
}

/**
* @return the string for the given locale, translating
* Android deprecated language codes into the modern ones
* used by Chromium.
* @return The string for the given locale, translating Android deprecated language codes
* into the modern ones used by Chromium.
*/
public static String getLocale(Locale locale) {
String language = locale.getLanguage();
String language = getLanguage(locale);
String country = locale.getCountry();

return country.isEmpty() ? language : language + "-" + country;
}

/**
* @return The language for the given locale, translating Android deprecated languages codes
* into modern ones used by Chromium.
*/
public static String getLanguage(Locale locale) {
String language = locale.getLanguage();

// Android uses deprecated lanuages codes for Hebrew and Indonesian but Chromium uses the
// updated codes. Also, Android uses "tl" while Chromium uses "fil" for Tagalog/Filipino.
// So apply a mapping.
Expand All @@ -38,21 +47,20 @@ public static String getLocale(Locale locale) {
} else if ("tl".equals(language)) {
language = "fil";
}
return country.isEmpty() ? language : language + "-" + country;
return language;
}

/**
* @return the default locale, translating Android deprecated
* language codes into the modern ones used by Chromium.
* @return The default locale, translating Android deprecated language codes into the modern
* ones used by Chromium.
*/
@CalledByNative
public static String getDefaultLocale() {
return getLocale(Locale.getDefault());
}

/**
* Get the default country code set during install.
* @return country code.
* @return The default country code set during install.
*/
@CalledByNative
private static String getDefaultCountryCode() {
Expand Down
69 changes: 25 additions & 44 deletions base/android/java/src/org/chromium/base/ResourceExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,21 @@

package org.chromium.base;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;

import org.chromium.base.annotations.SuppressFBWarnings;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;

Expand All @@ -35,23 +33,7 @@ public class ResourceExtractor {
private static final String V8_NATIVES_DATA_FILENAME = "natives_blob.bin";
private static final String V8_SNAPSHOT_DATA_FILENAME = "snapshot_blob.bin";
private static final String APP_VERSION_PREF = "org.chromium.base.ResourceExtractor.Version";

private static ResourceEntry[] sResourcesToExtract = new ResourceEntry[0];

/**
* 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 static final String FALLBACK_LOCALE = "en-US";

private class ExtractTask extends AsyncTask<Void, Void, Void> {
private static final int BUFFER_SIZE = 16 * 1024;
Expand Down Expand Up @@ -107,16 +89,16 @@ private void doInBackgroundImpl() {
TraceEvent.begin("WalkAssets");
byte[] buffer = new byte[BUFFER_SIZE];
try {
for (ResourceEntry entry : sResourcesToExtract) {
File output = new File(outputDir, entry.extractedFileName);
for (String assetName : mAssetsToExtract) {
File output = new File(outputDir, assetName);
// TODO(agrieve): It would be better to check that .length == expectedLength.
// http://crbug.com/606413
if (output.length() != 0) {
continue;
}
TraceEvent.begin("ExtractResource");
InputStream inputStream = mContext.getResources().openRawResource(
entry.resourceId);
InputStream inputStream =
ContextUtils.getApplicationContext().getAssets().open(assetName);
try {
extractResourceHelper(inputStream, output, buffer);
} finally {
Expand All @@ -128,7 +110,7 @@ private void doInBackgroundImpl() {
// Try to recover here, can we try again after deleting files instead of
// returning null? It might be useful to gather UMA here too to track if
// this happens with regularity.
Log.w(TAG, "Exception unpacking required pak resources: %s", e.getMessage());
Log.w(TAG, "Exception unpacking required pak asset: %s", e.getMessage());
deleteFiles();
return;
} finally {
Expand Down Expand Up @@ -166,42 +148,41 @@ protected void onPostExecute(Void result) {

/** Returns a number that is different each time the apk changes. */
private long getApkVersion() {
PackageManager pm = mContext.getPackageManager();
PackageManager pm = ContextUtils.getApplicationContext().getPackageManager();
try {
// More appropriate would be versionCode, but it doesn't change while developing.
PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), 0);
PackageInfo pi =
pm.getPackageInfo(ContextUtils.getApplicationContext().getPackageName(), 0);
return pi.lastUpdateTime;
} catch (PackageManager.NameNotFoundException e) {
throw new RuntimeException(e);
}
}
}

private final Context mContext;
private ExtractTask mExtractTask;
private final String[] mAssetsToExtract = detectFilesToExtract();

private static ResourceExtractor sInstance;

public static ResourceExtractor get(Context context) {
public static ResourceExtractor get() {
if (sInstance == null) {
sInstance = new ResourceExtractor(context);
sInstance = new ResourceExtractor();
}
return sInstance;
}

/**
* Specifies the files that should be extracted from the APK.
* and moved to {@link #getOutputDir()}.
*/
@SuppressFBWarnings("EI_EXPOSE_STATIC_REP2")
public static void setResourcesToExtract(ResourceEntry[] entries) {
assert (sInstance == null || sInstance.mExtractTask == null)
: "Must be called before startExtractingResources is called";
sResourcesToExtract = entries;
}

private ResourceExtractor(Context context) {
mContext = context.getApplicationContext();
private static String[] detectFilesToExtract() {
String language = LocaleUtils.getLanguage(Locale.getDefault());
// Currenty (Oct 2016), this array can be as big as 4 entries, so using a capacity
// that allows a bit of growth, but is still in the right ballpark..
ArrayList<String> activeLocalePakFiles = new ArrayList<String>(6);
for (String locale : BuildConfig.COMPRESSED_LOCALES) {
if (locale.startsWith(language)) {
activeLocalePakFiles.add(locale + ".pak");
}
}
return activeLocalePakFiles.toArray(new String[activeLocalePakFiles.size()]);
}

/**
Expand Down Expand Up @@ -329,6 +310,6 @@ private void deleteFiles() {
* Pak extraction not necessarily required by the embedder.
*/
private static boolean shouldSkipPakExtraction() {
return sResourcesToExtract.length == 0;
return get().mAssetsToExtract.length == 0;
}
}
17 changes: 17 additions & 0 deletions base/android/java/templates/BuildConfig.template
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,21 @@ public class BuildConfig {
public static final boolean DCHECK_IS_ON = false;
#endif

// Sorted list of locales that have a compressed .pak within assets.
// Stored as an array because AssetManager.list() is slow.
public static final String[] COMPRESSED_LOCALES =
#if defined(COMPRESSED_LOCALE_LIST)
COMPRESSED_LOCALE_LIST;
#else
{};
#endif

// Sorted list of locales that have an uncompressed .pak within assets.
// Stored as an array because AssetManager.list() is slow.
public static final String[] UNCOMPRESSED_LOCALES =
#if defined(UNCOMPRESSED_LOCALE_LIST)
UNCOMPRESSED_LOCALE_LIST;
#else
{};
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.chromium.base.CommandLineInitUtil;
import org.chromium.base.ContextUtils;
import org.chromium.base.PathUtils;
import org.chromium.base.ResourceExtractor;

/**
* The {@link Application} for the Blimp client.
Expand All @@ -29,7 +28,6 @@ protected void attachBaseContext(Context base) {
@Override
public void onCreate() {
super.onCreate();
ResourceExtractor.setResourcesToExtract(new ResourceExtractor.ResourceEntry[0]);
PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX);
initCommandLine();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import android.os.Handler;

import org.chromium.base.ContextUtils;
import org.chromium.base.ObserverList;
import org.chromium.base.ResourceExtractor;
import org.chromium.base.ThreadUtils;
Expand Down Expand Up @@ -81,7 +80,7 @@ public static void startAsync(final Callback callback) throws ProcessInitExcepti
if (sLoadAttempted) return;
sLoadAttempted = true;

ResourceExtractor extractor = ResourceExtractor.get(ContextUtils.getApplicationContext());
ResourceExtractor extractor = ResourceExtractor.get();
extractor.startExtractingResources();
LibraryLoader.get(LibraryProcessType.PROCESS_BROWSER).ensureInitialized();

Expand Down
Loading

0 comments on commit 732f0d8

Please sign in to comment.