Commit cf1e154f authored by Clark DuVall's avatar Clark DuVall Committed by Commit Bot

[WebLayer] Load pak files correctly on Android

Loading pak files was not quite working right in weblayer, since we were
not loading locale paks. This caused text on e.g. submit buttons to not
show up in some configurations like loading from monochrome.

This change makes weblayer load pak assets similar to webview, and
uses the webview pak deps for the weblayer support APK, since those
should be sufficient for weblayer.

As an added bonus this also allows l10n to work, so switching the
default language of the device will show buttons and labels in that
language.

Change-Id: I1a2b47009e7bb74f1ea3c16dc6f52451e2b78066
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1865015Reviewed-by: default avatarJohn Abd-El-Malek <jam@chromium.org>
Reviewed-by: default avatarRichard Coles <torne@chromium.org>
Commit-Queue: Clark DuVall <cduvall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#706978}
parent 2786e314
......@@ -13,7 +13,7 @@ bool NativeInit(base::android::LibraryProcessType library_process_type) {
switch (library_process_type) {
case base::android::PROCESS_WEBLAYER:
case base::android::PROCESS_WEBLAYER_CHILD:
return weblayer::OnJNIOnLoadInit("resources.pak");
return weblayer::OnJNIOnLoadInit();
break;
default:
return android_webview::OnJNIOnLoadInit();
......
......@@ -10,6 +10,7 @@ import("//build/config/locales.gni")
import("//build/util/version.gni")
import("//chrome/android/trichrome.gni")
import("//tools/v8_context_snapshot/v8_context_snapshot.gni")
import("//weblayer/variables.gni")
declare_args() {
# Android package name to use when compiling the system_webview_apk and
......@@ -34,7 +35,10 @@ template("system_webview_apk_tmpl") {
]
target_sdk_version = android_sdk_version
locale_config_java_packages = [ webview_locale_config_java_package ]
locale_config_java_packages = [
webview_locale_config_java_package,
weblayer_locale_config_java_package,
]
if (!defined(alternative_android_sdk_dep)) {
alternative_android_sdk_dep = webview_framework_dep
......
......@@ -14,6 +14,7 @@ import("//chrome/android/chrome_common_shared_library.gni")
import("//chrome/android/features/dev_ui/dev_ui_module.gni")
import("//chrome/common/features.gni")
import("//device/vr/buildflags/buildflags.gni")
import("//weblayer/variables.gni")
import("channel.gni")
declare_args() {
......@@ -344,6 +345,7 @@ template("monochrome_public_common_apk_or_module_tmpl") {
locale_config_java_packages = [
"org.chromium.chrome.browser",
webview_locale_config_java_package,
weblayer_locale_config_java_package,
]
if (android_64bit_target_cpu) {
# Build //android_webview:monochrome with the opposite bitness that
......
......@@ -23,7 +23,7 @@ bool NativeInit(base::android::LibraryProcessType library_process_type) {
break;
case base::android::PROCESS_WEBLAYER:
case base::android::PROCESS_WEBLAYER_CHILD:
return weblayer::OnJNIOnLoadInit("resources.pak");
return weblayer::OnJNIOnLoadInit();
break;
default:
NOTREACHED();
......
......@@ -7,6 +7,7 @@
#include <iostream>
#include "base/base_switches.h"
#include "base/command_line.h"
#include "base/cpu.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
......@@ -22,8 +23,12 @@
#if defined(OS_ANDROID)
#include "base/android/apk_assets.h"
#include "base/android/locale_utils.h"
#include "base/i18n/rtl.h"
#include "base/posix/global_descriptors.h"
#include "content/public/browser/android/compositor.h"
#include "ui/base/resource/resource_bundle_android.h"
#include "ui/base/ui_base_switches.h"
#include "weblayer/browser/android_descriptors.h"
#endif
......@@ -118,34 +123,49 @@ int ContentMainDelegateImpl::RunProcess(
void ContentMainDelegateImpl::InitializeResourceBundle() {
#if defined(OS_ANDROID)
// On Android, the renderer runs with a different UID and can never access
// the file system. Use the file descriptor passed in at launch time.
auto* global_descriptors = base::GlobalDescriptors::GetInstance();
int pak_fd = global_descriptors->MaybeGet(kPakDescriptor);
base::MemoryMappedFile::Region pak_region;
if (pak_fd >= 0) {
pak_region = global_descriptors->GetRegion(kPakDescriptor);
} else {
pak_fd = base::android::OpenApkAsset(
std::string("assets/") + params_.pak_name, &pak_region);
if (pak_fd < 0) {
base::FilePath pak_file;
bool r = base::PathService::Get(base::DIR_ANDROID_APP_DATA, &pak_file);
DCHECK(r);
pak_file = pak_file.Append(FILE_PATH_LITERAL("paks"));
pak_file = pak_file.AppendASCII(params_.pak_name);
int flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
pak_fd = base::File(pak_file, flags).TakePlatformFile();
pak_region = base::MemoryMappedFile::Region::kWholeFile;
}
global_descriptors->Set(kPakDescriptor, pak_fd, pak_region);
const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess();
bool is_browser_process =
command_line.GetSwitchValueASCII(switches::kProcessType).empty();
if (is_browser_process) {
ui::SetLocalePaksStoredInApk(true);
std::string locale = ui::ResourceBundle::InitSharedInstanceWithLocale(
base::android::GetDefaultLocaleString(), nullptr,
ui::ResourceBundle::LOAD_COMMON_RESOURCES);
if (locale.empty()) {
LOG(WARNING) << "Failed to load locale .pak from apk.";
}
DCHECK_GE(pak_fd, 0);
// This is clearly wrong. See crbug.com/330930
base::i18n::SetICUDefaultLocale(locale);
// Try to directly mmap the resources.pak from the apk. Fall back to load
// from file, using PATH_SERVICE, otherwise.
base::FilePath pak_file_path;
base::PathService::Get(ui::DIR_RESOURCE_PAKS_ANDROID, &pak_file_path);
pak_file_path = pak_file_path.AppendASCII("resources.pak");
ui::LoadMainAndroidPackFile("assets/resources.pak", pak_file_path);
} else {
base::i18n::SetICUDefaultLocale(
command_line.GetSwitchValueASCII(switches::kLang));
auto* global_descriptors = base::GlobalDescriptors::GetInstance();
int pak_fd = global_descriptors->Get(kWebLayerLocalePakDescriptor);
base::MemoryMappedFile::Region pak_region =
global_descriptors->GetRegion(kWebLayerLocalePakDescriptor);
ui::ResourceBundle::InitSharedInstanceWithPakFileRegion(base::File(pak_fd),
pak_region);
std::pair<int, ui::ScaleFactor> extra_paks[] = {
{kWebLayerMainPakDescriptor, ui::SCALE_FACTOR_NONE},
{kWebLayer100PercentPakDescriptor, ui::SCALE_FACTOR_100P}};
for (const auto& pak_info : extra_paks) {
pak_fd = global_descriptors->Get(pak_info.first);
pak_region = global_descriptors->GetRegion(pak_info.first);
ui::ResourceBundle::GetSharedInstance().AddDataPackFromFileRegion(
base::File(pak_fd), pak_region, ui::SCALE_FACTOR_100P);
base::File(pak_fd), pak_region, pak_info.second);
}
}
#else
base::FilePath pak_file;
bool r = base::PathService::Get(base::DIR_ASSETS, &pak_file);
......
......@@ -9,7 +9,7 @@
namespace {
bool NativeInit(base::android::LibraryProcessType) {
return weblayer::OnJNIOnLoadInit("weblayer_support.pak");
return weblayer::OnJNIOnLoadInit();
}
} // namespace
......
......@@ -18,12 +18,11 @@ class MainDelegateImpl : public MainDelegate {
};
// This is called by the VM when the shared library is first loaded.
bool OnJNIOnLoadInit(const std::string& pak_name) {
bool OnJNIOnLoadInit() {
if (!content::android::OnJNIOnLoadInit())
return false;
weblayer::MainParams params;
params.delegate = new weblayer::MainDelegateImpl;
params.pak_name = pak_name;
params.brand = "WebLayer";
base::android::SetVersionNumber(PRODUCT_VERSION);
......
......@@ -9,7 +9,7 @@
namespace weblayer {
bool OnJNIOnLoadInit(const std::string& pak_name);
bool OnJNIOnLoadInit();
} // namespace weblayer
......
......@@ -12,7 +12,9 @@ namespace weblayer {
// This is a list of global descriptor keys to be used with the
// base::GlobalDescriptors object (see base/posix/global_descriptors.h)
enum {
kPakDescriptor = kContentIPCDescriptorMax + 1,
kWebLayerLocalePakDescriptor = kContentIPCDescriptorMax + 1,
kWebLayerMainPakDescriptor,
kWebLayer100PercentPakDescriptor,
};
} // namespace weblayer
......
......@@ -31,8 +31,8 @@
#include "weblayer/public/main.h"
#if defined(OS_ANDROID)
#include "base/android/apk_assets.h"
#include "base/android/path_utils.h"
#include "ui/base/resource/resource_bundle_android.h"
#include "weblayer/browser/android_descriptors.h"
#endif
......@@ -141,10 +141,15 @@ void ContentBrowserClientImpl::GetAdditionalMappedFilesForChildProcess(
int child_process_id,
content::PosixFileDescriptorInfo* mappings) {
#if defined(OS_ANDROID)
mappings->ShareWithRegion(
kPakDescriptor,
base::GlobalDescriptors::GetInstance()->Get(kPakDescriptor),
base::GlobalDescriptors::GetInstance()->GetRegion(kPakDescriptor));
base::MemoryMappedFile::Region region;
int fd = ui::GetMainAndroidPackFd(&region);
mappings->ShareWithRegion(kWebLayerMainPakDescriptor, fd, region);
fd = ui::GetCommonResourcesPackFd(&region);
mappings->ShareWithRegion(kWebLayer100PercentPakDescriptor, fd, region);
fd = ui::GetLocalePackFd(&region);
mappings->ShareWithRegion(kWebLayerLocalePakDescriptor, fd, region);
#endif
}
#endif // defined(OS_LINUX) || defined(OS_ANDROID)
......
......@@ -4,12 +4,17 @@
import("//build/config/android/config.gni")
import("//build/config/android/rules.gni")
import("//weblayer/variables.gni")
android_resources("weblayer_resources") {
resource_dirs = []
custom_package = "org.chromium.weblayer_private"
}
generate_locale_config_srcjar("weblayer_locale_config") {
java_package = weblayer_locale_config_java_package
}
android_library("java") {
java_files = [
"org/chromium/weblayer_private/BrowserControllerImpl.java",
......@@ -37,6 +42,8 @@ android_library("java") {
"//content/public/android:content_java",
"//ui/android:ui_java",
]
srcjar_deps = [ ":weblayer_locale_config" ]
jar_excluded_patterns = [ "*/LocaleConfig.class" ]
annotation_processor_deps = [ "//base/android/jni_generator:jni_processor" ]
}
......
......@@ -60,7 +60,7 @@ public final class WebLayerImpl extends IWebLayer.Stub {
Context context = ObjectWrapper.unwrap(webLayerContextWrapper, Context.class);
ContextUtils.initApplicationContext(context);
ResourceBundle.setNoAvailableLocalePaks();
ResourceBundle.setAvailablePakLocales(new String[] {}, LocaleConfig.UNCOMPRESSED_LOCALES);
PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX);
ChildProcessCreationParams.set(context.getPackageName(), false /* isExternalService */,
......
......@@ -7,6 +7,7 @@ import("//build/config/android/config.gni")
import("//build/config/android/rules.gni")
import("//third_party/icu/config.gni")
import("//tools/v8_context_snapshot/v8_context_snapshot.gni")
import("//weblayer/variables.gni")
android_assets("weblayer_shell_assets") {
testonly = true
......@@ -206,30 +207,14 @@ jinja_template("weblayer_support_manifest") {
output = weblayer_support_manifest
}
android_assets("weblayer_support_assets") {
testonly = true
sources = [
"$root_out_dir/weblayer_support.pak",
]
disable_compression = true
deps = [
"//third_party/icu:icu_assets",
"//weblayer/shell:support_pak",
]
if (use_v8_context_snapshot) {
deps += [ "//tools/v8_context_snapshot:v8_context_snapshot_assets" ]
} else {
deps += [ "//v8:v8_external_startup_data_assets" ]
}
}
android_apk("weblayer_support_apk") {
testonly = true
deps = [
":weblayer_support_assets",
":weblayer_support_manifest",
"//android_webview:locale_pak_assets",
"//android_webview:monochrome_webview_assets",
"//android_webview:pak_file_assets",
"//base:base_java",
"//weblayer/browser/java",
]
......@@ -249,6 +234,8 @@ android_apk("weblayer_support_apk") {
android_manifest_dep = ":weblayer_support_manifest"
shared_resources = true
locale_config_java_packages = [ weblayer_locale_config_java_package ]
native_lib_version_rule = "//build/util:chrome_version_json"
_native_lib_file =
rebase_path("$root_gen_dir/CHROME_VERSION.json", root_build_dir)
......
# Copyright 2019 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
weblayer_locale_config_java_package = "org.chromium.weblayer_private"
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