Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
Philip P. Moltmann | 29ee0a4 | 2018-12-19 11:56:05 -0800 | [diff] [blame] | 17 | package com.android.internal.infra; |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 18 | |
| 19 | import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage; |
| 20 | |
| 21 | import android.annotation.NonNull; |
Philip P. Moltmann | 22b8498 | 2018-12-17 20:45:40 -0800 | [diff] [blame] | 22 | import android.annotation.Nullable; |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 23 | import android.content.ComponentName; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.content.ServiceConnection; |
| 27 | import android.os.Handler; |
| 28 | import android.os.IBinder; |
| 29 | import android.os.IBinder.DeathRecipient; |
| 30 | import android.os.IInterface; |
Philip P. Moltmann | 29ee0a4 | 2018-12-19 11:56:05 -0800 | [diff] [blame] | 31 | import android.os.Looper; |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 32 | import android.os.RemoteException; |
| 33 | import android.os.SystemClock; |
| 34 | import android.os.UserHandle; |
| 35 | import android.util.Slog; |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 36 | import android.util.TimeUtils; |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 37 | |
| 38 | import com.android.internal.annotations.GuardedBy; |
| 39 | |
| 40 | import java.io.PrintWriter; |
| 41 | import java.lang.ref.WeakReference; |
Philip P. Moltmann | 883ff1a6 | 2019-01-07 14:34:47 -0800 | [diff] [blame] | 42 | import java.util.ArrayList; |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Base class representing a remote service. |
| 46 | * |
| 47 | * <p>It abstracts away the binding and unbinding from the remote implementation, so clients can |
| 48 | * call its methods without worrying about when and how to bind/unbind/timeout. |
| 49 | * |
| 50 | * <p>All state of this class is modified on a handler thread. |
| 51 | * |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 52 | * <p><b>NOTE: </b>this class should not be extended directly, you should extend either |
| 53 | * {@link AbstractSinglePendingRequestRemoteService} or |
| 54 | * {@link AbstractMultiplePendingRequestsRemoteService}. |
| 55 | * |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 56 | * <p>See {@code com.android.server.autofill.RemoteFillService} for a concrete |
| 57 | * (no pun intended) example of how to use it. |
| 58 | * |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 59 | * @param <S> the concrete remote service class |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 60 | * @param <I> the interface of the binder service |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 61 | * |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 62 | * @hide |
| 63 | */ |
| 64 | //TODO(b/117779333): improve javadoc above instead of using Autofill as an example |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 65 | public abstract class AbstractRemoteService<S extends AbstractRemoteService<S, I>, |
| 66 | I extends IInterface> implements DeathRecipient { |
Felipe Leme | bb98ed6 | 2018-12-21 09:29:27 -0800 | [diff] [blame] | 67 | private static final int MSG_BIND = 1; |
| 68 | private static final int MSG_UNBIND = 2; |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 69 | |
Felipe Leme | e764fa2 | 2019-02-21 16:45:35 -0800 | [diff] [blame] | 70 | public static final long PERMANENT_BOUND_TIMEOUT_MS = 0; |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 71 | |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 72 | protected static final int LAST_PRIVATE_MSG = MSG_UNBIND; |
| 73 | |
| 74 | // TODO(b/117779333): convert all booleans into an integer / flags |
| 75 | public final boolean mVerbose; |
| 76 | |
| 77 | protected final String mTag = getClass().getSimpleName(); |
| 78 | protected final Handler mHandler; |
| 79 | protected final ComponentName mComponentName; |
| 80 | |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 81 | private final Context mContext; |
| 82 | private final Intent mIntent; |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 83 | private final VultureCallback<S> mVultureCallback; |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 84 | private final int mUserId; |
| 85 | private final ServiceConnection mServiceConnection = new RemoteServiceConnection(); |
| 86 | private final boolean mBindInstantServiceAllowed; |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 87 | protected I mService; |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 88 | |
| 89 | private boolean mBinding; |
| 90 | private boolean mDestroyed; |
| 91 | private boolean mServiceDied; |
| 92 | private boolean mCompleted; |
| 93 | |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 94 | // Used just for debugging purposes (on dump) |
| 95 | private long mNextUnbind; |
| 96 | |
Philip P. Moltmann | 883ff1a6 | 2019-01-07 14:34:47 -0800 | [diff] [blame] | 97 | /** Requests that have been scheduled, but that are not finished yet */ |
Felipe Leme | 81299d0 | 2019-02-21 15:48:50 -0800 | [diff] [blame] | 98 | private final ArrayList<BasePendingRequest<S, I>> mUnfinishedRequests = new ArrayList<>(); |
Philip P. Moltmann | 883ff1a6 | 2019-01-07 14:34:47 -0800 | [diff] [blame] | 99 | |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 100 | /** |
| 101 | * Callback called when the service dies. |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 102 | * |
| 103 | * @param <T> service class |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 104 | */ |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 105 | public interface VultureCallback<T> { |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 106 | /** |
| 107 | * Called when the service dies. |
| 108 | * |
| 109 | * @param service service that died! |
| 110 | */ |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 111 | void onServiceDied(T service); |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Felipe Leme | bb98ed6 | 2018-12-21 09:29:27 -0800 | [diff] [blame] | 114 | // NOTE: must be package-protected so this class is not extended outside |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 115 | AbstractRemoteService(@NonNull Context context, @NonNull String serviceInterface, |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 116 | @NonNull ComponentName componentName, int userId, @NonNull VultureCallback<S> callback, |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 117 | boolean bindInstantServiceAllowed, boolean verbose) { |
| 118 | mContext = context; |
| 119 | mVultureCallback = callback; |
| 120 | mVerbose = verbose; |
| 121 | mComponentName = componentName; |
| 122 | mIntent = new Intent(serviceInterface).setComponent(mComponentName); |
| 123 | mUserId = userId; |
Philip P. Moltmann | 29ee0a4 | 2018-12-19 11:56:05 -0800 | [diff] [blame] | 124 | mHandler = new Handler(Looper.getMainLooper()); |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 125 | mBindInstantServiceAllowed = bindInstantServiceAllowed; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Destroys this service. |
| 130 | */ |
| 131 | public final void destroy() { |
| 132 | mHandler.sendMessage(obtainMessage(AbstractRemoteService::handleDestroy, this)); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Checks whether this service is destroyed. |
| 137 | */ |
| 138 | public final boolean isDestroyed() { |
| 139 | return mDestroyed; |
| 140 | } |
| 141 | |
Felipe Leme | 930f324 | 2019-01-15 16:47:09 -0800 | [diff] [blame] | 142 | /** |
| 143 | * Gets the name of the service. |
| 144 | */ |
| 145 | @NonNull |
| 146 | public final ComponentName getComponentName() { |
| 147 | return mComponentName; |
| 148 | } |
| 149 | |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 150 | private void handleOnConnectedStateChangedInternal(boolean connected) { |
Adam He | f0d7409 | 2019-02-19 14:48:19 -0800 | [diff] [blame] | 151 | handleOnConnectedStateChanged(connected); |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 152 | if (connected) { |
| 153 | handlePendingRequests(); |
| 154 | } |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 157 | /** |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 158 | * Handles the pending requests when the connection it bounds to the remote service. |
| 159 | */ |
| 160 | abstract void handlePendingRequests(); |
| 161 | |
| 162 | /** |
| 163 | * Callback called when the system connected / disconnected to the service and the pending |
| 164 | * requests have been handled. |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 165 | * |
| 166 | * @param state {@code true} when connected, {@code false} when disconnected. |
| 167 | */ |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 168 | protected void handleOnConnectedStateChanged(boolean state) { |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Gets the base Binder interface from the service. |
| 173 | */ |
| 174 | @NonNull |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 175 | protected abstract I getServiceInterface(@NonNull IBinder service); |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 176 | |
| 177 | /** |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 178 | * Defines how long after the last interaction with the service we would unbind. |
| 179 | * |
| 180 | * @return time to unbind (in millis), or {@link #PERMANENT_BOUND_TIMEOUT_MS} to not unbind. |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 181 | */ |
| 182 | protected abstract long getTimeoutIdleBindMillis(); |
| 183 | |
| 184 | /** |
| 185 | * Defines how long after we make a remote request to a fill service we timeout. |
Felipe Leme | 81299d0 | 2019-02-21 15:48:50 -0800 | [diff] [blame] | 186 | * |
| 187 | * <p>Just need to be overridden by subclasses that uses sync {@link PendingRequest}s. |
| 188 | * |
| 189 | * @throws UnsupportedOperationException if called when not overridden. |
| 190 | * |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 191 | */ |
Felipe Leme | 81299d0 | 2019-02-21 15:48:50 -0800 | [diff] [blame] | 192 | protected long getRemoteRequestMillis() { |
| 193 | throw new UnsupportedOperationException("not implemented by " + getClass()); |
| 194 | } |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 195 | |
Philip P. Moltmann | 22b8498 | 2018-12-17 20:45:40 -0800 | [diff] [blame] | 196 | /** |
| 197 | * Gets the currently registered service interface or {@code null} if the service is not |
| 198 | * connected. |
| 199 | */ |
| 200 | @Nullable |
| 201 | public final I getServiceInterface() { |
| 202 | return mService; |
| 203 | } |
| 204 | |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 205 | private void handleDestroy() { |
| 206 | if (checkIfDestroyed()) return; |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 207 | handleOnDestroy(); |
| 208 | handleEnsureUnbound(); |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 209 | mDestroyed = true; |
| 210 | } |
| 211 | |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 212 | /** |
| 213 | * Clears the state when this object is destroyed. |
| 214 | * |
| 215 | * <p>Typically used to cancel the pending requests. |
| 216 | */ |
| 217 | protected abstract void handleOnDestroy(); |
| 218 | |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 219 | @Override // from DeathRecipient |
| 220 | public void binderDied() { |
| 221 | mHandler.sendMessage(obtainMessage(AbstractRemoteService::handleBinderDied, this)); |
| 222 | } |
| 223 | |
| 224 | private void handleBinderDied() { |
| 225 | if (checkIfDestroyed()) return; |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 226 | if (mService != null) { |
| 227 | mService.asBinder().unlinkToDeath(this, 0); |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 228 | } |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 229 | mService = null; |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 230 | mServiceDied = true; |
Philip P. Moltmann | 26f8b80 | 2019-01-12 17:02:56 -0800 | [diff] [blame] | 231 | cancelScheduledUnbind(); |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 232 | @SuppressWarnings("unchecked") // TODO(b/117779333): fix this warning |
| 233 | final S castService = (S) this; |
| 234 | mVultureCallback.onServiceDied(castService); |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | // Note: we are dumping without a lock held so this is a bit racy but |
| 238 | // adding a lock to a class that offloads to a handler thread would |
| 239 | // mean adding a lock adding overhead to normal runtime operation. |
| 240 | /** |
| 241 | * Dump it! |
| 242 | */ |
| 243 | public void dump(@NonNull String prefix, @NonNull PrintWriter pw) { |
| 244 | String tab = " "; |
| 245 | pw.append(prefix).append("service:").println(); |
| 246 | pw.append(prefix).append(tab).append("userId=") |
| 247 | .append(String.valueOf(mUserId)).println(); |
| 248 | pw.append(prefix).append(tab).append("componentName=") |
| 249 | .append(mComponentName.flattenToString()).println(); |
| 250 | pw.append(prefix).append(tab).append("destroyed=") |
| 251 | .append(String.valueOf(mDestroyed)).println(); |
Philip P. Moltmann | 883ff1a6 | 2019-01-07 14:34:47 -0800 | [diff] [blame] | 252 | pw.append(prefix).append(tab).append("numUnfinishedRequests=") |
Felipe Leme | 81299d0 | 2019-02-21 15:48:50 -0800 | [diff] [blame] | 253 | .append(String.valueOf(mUnfinishedRequests.size())).println(); |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 254 | final boolean bound = handleIsBound(); |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 255 | pw.append(prefix).append(tab).append("bound=") |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 256 | .append(String.valueOf(bound)); |
| 257 | final long idleTimeout = getTimeoutIdleBindMillis(); |
| 258 | if (bound) { |
| 259 | if (idleTimeout > 0) { |
| 260 | pw.append(" (unbind in : "); |
| 261 | TimeUtils.formatDuration(mNextUnbind - SystemClock.elapsedRealtime(), pw); |
| 262 | pw.append(")"); |
| 263 | } else { |
| 264 | pw.append(" (permanently bound)"); |
| 265 | } |
| 266 | } |
| 267 | pw.println(); |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 268 | pw.append(prefix).append("mBindInstantServiceAllowed=").println(mBindInstantServiceAllowed); |
| 269 | pw.append(prefix).append("idleTimeout=") |
Felipe Leme | 81299d0 | 2019-02-21 15:48:50 -0800 | [diff] [blame] | 270 | .append(Long.toString(idleTimeout / 1000)).append("s\n"); |
| 271 | pw.append(prefix).append("requestTimeout="); |
| 272 | try { |
| 273 | pw.append(Long.toString(getRemoteRequestMillis() / 1000)).append("s\n"); |
| 274 | } catch (UnsupportedOperationException e) { |
| 275 | pw.append("not supported\n"); |
| 276 | } |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 277 | pw.println(); |
| 278 | } |
| 279 | |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 280 | /** |
| 281 | * Schedules a "sync" request. |
| 282 | * |
| 283 | * <p>This request must be responded by the service somehow (typically using a callback), |
| 284 | * othewise it will trigger a {@link PendingRequest#onTimeout(AbstractRemoteService)} if the |
| 285 | * service doesn't respond. |
| 286 | */ |
Felipe Leme | 81299d0 | 2019-02-21 15:48:50 -0800 | [diff] [blame] | 287 | protected void scheduleRequest(@NonNull BasePendingRequest<S, I> pendingRequest) { |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 288 | mHandler.sendMessage(obtainMessage( |
| 289 | AbstractRemoteService::handlePendingRequest, this, pendingRequest)); |
| 290 | } |
| 291 | |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 292 | /** |
Philip P. Moltmann | 883ff1a6 | 2019-01-07 14:34:47 -0800 | [diff] [blame] | 293 | * Marks a pendingRequest as finished. |
| 294 | * |
| 295 | * @param finshedRequest The request that is finished |
| 296 | */ |
Felipe Leme | 81299d0 | 2019-02-21 15:48:50 -0800 | [diff] [blame] | 297 | void finishRequest(@NonNull BasePendingRequest<S, I> finshedRequest) { |
Philip P. Moltmann | 883ff1a6 | 2019-01-07 14:34:47 -0800 | [diff] [blame] | 298 | mHandler.sendMessage( |
| 299 | obtainMessage(AbstractRemoteService::handleFinishRequest, this, finshedRequest)); |
| 300 | } |
| 301 | |
Felipe Leme | 81299d0 | 2019-02-21 15:48:50 -0800 | [diff] [blame] | 302 | private void handleFinishRequest(@NonNull BasePendingRequest<S, I> finshedRequest) { |
Philip P. Moltmann | 883ff1a6 | 2019-01-07 14:34:47 -0800 | [diff] [blame] | 303 | mUnfinishedRequests.remove(finshedRequest); |
| 304 | |
| 305 | if (mUnfinishedRequests.isEmpty()) { |
| 306 | scheduleUnbind(); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /** |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 311 | * Schedules an async request. |
| 312 | * |
| 313 | * <p>This request is not expecting a callback from the service, hence it's represented by |
| 314 | * a simple {@link Runnable}. |
| 315 | */ |
| 316 | protected void scheduleAsyncRequest(@NonNull AsyncRequest<I> request) { |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 317 | // TODO(b/117779333): fix generics below |
| 318 | @SuppressWarnings({"unchecked", "rawtypes"}) |
| 319 | final MyAsyncPendingRequest<S, I> asyncRequest = new MyAsyncPendingRequest(this, request); |
| 320 | mHandler.sendMessage( |
| 321 | obtainMessage(AbstractRemoteService::handlePendingRequest, this, asyncRequest)); |
| 322 | } |
| 323 | |
| 324 | private void cancelScheduledUnbind() { |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 325 | mHandler.removeMessages(MSG_UNBIND); |
| 326 | } |
| 327 | |
Felipe Leme | bb98ed6 | 2018-12-21 09:29:27 -0800 | [diff] [blame] | 328 | /** |
| 329 | * Schedules a request to bind to the remote service. |
| 330 | * |
| 331 | * <p>Typically used on constructor for implementations that need a permanent connection to |
| 332 | * the remote service. |
| 333 | */ |
| 334 | protected void scheduleBind() { |
| 335 | if (mHandler.hasMessages(MSG_BIND)) { |
| 336 | if (mVerbose) Slog.v(mTag, "scheduleBind(): already scheduled"); |
| 337 | return; |
| 338 | } |
| 339 | mHandler.sendMessage(obtainMessage(AbstractRemoteService::handleEnsureBound, this) |
| 340 | .setWhat(MSG_BIND)); |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * Schedules a request to automatically unbind from the service after the |
| 345 | * {@link #getTimeoutIdleBindMillis() idle timeout} expires. |
| 346 | */ |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 347 | protected void scheduleUnbind() { |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 348 | final long unbindDelay = getTimeoutIdleBindMillis(); |
| 349 | |
| 350 | if (unbindDelay <= 0) { |
| 351 | if (mVerbose) Slog.v(mTag, "not scheduling unbind when value is " + unbindDelay); |
| 352 | return; |
| 353 | } |
| 354 | |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 355 | cancelScheduledUnbind(); |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 356 | // TODO(b/117779333): make sure it's unbound if the service settings changing (right now |
| 357 | // it's not) |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 358 | |
| 359 | mNextUnbind = SystemClock.elapsedRealtime() + unbindDelay; |
| 360 | if (mVerbose) Slog.v(mTag, "unbinding in " + unbindDelay + "ms: " + mNextUnbind); |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 361 | mHandler.sendMessageDelayed(obtainMessage(AbstractRemoteService::handleUnbind, this) |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 362 | .setWhat(MSG_UNBIND), unbindDelay); |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | private void handleUnbind() { |
| 366 | if (checkIfDestroyed()) return; |
| 367 | |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 368 | handleEnsureUnbound(); |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 371 | /** |
| 372 | * Handles a request, either processing it right now when bound, or saving it to be handled when |
| 373 | * bound. |
| 374 | */ |
Felipe Leme | 81299d0 | 2019-02-21 15:48:50 -0800 | [diff] [blame] | 375 | protected final void handlePendingRequest(@NonNull BasePendingRequest<S, I> pendingRequest) { |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 376 | if (checkIfDestroyed() || mCompleted) return; |
| 377 | |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 378 | if (!handleIsBound()) { |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 379 | if (mVerbose) Slog.v(mTag, "handlePendingRequest(): queuing " + pendingRequest); |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 380 | handlePendingRequestWhileUnBound(pendingRequest); |
| 381 | handleEnsureBound(); |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 382 | } else { |
| 383 | if (mVerbose) Slog.v(mTag, "handlePendingRequest(): " + pendingRequest); |
Philip P. Moltmann | 883ff1a6 | 2019-01-07 14:34:47 -0800 | [diff] [blame] | 384 | |
| 385 | mUnfinishedRequests.add(pendingRequest); |
| 386 | cancelScheduledUnbind(); |
| 387 | |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 388 | pendingRequest.run(); |
| 389 | if (pendingRequest.isFinal()) { |
| 390 | mCompleted = true; |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 395 | /** |
| 396 | * Defines what to do with a request that arrives while not bound to the service. |
| 397 | */ |
Felipe Leme | 81299d0 | 2019-02-21 15:48:50 -0800 | [diff] [blame] | 398 | abstract void handlePendingRequestWhileUnBound( |
| 399 | @NonNull BasePendingRequest<S, I> pendingRequest); |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 400 | |
| 401 | private boolean handleIsBound() { |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 402 | return mService != null; |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 405 | private void handleEnsureBound() { |
| 406 | if (handleIsBound() || mBinding) return; |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 407 | |
| 408 | if (mVerbose) Slog.v(mTag, "ensureBound()"); |
| 409 | mBinding = true; |
| 410 | |
| 411 | int flags = Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE; |
| 412 | if (mBindInstantServiceAllowed) { |
| 413 | flags |= Context.BIND_ALLOW_INSTANT; |
| 414 | } |
| 415 | |
| 416 | final boolean willBind = mContext.bindServiceAsUser(mIntent, mServiceConnection, flags, |
Ethon_Hu | c21af2f | 2018-11-27 21:39:22 +0800 | [diff] [blame] | 417 | mHandler, new UserHandle(mUserId)); |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 418 | |
| 419 | if (!willBind) { |
| 420 | Slog.w(mTag, "could not bind to " + mIntent + " using flags " + flags); |
| 421 | mBinding = false; |
| 422 | |
| 423 | if (!mServiceDied) { |
| 424 | handleBinderDied(); |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 429 | private void handleEnsureUnbound() { |
| 430 | if (!handleIsBound() && !mBinding) return; |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 431 | |
| 432 | if (mVerbose) Slog.v(mTag, "ensureUnbound()"); |
| 433 | mBinding = false; |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 434 | if (handleIsBound()) { |
| 435 | handleOnConnectedStateChangedInternal(false); |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 436 | if (mService != null) { |
| 437 | mService.asBinder().unlinkToDeath(this, 0); |
| 438 | mService = null; |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 439 | } |
| 440 | } |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 441 | mNextUnbind = 0; |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 442 | mContext.unbindService(mServiceConnection); |
| 443 | } |
| 444 | |
| 445 | private class RemoteServiceConnection implements ServiceConnection { |
| 446 | @Override |
| 447 | public void onServiceConnected(ComponentName name, IBinder service) { |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 448 | if (mVerbose) Slog.v(mTag, "onServiceConnected()"); |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 449 | if (mDestroyed || !mBinding) { |
| 450 | // This is abnormal. Unbinding the connection has been requested already. |
| 451 | Slog.wtf(mTag, "onServiceConnected() was dispatched after unbindService."); |
| 452 | return; |
| 453 | } |
| 454 | mBinding = false; |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 455 | mService = getServiceInterface(service); |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 456 | try { |
| 457 | service.linkToDeath(AbstractRemoteService.this, 0); |
| 458 | } catch (RemoteException re) { |
| 459 | handleBinderDied(); |
| 460 | return; |
| 461 | } |
Felipe Leme | 39233ff | 2018-11-28 16:54:23 -0800 | [diff] [blame] | 462 | handleOnConnectedStateChangedInternal(true); |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 463 | mServiceDied = false; |
| 464 | } |
| 465 | |
| 466 | @Override |
| 467 | public void onServiceDisconnected(ComponentName name) { |
| 468 | mBinding = true; |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 469 | mService = null; |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
| 473 | private boolean checkIfDestroyed() { |
| 474 | if (mDestroyed) { |
| 475 | if (mVerbose) { |
| 476 | Slog.v(mTag, "Not handling operation as service for " + mComponentName |
| 477 | + " is already destroyed"); |
| 478 | } |
| 479 | } |
| 480 | return mDestroyed; |
| 481 | } |
| 482 | |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 483 | /** |
| 484 | * Base class for the requests serviced by the remote service. |
| 485 | * |
Felipe Leme | 81299d0 | 2019-02-21 15:48:50 -0800 | [diff] [blame] | 486 | * <p><b>NOTE: </b> this class is not used directly, you should either override |
| 487 | * {@link com.android.internal.infra.AbstractRemoteService.PendingRequest} for sync requests, or |
| 488 | * use {@link AbstractRemoteService#scheduleAsyncRequest(AsyncRequest)} for async requests. |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 489 | * |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 490 | * @param <S> the remote service class |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 491 | * @param <I> the interface of the binder service |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 492 | */ |
Felipe Leme | 81299d0 | 2019-02-21 15:48:50 -0800 | [diff] [blame] | 493 | public abstract static class BasePendingRequest<S extends AbstractRemoteService<S, I>, |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 494 | I extends IInterface> implements Runnable { |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 495 | protected final String mTag = getClass().getSimpleName(); |
| 496 | protected final Object mLock = new Object(); |
| 497 | |
Felipe Leme | 81299d0 | 2019-02-21 15:48:50 -0800 | [diff] [blame] | 498 | final WeakReference<S> mWeakService; |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 499 | |
| 500 | @GuardedBy("mLock") |
Felipe Leme | 81299d0 | 2019-02-21 15:48:50 -0800 | [diff] [blame] | 501 | boolean mCancelled; |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 502 | |
| 503 | @GuardedBy("mLock") |
Felipe Leme | 81299d0 | 2019-02-21 15:48:50 -0800 | [diff] [blame] | 504 | boolean mCompleted; |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 505 | |
Felipe Leme | 81299d0 | 2019-02-21 15:48:50 -0800 | [diff] [blame] | 506 | BasePendingRequest(@NonNull S service) { |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 507 | mWeakService = new WeakReference<>(service); |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | /** |
| 511 | * Gets a reference to the remote service. |
| 512 | */ |
| 513 | protected final S getService() { |
| 514 | return mWeakService.get(); |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * Subclasses must call this method when the remote service finishes, i.e., when the service |
| 519 | * finishes processing a request. |
| 520 | * |
| 521 | * @return {@code false} in the service is already finished, {@code true} otherwise. |
| 522 | */ |
| 523 | protected final boolean finish() { |
| 524 | synchronized (mLock) { |
| 525 | if (mCompleted || mCancelled) { |
| 526 | return false; |
| 527 | } |
| 528 | mCompleted = true; |
| 529 | } |
Philip P. Moltmann | 883ff1a6 | 2019-01-07 14:34:47 -0800 | [diff] [blame] | 530 | |
| 531 | S service = mWeakService.get(); |
| 532 | if (service != null) { |
| 533 | service.finishRequest(this); |
| 534 | } |
| 535 | |
Felipe Leme | 81299d0 | 2019-02-21 15:48:50 -0800 | [diff] [blame] | 536 | onFinished(); |
| 537 | |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 538 | return true; |
| 539 | } |
| 540 | |
Felipe Leme | 81299d0 | 2019-02-21 15:48:50 -0800 | [diff] [blame] | 541 | void onFinished() { } |
| 542 | |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 543 | /** |
| 544 | * Checks whether this request was cancelled. |
| 545 | */ |
| 546 | @GuardedBy("mLock") |
| 547 | protected final boolean isCancelledLocked() { |
| 548 | return mCancelled; |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * Cancels the service. |
| 553 | * |
| 554 | * @return {@code false} if service is already canceled, {@code true} otherwise. |
| 555 | */ |
| 556 | public boolean cancel() { |
| 557 | synchronized (mLock) { |
| 558 | if (mCancelled || mCompleted) { |
| 559 | return false; |
| 560 | } |
| 561 | mCancelled = true; |
| 562 | } |
| 563 | |
Felipe Leme | 81299d0 | 2019-02-21 15:48:50 -0800 | [diff] [blame] | 564 | onCancel(); |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 565 | return true; |
| 566 | } |
| 567 | |
Felipe Leme | 81299d0 | 2019-02-21 15:48:50 -0800 | [diff] [blame] | 568 | void onCancel() {} |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 569 | |
| 570 | /** |
| 571 | * Checks whether this request leads to a final state where no other requests can be made. |
| 572 | */ |
| 573 | protected boolean isFinal() { |
| 574 | return false; |
| 575 | } |
| 576 | } |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 577 | |
| 578 | /** |
Felipe Leme | 81299d0 | 2019-02-21 15:48:50 -0800 | [diff] [blame] | 579 | * Base class for the requests serviced by the remote service. |
| 580 | * |
| 581 | * <p><b>NOTE: </b> this class is typically used when the service needs to use a callback to |
| 582 | * communicate back with the system server. For cases where that's not needed, you should use |
| 583 | * {@link AbstractRemoteService#scheduleAsyncRequest(AsyncRequest)} instead. |
| 584 | * |
| 585 | * <p><b>NOTE: </b> you must override {@link AbstractRemoteService#getRemoteRequestMillis()}, |
| 586 | * otherwise the constructor will throw an {@link UnsupportedOperationException}. |
| 587 | * |
| 588 | * @param <S> the remote service class |
| 589 | * @param <I> the interface of the binder service |
| 590 | */ |
| 591 | public abstract static class PendingRequest<S extends AbstractRemoteService<S, I>, |
| 592 | I extends IInterface> extends BasePendingRequest<S, I> { |
| 593 | |
| 594 | private final Runnable mTimeoutTrigger; |
| 595 | private final Handler mServiceHandler; |
| 596 | |
| 597 | protected PendingRequest(S service) { |
| 598 | super(service); |
| 599 | mServiceHandler = service.mHandler; |
| 600 | |
| 601 | mTimeoutTrigger = () -> { |
| 602 | synchronized (mLock) { |
| 603 | if (mCancelled) { |
| 604 | return; |
| 605 | } |
| 606 | mCompleted = true; |
| 607 | } |
| 608 | |
| 609 | final S remoteService = mWeakService.get(); |
| 610 | if (remoteService != null) { |
| 611 | // TODO(b/117779333): we should probably ignore it if service is destroyed. |
| 612 | Slog.w(mTag, "timed out after " + service.getRemoteRequestMillis() + " ms"); |
| 613 | onTimeout(remoteService); |
| 614 | } else { |
| 615 | Slog.w(mTag, "timed out (no service)"); |
| 616 | } |
| 617 | }; |
| 618 | mServiceHandler.postAtTime(mTimeoutTrigger, |
| 619 | SystemClock.uptimeMillis() + service.getRemoteRequestMillis()); |
| 620 | } |
| 621 | |
| 622 | @Override |
| 623 | final void onFinished() { |
| 624 | mServiceHandler.removeCallbacks(mTimeoutTrigger); |
| 625 | } |
| 626 | |
| 627 | @Override |
| 628 | final void onCancel() { |
| 629 | mServiceHandler.removeCallbacks(mTimeoutTrigger); |
| 630 | } |
| 631 | |
| 632 | /** |
| 633 | * Called by the self-destruct timeout when the remote service didn't reply to the |
| 634 | * request on time. |
| 635 | */ |
| 636 | protected abstract void onTimeout(S remoteService); |
| 637 | } |
| 638 | |
| 639 | /** |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 640 | * Represents a request that does not expect a callback from the remote service. |
| 641 | * |
| 642 | * @param <I> the interface of the binder service |
| 643 | */ |
| 644 | public interface AsyncRequest<I extends IInterface> { |
| 645 | |
| 646 | /** |
| 647 | * Run Forrest, run! |
| 648 | */ |
| 649 | void run(@NonNull I binder) throws RemoteException; |
| 650 | } |
| 651 | |
| 652 | private static final class MyAsyncPendingRequest<S extends AbstractRemoteService<S, I>, |
Felipe Leme | 81299d0 | 2019-02-21 15:48:50 -0800 | [diff] [blame] | 653 | I extends IInterface> extends BasePendingRequest<S, I> { |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 654 | private static final String TAG = MyAsyncPendingRequest.class.getSimpleName(); |
| 655 | |
| 656 | private final AsyncRequest<I> mRequest; |
| 657 | |
| 658 | protected MyAsyncPendingRequest(@NonNull S service, @NonNull AsyncRequest<I> request) { |
| 659 | super(service); |
| 660 | |
| 661 | mRequest = request; |
| 662 | } |
| 663 | |
| 664 | @Override |
| 665 | public void run() { |
| 666 | final S remoteService = getService(); |
| 667 | if (remoteService == null) return; |
| 668 | try { |
| 669 | mRequest.run(remoteService.mService); |
| 670 | } catch (RemoteException e) { |
| 671 | Slog.w(TAG, "exception handling async request (" + this + "): " + e); |
| 672 | } finally { |
| 673 | finish(); |
| 674 | } |
| 675 | } |
Felipe Leme | afb55b6 | 2018-12-06 11:36:09 -0800 | [diff] [blame] | 676 | } |
Felipe Leme | 31a1f80 | 2018-10-16 11:24:55 -0700 | [diff] [blame] | 677 | } |