Apply FontPreloadingWorkaround to ChromeApplication

Move the FontPreloadingWorkaround from WebViewApplication to
ChromeApplication. Used to workaround an Android bug where
preloaded fonts are attempted to be loaded in an isolated process.

BUG=920692

Change-Id: Ib1e646f94011e935ba4ec9e3b55c708767a14a63
Reviewed-on: https://chromium-review.googlesource.com/c/1407120
Reviewed-by: Richard Coles <torne@chromium.org>
Reviewed-by: Ted Choc <tedchoc@chromium.org>
Commit-Queue: Theresa <twellington@chromium.org>
Cr-Commit-Position: refs/heads/master@{#622489}
diff --git a/android_webview/glue/BUILD.gn b/android_webview/glue/BUILD.gn
index 7035687..884c91c6 100644
--- a/android_webview/glue/BUILD.gn
+++ b/android_webview/glue/BUILD.gn
@@ -29,7 +29,6 @@
     "java/src/com/android/webview/chromium/CookieManagerAdapter.java",
     "java/src/com/android/webview/chromium/DrawFunctor.java",
     "java/src/com/android/webview/chromium/DrawGLFunctor.java",
-    "java/src/com/android/webview/chromium/FontPreloadingWorkaround.java",
     "java/src/com/android/webview/chromium/GeolocationPermissionsAdapter.java",
     "java/src/com/android/webview/chromium/GlueApiHelperForM.java",
     "java/src/com/android/webview/chromium/GlueApiHelperForN.java",
diff --git a/android_webview/glue/glue.gni b/android_webview/glue/glue.gni
index 0581c9b..72a44aee 100644
--- a/android_webview/glue/glue.gni
+++ b/android_webview/glue/glue.gni
@@ -14,6 +14,7 @@
   "//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",
diff --git a/android_webview/glue/java/DEPS b/android_webview/glue/java/DEPS
index f986aa0..5f1cab87 100644
--- a/android_webview/glue/java/DEPS
+++ b/android_webview/glue/java/DEPS
@@ -2,6 +2,7 @@
   "+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",
diff --git a/android_webview/glue/java/src/com/android/webview/chromium/WebViewApplication.java b/android_webview/glue/java/src/com/android/webview/chromium/WebViewApplication.java
index a2e1b30..64f1bd3 100644
--- a/android_webview/glue/java/src/com/android/webview/chromium/WebViewApplication.java
+++ b/android_webview/glue/java/src/com/android/webview/chromium/WebViewApplication.java
@@ -6,6 +6,8 @@
 
 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.
diff --git a/chrome/android/BUILD.gn b/chrome/android/BUILD.gn
index 2330751..9d34703 100644
--- a/chrome/android/BUILD.gn
+++ b/chrome/android/BUILD.gn
@@ -296,6 +296,7 @@
     "//components/download/network:network_java",
     "//components/download/public/common:public_java",
     "//components/download/public/task:public_java",
+    "//components/embedder_support/android:application_java",
     "//components/embedder_support/android:content_view_java",
     "//components/embedder_support/android:web_contents_delegate_java",
     "//components/feature_engagement:feature_engagement_java",
diff --git a/chrome/android/java/DEPS b/chrome/android/java/DEPS
index cbfd8f4..3f29a70e 100644
--- a/chrome/android/java/DEPS
+++ b/chrome/android/java/DEPS
@@ -29,7 +29,6 @@
 specific_include_rules = {
   # Special-case where monochrome composes chrome+webview
   "MonochromeApplication\.java": [
-    "+android_webview/glue/java/src/com/android/webview/chromium/FontPreloadingWorkaround.java",
     "+android_webview/glue/java/src/com/android/webview/chromium/MonochromeLibraryPreloader.java",
   ]
 }
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ChromeApplication.java b/chrome/android/java/src/org/chromium/chrome/browser/ChromeApplication.java
index 706a37c..b4b20c9 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/ChromeApplication.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ChromeApplication.java
@@ -44,6 +44,7 @@
 import org.chromium.chrome.browser.util.FeatureUtilities;
 import org.chromium.chrome.browser.vr.OnExitVrRequestListener;
 import org.chromium.chrome.browser.vr.VrModuleProvider;
+import org.chromium.components.embedder_support.application.FontPreloadingWorkaround;
 import org.chromium.components.module_installer.ModuleInstaller;
 
 /**
@@ -62,6 +63,7 @@
     @Override
     public void onCreate() {
         super.onCreate();
+        FontPreloadingWorkaround.maybeInstallWorkaround(this);
         if (isBrowserProcess()) initDefaultNightMode();
     }
 
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/MonochromeApplication.java b/chrome/android/java/src/org/chromium/chrome/browser/MonochromeApplication.java
index 0a83cbe..33c3ddf9 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/MonochromeApplication.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/MonochromeApplication.java
@@ -4,7 +4,6 @@
 
 package org.chromium.chrome.browser;
 
-import com.android.webview.chromium.FontPreloadingWorkaround;
 import com.android.webview.chromium.MonochromeLibraryPreloader;
 
 import org.chromium.base.library_loader.LibraryLoader;
@@ -22,7 +21,6 @@
     @Override
     public void onCreate() {
         super.onCreate();
-        FontPreloadingWorkaround.maybeInstallWorkaround(this);
         LibraryLoader.getInstance().setNativeLibraryPreloader(new MonochromeLibraryPreloader());
         // ChildProcessCreationParams is only needed for browser process, though it is
         // created and set in all processes.
diff --git a/components/embedder_support/android/BUILD.gn b/components/embedder_support/android/BUILD.gn
index 4e6e893..ba53ed8 100644
--- a/components/embedder_support/android/BUILD.gn
+++ b/components/embedder_support/android/BUILD.gn
@@ -4,6 +4,13 @@
 
 import("//build/config/android/rules.gni")
 
+android_library("application_java") {
+  deps = [
+    "//base:base_java",
+  ]
+  java_files = [ "java/src/org/chromium/components/embedder_support/application/FontPreloadingWorkaround.java" ]
+}
+
 static_library("view") {
   sources = [
     "view/content_view_render_view.cc",
diff --git a/android_webview/glue/java/src/com/android/webview/chromium/FontPreloadingWorkaround.java b/components/embedder_support/android/java/src/org/chromium/components/embedder_support/application/FontPreloadingWorkaround.java
similarity index 91%
rename from android_webview/glue/java/src/com/android/webview/chromium/FontPreloadingWorkaround.java
rename to components/embedder_support/android/java/src/org/chromium/components/embedder_support/application/FontPreloadingWorkaround.java
index e0ef2e2..656ef357 100644
--- a/android_webview/glue/java/src/com/android/webview/chromium/FontPreloadingWorkaround.java
+++ b/components/embedder_support/android/java/src/org/chromium/components/embedder_support/application/FontPreloadingWorkaround.java
@@ -2,7 +2,7 @@
 // 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;
+package org.chromium.components.embedder_support.application;
 
 import android.content.Context;
 import android.content.pm.ApplicationInfo;
@@ -19,13 +19,18 @@
 import java.lang.reflect.Proxy;
 
 /**
- * WebView-side workaround for the Android O framework bug described in https://crbug.com/809636.
+ * Workaround for the Android O framework bug described in https://crbug.com/809636 and
+ * https://crbug.com/920692.
  *
  * If the application which uses WebView has a preloaded_fonts metadata key in its manifest, the
  * framework's font preloading code will attempt to use the application resource ID specified there
  * to do font preloading in the WebView renderer process, which will fail as the resource will not
  * exist in the WebView APK, resulting in a crash.
  *
+ * If Chromium has a preloaded_fonts metadata key in its manifest and the framework's font
+ * preloading code attempts to use load it from a renderer process, it will fail on a
+ * SecurityException.
+ *
  * However, Application.onCreate runs before the font preloading attempt, and can use reflection to
  * install a replacement implementation of IPackageManager into ActivityThread. This replacement
  * will filter out the preloaded_fonts metadata key from any returned results, avoiding the crash.
@@ -104,7 +109,7 @@
                 }
             } catch (InvocationTargetException e) {
                 throw e.getTargetException();
-            } catch (ReflectiveOperationException e) {
+            } catch (IllegalAccessException e) {
                 throw new RuntimeException("Reflection failed when proxying IPackageManager", e);
             }
             return result;