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; |
Primiano Tucci | 810c052 | 2014-07-25 18:03:16 +0100 | [diff] [blame] | 20 | import android.app.ActivityManagerInternal; |
Torne (Richard Coles) | 6c778ce | 2014-07-17 14:14:48 -0700 | [diff] [blame] | 21 | import android.app.AppGlobals; |
Jeff Sharkey | 8584491 | 2014-11-13 16:20:38 -0800 | [diff] [blame] | 22 | import android.app.Application; |
Torne (Richard Coles) | 6c778ce | 2014-07-17 14:14:48 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Primiano Tucci | 1b7977b | 2014-07-25 19:19:32 +0100 | [diff] [blame] | 24 | import android.content.pm.ApplicationInfo; |
Torne (Richard Coles) | 0606cd5 | 2014-08-05 16:12:09 +0100 | [diff] [blame] | 25 | import android.content.pm.PackageInfo; |
Torne (Richard Coles) | 6c778ce | 2014-07-17 14:14:48 -0700 | [diff] [blame] | 26 | import android.content.pm.PackageManager; |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 27 | import android.os.Build; |
| 28 | import android.os.Process; |
| 29 | import android.os.RemoteException; |
| 30 | import android.os.ServiceManager; |
Ben Murdoch | e09e976 | 2012-07-19 14:48:13 +0100 | [diff] [blame] | 31 | import android.os.StrictMode; |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 32 | import android.os.SystemProperties; |
Torne (Richard Coles) | 3822882 | 2014-08-13 17:11:45 +0100 | [diff] [blame] | 33 | import android.os.Trace; |
Primiano Tucci | 1b7977b | 2014-07-25 19:19:32 +0100 | [diff] [blame] | 34 | import android.text.TextUtils; |
Torne (Richard Coles) | 03ce9b3 | 2013-06-12 16:02:03 +0100 | [diff] [blame] | 35 | import android.util.AndroidRuntimeException; |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 36 | import android.util.Log; |
Jeff Sharkey | 8584491 | 2014-11-13 16:20:38 -0800 | [diff] [blame] | 37 | |
Primiano Tucci | 810c052 | 2014-07-25 18:03:16 +0100 | [diff] [blame] | 38 | import com.android.server.LocalServices; |
Jeff Sharkey | 8584491 | 2014-11-13 16:20:38 -0800 | [diff] [blame] | 39 | |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 40 | import dalvik.system.VMRuntime; |
| 41 | |
| 42 | import java.io.File; |
Primiano Tucci | 1b7977b | 2014-07-25 19:19:32 +0100 | [diff] [blame] | 43 | import java.util.Arrays; |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 44 | |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 45 | /** |
| 46 | * Top level factory, used creating all the main WebView implementation classes. |
Jared Duke | b0e3584 | 2013-03-19 16:25:39 -0700 | [diff] [blame] | 47 | * |
| 48 | * @hide |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 49 | */ |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 50 | @SystemApi |
Jared Duke | b0e3584 | 2013-03-19 16:25:39 -0700 | [diff] [blame] | 51 | public final class WebViewFactory { |
Jonathan Dixon | a7eaa8e | 2013-07-25 19:52:47 -0700 | [diff] [blame] | 52 | |
Ben Murdoch | e09e976 | 2012-07-19 14:48:13 +0100 | [diff] [blame] | 53 | private static final String CHROMIUM_WEBVIEW_FACTORY = |
Torne (Richard Coles) | a9bbd94 | 2012-10-24 11:59:22 +0100 | [diff] [blame] | 54 | "com.android.webview.chromium.WebViewChromiumFactoryProvider"; |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 55 | |
Ben Murdoch | 0e04bcf | 2014-05-16 13:41:12 +0100 | [diff] [blame] | 56 | private static final String NULL_WEBVIEW_FACTORY = |
| 57 | "com.android.webview.nullwebview.NullWebViewFactoryProvider"; |
| 58 | |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 59 | private static final String CHROMIUM_WEBVIEW_NATIVE_RELRO_32 = |
| 60 | "/data/misc/shared_relro/libwebviewchromium32.relro"; |
| 61 | private static final String CHROMIUM_WEBVIEW_NATIVE_RELRO_64 = |
| 62 | "/data/misc/shared_relro/libwebviewchromium64.relro"; |
| 63 | |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 64 | public static final String CHROMIUM_WEBVIEW_VMSIZE_SIZE_PROPERTY = |
| 65 | "persist.sys.webview.vmsize"; |
| 66 | private static final long CHROMIUM_WEBVIEW_DEFAULT_VMSIZE_BYTES = 100 * 1024 * 1024; |
Primiano Tucci | 1b7977b | 2014-07-25 19:19:32 +0100 | [diff] [blame] | 67 | |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 68 | private static final String LOGTAG = "WebViewFactory"; |
| 69 | |
| 70 | private static final boolean DEBUG = false; |
| 71 | |
| 72 | // Cache the factory both for efficiency, and ensure any one process gets all webviews from the |
| 73 | // same provider. |
| 74 | private static WebViewFactoryProvider sProviderInstance; |
John Reck | 9f9d345 | 2012-09-20 13:18:59 -0700 | [diff] [blame] | 75 | private static final Object sProviderLock = new Object(); |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 76 | private static boolean sAddressSpaceReserved = false; |
Torne (Richard Coles) | 84392d7 | 2014-08-14 16:43:18 +0100 | [diff] [blame] | 77 | private static PackageInfo sPackageInfo; |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 78 | |
Gustav Sennton | a8366e7 | 2015-04-17 11:24:07 +0100 | [diff] [blame] | 79 | private static class MissingWebViewPackageException extends AndroidRuntimeException { |
| 80 | public MissingWebViewPackageException(String message) { super(message); } |
| 81 | public MissingWebViewPackageException(Exception e) { super(e); } |
| 82 | } |
| 83 | |
Gustav Sennton | 2ed6fee | 2015-03-03 15:12:34 +0000 | [diff] [blame] | 84 | /** @hide */ |
| 85 | public static String[] getWebViewPackageNames() { |
| 86 | return AppGlobals.getInitialApplication().getResources().getStringArray( |
| 87 | com.android.internal.R.array.config_webViewPackageNames); |
| 88 | } |
| 89 | |
| 90 | // TODO (gsennton) remove when committing webview xts test change |
Ben Murdoch | dc00a84 | 2014-07-17 14:55:00 +0100 | [diff] [blame] | 91 | public static String getWebViewPackageName() { |
Gustav Sennton | 2ed6fee | 2015-03-03 15:12:34 +0000 | [diff] [blame] | 92 | String[] webViewPackageNames = getWebViewPackageNames(); |
| 93 | return webViewPackageNames[webViewPackageNames.length-1]; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Return the package info of the first package in the webview priority list that contains |
| 98 | * webview. |
| 99 | * |
| 100 | * @hide |
| 101 | */ |
| 102 | public static PackageInfo findPreferredWebViewPackage() { |
| 103 | PackageManager pm = AppGlobals.getInitialApplication().getPackageManager(); |
| 104 | |
| 105 | for (String packageName : getWebViewPackageNames()) { |
| 106 | try { |
| 107 | PackageInfo packageInfo = pm.getPackageInfo(packageName, |
| 108 | PackageManager.GET_META_DATA); |
| 109 | ApplicationInfo applicationInfo = packageInfo.applicationInfo; |
| 110 | |
| 111 | // If the correct flag is set the package contains webview. |
| 112 | if (getWebViewLibrary(applicationInfo) != null) { |
| 113 | return packageInfo; |
| 114 | } |
| 115 | } catch (PackageManager.NameNotFoundException e) { |
| 116 | } |
| 117 | } |
Gustav Sennton | a8366e7 | 2015-04-17 11:24:07 +0100 | [diff] [blame] | 118 | throw new MissingWebViewPackageException("Could not find a loadable WebView package"); |
Gustav Sennton | 2ed6fee | 2015-03-03 15:12:34 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Gustav Sennton | a8366e7 | 2015-04-17 11:24:07 +0100 | [diff] [blame] | 121 | // throws MissingWebViewPackageException |
Gustav Sennton | 2ed6fee | 2015-03-03 15:12:34 +0000 | [diff] [blame] | 122 | private static ApplicationInfo getWebViewApplicationInfo() { |
| 123 | if (sPackageInfo == null) |
| 124 | return findPreferredWebViewPackage().applicationInfo; |
| 125 | else |
| 126 | return sPackageInfo.applicationInfo; |
| 127 | } |
| 128 | |
| 129 | private static String getWebViewLibrary(ApplicationInfo ai) { |
| 130 | if (ai.metaData != null) |
| 131 | return ai.metaData.getString("com.android.webview.WebViewLibrary"); |
| 132 | return null; |
Ben Murdoch | dc00a84 | 2014-07-17 14:55:00 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Torne (Richard Coles) | 84392d7 | 2014-08-14 16:43:18 +0100 | [diff] [blame] | 135 | public static PackageInfo getLoadedPackageInfo() { |
| 136 | return sPackageInfo; |
| 137 | } |
| 138 | |
John Reck | 9f9d345 | 2012-09-20 13:18:59 -0700 | [diff] [blame] | 139 | static WebViewFactoryProvider getProvider() { |
| 140 | synchronized (sProviderLock) { |
| 141 | // 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] | 142 | // us honest and minimize usage of WebView internals when binding the proxy. |
John Reck | 9f9d345 | 2012-09-20 13:18:59 -0700 | [diff] [blame] | 143 | if (sProviderInstance != null) return sProviderInstance; |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 144 | |
Jeff Sharkey | 8584491 | 2014-11-13 16:20:38 -0800 | [diff] [blame] | 145 | final int uid = android.os.Process.myUid(); |
| 146 | if (uid == android.os.Process.ROOT_UID || uid == android.os.Process.SYSTEM_UID) { |
| 147 | throw new UnsupportedOperationException( |
| 148 | "For security reasons, WebView is not allowed in privileged processes"); |
| 149 | } |
| 150 | |
Torne (Richard Coles) | 3822882 | 2014-08-13 17:11:45 +0100 | [diff] [blame] | 151 | Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "WebViewFactory.getProvider()"); |
Torne (Richard Coles) | 03ce9b3 | 2013-06-12 16:02:03 +0100 | [diff] [blame] | 152 | try { |
Gustav Sennton | a8366e7 | 2015-04-17 11:24:07 +0100 | [diff] [blame] | 153 | Class<WebViewFactoryProvider> providerClass = getProviderClass(); |
Torne (Richard Coles) | 3822882 | 2014-08-13 17:11:45 +0100 | [diff] [blame] | 154 | |
| 155 | StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); |
| 156 | Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "providerClass.newInstance()"); |
| 157 | try { |
Ignacio Solla | 1ea39b3 | 2014-11-10 14:08:37 +0000 | [diff] [blame] | 158 | sProviderInstance = providerClass.getConstructor(WebViewDelegate.class) |
| 159 | .newInstance(new WebViewDelegate()); |
Torne (Richard Coles) | 3822882 | 2014-08-13 17:11:45 +0100 | [diff] [blame] | 160 | if (DEBUG) Log.v(LOGTAG, "Loaded provider: " + sProviderInstance); |
| 161 | return sProviderInstance; |
| 162 | } catch (Exception e) { |
| 163 | Log.e(LOGTAG, "error instantiating provider", e); |
| 164 | throw new AndroidRuntimeException(e); |
| 165 | } finally { |
| 166 | Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW); |
| 167 | StrictMode.setThreadPolicy(oldPolicy); |
| 168 | } |
Torne (Richard Coles) | 03ce9b3 | 2013-06-12 16:02:03 +0100 | [diff] [blame] | 169 | } finally { |
Torne (Richard Coles) | 3822882 | 2014-08-13 17:11:45 +0100 | [diff] [blame] | 170 | Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW); |
Torne (Richard Coles) | 03ce9b3 | 2013-06-12 16:02:03 +0100 | [diff] [blame] | 171 | } |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 172 | } |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 173 | } |
| 174 | |
Gustav Sennton | a8366e7 | 2015-04-17 11:24:07 +0100 | [diff] [blame] | 175 | private static Class<WebViewFactoryProvider> getProviderClass() { |
| 176 | try { |
| 177 | // First fetch the package info so we can log the webview package version. |
| 178 | sPackageInfo = findPreferredWebViewPackage(); |
| 179 | Log.i(LOGTAG, "Loading " + sPackageInfo.packageName + " version " + |
| 180 | sPackageInfo.versionName + " (code " + sPackageInfo.versionCode + ")"); |
| 181 | |
| 182 | Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "WebViewFactory.loadNativeLibrary()"); |
| 183 | loadNativeLibrary(); |
| 184 | Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW); |
| 185 | |
| 186 | Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "WebViewFactory.getChromiumProviderClass()"); |
| 187 | try { |
| 188 | return getChromiumProviderClass(); |
| 189 | } catch (ClassNotFoundException e) { |
| 190 | Log.e(LOGTAG, "error loading provider", e); |
| 191 | throw new AndroidRuntimeException(e); |
| 192 | } finally { |
| 193 | Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW); |
| 194 | } |
| 195 | } catch (MissingWebViewPackageException e) { |
| 196 | // If the package doesn't exist, then try loading the null WebView instead. |
| 197 | // If that succeeds, then this is a device without WebView support; if it fails then |
| 198 | // swallow the failure, complain that the real WebView is missing and rethrow the |
| 199 | // original exception. |
| 200 | try { |
| 201 | return (Class<WebViewFactoryProvider>) Class.forName(NULL_WEBVIEW_FACTORY); |
| 202 | } catch (ClassNotFoundException e2) { |
| 203 | // Ignore. |
| 204 | } |
| 205 | Log.e(LOGTAG, "Chromium WebView package does not exist", e); |
| 206 | throw new AndroidRuntimeException(e); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | // throws MissingWebViewPackageException |
| 211 | private static Class<WebViewFactoryProvider> getChromiumProviderClass() |
| 212 | throws ClassNotFoundException { |
Marcin Kosiba | 63d3d8a | 2014-07-22 19:22:48 +0100 | [diff] [blame] | 213 | Application initialApplication = AppGlobals.getInitialApplication(); |
Ben Murdoch | 0e04bcf | 2014-05-16 13:41:12 +0100 | [diff] [blame] | 214 | try { |
Torne (Richard Coles) | 0606cd5 | 2014-08-05 16:12:09 +0100 | [diff] [blame] | 215 | // Construct a package context to load the Java code into the current app. |
Gustav Sennton | 2ed6fee | 2015-03-03 15:12:34 +0000 | [diff] [blame] | 216 | Context webViewContext = initialApplication.createPackageContext( |
| 217 | sPackageInfo.packageName, |
| 218 | Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY); |
Marcin Kosiba | 63d3d8a | 2014-07-22 19:22:48 +0100 | [diff] [blame] | 219 | initialApplication.getAssets().addAssetPath( |
| 220 | webViewContext.getApplicationInfo().sourceDir); |
Torne (Richard Coles) | 6c778ce | 2014-07-17 14:14:48 -0700 | [diff] [blame] | 221 | ClassLoader clazzLoader = webViewContext.getClassLoader(); |
Torne (Richard Coles) | 3822882 | 2014-08-13 17:11:45 +0100 | [diff] [blame] | 222 | Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "Class.forName()"); |
| 223 | try { |
| 224 | return (Class<WebViewFactoryProvider>) Class.forName(CHROMIUM_WEBVIEW_FACTORY, true, |
| 225 | clazzLoader); |
| 226 | } finally { |
| 227 | Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW); |
| 228 | } |
Torne (Richard Coles) | 6c778ce | 2014-07-17 14:14:48 -0700 | [diff] [blame] | 229 | } catch (PackageManager.NameNotFoundException e) { |
Gustav Sennton | a8366e7 | 2015-04-17 11:24:07 +0100 | [diff] [blame] | 230 | throw new MissingWebViewPackageException(e); |
Ben Murdoch | 0e04bcf | 2014-05-16 13:41:12 +0100 | [diff] [blame] | 231 | } |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 232 | } |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 233 | |
| 234 | /** |
| 235 | * Perform any WebView loading preparations that must happen in the zygote. |
| 236 | * Currently, this means allocating address space to load the real JNI library later. |
| 237 | */ |
| 238 | public static void prepareWebViewInZygote() { |
| 239 | try { |
| 240 | System.loadLibrary("webviewchromium_loader"); |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 241 | long addressSpaceToReserve = |
| 242 | SystemProperties.getLong(CHROMIUM_WEBVIEW_VMSIZE_SIZE_PROPERTY, |
| 243 | CHROMIUM_WEBVIEW_DEFAULT_VMSIZE_BYTES); |
| 244 | sAddressSpaceReserved = nativeReserveAddressSpace(addressSpaceToReserve); |
| 245 | |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 246 | if (sAddressSpaceReserved) { |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 247 | if (DEBUG) { |
| 248 | Log.v(LOGTAG, "address space reserved: " + addressSpaceToReserve + " bytes"); |
| 249 | } |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 250 | } else { |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 251 | Log.e(LOGTAG, "reserving " + addressSpaceToReserve + |
| 252 | " bytes of address space failed"); |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 253 | } |
Primiano Tucci | 810c052 | 2014-07-25 18:03:16 +0100 | [diff] [blame] | 254 | } catch (Throwable t) { |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 255 | // 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] | 256 | Log.e(LOGTAG, "error preparing native loader", t); |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Perform any WebView loading preparations that must happen at boot from the system server, |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 262 | * after the package manager has started or after an update to the webview is installed. |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 263 | * This must be called in the system server. |
| 264 | * Currently, this means spawning the child processes which will create the relro files. |
| 265 | */ |
| 266 | public static void prepareWebViewInSystemServer() { |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 267 | String[] nativePaths = null; |
| 268 | try { |
| 269 | nativePaths = getWebViewNativeLibraryPaths(); |
Torne (Richard Coles) | 27cb0d2 | 2014-08-08 18:24:12 +0100 | [diff] [blame] | 270 | } catch (Throwable t) { |
| 271 | // Log and discard errors at this stage as we must not crash the system server. |
| 272 | Log.e(LOGTAG, "error preparing webview native library", t); |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 273 | } |
| 274 | prepareWebViewInSystemServer(nativePaths); |
| 275 | } |
| 276 | |
| 277 | private static void prepareWebViewInSystemServer(String[] nativeLibraryPaths) { |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 278 | if (DEBUG) Log.v(LOGTAG, "creating relro files"); |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 279 | |
| 280 | // We must always trigger createRelRo regardless of the value of nativeLibraryPaths. Any |
| 281 | // unexpected values will be handled there to ensure that we trigger notifying any process |
| 282 | // waiting on relreo creation. |
| 283 | if (Build.SUPPORTED_32_BIT_ABIS.length > 0) { |
| 284 | if (DEBUG) Log.v(LOGTAG, "Create 32 bit relro"); |
| 285 | createRelroFile(false /* is64Bit */, nativeLibraryPaths); |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 286 | } |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 287 | |
| 288 | if (Build.SUPPORTED_64_BIT_ABIS.length > 0) { |
| 289 | if (DEBUG) Log.v(LOGTAG, "Create 64 bit relro"); |
| 290 | createRelroFile(true /* is64Bit */, nativeLibraryPaths); |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 294 | public static void onWebViewUpdateInstalled() { |
| 295 | String[] nativeLibs = null; |
| 296 | try { |
| 297 | nativeLibs = WebViewFactory.getWebViewNativeLibraryPaths(); |
Torne (Richard Coles) | 27cb0d2 | 2014-08-08 18:24:12 +0100 | [diff] [blame] | 298 | if (nativeLibs != null) { |
| 299 | long newVmSize = 0L; |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 300 | |
Torne (Richard Coles) | 27cb0d2 | 2014-08-08 18:24:12 +0100 | [diff] [blame] | 301 | for (String path : nativeLibs) { |
| 302 | if (DEBUG) Log.d(LOGTAG, "Checking file size of " + path); |
| 303 | if (path == null) continue; |
| 304 | File f = new File(path); |
| 305 | if (f.exists()) { |
| 306 | long length = f.length(); |
| 307 | if (length > newVmSize) { |
| 308 | newVmSize = length; |
| 309 | } |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 310 | } |
| 311 | } |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 312 | |
Torne (Richard Coles) | 27cb0d2 | 2014-08-08 18:24:12 +0100 | [diff] [blame] | 313 | if (DEBUG) { |
| 314 | Log.v(LOGTAG, "Based on library size, need " + newVmSize + |
| 315 | " bytes of address space."); |
| 316 | } |
| 317 | // The required memory can be larger than the file on disk (due to .bss), and an |
| 318 | // upgraded version of the library will likely be larger, so always attempt to |
| 319 | // reserve twice as much as we think to allow for the library to grow during this |
| 320 | // boot cycle. |
| 321 | newVmSize = Math.max(2 * newVmSize, CHROMIUM_WEBVIEW_DEFAULT_VMSIZE_BYTES); |
| 322 | Log.d(LOGTAG, "Setting new address space to " + newVmSize); |
| 323 | SystemProperties.set(CHROMIUM_WEBVIEW_VMSIZE_SIZE_PROPERTY, |
| 324 | Long.toString(newVmSize)); |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 325 | } |
Torne (Richard Coles) | 27cb0d2 | 2014-08-08 18:24:12 +0100 | [diff] [blame] | 326 | } catch (Throwable t) { |
| 327 | // Log and discard errors at this stage as we must not crash the system server. |
| 328 | Log.e(LOGTAG, "error preparing webview native library", t); |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 329 | } |
| 330 | prepareWebViewInSystemServer(nativeLibs); |
| 331 | } |
| 332 | |
Gustav Sennton | a8366e7 | 2015-04-17 11:24:07 +0100 | [diff] [blame] | 333 | // throws MissingWebViewPackageException |
| 334 | private static String[] getWebViewNativeLibraryPaths() { |
Gustav Sennton | 2ed6fee | 2015-03-03 15:12:34 +0000 | [diff] [blame] | 335 | ApplicationInfo ai = getWebViewApplicationInfo(); |
| 336 | final String NATIVE_LIB_FILE_NAME = getWebViewLibrary(ai); |
Primiano Tucci | 1b7977b | 2014-07-25 19:19:32 +0100 | [diff] [blame] | 337 | |
| 338 | String path32; |
| 339 | String path64; |
| 340 | boolean primaryArchIs64bit = VMRuntime.is64BitAbi(ai.primaryCpuAbi); |
| 341 | if (!TextUtils.isEmpty(ai.secondaryCpuAbi)) { |
| 342 | // Multi-arch case. |
| 343 | if (primaryArchIs64bit) { |
| 344 | // Primary arch: 64-bit, secondary: 32-bit. |
| 345 | path64 = ai.nativeLibraryDir; |
| 346 | path32 = ai.secondaryNativeLibraryDir; |
| 347 | } else { |
| 348 | // Primary arch: 32-bit, secondary: 64-bit. |
| 349 | path64 = ai.secondaryNativeLibraryDir; |
| 350 | path32 = ai.nativeLibraryDir; |
| 351 | } |
| 352 | } else if (primaryArchIs64bit) { |
| 353 | // Single-arch 64-bit. |
| 354 | path64 = ai.nativeLibraryDir; |
| 355 | path32 = ""; |
| 356 | } else { |
| 357 | // Single-arch 32-bit. |
| 358 | path32 = ai.nativeLibraryDir; |
| 359 | path64 = ""; |
| 360 | } |
| 361 | if (!TextUtils.isEmpty(path32)) path32 += "/" + NATIVE_LIB_FILE_NAME; |
| 362 | if (!TextUtils.isEmpty(path64)) path64 += "/" + NATIVE_LIB_FILE_NAME; |
| 363 | return new String[] { path32, path64 }; |
| 364 | } |
| 365 | |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 366 | private static void createRelroFile(final boolean is64Bit, String[] nativeLibraryPaths) { |
Primiano Tucci | 1b7977b | 2014-07-25 19:19:32 +0100 | [diff] [blame] | 367 | final String abi = |
| 368 | is64Bit ? Build.SUPPORTED_64_BIT_ABIS[0] : Build.SUPPORTED_32_BIT_ABIS[0]; |
Primiano Tucci | 810c052 | 2014-07-25 18:03:16 +0100 | [diff] [blame] | 369 | |
| 370 | // crashHandler is invoked by the ActivityManagerService when the isolated process crashes. |
| 371 | Runnable crashHandler = new Runnable() { |
| 372 | @Override |
| 373 | public void run() { |
| 374 | try { |
Primiano Tucci | 1b7977b | 2014-07-25 19:19:32 +0100 | [diff] [blame] | 375 | Log.e(LOGTAG, "relro file creator for " + abi + " crashed. Proceeding without"); |
Primiano Tucci | 810c052 | 2014-07-25 18:03:16 +0100 | [diff] [blame] | 376 | getUpdateService().notifyRelroCreationCompleted(is64Bit, false); |
| 377 | } catch (RemoteException e) { |
| 378 | Log.e(LOGTAG, "Cannot reach WebViewUpdateService. " + e.getMessage()); |
| 379 | } |
| 380 | } |
| 381 | }; |
| 382 | |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 383 | try { |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 384 | if (nativeLibraryPaths == null |
| 385 | || nativeLibraryPaths[0] == null || nativeLibraryPaths[1] == null) { |
| 386 | throw new IllegalArgumentException( |
| 387 | "Native library paths to the WebView RelRo process must not be null!"); |
| 388 | } |
Primiano Tucci | 161536b | 2014-07-28 18:51:45 +0100 | [diff] [blame] | 389 | int pid = LocalServices.getService(ActivityManagerInternal.class).startIsolatedProcess( |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 390 | RelroFileCreator.class.getName(), nativeLibraryPaths, "WebViewLoader-" + abi, abi, |
Primiano Tucci | 810c052 | 2014-07-25 18:03:16 +0100 | [diff] [blame] | 391 | Process.SHARED_RELRO_UID, crashHandler); |
Primiano Tucci | e76e81a | 2014-07-29 16:38:33 +0100 | [diff] [blame] | 392 | if (pid <= 0) throw new Exception("Failed to start the relro file creator process"); |
Primiano Tucci | 810c052 | 2014-07-25 18:03:16 +0100 | [diff] [blame] | 393 | } catch (Throwable t) { |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 394 | // Log and discard errors as we must not crash the system server. |
Primiano Tucci | 810c052 | 2014-07-25 18:03:16 +0100 | [diff] [blame] | 395 | Log.e(LOGTAG, "error starting relro file creator for abi " + abi, t); |
| 396 | crashHandler.run(); |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
| 400 | private static class RelroFileCreator { |
| 401 | // Called in an unprivileged child process to create the relro file. |
| 402 | public static void main(String[] args) { |
Primiano Tucci | 161536b | 2014-07-28 18:51:45 +0100 | [diff] [blame] | 403 | boolean result = false; |
| 404 | boolean is64Bit = VMRuntime.getRuntime().is64Bit(); |
Primiano Tucci | 1b7977b | 2014-07-25 19:19:32 +0100 | [diff] [blame] | 405 | try{ |
| 406 | if (args.length != 2 || args[0] == null || args[1] == null) { |
| 407 | Log.e(LOGTAG, "Invalid RelroFileCreator args: " + Arrays.toString(args)); |
| 408 | return; |
| 409 | } |
Primiano Tucci | 1b7977b | 2014-07-25 19:19:32 +0100 | [diff] [blame] | 410 | Log.v(LOGTAG, "RelroFileCreator (64bit = " + is64Bit + "), " + |
| 411 | " 32-bit lib: " + args[0] + ", 64-bit lib: " + args[1]); |
| 412 | if (!sAddressSpaceReserved) { |
| 413 | Log.e(LOGTAG, "can't create relro file; address space not reserved"); |
| 414 | return; |
| 415 | } |
Primiano Tucci | 161536b | 2014-07-28 18:51:45 +0100 | [diff] [blame] | 416 | result = nativeCreateRelroFile(args[0] /* path32 */, |
| 417 | args[1] /* path64 */, |
| 418 | CHROMIUM_WEBVIEW_NATIVE_RELRO_32, |
| 419 | CHROMIUM_WEBVIEW_NATIVE_RELRO_64); |
Primiano Tucci | e76e81a | 2014-07-29 16:38:33 +0100 | [diff] [blame] | 420 | if (result && DEBUG) Log.v(LOGTAG, "created relro file"); |
Primiano Tucci | 161536b | 2014-07-28 18:51:45 +0100 | [diff] [blame] | 421 | } finally { |
| 422 | // We must do our best to always notify the update service, even if something fails. |
Primiano Tucci | 1b7977b | 2014-07-25 19:19:32 +0100 | [diff] [blame] | 423 | try { |
| 424 | getUpdateService().notifyRelroCreationCompleted(is64Bit, result); |
| 425 | } catch (RemoteException e) { |
| 426 | Log.e(LOGTAG, "error notifying update service", e); |
| 427 | } |
Primiano Tucci | 161536b | 2014-07-28 18:51:45 +0100 | [diff] [blame] | 428 | |
| 429 | if (!result) Log.e(LOGTAG, "failed to create relro file"); |
| 430 | |
Primiano Tucci | 1b7977b | 2014-07-25 19:19:32 +0100 | [diff] [blame] | 431 | // Must explicitly exit or else this process will just sit around after we return. |
| 432 | System.exit(0); |
| 433 | } |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
| 437 | private static void loadNativeLibrary() { |
| 438 | if (!sAddressSpaceReserved) { |
| 439 | Log.e(LOGTAG, "can't load with relro file; address space not reserved"); |
| 440 | return; |
| 441 | } |
| 442 | |
| 443 | try { |
| 444 | getUpdateService().waitForRelroCreationCompleted(VMRuntime.getRuntime().is64Bit()); |
| 445 | } catch (RemoteException e) { |
| 446 | Log.e(LOGTAG, "error waiting for relro creation, proceeding without", e); |
| 447 | return; |
| 448 | } |
| 449 | |
Primiano Tucci | 1b7977b | 2014-07-25 19:19:32 +0100 | [diff] [blame] | 450 | try { |
| 451 | String[] args = getWebViewNativeLibraryPaths(); |
| 452 | boolean result = nativeLoadWithRelroFile(args[0] /* path32 */, |
| 453 | args[1] /* path64 */, |
| 454 | CHROMIUM_WEBVIEW_NATIVE_RELRO_32, |
| 455 | CHROMIUM_WEBVIEW_NATIVE_RELRO_64); |
| 456 | if (!result) { |
| 457 | Log.w(LOGTAG, "failed to load with relro file, proceeding without"); |
| 458 | } else if (DEBUG) { |
| 459 | Log.v(LOGTAG, "loaded with relro file"); |
| 460 | } |
Gustav Sennton | a8366e7 | 2015-04-17 11:24:07 +0100 | [diff] [blame] | 461 | } catch (MissingWebViewPackageException e) { |
Primiano Tucci | 1b7977b | 2014-07-25 19:19:32 +0100 | [diff] [blame] | 462 | Log.e(LOGTAG, "Failed to list WebView package libraries for loadNativeLibrary", e); |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | |
| 466 | private static IWebViewUpdateService getUpdateService() { |
| 467 | return IWebViewUpdateService.Stub.asInterface(ServiceManager.getService("webviewupdate")); |
| 468 | } |
| 469 | |
Ben Murdoch | 5ced502 | 2014-07-28 15:57:00 +0100 | [diff] [blame] | 470 | private static native boolean nativeReserveAddressSpace(long addressSpaceToReserve); |
Torne (Richard Coles) | 08cfaf6 | 2014-05-08 16:07:05 +0100 | [diff] [blame] | 471 | private static native boolean nativeCreateRelroFile(String lib32, String lib64, |
| 472 | String relro32, String relro64); |
| 473 | private static native boolean nativeLoadWithRelroFile(String lib32, String lib64, |
| 474 | String relro32, String relro64); |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 475 | } |