WebViewFactory.java revision dc00a84af15ff3594a6dfa512be21095bf9fee82
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 17package android.webkit; 18 19import android.os.Build; 20import android.os.Process; 21import android.os.RemoteException; 22import android.os.ServiceManager; 23import android.os.StrictMode; 24import android.util.AndroidRuntimeException; 25import android.util.Log; 26import dalvik.system.VMRuntime; 27 28import java.io.File; 29 30import com.android.internal.os.Zygote; 31 32/** 33 * Top level factory, used creating all the main WebView implementation classes. 34 * 35 * @hide 36 */ 37public final class WebViewFactory { 38 39 private static final String CHROMIUM_WEBVIEW_FACTORY = 40 "com.android.webview.chromium.WebViewChromiumFactoryProvider"; 41 42 private static final String NULL_WEBVIEW_FACTORY = 43 "com.android.webview.nullwebview.NullWebViewFactoryProvider"; 44 45 // TODO(torne): we need to use a system property instead of hardcoding the library paths to 46 // enable it to be changed when a webview update apk is installed. 47 private static final String CHROMIUM_WEBVIEW_NATIVE_LIB_32 = 48 "/system/lib/libwebviewchromium.so"; 49 private static final String CHROMIUM_WEBVIEW_NATIVE_LIB_64 = 50 "/system/lib64/libwebviewchromium.so"; 51 private static final String CHROMIUM_WEBVIEW_NATIVE_RELRO_32 = 52 "/data/misc/shared_relro/libwebviewchromium32.relro"; 53 private static final String CHROMIUM_WEBVIEW_NATIVE_RELRO_64 = 54 "/data/misc/shared_relro/libwebviewchromium64.relro"; 55 56 private static final String LOGTAG = "WebViewFactory"; 57 58 private static final boolean DEBUG = false; 59 60 // Cache the factory both for efficiency, and ensure any one process gets all webviews from the 61 // same provider. 62 private static WebViewFactoryProvider sProviderInstance; 63 private static final Object sProviderLock = new Object(); 64 private static boolean sAddressSpaceReserved = false; 65 66 public static String getWebViewPackageName() { 67 // TODO: Make this dynamic based on resource configuration. 68 return "com.android.webview"; 69 } 70 71 static WebViewFactoryProvider getProvider() { 72 synchronized (sProviderLock) { 73 // For now the main purpose of this function (and the factory abstraction) is to keep 74 // us honest and minimize usage of WebView internals when binding the proxy. 75 if (sProviderInstance != null) return sProviderInstance; 76 77 loadNativeLibrary(); 78 79 Class<WebViewFactoryProvider> providerClass; 80 try { 81 providerClass = getFactoryClass(); 82 } catch (ClassNotFoundException e) { 83 Log.e(LOGTAG, "error loading provider", e); 84 throw new AndroidRuntimeException(e); 85 } 86 87 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); 88 try { 89 sProviderInstance = providerClass.newInstance(); 90 if (DEBUG) Log.v(LOGTAG, "Loaded provider: " + sProviderInstance); 91 return sProviderInstance; 92 } catch (Exception e) { 93 Log.e(LOGTAG, "error instantiating provider", e); 94 throw new AndroidRuntimeException(e); 95 } finally { 96 StrictMode.setThreadPolicy(oldPolicy); 97 } 98 } 99 } 100 101 private static Class<WebViewFactoryProvider> getFactoryClass() throws ClassNotFoundException { 102 try { 103 return (Class<WebViewFactoryProvider>) Class.forName(CHROMIUM_WEBVIEW_FACTORY); 104 } catch (ClassNotFoundException e) { 105 Log.e(LOGTAG, "Chromium WebView does not exist"); 106 return (Class<WebViewFactoryProvider>) Class.forName(NULL_WEBVIEW_FACTORY); 107 } 108 } 109 110 /** 111 * Perform any WebView loading preparations that must happen in the zygote. 112 * Currently, this means allocating address space to load the real JNI library later. 113 */ 114 public static void prepareWebViewInZygote() { 115 try { 116 System.loadLibrary("webviewchromium_loader"); 117 sAddressSpaceReserved = nativeReserveAddressSpace(CHROMIUM_WEBVIEW_NATIVE_LIB_32, 118 CHROMIUM_WEBVIEW_NATIVE_LIB_64); 119 if (sAddressSpaceReserved) { 120 if (DEBUG) Log.v(LOGTAG, "address space reserved"); 121 } else { 122 Log.e(LOGTAG, "reserving address space failed"); 123 } 124 } catch (Throwable e) { 125 // Log and discard errors at this stage as we must not crash the zygote. 126 Log.e(LOGTAG, "error preparing native loader", e); 127 } 128 } 129 130 /** 131 * Perform any WebView loading preparations that must happen at boot from the system server, 132 * after the package manager has started. 133 * This must be called in the system server. 134 * Currently, this means spawning the child processes which will create the relro files. 135 */ 136 public static void prepareWebViewInSystemServer() { 137 if (DEBUG) Log.v(LOGTAG, "creating relro files"); 138 if (new File(CHROMIUM_WEBVIEW_NATIVE_LIB_64).exists()) { 139 createRelroFile(Build.SUPPORTED_64_BIT_ABIS[0]); 140 } 141 if (new File(CHROMIUM_WEBVIEW_NATIVE_LIB_32).exists()) { 142 createRelroFile(Build.SUPPORTED_32_BIT_ABIS[0]); 143 } 144 } 145 146 private static void createRelroFile(String abi) { 147 try { 148 Process.start("android.webkit.WebViewFactory$RelroFileCreator", 149 "WebViewLoader-" + abi, 150 Process.SHARED_RELRO_UID, 151 Process.SHARED_RELRO_UID, 152 null, 153 0, // TODO(torne): do we need to set debug flags? 154 Zygote.MOUNT_EXTERNAL_NONE, 155 Build.VERSION.SDK_INT, 156 null, 157 abi, 158 null); 159 } catch (Throwable e) { 160 // Log and discard errors as we must not crash the system server. 161 Log.e(LOGTAG, "error starting relro file creator for abi " + abi, e); 162 } 163 } 164 165 private static class RelroFileCreator { 166 // Called in an unprivileged child process to create the relro file. 167 public static void main(String[] args) { 168 if (!sAddressSpaceReserved) { 169 Log.e(LOGTAG, "can't create relro file; address space not reserved"); 170 return; 171 } 172 boolean result = nativeCreateRelroFile(CHROMIUM_WEBVIEW_NATIVE_LIB_32, 173 CHROMIUM_WEBVIEW_NATIVE_LIB_64, 174 CHROMIUM_WEBVIEW_NATIVE_RELRO_32, 175 CHROMIUM_WEBVIEW_NATIVE_RELRO_64); 176 if (!result) { 177 Log.e(LOGTAG, "failed to create relro file"); 178 } else if (DEBUG) { 179 Log.v(LOGTAG, "created relro file"); 180 } 181 try { 182 getUpdateService().notifyRelroCreationCompleted(VMRuntime.getRuntime().is64Bit(), 183 result); 184 } catch (RemoteException e) { 185 Log.e(LOGTAG, "error notifying update service", e); 186 } 187 188 // Must explicitly exit or else this process will just sit around after we return. 189 System.exit(0); 190 } 191 } 192 193 private static void loadNativeLibrary() { 194 if (!sAddressSpaceReserved) { 195 Log.e(LOGTAG, "can't load with relro file; address space not reserved"); 196 return; 197 } 198 199 try { 200 getUpdateService().waitForRelroCreationCompleted(VMRuntime.getRuntime().is64Bit()); 201 } catch (RemoteException e) { 202 Log.e(LOGTAG, "error waiting for relro creation, proceeding without", e); 203 return; 204 } 205 206 boolean result = nativeLoadWithRelroFile(CHROMIUM_WEBVIEW_NATIVE_LIB_32, 207 CHROMIUM_WEBVIEW_NATIVE_LIB_64, 208 CHROMIUM_WEBVIEW_NATIVE_RELRO_32, 209 CHROMIUM_WEBVIEW_NATIVE_RELRO_64); 210 if (!result) { 211 Log.w(LOGTAG, "failed to load with relro file, proceeding without"); 212 } else if (DEBUG) { 213 Log.v(LOGTAG, "loaded with relro file"); 214 } 215 } 216 217 private static IWebViewUpdateService getUpdateService() { 218 return IWebViewUpdateService.Stub.asInterface(ServiceManager.getService("webviewupdate")); 219 } 220 221 private static native boolean nativeReserveAddressSpace(String lib32, String lib64); 222 private static native boolean nativeCreateRelroFile(String lib32, String lib64, 223 String relro32, String relro64); 224 private static native boolean nativeLoadWithRelroFile(String lib32, String lib64, 225 String relro32, String relro64); 226} 227