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