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