Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [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 | |
| 17 | package com.android.server.textclassifier; |
| 18 | |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 21 | import android.annotation.UserIdInt; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 22 | import android.content.ComponentName; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.content.ServiceConnection; |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 26 | import android.content.pm.PackageManager; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 27 | import android.os.Binder; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 28 | import android.os.IBinder; |
| 29 | import android.os.RemoteException; |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 30 | import android.os.UserHandle; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 31 | import android.service.textclassifier.ITextClassificationCallback; |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 32 | import android.service.textclassifier.ITextClassifierService; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 33 | import android.service.textclassifier.ITextLinksCallback; |
| 34 | import android.service.textclassifier.ITextSelectionCallback; |
| 35 | import android.service.textclassifier.TextClassifierService; |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 36 | import android.util.Slog; |
| 37 | import android.util.SparseArray; |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 38 | import android.view.textclassifier.SelectionEvent; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 39 | import android.view.textclassifier.TextClassification; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 40 | import android.view.textclassifier.TextClassificationContext; |
Tony Mak | f93e9e5 | 2018-07-16 14:46:29 +0200 | [diff] [blame] | 41 | import android.view.textclassifier.TextClassificationManager; |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 42 | import android.view.textclassifier.TextClassificationSessionId; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 43 | import android.view.textclassifier.TextLinks; |
| 44 | import android.view.textclassifier.TextSelection; |
| 45 | |
| 46 | import com.android.internal.annotations.GuardedBy; |
Tony Mak | f93e9e5 | 2018-07-16 14:46:29 +0200 | [diff] [blame] | 47 | import com.android.internal.util.DumpUtils; |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 48 | import com.android.internal.util.FunctionalUtils; |
| 49 | import com.android.internal.util.FunctionalUtils.ThrowingRunnable; |
Tony Mak | f93e9e5 | 2018-07-16 14:46:29 +0200 | [diff] [blame] | 50 | import com.android.internal.util.IndentingPrintWriter; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 51 | import com.android.internal.util.Preconditions; |
| 52 | import com.android.server.SystemService; |
| 53 | |
Tony Mak | f93e9e5 | 2018-07-16 14:46:29 +0200 | [diff] [blame] | 54 | import java.io.FileDescriptor; |
| 55 | import java.io.PrintWriter; |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 56 | import java.util.ArrayDeque; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 57 | import java.util.Queue; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * A manager for TextClassifier services. |
| 61 | * Apps bind to the TextClassificationManagerService for text classification. This service |
| 62 | * reroutes calls to it to a {@link TextClassifierService} that it manages. |
| 63 | */ |
| 64 | public final class TextClassificationManagerService extends ITextClassifierService.Stub { |
| 65 | |
| 66 | private static final String LOG_TAG = "TextClassificationManagerService"; |
| 67 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 68 | public static final class Lifecycle extends SystemService { |
| 69 | |
| 70 | private final TextClassificationManagerService mManagerService; |
| 71 | |
| 72 | public Lifecycle(Context context) { |
| 73 | super(context); |
| 74 | mManagerService = new TextClassificationManagerService(context); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public void onStart() { |
| 79 | try { |
| 80 | publishBinderService(Context.TEXT_CLASSIFICATION_SERVICE, mManagerService); |
| 81 | } catch (Throwable t) { |
| 82 | // Starting this service is not critical to the running of this device and should |
| 83 | // therefore not crash the device. If it fails, log the error and continue. |
| 84 | Slog.e(LOG_TAG, "Could not start the TextClassificationManagerService.", t); |
| 85 | } |
| 86 | } |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 87 | |
| 88 | @Override |
| 89 | public void onStartUser(int userId) { |
| 90 | processAnyPendingWork(userId); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public void onUnlockUser(int userId) { |
| 95 | // Rebind if we failed earlier due to locked encrypted user |
| 96 | processAnyPendingWork(userId); |
| 97 | } |
| 98 | |
| 99 | private void processAnyPendingWork(int userId) { |
| 100 | synchronized (mManagerService.mLock) { |
| 101 | mManagerService.getUserStateLocked(userId).bindIfHasPendingRequestsLocked(); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void onStopUser(int userId) { |
| 107 | synchronized (mManagerService.mLock) { |
| 108 | UserState userState = mManagerService.peekUserStateLocked(userId); |
| 109 | if (userState != null) { |
| 110 | userState.mConnection.cleanupService(); |
| 111 | mManagerService.mUserStates.remove(userId); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | private final Context mContext; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 119 | private final Object mLock; |
| 120 | @GuardedBy("mLock") |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 121 | final SparseArray<UserState> mUserStates = new SparseArray<>(); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 122 | |
| 123 | private TextClassificationManagerService(Context context) { |
| 124 | mContext = Preconditions.checkNotNull(context); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 125 | mLock = new Object(); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public void onSuggestSelection( |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 130 | TextClassificationSessionId sessionId, |
| 131 | TextSelection.Request request, ITextSelectionCallback callback) |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 132 | throws RemoteException { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 133 | Preconditions.checkNotNull(request); |
| 134 | Preconditions.checkNotNull(callback); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 135 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 136 | synchronized (mLock) { |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 137 | UserState userState = getCallingUserStateLocked(); |
| 138 | if (!userState.bindLocked()) { |
| 139 | callback.onFailure(); |
| 140 | } else if (userState.isBoundLocked()) { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 141 | userState.mService.onSuggestSelection(sessionId, request, callback); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 142 | } else { |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 143 | userState.mPendingRequests.add(new PendingRequest( |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 144 | () -> onSuggestSelection(sessionId, request, callback), |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 145 | callback::onFailure, callback.asBinder(), this, userState)); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void onClassifyText( |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 152 | TextClassificationSessionId sessionId, |
| 153 | TextClassification.Request request, ITextClassificationCallback callback) |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 154 | throws RemoteException { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 155 | Preconditions.checkNotNull(request); |
| 156 | Preconditions.checkNotNull(callback); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 157 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 158 | synchronized (mLock) { |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 159 | UserState userState = getCallingUserStateLocked(); |
| 160 | if (!userState.bindLocked()) { |
| 161 | callback.onFailure(); |
| 162 | } else if (userState.isBoundLocked()) { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 163 | userState.mService.onClassifyText(sessionId, request, callback); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 164 | } else { |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 165 | userState.mPendingRequests.add(new PendingRequest( |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 166 | () -> onClassifyText(sessionId, request, callback), |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 167 | callback::onFailure, callback.asBinder(), this, userState)); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public void onGenerateLinks( |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 174 | TextClassificationSessionId sessionId, |
| 175 | TextLinks.Request request, ITextLinksCallback callback) |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 176 | throws RemoteException { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 177 | Preconditions.checkNotNull(request); |
| 178 | Preconditions.checkNotNull(callback); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 179 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 180 | synchronized (mLock) { |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 181 | UserState userState = getCallingUserStateLocked(); |
| 182 | if (!userState.bindLocked()) { |
| 183 | callback.onFailure(); |
| 184 | } else if (userState.isBoundLocked()) { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 185 | userState.mService.onGenerateLinks(sessionId, request, callback); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 186 | } else { |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 187 | userState.mPendingRequests.add(new PendingRequest( |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 188 | () -> onGenerateLinks(sessionId, request, callback), |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 189 | callback::onFailure, callback.asBinder(), this, userState)); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 194 | @Override |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 195 | public void onSelectionEvent( |
| 196 | TextClassificationSessionId sessionId, SelectionEvent event) throws RemoteException { |
| 197 | Preconditions.checkNotNull(event); |
| 198 | validateInput(event.getPackageName(), mContext); |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 199 | |
| 200 | synchronized (mLock) { |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 201 | UserState userState = getCallingUserStateLocked(); |
| 202 | if (userState.isBoundLocked()) { |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 203 | userState.mService.onSelectionEvent(sessionId, event); |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 204 | } else { |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 205 | userState.mPendingRequests.add(new PendingRequest( |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 206 | () -> onSelectionEvent(sessionId, event), |
| 207 | null /* onServiceFailure */, null /* binder */, this, userState)); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public void onCreateTextClassificationSession( |
| 214 | TextClassificationContext classificationContext, TextClassificationSessionId sessionId) |
| 215 | throws RemoteException { |
| 216 | Preconditions.checkNotNull(sessionId); |
| 217 | Preconditions.checkNotNull(classificationContext); |
| 218 | validateInput(classificationContext.getPackageName(), mContext); |
| 219 | |
| 220 | synchronized (mLock) { |
| 221 | UserState userState = getCallingUserStateLocked(); |
| 222 | if (userState.isBoundLocked()) { |
| 223 | userState.mService.onCreateTextClassificationSession( |
| 224 | classificationContext, sessionId); |
| 225 | } else { |
| 226 | userState.mPendingRequests.add(new PendingRequest( |
| 227 | () -> onCreateTextClassificationSession(classificationContext, sessionId), |
| 228 | null /* onServiceFailure */, null /* binder */, this, userState)); |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public void onDestroyTextClassificationSession(TextClassificationSessionId sessionId) |
| 235 | throws RemoteException { |
| 236 | Preconditions.checkNotNull(sessionId); |
| 237 | |
| 238 | synchronized (mLock) { |
| 239 | UserState userState = getCallingUserStateLocked(); |
| 240 | if (userState.isBoundLocked()) { |
| 241 | userState.mService.onDestroyTextClassificationSession(sessionId); |
| 242 | } else { |
| 243 | userState.mPendingRequests.add(new PendingRequest( |
| 244 | () -> onDestroyTextClassificationSession(sessionId), |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 245 | null /* onServiceFailure */, null /* binder */, this, userState)); |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
Andreas Gampe | d6d4206 | 2018-07-20 13:08:21 -0700 | [diff] [blame] | 250 | @GuardedBy("mLock") |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 251 | private UserState getCallingUserStateLocked() { |
| 252 | return getUserStateLocked(UserHandle.getCallingUserId()); |
| 253 | } |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 254 | |
Andreas Gampe | d6d4206 | 2018-07-20 13:08:21 -0700 | [diff] [blame] | 255 | @GuardedBy("mLock") |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 256 | private UserState getUserStateLocked(int userId) { |
| 257 | UserState result = mUserStates.get(userId); |
| 258 | if (result == null) { |
| 259 | result = new UserState(userId, mContext, mLock); |
| 260 | mUserStates.put(userId, result); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 261 | } |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 262 | return result; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Andreas Gampe | d6d4206 | 2018-07-20 13:08:21 -0700 | [diff] [blame] | 265 | @GuardedBy("mLock") |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 266 | UserState peekUserStateLocked(int userId) { |
| 267 | return mUserStates.get(userId); |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Tony Mak | f93e9e5 | 2018-07-16 14:46:29 +0200 | [diff] [blame] | 270 | @Override |
| 271 | protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) { |
| 272 | if (!DumpUtils.checkDumpPermission(mContext, LOG_TAG, fout)) return; |
| 273 | IndentingPrintWriter pw = new IndentingPrintWriter(fout, " "); |
| 274 | TextClassificationManager tcm = mContext.getSystemService(TextClassificationManager.class); |
| 275 | tcm.dump(pw); |
Felipe Leme | f819213 | 2018-10-12 13:42:40 -0700 | [diff] [blame] | 276 | |
| 277 | pw.printPair("context", mContext); pw.println(); |
| 278 | synchronized (mLock) { |
| 279 | int size = mUserStates.size(); |
| 280 | pw.print("Number user states: "); pw.println(size); |
| 281 | if (size > 0) { |
| 282 | for (int i = 0; i < size; i++) { |
| 283 | pw.increaseIndent(); |
| 284 | UserState userState = mUserStates.valueAt(i); |
| 285 | pw.print(i); pw.print(":"); userState.dump(pw); pw.println(); |
| 286 | pw.decreaseIndent(); |
| 287 | } |
| 288 | } |
| 289 | } |
Tony Mak | f93e9e5 | 2018-07-16 14:46:29 +0200 | [diff] [blame] | 290 | } |
| 291 | |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 292 | private static final class PendingRequest implements IBinder.DeathRecipient { |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 293 | |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 294 | @Nullable private final IBinder mBinder; |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 295 | @NonNull private final Runnable mRequest; |
| 296 | @Nullable private final Runnable mOnServiceFailure; |
| 297 | @GuardedBy("mLock") |
| 298 | @NonNull private final UserState mOwningUser; |
| 299 | @NonNull private final TextClassificationManagerService mService; |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 300 | |
| 301 | /** |
| 302 | * Initializes a new pending request. |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 303 | * @param request action to perform when the service is bound |
| 304 | * @param onServiceFailure action to perform when the service dies or disconnects |
| 305 | * @param binder binder to the process that made this pending request |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 306 | * @param service |
| 307 | * @param owningUser |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 308 | */ |
| 309 | PendingRequest( |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 310 | @NonNull ThrowingRunnable request, @Nullable ThrowingRunnable onServiceFailure, |
| 311 | @Nullable IBinder binder, |
| 312 | TextClassificationManagerService service, |
| 313 | UserState owningUser) { |
| 314 | mRequest = |
| 315 | logOnFailure(Preconditions.checkNotNull(request), "handling pending request"); |
| 316 | mOnServiceFailure = |
| 317 | logOnFailure(onServiceFailure, "notifying callback of service failure"); |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 318 | mBinder = binder; |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 319 | mService = service; |
| 320 | mOwningUser = owningUser; |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 321 | if (mBinder != null) { |
| 322 | try { |
| 323 | mBinder.linkToDeath(this, 0); |
| 324 | } catch (RemoteException e) { |
| 325 | e.printStackTrace(); |
| 326 | } |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 330 | @Override |
| 331 | public void binderDied() { |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 332 | synchronized (mService.mLock) { |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 333 | // No need to handle this pending request anymore. Remove. |
| 334 | removeLocked(); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | @GuardedBy("mLock") |
| 339 | private void removeLocked() { |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 340 | mOwningUser.mPendingRequests.remove(this); |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 341 | if (mBinder != null) { |
| 342 | mBinder.unlinkToDeath(this, 0); |
| 343 | } |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 347 | private static Runnable logOnFailure(@Nullable ThrowingRunnable r, String opDesc) { |
| 348 | if (r == null) return null; |
| 349 | return FunctionalUtils.handleExceptions(r, |
| 350 | e -> Slog.d(LOG_TAG, "Error " + opDesc + ": " + e.getMessage())); |
| 351 | } |
| 352 | |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 353 | private static void validateInput(String packageName, Context context) |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 354 | throws RemoteException { |
| 355 | try { |
| 356 | final int uid = context.getPackageManager() |
Abodunrinwa Toki | 080c854 | 2018-03-27 00:04:06 +0100 | [diff] [blame] | 357 | .getPackageUid(packageName, 0); |
Abodunrinwa Toki | ad52f4b | 2018-02-06 23:32:41 +0000 | [diff] [blame] | 358 | Preconditions.checkArgument(Binder.getCallingUid() == uid); |
| 359 | } catch (IllegalArgumentException | NullPointerException | |
| 360 | PackageManager.NameNotFoundException e) { |
| 361 | throw new RemoteException(e.getMessage()); |
| 362 | } |
| 363 | } |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 364 | |
| 365 | private static final class UserState { |
| 366 | @UserIdInt final int mUserId; |
| 367 | final TextClassifierServiceConnection mConnection = new TextClassifierServiceConnection(); |
| 368 | @GuardedBy("mLock") |
| 369 | final Queue<PendingRequest> mPendingRequests = new ArrayDeque<>(); |
| 370 | @GuardedBy("mLock") |
| 371 | ITextClassifierService mService; |
| 372 | @GuardedBy("mLock") |
| 373 | boolean mBinding; |
| 374 | |
| 375 | private final Context mContext; |
| 376 | private final Object mLock; |
| 377 | |
| 378 | private UserState(int userId, Context context, Object lock) { |
| 379 | mUserId = userId; |
| 380 | mContext = Preconditions.checkNotNull(context); |
| 381 | mLock = Preconditions.checkNotNull(lock); |
| 382 | } |
| 383 | |
| 384 | @GuardedBy("mLock") |
| 385 | boolean isBoundLocked() { |
| 386 | return mService != null; |
| 387 | } |
| 388 | |
| 389 | @GuardedBy("mLock") |
| 390 | private void handlePendingRequestsLocked() { |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 391 | PendingRequest request; |
| 392 | while ((request = mPendingRequests.poll()) != null) { |
| 393 | if (isBoundLocked()) { |
| 394 | request.mRequest.run(); |
| 395 | } else { |
| 396 | if (request.mOnServiceFailure != null) { |
| 397 | request.mOnServiceFailure.run(); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | if (request.mBinder != null) { |
| 402 | request.mBinder.unlinkToDeath(request, 0); |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
Andreas Gampe | d6d4206 | 2018-07-20 13:08:21 -0700 | [diff] [blame] | 407 | @GuardedBy("mLock") |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 408 | private boolean bindIfHasPendingRequestsLocked() { |
| 409 | return !mPendingRequests.isEmpty() && bindLocked(); |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * @return true if the service is bound or in the process of being bound. |
| 414 | * Returns false otherwise. |
| 415 | */ |
Andreas Gampe | d6d4206 | 2018-07-20 13:08:21 -0700 | [diff] [blame] | 416 | @GuardedBy("mLock") |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 417 | private boolean bindLocked() { |
| 418 | if (isBoundLocked() || mBinding) { |
| 419 | return true; |
| 420 | } |
| 421 | |
| 422 | // TODO: Handle bind timeout. |
| 423 | final boolean willBind; |
| 424 | final long identity = Binder.clearCallingIdentity(); |
| 425 | try { |
| 426 | ComponentName componentName = |
| 427 | TextClassifierService.getServiceComponentName(mContext); |
| 428 | if (componentName == null) { |
| 429 | // Might happen if the storage is encrypted and the user is not unlocked |
| 430 | return false; |
| 431 | } |
| 432 | Intent serviceIntent = new Intent(TextClassifierService.SERVICE_INTERFACE) |
| 433 | .setComponent(componentName); |
| 434 | Slog.d(LOG_TAG, "Binding to " + serviceIntent.getComponent()); |
| 435 | willBind = mContext.bindServiceAsUser( |
| 436 | serviceIntent, mConnection, |
| 437 | Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE, |
| 438 | UserHandle.of(mUserId)); |
| 439 | mBinding = willBind; |
| 440 | } finally { |
| 441 | Binder.restoreCallingIdentity(identity); |
| 442 | } |
| 443 | return willBind; |
| 444 | } |
| 445 | |
Felipe Leme | f819213 | 2018-10-12 13:42:40 -0700 | [diff] [blame] | 446 | private void dump(IndentingPrintWriter pw) { |
| 447 | pw.printPair("context", mContext); |
| 448 | pw.printPair("userId", mUserId); |
| 449 | synchronized (mLock) { |
| 450 | pw.printPair("binding", mBinding); |
| 451 | pw.printPair("numberRequests", mPendingRequests.size()); |
| 452 | } |
| 453 | } |
| 454 | |
Eugene Susla | 7b24b2b | 2018-03-16 14:33:31 -0700 | [diff] [blame] | 455 | private final class TextClassifierServiceConnection implements ServiceConnection { |
| 456 | @Override |
| 457 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 458 | init(ITextClassifierService.Stub.asInterface(service)); |
| 459 | } |
| 460 | |
| 461 | @Override |
| 462 | public void onServiceDisconnected(ComponentName name) { |
| 463 | cleanupService(); |
| 464 | } |
| 465 | |
| 466 | @Override |
| 467 | public void onBindingDied(ComponentName name) { |
| 468 | cleanupService(); |
| 469 | } |
| 470 | |
| 471 | @Override |
| 472 | public void onNullBinding(ComponentName name) { |
| 473 | cleanupService(); |
| 474 | } |
| 475 | |
| 476 | void cleanupService() { |
| 477 | init(null); |
| 478 | } |
| 479 | |
| 480 | private void init(@Nullable ITextClassifierService service) { |
| 481 | synchronized (mLock) { |
| 482 | mService = service; |
| 483 | mBinding = false; |
| 484 | handlePendingRequestsLocked(); |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | } |
Abodunrinwa Toki | d32906c | 2018-01-18 04:34:44 -0800 | [diff] [blame] | 489 | } |