blob: d7458f2f6e1ab5366425d0da282c8933a7c0870b [file] [log] [blame]
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +01001/*
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 com.android.server.webkit;
18
Ben Murdochdc00a842014-07-17 14:55:00 +010019import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
Gustav Senntonbf683e02016-09-15 14:42:50 +010023import android.content.pm.PackageInfo;
Gustav Sennton6258dcd2015-10-30 19:25:37 +000024import android.content.pm.PackageManager;
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010025import android.os.Binder;
Gustav Senntonc83e3fa2016-02-18 12:19:13 +000026import android.os.PatternMatcher;
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010027import android.os.Process;
Gustav Senntonc83e3fa2016-02-18 12:19:13 +000028import android.os.ResultReceiver;
Dianne Hackborn354736e2016-08-22 17:00:05 -070029import android.os.ShellCallback;
Gustav Sennton23875b22016-02-09 14:11:33 +000030import android.os.UserHandle;
Primiano Tucci810c0522014-07-25 18:03:16 +010031import android.util.Slog;
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010032import android.webkit.IWebViewUpdateService;
Gustav Sennton6258dcd2015-10-30 19:25:37 +000033import android.webkit.WebViewProviderInfo;
34import android.webkit.WebViewProviderResponse;
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010035
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -060036import com.android.internal.util.DumpUtils;
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +010037import com.android.server.SystemService;
38
Gustav Senntonc83e3fa2016-02-18 12:19:13 +000039import java.io.FileDescriptor;
Gustav Sennton1eb38202016-10-21 13:40:10 +010040import java.io.PrintWriter;
Gustav Sennton6258dcd2015-10-30 19:25:37 +000041import java.util.Arrays;
Gustav Sennton6258dcd2015-10-30 19:25:37 +000042
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010043/**
44 * Private service to wait for the updatable WebView to be ready for use.
45 * @hide
46 */
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +010047public class WebViewUpdateService extends SystemService {
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010048
49 private static final String TAG = "WebViewUpdateService";
Gustav Sennton6ce92c92015-10-23 11:10:39 +010050
Ben Murdochdc00a842014-07-17 14:55:00 +010051 private BroadcastReceiver mWebViewUpdatedReceiver;
Gustav Sennton79fea482016-04-07 14:22:56 +010052 private WebViewUpdateServiceImpl mImpl;
Ben Murdochdc00a842014-07-17 14:55:00 +010053
Gustav Sennton3a6e6b22016-04-05 14:09:09 +010054 static final int PACKAGE_CHANGED = 0;
55 static final int PACKAGE_ADDED = 1;
56 static final int PACKAGE_ADDED_REPLACED = 2;
57 static final int PACKAGE_REMOVED = 3;
58
Ben Murdochdc00a842014-07-17 14:55:00 +010059 public WebViewUpdateService(Context context) {
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +010060 super(context);
Hui Shu9455bd02016-04-08 13:25:26 -070061 mImpl = new WebViewUpdateServiceImpl(context, SystemImpl.getInstance());
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +010062 }
63
64 @Override
65 public void onStart() {
Ben Murdochdc00a842014-07-17 14:55:00 +010066 mWebViewUpdatedReceiver = new BroadcastReceiver() {
67 @Override
68 public void onReceive(Context context, Intent intent) {
Gustav Sennton0df2c552016-06-14 15:32:19 +010069 int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL);
Gustav Sennton3a6e6b22016-04-05 14:09:09 +010070 switch (intent.getAction()) {
71 case Intent.ACTION_PACKAGE_REMOVED:
72 // When a package is replaced we will receive two intents, one
73 // representing the removal of the old package and one representing the
74 // addition of the new package.
75 // In the case where we receive an intent to remove the old version of
76 // the package that is being replaced we early-out here so that we don't
77 // run the update-logic twice.
78 if (intent.getExtras().getBoolean(Intent.EXTRA_REPLACING)) return;
Gustav Sennton79fea482016-04-07 14:22:56 +010079 mImpl.packageStateChanged(packageNameFromIntent(intent),
Gustav Sennton0df2c552016-06-14 15:32:19 +010080 PACKAGE_REMOVED, userId);
Gustav Sennton3a6e6b22016-04-05 14:09:09 +010081 break;
82 case Intent.ACTION_PACKAGE_CHANGED:
83 // Ensure that we only heed PACKAGE_CHANGED intents if they change an
84 // entire package, not just a component
85 if (entirePackageChanged(intent)) {
Gustav Sennton79fea482016-04-07 14:22:56 +010086 mImpl.packageStateChanged(packageNameFromIntent(intent),
Gustav Sennton0df2c552016-06-14 15:32:19 +010087 PACKAGE_CHANGED, userId);
Gustav Sennton6258dcd2015-10-30 19:25:37 +000088 }
Gustav Sennton3a6e6b22016-04-05 14:09:09 +010089 break;
90 case Intent.ACTION_PACKAGE_ADDED:
Gustav Sennton79fea482016-04-07 14:22:56 +010091 mImpl.packageStateChanged(packageNameFromIntent(intent),
Gustav Sennton3a6e6b22016-04-05 14:09:09 +010092 (intent.getExtras().getBoolean(Intent.EXTRA_REPLACING)
Gustav Sennton0df2c552016-06-14 15:32:19 +010093 ? PACKAGE_ADDED_REPLACED : PACKAGE_ADDED), userId);
Gustav Sennton3a6e6b22016-04-05 14:09:09 +010094 break;
Gustav Senntona43f1442017-03-23 17:04:23 +000095 case Intent.ACTION_USER_STARTED:
Gustav Sennton79fea482016-04-07 14:22:56 +010096 mImpl.handleNewUser(userId);
Gustav Sennton3a6e6b22016-04-05 14:09:09 +010097 break;
Gustav Sennton364e1602016-12-14 09:10:50 +000098 case Intent.ACTION_USER_REMOVED:
99 mImpl.handleUserRemoved(userId);
100 break;
Ben Murdochdc00a842014-07-17 14:55:00 +0100101 }
102 }
103 };
104 IntentFilter filter = new IntentFilter();
Gustav Sennton3098cf22015-11-10 03:33:09 +0000105 filter.addAction(Intent.ACTION_PACKAGE_ADDED);
106 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000107 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Ben Murdochdc00a842014-07-17 14:55:00 +0100108 filter.addDataScheme("package");
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000109 // Make sure we only receive intents for WebView packages from our config file.
Gustav Sennton79fea482016-04-07 14:22:56 +0100110 for (WebViewProviderInfo provider : mImpl.getWebViewPackages()) {
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000111 filter.addDataSchemeSpecificPart(provider.packageName, PatternMatcher.PATTERN_LITERAL);
112 }
Gustav Sennton0df2c552016-06-14 15:32:19 +0100113
114 getContext().registerReceiverAsUser(mWebViewUpdatedReceiver, UserHandle.ALL, filter,
115 null /* broadcast permission */, null /* handler */);
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100116
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000117 IntentFilter userAddedFilter = new IntentFilter();
Gustav Senntona43f1442017-03-23 17:04:23 +0000118 userAddedFilter.addAction(Intent.ACTION_USER_STARTED);
Gustav Sennton364e1602016-12-14 09:10:50 +0000119 userAddedFilter.addAction(Intent.ACTION_USER_REMOVED);
Gustav Sennton0df2c552016-06-14 15:32:19 +0100120 getContext().registerReceiverAsUser(mWebViewUpdatedReceiver, UserHandle.ALL,
121 userAddedFilter, null /* broadcast permission */, null /* handler */);
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000122
Torne (Richard Coles)fc19b0a2016-02-01 16:16:57 +0000123 publishBinderService("webviewupdate", new BinderService(), true /*allowIsolated*/);
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100124 }
Ben Murdochdc00a842014-07-17 14:55:00 +0100125
Gustav Sennton3a6e6b22016-04-05 14:09:09 +0100126 public void prepareWebViewInSystemServer() {
Gustav Sennton79fea482016-04-07 14:22:56 +0100127 mImpl.prepareWebViewInSystemServer();
Gustav Sennton3a6e6b22016-04-05 14:09:09 +0100128 }
129
130 private static String packageNameFromIntent(Intent intent) {
131 return intent.getDataString().substring("package:".length());
132 }
133
Gustav Sennton065b7e62016-04-01 15:11:43 +0100134 /**
135 * Returns whether the entire package from an ACTION_PACKAGE_CHANGED intent was changed (rather
136 * than just one of its components).
137 * @hide
138 */
139 public static boolean entirePackageChanged(Intent intent) {
140 String[] componentList =
141 intent.getStringArrayExtra(Intent.EXTRA_CHANGED_COMPONENT_NAME_LIST);
142 return Arrays.asList(componentList).contains(
143 intent.getDataString().substring("package:".length()));
Gustav Senntondbf5eb02016-03-30 14:53:03 +0100144 }
145
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100146 private class BinderService extends IWebViewUpdateService.Stub {
147
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000148 @Override
149 public void onShellCommand(FileDescriptor in, FileDescriptor out,
Dianne Hackborn354736e2016-08-22 17:00:05 -0700150 FileDescriptor err, String[] args, ShellCallback callback,
151 ResultReceiver resultReceiver) {
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000152 (new WebViewUpdateServiceShellCommand(this)).exec(
Dianne Hackborn354736e2016-08-22 17:00:05 -0700153 this, in, out, err, args, callback, resultReceiver);
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000154 }
155
156
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100157 /**
158 * The shared relro process calls this to notify us that it's done trying to create a relro
159 * file. This method gets called even if the relro creation has failed or the process
160 * crashed.
161 */
162 @Override // Binder call
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000163 public void notifyRelroCreationCompleted() {
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100164 // Verify that the caller is either the shared relro process (nominal case) or the
165 // system server (only in the case the relro process crashes and we get here via the
166 // crashHandler).
167 if (Binder.getCallingUid() != Process.SHARED_RELRO_UID &&
168 Binder.getCallingUid() != Process.SYSTEM_UID) {
169 return;
170 }
171
Gustav Sennton275d13c2016-02-24 10:58:09 +0000172 long callingId = Binder.clearCallingIdentity();
173 try {
Gustav Sennton79fea482016-04-07 14:22:56 +0100174 WebViewUpdateService.this.mImpl.notifyRelroCreationCompleted();
Gustav Sennton275d13c2016-02-24 10:58:09 +0000175 } finally {
176 Binder.restoreCallingIdentity(callingId);
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100177 }
178 }
179
180 /**
181 * WebViewFactory calls this to block WebView loading until the relro file is created.
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000182 * Returns the WebView provider for which we create relro files.
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100183 */
184 @Override // Binder call
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000185 public WebViewProviderResponse waitForAndGetProvider() {
Primiano Tuccie76e81a2014-07-29 16:38:33 +0100186 // The WebViewUpdateService depends on the prepareWebViewInSystemServer call, which
187 // happens later (during the PHASE_ACTIVITY_MANAGER_READY) in SystemServer.java. If
188 // another service there tries to bring up a WebView in the between, the wait below
189 // would deadlock without the check below.
190 if (Binder.getCallingPid() == Process.myPid()) {
191 throw new IllegalStateException("Cannot create a WebView from the SystemServer");
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100192 }
193
Gustav Sennton79fea482016-04-07 14:22:56 +0100194 return WebViewUpdateService.this.mImpl.waitForAndGetProvider();
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000195 }
196
197 /**
198 * This is called from DeveloperSettings when the user changes WebView provider.
199 */
200 @Override // Binder call
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000201 public String changeProviderAndSetting(String newProvider) {
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000202 if (getContext().checkCallingPermission(
203 android.Manifest.permission.WRITE_SECURE_SETTINGS)
204 != PackageManager.PERMISSION_GRANTED) {
205 String msg = "Permission Denial: changeProviderAndSetting() from pid="
206 + Binder.getCallingPid()
207 + ", uid=" + Binder.getCallingUid()
208 + " requires " + android.Manifest.permission.WRITE_SECURE_SETTINGS;
209 Slog.w(TAG, msg);
210 throw new SecurityException(msg);
211 }
212
Gustav Senntonab3b6b12016-03-16 17:38:42 +0000213 long callingId = Binder.clearCallingIdentity();
214 try {
Gustav Sennton79fea482016-04-07 14:22:56 +0100215 return WebViewUpdateService.this.mImpl.changeProviderAndSetting(
Gustav Sennton3a6e6b22016-04-05 14:09:09 +0100216 newProvider);
Gustav Senntonab3b6b12016-03-16 17:38:42 +0000217 } finally {
218 Binder.restoreCallingIdentity(callingId);
219 }
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000220 }
221
222 @Override // Binder call
223 public WebViewProviderInfo[] getValidWebViewPackages() {
Gustav Sennton79fea482016-04-07 14:22:56 +0100224 return WebViewUpdateService.this.mImpl.getValidWebViewPackages();
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000225 }
226
227 @Override // Binder call
Gustav Sennton8b179262016-03-14 11:31:14 +0000228 public WebViewProviderInfo[] getAllWebViewPackages() {
Gustav Sennton79fea482016-04-07 14:22:56 +0100229 return WebViewUpdateService.this.mImpl.getWebViewPackages();
Gustav Sennton8b179262016-03-14 11:31:14 +0000230 }
231
232 @Override // Binder call
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000233 public String getCurrentWebViewPackageName() {
Gustav Senntonbf683e02016-09-15 14:42:50 +0100234 PackageInfo pi = WebViewUpdateService.this.mImpl.getCurrentWebViewPackage();
235 return pi == null ? null : pi.packageName;
236 }
237
238 @Override // Binder call
239 public PackageInfo getCurrentWebViewPackage() {
240 return WebViewUpdateService.this.mImpl.getCurrentWebViewPackage();
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100241 }
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000242
243 @Override // Binder call
244 public boolean isFallbackPackage(String packageName) {
Gustav Sennton79fea482016-04-07 14:22:56 +0100245 return WebViewUpdateService.this.mImpl.isFallbackPackage(packageName);
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000246 }
247
248 @Override // Binder call
249 public void enableFallbackLogic(boolean enable) {
250 if (getContext().checkCallingPermission(
251 android.Manifest.permission.WRITE_SECURE_SETTINGS)
252 != PackageManager.PERMISSION_GRANTED) {
253 String msg = "Permission Denial: enableFallbackLogic() from pid="
254 + Binder.getCallingPid()
255 + ", uid=" + Binder.getCallingUid()
256 + " requires " + android.Manifest.permission.WRITE_SECURE_SETTINGS;
257 Slog.w(TAG, msg);
258 throw new SecurityException(msg);
259 }
260
Gustav Sennton6824c7c2016-04-04 14:07:23 +0100261 long callingId = Binder.clearCallingIdentity();
262 try {
263 WebViewUpdateService.this.mImpl.enableFallbackLogic(enable);
264 } finally {
265 Binder.restoreCallingIdentity(callingId);
266 }
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000267 }
Gustav Sennton1eb38202016-10-21 13:40:10 +0100268
Torne (Richard Coles)1a4c4e32017-01-10 15:57:41 +0000269 @Override // Binder call
270 public boolean isMultiProcessEnabled() {
271 return WebViewUpdateService.this.mImpl.isMultiProcessEnabled();
272 }
273
274 @Override // Binder call
275 public void enableMultiProcess(boolean enable) {
276 if (getContext().checkCallingPermission(
277 android.Manifest.permission.WRITE_SECURE_SETTINGS)
278 != PackageManager.PERMISSION_GRANTED) {
279 String msg = "Permission Denial: enableMultiProcess() from pid="
280 + Binder.getCallingPid()
281 + ", uid=" + Binder.getCallingUid()
282 + " requires " + android.Manifest.permission.WRITE_SECURE_SETTINGS;
283 Slog.w(TAG, msg);
284 throw new SecurityException(msg);
285 }
286
287 long callingId = Binder.clearCallingIdentity();
288 try {
289 WebViewUpdateService.this.mImpl.enableMultiProcess(enable);
290 } finally {
291 Binder.restoreCallingIdentity(callingId);
292 }
293 }
294
Gustav Sennton1eb38202016-10-21 13:40:10 +0100295 @Override
296 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyfe9a53b2017-03-31 14:08:23 -0600297 if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) return;
Gustav Sennton1eb38202016-10-21 13:40:10 +0100298 WebViewUpdateService.this.mImpl.dumpState(pw);
299 }
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100300 }
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100301}