Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 com.android.internal.os; |
| 18 | |
Torne (Richard Coles) | 3b6ca99 | 2016-10-10 15:11:36 +0100 | [diff] [blame] | 19 | import android.app.ApplicationLoaders; |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 20 | import android.net.LocalSocket; |
| 21 | import android.os.Build; |
| 22 | import android.system.ErrnoException; |
| 23 | import android.system.Os; |
| 24 | import android.text.TextUtils; |
| 25 | import android.util.Log; |
Torne (Richard Coles) | 3b6ca99 | 2016-10-10 15:11:36 +0100 | [diff] [blame] | 26 | import android.webkit.WebViewFactory; |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 27 | |
| 28 | import java.io.IOException; |
Torne (Richard Coles) | 3b6ca99 | 2016-10-10 15:11:36 +0100 | [diff] [blame] | 29 | import java.lang.reflect.InvocationTargetException; |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 30 | |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 31 | /** |
| 32 | * Startup class for the WebView zygote process. |
| 33 | * |
| 34 | * See {@link ZygoteInit} for generic zygote startup documentation. |
| 35 | * |
| 36 | * @hide |
| 37 | */ |
| 38 | class WebViewZygoteInit { |
| 39 | public static final String TAG = "WebViewZygoteInit"; |
| 40 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 41 | private static ZygoteServer sServer; |
| 42 | |
| 43 | private static class WebViewZygoteServer extends ZygoteServer { |
| 44 | @Override |
| 45 | protected ZygoteConnection createNewConnection(LocalSocket socket, String abiList) |
| 46 | throws IOException { |
| 47 | return new WebViewZygoteConnection(socket, abiList); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | private static class WebViewZygoteConnection extends ZygoteConnection { |
| 52 | WebViewZygoteConnection(LocalSocket socket, String abiList) throws IOException { |
| 53 | super(socket, abiList); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | protected boolean handlePreloadPackage(String packagePath, String libsPath) { |
Torne (Richard Coles) | 3b6ca99 | 2016-10-10 15:11:36 +0100 | [diff] [blame] | 58 | // Ask ApplicationLoaders to create and cache a classloader for the WebView APK so that |
| 59 | // our children will reuse the same classloader instead of creating their own. |
| 60 | // This enables us to preload Java and native code in the webview zygote process and |
| 61 | // have the preloaded versions actually be used post-fork. |
| 62 | ClassLoader loader = ApplicationLoaders.getDefault().createAndCacheWebViewClassLoader( |
| 63 | packagePath, libsPath); |
| 64 | |
Robert Sesek | 54e387d | 2016-12-02 17:27:50 -0500 | [diff] [blame] | 65 | // Add the APK to the Zygote's list of allowed files for children. |
| 66 | Zygote.nativeAllowFileAcrossFork(packagePath); |
| 67 | |
Torne (Richard Coles) | 3b6ca99 | 2016-10-10 15:11:36 +0100 | [diff] [blame] | 68 | // Once we have the classloader, look up the WebViewFactoryProvider implementation and |
| 69 | // call preloadInZygote() on it to give it the opportunity to preload the native library |
| 70 | // and perform any other initialisation work that should be shared among the children. |
| 71 | try { |
| 72 | Class providerClass = Class.forName(WebViewFactory.CHROMIUM_WEBVIEW_FACTORY, true, |
| 73 | loader); |
| 74 | Object result = providerClass.getMethod("preloadInZygote").invoke(null); |
| 75 | if (!((Boolean)result).booleanValue()) { |
| 76 | Log.e(TAG, "preloadInZygote returned false"); |
| 77 | } |
| 78 | } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | |
| 79 | IllegalAccessException | InvocationTargetException e) { |
| 80 | Log.e(TAG, "Exception while preloading package", e); |
| 81 | } |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 82 | return false; |
| 83 | } |
| 84 | } |
| 85 | |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 86 | public static void main(String argv[]) { |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 87 | sServer = new WebViewZygoteServer(); |
| 88 | |
| 89 | // Zygote goes into its own process group. |
| 90 | try { |
| 91 | Os.setpgid(0, 0); |
| 92 | } catch (ErrnoException ex) { |
| 93 | throw new RuntimeException("Failed to setpgid(0,0)", ex); |
| 94 | } |
| 95 | |
| 96 | try { |
| 97 | sServer.registerServerSocket("webview_zygote"); |
| 98 | sServer.runSelectLoop(TextUtils.join(",", Build.SUPPORTED_ABIS)); |
| 99 | sServer.closeServerSocket(); |
| 100 | } catch (Zygote.MethodAndArgsCaller caller) { |
| 101 | caller.run(); |
| 102 | } catch (RuntimeException e) { |
| 103 | Log.e(TAG, "Fatal exception:", e); |
| 104 | } |
| 105 | |
| 106 | System.exit(0); |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 107 | } |
| 108 | } |