blob: 0ac57319d5deb70cecb77045fdf8cd4184356cb9 [file] [log] [blame]
Jonathan Dixond3101b12012-04-12 20:51:51 +01001/*
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
17package android.webkit;
18
Ignacio Solla451e3382014-11-10 10:35:54 +000019import android.annotation.SystemApi;
Primiano Tucci810c0522014-07-25 18:03:16 +010020import android.app.ActivityManagerInternal;
Torne (Richard Coles)6c778ce2014-07-17 14:14:48 -070021import android.app.AppGlobals;
Jeff Sharkey85844912014-11-13 16:20:38 -080022import android.app.Application;
Torne (Richard Coles)6c778ce2014-07-17 14:14:48 -070023import android.content.Context;
Primiano Tucci1b7977b2014-07-25 19:19:32 +010024import android.content.pm.ApplicationInfo;
Torne (Richard Coles)0606cd52014-08-05 16:12:09 +010025import android.content.pm.PackageInfo;
Torne (Richard Coles)6c778ce2014-07-17 14:14:48 -070026import android.content.pm.PackageManager;
Gustav Sennton6258dcd2015-10-30 19:25:37 +000027import android.content.res.XmlResourceParser;
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010028import android.os.Build;
29import android.os.Process;
30import android.os.RemoteException;
31import android.os.ServiceManager;
Ben Murdoche09e9762012-07-19 14:48:13 +010032import android.os.StrictMode;
Ben Murdoch5ced5022014-07-28 15:57:00 +010033import android.os.SystemProperties;
Torne (Richard Coles)38228822014-08-13 17:11:45 +010034import android.os.Trace;
Gustav Sennton6258dcd2015-10-30 19:25:37 +000035import android.provider.Settings;
36import android.provider.Settings.Secure;
Primiano Tucci1b7977b2014-07-25 19:19:32 +010037import android.text.TextUtils;
Torne (Richard Coles)03ce9b32013-06-12 16:02:03 +010038import android.util.AndroidRuntimeException;
Jonathan Dixond3101b12012-04-12 20:51:51 +010039import android.util.Log;
Jeff Sharkey85844912014-11-13 16:20:38 -080040
Gustav Sennton6258dcd2015-10-30 19:25:37 +000041import com.android.internal.util.XmlUtils;
Primiano Tucci810c0522014-07-25 18:03:16 +010042import com.android.server.LocalServices;
Jeff Sharkey85844912014-11-13 16:20:38 -080043
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010044import dalvik.system.VMRuntime;
45
46import java.io.File;
Simon Baldwinb98082dc2015-05-15 12:56:50 +010047import java.io.IOException;
Gustav Sennton6258dcd2015-10-30 19:25:37 +000048import java.util.ArrayList;
Primiano Tucci1b7977b2014-07-25 19:19:32 +010049import java.util.Arrays;
Gustav Sennton6258dcd2015-10-30 19:25:37 +000050import java.util.List;
Simon Baldwinb98082dc2015-05-15 12:56:50 +010051import java.util.zip.ZipEntry;
52import java.util.zip.ZipFile;
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010053
Gustav Sennton6258dcd2015-10-30 19:25:37 +000054import org.xmlpull.v1.XmlPullParserException;
55
Jonathan Dixond3101b12012-04-12 20:51:51 +010056/**
57 * Top level factory, used creating all the main WebView implementation classes.
Jared Dukeb0e35842013-03-19 16:25:39 -070058 *
59 * @hide
Jonathan Dixond3101b12012-04-12 20:51:51 +010060 */
Ignacio Solla451e3382014-11-10 10:35:54 +000061@SystemApi
Jared Dukeb0e35842013-03-19 16:25:39 -070062public final class WebViewFactory {
Jonathan Dixona7eaa8e2013-07-25 19:52:47 -070063
Ben Murdoche09e9762012-07-19 14:48:13 +010064 private static final String CHROMIUM_WEBVIEW_FACTORY =
Torne (Richard Coles)a9bbd942012-10-24 11:59:22 +010065 "com.android.webview.chromium.WebViewChromiumFactoryProvider";
Jonathan Dixond3101b12012-04-12 20:51:51 +010066
Ben Murdoch0e04bcf2014-05-16 13:41:12 +010067 private static final String NULL_WEBVIEW_FACTORY =
68 "com.android.webview.nullwebview.NullWebViewFactoryProvider";
69
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010070 private static final String CHROMIUM_WEBVIEW_NATIVE_RELRO_32 =
71 "/data/misc/shared_relro/libwebviewchromium32.relro";
72 private static final String CHROMIUM_WEBVIEW_NATIVE_RELRO_64 =
73 "/data/misc/shared_relro/libwebviewchromium64.relro";
74
Ben Murdoch5ced5022014-07-28 15:57:00 +010075 public static final String CHROMIUM_WEBVIEW_VMSIZE_SIZE_PROPERTY =
76 "persist.sys.webview.vmsize";
77 private static final long CHROMIUM_WEBVIEW_DEFAULT_VMSIZE_BYTES = 100 * 1024 * 1024;
Primiano Tucci1b7977b2014-07-25 19:19:32 +010078
Jonathan Dixond3101b12012-04-12 20:51:51 +010079 private static final String LOGTAG = "WebViewFactory";
80
81 private static final boolean DEBUG = false;
82
83 // Cache the factory both for efficiency, and ensure any one process gets all webviews from the
84 // same provider.
85 private static WebViewFactoryProvider sProviderInstance;
John Reck9f9d3452012-09-20 13:18:59 -070086 private static final Object sProviderLock = new Object();
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010087 private static boolean sAddressSpaceReserved = false;
Torne (Richard Coles)84392d72014-08-14 16:43:18 +010088 private static PackageInfo sPackageInfo;
Jonathan Dixond3101b12012-04-12 20:51:51 +010089
Gustav Sennton85edb6c2015-04-15 11:54:20 +010090 // Error codes for loadWebViewNativeLibraryFromPackage
91 public static final int LIBLOAD_SUCCESS = 0;
92 public static final int LIBLOAD_WRONG_PACKAGE_NAME = 1;
93 public static final int LIBLOAD_ADDRESS_SPACE_NOT_RESERVED = 2;
Gustav Sennton6258dcd2015-10-30 19:25:37 +000094
95 // error codes for waiting for WebView preparation
Gustav Sennton85edb6c2015-04-15 11:54:20 +010096 public static final int LIBLOAD_FAILED_WAITING_FOR_RELRO = 3;
97 public static final int LIBLOAD_FAILED_LISTING_WEBVIEW_PACKAGES = 4;
98
99 // native relro loading error codes
100 public static final int LIBLOAD_FAILED_TO_OPEN_RELRO_FILE = 5;
101 public static final int LIBLOAD_FAILED_TO_LOAD_LIBRARY = 6;
102 public static final int LIBLOAD_FAILED_JNI_CALL = 7;
103
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000104 // more error codes for waiting for WebView preparation
Gustav Sennton26c82ff2016-03-11 13:06:40 +0000105 public static final int LIBLOAD_FAILED_WAITING_FOR_WEBVIEW_REASON_UNKNOWN = 8;
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000106
Torne (Richard Coles)1a904122016-03-14 13:45:55 +0000107 // error for namespace lookup
108 public static final int LIBLOAD_FAILED_TO_FIND_NAMESPACE = 10;
109
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000110 private static String getWebViewPreparationErrorReason(int error) {
111 switch (error) {
112 case LIBLOAD_FAILED_WAITING_FOR_RELRO:
113 return "Time out waiting for Relro files being created";
114 case LIBLOAD_FAILED_LISTING_WEBVIEW_PACKAGES:
115 return "No WebView installed";
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000116 case LIBLOAD_FAILED_WAITING_FOR_WEBVIEW_REASON_UNKNOWN:
117 return "Crashed for unknown reason";
118 }
119 return "Unknown";
120 }
121
122 /**
123 * @hide
124 */
125 public static class MissingWebViewPackageException extends AndroidRuntimeException {
Gustav Senntona8366e72015-04-17 11:24:07 +0100126 public MissingWebViewPackageException(String message) { super(message); }
127 public MissingWebViewPackageException(Exception e) { super(e); }
128 }
129
Gustav Sennton6ce92c92015-10-23 11:10:39 +0100130 /**
Gustav Sennton6ce92c92015-10-23 11:10:39 +0100131 * @hide
132 */
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000133 public static String getWebViewLibrary(ApplicationInfo ai) {
Gustav Sennton2ed6fee2015-03-03 15:12:34 +0000134 if (ai.metaData != null)
135 return ai.metaData.getString("com.android.webview.WebViewLibrary");
136 return null;
Ben Murdochdc00a842014-07-17 14:55:00 +0100137 }
138
Torne (Richard Coles)84392d72014-08-14 16:43:18 +0100139 public static PackageInfo getLoadedPackageInfo() {
140 return sPackageInfo;
141 }
142
Gustav Sennton85edb6c2015-04-15 11:54:20 +0100143 /**
144 * Load the native library for the given package name iff that package
Gustav Senntond97301822015-06-18 16:56:26 +0100145 * name is the same as the one providing the webview.
Gustav Sennton85edb6c2015-04-15 11:54:20 +0100146 */
Torne (Richard Coles)1a904122016-03-14 13:45:55 +0000147 public static int loadWebViewNativeLibraryFromPackage(String packageName,
148 ClassLoader clazzLoader) {
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000149 int ret = waitForProviderAndSetPackageInfo();
150 if (ret != LIBLOAD_SUCCESS) {
151 return ret;
Gustav Sennton6ce92c92015-10-23 11:10:39 +0100152 }
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000153 if (!sPackageInfo.packageName.equals(packageName))
154 return LIBLOAD_WRONG_PACKAGE_NAME;
Gustav Sennton6ce92c92015-10-23 11:10:39 +0100155
Torne (Richard Coles)1a904122016-03-14 13:45:55 +0000156 return loadNativeLibrary(clazzLoader);
Gustav Sennton85edb6c2015-04-15 11:54:20 +0100157 }
158
John Reck9f9d3452012-09-20 13:18:59 -0700159 static WebViewFactoryProvider getProvider() {
160 synchronized (sProviderLock) {
161 // For now the main purpose of this function (and the factory abstraction) is to keep
Torne (Richard Coles)d892afc2013-10-14 17:14:04 +0100162 // us honest and minimize usage of WebView internals when binding the proxy.
John Reck9f9d3452012-09-20 13:18:59 -0700163 if (sProviderInstance != null) return sProviderInstance;
Jonathan Dixond3101b12012-04-12 20:51:51 +0100164
Jeff Sharkey85844912014-11-13 16:20:38 -0800165 final int uid = android.os.Process.myUid();
166 if (uid == android.os.Process.ROOT_UID || uid == android.os.Process.SYSTEM_UID) {
167 throw new UnsupportedOperationException(
168 "For security reasons, WebView is not allowed in privileged processes");
169 }
170
Gustav Sennton5df5e222016-02-25 18:20:12 +0000171 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
Torne (Richard Coles)38228822014-08-13 17:11:45 +0100172 Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "WebViewFactory.getProvider()");
Torne (Richard Coles)03ce9b32013-06-12 16:02:03 +0100173 try {
Gustav Senntona8366e72015-04-17 11:24:07 +0100174 Class<WebViewFactoryProvider> providerClass = getProviderClass();
Torne (Richard Coles)38228822014-08-13 17:11:45 +0100175
Torne (Richard Coles)38228822014-08-13 17:11:45 +0100176 Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "providerClass.newInstance()");
177 try {
Ignacio Solla1ea39b32014-11-10 14:08:37 +0000178 sProviderInstance = providerClass.getConstructor(WebViewDelegate.class)
179 .newInstance(new WebViewDelegate());
Torne (Richard Coles)38228822014-08-13 17:11:45 +0100180 if (DEBUG) Log.v(LOGTAG, "Loaded provider: " + sProviderInstance);
181 return sProviderInstance;
182 } catch (Exception e) {
183 Log.e(LOGTAG, "error instantiating provider", e);
184 throw new AndroidRuntimeException(e);
185 } finally {
186 Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
Torne (Richard Coles)38228822014-08-13 17:11:45 +0100187 }
Torne (Richard Coles)03ce9b32013-06-12 16:02:03 +0100188 } finally {
Torne (Richard Coles)38228822014-08-13 17:11:45 +0100189 Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
Gustav Sennton5df5e222016-02-25 18:20:12 +0000190 StrictMode.setThreadPolicy(oldPolicy);
Torne (Richard Coles)03ce9b32013-06-12 16:02:03 +0100191 }
Jonathan Dixond3101b12012-04-12 20:51:51 +0100192 }
Jonathan Dixond3101b12012-04-12 20:51:51 +0100193 }
194
Gustav Senntona8366e72015-04-17 11:24:07 +0100195 private static Class<WebViewFactoryProvider> getProviderClass() {
196 try {
Gustav Senntonfc424472016-01-06 17:11:09 +0000197 Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW,
198 "WebViewFactory.waitForProviderAndSetPackageInfo()");
199 try {
200 // First fetch the package info so we can log the webview package version.
201 int res = waitForProviderAndSetPackageInfo();
202 if (res != LIBLOAD_SUCCESS) {
203 throw new MissingWebViewPackageException(
204 "Failed to load WebView provider, error: "
205 + getWebViewPreparationErrorReason(res));
206 }
207 } finally {
208 Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000209 }
Gustav Senntona8366e72015-04-17 11:24:07 +0100210 Log.i(LOGTAG, "Loading " + sPackageInfo.packageName + " version " +
211 sPackageInfo.versionName + " (code " + sPackageInfo.versionCode + ")");
212
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000213 Application initialApplication = AppGlobals.getInitialApplication();
214 Context webViewContext = null;
Gustav Senntonb1e45cd2016-01-22 11:25:34 +0000215 Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "PackageManager.getApplicationInfo()");
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000216 try {
217 // Construct a package context to load the Java code into the current app.
218 // This is done as early as possible since by constructing a package context we
219 // register the WebView package as a dependency for the current application so that
220 // when the WebView package is updated this application will be killed.
Gustav Senntonb1e45cd2016-01-22 11:25:34 +0000221 ApplicationInfo applicationInfo =
222 initialApplication.getPackageManager().getApplicationInfo(
223 sPackageInfo.packageName, PackageManager.GET_SHARED_LIBRARY_FILES
224 | PackageManager.MATCH_DEBUG_TRIAGED_MISSING
225 // make sure that we fetch the current provider even if its not installed
226 // for the current user
227 | PackageManager.MATCH_UNINSTALLED_PACKAGES);
228 Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW,
229 "initialApplication.createApplicationContext");
230 try {
231 webViewContext = initialApplication.createApplicationContext(applicationInfo,
232 Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);
233 } finally {
234 Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
235 }
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000236 } catch (PackageManager.NameNotFoundException e) {
237 throw new MissingWebViewPackageException(e);
Gustav Senntonfc424472016-01-06 17:11:09 +0000238 } finally {
239 Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000240 }
241
Gustav Senntona8366e72015-04-17 11:24:07 +0100242 Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "WebViewFactory.getChromiumProviderClass()");
243 try {
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000244 initialApplication.getAssets().addAssetPathAsSharedLibrary(
245 webViewContext.getApplicationInfo().sourceDir);
246 ClassLoader clazzLoader = webViewContext.getClassLoader();
Torne (Richard Coles)1a904122016-03-14 13:45:55 +0000247
248 Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "WebViewFactory.loadNativeLibrary()");
249 loadNativeLibrary(clazzLoader);
250 Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
251
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000252 Trace.traceBegin(Trace.TRACE_TAG_WEBVIEW, "Class.forName()");
253 try {
254 return (Class<WebViewFactoryProvider>) Class.forName(CHROMIUM_WEBVIEW_FACTORY,
255 true, clazzLoader);
256 } finally {
257 Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
258 }
Gustav Senntona8366e72015-04-17 11:24:07 +0100259 } catch (ClassNotFoundException e) {
260 Log.e(LOGTAG, "error loading provider", e);
261 throw new AndroidRuntimeException(e);
262 } finally {
263 Trace.traceEnd(Trace.TRACE_TAG_WEBVIEW);
264 }
265 } catch (MissingWebViewPackageException e) {
266 // If the package doesn't exist, then try loading the null WebView instead.
267 // If that succeeds, then this is a device without WebView support; if it fails then
268 // swallow the failure, complain that the real WebView is missing and rethrow the
269 // original exception.
270 try {
271 return (Class<WebViewFactoryProvider>) Class.forName(NULL_WEBVIEW_FACTORY);
272 } catch (ClassNotFoundException e2) {
273 // Ignore.
274 }
275 Log.e(LOGTAG, "Chromium WebView package does not exist", e);
276 throw new AndroidRuntimeException(e);
277 }
278 }
279
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100280 /**
281 * Perform any WebView loading preparations that must happen in the zygote.
282 * Currently, this means allocating address space to load the real JNI library later.
283 */
284 public static void prepareWebViewInZygote() {
285 try {
286 System.loadLibrary("webviewchromium_loader");
Ben Murdoch5ced5022014-07-28 15:57:00 +0100287 long addressSpaceToReserve =
288 SystemProperties.getLong(CHROMIUM_WEBVIEW_VMSIZE_SIZE_PROPERTY,
289 CHROMIUM_WEBVIEW_DEFAULT_VMSIZE_BYTES);
290 sAddressSpaceReserved = nativeReserveAddressSpace(addressSpaceToReserve);
291
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100292 if (sAddressSpaceReserved) {
Ben Murdoch5ced5022014-07-28 15:57:00 +0100293 if (DEBUG) {
294 Log.v(LOGTAG, "address space reserved: " + addressSpaceToReserve + " bytes");
295 }
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100296 } else {
Ben Murdoch5ced5022014-07-28 15:57:00 +0100297 Log.e(LOGTAG, "reserving " + addressSpaceToReserve +
298 " bytes of address space failed");
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100299 }
Primiano Tucci810c0522014-07-25 18:03:16 +0100300 } catch (Throwable t) {
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100301 // Log and discard errors at this stage as we must not crash the zygote.
Primiano Tucci810c0522014-07-25 18:03:16 +0100302 Log.e(LOGTAG, "error preparing native loader", t);
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100303 }
304 }
305
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000306 private static int prepareWebViewInSystemServer(String[] nativeLibraryPaths) {
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100307 if (DEBUG) Log.v(LOGTAG, "creating relro files");
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000308 int numRelros = 0;
Ben Murdoch5ced5022014-07-28 15:57:00 +0100309
310 // We must always trigger createRelRo regardless of the value of nativeLibraryPaths. Any
311 // unexpected values will be handled there to ensure that we trigger notifying any process
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000312 // waiting on relro creation.
Ben Murdoch5ced5022014-07-28 15:57:00 +0100313 if (Build.SUPPORTED_32_BIT_ABIS.length > 0) {
314 if (DEBUG) Log.v(LOGTAG, "Create 32 bit relro");
315 createRelroFile(false /* is64Bit */, nativeLibraryPaths);
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000316 numRelros++;
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100317 }
Ben Murdoch5ced5022014-07-28 15:57:00 +0100318
319 if (Build.SUPPORTED_64_BIT_ABIS.length > 0) {
320 if (DEBUG) Log.v(LOGTAG, "Create 64 bit relro");
321 createRelroFile(true /* is64Bit */, nativeLibraryPaths);
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000322 numRelros++;
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100323 }
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000324 return numRelros;
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100325 }
326
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000327 /**
328 * @hide
329 */
330 public static int onWebViewProviderChanged(PackageInfo packageInfo) {
Ben Murdoch5ced5022014-07-28 15:57:00 +0100331 String[] nativeLibs = null;
332 try {
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000333 nativeLibs = WebViewFactory.getWebViewNativeLibraryPaths(packageInfo);
Torne (Richard Coles)27cb0d22014-08-08 18:24:12 +0100334 if (nativeLibs != null) {
335 long newVmSize = 0L;
Ben Murdoch5ced5022014-07-28 15:57:00 +0100336
Torne (Richard Coles)27cb0d22014-08-08 18:24:12 +0100337 for (String path : nativeLibs) {
Simon Baldwinb98082dc2015-05-15 12:56:50 +0100338 if (path == null || TextUtils.isEmpty(path)) continue;
Torne (Richard Coles)27cb0d22014-08-08 18:24:12 +0100339 if (DEBUG) Log.d(LOGTAG, "Checking file size of " + path);
Torne (Richard Coles)27cb0d22014-08-08 18:24:12 +0100340 File f = new File(path);
341 if (f.exists()) {
Simon Baldwinb98082dc2015-05-15 12:56:50 +0100342 newVmSize = Math.max(newVmSize, f.length());
343 continue;
344 }
Simon Baldwin519919b2015-06-11 17:09:49 +0100345 if (path.contains("!/")) {
346 String[] split = TextUtils.split(path, "!/");
Simon Baldwinb98082dc2015-05-15 12:56:50 +0100347 if (split.length == 2) {
Torne (Richard Coles)c5283922015-08-10 16:40:50 +0100348 try (ZipFile z = new ZipFile(split[0])) {
Simon Baldwinb98082dc2015-05-15 12:56:50 +0100349 ZipEntry e = z.getEntry(split[1]);
350 if (e != null && e.getMethod() == ZipEntry.STORED) {
351 newVmSize = Math.max(newVmSize, e.getSize());
352 continue;
353 }
354 }
355 catch (IOException e) {
356 Log.e(LOGTAG, "error reading APK file " + split[0] + ", ", e);
357 }
Torne (Richard Coles)27cb0d22014-08-08 18:24:12 +0100358 }
Ben Murdoch5ced5022014-07-28 15:57:00 +0100359 }
Simon Baldwinb98082dc2015-05-15 12:56:50 +0100360 Log.e(LOGTAG, "error sizing load for " + path);
Ben Murdoch5ced5022014-07-28 15:57:00 +0100361 }
Ben Murdoch5ced5022014-07-28 15:57:00 +0100362
Torne (Richard Coles)27cb0d22014-08-08 18:24:12 +0100363 if (DEBUG) {
364 Log.v(LOGTAG, "Based on library size, need " + newVmSize +
365 " bytes of address space.");
366 }
367 // The required memory can be larger than the file on disk (due to .bss), and an
368 // upgraded version of the library will likely be larger, so always attempt to
369 // reserve twice as much as we think to allow for the library to grow during this
370 // boot cycle.
371 newVmSize = Math.max(2 * newVmSize, CHROMIUM_WEBVIEW_DEFAULT_VMSIZE_BYTES);
372 Log.d(LOGTAG, "Setting new address space to " + newVmSize);
373 SystemProperties.set(CHROMIUM_WEBVIEW_VMSIZE_SIZE_PROPERTY,
374 Long.toString(newVmSize));
Ben Murdoch5ced5022014-07-28 15:57:00 +0100375 }
Torne (Richard Coles)27cb0d22014-08-08 18:24:12 +0100376 } catch (Throwable t) {
377 // Log and discard errors at this stage as we must not crash the system server.
378 Log.e(LOGTAG, "error preparing webview native library", t);
Ben Murdoch5ced5022014-07-28 15:57:00 +0100379 }
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000380 return prepareWebViewInSystemServer(nativeLibs);
Ben Murdoch5ced5022014-07-28 15:57:00 +0100381 }
382
Gustav Senntona8366e72015-04-17 11:24:07 +0100383 // throws MissingWebViewPackageException
Simon Baldwinb98082dc2015-05-15 12:56:50 +0100384 private static String getLoadFromApkPath(String apkPath,
385 String[] abiList,
386 String nativeLibFileName) {
387 // Search the APK for a native library conforming to a listed ABI.
Torne (Richard Coles)c5283922015-08-10 16:40:50 +0100388 try (ZipFile z = new ZipFile(apkPath)) {
Simon Baldwinb98082dc2015-05-15 12:56:50 +0100389 for (String abi : abiList) {
390 final String entry = "lib/" + abi + "/" + nativeLibFileName;
391 ZipEntry e = z.getEntry(entry);
392 if (e != null && e.getMethod() == ZipEntry.STORED) {
393 // Return a path formatted for dlopen() load from APK.
Simon Baldwin519919b2015-06-11 17:09:49 +0100394 return apkPath + "!/" + entry;
Simon Baldwinb98082dc2015-05-15 12:56:50 +0100395 }
396 }
397 } catch (IOException e) {
398 throw new MissingWebViewPackageException(e);
399 }
400 return "";
401 }
402
403 // throws MissingWebViewPackageException
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000404 private static String[] getWebViewNativeLibraryPaths(PackageInfo packageInfo) {
405 ApplicationInfo ai = packageInfo.applicationInfo;
Gustav Sennton2ed6fee2015-03-03 15:12:34 +0000406 final String NATIVE_LIB_FILE_NAME = getWebViewLibrary(ai);
Primiano Tucci1b7977b2014-07-25 19:19:32 +0100407
408 String path32;
409 String path64;
410 boolean primaryArchIs64bit = VMRuntime.is64BitAbi(ai.primaryCpuAbi);
411 if (!TextUtils.isEmpty(ai.secondaryCpuAbi)) {
412 // Multi-arch case.
413 if (primaryArchIs64bit) {
414 // Primary arch: 64-bit, secondary: 32-bit.
415 path64 = ai.nativeLibraryDir;
416 path32 = ai.secondaryNativeLibraryDir;
417 } else {
418 // Primary arch: 32-bit, secondary: 64-bit.
419 path64 = ai.secondaryNativeLibraryDir;
420 path32 = ai.nativeLibraryDir;
421 }
422 } else if (primaryArchIs64bit) {
423 // Single-arch 64-bit.
424 path64 = ai.nativeLibraryDir;
425 path32 = "";
426 } else {
427 // Single-arch 32-bit.
428 path32 = ai.nativeLibraryDir;
429 path64 = "";
430 }
Simon Baldwinb98082dc2015-05-15 12:56:50 +0100431
432 // Form the full paths to the extracted native libraries.
433 // If libraries were not extracted, try load from APK paths instead.
434 if (!TextUtils.isEmpty(path32)) {
435 path32 += "/" + NATIVE_LIB_FILE_NAME;
436 File f = new File(path32);
437 if (!f.exists()) {
438 path32 = getLoadFromApkPath(ai.sourceDir,
439 Build.SUPPORTED_32_BIT_ABIS,
440 NATIVE_LIB_FILE_NAME);
441 }
442 }
443 if (!TextUtils.isEmpty(path64)) {
444 path64 += "/" + NATIVE_LIB_FILE_NAME;
445 File f = new File(path64);
446 if (!f.exists()) {
447 path64 = getLoadFromApkPath(ai.sourceDir,
448 Build.SUPPORTED_64_BIT_ABIS,
449 NATIVE_LIB_FILE_NAME);
450 }
451 }
452
453 if (DEBUG) Log.v(LOGTAG, "Native 32-bit lib: " + path32 + ", 64-bit lib: " + path64);
Primiano Tucci1b7977b2014-07-25 19:19:32 +0100454 return new String[] { path32, path64 };
455 }
456
Ben Murdoch5ced5022014-07-28 15:57:00 +0100457 private static void createRelroFile(final boolean is64Bit, String[] nativeLibraryPaths) {
Primiano Tucci1b7977b2014-07-25 19:19:32 +0100458 final String abi =
459 is64Bit ? Build.SUPPORTED_64_BIT_ABIS[0] : Build.SUPPORTED_32_BIT_ABIS[0];
Primiano Tucci810c0522014-07-25 18:03:16 +0100460
461 // crashHandler is invoked by the ActivityManagerService when the isolated process crashes.
462 Runnable crashHandler = new Runnable() {
463 @Override
464 public void run() {
465 try {
Primiano Tucci1b7977b2014-07-25 19:19:32 +0100466 Log.e(LOGTAG, "relro file creator for " + abi + " crashed. Proceeding without");
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000467 getUpdateService().notifyRelroCreationCompleted();
Primiano Tucci810c0522014-07-25 18:03:16 +0100468 } catch (RemoteException e) {
469 Log.e(LOGTAG, "Cannot reach WebViewUpdateService. " + e.getMessage());
470 }
471 }
472 };
473
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100474 try {
Ben Murdoch5ced5022014-07-28 15:57:00 +0100475 if (nativeLibraryPaths == null
476 || nativeLibraryPaths[0] == null || nativeLibraryPaths[1] == null) {
477 throw new IllegalArgumentException(
478 "Native library paths to the WebView RelRo process must not be null!");
479 }
Primiano Tucci161536b2014-07-28 18:51:45 +0100480 int pid = LocalServices.getService(ActivityManagerInternal.class).startIsolatedProcess(
Ben Murdoch5ced5022014-07-28 15:57:00 +0100481 RelroFileCreator.class.getName(), nativeLibraryPaths, "WebViewLoader-" + abi, abi,
Primiano Tucci810c0522014-07-25 18:03:16 +0100482 Process.SHARED_RELRO_UID, crashHandler);
Primiano Tuccie76e81a2014-07-29 16:38:33 +0100483 if (pid <= 0) throw new Exception("Failed to start the relro file creator process");
Primiano Tucci810c0522014-07-25 18:03:16 +0100484 } catch (Throwable t) {
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100485 // Log and discard errors as we must not crash the system server.
Primiano Tucci810c0522014-07-25 18:03:16 +0100486 Log.e(LOGTAG, "error starting relro file creator for abi " + abi, t);
487 crashHandler.run();
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100488 }
489 }
490
491 private static class RelroFileCreator {
492 // Called in an unprivileged child process to create the relro file.
493 public static void main(String[] args) {
Primiano Tucci161536b2014-07-28 18:51:45 +0100494 boolean result = false;
495 boolean is64Bit = VMRuntime.getRuntime().is64Bit();
Primiano Tucci1b7977b2014-07-25 19:19:32 +0100496 try{
497 if (args.length != 2 || args[0] == null || args[1] == null) {
498 Log.e(LOGTAG, "Invalid RelroFileCreator args: " + Arrays.toString(args));
499 return;
500 }
Primiano Tucci1b7977b2014-07-25 19:19:32 +0100501 Log.v(LOGTAG, "RelroFileCreator (64bit = " + is64Bit + "), " +
502 " 32-bit lib: " + args[0] + ", 64-bit lib: " + args[1]);
503 if (!sAddressSpaceReserved) {
504 Log.e(LOGTAG, "can't create relro file; address space not reserved");
505 return;
506 }
Primiano Tucci161536b2014-07-28 18:51:45 +0100507 result = nativeCreateRelroFile(args[0] /* path32 */,
508 args[1] /* path64 */,
509 CHROMIUM_WEBVIEW_NATIVE_RELRO_32,
510 CHROMIUM_WEBVIEW_NATIVE_RELRO_64);
Primiano Tuccie76e81a2014-07-29 16:38:33 +0100511 if (result && DEBUG) Log.v(LOGTAG, "created relro file");
Primiano Tucci161536b2014-07-28 18:51:45 +0100512 } finally {
513 // We must do our best to always notify the update service, even if something fails.
Primiano Tucci1b7977b2014-07-25 19:19:32 +0100514 try {
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000515 getUpdateService().notifyRelroCreationCompleted();
Primiano Tucci1b7977b2014-07-25 19:19:32 +0100516 } catch (RemoteException e) {
517 Log.e(LOGTAG, "error notifying update service", e);
518 }
Primiano Tucci161536b2014-07-28 18:51:45 +0100519
520 if (!result) Log.e(LOGTAG, "failed to create relro file");
521
Primiano Tucci1b7977b2014-07-25 19:19:32 +0100522 // Must explicitly exit or else this process will just sit around after we return.
523 System.exit(0);
524 }
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100525 }
526 }
527
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000528 private static int waitForProviderAndSetPackageInfo() {
529 WebViewProviderResponse response = null;
530 try {
531 response =
532 getUpdateService().waitForAndGetProvider();
533 if (response.status == WebViewFactory.LIBLOAD_SUCCESS)
534 sPackageInfo = response.packageInfo;
535 } catch (RemoteException e) {
536 Log.e(LOGTAG, "error waiting for relro creation", e);
537 return LIBLOAD_FAILED_WAITING_FOR_WEBVIEW_REASON_UNKNOWN;
538 }
539 return response.status;
540 }
541
542 // Assumes that we have waited for relro creation and set sPackageInfo
Torne (Richard Coles)1a904122016-03-14 13:45:55 +0000543 private static int loadNativeLibrary(ClassLoader clazzLoader) {
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100544 if (!sAddressSpaceReserved) {
545 Log.e(LOGTAG, "can't load with relro file; address space not reserved");
Gustav Sennton85edb6c2015-04-15 11:54:20 +0100546 return LIBLOAD_ADDRESS_SPACE_NOT_RESERVED;
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100547 }
548
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000549 String[] args = getWebViewNativeLibraryPaths(sPackageInfo);
550 int result = nativeLoadWithRelroFile(args[0] /* path32 */,
Torne (Richard Coles)1a904122016-03-14 13:45:55 +0000551 args[1] /* path64 */,
552 CHROMIUM_WEBVIEW_NATIVE_RELRO_32,
553 CHROMIUM_WEBVIEW_NATIVE_RELRO_64,
554 clazzLoader);
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000555 if (result != LIBLOAD_SUCCESS) {
556 Log.w(LOGTAG, "failed to load with relro file, proceeding without");
557 } else if (DEBUG) {
558 Log.v(LOGTAG, "loaded with relro file");
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100559 }
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000560 return result;
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100561 }
562
Gustav Sennton1c177d82016-03-29 20:43:11 +0100563 private static String WEBVIEW_UPDATE_SERVICE_NAME = "webviewupdate";
564
565 /** @hide */
566 public static IWebViewUpdateService getUpdateService() {
567 return IWebViewUpdateService.Stub.asInterface(
568 ServiceManager.getService(WEBVIEW_UPDATE_SERVICE_NAME));
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100569 }
570
Ben Murdoch5ced5022014-07-28 15:57:00 +0100571 private static native boolean nativeReserveAddressSpace(long addressSpaceToReserve);
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100572 private static native boolean nativeCreateRelroFile(String lib32, String lib64,
573 String relro32, String relro64);
Gustav Sennton85edb6c2015-04-15 11:54:20 +0100574 private static native int nativeLoadWithRelroFile(String lib32, String lib64,
Torne (Richard Coles)1a904122016-03-14 13:45:55 +0000575 String relro32, String relro64,
576 ClassLoader clazzLoader);
Jonathan Dixond3101b12012-04-12 20:51:51 +0100577}