Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -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 | |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 17 | package com.android.server.contentcapture; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 18 | |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 19 | import static android.service.contentcapture.ContentCaptureService.setClientState; |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 20 | import static android.view.contentcapture.ContentCaptureSession.STATE_DISABLED; |
| 21 | import static android.view.contentcapture.ContentCaptureSession.STATE_DUPLICATED_ID; |
Adam He | bb82c6a | 2019-01-17 14:59:02 -0800 | [diff] [blame] | 22 | import static android.view.contentcapture.ContentCaptureSession.STATE_INTERNAL_ERROR; |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame^] | 23 | import static android.view.contentcapture.ContentCaptureSession.STATE_NOT_WHITELISTED; |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 24 | import static android.view.contentcapture.ContentCaptureSession.STATE_NO_SERVICE; |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 25 | |
Winson Chung | fbbb158 | 2018-11-13 16:09:01 -0800 | [diff] [blame] | 26 | import static com.android.server.wm.ActivityTaskManagerInternal.ASSIST_KEY_CONTENT; |
| 27 | import static com.android.server.wm.ActivityTaskManagerInternal.ASSIST_KEY_DATA; |
| 28 | import static com.android.server.wm.ActivityTaskManagerInternal.ASSIST_KEY_STRUCTURE; |
| 29 | |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 30 | import android.Manifest; |
| 31 | import android.annotation.NonNull; |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame^] | 32 | import android.annotation.Nullable; |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 33 | import android.annotation.UserIdInt; |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 34 | import android.app.ActivityManagerInternal; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 35 | import android.app.AppGlobals; |
Winson Chung | fbbb158 | 2018-11-13 16:09:01 -0800 | [diff] [blame] | 36 | import android.app.assist.AssistContent; |
| 37 | import android.app.assist.AssistStructure; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 38 | import android.content.ComponentName; |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 39 | import android.content.ContentCaptureOptions; |
Adam He | bb82c6a | 2019-01-17 14:59:02 -0800 | [diff] [blame] | 40 | import android.content.pm.ActivityPresentationInfo; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 41 | import android.content.pm.PackageManager; |
| 42 | import android.content.pm.PackageManager.NameNotFoundException; |
| 43 | import android.content.pm.ServiceInfo; |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 44 | import android.os.Binder; |
Winson Chung | fbbb158 | 2018-11-13 16:09:01 -0800 | [diff] [blame] | 45 | import android.os.Bundle; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 46 | import android.os.IBinder; |
| 47 | import android.os.RemoteException; |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 48 | import android.os.UserHandle; |
Felipe Leme | 72e83d8 | 2019-02-13 14:51:17 -0800 | [diff] [blame] | 49 | import android.provider.Settings; |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 50 | import android.service.contentcapture.ContentCaptureService; |
Felipe Leme | af4e4fd | 2019-01-15 08:32:42 -0800 | [diff] [blame] | 51 | import android.service.contentcapture.IContentCaptureServiceCallback; |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 52 | import android.service.contentcapture.SnapshotData; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 53 | import android.util.ArrayMap; |
Felipe Leme | e8eae9d4 | 2019-02-13 15:41:55 -0800 | [diff] [blame] | 54 | import android.util.ArraySet; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 55 | import android.util.Slog; |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 56 | import android.view.contentcapture.UserDataRemovalRequest; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 57 | |
| 58 | import com.android.internal.annotations.GuardedBy; |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame^] | 59 | import com.android.internal.infra.WhitelistHelper; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 60 | import com.android.internal.os.IResultReceiver; |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 61 | import com.android.server.LocalServices; |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 62 | import com.android.server.contentcapture.RemoteContentCaptureService.ContentCaptureServiceCallbacks; |
Felipe Leme | 1e3de57 | 2018-12-05 15:44:17 -0800 | [diff] [blame] | 63 | import com.android.server.infra.AbstractPerUserSystemService; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 64 | |
| 65 | import java.io.PrintWriter; |
Felipe Leme | 5933efb | 2018-11-29 16:08:49 -0800 | [diff] [blame] | 66 | import java.util.ArrayList; |
Felipe Leme | af4e4fd | 2019-01-15 08:32:42 -0800 | [diff] [blame] | 67 | import java.util.List; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 68 | |
| 69 | /** |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 70 | * Per-user instance of {@link ContentCaptureManagerService}. |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 71 | */ |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 72 | final class ContentCapturePerUserService |
| 73 | extends |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 74 | AbstractPerUserSystemService<ContentCapturePerUserService, ContentCaptureManagerService> |
| 75 | implements ContentCaptureServiceCallbacks { |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 76 | |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 77 | private static final String TAG = ContentCaptureManagerService.class.getSimpleName(); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 78 | |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 79 | @GuardedBy("mLock") |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 80 | private final ArrayMap<String, ContentCaptureServerSession> mSessions = |
Felipe Leme | a7bdb14 | 2018-11-05 16:29:29 -0800 | [diff] [blame] | 81 | new ArrayMap<>(); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 82 | |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 83 | /** |
| 84 | * Reference to the remote service. |
| 85 | * |
| 86 | * <p>It's set in the constructor, but it's also updated when the service's updated in the |
| 87 | * master's cache (for example, because a temporary service was set). |
| 88 | */ |
| 89 | @GuardedBy("mLock") |
| 90 | private RemoteContentCaptureService mRemoteService; |
| 91 | |
Felipe Leme | af4e4fd | 2019-01-15 08:32:42 -0800 | [diff] [blame] | 92 | private final ContentCaptureServiceRemoteCallback mRemoteServiceCallback = |
| 93 | new ContentCaptureServiceRemoteCallback(); |
| 94 | |
Felipe Leme | e8eae9d4 | 2019-02-13 15:41:55 -0800 | [diff] [blame] | 95 | /** |
| 96 | * List of packages that are whitelisted to be content captured. |
| 97 | */ |
| 98 | @GuardedBy("mLock") |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame^] | 99 | private final WhitelistHelper mWhitelistHelper = new WhitelistHelper(); |
Felipe Leme | e8eae9d4 | 2019-02-13 15:41:55 -0800 | [diff] [blame] | 100 | |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 101 | // TODO(b/111276913): add mechanism to prune stale sessions, similar to Autofill's |
| 102 | |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 103 | ContentCapturePerUserService(@NonNull ContentCaptureManagerService master, |
| 104 | @NonNull Object lock, boolean disabled, @UserIdInt int userId) { |
Felipe Leme | c28711c | 2018-12-20 11:17:02 -0800 | [diff] [blame] | 105 | super(master, lock, userId); |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 106 | updateRemoteServiceLocked(disabled); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Updates the reference to the remote service. |
| 111 | */ |
| 112 | private void updateRemoteServiceLocked(boolean disabled) { |
| 113 | if (mRemoteService != null) { |
| 114 | if (mMaster.debug) Slog.d(TAG, "updateRemoteService(): destroying old remote service"); |
| 115 | mRemoteService.destroy(); |
| 116 | mRemoteService = null; |
| 117 | } |
| 118 | |
| 119 | // Updates the component name |
| 120 | final ComponentName serviceComponentName = updateServiceInfoLocked(); |
| 121 | |
| 122 | if (serviceComponentName == null) { |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 123 | if (mMaster.debug) Slog.d(TAG, "updateRemoteService(): no service component name"); |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 124 | return; |
| 125 | } |
| 126 | |
| 127 | if (!disabled) { |
Felipe Leme | af4e4fd | 2019-01-15 08:32:42 -0800 | [diff] [blame] | 128 | mRemoteService = new RemoteContentCaptureService(mMaster.getContext(), |
| 129 | ContentCaptureService.SERVICE_INTERFACE, serviceComponentName, |
| 130 | mRemoteServiceCallback, mUserId, this, mMaster.isBindInstantServiceAllowed(), |
Felipe Leme | e764fa2 | 2019-02-21 16:45:35 -0800 | [diff] [blame] | 131 | mMaster.verbose, mMaster.mDevCfgIdleUnbindTimeoutMs); |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 132 | } |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override // from PerUserSystemService |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 136 | protected ServiceInfo newServiceInfoLocked(@NonNull ComponentName serviceComponent) |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 137 | throws NameNotFoundException { |
| 138 | |
Felipe Leme | 658c8e4 | 2018-11-30 12:49:45 -0800 | [diff] [blame] | 139 | int flags = PackageManager.GET_META_DATA; |
| 140 | final boolean isTemp = isTemporaryServiceSetLocked(); |
| 141 | if (!isTemp) { |
| 142 | flags |= PackageManager.MATCH_SYSTEM_ONLY; |
| 143 | } |
| 144 | |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 145 | ServiceInfo si; |
| 146 | try { |
Felipe Leme | 658c8e4 | 2018-11-30 12:49:45 -0800 | [diff] [blame] | 147 | si = AppGlobals.getPackageManager().getServiceInfo(serviceComponent, flags, mUserId); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 148 | } catch (RemoteException e) { |
| 149 | Slog.w(TAG, "Could not get service for " + serviceComponent + ": " + e); |
| 150 | return null; |
| 151 | } |
Felipe Leme | 658c8e4 | 2018-11-30 12:49:45 -0800 | [diff] [blame] | 152 | if (si == null) { |
| 153 | Slog.w(TAG, "Could not get serviceInfo for " + (isTemp ? " (temp)" : "(default system)") |
| 154 | + " " + serviceComponent.flattenToShortString()); |
| 155 | return null; |
| 156 | } |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 157 | if (!Manifest.permission.BIND_CONTENT_CAPTURE_SERVICE.equals(si.permission)) { |
| 158 | Slog.w(TAG, "ContentCaptureService from '" + si.packageName |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 159 | + "' does not require permission " |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 160 | + Manifest.permission.BIND_CONTENT_CAPTURE_SERVICE); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 161 | throw new SecurityException("Service does not require permission " |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 162 | + Manifest.permission.BIND_CONTENT_CAPTURE_SERVICE); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 163 | } |
| 164 | return si; |
| 165 | } |
| 166 | |
Felipe Leme | ecb08be | 2018-11-27 15:48:47 -0800 | [diff] [blame] | 167 | @Override // from PerUserSystemService |
| 168 | @GuardedBy("mLock") |
| 169 | protected boolean updateLocked(boolean disabled) { |
| 170 | destroyLocked(); |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 171 | final boolean disabledStateChanged = super.updateLocked(disabled); |
| 172 | updateRemoteServiceLocked(disabled); |
| 173 | return disabledStateChanged; |
Felipe Leme | ecb08be | 2018-11-27 15:48:47 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 176 | @Override // from ContentCaptureServiceCallbacks |
| 177 | public void onServiceDied(@NonNull RemoteContentCaptureService service) { |
| 178 | if (mMaster.debug) Slog.d(TAG, "remote service died: " + service); |
| 179 | synchronized (mLock) { |
| 180 | removeSelfFromCacheLocked(); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // TODO(b/119613670): log metrics |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 185 | @GuardedBy("mLock") |
| 186 | public void startSessionLocked(@NonNull IBinder activityToken, |
Adam He | bb82c6a | 2019-01-17 14:59:02 -0800 | [diff] [blame] | 187 | @NonNull ActivityPresentationInfo activityPresentationInfo, |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 188 | @NonNull String sessionId, int uid, int flags, |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 189 | @NonNull IResultReceiver clientReceiver) { |
Adam He | bb82c6a | 2019-01-17 14:59:02 -0800 | [diff] [blame] | 190 | if (activityPresentationInfo == null) { |
| 191 | Slog.w(TAG, "basic activity info is null"); |
| 192 | setClientState(clientReceiver, STATE_DISABLED | STATE_INTERNAL_ERROR, |
| 193 | /* binder= */ null); |
| 194 | return; |
| 195 | } |
| 196 | final int taskId = activityPresentationInfo.taskId; |
| 197 | final int displayId = activityPresentationInfo.displayId; |
| 198 | final ComponentName componentName = activityPresentationInfo.componentName; |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame^] | 199 | final boolean whiteListed = isWhitelistedLocked(componentName); |
Felipe Leme | 930f324 | 2019-01-15 16:47:09 -0800 | [diff] [blame] | 200 | final ComponentName serviceComponentName = getServiceComponentName(); |
| 201 | final boolean enabled = isEnabledLocked(); |
Felipe Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 202 | if (mMaster.mRequestsHistory != null) { |
| 203 | final String historyItem = |
| 204 | "id=" + sessionId + " uid=" + uid |
| 205 | + " a=" + ComponentName.flattenToShortString(componentName) |
| 206 | + " t=" + taskId + " d=" + displayId |
| 207 | + " s=" + ComponentName.flattenToShortString(serviceComponentName) |
Felipe Leme | e8eae9d4 | 2019-02-13 15:41:55 -0800 | [diff] [blame] | 208 | + " u=" + mUserId + " f=" + flags + (enabled ? "" : " (disabled)") |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame^] | 209 | + " w=" + whiteListed; |
Felipe Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 210 | mMaster.mRequestsHistory.log(historyItem); |
| 211 | } |
Felipe Leme | 930f324 | 2019-01-15 16:47:09 -0800 | [diff] [blame] | 212 | |
| 213 | if (!enabled) { |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 214 | // TODO: it would be better to split in differet reasons, like |
| 215 | // STATE_DISABLED_NO_SERVICE and STATE_DISABLED_BY_DEVICE_POLICY |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 216 | setClientState(clientReceiver, STATE_DISABLED | STATE_NO_SERVICE, |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 217 | /* binder= */ null); |
Felipe Leme | 8e2e341 | 2018-11-14 10:39:29 -0800 | [diff] [blame] | 218 | return; |
| 219 | } |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 220 | if (serviceComponentName == null) { |
| 221 | // TODO(b/111276913): this happens when the system service is starting, we should |
| 222 | // probably handle it in a more elegant way (like waiting for boot_complete or |
| 223 | // something like that |
Felipe Leme | 8e2e341 | 2018-11-14 10:39:29 -0800 | [diff] [blame] | 224 | if (mMaster.debug) { |
| 225 | Slog.d(TAG, "startSession(" + activityToken + "): hold your horses"); |
| 226 | } |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 227 | return; |
| 228 | } |
| 229 | |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame^] | 230 | if (!whiteListed) { |
Felipe Leme | e8eae9d4 | 2019-02-13 15:41:55 -0800 | [diff] [blame] | 231 | if (mMaster.debug) { |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame^] | 232 | Slog.d(TAG, "startSession(" + componentName + "): package or component " |
| 233 | + "not whitelisted"); |
Felipe Leme | e8eae9d4 | 2019-02-13 15:41:55 -0800 | [diff] [blame] | 234 | } |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame^] | 235 | setClientState(clientReceiver, STATE_DISABLED | STATE_NOT_WHITELISTED, |
Felipe Leme | e8eae9d4 | 2019-02-13 15:41:55 -0800 | [diff] [blame] | 236 | /* binder= */ null); |
| 237 | return; |
| 238 | } |
| 239 | |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 240 | final ContentCaptureServerSession existingSession = mSessions.get(sessionId); |
| 241 | if (existingSession != null) { |
| 242 | Slog.w(TAG, "startSession(id=" + existingSession + ", token=" + activityToken |
| 243 | + ": ignoring because it already exists for " + existingSession.mActivityToken); |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 244 | setClientState(clientReceiver, STATE_DISABLED | STATE_DUPLICATED_ID, |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 245 | /* binder=*/ null); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 246 | return; |
| 247 | } |
| 248 | |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 249 | if (mRemoteService == null) { |
| 250 | updateRemoteServiceLocked(/* disabled= */ false); // already checked for isEnabled |
| 251 | } |
| 252 | |
| 253 | if (mRemoteService == null) { |
| 254 | // TODO(b/119613670): log metrics |
| 255 | Slog.w(TAG, "startSession(id=" + existingSession + ", token=" + activityToken |
| 256 | + ": ignoring because service is not set"); |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 257 | setClientState(clientReceiver, STATE_DISABLED | STATE_NO_SERVICE, |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 258 | /* binder= */ null); |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 259 | return; |
| 260 | } |
| 261 | |
| 262 | final ContentCaptureServerSession newSession = new ContentCaptureServerSession( |
| 263 | activityToken, this, mRemoteService, componentName, taskId, |
| 264 | displayId, sessionId, uid, flags); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 265 | if (mMaster.verbose) { |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 266 | Slog.v(TAG, "startSession(): new session for " |
| 267 | + ComponentName.flattenToShortString(componentName) + " and id " + sessionId); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 268 | } |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 269 | mSessions.put(sessionId, newSession); |
| 270 | newSession.notifySessionStartedLocked(clientReceiver); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Felipe Leme | e8eae9d4 | 2019-02-13 15:41:55 -0800 | [diff] [blame] | 273 | @GuardedBy("mLock") |
| 274 | private boolean isWhitelistedLocked(@NonNull ComponentName componentName) { |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame^] | 275 | return mWhitelistHelper.isWhitelisted(componentName); |
Felipe Leme | e8eae9d4 | 2019-02-13 15:41:55 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame^] | 278 | private void setWhitelist(@Nullable List<String> packages, |
| 279 | @Nullable List<ComponentName> components) { |
Felipe Leme | e8eae9d4 | 2019-02-13 15:41:55 -0800 | [diff] [blame] | 280 | // TODO(b/122595322): add CTS test for when it's null |
| 281 | synchronized (mLock) { |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame^] | 282 | if (mMaster.verbose) { |
| 283 | Slog.v(TAG, "whitelisting packages: " + packages + "and activities: " + components); |
Felipe Leme | e8eae9d4 | 2019-02-13 15:41:55 -0800 | [diff] [blame] | 284 | } |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame^] | 285 | mWhitelistHelper.setWhitelist(new ArraySet<>(packages), new ArraySet<>(components)); |
Felipe Leme | e8eae9d4 | 2019-02-13 15:41:55 -0800 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 289 | // TODO(b/119613670): log metrics |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 290 | @GuardedBy("mLock") |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 291 | public void finishSessionLocked(@NonNull String sessionId) { |
Felipe Leme | 8e2e341 | 2018-11-14 10:39:29 -0800 | [diff] [blame] | 292 | if (!isEnabledLocked()) { |
| 293 | return; |
| 294 | } |
| 295 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 296 | final ContentCaptureServerSession session = mSessions.get(sessionId); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 297 | if (session == null) { |
Felipe Leme | 8e2e341 | 2018-11-14 10:39:29 -0800 | [diff] [blame] | 298 | if (mMaster.debug) { |
| 299 | Slog.d(TAG, "finishSession(): no session with id" + sessionId); |
| 300 | } |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 301 | return; |
| 302 | } |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 303 | if (mMaster.verbose) Slog.v(TAG, "finishSession(): id=" + sessionId); |
| 304 | session.removeSelfLocked(/* notifyRemoteService= */ true); |
Felipe Leme | 7a53408 | 2018-11-05 15:03:04 -0800 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | @GuardedBy("mLock") |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 308 | public void removeUserDataLocked(@NonNull UserDataRemovalRequest request) { |
| 309 | if (!isEnabledLocked()) { |
| 310 | return; |
| 311 | } |
| 312 | assertCallerLocked(request.getPackageName()); |
| 313 | mRemoteService.onUserDataRemovalRequest(request); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Asserts the component is owned by the caller. |
| 318 | */ |
| 319 | @GuardedBy("mLock") |
| 320 | private void assertCallerLocked(@NonNull String packageName) { |
| 321 | final PackageManager pm = getContext().getPackageManager(); |
| 322 | final int callingUid = Binder.getCallingUid(); |
| 323 | final int packageUid; |
| 324 | try { |
| 325 | packageUid = pm.getPackageUidAsUser(packageName, UserHandle.getCallingUserId()); |
| 326 | } catch (NameNotFoundException e) { |
| 327 | throw new SecurityException("Could not verify UID for " + packageName); |
| 328 | } |
| 329 | if (callingUid != packageUid && !LocalServices.getService(ActivityManagerInternal.class) |
| 330 | .hasRunningActivity(callingUid, packageName)) { |
| 331 | final String[] packages = pm.getPackagesForUid(callingUid); |
| 332 | final String callingPackage = packages != null ? packages[0] : "uid-" + callingUid; |
| 333 | Slog.w(TAG, "App (package=" + callingPackage + ", UID=" + callingUid |
| 334 | + ") passed package (" + packageName + ") owned by UID " + packageUid); |
| 335 | |
| 336 | throw new SecurityException("Invalid package: " + packageName); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | @GuardedBy("mLock") |
Winson Chung | fbbb158 | 2018-11-13 16:09:01 -0800 | [diff] [blame] | 341 | public boolean sendActivityAssistDataLocked(@NonNull IBinder activityToken, |
| 342 | @NonNull Bundle data) { |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 343 | final String id = getSessionId(activityToken); |
Winson Chung | fbbb158 | 2018-11-13 16:09:01 -0800 | [diff] [blame] | 344 | if (id != null) { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 345 | final ContentCaptureServerSession session = mSessions.get(id); |
Winson Chung | fbbb158 | 2018-11-13 16:09:01 -0800 | [diff] [blame] | 346 | final Bundle assistData = data.getBundle(ASSIST_KEY_DATA); |
| 347 | final AssistStructure assistStructure = data.getParcelable(ASSIST_KEY_STRUCTURE); |
| 348 | final AssistContent assistContent = data.getParcelable(ASSIST_KEY_CONTENT); |
| 349 | final SnapshotData snapshotData = new SnapshotData(assistData, |
| 350 | assistStructure, assistContent); |
| 351 | session.sendActivitySnapshotLocked(snapshotData); |
| 352 | return true; |
| 353 | } else { |
| 354 | Slog.e(TAG, "Failed to notify activity assist data for activity: " + activityToken); |
| 355 | } |
| 356 | return false; |
| 357 | } |
| 358 | |
| 359 | @GuardedBy("mLock") |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 360 | public void removeSessionLocked(@NonNull String sessionId) { |
Felipe Leme | a7bdb14 | 2018-11-05 16:29:29 -0800 | [diff] [blame] | 361 | mSessions.remove(sessionId); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 362 | } |
| 363 | |
Felipe Leme | 927fbf0 | 2018-11-12 17:27:23 -0800 | [diff] [blame] | 364 | @GuardedBy("mLock") |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 365 | public boolean isContentCaptureServiceForUserLocked(int uid) { |
Felipe Leme | 927fbf0 | 2018-11-12 17:27:23 -0800 | [diff] [blame] | 366 | return uid == getServiceUidLocked(); |
| 367 | } |
| 368 | |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 369 | @GuardedBy("mLock") |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 370 | private ContentCaptureServerSession getSession(@NonNull IBinder activityToken) { |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 371 | for (int i = 0; i < mSessions.size(); i++) { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 372 | final ContentCaptureServerSession session = mSessions.valueAt(i); |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 373 | if (session.mActivityToken.equals(activityToken)) { |
| 374 | return session; |
| 375 | } |
| 376 | } |
| 377 | return null; |
| 378 | } |
| 379 | |
Felipe Leme | 8e2e341 | 2018-11-14 10:39:29 -0800 | [diff] [blame] | 380 | /** |
| 381 | * Destroys the service and all state associated with it. |
| 382 | * |
| 383 | * <p>Called when the service was disabled (for example, if the settings change). |
| 384 | */ |
| 385 | @GuardedBy("mLock") |
| 386 | public void destroyLocked() { |
| 387 | if (mMaster.debug) Slog.d(TAG, "destroyLocked()"); |
Felipe Leme | 5933efb | 2018-11-29 16:08:49 -0800 | [diff] [blame] | 388 | destroySessionsLocked(); |
| 389 | } |
| 390 | |
| 391 | @GuardedBy("mLock") |
| 392 | void destroySessionsLocked() { |
Felipe Leme | 8e2e341 | 2018-11-14 10:39:29 -0800 | [diff] [blame] | 393 | final int numSessions = mSessions.size(); |
| 394 | for (int i = 0; i < numSessions; i++) { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 395 | final ContentCaptureServerSession session = mSessions.valueAt(i); |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 396 | session.destroyLocked(/* notifyRemoteService= */ true); |
Felipe Leme | 8e2e341 | 2018-11-14 10:39:29 -0800 | [diff] [blame] | 397 | } |
| 398 | mSessions.clear(); |
| 399 | } |
| 400 | |
Felipe Leme | 5933efb | 2018-11-29 16:08:49 -0800 | [diff] [blame] | 401 | @GuardedBy("mLock") |
| 402 | void listSessionsLocked(ArrayList<String> output) { |
| 403 | final int numSessions = mSessions.size(); |
| 404 | for (int i = 0; i < numSessions; i++) { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 405 | final ContentCaptureServerSession session = mSessions.valueAt(i); |
Felipe Leme | 5933efb | 2018-11-29 16:08:49 -0800 | [diff] [blame] | 406 | output.add(session.toShortString()); |
| 407 | } |
| 408 | } |
| 409 | |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 410 | @GuardedBy("mLock") |
| 411 | ContentCaptureOptions getOptionsForPackageLocked(@NonNull String packageName) { |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame^] | 412 | if (!mWhitelistHelper.isWhitelisted(packageName)) { |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 413 | if (mMaster.verbose) { |
| 414 | Slog.v(mTag, "getOptionsForPackage(" + packageName + "): not whitelisted"); |
| 415 | } |
| 416 | return null; |
| 417 | } |
| 418 | |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame^] | 419 | final ArraySet<ComponentName> whitelistedComponents = mWhitelistHelper |
| 420 | .getWhitelistedComponents(packageName); |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 421 | ContentCaptureOptions options = new ContentCaptureOptions(mMaster.mDevCfgLoggingLevel, |
| 422 | mMaster.mDevCfgMaxBufferSize, mMaster.mDevCfgIdleFlushingFrequencyMs, |
| 423 | mMaster.mDevCfgTextChangeFlushingFrequencyMs, mMaster.mDevCfgLogHistorySize, |
| 424 | whitelistedComponents); |
| 425 | if (mMaster.verbose) { |
| 426 | Slog.v(mTag, "getOptionsForPackage(" + packageName + "): " + options); |
| 427 | } |
| 428 | return options; |
| 429 | } |
| 430 | |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 431 | @Override |
| 432 | protected void dumpLocked(String prefix, PrintWriter pw) { |
| 433 | super.dumpLocked(prefix, pw); |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 434 | |
| 435 | final String prefix2 = prefix + " "; |
| 436 | if (mRemoteService != null) { |
| 437 | pw.print(prefix); pw.println("remote service:"); |
| 438 | mRemoteService.dump(prefix2, pw); |
| 439 | } |
| 440 | |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame^] | 441 | pw.print(prefix); pw.print("Whitelist: "); pw.println(mWhitelistHelper); |
Felipe Leme | e8eae9d4 | 2019-02-13 15:41:55 -0800 | [diff] [blame] | 442 | |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 443 | if (mSessions.isEmpty()) { |
| 444 | pw.print(prefix); pw.println("no sessions"); |
| 445 | } else { |
Felipe Leme | e8eae9d4 | 2019-02-13 15:41:55 -0800 | [diff] [blame] | 446 | final int sessionsSize = mSessions.size(); |
| 447 | pw.print(prefix); pw.print("number sessions: "); pw.println(sessionsSize); |
| 448 | for (int i = 0; i < sessionsSize; i++) { |
| 449 | pw.print(prefix); pw.print("#"); pw.println(i); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 450 | final ContentCaptureServerSession session = mSessions.valueAt(i); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 451 | session.dumpLocked(prefix2, pw); |
Felipe Leme | e8eae9d4 | 2019-02-13 15:41:55 -0800 | [diff] [blame] | 452 | pw.println(); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | } |
| 456 | |
Winson Chung | fbbb158 | 2018-11-13 16:09:01 -0800 | [diff] [blame] | 457 | /** |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 458 | * Returns the session id associated with the given activity. |
Winson Chung | fbbb158 | 2018-11-13 16:09:01 -0800 | [diff] [blame] | 459 | */ |
| 460 | @GuardedBy("mLock") |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 461 | private String getSessionId(@NonNull IBinder activityToken) { |
Winson Chung | fbbb158 | 2018-11-13 16:09:01 -0800 | [diff] [blame] | 462 | for (int i = 0; i < mSessions.size(); i++) { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 463 | ContentCaptureServerSession session = mSessions.valueAt(i); |
Winson Chung | fbbb158 | 2018-11-13 16:09:01 -0800 | [diff] [blame] | 464 | if (session.isActivitySession(activityToken)) { |
| 465 | return mSessions.keyAt(i); |
| 466 | } |
| 467 | } |
| 468 | return null; |
| 469 | } |
Felipe Leme | af4e4fd | 2019-01-15 08:32:42 -0800 | [diff] [blame] | 470 | |
| 471 | private final class ContentCaptureServiceRemoteCallback extends |
| 472 | IContentCaptureServiceCallback.Stub { |
| 473 | |
| 474 | @Override |
| 475 | public void setContentCaptureWhitelist(List<String> packages, |
| 476 | List<ComponentName> activities) { |
| 477 | if (mMaster.verbose) { |
Felipe Leme | e8eae9d4 | 2019-02-13 15:41:55 -0800 | [diff] [blame] | 478 | Slog.v(TAG, "setContentCaptureWhitelist(packages=" + packages + ", activities=" |
Felipe Leme | af4e4fd | 2019-01-15 08:32:42 -0800 | [diff] [blame] | 479 | + activities + ")"); |
| 480 | } |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame^] | 481 | setWhitelist(packages, activities); |
Felipe Leme | e8eae9d4 | 2019-02-13 15:41:55 -0800 | [diff] [blame] | 482 | |
Felipe Leme | af4e4fd | 2019-01-15 08:32:42 -0800 | [diff] [blame] | 483 | // TODO(b/119613670): log metrics |
| 484 | } |
Felipe Leme | 72e83d8 | 2019-02-13 14:51:17 -0800 | [diff] [blame] | 485 | |
| 486 | @Override |
| 487 | public void disableSelf() { |
| 488 | if (mMaster.verbose) Slog.v(TAG, "disableSelf()"); |
| 489 | |
| 490 | final long token = Binder.clearCallingIdentity(); |
| 491 | try { |
| 492 | Settings.Secure.putStringForUser(getContext().getContentResolver(), |
| 493 | Settings.Secure.CONTENT_CAPTURE_ENABLED, "false", mUserId); |
| 494 | } finally { |
| 495 | Binder.restoreCallingIdentity(token); |
| 496 | } |
| 497 | } |
Felipe Leme | af4e4fd | 2019-01-15 08:32:42 -0800 | [diff] [blame] | 498 | } |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 499 | } |