Gustav Sennton | 8b17926 | 2016-03-14 11:31:14 +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.server.webkit; |
| 18 | |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Gustav Sennton | 8b17926 | 2016-03-14 11:31:14 +0000 | [diff] [blame] | 20 | import android.app.AppGlobals; |
| 21 | import android.content.Context; |
| 22 | import android.content.pm.PackageInfo; |
Gustav Sennton | 065b7e6 | 2016-04-01 15:11:43 +0100 | [diff] [blame] | 23 | import android.content.pm.PackageManager; |
| 24 | import android.content.pm.PackageManager.NameNotFoundException; |
| 25 | import android.content.pm.UserInfo; |
Gustav Sennton | 8b17926 | 2016-03-14 11:31:14 +0000 | [diff] [blame] | 26 | import android.content.res.XmlResourceParser; |
Gustav Sennton | 065b7e6 | 2016-04-01 15:11:43 +0100 | [diff] [blame] | 27 | import android.os.Build; |
Gustav Sennton | 8b17926 | 2016-03-14 11:31:14 +0000 | [diff] [blame] | 28 | import android.os.RemoteException; |
| 29 | import android.os.UserHandle; |
Gustav Sennton | 065b7e6 | 2016-04-01 15:11:43 +0100 | [diff] [blame] | 30 | import android.os.UserManager; |
Gustav Sennton | 8b17926 | 2016-03-14 11:31:14 +0000 | [diff] [blame] | 31 | import android.provider.Settings; |
| 32 | import android.util.AndroidRuntimeException; |
| 33 | import android.util.Log; |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 34 | import android.webkit.UserPackage; |
Gustav Sennton | 065b7e6 | 2016-04-01 15:11:43 +0100 | [diff] [blame] | 35 | import android.webkit.WebViewFactory; |
Gustav Sennton | 8b17926 | 2016-03-14 11:31:14 +0000 | [diff] [blame] | 36 | import android.webkit.WebViewProviderInfo; |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 37 | import android.webkit.WebViewZygote; |
Gustav Sennton | 8b17926 | 2016-03-14 11:31:14 +0000 | [diff] [blame] | 38 | |
| 39 | import com.android.internal.util.XmlUtils; |
| 40 | |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 41 | import org.xmlpull.v1.XmlPullParserException; |
| 42 | |
Gustav Sennton | 8b17926 | 2016-03-14 11:31:14 +0000 | [diff] [blame] | 43 | import java.io.IOException; |
| 44 | import java.util.ArrayList; |
| 45 | import java.util.List; |
| 46 | |
Gustav Sennton | 8b17926 | 2016-03-14 11:31:14 +0000 | [diff] [blame] | 47 | /** |
| 48 | * Default implementation for the WebView preparation Utility interface. |
| 49 | * @hide |
| 50 | */ |
Gustav Sennton | 065b7e6 | 2016-04-01 15:11:43 +0100 | [diff] [blame] | 51 | public class SystemImpl implements SystemInterface { |
| 52 | private static final String TAG = SystemImpl.class.getSimpleName(); |
Gustav Sennton | 8b17926 | 2016-03-14 11:31:14 +0000 | [diff] [blame] | 53 | private static final String TAG_START = "webviewproviders"; |
| 54 | private static final String TAG_WEBVIEW_PROVIDER = "webviewprovider"; |
| 55 | private static final String TAG_PACKAGE_NAME = "packageName"; |
| 56 | private static final String TAG_DESCRIPTION = "description"; |
| 57 | // Whether or not the provider must be explicitly chosen by the user to be used. |
| 58 | private static final String TAG_AVAILABILITY = "availableByDefault"; |
| 59 | private static final String TAG_SIGNATURE = "signature"; |
| 60 | private static final String TAG_FALLBACK = "isFallback"; |
Hui Shu | 9455bd0 | 2016-04-08 13:25:26 -0700 | [diff] [blame] | 61 | private final WebViewProviderInfo[] mWebViewProviderPackages; |
Gustav Sennton | 8b17926 | 2016-03-14 11:31:14 +0000 | [diff] [blame] | 62 | |
Hui Shu | 9455bd0 | 2016-04-08 13:25:26 -0700 | [diff] [blame] | 63 | // Initialization-on-demand holder idiom for getting the WebView provider packages once and |
| 64 | // for all in a thread-safe manner. |
| 65 | private static class LazyHolder { |
| 66 | private static final SystemImpl INSTANCE = new SystemImpl(); |
| 67 | } |
| 68 | |
| 69 | public static SystemImpl getInstance() { |
| 70 | return LazyHolder.INSTANCE; |
| 71 | } |
| 72 | |
| 73 | private SystemImpl() { |
Gustav Sennton | 8b17926 | 2016-03-14 11:31:14 +0000 | [diff] [blame] | 74 | int numFallbackPackages = 0; |
Gustav Sennton | 631de99e | 2016-04-07 16:37:58 +0100 | [diff] [blame] | 75 | int numAvailableByDefaultPackages = 0; |
Gustav Sennton | 8b17926 | 2016-03-14 11:31:14 +0000 | [diff] [blame] | 76 | XmlResourceParser parser = null; |
| 77 | List<WebViewProviderInfo> webViewProviders = new ArrayList<WebViewProviderInfo>(); |
| 78 | try { |
| 79 | parser = AppGlobals.getInitialApplication().getResources().getXml( |
| 80 | com.android.internal.R.xml.config_webview_packages); |
| 81 | XmlUtils.beginDocument(parser, TAG_START); |
| 82 | while(true) { |
| 83 | XmlUtils.nextElement(parser); |
| 84 | String element = parser.getName(); |
| 85 | if (element == null) { |
| 86 | break; |
| 87 | } |
| 88 | if (element.equals(TAG_WEBVIEW_PROVIDER)) { |
| 89 | String packageName = parser.getAttributeValue(null, TAG_PACKAGE_NAME); |
| 90 | if (packageName == null) { |
Gustav Sennton | 631de99e | 2016-04-07 16:37:58 +0100 | [diff] [blame] | 91 | throw new AndroidRuntimeException( |
Gustav Sennton | 8b17926 | 2016-03-14 11:31:14 +0000 | [diff] [blame] | 92 | "WebView provider in framework resources missing package name"); |
| 93 | } |
| 94 | String description = parser.getAttributeValue(null, TAG_DESCRIPTION); |
| 95 | if (description == null) { |
Gustav Sennton | 631de99e | 2016-04-07 16:37:58 +0100 | [diff] [blame] | 96 | throw new AndroidRuntimeException( |
Gustav Sennton | 8b17926 | 2016-03-14 11:31:14 +0000 | [diff] [blame] | 97 | "WebView provider in framework resources missing description"); |
| 98 | } |
| 99 | boolean availableByDefault = "true".equals( |
| 100 | parser.getAttributeValue(null, TAG_AVAILABILITY)); |
| 101 | boolean isFallback = "true".equals( |
| 102 | parser.getAttributeValue(null, TAG_FALLBACK)); |
Gustav Sennton | dbf5eb0 | 2016-03-30 14:53:03 +0100 | [diff] [blame] | 103 | WebViewProviderInfo currentProvider = new WebViewProviderInfo( |
| 104 | packageName, description, availableByDefault, isFallback, |
| 105 | readSignatures(parser)); |
| 106 | if (currentProvider.isFallback) { |
Gustav Sennton | 8b17926 | 2016-03-14 11:31:14 +0000 | [diff] [blame] | 107 | numFallbackPackages++; |
Gustav Sennton | 631de99e | 2016-04-07 16:37:58 +0100 | [diff] [blame] | 108 | if (!currentProvider.availableByDefault) { |
| 109 | throw new AndroidRuntimeException( |
| 110 | "Each WebView fallback package must be available by default."); |
| 111 | } |
Gustav Sennton | 8b17926 | 2016-03-14 11:31:14 +0000 | [diff] [blame] | 112 | if (numFallbackPackages > 1) { |
| 113 | throw new AndroidRuntimeException( |
Gustav Sennton | 631de99e | 2016-04-07 16:37:58 +0100 | [diff] [blame] | 114 | "There can be at most one WebView fallback package."); |
Gustav Sennton | 8b17926 | 2016-03-14 11:31:14 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
Gustav Sennton | 631de99e | 2016-04-07 16:37:58 +0100 | [diff] [blame] | 117 | if (currentProvider.availableByDefault) { |
| 118 | numAvailableByDefaultPackages++; |
| 119 | } |
Gustav Sennton | 8b17926 | 2016-03-14 11:31:14 +0000 | [diff] [blame] | 120 | webViewProviders.add(currentProvider); |
| 121 | } |
| 122 | else { |
Gustav Sennton | 631de99e | 2016-04-07 16:37:58 +0100 | [diff] [blame] | 123 | Log.e(TAG, "Found an element that is not a WebView provider"); |
Gustav Sennton | 8b17926 | 2016-03-14 11:31:14 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
Hui Shu | 9e8c058 | 2016-03-24 17:02:58 -0700 | [diff] [blame] | 126 | } catch (XmlPullParserException | IOException e) { |
Gustav Sennton | 631de99e | 2016-04-07 16:37:58 +0100 | [diff] [blame] | 127 | throw new AndroidRuntimeException("Error when parsing WebView config " + e); |
Gustav Sennton | 8b17926 | 2016-03-14 11:31:14 +0000 | [diff] [blame] | 128 | } finally { |
| 129 | if (parser != null) parser.close(); |
| 130 | } |
Gustav Sennton | 631de99e | 2016-04-07 16:37:58 +0100 | [diff] [blame] | 131 | if (numAvailableByDefaultPackages == 0) { |
| 132 | throw new AndroidRuntimeException("There must be at least one WebView package " |
| 133 | + "that is available by default"); |
| 134 | } |
Hui Shu | 9455bd0 | 2016-04-08 13:25:26 -0700 | [diff] [blame] | 135 | mWebViewProviderPackages = |
| 136 | webViewProviders.toArray(new WebViewProviderInfo[webViewProviders.size()]); |
| 137 | } |
| 138 | /** |
| 139 | * Returns all packages declared in the framework resources as potential WebView providers. |
| 140 | * @hide |
| 141 | * */ |
| 142 | @Override |
| 143 | public WebViewProviderInfo[] getWebViewPackages() { |
| 144 | return mWebViewProviderPackages; |
Gustav Sennton | 8b17926 | 2016-03-14 11:31:14 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 147 | public long getFactoryPackageVersion(String packageName) throws NameNotFoundException { |
Hui Shu | 9e8c058 | 2016-03-24 17:02:58 -0700 | [diff] [blame] | 148 | PackageManager pm = AppGlobals.getInitialApplication().getPackageManager(); |
Dianne Hackborn | 3accca0 | 2013-09-20 09:32:11 -0700 | [diff] [blame] | 149 | return pm.getPackageInfo(packageName, PackageManager.MATCH_FACTORY_ONLY) |
| 150 | .getLongVersionCode(); |
Hui Shu | 9e8c058 | 2016-03-24 17:02:58 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Gustav Sennton | 8b17926 | 2016-03-14 11:31:14 +0000 | [diff] [blame] | 153 | /** |
| 154 | * Reads all signatures at the current depth (within the current provider) from the XML parser. |
| 155 | */ |
| 156 | private static String[] readSignatures(XmlResourceParser parser) throws IOException, |
| 157 | XmlPullParserException { |
| 158 | List<String> signatures = new ArrayList<String>(); |
| 159 | int outerDepth = parser.getDepth(); |
| 160 | while(XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 161 | if (parser.getName().equals(TAG_SIGNATURE)) { |
| 162 | // Parse the value within the signature tag |
| 163 | String signature = parser.nextText(); |
| 164 | signatures.add(signature); |
| 165 | } else { |
| 166 | Log.e(TAG, "Found an element in a webview provider that is not a signature"); |
| 167 | } |
| 168 | } |
| 169 | return signatures.toArray(new String[signatures.size()]); |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public int onWebViewProviderChanged(PackageInfo packageInfo) { |
| 174 | return WebViewFactory.onWebViewProviderChanged(packageInfo); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public String getUserChosenWebViewProvider(Context context) { |
| 179 | return Settings.Global.getString(context.getContentResolver(), |
| 180 | Settings.Global.WEBVIEW_PROVIDER); |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public void updateUserSetting(Context context, String newProviderName) { |
| 185 | Settings.Global.putString(context.getContentResolver(), |
| 186 | Settings.Global.WEBVIEW_PROVIDER, |
| 187 | newProviderName == null ? "" : newProviderName); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public void killPackageDependents(String packageName) { |
| 192 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 193 | ActivityManager.getService().killPackageDependents(packageName, |
Gustav Sennton | 8b17926 | 2016-03-14 11:31:14 +0000 | [diff] [blame] | 194 | UserHandle.USER_ALL); |
| 195 | } catch (RemoteException e) { |
| 196 | } |
| 197 | } |
Gustav Sennton | 065b7e6 | 2016-04-01 15:11:43 +0100 | [diff] [blame] | 198 | |
| 199 | @Override |
| 200 | public boolean isFallbackLogicEnabled() { |
| 201 | // Note that this is enabled by default (i.e. if the setting hasn't been set). |
| 202 | return Settings.Global.getInt(AppGlobals.getInitialApplication().getContentResolver(), |
| 203 | Settings.Global.WEBVIEW_FALLBACK_LOGIC_ENABLED, 1) == 1; |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public void enableFallbackLogic(boolean enable) { |
| 208 | Settings.Global.putInt(AppGlobals.getInitialApplication().getContentResolver(), |
| 209 | Settings.Global.WEBVIEW_FALLBACK_LOGIC_ENABLED, enable ? 1 : 0); |
| 210 | } |
| 211 | |
| 212 | @Override |
Gustav Sennton | 065b7e6 | 2016-04-01 15:11:43 +0100 | [diff] [blame] | 213 | public void enablePackageForAllUsers(Context context, String packageName, boolean enable) { |
| 214 | UserManager userManager = (UserManager)context.getSystemService(Context.USER_SERVICE); |
| 215 | for(UserInfo userInfo : userManager.getUsers()) { |
| 216 | enablePackageForUser(packageName, enable, userInfo.id); |
| 217 | } |
| 218 | } |
| 219 | |
Torne (Richard Coles) | ef47890 | 2019-03-28 15:03:21 -0400 | [diff] [blame] | 220 | private void enablePackageForUser(String packageName, boolean enable, int userId) { |
Gustav Sennton | 065b7e6 | 2016-04-01 15:11:43 +0100 | [diff] [blame] | 221 | try { |
| 222 | AppGlobals.getPackageManager().setApplicationEnabledSetting( |
| 223 | packageName, |
| 224 | enable ? PackageManager.COMPONENT_ENABLED_STATE_DEFAULT : |
| 225 | PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER, 0, |
| 226 | userId, null); |
Gustav Sennton | ff396f2 | 2016-04-08 18:33:30 +0100 | [diff] [blame] | 227 | } catch (RemoteException | IllegalArgumentException e) { |
| 228 | Log.w(TAG, "Tried to " + (enable ? "enable " : "disable ") + packageName |
| 229 | + " for user " + userId + ": " + e); |
Gustav Sennton | 065b7e6 | 2016-04-01 15:11:43 +0100 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public boolean systemIsDebuggable() { |
| 235 | return Build.IS_DEBUGGABLE; |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public PackageInfo getPackageInfoForProvider(WebViewProviderInfo configInfo) |
| 240 | throws NameNotFoundException { |
| 241 | PackageManager pm = AppGlobals.getInitialApplication().getPackageManager(); |
| 242 | return pm.getPackageInfo(configInfo.packageName, PACKAGE_FLAGS); |
| 243 | } |
| 244 | |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 245 | @Override |
Gustav Sennton | 364e160 | 2016-12-14 09:10:50 +0000 | [diff] [blame] | 246 | public List<UserPackage> getPackageInfoForProviderAllUsers(Context context, |
| 247 | WebViewProviderInfo configInfo) { |
| 248 | return UserPackage.getPackageInfosAllUsers(context, configInfo.packageName, PACKAGE_FLAGS); |
| 249 | } |
| 250 | |
| 251 | @Override |
Torne (Richard Coles) | 1a4c4e3 | 2017-01-10 15:57:41 +0000 | [diff] [blame] | 252 | public int getMultiProcessSetting(Context context) { |
| 253 | return Settings.Global.getInt(context.getContentResolver(), |
| 254 | Settings.Global.WEBVIEW_MULTIPROCESS, 0); |
Gustav Sennton | 86f7bbe | 2016-10-24 16:49:32 +0100 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | @Override |
Torne (Richard Coles) | 1a4c4e3 | 2017-01-10 15:57:41 +0000 | [diff] [blame] | 258 | public void setMultiProcessSetting(Context context, int value) { |
| 259 | Settings.Global.putInt(context.getContentResolver(), |
| 260 | Settings.Global.WEBVIEW_MULTIPROCESS, value); |
| 261 | } |
| 262 | |
| 263 | @Override |
| 264 | public void notifyZygote(boolean enableMultiProcess) { |
| 265 | WebViewZygote.setMultiprocessEnabled(enableMultiProcess); |
Robert Sesek | ded2098 | 2016-08-15 13:59:13 -0400 | [diff] [blame] | 266 | } |
| 267 | |
Torne (Richard Coles) | dc37507 | 2017-01-11 15:48:13 +0000 | [diff] [blame] | 268 | @Override |
Torne (Richard Coles) | 05a766e | 2019-04-10 17:55:53 -0400 | [diff] [blame] | 269 | public void ensureZygoteStarted() { |
| 270 | WebViewZygote.getProcess(); |
| 271 | } |
| 272 | |
| 273 | @Override |
Torne (Richard Coles) | dc37507 | 2017-01-11 15:48:13 +0000 | [diff] [blame] | 274 | public boolean isMultiProcessDefaultEnabled() { |
Torne (Richard Coles) | 2bdf647 | 2019-05-14 17:08:04 -0400 | [diff] [blame] | 275 | // Multiprocess is enabled by default for all devices. |
| 276 | return true; |
Torne (Richard Coles) | dc37507 | 2017-01-11 15:48:13 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Gustav Sennton | 065b7e6 | 2016-04-01 15:11:43 +0100 | [diff] [blame] | 279 | // flags declaring we want extra info from the package manager for webview providers |
| 280 | private final static int PACKAGE_FLAGS = PackageManager.GET_META_DATA |
Torne (Richard Coles) | 4fd8aa5 | 2017-09-19 15:21:29 -0400 | [diff] [blame] | 281 | | PackageManager.GET_SIGNATURES | PackageManager.GET_SHARED_LIBRARY_FILES |
| 282 | | PackageManager.MATCH_DEBUG_TRIAGED_MISSING | PackageManager.MATCH_ANY_USER; |
Gustav Sennton | 8b17926 | 2016-03-14 11:31:14 +0000 | [diff] [blame] | 283 | } |