blob: 311570e893cff5580f12f6b10761784818919afc [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
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +010036import com.android.server.SystemService;
37
Gustav Senntonc83e3fa2016-02-18 12:19:13 +000038import java.io.FileDescriptor;
Gustav Sennton1eb38202016-10-21 13:40:10 +010039import java.io.PrintWriter;
Gustav Sennton6258dcd2015-10-30 19:25:37 +000040import java.util.Arrays;
Gustav Sennton6258dcd2015-10-30 19:25:37 +000041
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010042/**
43 * Private service to wait for the updatable WebView to be ready for use.
44 * @hide
45 */
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +010046public class WebViewUpdateService extends SystemService {
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010047
48 private static final String TAG = "WebViewUpdateService";
Gustav Sennton6ce92c92015-10-23 11:10:39 +010049
Ben Murdochdc00a842014-07-17 14:55:00 +010050 private BroadcastReceiver mWebViewUpdatedReceiver;
Gustav Sennton79fea482016-04-07 14:22:56 +010051 private WebViewUpdateServiceImpl mImpl;
Ben Murdochdc00a842014-07-17 14:55:00 +010052
Gustav Sennton3a6e6b22016-04-05 14:09:09 +010053 static final int PACKAGE_CHANGED = 0;
54 static final int PACKAGE_ADDED = 1;
55 static final int PACKAGE_ADDED_REPLACED = 2;
56 static final int PACKAGE_REMOVED = 3;
57
Ben Murdochdc00a842014-07-17 14:55:00 +010058 public WebViewUpdateService(Context context) {
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +010059 super(context);
Hui Shu9455bd02016-04-08 13:25:26 -070060 mImpl = new WebViewUpdateServiceImpl(context, SystemImpl.getInstance());
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +010061 }
62
63 @Override
64 public void onStart() {
Ben Murdochdc00a842014-07-17 14:55:00 +010065 mWebViewUpdatedReceiver = new BroadcastReceiver() {
66 @Override
67 public void onReceive(Context context, Intent intent) {
Gustav Sennton0df2c552016-06-14 15:32:19 +010068 int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL);
Gustav Sennton3a6e6b22016-04-05 14:09:09 +010069 switch (intent.getAction()) {
70 case Intent.ACTION_PACKAGE_REMOVED:
71 // When a package is replaced we will receive two intents, one
72 // representing the removal of the old package and one representing the
73 // addition of the new package.
74 // In the case where we receive an intent to remove the old version of
75 // the package that is being replaced we early-out here so that we don't
76 // run the update-logic twice.
77 if (intent.getExtras().getBoolean(Intent.EXTRA_REPLACING)) return;
Gustav Sennton79fea482016-04-07 14:22:56 +010078 mImpl.packageStateChanged(packageNameFromIntent(intent),
Gustav Sennton0df2c552016-06-14 15:32:19 +010079 PACKAGE_REMOVED, userId);
Gustav Sennton3a6e6b22016-04-05 14:09:09 +010080 break;
81 case Intent.ACTION_PACKAGE_CHANGED:
82 // Ensure that we only heed PACKAGE_CHANGED intents if they change an
83 // entire package, not just a component
84 if (entirePackageChanged(intent)) {
Gustav Sennton79fea482016-04-07 14:22:56 +010085 mImpl.packageStateChanged(packageNameFromIntent(intent),
Gustav Sennton0df2c552016-06-14 15:32:19 +010086 PACKAGE_CHANGED, userId);
Gustav Sennton6258dcd2015-10-30 19:25:37 +000087 }
Gustav Sennton3a6e6b22016-04-05 14:09:09 +010088 break;
89 case Intent.ACTION_PACKAGE_ADDED:
Gustav Sennton79fea482016-04-07 14:22:56 +010090 mImpl.packageStateChanged(packageNameFromIntent(intent),
Gustav Sennton3a6e6b22016-04-05 14:09:09 +010091 (intent.getExtras().getBoolean(Intent.EXTRA_REPLACING)
Gustav Sennton0df2c552016-06-14 15:32:19 +010092 ? PACKAGE_ADDED_REPLACED : PACKAGE_ADDED), userId);
Gustav Sennton3a6e6b22016-04-05 14:09:09 +010093 break;
94 case Intent.ACTION_USER_ADDED:
Gustav Sennton79fea482016-04-07 14:22:56 +010095 mImpl.handleNewUser(userId);
Gustav Sennton3a6e6b22016-04-05 14:09:09 +010096 break;
Ben Murdochdc00a842014-07-17 14:55:00 +010097 }
98 }
99 };
100 IntentFilter filter = new IntentFilter();
Gustav Sennton3098cf22015-11-10 03:33:09 +0000101 filter.addAction(Intent.ACTION_PACKAGE_ADDED);
102 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000103 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Ben Murdochdc00a842014-07-17 14:55:00 +0100104 filter.addDataScheme("package");
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000105 // Make sure we only receive intents for WebView packages from our config file.
Gustav Sennton79fea482016-04-07 14:22:56 +0100106 for (WebViewProviderInfo provider : mImpl.getWebViewPackages()) {
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000107 filter.addDataSchemeSpecificPart(provider.packageName, PatternMatcher.PATTERN_LITERAL);
108 }
Gustav Sennton0df2c552016-06-14 15:32:19 +0100109
110 getContext().registerReceiverAsUser(mWebViewUpdatedReceiver, UserHandle.ALL, filter,
111 null /* broadcast permission */, null /* handler */);
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100112
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000113 IntentFilter userAddedFilter = new IntentFilter();
114 userAddedFilter.addAction(Intent.ACTION_USER_ADDED);
Gustav Sennton0df2c552016-06-14 15:32:19 +0100115 getContext().registerReceiverAsUser(mWebViewUpdatedReceiver, UserHandle.ALL,
116 userAddedFilter, null /* broadcast permission */, null /* handler */);
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000117
Torne (Richard Coles)fc19b0a2016-02-01 16:16:57 +0000118 publishBinderService("webviewupdate", new BinderService(), true /*allowIsolated*/);
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100119 }
Ben Murdochdc00a842014-07-17 14:55:00 +0100120
Gustav Sennton3a6e6b22016-04-05 14:09:09 +0100121 public void prepareWebViewInSystemServer() {
Gustav Sennton79fea482016-04-07 14:22:56 +0100122 mImpl.prepareWebViewInSystemServer();
Gustav Sennton3a6e6b22016-04-05 14:09:09 +0100123 }
124
125 private static String packageNameFromIntent(Intent intent) {
126 return intent.getDataString().substring("package:".length());
127 }
128
Gustav Sennton065b7e62016-04-01 15:11:43 +0100129 /**
130 * Returns whether the entire package from an ACTION_PACKAGE_CHANGED intent was changed (rather
131 * than just one of its components).
132 * @hide
133 */
134 public static boolean entirePackageChanged(Intent intent) {
135 String[] componentList =
136 intent.getStringArrayExtra(Intent.EXTRA_CHANGED_COMPONENT_NAME_LIST);
137 return Arrays.asList(componentList).contains(
138 intent.getDataString().substring("package:".length()));
Gustav Senntondbf5eb02016-03-30 14:53:03 +0100139 }
140
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100141 private class BinderService extends IWebViewUpdateService.Stub {
142
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000143 @Override
144 public void onShellCommand(FileDescriptor in, FileDescriptor out,
Dianne Hackborn354736e2016-08-22 17:00:05 -0700145 FileDescriptor err, String[] args, ShellCallback callback,
146 ResultReceiver resultReceiver) {
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000147 (new WebViewUpdateServiceShellCommand(this)).exec(
Dianne Hackborn354736e2016-08-22 17:00:05 -0700148 this, in, out, err, args, callback, resultReceiver);
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000149 }
150
151
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100152 /**
153 * The shared relro process calls this to notify us that it's done trying to create a relro
154 * file. This method gets called even if the relro creation has failed or the process
155 * crashed.
156 */
157 @Override // Binder call
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000158 public void notifyRelroCreationCompleted() {
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100159 // Verify that the caller is either the shared relro process (nominal case) or the
160 // system server (only in the case the relro process crashes and we get here via the
161 // crashHandler).
162 if (Binder.getCallingUid() != Process.SHARED_RELRO_UID &&
163 Binder.getCallingUid() != Process.SYSTEM_UID) {
164 return;
165 }
166
Gustav Sennton275d13c2016-02-24 10:58:09 +0000167 long callingId = Binder.clearCallingIdentity();
168 try {
Gustav Sennton79fea482016-04-07 14:22:56 +0100169 WebViewUpdateService.this.mImpl.notifyRelroCreationCompleted();
Gustav Sennton275d13c2016-02-24 10:58:09 +0000170 } finally {
171 Binder.restoreCallingIdentity(callingId);
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100172 }
173 }
174
175 /**
176 * WebViewFactory calls this to block WebView loading until the relro file is created.
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000177 * Returns the WebView provider for which we create relro files.
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100178 */
179 @Override // Binder call
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000180 public WebViewProviderResponse waitForAndGetProvider() {
Primiano Tuccie76e81a2014-07-29 16:38:33 +0100181 // The WebViewUpdateService depends on the prepareWebViewInSystemServer call, which
182 // happens later (during the PHASE_ACTIVITY_MANAGER_READY) in SystemServer.java. If
183 // another service there tries to bring up a WebView in the between, the wait below
184 // would deadlock without the check below.
185 if (Binder.getCallingPid() == Process.myPid()) {
186 throw new IllegalStateException("Cannot create a WebView from the SystemServer");
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100187 }
188
Gustav Sennton79fea482016-04-07 14:22:56 +0100189 return WebViewUpdateService.this.mImpl.waitForAndGetProvider();
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000190 }
191
192 /**
193 * This is called from DeveloperSettings when the user changes WebView provider.
194 */
195 @Override // Binder call
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000196 public String changeProviderAndSetting(String newProvider) {
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000197 if (getContext().checkCallingPermission(
198 android.Manifest.permission.WRITE_SECURE_SETTINGS)
199 != PackageManager.PERMISSION_GRANTED) {
200 String msg = "Permission Denial: changeProviderAndSetting() from pid="
201 + Binder.getCallingPid()
202 + ", uid=" + Binder.getCallingUid()
203 + " requires " + android.Manifest.permission.WRITE_SECURE_SETTINGS;
204 Slog.w(TAG, msg);
205 throw new SecurityException(msg);
206 }
207
Gustav Senntonab3b6b12016-03-16 17:38:42 +0000208 long callingId = Binder.clearCallingIdentity();
209 try {
Gustav Sennton79fea482016-04-07 14:22:56 +0100210 return WebViewUpdateService.this.mImpl.changeProviderAndSetting(
Gustav Sennton3a6e6b22016-04-05 14:09:09 +0100211 newProvider);
Gustav Senntonab3b6b12016-03-16 17:38:42 +0000212 } finally {
213 Binder.restoreCallingIdentity(callingId);
214 }
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000215 }
216
217 @Override // Binder call
218 public WebViewProviderInfo[] getValidWebViewPackages() {
Gustav Sennton79fea482016-04-07 14:22:56 +0100219 return WebViewUpdateService.this.mImpl.getValidWebViewPackages();
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000220 }
221
222 @Override // Binder call
Gustav Sennton8b179262016-03-14 11:31:14 +0000223 public WebViewProviderInfo[] getAllWebViewPackages() {
Gustav Sennton79fea482016-04-07 14:22:56 +0100224 return WebViewUpdateService.this.mImpl.getWebViewPackages();
Gustav Sennton8b179262016-03-14 11:31:14 +0000225 }
226
227 @Override // Binder call
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000228 public String getCurrentWebViewPackageName() {
Gustav Senntonbf683e02016-09-15 14:42:50 +0100229 PackageInfo pi = WebViewUpdateService.this.mImpl.getCurrentWebViewPackage();
230 return pi == null ? null : pi.packageName;
231 }
232
233 @Override // Binder call
234 public PackageInfo getCurrentWebViewPackage() {
235 return WebViewUpdateService.this.mImpl.getCurrentWebViewPackage();
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100236 }
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000237
238 @Override // Binder call
239 public boolean isFallbackPackage(String packageName) {
Gustav Sennton79fea482016-04-07 14:22:56 +0100240 return WebViewUpdateService.this.mImpl.isFallbackPackage(packageName);
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000241 }
242
243 @Override // Binder call
244 public void enableFallbackLogic(boolean enable) {
245 if (getContext().checkCallingPermission(
246 android.Manifest.permission.WRITE_SECURE_SETTINGS)
247 != PackageManager.PERMISSION_GRANTED) {
248 String msg = "Permission Denial: enableFallbackLogic() from pid="
249 + Binder.getCallingPid()
250 + ", uid=" + Binder.getCallingUid()
251 + " requires " + android.Manifest.permission.WRITE_SECURE_SETTINGS;
252 Slog.w(TAG, msg);
253 throw new SecurityException(msg);
254 }
255
Gustav Sennton6824c7c2016-04-04 14:07:23 +0100256 long callingId = Binder.clearCallingIdentity();
257 try {
258 WebViewUpdateService.this.mImpl.enableFallbackLogic(enable);
259 } finally {
260 Binder.restoreCallingIdentity(callingId);
261 }
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000262 }
Gustav Sennton1eb38202016-10-21 13:40:10 +0100263
Torne (Richard Coles)1a4c4e32017-01-10 15:57:41 +0000264 @Override // Binder call
265 public boolean isMultiProcessEnabled() {
266 return WebViewUpdateService.this.mImpl.isMultiProcessEnabled();
267 }
268
269 @Override // Binder call
270 public void enableMultiProcess(boolean enable) {
271 if (getContext().checkCallingPermission(
272 android.Manifest.permission.WRITE_SECURE_SETTINGS)
273 != PackageManager.PERMISSION_GRANTED) {
274 String msg = "Permission Denial: enableMultiProcess() from pid="
275 + Binder.getCallingPid()
276 + ", uid=" + Binder.getCallingUid()
277 + " requires " + android.Manifest.permission.WRITE_SECURE_SETTINGS;
278 Slog.w(TAG, msg);
279 throw new SecurityException(msg);
280 }
281
282 long callingId = Binder.clearCallingIdentity();
283 try {
284 WebViewUpdateService.this.mImpl.enableMultiProcess(enable);
285 } finally {
286 Binder.restoreCallingIdentity(callingId);
287 }
288 }
289
Gustav Sennton1eb38202016-10-21 13:40:10 +0100290 @Override
291 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
292 if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
293 != PackageManager.PERMISSION_GRANTED) {
294
295 pw.println("Permission Denial: can't dump webviewupdate service from pid="
296 + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid());
297 return;
298 }
299
300 WebViewUpdateService.this.mImpl.dumpState(pw);
301 }
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100302 }
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100303}