Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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; |
| 18 | import static android.service.watchdog.ExplicitHealthCheckService.EXTRA_HEALTH_CHECK_PASSED_PACKAGE; |
| 19 | import static android.service.watchdog.ExplicitHealthCheckService.EXTRA_REQUESTED_PACKAGES; |
| 20 | import static android.service.watchdog.ExplicitHealthCheckService.EXTRA_SUPPORTED_PACKAGES; |
Zimuzo | 1a9aac7 | 2019-04-24 14:07:24 +0100 | [diff] [blame] | 21 | import static android.service.watchdog.ExplicitHealthCheckService.PackageConfig; |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 22 | |
| 23 | import android.Manifest; |
| 24 | import android.annotation.MainThread; |
| 25 | import android.annotation.Nullable; |
| 26 | import android.content.ComponentName; |
| 27 | import android.content.Context; |
| 28 | import android.content.Intent; |
| 29 | import android.content.ServiceConnection; |
| 30 | import android.content.pm.PackageManager; |
| 31 | import android.content.pm.ResolveInfo; |
| 32 | import android.content.pm.ServiceInfo; |
| 33 | import android.os.IBinder; |
| 34 | import android.os.RemoteCallback; |
| 35 | import android.os.RemoteException; |
| 36 | import android.os.UserHandle; |
| 37 | import android.service.watchdog.ExplicitHealthCheckService; |
| 38 | import android.service.watchdog.IExplicitHealthCheckService; |
| 39 | import android.text.TextUtils; |
Zimuzo | 6997f3c | 2019-04-05 17:06:25 +0100 | [diff] [blame] | 40 | import android.util.ArraySet; |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 41 | import android.util.Slog; |
| 42 | |
| 43 | import com.android.internal.annotations.GuardedBy; |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 44 | import com.android.internal.util.Preconditions; |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 45 | |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 46 | import java.util.Collection; |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 47 | import java.util.Collections; |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 48 | import java.util.Iterator; |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 49 | import java.util.List; |
Daulet Zhanguzin | ea1a7ca | 2020-01-03 09:46:50 +0000 | [diff] [blame] | 50 | import java.util.Objects; |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 51 | import java.util.Set; |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 52 | import java.util.function.Consumer; |
| 53 | |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 54 | // TODO(b/120598832): Add tests |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 55 | /** |
| 56 | * Controls the connections with {@link ExplicitHealthCheckService}. |
| 57 | */ |
| 58 | class ExplicitHealthCheckController { |
| 59 | private static final String TAG = "ExplicitHealthCheckController"; |
| 60 | private final Object mLock = new Object(); |
| 61 | private final Context mContext; |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 62 | |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 63 | // Called everytime a package passes the health check, so the watchdog is notified of the |
| 64 | // passing check. In practice, should never be null after it has been #setEnabled. |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 65 | // To prevent deadlocks between the controller and watchdog threads, we have |
| 66 | // a lock invariant to ALWAYS acquire the PackageWatchdog#mLock before #mLock in this class. |
| 67 | // It's easier to just NOT hold #mLock when calling into watchdog code on this consumer. |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 68 | @GuardedBy("mLock") @Nullable private Consumer<String> mPassedConsumer; |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 69 | // Called everytime after a successful #syncRequest call, so the watchdog can receive packages |
| 70 | // supporting health checks and update its internal state. In practice, should never be null |
| 71 | // after it has been #setEnabled. |
| 72 | // To prevent deadlocks between the controller and watchdog threads, we have |
| 73 | // a lock invariant to ALWAYS acquire the PackageWatchdog#mLock before #mLock in this class. |
| 74 | // It's easier to just NOT hold #mLock when calling into watchdog code on this consumer. |
Zimuzo | 1a9aac7 | 2019-04-24 14:07:24 +0100 | [diff] [blame] | 75 | @GuardedBy("mLock") @Nullable private Consumer<List<PackageConfig>> mSupportedConsumer; |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 76 | // Called everytime we need to notify the watchdog to sync requests between itself and the |
| 77 | // health check service. In practice, should never be null after it has been #setEnabled. |
| 78 | // To prevent deadlocks between the controller and watchdog threads, we have |
| 79 | // a lock invariant to ALWAYS acquire the PackageWatchdog#mLock before #mLock in this class. |
| 80 | // It's easier to just NOT hold #mLock when calling into watchdog code on this runnable. |
| 81 | @GuardedBy("mLock") @Nullable private Runnable mNotifySyncRunnable; |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 82 | // Actual binder object to the explicit health check service. |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 83 | @GuardedBy("mLock") @Nullable private IExplicitHealthCheckService mRemoteService; |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 84 | // Connection to the explicit health check service, necessary to unbind. |
| 85 | // We should only try to bind if mConnection is null, non-null indicates we |
| 86 | // are connected or at least connecting. |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 87 | @GuardedBy("mLock") @Nullable private ServiceConnection mConnection; |
| 88 | // Bind state of the explicit health check service. |
| 89 | @GuardedBy("mLock") private boolean mEnabled; |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 90 | |
| 91 | ExplicitHealthCheckController(Context context) { |
| 92 | mContext = context; |
| 93 | } |
| 94 | |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 95 | /** Enables or disables explicit health checks. */ |
| 96 | public void setEnabled(boolean enabled) { |
| 97 | synchronized (mLock) { |
| 98 | Slog.i(TAG, "Explicit health checks " + (enabled ? "enabled." : "disabled.")); |
| 99 | mEnabled = enabled; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Sets callbacks to listen to important events from the controller. |
| 105 | * |
| 106 | * <p> Should be called once at initialization before any other calls to the controller to |
| 107 | * ensure a happens-before relationship of the set parameters and visibility on other threads. |
| 108 | */ |
| 109 | public void setCallbacks(Consumer<String> passedConsumer, |
Zimuzo | 1a9aac7 | 2019-04-24 14:07:24 +0100 | [diff] [blame] | 110 | Consumer<List<PackageConfig>> supportedConsumer, Runnable notifySyncRunnable) { |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 111 | synchronized (mLock) { |
| 112 | if (mPassedConsumer != null || mSupportedConsumer != null |
| 113 | || mNotifySyncRunnable != null) { |
| 114 | Slog.wtf(TAG, "Resetting health check controller callbacks"); |
| 115 | } |
| 116 | |
Daulet Zhanguzin | ea1a7ca | 2020-01-03 09:46:50 +0000 | [diff] [blame] | 117 | mPassedConsumer = Objects.requireNonNull(passedConsumer); |
| 118 | mSupportedConsumer = Objects.requireNonNull(supportedConsumer); |
| 119 | mNotifySyncRunnable = Objects.requireNonNull(notifySyncRunnable); |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Calls the health check service to request or cancel packages based on |
| 125 | * {@code newRequestedPackages}. |
| 126 | * |
| 127 | * <p> Supported packages in {@code newRequestedPackages} that have not been previously |
| 128 | * requested will be requested while supported packages not in {@code newRequestedPackages} |
| 129 | * but were previously requested will be cancelled. |
| 130 | * |
| 131 | * <p> This handles binding and unbinding to the health check service as required. |
| 132 | * |
| 133 | * <p> Note, calling this may modify {@code newRequestedPackages}. |
| 134 | * |
| 135 | * <p> Note, this method is not thread safe, all calls should be serialized. |
| 136 | */ |
| 137 | public void syncRequests(Set<String> newRequestedPackages) { |
| 138 | boolean enabled; |
| 139 | synchronized (mLock) { |
| 140 | enabled = mEnabled; |
| 141 | } |
| 142 | |
| 143 | if (!enabled) { |
| 144 | Slog.i(TAG, "Health checks disabled, no supported packages"); |
| 145 | // Call outside lock |
| 146 | mSupportedConsumer.accept(Collections.emptyList()); |
| 147 | return; |
| 148 | } |
| 149 | |
Zimuzo | 1a9aac7 | 2019-04-24 14:07:24 +0100 | [diff] [blame] | 150 | getSupportedPackages(supportedPackageConfigs -> { |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 151 | // Notify the watchdog without lock held |
Zimuzo | 1a9aac7 | 2019-04-24 14:07:24 +0100 | [diff] [blame] | 152 | mSupportedConsumer.accept(supportedPackageConfigs); |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 153 | getRequestedPackages(previousRequestedPackages -> { |
| 154 | synchronized (mLock) { |
| 155 | // Hold lock so requests and cancellations are sent atomically. |
| 156 | // It is important we don't mix requests from multiple threads. |
| 157 | |
Zimuzo | 6997f3c | 2019-04-05 17:06:25 +0100 | [diff] [blame] | 158 | Set<String> supportedPackages = new ArraySet<>(); |
Zimuzo | 1a9aac7 | 2019-04-24 14:07:24 +0100 | [diff] [blame] | 159 | for (PackageConfig config : supportedPackageConfigs) { |
| 160 | supportedPackages.add(config.getPackageName()); |
Zimuzo | 6997f3c | 2019-04-05 17:06:25 +0100 | [diff] [blame] | 161 | } |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 162 | // Note, this may modify newRequestedPackages |
| 163 | newRequestedPackages.retainAll(supportedPackages); |
| 164 | |
| 165 | // Cancel packages no longer requested |
| 166 | actOnDifference(previousRequestedPackages, |
| 167 | newRequestedPackages, p -> cancel(p)); |
| 168 | // Request packages not yet requested |
| 169 | actOnDifference(newRequestedPackages, |
| 170 | previousRequestedPackages, p -> request(p)); |
| 171 | |
| 172 | if (newRequestedPackages.isEmpty()) { |
| 173 | Slog.i(TAG, "No more health check requests, unbinding..."); |
| 174 | unbindService(); |
| 175 | return; |
| 176 | } |
| 177 | } |
| 178 | }); |
| 179 | }); |
| 180 | } |
| 181 | |
| 182 | private void actOnDifference(Collection<String> collection1, Collection<String> collection2, |
| 183 | Consumer<String> action) { |
| 184 | Iterator<String> iterator = collection1.iterator(); |
| 185 | while (iterator.hasNext()) { |
| 186 | String packageName = iterator.next(); |
| 187 | if (!collection2.contains(packageName)) { |
| 188 | action.accept(packageName); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 193 | /** |
| 194 | * Requests an explicit health check for {@code packageName}. |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 195 | * After this request, the callback registered on {@link #setCallbacks} can receive explicit |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 196 | * health check passed results. |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 197 | */ |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 198 | private void request(String packageName) { |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 199 | synchronized (mLock) { |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 200 | if (!prepareServiceLocked("request health check for " + packageName)) { |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 201 | return; |
| 202 | } |
| 203 | |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 204 | Slog.i(TAG, "Requesting health check for package " + packageName); |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 205 | try { |
| 206 | mRemoteService.request(packageName); |
| 207 | } catch (RemoteException e) { |
| 208 | Slog.w(TAG, "Failed to request health check for package " + packageName, e); |
| 209 | } |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Cancels all explicit health checks for {@code packageName}. |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 215 | * After this request, the callback registered on {@link #setCallbacks} can no longer receive |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 216 | * explicit health check passed results. |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 217 | */ |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 218 | private void cancel(String packageName) { |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 219 | synchronized (mLock) { |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 220 | if (!prepareServiceLocked("cancel health check for " + packageName)) { |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 221 | return; |
| 222 | } |
| 223 | |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 224 | Slog.i(TAG, "Cancelling health check for package " + packageName); |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 225 | try { |
| 226 | mRemoteService.cancel(packageName); |
| 227 | } catch (RemoteException e) { |
| 228 | // Do nothing, if the service is down, when it comes up, we will sync requests, |
| 229 | // if there's some other error, retrying wouldn't fix anyways. |
| 230 | Slog.w(TAG, "Failed to cancel health check for package " + packageName, e); |
| 231 | } |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Returns the packages that we can request explicit health checks for. |
| 237 | * The packages will be returned to the {@code consumer}. |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 238 | */ |
Zimuzo | 1a9aac7 | 2019-04-24 14:07:24 +0100 | [diff] [blame] | 239 | private void getSupportedPackages(Consumer<List<PackageConfig>> consumer) { |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 240 | synchronized (mLock) { |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 241 | if (!prepareServiceLocked("get health check supported packages")) { |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 242 | return; |
| 243 | } |
| 244 | |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 245 | Slog.d(TAG, "Getting health check supported packages"); |
| 246 | try { |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 247 | mRemoteService.getSupportedPackages(new RemoteCallback(result -> { |
Zimuzo | 1a9aac7 | 2019-04-24 14:07:24 +0100 | [diff] [blame] | 248 | List<PackageConfig> packages = |
Zimuzo | 6997f3c | 2019-04-05 17:06:25 +0100 | [diff] [blame] | 249 | result.getParcelableArrayList(EXTRA_SUPPORTED_PACKAGES); |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 250 | Slog.i(TAG, "Explicit health check supported packages " + packages); |
| 251 | consumer.accept(packages); |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 252 | })); |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 253 | } catch (RemoteException e) { |
| 254 | // Request failed, treat as if all observed packages are supported, if any packages |
| 255 | // expire during this period, we may incorrectly treat it as failing health checks |
| 256 | // even if we don't support health checks for the package. |
| 257 | Slog.w(TAG, "Failed to get health check supported packages", e); |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Returns the packages for which health checks are currently in progress. |
| 264 | * The packages will be returned to the {@code consumer}. |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 265 | */ |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 266 | private void getRequestedPackages(Consumer<List<String>> consumer) { |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 267 | synchronized (mLock) { |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 268 | if (!prepareServiceLocked("get health check requested packages")) { |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 269 | return; |
| 270 | } |
| 271 | |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 272 | Slog.d(TAG, "Getting health check requested packages"); |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 273 | try { |
| 274 | mRemoteService.getRequestedPackages(new RemoteCallback(result -> { |
| 275 | List<String> packages = result.getStringArrayList(EXTRA_REQUESTED_PACKAGES); |
| 276 | Slog.i(TAG, "Explicit health check requested packages " + packages); |
| 277 | consumer.accept(packages); |
| 278 | })); |
| 279 | } catch (RemoteException e) { |
| 280 | // Request failed, treat as if we haven't requested any packages, if any packages |
| 281 | // were actually requested, they will not be cancelled now. May be cancelled later |
| 282 | Slog.w(TAG, "Failed to get health check requested packages", e); |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 287 | /** |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 288 | * Binds to the explicit health check service if the controller is enabled and |
| 289 | * not already bound. |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 290 | */ |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 291 | private void bindService() { |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 292 | synchronized (mLock) { |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 293 | if (!mEnabled || mConnection != null || mRemoteService != null) { |
| 294 | if (!mEnabled) { |
| 295 | Slog.i(TAG, "Not binding to service, service disabled"); |
| 296 | } else if (mRemoteService != null) { |
| 297 | Slog.i(TAG, "Not binding to service, service already connected"); |
| 298 | } else { |
| 299 | Slog.i(TAG, "Not binding to service, service already connecting"); |
| 300 | } |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 301 | return; |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 302 | } |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 303 | ComponentName component = getServiceComponentNameLocked(); |
| 304 | if (component == null) { |
| 305 | Slog.wtf(TAG, "Explicit health check service not found"); |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | Intent intent = new Intent(); |
| 310 | intent.setComponent(component); |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 311 | mConnection = new ServiceConnection() { |
| 312 | @Override |
| 313 | public void onServiceConnected(ComponentName name, IBinder service) { |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 314 | Slog.i(TAG, "Explicit health check service is connected " + name); |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 315 | initState(service); |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | @Override |
| 319 | @MainThread |
| 320 | public void onServiceDisconnected(ComponentName name) { |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 321 | // Service crashed or process was killed, #onServiceConnected will be called. |
| 322 | // Don't need to re-bind. |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 323 | Slog.i(TAG, "Explicit health check service is disconnected " + name); |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 324 | synchronized (mLock) { |
| 325 | mRemoteService = null; |
| 326 | } |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | @Override |
| 330 | public void onBindingDied(ComponentName name) { |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 331 | // Application hosting service probably got updated |
| 332 | // Need to re-bind. |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 333 | Slog.i(TAG, "Explicit health check service binding is dead. Rebind: " + name); |
| 334 | unbindService(); |
| 335 | bindService(); |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | @Override |
| 339 | public void onNullBinding(ComponentName name) { |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 340 | // Should never happen. Service returned null from #onBind. |
| 341 | Slog.wtf(TAG, "Explicit health check service binding is null?? " + name); |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 342 | } |
| 343 | }; |
| 344 | |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 345 | mContext.bindServiceAsUser(intent, mConnection, |
| 346 | Context.BIND_AUTO_CREATE, UserHandle.of(UserHandle.USER_SYSTEM)); |
| 347 | Slog.i(TAG, "Explicit health check service is bound"); |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 351 | /** Unbinds the explicit health check service. */ |
| 352 | private void unbindService() { |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 353 | synchronized (mLock) { |
| 354 | if (mRemoteService != null) { |
| 355 | mContext.unbindService(mConnection); |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 356 | mRemoteService = null; |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 357 | mConnection = null; |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 358 | } |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 359 | Slog.i(TAG, "Explicit health check service is unbound"); |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | |
| 363 | @GuardedBy("mLock") |
| 364 | @Nullable |
| 365 | private ServiceInfo getServiceInfoLocked() { |
| 366 | final String packageName = |
| 367 | mContext.getPackageManager().getServicesSystemSharedLibraryPackageName(); |
| 368 | if (packageName == null) { |
| 369 | Slog.w(TAG, "no external services package!"); |
| 370 | return null; |
| 371 | } |
| 372 | |
| 373 | final Intent intent = new Intent(ExplicitHealthCheckService.SERVICE_INTERFACE); |
| 374 | intent.setPackage(packageName); |
| 375 | final ResolveInfo resolveInfo = mContext.getPackageManager().resolveService(intent, |
| 376 | PackageManager.GET_SERVICES | PackageManager.GET_META_DATA); |
| 377 | if (resolveInfo == null || resolveInfo.serviceInfo == null) { |
| 378 | Slog.w(TAG, "No valid components found."); |
| 379 | return null; |
| 380 | } |
| 381 | return resolveInfo.serviceInfo; |
| 382 | } |
| 383 | |
| 384 | @GuardedBy("mLock") |
| 385 | @Nullable |
| 386 | private ComponentName getServiceComponentNameLocked() { |
| 387 | final ServiceInfo serviceInfo = getServiceInfoLocked(); |
| 388 | if (serviceInfo == null) { |
| 389 | return null; |
| 390 | } |
| 391 | |
| 392 | final ComponentName name = new ComponentName(serviceInfo.packageName, serviceInfo.name); |
| 393 | if (!Manifest.permission.BIND_EXPLICIT_HEALTH_CHECK_SERVICE |
| 394 | .equals(serviceInfo.permission)) { |
| 395 | Slog.w(TAG, name.flattenToShortString() + " does not require permission " |
| 396 | + Manifest.permission.BIND_EXPLICIT_HEALTH_CHECK_SERVICE); |
| 397 | return null; |
| 398 | } |
| 399 | return name; |
| 400 | } |
| 401 | |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 402 | private void initState(IBinder service) { |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 403 | synchronized (mLock) { |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 404 | if (!mEnabled) { |
| 405 | Slog.w(TAG, "Attempting to connect disabled service?? Unbinding..."); |
| 406 | // Very unlikely, but we disabled the service after binding but before we connected |
| 407 | unbindService(); |
| 408 | return; |
| 409 | } |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 410 | mRemoteService = IExplicitHealthCheckService.Stub.asInterface(service); |
| 411 | try { |
| 412 | mRemoteService.setCallback(new RemoteCallback(result -> { |
| 413 | String packageName = result.getString(EXTRA_HEALTH_CHECK_PASSED_PACKAGE); |
| 414 | if (!TextUtils.isEmpty(packageName)) { |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 415 | if (mPassedConsumer == null) { |
| 416 | Slog.wtf(TAG, "Health check passed for package " + packageName |
| 417 | + "but no consumer registered."); |
| 418 | } else { |
| 419 | // Call without lock held |
| 420 | mPassedConsumer.accept(packageName); |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 421 | } |
| 422 | } else { |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 423 | Slog.wtf(TAG, "Empty package passed explicit health check?"); |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 424 | } |
| 425 | })); |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 426 | Slog.i(TAG, "Service initialized, syncing requests"); |
Zimuzo | caa435e | 2019-03-20 11:16:06 +0000 | [diff] [blame] | 427 | } catch (RemoteException e) { |
| 428 | Slog.wtf(TAG, "Could not setCallback on explicit health check service"); |
| 429 | } |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 430 | } |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 431 | // Calling outside lock |
| 432 | mNotifySyncRunnable.run(); |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 435 | /** |
| 436 | * Prepares the health check service to receive requests. |
| 437 | * |
| 438 | * @return {@code true} if it is ready and we can proceed with a request, |
| 439 | * {@code false} otherwise. If it is not ready, and the service is enabled, |
| 440 | * we will bind and the request should be automatically attempted later. |
| 441 | */ |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 442 | @GuardedBy("mLock") |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 443 | private boolean prepareServiceLocked(String action) { |
| 444 | if (mRemoteService != null && mEnabled) { |
| 445 | return true; |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 446 | } |
Zimuzo | cb148b2 | 2019-04-01 18:54:17 +0100 | [diff] [blame] | 447 | Slog.i(TAG, "Service not ready to " + action |
| 448 | + (mEnabled ? ". Binding..." : ". Disabled")); |
| 449 | if (mEnabled) { |
| 450 | bindService(); |
| 451 | } |
| 452 | return false; |
Zimuzo | 0de99f3 | 2019-03-18 13:23:55 +0000 | [diff] [blame] | 453 | } |
| 454 | } |