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.content.Context; 20 import android.content.pm.PackageInfo; 21 import android.content.pm.PackageManager.NameNotFoundException; 22 import android.content.pm.UserInfo; 23 import android.webkit.UserPackage; 24 import android.webkit.WebViewProviderInfo; 25 26 import java.util.ArrayList; 27 import java.util.HashMap; 28 import java.util.List; 29 import java.util.Map; 30 31 public class TestSystemImpl implements SystemInterface { 32 private String mUserProvider = null; 33 private final WebViewProviderInfo[] mPackageConfigs; 34 List<Integer> mUsers = new ArrayList<>(); 35 // Package -> [user, package] 36 Map<String, Map<Integer, PackageInfo>> mPackages = new HashMap(); 37 private boolean mFallbackLogicEnabled; 38 private final int mNumRelros; 39 private final boolean mIsDebuggable; 40 private int mMultiProcessSetting; 41 private final boolean mMultiProcessDefault; 42 43 public static final int PRIMARY_USER_ID = 0; 44 45 public TestSystemImpl(WebViewProviderInfo[] packageConfigs, boolean fallbackLogicEnabled, 46 int numRelros, boolean isDebuggable, boolean multiProcessDefault) { 47 mPackageConfigs = packageConfigs; 48 mFallbackLogicEnabled = fallbackLogicEnabled; 49 mNumRelros = numRelros; 50 mIsDebuggable = isDebuggable; 51 mUsers.add(PRIMARY_USER_ID); 52 mMultiProcessDefault = multiProcessDefault; 53 } 54 55 public void addUser(int userId) { 56 mUsers.add(userId); 57 } 58 59 @Override 60 public WebViewProviderInfo[] getWebViewPackages() { 61 return mPackageConfigs; 62 } 63 64 @Override 65 public int onWebViewProviderChanged(PackageInfo packageInfo) { 66 return mNumRelros; 67 } 68 69 @Override 70 public String getUserChosenWebViewProvider(Context context) { return mUserProvider; } 71 72 @Override 73 public void updateUserSetting(Context context, String newProviderName) { 74 mUserProvider = newProviderName; 75 } 76 77 @Override 78 public void killPackageDependents(String packageName) {} 79 80 @Override 81 public boolean isFallbackLogicEnabled() { 82 return mFallbackLogicEnabled; 83 } 84 85 @Override 86 public void enableFallbackLogic(boolean enable) { 87 mFallbackLogicEnabled = enable; 88 } 89 90 @Override 91 public void uninstallAndDisablePackageForAllUsers(Context context, String packageName) { 92 enablePackageForAllUsers(context, packageName, false); 93 } 94 95 @Override 96 public void enablePackageForAllUsers(Context context, String packageName, boolean enable) { 97 for(int userId : mUsers) { 98 enablePackageForUser(packageName, enable, userId); 99 } 100 } 101 102 @Override 103 public void enablePackageForUser(String packageName, boolean enable, int userId) { 104 Map<Integer, PackageInfo> userPackages = mPackages.get(packageName); 105 if (userPackages == null) { 106 throw new IllegalArgumentException("There is no package called " + packageName); 107 } 108 PackageInfo packageInfo = userPackages.get(userId); 109 packageInfo.applicationInfo.enabled = enable; 110 setPackageInfoForUser(userId, packageInfo); 111 } 112 113 @Override 114 public boolean systemIsDebuggable() { return mIsDebuggable; } 115 116 @Override 117 public PackageInfo getPackageInfoForProvider(WebViewProviderInfo info) throws 118 NameNotFoundException { 119 Map<Integer, PackageInfo> userPackages = mPackages.get(info.packageName); 120 if (userPackages == null) throw new NameNotFoundException(info.packageName); 121 PackageInfo ret = userPackages.get(PRIMARY_USER_ID); 122 if (ret == null) throw new NameNotFoundException(info.packageName); 123 return ret; 124 } 125 126 @Override 127 public List<UserPackage> getPackageInfoForProviderAllUsers( 128 Context context, WebViewProviderInfo info) { 129 Map<Integer, PackageInfo> userPackages = mPackages.get(info.packageName); 130 List<UserPackage> ret = new ArrayList(); 131 // Loop over defined users, and find the corresponding package for each user. 132 for (int userId : mUsers) { 133 ret.add(new UserPackage(createUserInfo(userId), 134 userPackages == null ? null : userPackages.get(userId))); 135 } 136 return ret; 137 } 138 139 private static UserInfo createUserInfo(int userId) { 140 return new UserInfo(userId, "User nr. " + userId, 0 /* flags */); 141 } 142 143 /** 144 * Set package for primary user. 145 */ 146 public void setPackageInfo(PackageInfo pi) { 147 setPackageInfoForUser(PRIMARY_USER_ID, pi); 148 } 149 150 public void setPackageInfoForUser(int userId, PackageInfo pi) { 151 if (!mUsers.contains(userId)) { 152 throw new IllegalArgumentException("User nr. " + userId + " doesn't exist"); 153 } 154 if (!mPackages.containsKey(pi.packageName)) { 155 mPackages.put(pi.packageName, new HashMap<Integer, PackageInfo>()); 156 } 157 mPackages.get(pi.packageName).put(userId, pi); 158 } 159 160 /** 161 * Removes the package {@param packageName} for the primary user. 162 */ 163 public void removePackageInfo(String packageName) { 164 mPackages.get(packageName).remove(PRIMARY_USER_ID); 165 } 166 167 @Override 168 public long getFactoryPackageVersion(String packageName) throws NameNotFoundException { 169 PackageInfo pi = null; 170 Map<Integer, PackageInfo> userPackages = mPackages.get(packageName); 171 if (userPackages == null) throw new NameNotFoundException(); 172 173 pi = userPackages.get(PRIMARY_USER_ID); 174 if (pi != null && pi.applicationInfo.isSystemApp()) { 175 return pi.applicationInfo.versionCode; 176 } 177 throw new NameNotFoundException(); 178 } 179 180 @Override 181 public int getMultiProcessSetting(Context context) { 182 return mMultiProcessSetting; 183 } 184 185 @Override 186 public void setMultiProcessSetting(Context context, int value) { 187 mMultiProcessSetting = value; 188 } 189 190 @Override 191 public void notifyZygote(boolean enableMultiProcess) {} 192 193 @Override 194 public boolean isMultiProcessDefaultEnabled() { 195 return mMultiProcessDefault; 196 } 197 } 198