Commit 8f6d1eb4 authored by Andrew Grieve's avatar Andrew Grieve Committed by Commit Bot

android_webview: Store licenses in .pak file rather than as an asset

This allows it to be brotli-compressed, saving 560kb for both
SystemWebview.apk and TrichromeWebview.apk.

The Monochrome Webview Stub APK continues to use licenses as an asset.

Moves WebViewApplication from glue/ -> apk/ and renames it to
"WebViewApkApplication" in order to not conflict with not-yet-updated
downstream glue/ code.

Bug: 734846
Change-Id: Icb0655057eae4ef80117f59cb060c1f596abf646
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1612660Reviewed-by: default avatarRichard Coles <torne@chromium.org>
Reviewed-by: default avatarDemetrios Papadopoulos <dpapad@chromium.org>
Reviewed-by: default avatarBo <boliu@chromium.org>
Commit-Queue: Andrew Grieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/master@{#660993}
parent ab18c7f3
......@@ -32,6 +32,7 @@ group("android_webview") {
generate_jni("native_jni") {
sources = [
"apk/java/src/com/android/webview/chromium/WebViewApkApplication.java",
"java/src/org/chromium/android_webview/AndroidProtocolHandler.java",
"java/src/org/chromium/android_webview/AwAutofillClient.java",
"java/src/org/chromium/android_webview/AwBrowserProcess.java",
......@@ -222,13 +223,7 @@ android_assets("monochrome_webview_assets") {
}
}
java_group("stub_assets") {
deps = [
":license_assets",
]
}
android_assets("license_assets") {
android_assets("stub_assets") {
renaming_sources = [ "$root_gen_dir/components/resources/about_credits.html" ]
renaming_destinations = [ "webview_licenses.notice" ]
deps = [
......@@ -275,13 +270,13 @@ grit("generate_webui_resources") {
# webui/resources has way too many resources. The whitelist is trim this down
# to a reasonable size
whitelist = rebase_path("ui/grit_resources_whitelist.txt", root_build_dir)
_whitelist = "ui/grit_resources_whitelist.txt"
inputs = [
"//android_webview/ui/grit_resources_whitelist.txt",
_whitelist,
]
grit_flags = [
"-w",
whitelist,
rebase_path(_whitelist, root_build_dir),
]
outputs = [
"grit/webui_resources.h",
......@@ -298,13 +293,20 @@ grit("generate_components_resources") {
source_is_generated = true
# See :generate_webui_resources for an explanation of the whitelist
whitelist = rebase_path("ui/grit_resources_whitelist.txt", root_build_dir)
_whitelist = "ui/grit_resources_whitelist.txt"
inputs = [
"//android_webview/ui/grit_resources_whitelist.txt",
_whitelist,
]
_about_credits_file_bro =
get_label_info("//components/resources", "target_gen_dir") +
"/about_credits.bro"
grit_flags = [
"-w",
whitelist,
rebase_path(_whitelist, root_build_dir),
"-E",
"about_credits_file=" +
rebase_path(_about_credits_file_bro, root_build_dir),
]
outputs = [
"grit/components_resources.h",
......@@ -312,7 +314,7 @@ grit("generate_components_resources") {
]
deps = [
"//components/resources:components_resources",
"//components/resources:compressed_about_credits",
]
}
......@@ -480,6 +482,7 @@ if (enable_resource_whitelist_generation) {
source_set("common") {
sources = [
"apk/webview_apk_application.cc",
"browser/android_protocol_handler.cc",
"browser/android_protocol_handler.h",
"browser/aw_autofill_client.cc",
......@@ -668,6 +671,8 @@ source_set("common") {
"common/aw_paths.h",
"common/aw_resource.cc",
"common/aw_resource.h",
"common/aw_resource_bundle.cc",
"common/aw_resource_bundle.h",
"common/aw_switches.cc",
"common/aw_switches.h",
"common/crash_reporter/aw_crash_reporter_client.cc",
......@@ -719,6 +724,7 @@ source_set("common") {
"//android_webview/utility",
"//base",
"//base/third_party/dynamic_annotations:dynamic_annotations",
"//components/about_ui",
"//components/autofill/android:provider",
"//components/autofill/content/browser",
"//components/autofill/content/common:mojo_interfaces",
......
......@@ -7,7 +7,9 @@ include_rules = [
# lib is the top-level target, and must remain a leaf in the dependency tree.
"-android_webview/lib",
"+components/about_ui",
"+components/content_capture",
"+components/embedder_support/android/java",
"+components/heap_profiling",
"+components/google/core",
"+components/network_session_configurator/common",
......
......@@ -4,15 +4,34 @@
import("//build/config/android/rules.gni")
# Since Monochrome has its own content provider, these two files are put
# in two different targets.
# Monochrome uses a different copy of LicenseContentProvider.java.
# This one is used in Trichrome and SystemWebview.
android_library("webview_license_provider_java") {
java_files = [ "//android_webview/apk/java/src/com/android/webview/chromium/LicenseContentProvider.java" ]
java_files =
[ "java/src/com/android/webview/chromium/LicenseContentProvider.java" ]
deps = [
":apk_java",
"//base:base_java",
"//components/about_ui/android:aboutui_java",
]
}
android_library("webview_license_activity_java") {
java_files = [ "//android_webview/apk/java/src/com/android/webview/chromium/LicenseActivity.java" ]
# TODO(agrieve): Delete once downstream reference is updated.
java_group("webview_license_activity_java") {
deps = [
":apk_java",
]
}
# Contains classes needed by the webview apk, but not used when loading the apk
# as a library.
android_library("apk_java") {
java_files = [
"java/src/com/android/webview/chromium/LicenseActivity.java",
"java/src/com/android/webview/chromium/WebViewApkApplication.java",
]
deps = [
"//base:base_java",
"//components/embedder_support/android:application_java",
]
}
......@@ -26,7 +26,7 @@
<application android:label="{{ application_label|default('Android System WebView') }}"
android:icon="@{{manifest_package|default('com.android.webview')}}:drawable/icon_webview"
android:name="{{ application_name|default('com.android.webview.chromium.WebViewApplication') }}"
android:name="{{ application_name|default('com.android.webview.chromium.WebViewApkApplication') }}"
android:multiArch="true"
android:use32bitAbi="true"
android:extractNativeLibs="{{ trichrome_library is not defined }}">
......
......@@ -12,9 +12,9 @@ import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import org.chromium.base.FileUtils;
import org.chromium.components.aboutui.CreditUtils;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
......@@ -23,10 +23,10 @@ import java.io.InputStream;
import java.io.OutputStream;
/**
* Content provider for the OSS licenses file.
* This is compiled into the stub WebView and so should not depend on any classes from Chromium.
* Content provider for about:credits.
* Used by SystemWebview and TrichromeWebview, but not Monochrome.
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class LicenseContentProvider
extends ContentProvider implements ContentProvider.PipeDataWriter<String> {
public static final String LICENSES_URI_SUFFIX = "LicenseContentProvider/webview_licenses";
......@@ -49,11 +49,17 @@ public class LicenseContentProvider
@Override
public void writeDataToPipe(
ParcelFileDescriptor output, Uri uri, String mimeType, Bundle opts, String filename) {
try (InputStream in = getContext().getAssets().open(filename);
OutputStream out = new FileOutputStream(output.getFileDescriptor());) {
FileUtils.copyStream(in, out);
} catch (IOException e) {
Log.e(TAG, "Failed to read the license file", e);
if (WebViewApkApplication.initializeNative()) {
CreditUtils.nativeWriteCreditsHtml(output.detachFd());
} else {
// Missing native library means we're the webview stub and licenses are stored as an
// asset.
try (InputStream in = getContext().getAssets().open(filename);
OutputStream out = new FileOutputStream(output.getFileDescriptor())) {
FileUtils.copyStream(in, out);
} catch (IOException ioException) {
throw new RuntimeException(ioException);
}
}
}
......@@ -85,4 +91,4 @@ public class LicenseContentProvider
String sortOrder) {
throw new UnsupportedOperationException();
}
}
\ No newline at end of file
}
// Copyright 2018 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.
package com.android.webview.chromium;
import android.app.Application;
import android.content.Context;
import org.chromium.base.ContextUtils;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.library_loader.LibraryLoader;
import org.chromium.components.embedder_support.application.FontPreloadingWorkaround;
/**
* Application subclass for SystemWebView and Trichrome.
*
* Application subclass is only used in renderer processes and in the WebView APK's own services.
* None of this code runs in an application which simply uses WebView.
*/
@JNINamespace("android_webview")
public class WebViewApkApplication extends Application {
// Called by the framework for ALL processes. Runs before ContentProviders are created.
// Quirk: context.getApplicationContext() returns null during this method.
@Override
protected void attachBaseContext(Context context) {
super.attachBaseContext(context);
ContextUtils.initApplicationContext(this);
}
@Override
public void onCreate() {
super.onCreate();
FontPreloadingWorkaround.maybeInstallWorkaround(this);
}
/**
* Performs minimal native library initialization required when running as a stand-alone APK.
* @return True if the library was loaded, false if running as webview stub.
*/
static synchronized boolean initializeNative() {
try {
if (LibraryLoader.getInstance().isInitialized()) {
return true;
}
LibraryLoader.getInstance().loadNow();
} catch (Throwable unused) {
// Happens for WebView Stub. Throws NoClassDefFoundError because of no
// NativeLibraries.java being generated.
return false;
}
LibraryLoader.getInstance().switchCommandLineForWebView();
nativeInitializePakResources();
return true;
}
private static native void nativeInitializePakResources();
}
// 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.
#include "jni/WebViewApkApplication_jni.h"
#include "android_webview/common/aw_resource_bundle.h"
#include "base/android/base_jni_onload.h"
#include "base/logging.h"
namespace android_webview {
void JNI_WebViewApkApplication_InitializePakResources(JNIEnv* env) {
CHECK(base::android::OnJNIOnLoadInit());
InitIcuAndResourceBundleBrowserSide();
}
} // namespace android_webview
// 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.
#include "android_webview/common/aw_resource_bundle.h"
#include "android_webview/common/aw_descriptors.h"
#include "base/android/locale_utils.h"
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/files/memory_mapped_file.h"
#include "base/i18n/rtl.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/posix/global_descriptors.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/resource/resource_bundle_android.h"
#include "ui/base/ui_base_paths.h"
namespace android_webview {
void InitIcuAndResourceBundleBrowserSide() {
ui::SetLocalePaksStoredInApk(true);
std::string locale = ui::ResourceBundle::InitSharedInstanceWithLocale(
base::android::GetDefaultLocaleString(), NULL,
ui::ResourceBundle::LOAD_COMMON_RESOURCES);
if (locale.empty()) {
LOG(WARNING) << "Failed to load locale .pak from apk.";
}
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);
}
void InitResourceBundleRendererSide() {
auto* global_descriptors = base::GlobalDescriptors::GetInstance();
int pak_fd = global_descriptors->Get(kAndroidWebViewLocalePakDescriptor);
base::MemoryMappedFile::Region pak_region =
global_descriptors->GetRegion(kAndroidWebViewLocalePakDescriptor);
ui::ResourceBundle::InitSharedInstanceWithPakFileRegion(base::File(pak_fd),
pak_region);
std::pair<int, ui::ScaleFactor> extra_paks[] = {
{kAndroidWebViewMainPakDescriptor, ui::SCALE_FACTOR_NONE},
{kAndroidWebView100PercentPakDescriptor, 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, pak_info.second);
}
}
} // namespace android_webview
// 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.
#ifndef ANDROID_WEBVIEW_COMMON_AW_RESOURCE_BUNDLE_H_
#define ANDROID_WEBVIEW_COMMON_AW_RESOURCE_BUNDLE_H_
namespace android_webview {
void InitIcuAndResourceBundleBrowserSide();
void InitResourceBundleRendererSide();
} // namespace android_webview
#endif // ANDROID_WEBVIEW_COMMON_AW_RESOURCE_BUNDLE_H_
......@@ -53,7 +53,6 @@ android_library("glue") {
"java/src/com/android/webview/chromium/WebResourceRequestAdapter.java",
"java/src/com/android/webview/chromium/WebStorageAdapter.java",
"java/src/com/android/webview/chromium/WebkitToSharedGlueConverter.java",
"java/src/com/android/webview/chromium/WebViewApplication.java",
"java/src/com/android/webview/chromium/WebViewChromiumAwInit.java",
"java/src/com/android/webview/chromium/WebViewChromium.java",
"java/src/com/android/webview/chromium/WebViewChromiumFactoryProvider.java",
......
......@@ -14,7 +14,6 @@ glue_library_deps = [
"//base:base_java",
"//components/autofill/android:autofill_java",
"//components/autofill/android:provider_java",
"//components/embedder_support/android:application_java",
"//content/public/android:content_java",
"//net/android:net_java",
"//ui/android:ui_java",
......
......@@ -2,7 +2,6 @@ include_rules = [
"+android_webview/java",
"+base/android/java",
"+components/autofill/android/java",
"+components/embedder_support/android/java",
"-content/public/android/java",
"+content/public/android/java/src/org/chromium/content_public",
......
// Copyright 2018 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.
package com.android.webview.chromium;
import android.app.Application;
import org.chromium.components.embedder_support.application.FontPreloadingWorkaround;
/**
* Application subclass for standalone WebView, to allow installing the font preloading workaround
* in renderer processes before the problematic code in ActivityThread runs.
*
* DO NOT ADD ANYTHING ELSE HERE! WebView's application subclass is only used in renderer processes
* and in the WebView APK's own services. None of this code runs in an application which simply uses
* WebView. This only exists because this is the only point where the font preloading workaround can
* be installed early enough to be effective.
*/
public class WebViewApplication extends Application {
@Override
public void onCreate() {
// Don't add anything else here! See above.
super.onCreate();
FontPreloadingWorkaround.maybeInstallWorkaround(this);
}
}
......@@ -14,6 +14,7 @@
#include "android_webview/browser/tracing/aw_trace_event_args_whitelist.h"
#include "android_webview/common/aw_descriptors.h"
#include "android_webview/common/aw_paths.h"
#include "android_webview/common/aw_resource_bundle.h"
#include "android_webview/common/aw_switches.h"
#include "android_webview/common/crash_reporter/aw_crash_reporter_client.h"
#include "android_webview/common/crash_reporter/crash_keys.h"
......@@ -22,7 +23,6 @@
#include "android_webview/utility/aw_content_utility_client.h"
#include "base/android/apk_assets.h"
#include "base/android/build_info.h"
#include "base/android/locale_utils.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/cpu.h"
......@@ -30,7 +30,7 @@
#include "base/i18n/rtl.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/posix/global_descriptors.h"
#include "base/strings/string_number_conversions.h"
#include "base/threading/thread_restrictions.h"
#include "cc/base/switches.h"
......@@ -54,8 +54,6 @@
#include "gpu/ipc/gl_in_process_context.h"
#include "media/base/media_switches.h"
#include "media/media_buildflags.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/resource/resource_bundle_android.h"
#include "ui/base/ui_base_paths.h"
#include "ui/base/ui_base_switches.h"
#include "ui/events/gesture_detection/gesture_configuration.h"
......@@ -248,23 +246,7 @@ void AwMainDelegate::PreSandboxStartup() {
}
if (process_type == switches::kRendererProcess) {
auto* global_descriptors = base::GlobalDescriptors::GetInstance();
int pak_fd = global_descriptors->Get(kAndroidWebViewLocalePakDescriptor);
base::MemoryMappedFile::Region pak_region =
global_descriptors->GetRegion(kAndroidWebViewLocalePakDescriptor);
ui::ResourceBundle::InitSharedInstanceWithPakFileRegion(base::File(pak_fd),
pak_region);
std::pair<int, ui::ScaleFactor> extra_paks[] = {
{kAndroidWebViewMainPakDescriptor, ui::SCALE_FACTOR_NONE},
{kAndroidWebView100PercentPakDescriptor, 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, pak_info.second);
}
InitResourceBundleRendererSide();
}
EnableCrashReporter(process_type);
......@@ -317,23 +299,7 @@ bool AwMainDelegate::ShouldCreateFeatureList() {
// This function is called only on the browser process.
void AwMainDelegate::PostEarlyInitialization(bool is_running_tests) {
ui::SetLocalePaksStoredInApk(true);
std::string locale = ui::ResourceBundle::InitSharedInstanceWithLocale(
base::android::GetDefaultLocaleString(), NULL,
ui::ResourceBundle::LOAD_COMMON_RESOURCES);
if (locale.empty()) {
LOG(WARNING) << "Failed to load locale .pak from the apk. "
"Bringing up WebView without any locale";
}
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);
InitIcuAndResourceBundleBrowserSide();
aw_feature_list_creator_->CreateFeatureListAndFieldTrials();
}
......
......@@ -23,10 +23,9 @@ template("system_webview_apk_tmpl") {
forward_variables_from(invoker, "*")
deps += [
"//android_webview:license_assets",
"//android_webview:locale_pak_assets",
"//android_webview:pak_file_assets",
"//android_webview/apk:webview_license_activity_java",
"//android_webview/apk:apk_java",
"//android_webview/apk:webview_license_provider_java",
"//android_webview/glue",
"//android_webview/support_library:support_lib_glue_java",
......
IDR_ABOUT_UI_CREDITS_HTML
IDR_WEBUI_JS_LOAD_TIME_DATA
IDR_WEBUI_JSTEMPLATE_JS
IDR_SECURITY_INTERSTITIAL_HTML
......
......@@ -336,7 +336,7 @@ template("monochrome_public_common_apk_or_module_tmpl") {
_deps += [
"//android_webview:monochrome_webview_assets",
"//android_webview/apk:webview_license_activity_java",
"//android_webview/apk:apk_java",
"//android_webview/glue",
"//chrome/android:monochrome_java",
"//chrome/android/monochrome:monochrome_license_provider_java",
......
......@@ -12,7 +12,6 @@ import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import org.chromium.base.ThreadUtils;
import org.chromium.base.library_loader.ProcessInitException;
......@@ -20,19 +19,15 @@ import org.chromium.chrome.browser.init.ChromeBrowserInitializer;
import org.chromium.components.aboutui.CreditUtils;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* Content provider for the OSS licenses file.
* Content provider for the OSS licenses file used on Monochrome and Trichrome.
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
@TargetApi(Build.VERSION_CODES.N)
public class LicenseContentProvider
extends ContentProvider implements ContentProvider.PipeDataWriter<String> {
public static final String LICENSES_URI_SUFFIX = "LicenseContentProvider/webview_licenses";
public static final String LICENSES_CONTENT_TYPE = "text/html";
private static final String TAG = "LicenseCP";
@Override
public boolean onCreate() {
......@@ -50,22 +45,17 @@ public class LicenseContentProvider
@Override
public void writeDataToPipe(
ParcelFileDescriptor output, Uri uri, String mimeType, Bundle opts, String filename) {
try (OutputStream out = new FileOutputStream(output.getFileDescriptor());) {
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
try {
ChromeBrowserInitializer.getInstance(getContext())
.handleSynchronousStartup();
} catch (ProcessInitException e) {
Log.e(TAG, "Fail to initialize the Chrome Browser.", e);
}
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
try {
ChromeBrowserInitializer.getInstance(getContext()).handleSynchronousStartup();
} catch (ProcessInitException e) {
throw new RuntimeException(e);
}
});
out.write(CreditUtils.nativeGetJavaWrapperCredits());
} catch (IOException e) {
Log.e(TAG, "Failed to write the license file", e);
}
}
});
CreditUtils.nativeWriteCreditsHtml(output.detachFd());
}
@Override
......
file://ui/webui/PLATFORM_OWNERS
agrieve@chromium.org
# COMPONENT: UI>Browser>WebUI
......@@ -11,6 +11,6 @@ import org.chromium.base.annotations.JNINamespace;
public class CreditUtils {
private CreditUtils() {}
/** Returns a string containing the content of about_credits.html. */
public static native byte[] nativeGetJavaWrapperCredits();
/** Writes the chrome://credits HTML to the given descriptor. */
public static native void nativeWriteCreditsHtml(int fd);
}
......@@ -12,7 +12,7 @@
#include "ui/base/resource/resource_bundle.h"
#if defined(OS_ANDROID)
#include "base/android/jni_array.h"
#include "base/files/file.h"
#include "jni/CreditUtils_jni.h"
#endif
......@@ -52,13 +52,10 @@ std::string GetCredits(bool include_scripts) {
}
#if defined(OS_ANDROID)
static base::android::ScopedJavaLocalRef<jbyteArray>
JNI_CreditUtils_GetJavaWrapperCredits(JNIEnv* env) {
static void JNI_CreditUtils_WriteCreditsHtml(JNIEnv* env, jint fd) {
std::string html_content = GetCredits(false);
const char* html_content_arr = html_content.c_str();
return base::android::ToJavaByteArray(
env, reinterpret_cast<const uint8_t*>(html_content_arr),
html_content.size());
base::File out_file(fd);
out_file.WriteAtCurrentPos(html_content.c_str(), html_content.size());
}
#endif
......
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