blob: a8a55499f5f3cc06b4ee4f81368b05512c639a43 [file] [log] [blame]
Tobias Sargeantb9679dc2016-01-19 16:34:54 +00001/*
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
17package com.android.internal.os;
18
Torne (Richard Coles)3b6ca992016-10-10 15:11:36 +010019import android.app.ApplicationLoaders;
Robert Sesekded20982016-08-15 13:59:13 -040020import android.net.LocalSocket;
21import android.os.Build;
22import android.system.ErrnoException;
23import android.system.Os;
24import android.text.TextUtils;
25import android.util.Log;
Torne (Richard Coles)3b6ca992016-10-10 15:11:36 +010026import android.webkit.WebViewFactory;
Robert Sesekded20982016-08-15 13:59:13 -040027
28import java.io.IOException;
Torne (Richard Coles)3b6ca992016-10-10 15:11:36 +010029import java.lang.reflect.InvocationTargetException;
Robert Sesekded20982016-08-15 13:59:13 -040030
Tobias Sargeantb9679dc2016-01-19 16:34:54 +000031/**
32 * Startup class for the WebView zygote process.
33 *
34 * See {@link ZygoteInit} for generic zygote startup documentation.
35 *
36 * @hide
37 */
38class WebViewZygoteInit {
39 public static final String TAG = "WebViewZygoteInit";
40
Robert Sesekded20982016-08-15 13:59:13 -040041 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)3b6ca992016-10-10 15:11:36 +010058 // 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 Sesek54e387d2016-12-02 17:27:50 -050065 // Add the APK to the Zygote's list of allowed files for children.
66 Zygote.nativeAllowFileAcrossFork(packagePath);
67
Torne (Richard Coles)3b6ca992016-10-10 15:11:36 +010068 // 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 Sesekded20982016-08-15 13:59:13 -040082 return false;
83 }
84 }
85
Tobias Sargeantb9679dc2016-01-19 16:34:54 +000086 public static void main(String argv[]) {
Robert Sesekded20982016-08-15 13:59:13 -040087 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 Sargeantb9679dc2016-01-19 16:34:54 +0000107 }
108}