blob: 6d97796d3d3ea5182dfda3d9b1147087e8cb757b [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 Sennton6258dcd2015-10-30 19:25:37 +000039import java.util.Arrays;
Gustav Sennton6258dcd2015-10-30 19:25:37 +000040
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010041/**
42 * Private service to wait for the updatable WebView to be ready for use.
43 * @hide
44 */
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +010045public class WebViewUpdateService extends SystemService {
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +010046
47 private static final String TAG = "WebViewUpdateService";
Gustav Sennton6ce92c92015-10-23 11:10:39 +010048
Ben Murdochdc00a842014-07-17 14:55:00 +010049 private BroadcastReceiver mWebViewUpdatedReceiver;
Gustav Sennton79fea482016-04-07 14:22:56 +010050 private WebViewUpdateServiceImpl mImpl;
Ben Murdochdc00a842014-07-17 14:55:00 +010051
Gustav Sennton3a6e6b22016-04-05 14:09:09 +010052 static final int PACKAGE_CHANGED = 0;
53 static final int PACKAGE_ADDED = 1;
54 static final int PACKAGE_ADDED_REPLACED = 2;
55 static final int PACKAGE_REMOVED = 3;
56
Ben Murdochdc00a842014-07-17 14:55:00 +010057 public WebViewUpdateService(Context context) {
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +010058 super(context);
Hui Shu9455bd02016-04-08 13:25:26 -070059 mImpl = new WebViewUpdateServiceImpl(context, SystemImpl.getInstance());
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +010060 }
61
62 @Override
63 public void onStart() {
Ben Murdochdc00a842014-07-17 14:55:00 +010064 mWebViewUpdatedReceiver = new BroadcastReceiver() {
65 @Override
66 public void onReceive(Context context, Intent intent) {
Gustav Sennton0df2c552016-06-14 15:32:19 +010067 int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL);
Gustav Sennton3a6e6b22016-04-05 14:09:09 +010068 switch (intent.getAction()) {
69 case Intent.ACTION_PACKAGE_REMOVED:
70 // When a package is replaced we will receive two intents, one
71 // representing the removal of the old package and one representing the
72 // addition of the new package.
73 // In the case where we receive an intent to remove the old version of
74 // the package that is being replaced we early-out here so that we don't
75 // run the update-logic twice.
76 if (intent.getExtras().getBoolean(Intent.EXTRA_REPLACING)) return;
Gustav Sennton79fea482016-04-07 14:22:56 +010077 mImpl.packageStateChanged(packageNameFromIntent(intent),
Gustav Sennton0df2c552016-06-14 15:32:19 +010078 PACKAGE_REMOVED, userId);
Gustav Sennton3a6e6b22016-04-05 14:09:09 +010079 break;
80 case Intent.ACTION_PACKAGE_CHANGED:
81 // Ensure that we only heed PACKAGE_CHANGED intents if they change an
82 // entire package, not just a component
83 if (entirePackageChanged(intent)) {
Gustav Sennton79fea482016-04-07 14:22:56 +010084 mImpl.packageStateChanged(packageNameFromIntent(intent),
Gustav Sennton0df2c552016-06-14 15:32:19 +010085 PACKAGE_CHANGED, userId);
Gustav Sennton6258dcd2015-10-30 19:25:37 +000086 }
Gustav Sennton3a6e6b22016-04-05 14:09:09 +010087 break;
88 case Intent.ACTION_PACKAGE_ADDED:
Gustav Sennton79fea482016-04-07 14:22:56 +010089 mImpl.packageStateChanged(packageNameFromIntent(intent),
Gustav Sennton3a6e6b22016-04-05 14:09:09 +010090 (intent.getExtras().getBoolean(Intent.EXTRA_REPLACING)
Gustav Sennton0df2c552016-06-14 15:32:19 +010091 ? PACKAGE_ADDED_REPLACED : PACKAGE_ADDED), userId);
Gustav Sennton3a6e6b22016-04-05 14:09:09 +010092 break;
93 case Intent.ACTION_USER_ADDED:
Gustav Sennton79fea482016-04-07 14:22:56 +010094 mImpl.handleNewUser(userId);
Gustav Sennton3a6e6b22016-04-05 14:09:09 +010095 break;
Ben Murdochdc00a842014-07-17 14:55:00 +010096 }
97 }
98 };
99 IntentFilter filter = new IntentFilter();
Gustav Sennton3098cf22015-11-10 03:33:09 +0000100 filter.addAction(Intent.ACTION_PACKAGE_ADDED);
101 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000102 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Ben Murdochdc00a842014-07-17 14:55:00 +0100103 filter.addDataScheme("package");
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000104 // Make sure we only receive intents for WebView packages from our config file.
Gustav Sennton79fea482016-04-07 14:22:56 +0100105 for (WebViewProviderInfo provider : mImpl.getWebViewPackages()) {
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000106 filter.addDataSchemeSpecificPart(provider.packageName, PatternMatcher.PATTERN_LITERAL);
107 }
Gustav Sennton0df2c552016-06-14 15:32:19 +0100108
109 getContext().registerReceiverAsUser(mWebViewUpdatedReceiver, UserHandle.ALL, filter,
110 null /* broadcast permission */, null /* handler */);
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100111
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000112 IntentFilter userAddedFilter = new IntentFilter();
113 userAddedFilter.addAction(Intent.ACTION_USER_ADDED);
Gustav Sennton0df2c552016-06-14 15:32:19 +0100114 getContext().registerReceiverAsUser(mWebViewUpdatedReceiver, UserHandle.ALL,
115 userAddedFilter, null /* broadcast permission */, null /* handler */);
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000116
Torne (Richard Coles)fc19b0a2016-02-01 16:16:57 +0000117 publishBinderService("webviewupdate", new BinderService(), true /*allowIsolated*/);
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100118 }
Ben Murdochdc00a842014-07-17 14:55:00 +0100119
Gustav Sennton3a6e6b22016-04-05 14:09:09 +0100120 public void prepareWebViewInSystemServer() {
Gustav Sennton79fea482016-04-07 14:22:56 +0100121 mImpl.prepareWebViewInSystemServer();
Gustav Sennton3a6e6b22016-04-05 14:09:09 +0100122 }
123
124 private static String packageNameFromIntent(Intent intent) {
125 return intent.getDataString().substring("package:".length());
126 }
127
Gustav Sennton065b7e62016-04-01 15:11:43 +0100128 /**
129 * Returns whether the entire package from an ACTION_PACKAGE_CHANGED intent was changed (rather
130 * than just one of its components).
131 * @hide
132 */
133 public static boolean entirePackageChanged(Intent intent) {
134 String[] componentList =
135 intent.getStringArrayExtra(Intent.EXTRA_CHANGED_COMPONENT_NAME_LIST);
136 return Arrays.asList(componentList).contains(
137 intent.getDataString().substring("package:".length()));
Gustav Senntondbf5eb02016-03-30 14:53:03 +0100138 }
139
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100140 private class BinderService extends IWebViewUpdateService.Stub {
141
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000142 @Override
143 public void onShellCommand(FileDescriptor in, FileDescriptor out,
Dianne Hackborn354736e2016-08-22 17:00:05 -0700144 FileDescriptor err, String[] args, ShellCallback callback,
145 ResultReceiver resultReceiver) {
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000146 (new WebViewUpdateServiceShellCommand(this)).exec(
Dianne Hackborn354736e2016-08-22 17:00:05 -0700147 this, in, out, err, args, callback, resultReceiver);
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000148 }
149
150
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100151 /**
152 * The shared relro process calls this to notify us that it's done trying to create a relro
153 * file. This method gets called even if the relro creation has failed or the process
154 * crashed.
155 */
156 @Override // Binder call
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000157 public void notifyRelroCreationCompleted() {
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100158 // Verify that the caller is either the shared relro process (nominal case) or the
159 // system server (only in the case the relro process crashes and we get here via the
160 // crashHandler).
161 if (Binder.getCallingUid() != Process.SHARED_RELRO_UID &&
162 Binder.getCallingUid() != Process.SYSTEM_UID) {
163 return;
164 }
165
Gustav Sennton275d13c2016-02-24 10:58:09 +0000166 long callingId = Binder.clearCallingIdentity();
167 try {
Gustav Sennton79fea482016-04-07 14:22:56 +0100168 WebViewUpdateService.this.mImpl.notifyRelroCreationCompleted();
Gustav Sennton275d13c2016-02-24 10:58:09 +0000169 } finally {
170 Binder.restoreCallingIdentity(callingId);
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100171 }
172 }
173
174 /**
175 * WebViewFactory calls this to block WebView loading until the relro file is created.
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000176 * Returns the WebView provider for which we create relro files.
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100177 */
178 @Override // Binder call
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000179 public WebViewProviderResponse waitForAndGetProvider() {
Primiano Tuccie76e81a2014-07-29 16:38:33 +0100180 // The WebViewUpdateService depends on the prepareWebViewInSystemServer call, which
181 // happens later (during the PHASE_ACTIVITY_MANAGER_READY) in SystemServer.java. If
182 // another service there tries to bring up a WebView in the between, the wait below
183 // would deadlock without the check below.
184 if (Binder.getCallingPid() == Process.myPid()) {
185 throw new IllegalStateException("Cannot create a WebView from the SystemServer");
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100186 }
187
Gustav Sennton79fea482016-04-07 14:22:56 +0100188 return WebViewUpdateService.this.mImpl.waitForAndGetProvider();
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000189 }
190
191 /**
192 * This is called from DeveloperSettings when the user changes WebView provider.
193 */
194 @Override // Binder call
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000195 public String changeProviderAndSetting(String newProvider) {
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000196 if (getContext().checkCallingPermission(
197 android.Manifest.permission.WRITE_SECURE_SETTINGS)
198 != PackageManager.PERMISSION_GRANTED) {
199 String msg = "Permission Denial: changeProviderAndSetting() from pid="
200 + Binder.getCallingPid()
201 + ", uid=" + Binder.getCallingUid()
202 + " requires " + android.Manifest.permission.WRITE_SECURE_SETTINGS;
203 Slog.w(TAG, msg);
204 throw new SecurityException(msg);
205 }
206
Gustav Senntonab3b6b12016-03-16 17:38:42 +0000207 long callingId = Binder.clearCallingIdentity();
208 try {
Gustav Sennton79fea482016-04-07 14:22:56 +0100209 return WebViewUpdateService.this.mImpl.changeProviderAndSetting(
Gustav Sennton3a6e6b22016-04-05 14:09:09 +0100210 newProvider);
Gustav Senntonab3b6b12016-03-16 17:38:42 +0000211 } finally {
212 Binder.restoreCallingIdentity(callingId);
213 }
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000214 }
215
216 @Override // Binder call
217 public WebViewProviderInfo[] getValidWebViewPackages() {
Gustav Sennton79fea482016-04-07 14:22:56 +0100218 return WebViewUpdateService.this.mImpl.getValidWebViewPackages();
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000219 }
220
221 @Override // Binder call
Gustav Sennton8b179262016-03-14 11:31:14 +0000222 public WebViewProviderInfo[] getAllWebViewPackages() {
Gustav Sennton79fea482016-04-07 14:22:56 +0100223 return WebViewUpdateService.this.mImpl.getWebViewPackages();
Gustav Sennton8b179262016-03-14 11:31:14 +0000224 }
225
226 @Override // Binder call
Gustav Sennton6258dcd2015-10-30 19:25:37 +0000227 public String getCurrentWebViewPackageName() {
Gustav Senntonbf683e02016-09-15 14:42:50 +0100228 PackageInfo pi = WebViewUpdateService.this.mImpl.getCurrentWebViewPackage();
229 return pi == null ? null : pi.packageName;
230 }
231
232 @Override // Binder call
233 public PackageInfo getCurrentWebViewPackage() {
234 return WebViewUpdateService.this.mImpl.getCurrentWebViewPackage();
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100235 }
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000236
237 @Override // Binder call
238 public boolean isFallbackPackage(String packageName) {
Gustav Sennton79fea482016-04-07 14:22:56 +0100239 return WebViewUpdateService.this.mImpl.isFallbackPackage(packageName);
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000240 }
241
242 @Override // Binder call
243 public void enableFallbackLogic(boolean enable) {
244 if (getContext().checkCallingPermission(
245 android.Manifest.permission.WRITE_SECURE_SETTINGS)
246 != PackageManager.PERMISSION_GRANTED) {
247 String msg = "Permission Denial: enableFallbackLogic() from pid="
248 + Binder.getCallingPid()
249 + ", uid=" + Binder.getCallingUid()
250 + " requires " + android.Manifest.permission.WRITE_SECURE_SETTINGS;
251 Slog.w(TAG, msg);
252 throw new SecurityException(msg);
253 }
254
Gustav Sennton6824c7c2016-04-04 14:07:23 +0100255 long callingId = Binder.clearCallingIdentity();
256 try {
257 WebViewUpdateService.this.mImpl.enableFallbackLogic(enable);
258 } finally {
259 Binder.restoreCallingIdentity(callingId);
260 }
Gustav Senntonc83e3fa2016-02-18 12:19:13 +0000261 }
Torne (Richard Coles)4dbeb352014-07-29 19:14:24 +0100262 }
Torne (Richard Coles)08cfaf62014-05-08 16:07:05 +0100263}