Commit 92e8223d authored by David 'Digit' Turner's avatar David 'Digit' Turner Committed by Commit Bot

android: store compressed locales under assets/locales/

This patch simply moves the location where the compressed
locale .pak files are stored within the final Chrome and
Monochrome APKs, from assets/<locale>.pak to
assets/locales/<locale>.pak

The reason to do this is to allow language-dependent
splits in the future, which will require apkbuilder.py
to support an option to place the files under a
directory like assets/locales#<lang>/<locale>.pak,
where <lang> is the language code of each corresponding
locale.

Note that these files are never accessed directly from
Chromium, instead they are extracted at startup time
if needed to the application's private data directory,
and the uncompressed file there is what Chrome will
later memory-map from native code.

In detail:

- Modify chrome/android/BUILD.gn to ensure that the
  compressed locale pak files are placed under
  assets/locales/

- Modify write_build_config.py to not use the directory
  of locale pak files when generating the Java locale
  lists for COMPRESSED_LOCALES and UNCOMPRESSED_LOCALES.

- Modify ResourceBundle.java to deal with the new list
  format when looking for uncompressed locales.

- Modify ResourceExtractor.java to deal with the new
  list format and the new locations.

BUG=820459
R=agrieve@chromium.org,dtrainor@chromium.org,tedchoc@chromium.org

Change-Id: Ia0caef3514a1ccfa72fe0ab1ff6fc1bf418377a6
Reviewed-on: https://chromium-review.googlesource.com/1010352
Commit-Queue: David Turner <digit@chromium.org>
Reviewed-by: default avataragrieve <agrieve@chromium.org>
Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Cr-Commit-Position: refs/heads/master@{#551332}
parent 02dcc390
...@@ -53,6 +53,7 @@ public class ResourceExtractor { ...@@ -53,6 +53,7 @@ public class ResourceExtractor {
private static final String V8_SNAPSHOT_DATA_FILENAME = "snapshot_blob.bin"; private static final String V8_SNAPSHOT_DATA_FILENAME = "snapshot_blob.bin";
private static final String FALLBACK_LOCALE = "en-US"; private static final String FALLBACK_LOCALE = "en-US";
private static final String LIBRARY_DIR = "native_libraries"; private static final String LIBRARY_DIR = "native_libraries";
private static final String COMPRESSED_LOCALES_DIR = "locales";
private static final int BUFFER_SIZE = 16 * 1024; private static final int BUFFER_SIZE = 16 * 1024;
private class ExtractTask extends AsyncTask<Void, Void, Void> { private class ExtractTask extends AsyncTask<Void, Void, Void> {
...@@ -85,10 +86,11 @@ public class ResourceExtractor { ...@@ -85,10 +86,11 @@ public class ResourceExtractor {
AssetManager assetManager = ContextUtils.getApplicationAssets(); AssetManager assetManager = ContextUtils.getApplicationAssets();
byte[] buffer = new byte[BUFFER_SIZE]; byte[] buffer = new byte[BUFFER_SIZE];
for (String assetName : mAssetsToExtract) { for (String assetPath : mAssetsToExtract) {
String assetName = assetPath.substring(assetPath.lastIndexOf('/') + 1);
File output = new File(outputDir, assetName + extractSuffix); File output = new File(outputDir, assetName + extractSuffix);
TraceEvent.begin("ExtractResource"); TraceEvent.begin("ExtractResource");
try (InputStream inputStream = assetManager.open(assetName)) { try (InputStream inputStream = assetManager.open(assetPath)) {
extractResourceHelper(inputStream, output, buffer); extractResourceHelper(inputStream, output, buffer);
} catch (IOException e) { } catch (IOException e) {
// The app would just crash later if files are missing. // The app would just crash later if files are missing.
...@@ -205,21 +207,26 @@ public class ResourceExtractor { ...@@ -205,21 +207,26 @@ public class ResourceExtractor {
private static String[] detectFilesToExtract() { private static String[] detectFilesToExtract() {
Locale defaultLocale = Locale.getDefault(); Locale defaultLocale = Locale.getDefault();
String language = LocaleUtils.getUpdatedLanguageForChromium(defaultLocale.getLanguage()); String language = LocaleUtils.getUpdatedLanguageForChromium(defaultLocale.getLanguage());
// Currenty (Oct 2016), this array can be as big as 4 entries, so using a capacity // Currenty (Apr 2018), this array can be as big as 6 entries, so using a capacity
// that allows a bit of growth, but is still in the right ballpark.. // that allows a bit of growth, but is still in the right ballpark..
ArrayList<String> activeLocalePakFiles = new ArrayList<String>(6); ArrayList<String> activeLocales = new ArrayList<String>(6);
for (String locale : BuildConfig.COMPRESSED_LOCALES) { for (String locale : BuildConfig.COMPRESSED_LOCALES) {
if (locale.startsWith(language)) { if (locale.startsWith(language)) {
activeLocalePakFiles.add(locale + ".pak"); activeLocales.add(locale);
} }
} }
if (activeLocalePakFiles.isEmpty() && BuildConfig.COMPRESSED_LOCALES.length > 0) { if (activeLocales.isEmpty() && BuildConfig.COMPRESSED_LOCALES.length > 0) {
assert Arrays.asList(BuildConfig.COMPRESSED_LOCALES).contains(FALLBACK_LOCALE); assert Arrays.asList(BuildConfig.COMPRESSED_LOCALES).contains(FALLBACK_LOCALE);
activeLocalePakFiles.add(FALLBACK_LOCALE + ".pak"); activeLocales.add(FALLBACK_LOCALE);
}
String[] localePakFiles = new String[activeLocales.size()];
for (int n = 0; n < activeLocales.size(); ++n) {
localePakFiles[n] = COMPRESSED_LOCALES_DIR + '/' + activeLocales.get(n) + ".pak";
} }
Log.i(TAG, "Android Locale: %s requires .pak files: %s", defaultLocale, Log.i(TAG, "Android Locale: %s requires .pak files: %s", defaultLocale,
activeLocalePakFiles); Arrays.toString(activeLocales.toArray()));
return activeLocalePakFiles.toArray(new String[activeLocalePakFiles.size()]);
return localePakFiles;
} }
/** /**
......
...@@ -716,11 +716,22 @@ def _CreateJavaLibrariesList(library_paths): ...@@ -716,11 +716,22 @@ def _CreateJavaLibrariesList(library_paths):
return ('{%s}' % ','.join(['"%s"' % s[3:-3] for s in library_paths])) return ('{%s}' % ','.join(['"%s"' % s[3:-3] for s in library_paths]))
def _CreateLocalePaksAssetJavaList(assets, locale_paks): def _CreateJavaLocaleListFromAssets(assets, locale_paks):
"""Returns a java literal array from a list of assets in the form src:dst.""" """Returns a java literal array from a list of locale assets.
asset_paths = [a.split(':')[1] for a in assets]
return '{%s}' % ','.join( Args:
sorted('"%s"' % a[:-4] for a in asset_paths if a in locale_paks)) assets: A list of all APK asset paths in the form 'src:dst'
locale_paks: A list of asset paths that correponds to the locale pak
files of interest. Each |assets| entry will have its 'dst' part matched
against it to determine if they are part of the result.
Returns:
A string that is a Java source literal array listing the locale names
of the corresponding asset files, without directory or .pak suffix.
E.g. '{"en-GB", "en-US", "es-ES", "fr", ... }'
"""
assets_paths = [a.split(':')[1] for a in assets]
locales = [os.path.basename(a)[:-4] for a in assets_paths if a in locale_paks]
return '{%s}' % ','.join(['"%s"' % l for l in sorted(locales)])
def main(argv): def main(argv):
...@@ -1269,9 +1280,9 @@ def main(argv): ...@@ -1269,9 +1280,9 @@ def main(argv):
} }
config['assets'], config['uncompressed_assets'], locale_paks = ( config['assets'], config['uncompressed_assets'], locale_paks = (
_MergeAssets(deps.All('android_assets'))) _MergeAssets(deps.All('android_assets')))
config['compressed_locales_java_list'] = _CreateLocalePaksAssetJavaList( config['compressed_locales_java_list'] = _CreateJavaLocaleListFromAssets(
config['assets'], locale_paks) config['assets'], locale_paks)
config['uncompressed_locales_java_list'] = _CreateLocalePaksAssetJavaList( config['uncompressed_locales_java_list'] = _CreateJavaLocaleListFromAssets(
config['uncompressed_assets'], locale_paks) config['uncompressed_assets'], locale_paks)
config['extra_android_manifests'] = filter(None, ( config['extra_android_manifests'] = filter(None, (
......
...@@ -771,9 +771,11 @@ android_assets("chrome_public_pak_assets") { ...@@ -771,9 +771,11 @@ android_assets("chrome_public_pak_assets") {
# This target is separate from chrome_public_pak_assets because it does not # This target is separate from chrome_public_pak_assets because it does not
# disable compression. # disable compression.
android_assets("chrome_public_locale_pak_assets") { android_assets("chrome_public_locale_pak_assets") {
sources = [] renaming_sources = []
renaming_destinations = []
foreach(_locale, locales - android_chrome_omitted_locales) { foreach(_locale, locales - android_chrome_omitted_locales) {
sources += [ "$root_out_dir/locales/$_locale.pak" ] renaming_sources += [ "$root_out_dir/locales/$_locale.pak" ]
renaming_destinations += [ "locales/$_locale.pak" ]
} }
treat_as_locale_paks = true treat_as_locale_paks = true
...@@ -981,9 +983,12 @@ if (current_toolchain == default_toolchain) { ...@@ -981,9 +983,12 @@ if (current_toolchain == default_toolchain) {
# This target is separate from monochrome_pak_assets because it does not # This target is separate from monochrome_pak_assets because it does not
# disable compression. # disable compression.
android_assets("monochrome_locale_pak_assets") { android_assets("monochrome_locale_pak_assets") {
sources = [] renaming_sources = []
renaming_destinations = []
foreach(_locale, locales - android_chrome_omitted_locales) { foreach(_locale, locales - android_chrome_omitted_locales) {
sources += [ "$target_gen_dir/monochrome_paks/locales/$_locale.pak" ] renaming_sources +=
[ "$target_gen_dir/monochrome_paks/locales/$_locale.pak" ]
renaming_destinations += [ "locales/$_locale.pak" ]
} }
treat_as_locale_paks = true treat_as_locale_paks = true
......
...@@ -20,8 +20,7 @@ final class ResourceBundle { ...@@ -20,8 +20,7 @@ final class ResourceBundle {
@CalledByNative @CalledByNative
private static String getLocalePakResourcePath(String locale) { private static String getLocalePakResourcePath(String locale) {
if (Arrays.binarySearch(BuildConfig.UNCOMPRESSED_LOCALES, if (Arrays.binarySearch(BuildConfig.UNCOMPRESSED_LOCALES, locale) >= 0) {
"stored-locales/" + locale) >= 0) {
return "assets/stored-locales/" + locale + ".pak"; return "assets/stored-locales/" + locale + ".pak";
} }
return null; return null;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment