Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.webkit; |
| 18 | |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 19 | import android.annotation.SystemApi; |
Mathew Inwood | 42afea2 | 2018-08-16 19:18:28 +0100 | [diff] [blame] | 20 | import android.annotation.UnsupportedAppUsage; |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 21 | import android.app.ActivityManager; |
Torne (Richard Coles) | 6c778ce | 2014-07-17 14:14:48 -0700 | [diff] [blame] | 22 | import android.app.AppGlobals; |
Jeff Sharkey | 8584491 | 2014-11-13 16:20:38 -0800 | [diff] [blame] | 23 | import android.app.Application; |
Torne (Richard Coles) | 6c778ce | 2014-07-17 14:14:48 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Primiano Tucci | 1b7977b | 2014-07-25 19:19:32 +0100 | [diff] [blame] | 25 | import android.content.pm.ApplicationInfo; |
Torne (Richard Coles) | 0606cd5 | 2014-08-05 16:12:09 +0100 | [diff] [blame] | 26 | import android.content.pm.PackageInfo; |
Torne (Richard Coles) | 6c778ce | 2014-07-17 14:14:48 -0700 | [diff] [blame] | 27 | import android.content.pm.PackageManager; |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 28 | import android.content.pm.Signature; |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 29 | import android.os.RemoteException; |
| 30 | import android.os.ServiceManager; |
Torne (Richard Coles) | 3822882 | 2014-08-13 17:11:45 +0100 | [diff] [blame] | 31 | import android.os.Trace; |
Torne (Richard Coles) | 03ce9b3 | 2013-06-12 16:02:03 +0100 | [diff] [blame] | 32 | import android.util.AndroidRuntimeException; |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 33 | import android.util.ArraySet; |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 34 | import android.util.Log; |
Jeff Sharkey | 8584491 | 2014-11-13 16:20:38 -0800 | [diff] [blame] | 35 | |
Torne (Richard Coles) | 12f64da | 2017-10-24 14:59:07 -0400 | [diff] [blame] | 36 | import java.io.File; |
Bo Liu | 4827385 | 2016-08-16 19:03:44 -0700 | [diff] [blame] | 37 | import java.lang.reflect.Method; |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 38 | |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 39 | /** |
| 40 | * Top level factory, used creating all the main WebView implementation classes. |
Jared Duke | b0e3584 | 2013-03-19 16:25:39 -0700 | [diff] [blame] | 41 | * |
| 42 | * @hide |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 43 | */ |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 44 | @SystemApi |
Jared Duke | b0e3584 | 2013-03-19 16:25:39 -0700 | [diff] [blame] | 45 | public final class WebViewFactory { |
Jonathan Dixon | a7eaa8e | 2013-07-25 19:52:47 -0700 | [diff] [blame] | 46 | |
Torne (Richard Coles) | 3b6ca99 | 2016-10-10 15:11:36 +0100 | [diff] [blame] | 47 | // visible for WebViewZygoteInit to look up the class by reflection and call preloadInZygote. |
| 48 | /** @hide */ |
Tao Bai | 755eded | 2017-01-09 18:35:44 -0800 | [diff] [blame] | 49 | private static final String CHROMIUM_WEBVIEW_FACTORY = |
Shimi Zhang | 295c5a1 | 2017-12-14 19:03:30 -0800 | [diff] [blame] | 50 | "com.android.webview.chromium.WebViewChromiumFactoryProviderForP"; |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 51 | |
Bo Liu | 4827385 | 2016-08-16 19:03:44 -0700 | [diff] [blame] | 52 | private static final String CHROMIUM_WEBVIEW_FACTORY_METHOD = "create"; |
| 53 | |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 54 | public static final String CHROMIUM_WEBVIEW_VMSIZE_SIZE_PROPERTY = |
| 55 | "persist.sys.webview.vmsize"; |
Primiano Tucci | 1b7977b | 2014-07-25 19:19:32 +0100 | [diff] [blame] | 56 | |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 57 | private static final String LOGTAG = "WebViewFactory"; |
| 58 | |
| 59 | private static final boolean DEBUG = false; |
| 60 | |
| 61 | // Cache the factory both for efficiency, and ensure any one process gets all webviews from the |
| 62 | // same provider. |
Mathew Inwood | 42afea2 | 2018-08-16 19:18:28 +0100 | [diff] [blame] | 63 | @UnsupportedAppUsage |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 64 | private static WebViewFactoryProvider sProviderInstance; |
John Reck | 9f9d345 | 2012-09-20 13:18:59 -0700 | [diff] [blame] | 65 | private static final Object sProviderLock = new Object(); |
Mathew Inwood | 42afea2 | 2018-08-16 19:18:28 +0100 | [diff] [blame] | 66 | @UnsupportedAppUsage |
Torne (Richard Coles) | 84392d7 | 2014-08-14 16:43:18 +0100 | [diff] [blame] | 67 | private static PackageInfo sPackageInfo; |
Torne (Richard Coles) | ff937ac | 2017-10-02 17:09:32 -0400 | [diff] [blame] | 68 | private static Boolean sWebViewSupported; |
Torne (Richard Coles) | 12f64da | 2017-10-24 14:59:07 -0400 | [diff] [blame] | 69 | private static boolean sWebViewDisabled; |
| 70 | private static String sDataDirectorySuffix; // stored here so it can be set without loading WV |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 71 | |
Gustav Sennton | 85edb6c | 2015-04-15 11:54:20 +0100 | [diff] [blame] | 72 | // Error codes for loadWebViewNativeLibraryFromPackage |
| 73 | public static final int LIBLOAD_SUCCESS = 0; |
| 74 | public static final int LIBLOAD_WRONG_PACKAGE_NAME = 1; |
| 75 | public static final int LIBLOAD_ADDRESS_SPACE_NOT_RESERVED = 2; |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 76 | |
| 77 | // error codes for waiting for WebView preparation |
Gustav Sennton | 85edb6c | 2015-04-15 11:54:20 +0100 | [diff] [blame] | 78 | public static final int LIBLOAD_FAILED_WAITING_FOR_RELRO = 3; |
| 79 | public static final int LIBLOAD_FAILED_LISTING_WEBVIEW_PACKAGES = 4; |
| 80 | |
| 81 | // native relro loading error codes |
| 82 | public static final int LIBLOAD_FAILED_TO_OPEN_RELRO_FILE = 5; |
| 83 | public static final int LIBLOAD_FAILED_TO_LOAD_LIBRARY = 6; |
| 84 | public static final int LIBLOAD_FAILED_JNI_CALL = 7; |
| 85 | |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 86 | // more error codes for waiting for WebView preparation |
Gustav Sennton | 26c82ff | 2016-03-11 13:06:40 +0000 | [diff] [blame] | 87 | public static final int LIBLOAD_FAILED_WAITING_FOR_WEBVIEW_REASON_UNKNOWN = 8; |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 88 | |
Torne (Richard Coles) | 1a90412 | 2016-03-14 13:45:55 +0000 | [diff] [blame] | 89 | // error for namespace lookup |
| 90 | public static final int LIBLOAD_FAILED_TO_FIND_NAMESPACE = 10; |
| 91 | |
Gustav Sennton | b715d40 | 2017-05-12 14:22:58 +0100 | [diff] [blame] | 92 | |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 93 | private static String getWebViewPreparationErrorReason(int error) { |
| 94 | switch (error) { |
| 95 | case LIBLOAD_FAILED_WAITING_FOR_RELRO: |
| 96 | return "Time out waiting for Relro files being created"; |
| 97 | case LIBLOAD_FAILED_LISTING_WEBVIEW_PACKAGES: |
| 98 | return "No WebView installed"; |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 99 | case LIBLOAD_FAILED_WAITING_FOR_WEBVIEW_REASON_UNKNOWN: |
| 100 | return "Crashed for unknown reason"; |
| 101 | } |
| 102 | return "Unknown"; |
| 103 | } |
| 104 | |
Gustav Sennton | ed98a156 | 2017-06-06 14:25:49 +0100 | [diff] [blame] | 105 | static class MissingWebViewPackageException extends Exception { |
Gustav Sennton | a8366e7 | 2015-04-17 11:24:07 +0100 | [diff] [blame] | 106 | public MissingWebViewPackageException(String message) { super(message); } |
| 107 | public MissingWebViewPackageException(Exception e) { super(e); } |
| 108 | } |
| 109 | |
Torne (Richard Coles) | ff937ac | 2017-10-02 17:09:32 -0400 | [diff] [blame] | 110 | private static boolean isWebViewSupported() { |
| 111 | // No lock; this is a benign race as Boolean's state is final and the PackageManager call |
| 112 | // will always return the same value. |
| 113 | if (sWebViewSupported == null) { |
| 114 | sWebViewSupported = AppGlobals.getInitialApplication().getPackageManager() |
| 115 | .hasSystemFeature(PackageManager.FEATURE_WEBVIEW); |
| 116 | } |
| 117 | return sWebViewSupported; |
| 118 | } |
| 119 | |
Gustav Sennton | 6ce92c9 | 2015-10-23 11:10:39 +0100 | [diff] [blame] | 120 | /** |
Gustav Sennton | 6ce92c9 | 2015-10-23 11:10:39 +0100 | [diff] [blame] | 121 | * @hide |
| 122 | */ |
Torne (Richard Coles) | 12f64da | 2017-10-24 14:59:07 -0400 | [diff] [blame] | 123 | static void disableWebView() { |
| 124 | synchronized (sProviderLock) { |
| 125 | if (sProviderInstance != null) { |
| 126 | throw new IllegalStateException( |
| 127 | "Can't disable WebView: WebView already initialized"); |
| 128 | } |
| 129 | sWebViewDisabled = true; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * @hide |
| 135 | */ |
| 136 | static void setDataDirectorySuffix(String suffix) { |
| 137 | synchronized (sProviderLock) { |
| 138 | if (sProviderInstance != null) { |
| 139 | throw new IllegalStateException( |
| 140 | "Can't set data directory suffix: WebView already initialized"); |
| 141 | } |
| 142 | if (suffix.indexOf(File.separatorChar) >= 0) { |
| 143 | throw new IllegalArgumentException("Suffix " + suffix |
| 144 | + " contains a path separator"); |
| 145 | } |
| 146 | sDataDirectorySuffix = suffix; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * @hide |
| 152 | */ |
| 153 | static String getDataDirectorySuffix() { |
| 154 | synchronized (sProviderLock) { |
| 155 | return sDataDirectorySuffix; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @hide |
| 161 | */ |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 162 | public static String getWebViewLibrary(ApplicationInfo ai) { |
Gustav Sennton | 2ed6fee | 2015-03-03 15:12:34 +0000 | [diff] [blame] | 163 | if (ai.metaData != null) |
| 164 | return ai.metaData.getString("com.android.webview.WebViewLibrary"); |
| 165 | return null; |
Ben Murdoch | dc00a84 | 2014-07-17 14:55:00 +0100 | [diff] [blame] | 166 | } |
| 167 | |
Torne (Richard Coles) | 84392d7 | 2014-08-14 16:43:18 +0100 | [diff] [blame] | 168 | public static PackageInfo getLoadedPackageInfo() { |
Gustav Sennton | bf683e0 | 2016-09-15 14:42:50 +0100 | [diff] [blame] | 169 | synchronized (sProviderLock) { |
| 170 | return sPackageInfo; |
| 171 | } |
Torne (Richard Coles) | 84392d7 | 2014-08-14 16:43:18 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Gustav Sennton | 85edb6c | 2015-04-15 11:54:20 +0100 | [diff] [blame] | 174 | /** |
Tao Bai | 755eded | 2017-01-09 18:35:44 -0800 | [diff] [blame] | 175 | * @hide |
| 176 | */ |
Gustav Sennton | 564c2fd | 2017-01-30 18:08:01 +0000 | [diff] [blame] | 177 | public static Class<WebViewFactoryProvider> getWebViewProviderClass(ClassLoader clazzLoader) |
| 178 | throws ClassNotFoundException { |
| 179 | return (Class<WebViewFactoryProvider>) Class.forName(CHROMIUM_WEBVIEW_FACTORY, |
| 180 | true, clazzLoader); |
Tao Bai | 755eded | 2017-01-09 18:35:44 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /** |
Nate Fischer | 2be201e | 2017-10-26 10:43:00 -0700 | [diff] [blame] | 184 | * Load the native library for the given package name if that package |
Gustav Sennton | d9730182 | 2015-06-18 16:56:26 +0100 | [diff] [blame] | 185 | * name is the same as the one providing the webview. |
Gustav Sennton | 85edb6c | 2015-04-15 11:54:20 +0100 | [diff] [blame] | 186 | */ |
Torne (Richard Coles) | 1a90412 | 2016-03-14 13:45:55 +0000 | [diff] [blame] | 187 | public static int loadWebViewNativeLibraryFromPackage(String packageName, |
| 188 | ClassLoader clazzLoader) { |
Torne (Richard Coles) | ff937ac | 2017-10-02 17:09:32 -0400 | [diff] [blame] | 189 | if (!isWebViewSupported()) { |
| 190 | return LIBLOAD_WRONG_PACKAGE_NAME; |
| 191 | } |
| 192 | |
Gustav Sennton | b088cb3 | 2016-06-17 14:02:52 +0100 | [diff] [blame] | 193 | WebViewProviderResponse response = null; |
| 194 | try { |
| 195 | response = getUpdateService().waitForAndGetProvider(); |
| 196 | } catch (RemoteException e) { |
| 197 | Log.e(LOGTAG, "error waiting for relro creation", e); |
| 198 | return LIBLOAD_FAILED_WAITING_FOR_WEBVIEW_REASON_UNKNOWN; |
Gustav Sennton | 6ce92c9 | 2015-10-23 11:10:39 +0100 | [diff] [blame] | 199 | } |
Gustav Sennton | b088cb3 | 2016-06-17 14:02:52 +0100 | [diff] [blame] | 200 | |
| 201 | |
| 202 | if (response.status != LIBLOAD_SUCCESS |
| 203 | && response.status != LIBLOAD_FAILED_WAITING_FOR_RELRO) { |
| 204 | return response.status; |
| 205 | } |
| 206 | if (!response.packageInfo.packageName.equals(packageName)) { |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 207 | return LIBLOAD_WRONG_PACKAGE_NAME; |
Gustav Sennton | b088cb3 | 2016-06-17 14:02:52 +0100 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | PackageManager packageManager = AppGlobals.getInitialApplication().getPackageManager(); |
Torne (Richard Coles) | ec572f6 | 2018-02-21 15:55:24 -0500 | [diff] [blame] | 211 | String libraryFileName; |
Gustav Sennton | b088cb3 | 2016-06-17 14:02:52 +0100 | [diff] [blame] | 212 | try { |
Torne (Richard Coles) | ec572f6 | 2018-02-21 15:55:24 -0500 | [diff] [blame] | 213 | PackageInfo packageInfo = packageManager.getPackageInfo(packageName, |
Gustav Sennton | b088cb3 | 2016-06-17 14:02:52 +0100 | [diff] [blame] | 214 | PackageManager.GET_META_DATA | PackageManager.MATCH_DEBUG_TRIAGED_MISSING); |
Torne (Richard Coles) | ec572f6 | 2018-02-21 15:55:24 -0500 | [diff] [blame] | 215 | libraryFileName = getWebViewLibrary(packageInfo.applicationInfo); |
Gustav Sennton | b088cb3 | 2016-06-17 14:02:52 +0100 | [diff] [blame] | 216 | } catch (PackageManager.NameNotFoundException e) { |
| 217 | Log.e(LOGTAG, "Couldn't find package " + packageName); |
| 218 | return LIBLOAD_WRONG_PACKAGE_NAME; |
| 219 | } |
Gustav Sennton | 6ce92c9 | 2015-10-23 11:10:39 +0100 | [diff] [blame] | 220 | |
Torne (Richard Coles) | ec572f6 | 2018-02-21 15:55:24 -0500 | [diff] [blame] | 221 | int loadNativeRet = WebViewLibraryLoader.loadNativeLibrary(clazzLoader, libraryFileName); |
| 222 | // If we failed waiting for relro we want to return that fact even if we successfully |
| 223 | // load the relro file. |
| 224 | if (loadNativeRet == LIBLOAD_SUCCESS) return response.status; |
| 225 | return loadNativeRet; |
Gustav Sennton | 85edb6c | 2015-04-15 11:54:20 +0100 | [diff] [blame] | 226 | } |
| 227 | |
Mathew Inwood | 42afea2 | 2018-08-16 19:18:28 +0100 | [diff] [blame] | 228 | @UnsupportedAppUsage |
John Reck | 9f9d345 | 2012-09-20 13:18:59 -0700 | [diff] [blame] | 229 | static WebViewFactoryProvider getProvider() { |
| 230 | synchronized (sProviderLock) { |
| 231 | // For now the main purpose of this function (and the factory abstraction) is to keep |
Torne (Richard Coles) | d892afc | 2013-10-14 17:14:04 +0100 | [diff] [blame] | 232 | // us honest and minimize usage of WebView internals when binding the proxy. |
John Reck | 9f9d345 | 2012-09-20 13:18:59 -0700 | [diff] [blame] | 233 | if (sProviderInstance != null) return sProviderInstance; |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 234 | |
Jeff Sharkey | 8584491 | 2014-11-13 16:20:38 -0800 | [diff] [blame] | 235 | final int uid = android.os.Process.myUid(); |
Robert Sesek | 450b55b | 2017-03-16 18:17:11 -0400 | [diff] [blame] | 236 | if (uid == android.os.Process.ROOT_UID || uid == android.os.Process.SYSTEM_UID |
| 237 | || uid == android.os.Process.PHONE_UID || uid == android.os.Process.NFC_UID |
| 238 | || uid == android.os.Process.BLUETOOTH_UID) { |
Jeff Sharkey | 8584491 | 2014-11-13 16:20:38 -0800 | [diff] [blame] | 239 | throw new UnsupportedOperationException( |
| 240 | "For security reasons, WebView is not allowed in privileged processes"); |
| 241 | } |
| 242 | |
Torne (Richard Coles) | ff937ac | 2017-10-02 17:09:32 -0400 | [diff] [blame] | 243 | if (!isWebViewSupported()) { |
| 244 | // Device doesn't support WebView; don't try to load it, just throw. |
| 245 | throw new UnsupportedOperationException(); |
| 246 | } |
| 247 | |
Torne (Richard Coles) | 12f64da | 2017-10-24 14:59:07 -0400 | [diff] [blame] | 248 | if (sWebViewDisabled) { |
| 249 | throw new IllegalStateException( |
| 250 | "WebView.disableWebView() was called: WebView is disabled"); |
| 251 | } |
| 252 | |
Torne (Richard Coles) | 3822882 | 2014-08-13 17:11:45 +0100 | [diff] [blame] | 253 | Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "WebViewFactory.getProvider()"); |
Torne (Richard Coles) | 03ce9b3 | 2013-06-12 16:02:03 +0100 | [diff] [blame] | 254 | try { |
Gustav Sennton | a8366e7 | 2015-04-17 11:24:07 +0100 | [diff] [blame] | 255 | Class<WebViewFactoryProvider> providerClass = getProviderClass(); |
Bo Liu | 4827385 | 2016-08-16 19:03:44 -0700 | [diff] [blame] | 256 | Method staticFactory = null; |
| 257 | try { |
| 258 | staticFactory = providerClass.getMethod( |
| 259 | CHROMIUM_WEBVIEW_FACTORY_METHOD, WebViewDelegate.class); |
| 260 | } catch (Exception e) { |
| 261 | if (DEBUG) { |
| 262 | Log.w(LOGTAG, "error instantiating provider with static factory method", e); |
| 263 | } |
| 264 | } |
Torne (Richard Coles) | 3822882 | 2014-08-13 17:11:45 +0100 | [diff] [blame] | 265 | |
Gustav Sennton | 564c2fd | 2017-01-30 18:08:01 +0000 | [diff] [blame] | 266 | Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "WebViewFactoryProvider invocation"); |
Torne (Richard Coles) | 3822882 | 2014-08-13 17:11:45 +0100 | [diff] [blame] | 267 | try { |
Gustav Sennton | 564c2fd | 2017-01-30 18:08:01 +0000 | [diff] [blame] | 268 | sProviderInstance = (WebViewFactoryProvider) |
| 269 | staticFactory.invoke(null, new WebViewDelegate()); |
Torne (Richard Coles) | 3822882 | 2014-08-13 17:11:45 +0100 | [diff] [blame] | 270 | if (DEBUG) Log.v(LOGTAG, "Loaded provider: " + sProviderInstance); |
| 271 | return sProviderInstance; |
| 272 | } catch (Exception e) { |
| 273 | Log.e(LOGTAG, "error instantiating provider", e); |
| 274 | throw new AndroidRuntimeException(e); |
| 275 | } finally { |
| 276 | Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW); |
Torne (Richard Coles) | 3822882 | 2014-08-13 17:11:45 +0100 | [diff] [blame] | 277 | } |
Torne (Richard Coles) | 03ce9b3 | 2013-06-12 16:02:03 +0100 | [diff] [blame] | 278 | } finally { |
Torne (Richard Coles) | 3822882 | 2014-08-13 17:11:45 +0100 | [diff] [blame] | 279 | Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW); |
Torne (Richard Coles) | 03ce9b3 | 2013-06-12 16:02:03 +0100 | [diff] [blame] | 280 | } |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 281 | } |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 282 | } |
| 283 | |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 284 | /** |
Nate Fischer | 0a6140d | 2017-09-05 12:37:49 -0700 | [diff] [blame] | 285 | * Returns {@code true} if the signatures match, {@code false} otherwise |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 286 | */ |
| 287 | private static boolean signaturesEquals(Signature[] s1, Signature[] s2) { |
| 288 | if (s1 == null) { |
| 289 | return s2 == null; |
| 290 | } |
| 291 | if (s2 == null) return false; |
| 292 | |
| 293 | ArraySet<Signature> set1 = new ArraySet<>(); |
| 294 | for(Signature signature : s1) { |
| 295 | set1.add(signature); |
| 296 | } |
| 297 | ArraySet<Signature> set2 = new ArraySet<>(); |
| 298 | for(Signature signature : s2) { |
| 299 | set2.add(signature); |
| 300 | } |
| 301 | return set1.equals(set2); |
| 302 | } |
| 303 | |
| 304 | // Throws MissingWebViewPackageException on failure |
Gustav Sennton | b715d40 | 2017-05-12 14:22:58 +0100 | [diff] [blame] | 305 | private static void verifyPackageInfo(PackageInfo chosen, PackageInfo toUse) |
| 306 | throws MissingWebViewPackageException { |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 307 | if (!chosen.packageName.equals(toUse.packageName)) { |
| 308 | throw new MissingWebViewPackageException("Failed to verify WebView provider, " |
| 309 | + "packageName mismatch, expected: " |
| 310 | + chosen.packageName + " actual: " + toUse.packageName); |
| 311 | } |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 312 | if (chosen.getLongVersionCode() > toUse.getLongVersionCode()) { |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 313 | throw new MissingWebViewPackageException("Failed to verify WebView provider, " |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 314 | + "version code is lower than expected: " + chosen.getLongVersionCode() |
| 315 | + " actual: " + toUse.getLongVersionCode()); |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 316 | } |
| 317 | if (getWebViewLibrary(toUse.applicationInfo) == null) { |
| 318 | throw new MissingWebViewPackageException("Tried to load an invalid WebView provider: " |
| 319 | + toUse.packageName); |
| 320 | } |
| 321 | if (!signaturesEquals(chosen.signatures, toUse.signatures)) { |
| 322 | throw new MissingWebViewPackageException("Failed to verify WebView provider, " |
| 323 | + "signature mismatch"); |
| 324 | } |
| 325 | } |
| 326 | |
Torne (Richard Coles) | 0452670 | 2017-01-13 14:19:39 +0000 | [diff] [blame] | 327 | /** |
| 328 | * If the ApplicationInfo provided is for a stub WebView, fix up the object to include the |
| 329 | * required values from the donor package. If the ApplicationInfo is for a full WebView, |
| 330 | * leave it alone. Throws MissingWebViewPackageException if the donor is missing. |
| 331 | */ |
Gustav Sennton | b715d40 | 2017-05-12 14:22:58 +0100 | [diff] [blame] | 332 | private static void fixupStubApplicationInfo(ApplicationInfo ai, PackageManager pm) |
| 333 | throws MissingWebViewPackageException { |
Torne (Richard Coles) | 0452670 | 2017-01-13 14:19:39 +0000 | [diff] [blame] | 334 | String donorPackageName = null; |
| 335 | if (ai.metaData != null) { |
| 336 | donorPackageName = ai.metaData.getString("com.android.webview.WebViewDonorPackage"); |
| 337 | } |
| 338 | if (donorPackageName != null) { |
| 339 | PackageInfo donorPackage; |
| 340 | try { |
| 341 | donorPackage = pm.getPackageInfo( |
| 342 | donorPackageName, |
| 343 | PackageManager.GET_SHARED_LIBRARY_FILES |
| 344 | | PackageManager.MATCH_DEBUG_TRIAGED_MISSING |
| 345 | | PackageManager.MATCH_UNINSTALLED_PACKAGES |
| 346 | | PackageManager.MATCH_FACTORY_ONLY); |
| 347 | } catch (PackageManager.NameNotFoundException e) { |
| 348 | throw new MissingWebViewPackageException("Failed to find donor package: " + |
| 349 | donorPackageName); |
| 350 | } |
| 351 | ApplicationInfo donorInfo = donorPackage.applicationInfo; |
| 352 | |
| 353 | // Replace the stub's code locations with the donor's. |
| 354 | ai.sourceDir = donorInfo.sourceDir; |
| 355 | ai.splitSourceDirs = donorInfo.splitSourceDirs; |
| 356 | ai.nativeLibraryDir = donorInfo.nativeLibraryDir; |
| 357 | ai.secondaryNativeLibraryDir = donorInfo.secondaryNativeLibraryDir; |
| 358 | |
| 359 | // Copy the donor's primary and secondary ABIs, since the stub doesn't have native code |
| 360 | // and so they are unset. |
| 361 | ai.primaryCpuAbi = donorInfo.primaryCpuAbi; |
| 362 | ai.secondaryCpuAbi = donorInfo.secondaryCpuAbi; |
| 363 | } |
| 364 | } |
| 365 | |
Mathew Inwood | 42afea2 | 2018-08-16 19:18:28 +0100 | [diff] [blame] | 366 | @UnsupportedAppUsage |
Gustav Sennton | b715d40 | 2017-05-12 14:22:58 +0100 | [diff] [blame] | 367 | private static Context getWebViewContextAndSetProvider() throws MissingWebViewPackageException { |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 368 | Application initialApplication = AppGlobals.getInitialApplication(); |
| 369 | try { |
| 370 | WebViewProviderResponse response = null; |
| 371 | Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, |
| 372 | "WebViewUpdateService.waitForAndGetProvider()"); |
| 373 | try { |
| 374 | response = getUpdateService().waitForAndGetProvider(); |
| 375 | } finally { |
| 376 | Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW); |
| 377 | } |
Gustav Sennton | fd07efa | 2016-05-23 13:09:03 +0100 | [diff] [blame] | 378 | if (response.status != LIBLOAD_SUCCESS |
| 379 | && response.status != LIBLOAD_FAILED_WAITING_FOR_RELRO) { |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 380 | throw new MissingWebViewPackageException("Failed to load WebView provider: " |
| 381 | + getWebViewPreparationErrorReason(response.status)); |
| 382 | } |
| 383 | // Register to be killed before fetching package info - so that we will be |
| 384 | // killed if the package info goes out-of-date. |
| 385 | Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "ActivityManager.addPackageDependency()"); |
| 386 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 387 | ActivityManager.getService().addPackageDependency( |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 388 | response.packageInfo.packageName); |
| 389 | } finally { |
| 390 | Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW); |
| 391 | } |
| 392 | // Fetch package info and verify it against the chosen package |
| 393 | PackageInfo newPackageInfo = null; |
Torne (Richard Coles) | 0452670 | 2017-01-13 14:19:39 +0000 | [diff] [blame] | 394 | PackageManager pm = initialApplication.getPackageManager(); |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 395 | Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "PackageManager.getPackageInfo()"); |
| 396 | try { |
Torne (Richard Coles) | 0452670 | 2017-01-13 14:19:39 +0000 | [diff] [blame] | 397 | newPackageInfo = pm.getPackageInfo( |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 398 | response.packageInfo.packageName, |
| 399 | PackageManager.GET_SHARED_LIBRARY_FILES |
| 400 | | PackageManager.MATCH_DEBUG_TRIAGED_MISSING |
| 401 | // Make sure that we fetch the current provider even if its not |
| 402 | // installed for the current user |
| 403 | | PackageManager.MATCH_UNINSTALLED_PACKAGES |
| 404 | // Fetch signatures for verification |
| 405 | | PackageManager.GET_SIGNATURES |
| 406 | // Get meta-data for meta data flag verification |
| 407 | | PackageManager.GET_META_DATA); |
| 408 | } finally { |
| 409 | Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW); |
| 410 | } |
| 411 | |
| 412 | // Validate the newly fetched package info, throws MissingWebViewPackageException on |
| 413 | // failure |
| 414 | verifyPackageInfo(response.packageInfo, newPackageInfo); |
| 415 | |
Torne (Richard Coles) | 0452670 | 2017-01-13 14:19:39 +0000 | [diff] [blame] | 416 | ApplicationInfo ai = newPackageInfo.applicationInfo; |
| 417 | fixupStubApplicationInfo(ai, pm); |
| 418 | |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 419 | Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, |
| 420 | "initialApplication.createApplicationContext"); |
| 421 | try { |
| 422 | // Construct an app context to load the Java code into the current app. |
| 423 | Context webViewContext = initialApplication.createApplicationContext( |
Torne (Richard Coles) | 0452670 | 2017-01-13 14:19:39 +0000 | [diff] [blame] | 424 | ai, |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 425 | Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY); |
Gustav Sennton | b088cb3 | 2016-06-17 14:02:52 +0100 | [diff] [blame] | 426 | sPackageInfo = newPackageInfo; |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 427 | return webViewContext; |
| 428 | } finally { |
| 429 | Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW); |
| 430 | } |
| 431 | } catch (RemoteException | PackageManager.NameNotFoundException e) { |
| 432 | throw new MissingWebViewPackageException("Failed to load WebView provider: " + e); |
| 433 | } |
| 434 | } |
| 435 | |
Mathew Inwood | 42afea2 | 2018-08-16 19:18:28 +0100 | [diff] [blame] | 436 | @UnsupportedAppUsage |
Gustav Sennton | a8366e7 | 2015-04-17 11:24:07 +0100 | [diff] [blame] | 437 | private static Class<WebViewFactoryProvider> getProviderClass() { |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 438 | Context webViewContext = null; |
| 439 | Application initialApplication = AppGlobals.getInitialApplication(); |
| 440 | |
Gustav Sennton | a8366e7 | 2015-04-17 11:24:07 +0100 | [diff] [blame] | 441 | try { |
Gustav Sennton | fc42447 | 2016-01-06 17:11:09 +0000 | [diff] [blame] | 442 | Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 443 | "WebViewFactory.getWebViewContextAndSetProvider()"); |
Gustav Sennton | fc42447 | 2016-01-06 17:11:09 +0000 | [diff] [blame] | 444 | try { |
Gustav Sennton | cd8f273 | 2016-04-14 09:58:36 +0100 | [diff] [blame] | 445 | webViewContext = getWebViewContextAndSetProvider(); |
Gustav Sennton | fc42447 | 2016-01-06 17:11:09 +0000 | [diff] [blame] | 446 | } finally { |
| 447 | Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW); |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 448 | } |
Gustav Sennton | a8366e7 | 2015-04-17 11:24:07 +0100 | [diff] [blame] | 449 | Log.i(LOGTAG, "Loading " + sPackageInfo.packageName + " version " + |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 450 | sPackageInfo.versionName + " (code " + sPackageInfo.getLongVersionCode() + ")"); |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 451 | |
Gustav Sennton | a8366e7 | 2015-04-17 11:24:07 +0100 | [diff] [blame] | 452 | Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "WebViewFactory.getChromiumProviderClass()"); |
| 453 | try { |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 454 | initialApplication.getAssets().addAssetPathAsSharedLibrary( |
| 455 | webViewContext.getApplicationInfo().sourceDir); |
| 456 | ClassLoader clazzLoader = webViewContext.getClassLoader(); |
Torne (Richard Coles) | 1a90412 | 2016-03-14 13:45:55 +0000 | [diff] [blame] | 457 | |
| 458 | Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "WebViewFactory.loadNativeLibrary()"); |
Torne (Richard Coles) | ec572f6 | 2018-02-21 15:55:24 -0500 | [diff] [blame] | 459 | WebViewLibraryLoader.loadNativeLibrary(clazzLoader, |
| 460 | getWebViewLibrary(sPackageInfo.applicationInfo)); |
Torne (Richard Coles) | 1a90412 | 2016-03-14 13:45:55 +0000 | [diff] [blame] | 461 | Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW); |
| 462 | |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 463 | Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "Class.forName()"); |
| 464 | try { |
Tao Bai | 755eded | 2017-01-09 18:35:44 -0800 | [diff] [blame] | 465 | return getWebViewProviderClass(clazzLoader); |
Gustav Sennton | 564c2fd | 2017-01-30 18:08:01 +0000 | [diff] [blame] | 466 | } finally { |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 467 | Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW); |
| 468 | } |
Gustav Sennton | a8366e7 | 2015-04-17 11:24:07 +0100 | [diff] [blame] | 469 | } catch (ClassNotFoundException e) { |
| 470 | Log.e(LOGTAG, "error loading provider", e); |
| 471 | throw new AndroidRuntimeException(e); |
| 472 | } finally { |
| 473 | Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW); |
| 474 | } |
| 475 | } catch (MissingWebViewPackageException e) { |
Gustav Sennton | a8366e7 | 2015-04-17 11:24:07 +0100 | [diff] [blame] | 476 | Log.e(LOGTAG, "Chromium WebView package does not exist", e); |
| 477 | throw new AndroidRuntimeException(e); |
| 478 | } |
| 479 | } |
| 480 | |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 481 | /** |
| 482 | * Perform any WebView loading preparations that must happen in the zygote. |
| 483 | * Currently, this means allocating address space to load the real JNI library later. |
| 484 | */ |
| 485 | public static void prepareWebViewInZygote() { |
| 486 | try { |
Gustav Sennton | ed98a156 | 2017-06-06 14:25:49 +0100 | [diff] [blame] | 487 | WebViewLibraryLoader.reserveAddressSpaceInZygote(); |
Primiano Tucci | 810c052 | 2014-07-25 18:03:16 +0100 | [diff] [blame] | 488 | } catch (Throwable t) { |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 489 | // Log and discard errors at this stage as we must not crash the zygote. |
Primiano Tucci | 810c052 | 2014-07-25 18:03:16 +0100 | [diff] [blame] | 490 | Log.e(LOGTAG, "error preparing native loader", t); |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
Gustav Sennton | 6258dcd | 2015-10-30 19:25:37 +0000 | [diff] [blame] | 494 | /** |
| 495 | * @hide |
| 496 | */ |
| 497 | public static int onWebViewProviderChanged(PackageInfo packageInfo) { |
Gustav Sennton | 0e541ef | 2017-10-24 14:28:26 +0100 | [diff] [blame] | 498 | int startedRelroProcesses = 0; |
Torne (Richard Coles) | 4fd8aa5 | 2017-09-19 15:21:29 -0400 | [diff] [blame] | 499 | ApplicationInfo originalAppInfo = new ApplicationInfo(packageInfo.applicationInfo); |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 500 | try { |
Torne (Richard Coles) | 0452670 | 2017-01-13 14:19:39 +0000 | [diff] [blame] | 501 | fixupStubApplicationInfo(packageInfo.applicationInfo, |
| 502 | AppGlobals.getInitialApplication().getPackageManager()); |
| 503 | |
Gustav Sennton | 0e541ef | 2017-10-24 14:28:26 +0100 | [diff] [blame] | 504 | startedRelroProcesses = WebViewLibraryLoader.prepareNativeLibraries(packageInfo); |
Torne (Richard Coles) | 27cb0d2 | 2014-08-08 18:24:12 +0100 | [diff] [blame] | 505 | } catch (Throwable t) { |
| 506 | // Log and discard errors at this stage as we must not crash the system server. |
| 507 | Log.e(LOGTAG, "error preparing webview native library", t); |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 508 | } |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 509 | |
Torne (Richard Coles) | 4fd8aa5 | 2017-09-19 15:21:29 -0400 | [diff] [blame] | 510 | WebViewZygote.onWebViewProviderChanged(packageInfo, originalAppInfo); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 511 | |
Gustav Sennton | 0e541ef | 2017-10-24 14:28:26 +0100 | [diff] [blame] | 512 | return startedRelroProcesses; |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 513 | } |
| 514 | |
Gustav Sennton | 1c177d8 | 2016-03-29 20:43:11 +0100 | [diff] [blame] | 515 | private static String WEBVIEW_UPDATE_SERVICE_NAME = "webviewupdate"; |
| 516 | |
| 517 | /** @hide */ |
Mathew Inwood | 42afea2 | 2018-08-16 19:18:28 +0100 | [diff] [blame] | 518 | @UnsupportedAppUsage |
Gustav Sennton | 1c177d8 | 2016-03-29 20:43:11 +0100 | [diff] [blame] | 519 | public static IWebViewUpdateService getUpdateService() { |
Torne (Richard Coles) | ff937ac | 2017-10-02 17:09:32 -0400 | [diff] [blame] | 520 | if (isWebViewSupported()) { |
Torne (Richard Coles) | 29675b2 | 2017-10-04 11:29:58 -0400 | [diff] [blame] | 521 | return getUpdateServiceUnchecked(); |
Torne (Richard Coles) | ff937ac | 2017-10-02 17:09:32 -0400 | [diff] [blame] | 522 | } else { |
| 523 | return null; |
| 524 | } |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 525 | } |
Torne (Richard Coles) | 29675b2 | 2017-10-04 11:29:58 -0400 | [diff] [blame] | 526 | |
| 527 | /** @hide */ |
| 528 | static IWebViewUpdateService getUpdateServiceUnchecked() { |
| 529 | return IWebViewUpdateService.Stub.asInterface( |
| 530 | ServiceManager.getService(WEBVIEW_UPDATE_SERVICE_NAME)); |
| 531 | } |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 532 | } |