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 | 0805420 | 2019-03-28 11:29:25 -0700 | [diff] [blame] | 20 | import static android.view.contentcapture.ContentCaptureSession.NO_SESSION_ID; |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 21 | import static android.view.contentcapture.ContentCaptureSession.STATE_DISABLED; |
| 22 | import static android.view.contentcapture.ContentCaptureSession.STATE_DUPLICATED_ID; |
Adam He | bb82c6a | 2019-01-17 14:59:02 -0800 | [diff] [blame] | 23 | import static android.view.contentcapture.ContentCaptureSession.STATE_INTERNAL_ERROR; |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame] | 24 | import static android.view.contentcapture.ContentCaptureSession.STATE_NOT_WHITELISTED; |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 25 | import static android.view.contentcapture.ContentCaptureSession.STATE_NO_SERVICE; |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 26 | |
Adam He | 420947c | 2019-01-23 15:59:09 -0800 | [diff] [blame] | 27 | import static com.android.server.contentcapture.ContentCaptureMetricsLogger.writeServiceEvent; |
| 28 | import static com.android.server.contentcapture.ContentCaptureMetricsLogger.writeSessionEvent; |
| 29 | import static com.android.server.contentcapture.ContentCaptureMetricsLogger.writeSetWhitelistEvent; |
Winson Chung | fbbb158 | 2018-11-13 16:09:01 -0800 | [diff] [blame] | 30 | import static com.android.server.wm.ActivityTaskManagerInternal.ASSIST_KEY_CONTENT; |
| 31 | import static com.android.server.wm.ActivityTaskManagerInternal.ASSIST_KEY_DATA; |
| 32 | import static com.android.server.wm.ActivityTaskManagerInternal.ASSIST_KEY_STRUCTURE; |
| 33 | |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 34 | import android.annotation.NonNull; |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame] | 35 | import android.annotation.Nullable; |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 36 | import android.annotation.UserIdInt; |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 37 | import android.app.ActivityManagerInternal; |
Winson Chung | fbbb158 | 2018-11-13 16:09:01 -0800 | [diff] [blame] | 38 | import android.app.assist.AssistContent; |
| 39 | import android.app.assist.AssistStructure; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 40 | import android.content.ComponentName; |
Adam He | 420947c | 2019-01-23 15:59:09 -0800 | [diff] [blame] | 41 | import android.content.ContentCaptureOptions; |
Adam He | bb82c6a | 2019-01-17 14:59:02 -0800 | [diff] [blame] | 42 | import android.content.pm.ActivityPresentationInfo; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 43 | import android.content.pm.PackageManager; |
| 44 | import android.content.pm.PackageManager.NameNotFoundException; |
| 45 | import android.content.pm.ServiceInfo; |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 46 | import android.os.Binder; |
Winson Chung | fbbb158 | 2018-11-13 16:09:01 -0800 | [diff] [blame] | 47 | import android.os.Bundle; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 48 | import android.os.IBinder; |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 49 | import android.os.UserHandle; |
Felipe Leme | 72e83d8 | 2019-02-13 14:51:17 -0800 | [diff] [blame] | 50 | import android.provider.Settings; |
Felipe Leme | 141864d | 2019-02-27 17:01:51 -0800 | [diff] [blame] | 51 | import android.service.contentcapture.ActivityEvent; |
| 52 | import android.service.contentcapture.ActivityEvent.ActivityEventType; |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 53 | import android.service.contentcapture.ContentCaptureService; |
Felipe Leme | a5d5e2d | 2019-03-19 16:54:55 -0700 | [diff] [blame] | 54 | import android.service.contentcapture.ContentCaptureServiceInfo; |
Adam He | 420947c | 2019-01-23 15:59:09 -0800 | [diff] [blame] | 55 | import android.service.contentcapture.FlushMetrics; |
Felipe Leme | af4e4fd | 2019-01-15 08:32:42 -0800 | [diff] [blame] | 56 | import android.service.contentcapture.IContentCaptureServiceCallback; |
Sergey Volnov | 6e04901 | 2020-01-10 18:10:48 +0000 | [diff] [blame] | 57 | import android.service.contentcapture.IDataShareCallback; |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 58 | import android.service.contentcapture.SnapshotData; |
Felipe Leme | a8d33c2 | 2019-03-25 16:36:09 -0700 | [diff] [blame] | 59 | import android.util.ArrayMap; |
| 60 | import android.util.ArraySet; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 61 | import android.util.Slog; |
Felipe Leme | 0805420 | 2019-03-28 11:29:25 -0700 | [diff] [blame] | 62 | import android.util.SparseArray; |
Felipe Leme | 0a3bec5 | 2019-05-06 15:57:30 -0700 | [diff] [blame] | 63 | import android.util.SparseBooleanArray; |
Felipe Leme | a8d33c2 | 2019-03-25 16:36:09 -0700 | [diff] [blame] | 64 | import android.view.contentcapture.ContentCaptureCondition; |
Felipe Leme | 4439ee6 | 2019-05-08 10:27:49 -0700 | [diff] [blame] | 65 | import android.view.contentcapture.DataRemovalRequest; |
Sergey Volnov | 6e04901 | 2020-01-10 18:10:48 +0000 | [diff] [blame] | 66 | import android.view.contentcapture.DataShareRequest; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 67 | |
| 68 | import com.android.internal.annotations.GuardedBy; |
| 69 | import com.android.internal.os.IResultReceiver; |
Muhammad Qureshi | e2b2432 | 2020-01-28 10:54:17 -0800 | [diff] [blame] | 70 | import com.android.internal.util.FrameworkStatsLog; |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 71 | import com.android.server.LocalServices; |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 72 | import com.android.server.contentcapture.RemoteContentCaptureService.ContentCaptureServiceCallbacks; |
Felipe Leme | 1e3de57 | 2018-12-05 15:44:17 -0800 | [diff] [blame] | 73 | import com.android.server.infra.AbstractPerUserSystemService; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 74 | |
| 75 | import java.io.PrintWriter; |
Felipe Leme | 5933efb | 2018-11-29 16:08:49 -0800 | [diff] [blame] | 76 | import java.util.ArrayList; |
Felipe Leme | af4e4fd | 2019-01-15 08:32:42 -0800 | [diff] [blame] | 77 | import java.util.List; |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 78 | |
| 79 | /** |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 80 | * Per-user instance of {@link ContentCaptureManagerService}. |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 81 | */ |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 82 | final class ContentCapturePerUserService |
| 83 | extends |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 84 | AbstractPerUserSystemService<ContentCapturePerUserService, ContentCaptureManagerService> |
| 85 | implements ContentCaptureServiceCallbacks { |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 86 | |
Felipe Leme | 2f28843 | 2019-03-21 13:57:45 -0700 | [diff] [blame] | 87 | private static final String TAG = ContentCapturePerUserService.class.getSimpleName(); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 88 | |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 89 | @GuardedBy("mLock") |
Felipe Leme | 0805420 | 2019-03-28 11:29:25 -0700 | [diff] [blame] | 90 | private final SparseArray<ContentCaptureServerSession> mSessions = new SparseArray<>(); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 91 | |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 92 | /** |
| 93 | * Reference to the remote service. |
| 94 | * |
| 95 | * <p>It's set in the constructor, but it's also updated when the service's updated in the |
| 96 | * master's cache (for example, because a temporary service was set). |
| 97 | */ |
| 98 | @GuardedBy("mLock") |
Felipe Leme | 2f28843 | 2019-03-21 13:57:45 -0700 | [diff] [blame] | 99 | @Nullable |
| 100 | RemoteContentCaptureService mRemoteService; |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 101 | |
Felipe Leme | af4e4fd | 2019-01-15 08:32:42 -0800 | [diff] [blame] | 102 | private final ContentCaptureServiceRemoteCallback mRemoteServiceCallback = |
| 103 | new ContentCaptureServiceRemoteCallback(); |
| 104 | |
Felipe Leme | e8eae9d4 | 2019-02-13 15:41:55 -0800 | [diff] [blame] | 105 | /** |
Felipe Leme | a8d33c2 | 2019-03-25 16:36:09 -0700 | [diff] [blame] | 106 | * List of conditions keyed by package. |
| 107 | */ |
| 108 | @GuardedBy("mLock") |
| 109 | private final ArrayMap<String, ArraySet<ContentCaptureCondition>> mConditionsByPkg = |
| 110 | new ArrayMap<>(); |
| 111 | |
| 112 | /** |
Felipe Leme | 518fb62 | 2019-03-08 15:35:04 -0800 | [diff] [blame] | 113 | * When {@code true}, remote service died but service state is kept so it's restored after |
| 114 | * the system re-binds to it. |
| 115 | */ |
| 116 | @GuardedBy("mLock") |
| 117 | private boolean mZombie; |
| 118 | |
Felipe Leme | a5d5e2d | 2019-03-19 16:54:55 -0700 | [diff] [blame] | 119 | @GuardedBy("mLock") |
| 120 | private ContentCaptureServiceInfo mInfo; |
| 121 | |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 122 | // TODO(b/111276913): add mechanism to prune stale sessions, similar to Autofill's |
| 123 | |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 124 | ContentCapturePerUserService(@NonNull ContentCaptureManagerService master, |
| 125 | @NonNull Object lock, boolean disabled, @UserIdInt int userId) { |
Felipe Leme | c28711c | 2018-12-20 11:17:02 -0800 | [diff] [blame] | 126 | super(master, lock, userId); |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 127 | updateRemoteServiceLocked(disabled); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Updates the reference to the remote service. |
| 132 | */ |
| 133 | private void updateRemoteServiceLocked(boolean disabled) { |
Felipe Leme | 41e9eb0 | 2019-04-17 13:57:59 -0700 | [diff] [blame] | 134 | if (mMaster.verbose) Slog.v(TAG, "updateRemoteService(disabled=" + disabled + ")"); |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 135 | if (mRemoteService != null) { |
| 136 | if (mMaster.debug) Slog.d(TAG, "updateRemoteService(): destroying old remote service"); |
| 137 | mRemoteService.destroy(); |
| 138 | mRemoteService = null; |
Adam He | 59e96e1 | 2019-03-25 15:49:58 -0700 | [diff] [blame] | 139 | resetContentCaptureWhitelistLocked(); |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | // Updates the component name |
| 143 | final ComponentName serviceComponentName = updateServiceInfoLocked(); |
| 144 | |
| 145 | if (serviceComponentName == null) { |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 146 | if (mMaster.debug) Slog.d(TAG, "updateRemoteService(): no service component name"); |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 147 | return; |
| 148 | } |
| 149 | |
| 150 | if (!disabled) { |
Felipe Leme | 2f28843 | 2019-03-21 13:57:45 -0700 | [diff] [blame] | 151 | if (mMaster.debug) { |
| 152 | Slog.d(TAG, "updateRemoteService(): creating new remote service for " |
| 153 | + serviceComponentName); |
| 154 | } |
Felipe Leme | af4e4fd | 2019-01-15 08:32:42 -0800 | [diff] [blame] | 155 | mRemoteService = new RemoteContentCaptureService(mMaster.getContext(), |
| 156 | ContentCaptureService.SERVICE_INTERFACE, serviceComponentName, |
| 157 | mRemoteServiceCallback, mUserId, this, mMaster.isBindInstantServiceAllowed(), |
Felipe Leme | e764fa2 | 2019-02-21 16:45:35 -0800 | [diff] [blame] | 158 | mMaster.verbose, mMaster.mDevCfgIdleUnbindTimeoutMs); |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 159 | } |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | @Override // from PerUserSystemService |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 163 | protected ServiceInfo newServiceInfoLocked(@NonNull ComponentName serviceComponent) |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 164 | throws NameNotFoundException { |
Felipe Leme | a5d5e2d | 2019-03-19 16:54:55 -0700 | [diff] [blame] | 165 | mInfo = new ContentCaptureServiceInfo(getContext(), serviceComponent, |
| 166 | isTemporaryServiceSetLocked(), mUserId); |
| 167 | return mInfo.getServiceInfo(); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Felipe Leme | ecb08be | 2018-11-27 15:48:47 -0800 | [diff] [blame] | 170 | @Override // from PerUserSystemService |
| 171 | @GuardedBy("mLock") |
| 172 | protected boolean updateLocked(boolean disabled) { |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 173 | final boolean disabledStateChanged = super.updateLocked(disabled); |
Adam He | 6c0afca | 2019-04-15 13:54:11 -0700 | [diff] [blame] | 174 | if (disabledStateChanged) { |
| 175 | // update session content capture enabled state. |
| 176 | for (int i = 0; i < mSessions.size(); i++) { |
| 177 | mSessions.valueAt(i).setContentCaptureEnabledLocked(!disabled); |
| 178 | } |
| 179 | } |
| 180 | destroyLocked(); |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 181 | updateRemoteServiceLocked(disabled); |
| 182 | return disabledStateChanged; |
Felipe Leme | ecb08be | 2018-11-27 15:48:47 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 185 | @Override // from ContentCaptureServiceCallbacks |
| 186 | public void onServiceDied(@NonNull RemoteContentCaptureService service) { |
Felipe Leme | 817b5e1 | 2019-03-07 14:32:56 -0800 | [diff] [blame] | 187 | // Don't do anything; eventually the system will bind to it again... |
| 188 | Slog.w(TAG, "remote service died: " + service); |
Felipe Leme | 518fb62 | 2019-03-08 15:35:04 -0800 | [diff] [blame] | 189 | synchronized (mLock) { |
| 190 | mZombie = true; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Called after the remote service connected, it's used to restore state from a 'zombie' |
| 196 | * service (i.e., after it died). |
| 197 | */ |
| 198 | void onConnected() { |
| 199 | synchronized (mLock) { |
| 200 | if (mZombie) { |
| 201 | // Sanity check - shouldn't happen |
| 202 | if (mRemoteService == null) { |
| 203 | Slog.w(TAG, "Cannot ressurect sessions because remote service is null"); |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | mZombie = false; |
Felipe Leme | 2f28843 | 2019-03-21 13:57:45 -0700 | [diff] [blame] | 208 | resurrectSessionsLocked(); |
Felipe Leme | 518fb62 | 2019-03-08 15:35:04 -0800 | [diff] [blame] | 209 | } |
| 210 | } |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Felipe Leme | 2f28843 | 2019-03-21 13:57:45 -0700 | [diff] [blame] | 213 | private void resurrectSessionsLocked() { |
| 214 | final int numSessions = mSessions.size(); |
| 215 | if (mMaster.debug) { |
| 216 | Slog.d(TAG, "Ressurrecting remote service (" + mRemoteService + ") on " |
| 217 | + numSessions + " sessions"); |
| 218 | } |
| 219 | |
| 220 | for (int i = 0; i < numSessions; i++) { |
| 221 | final ContentCaptureServerSession session = mSessions.valueAt(i); |
| 222 | session.resurrectLocked(); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | void onPackageUpdatingLocked() { |
| 227 | final int numSessions = mSessions.size(); |
| 228 | if (mMaster.debug) { |
| 229 | Slog.d(TAG, "Pausing " + numSessions + " sessions while package is updating"); |
| 230 | } |
| 231 | for (int i = 0; i < numSessions; i++) { |
| 232 | final ContentCaptureServerSession session = mSessions.valueAt(i); |
| 233 | session.pauseLocked(); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | void onPackageUpdatedLocked() { |
| 238 | updateRemoteServiceLocked(!isEnabledLocked()); |
| 239 | resurrectSessionsLocked(); |
| 240 | } |
| 241 | |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 242 | @GuardedBy("mLock") |
| 243 | public void startSessionLocked(@NonNull IBinder activityToken, |
Felipe Leme | 0805420 | 2019-03-28 11:29:25 -0700 | [diff] [blame] | 244 | @NonNull ActivityPresentationInfo activityPresentationInfo, int sessionId, int uid, |
| 245 | int flags, @NonNull IResultReceiver clientReceiver) { |
Adam He | bb82c6a | 2019-01-17 14:59:02 -0800 | [diff] [blame] | 246 | if (activityPresentationInfo == null) { |
| 247 | Slog.w(TAG, "basic activity info is null"); |
| 248 | setClientState(clientReceiver, STATE_DISABLED | STATE_INTERNAL_ERROR, |
| 249 | /* binder= */ null); |
| 250 | return; |
| 251 | } |
| 252 | final int taskId = activityPresentationInfo.taskId; |
| 253 | final int displayId = activityPresentationInfo.displayId; |
| 254 | final ComponentName componentName = activityPresentationInfo.componentName; |
Felipe Leme | 9bee944 | 2019-03-27 13:40:40 -0700 | [diff] [blame] | 255 | final boolean whiteListed = mMaster.mGlobalContentCaptureOptions.isWhitelisted(mUserId, |
Felipe Leme | 41e9eb0 | 2019-04-17 13:57:59 -0700 | [diff] [blame] | 256 | componentName) || mMaster.mGlobalContentCaptureOptions.isWhitelisted(mUserId, |
| 257 | componentName.getPackageName()); |
Felipe Leme | 930f324 | 2019-01-15 16:47:09 -0800 | [diff] [blame] | 258 | final ComponentName serviceComponentName = getServiceComponentName(); |
| 259 | final boolean enabled = isEnabledLocked(); |
Felipe Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 260 | if (mMaster.mRequestsHistory != null) { |
| 261 | final String historyItem = |
| 262 | "id=" + sessionId + " uid=" + uid |
| 263 | + " a=" + ComponentName.flattenToShortString(componentName) |
| 264 | + " t=" + taskId + " d=" + displayId |
| 265 | + " s=" + ComponentName.flattenToShortString(serviceComponentName) |
Felipe Leme | e8eae9d4 | 2019-02-13 15:41:55 -0800 | [diff] [blame] | 266 | + " u=" + mUserId + " f=" + flags + (enabled ? "" : " (disabled)") |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame] | 267 | + " w=" + whiteListed; |
Felipe Leme | d32d8f6f | 2019-02-15 10:25:33 -0800 | [diff] [blame] | 268 | mMaster.mRequestsHistory.log(historyItem); |
| 269 | } |
Felipe Leme | 930f324 | 2019-01-15 16:47:09 -0800 | [diff] [blame] | 270 | |
| 271 | if (!enabled) { |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 272 | // TODO: it would be better to split in differet reasons, like |
Adam He | 420947c | 2019-01-23 15:59:09 -0800 | [diff] [blame] | 273 | // STATE_DISABLED_NO and STATE_DISABLED_BY_DEVICE_POLICY |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 274 | setClientState(clientReceiver, STATE_DISABLED | STATE_NO_SERVICE, |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 275 | /* binder= */ null); |
Adam He | 420947c | 2019-01-23 15:59:09 -0800 | [diff] [blame] | 276 | // Log metrics. |
| 277 | writeSessionEvent(sessionId, |
Muhammad Qureshi | e2b2432 | 2020-01-28 10:54:17 -0800 | [diff] [blame] | 278 | FrameworkStatsLog.CONTENT_CAPTURE_SESSION_EVENTS__EVENT__SESSION_NOT_CREATED, |
Adam He | 420947c | 2019-01-23 15:59:09 -0800 | [diff] [blame] | 279 | STATE_DISABLED | STATE_NO_SERVICE, serviceComponentName, |
| 280 | componentName, /* isChildSession= */ false); |
Felipe Leme | 8e2e341 | 2018-11-14 10:39:29 -0800 | [diff] [blame] | 281 | return; |
| 282 | } |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 283 | if (serviceComponentName == null) { |
| 284 | // TODO(b/111276913): this happens when the system service is starting, we should |
| 285 | // probably handle it in a more elegant way (like waiting for boot_complete or |
| 286 | // something like that |
Felipe Leme | 8e2e341 | 2018-11-14 10:39:29 -0800 | [diff] [blame] | 287 | if (mMaster.debug) { |
| 288 | Slog.d(TAG, "startSession(" + activityToken + "): hold your horses"); |
| 289 | } |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 290 | return; |
| 291 | } |
| 292 | |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame] | 293 | if (!whiteListed) { |
Felipe Leme | e8eae9d4 | 2019-02-13 15:41:55 -0800 | [diff] [blame] | 294 | if (mMaster.debug) { |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame] | 295 | Slog.d(TAG, "startSession(" + componentName + "): package or component " |
| 296 | + "not whitelisted"); |
Felipe Leme | e8eae9d4 | 2019-02-13 15:41:55 -0800 | [diff] [blame] | 297 | } |
Adam He | 6240eab | 2019-02-25 13:34:45 -0800 | [diff] [blame] | 298 | setClientState(clientReceiver, STATE_DISABLED | STATE_NOT_WHITELISTED, |
Felipe Leme | e8eae9d4 | 2019-02-13 15:41:55 -0800 | [diff] [blame] | 299 | /* binder= */ null); |
Adam He | 420947c | 2019-01-23 15:59:09 -0800 | [diff] [blame] | 300 | // Log metrics. |
| 301 | writeSessionEvent(sessionId, |
Muhammad Qureshi | e2b2432 | 2020-01-28 10:54:17 -0800 | [diff] [blame] | 302 | FrameworkStatsLog.CONTENT_CAPTURE_SESSION_EVENTS__EVENT__SESSION_NOT_CREATED, |
Adam He | 420947c | 2019-01-23 15:59:09 -0800 | [diff] [blame] | 303 | STATE_DISABLED | STATE_NOT_WHITELISTED, serviceComponentName, |
| 304 | componentName, /* isChildSession= */ false); |
Felipe Leme | e8eae9d4 | 2019-02-13 15:41:55 -0800 | [diff] [blame] | 305 | return; |
| 306 | } |
| 307 | |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 308 | final ContentCaptureServerSession existingSession = mSessions.get(sessionId); |
| 309 | if (existingSession != null) { |
| 310 | Slog.w(TAG, "startSession(id=" + existingSession + ", token=" + activityToken |
| 311 | + ": ignoring because it already exists for " + existingSession.mActivityToken); |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 312 | setClientState(clientReceiver, STATE_DISABLED | STATE_DUPLICATED_ID, |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 313 | /* binder=*/ null); |
Adam He | 420947c | 2019-01-23 15:59:09 -0800 | [diff] [blame] | 314 | // Log metrics. |
| 315 | writeSessionEvent(sessionId, |
Muhammad Qureshi | e2b2432 | 2020-01-28 10:54:17 -0800 | [diff] [blame] | 316 | FrameworkStatsLog.CONTENT_CAPTURE_SESSION_EVENTS__EVENT__SESSION_NOT_CREATED, |
Adam He | 420947c | 2019-01-23 15:59:09 -0800 | [diff] [blame] | 317 | STATE_DISABLED | STATE_DUPLICATED_ID, |
| 318 | serviceComponentName, componentName, /* isChildSession= */ false); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 319 | return; |
| 320 | } |
| 321 | |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 322 | if (mRemoteService == null) { |
| 323 | updateRemoteServiceLocked(/* disabled= */ false); // already checked for isEnabled |
| 324 | } |
| 325 | |
| 326 | if (mRemoteService == null) { |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 327 | Slog.w(TAG, "startSession(id=" + existingSession + ", token=" + activityToken |
| 328 | + ": ignoring because service is not set"); |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 329 | setClientState(clientReceiver, STATE_DISABLED | STATE_NO_SERVICE, |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 330 | /* binder= */ null); |
Adam He | 420947c | 2019-01-23 15:59:09 -0800 | [diff] [blame] | 331 | // Log metrics. |
| 332 | writeSessionEvent(sessionId, |
Muhammad Qureshi | e2b2432 | 2020-01-28 10:54:17 -0800 | [diff] [blame] | 333 | FrameworkStatsLog.CONTENT_CAPTURE_SESSION_EVENTS__EVENT__SESSION_NOT_CREATED, |
Adam He | 420947c | 2019-01-23 15:59:09 -0800 | [diff] [blame] | 334 | STATE_DISABLED | STATE_NO_SERVICE, serviceComponentName, |
| 335 | componentName, /* isChildSession= */ false); |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 336 | return; |
| 337 | } |
| 338 | |
Felipe Leme | 2f28843 | 2019-03-21 13:57:45 -0700 | [diff] [blame] | 339 | // Make sure service is bound, just in case the initial connection failed somehow |
| 340 | mRemoteService.ensureBoundLocked(); |
| 341 | |
Felipe Leme | 6334116 | 2019-04-16 14:03:56 -0700 | [diff] [blame] | 342 | final ContentCaptureServerSession newSession = new ContentCaptureServerSession(mLock, |
Felipe Leme | 2f28843 | 2019-03-21 13:57:45 -0700 | [diff] [blame] | 343 | activityToken, this, componentName, clientReceiver, taskId, displayId, sessionId, |
| 344 | uid, flags); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 345 | if (mMaster.verbose) { |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 346 | Slog.v(TAG, "startSession(): new session for " |
| 347 | + ComponentName.flattenToShortString(componentName) + " and id " + sessionId); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 348 | } |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 349 | mSessions.put(sessionId, newSession); |
| 350 | newSession.notifySessionStartedLocked(clientReceiver); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 351 | } |
| 352 | |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 353 | @GuardedBy("mLock") |
Felipe Leme | 0805420 | 2019-03-28 11:29:25 -0700 | [diff] [blame] | 354 | public void finishSessionLocked(int sessionId) { |
Felipe Leme | 8e2e341 | 2018-11-14 10:39:29 -0800 | [diff] [blame] | 355 | if (!isEnabledLocked()) { |
| 356 | return; |
| 357 | } |
| 358 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 359 | final ContentCaptureServerSession session = mSessions.get(sessionId); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 360 | if (session == null) { |
Felipe Leme | 8e2e341 | 2018-11-14 10:39:29 -0800 | [diff] [blame] | 361 | if (mMaster.debug) { |
| 362 | Slog.d(TAG, "finishSession(): no session with id" + sessionId); |
| 363 | } |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 364 | return; |
| 365 | } |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 366 | if (mMaster.verbose) Slog.v(TAG, "finishSession(): id=" + sessionId); |
| 367 | session.removeSelfLocked(/* notifyRemoteService= */ true); |
Felipe Leme | 7a53408 | 2018-11-05 15:03:04 -0800 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | @GuardedBy("mLock") |
Felipe Leme | 4439ee6 | 2019-05-08 10:27:49 -0700 | [diff] [blame] | 371 | public void removeDataLocked(@NonNull DataRemovalRequest request) { |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 372 | if (!isEnabledLocked()) { |
| 373 | return; |
| 374 | } |
| 375 | assertCallerLocked(request.getPackageName()); |
Felipe Leme | 4439ee6 | 2019-05-08 10:27:49 -0700 | [diff] [blame] | 376 | mRemoteService.onDataRemovalRequest(request); |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 377 | } |
| 378 | |
Felipe Leme | f8b8778 | 2019-03-20 14:31:18 -0700 | [diff] [blame] | 379 | @GuardedBy("mLock") |
Sergey Volnov | 6e04901 | 2020-01-10 18:10:48 +0000 | [diff] [blame] | 380 | public void onDataSharedLocked(@NonNull DataShareRequest request, |
| 381 | IDataShareCallback.Stub dataShareCallback) { |
| 382 | if (!isEnabledLocked()) { |
| 383 | return; |
| 384 | } |
| 385 | assertCallerLocked(request.getPackageName()); |
| 386 | mRemoteService.onDataShareRequest(request, dataShareCallback); |
| 387 | } |
| 388 | |
| 389 | @GuardedBy("mLock") |
Felipe Leme | f8b8778 | 2019-03-20 14:31:18 -0700 | [diff] [blame] | 390 | @Nullable |
| 391 | public ComponentName getServiceSettingsActivityLocked() { |
| 392 | if (mInfo == null) return null; |
| 393 | |
| 394 | final String activityName = mInfo.getSettingsActivity(); |
| 395 | if (activityName == null) return null; |
| 396 | |
| 397 | final String packageName = mInfo.getServiceInfo().packageName; |
| 398 | return new ComponentName(packageName, activityName); |
| 399 | } |
| 400 | |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 401 | /** |
| 402 | * Asserts the component is owned by the caller. |
| 403 | */ |
| 404 | @GuardedBy("mLock") |
| 405 | private void assertCallerLocked(@NonNull String packageName) { |
| 406 | final PackageManager pm = getContext().getPackageManager(); |
| 407 | final int callingUid = Binder.getCallingUid(); |
| 408 | final int packageUid; |
| 409 | try { |
| 410 | packageUid = pm.getPackageUidAsUser(packageName, UserHandle.getCallingUserId()); |
| 411 | } catch (NameNotFoundException e) { |
| 412 | throw new SecurityException("Could not verify UID for " + packageName); |
| 413 | } |
| 414 | if (callingUid != packageUid && !LocalServices.getService(ActivityManagerInternal.class) |
| 415 | .hasRunningActivity(callingUid, packageName)) { |
| 416 | final String[] packages = pm.getPackagesForUid(callingUid); |
| 417 | final String callingPackage = packages != null ? packages[0] : "uid-" + callingUid; |
| 418 | Slog.w(TAG, "App (package=" + callingPackage + ", UID=" + callingUid |
| 419 | + ") passed package (" + packageName + ") owned by UID " + packageUid); |
| 420 | |
| 421 | throw new SecurityException("Invalid package: " + packageName); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | @GuardedBy("mLock") |
Winson Chung | fbbb158 | 2018-11-13 16:09:01 -0800 | [diff] [blame] | 426 | public boolean sendActivityAssistDataLocked(@NonNull IBinder activityToken, |
| 427 | @NonNull Bundle data) { |
Felipe Leme | 0805420 | 2019-03-28 11:29:25 -0700 | [diff] [blame] | 428 | final int id = getSessionId(activityToken); |
| 429 | if (id != NO_SESSION_ID) { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 430 | final ContentCaptureServerSession session = mSessions.get(id); |
Winson Chung | fbbb158 | 2018-11-13 16:09:01 -0800 | [diff] [blame] | 431 | final Bundle assistData = data.getBundle(ASSIST_KEY_DATA); |
| 432 | final AssistStructure assistStructure = data.getParcelable(ASSIST_KEY_STRUCTURE); |
| 433 | final AssistContent assistContent = data.getParcelable(ASSIST_KEY_CONTENT); |
| 434 | final SnapshotData snapshotData = new SnapshotData(assistData, |
| 435 | assistStructure, assistContent); |
| 436 | session.sendActivitySnapshotLocked(snapshotData); |
| 437 | return true; |
| 438 | } else { |
| 439 | Slog.e(TAG, "Failed to notify activity assist data for activity: " + activityToken); |
| 440 | } |
| 441 | return false; |
| 442 | } |
| 443 | |
| 444 | @GuardedBy("mLock") |
Felipe Leme | 0805420 | 2019-03-28 11:29:25 -0700 | [diff] [blame] | 445 | public void removeSessionLocked(int sessionId) { |
Felipe Leme | a7bdb14 | 2018-11-05 16:29:29 -0800 | [diff] [blame] | 446 | mSessions.remove(sessionId); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 447 | } |
| 448 | |
Felipe Leme | 927fbf0 | 2018-11-12 17:27:23 -0800 | [diff] [blame] | 449 | @GuardedBy("mLock") |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 450 | public boolean isContentCaptureServiceForUserLocked(int uid) { |
Felipe Leme | 927fbf0 | 2018-11-12 17:27:23 -0800 | [diff] [blame] | 451 | return uid == getServiceUidLocked(); |
| 452 | } |
| 453 | |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 454 | @GuardedBy("mLock") |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 455 | private ContentCaptureServerSession getSession(@NonNull IBinder activityToken) { |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 456 | for (int i = 0; i < mSessions.size(); i++) { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 457 | final ContentCaptureServerSession session = mSessions.valueAt(i); |
Felipe Leme | 284ad1c | 2018-11-15 18:16:12 -0800 | [diff] [blame] | 458 | if (session.mActivityToken.equals(activityToken)) { |
| 459 | return session; |
| 460 | } |
| 461 | } |
| 462 | return null; |
| 463 | } |
| 464 | |
Felipe Leme | 8e2e341 | 2018-11-14 10:39:29 -0800 | [diff] [blame] | 465 | /** |
| 466 | * Destroys the service and all state associated with it. |
| 467 | * |
| 468 | * <p>Called when the service was disabled (for example, if the settings change). |
| 469 | */ |
| 470 | @GuardedBy("mLock") |
| 471 | public void destroyLocked() { |
| 472 | if (mMaster.debug) Slog.d(TAG, "destroyLocked()"); |
Adam He | d295472 | 2019-03-08 11:28:52 -0800 | [diff] [blame] | 473 | if (mRemoteService != null) { |
| 474 | mRemoteService.destroy(); |
| 475 | } |
Felipe Leme | 5933efb | 2018-11-29 16:08:49 -0800 | [diff] [blame] | 476 | destroySessionsLocked(); |
| 477 | } |
| 478 | |
| 479 | @GuardedBy("mLock") |
| 480 | void destroySessionsLocked() { |
Felipe Leme | 8e2e341 | 2018-11-14 10:39:29 -0800 | [diff] [blame] | 481 | final int numSessions = mSessions.size(); |
| 482 | for (int i = 0; i < numSessions; i++) { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 483 | final ContentCaptureServerSession session = mSessions.valueAt(i); |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 484 | session.destroyLocked(/* notifyRemoteService= */ true); |
Felipe Leme | 8e2e341 | 2018-11-14 10:39:29 -0800 | [diff] [blame] | 485 | } |
| 486 | mSessions.clear(); |
| 487 | } |
| 488 | |
Felipe Leme | 5933efb | 2018-11-29 16:08:49 -0800 | [diff] [blame] | 489 | @GuardedBy("mLock") |
| 490 | void listSessionsLocked(ArrayList<String> output) { |
| 491 | final int numSessions = mSessions.size(); |
| 492 | for (int i = 0; i < numSessions; i++) { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 493 | final ContentCaptureServerSession session = mSessions.valueAt(i); |
Felipe Leme | 5933efb | 2018-11-29 16:08:49 -0800 | [diff] [blame] | 494 | output.add(session.toShortString()); |
| 495 | } |
| 496 | } |
| 497 | |
Felipe Leme | 326f15a | 2019-02-19 09:42:24 -0800 | [diff] [blame] | 498 | @GuardedBy("mLock") |
Felipe Leme | a8d33c2 | 2019-03-25 16:36:09 -0700 | [diff] [blame] | 499 | @Nullable |
Felipe Leme | a8d33c2 | 2019-03-25 16:36:09 -0700 | [diff] [blame] | 500 | ArraySet<ContentCaptureCondition> getContentCaptureConditionsLocked( |
| 501 | @NonNull String packageName) { |
| 502 | return mConditionsByPkg.get(packageName); |
| 503 | } |
| 504 | |
| 505 | @GuardedBy("mLock") |
Felipe Leme | 141864d | 2019-02-27 17:01:51 -0800 | [diff] [blame] | 506 | void onActivityEventLocked(@NonNull ComponentName componentName, @ActivityEventType int type) { |
| 507 | if (mRemoteService == null) { |
| 508 | if (mMaster.debug) Slog.d(mTag, "onActivityEvent(): no remote service"); |
| 509 | return; |
| 510 | } |
| 511 | final ActivityEvent event = new ActivityEvent(componentName, type); |
| 512 | |
| 513 | if (mMaster.verbose) Slog.v(mTag, "onActivityEvent(): " + event); |
| 514 | |
| 515 | mRemoteService.onActivityLifecycleEvent(event); |
| 516 | } |
| 517 | |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 518 | @Override |
| 519 | protected void dumpLocked(String prefix, PrintWriter pw) { |
| 520 | super.dumpLocked(prefix, pw); |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 521 | |
Felipe Leme | a5d5e2d | 2019-03-19 16:54:55 -0700 | [diff] [blame] | 522 | final String prefix2 = prefix + " "; |
| 523 | pw.print(prefix); pw.print("Service Info: "); |
| 524 | if (mInfo == null) { |
| 525 | pw.println("N/A"); |
| 526 | } else { |
| 527 | pw.println(); |
| 528 | mInfo.dump(prefix2, pw); |
| 529 | } |
Felipe Leme | 518fb62 | 2019-03-08 15:35:04 -0800 | [diff] [blame] | 530 | pw.print(prefix); pw.print("Zombie: "); pw.println(mZombie); |
| 531 | |
Felipe Leme | 50b33dc | 2018-12-20 16:01:39 -0800 | [diff] [blame] | 532 | if (mRemoteService != null) { |
| 533 | pw.print(prefix); pw.println("remote service:"); |
| 534 | mRemoteService.dump(prefix2, pw); |
| 535 | } |
| 536 | |
Felipe Leme | 0805420 | 2019-03-28 11:29:25 -0700 | [diff] [blame] | 537 | if (mSessions.size() == 0) { |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 538 | pw.print(prefix); pw.println("no sessions"); |
| 539 | } else { |
Felipe Leme | e8eae9d4 | 2019-02-13 15:41:55 -0800 | [diff] [blame] | 540 | final int sessionsSize = mSessions.size(); |
| 541 | pw.print(prefix); pw.print("number sessions: "); pw.println(sessionsSize); |
| 542 | for (int i = 0; i < sessionsSize; i++) { |
| 543 | pw.print(prefix); pw.print("#"); pw.println(i); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 544 | final ContentCaptureServerSession session = mSessions.valueAt(i); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 545 | session.dumpLocked(prefix2, pw); |
Felipe Leme | e8eae9d4 | 2019-02-13 15:41:55 -0800 | [diff] [blame] | 546 | pw.println(); |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | } |
| 550 | |
Winson Chung | fbbb158 | 2018-11-13 16:09:01 -0800 | [diff] [blame] | 551 | /** |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 552 | * Returns the session id associated with the given activity. |
Winson Chung | fbbb158 | 2018-11-13 16:09:01 -0800 | [diff] [blame] | 553 | */ |
| 554 | @GuardedBy("mLock") |
Felipe Leme | 0805420 | 2019-03-28 11:29:25 -0700 | [diff] [blame] | 555 | private int getSessionId(@NonNull IBinder activityToken) { |
Winson Chung | fbbb158 | 2018-11-13 16:09:01 -0800 | [diff] [blame] | 556 | for (int i = 0; i < mSessions.size(); i++) { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 557 | ContentCaptureServerSession session = mSessions.valueAt(i); |
Winson Chung | fbbb158 | 2018-11-13 16:09:01 -0800 | [diff] [blame] | 558 | if (session.isActivitySession(activityToken)) { |
| 559 | return mSessions.keyAt(i); |
| 560 | } |
| 561 | } |
Felipe Leme | 0805420 | 2019-03-28 11:29:25 -0700 | [diff] [blame] | 562 | return NO_SESSION_ID; |
Winson Chung | fbbb158 | 2018-11-13 16:09:01 -0800 | [diff] [blame] | 563 | } |
Felipe Leme | af4e4fd | 2019-01-15 08:32:42 -0800 | [diff] [blame] | 564 | |
Adam He | 59e96e1 | 2019-03-25 15:49:58 -0700 | [diff] [blame] | 565 | /** |
| 566 | * Resets the content capture whitelist. |
| 567 | */ |
| 568 | @GuardedBy("mLock") |
| 569 | private void resetContentCaptureWhitelistLocked() { |
| 570 | if (mMaster.verbose) { |
| 571 | Slog.v(TAG, "resetting content capture whitelist"); |
| 572 | } |
Felipe Leme | 9bee944 | 2019-03-27 13:40:40 -0700 | [diff] [blame] | 573 | mMaster.mGlobalContentCaptureOptions.resetWhitelist(mUserId); |
Adam He | 59e96e1 | 2019-03-25 15:49:58 -0700 | [diff] [blame] | 574 | } |
| 575 | |
Felipe Leme | af4e4fd | 2019-01-15 08:32:42 -0800 | [diff] [blame] | 576 | private final class ContentCaptureServiceRemoteCallback extends |
| 577 | IContentCaptureServiceCallback.Stub { |
| 578 | |
| 579 | @Override |
| 580 | public void setContentCaptureWhitelist(List<String> packages, |
| 581 | List<ComponentName> activities) { |
Felipe Leme | 2f28843 | 2019-03-21 13:57:45 -0700 | [diff] [blame] | 582 | // TODO(b/122595322): add CTS test for when it's null |
Felipe Leme | af4e4fd | 2019-01-15 08:32:42 -0800 | [diff] [blame] | 583 | if (mMaster.verbose) { |
Felipe Leme | 2f28843 | 2019-03-21 13:57:45 -0700 | [diff] [blame] | 584 | Slog.v(TAG, "setContentCaptureWhitelist(" + (packages == null |
| 585 | ? "null_packages" : packages.size() + " packages") |
| 586 | + ", " + (activities == null |
Adam He | 6c0afca | 2019-04-15 13:54:11 -0700 | [diff] [blame] | 587 | ? "null_activities" : activities.size() + " activities") + ")" |
| 588 | + " for user " + mUserId); |
Felipe Leme | af4e4fd | 2019-01-15 08:32:42 -0800 | [diff] [blame] | 589 | } |
Felipe Leme | 9bee944 | 2019-03-27 13:40:40 -0700 | [diff] [blame] | 590 | mMaster.mGlobalContentCaptureOptions.setWhitelist(mUserId, packages, activities); |
Adam He | 420947c | 2019-01-23 15:59:09 -0800 | [diff] [blame] | 591 | writeSetWhitelistEvent(getServiceComponentName(), packages, activities); |
Felipe Leme | 0a3bec5 | 2019-05-06 15:57:30 -0700 | [diff] [blame] | 592 | |
| 593 | // Must disable session that are not the whitelist anymore... |
| 594 | final int numSessions = mSessions.size(); |
| 595 | if (numSessions <= 0) return; |
| 596 | |
| 597 | // ...but without holding the lock on mGlobalContentCaptureOptions |
| 598 | final SparseBooleanArray blacklistedSessions = new SparseBooleanArray(numSessions); |
| 599 | |
| 600 | for (int i = 0; i < numSessions; i++) { |
| 601 | final ContentCaptureServerSession session = mSessions.valueAt(i); |
| 602 | final boolean whitelisted = mMaster.mGlobalContentCaptureOptions |
| 603 | .isWhitelisted(mUserId, session.appComponentName); |
| 604 | if (!whitelisted) { |
| 605 | final int sessionId = mSessions.keyAt(i); |
| 606 | if (mMaster.debug) { |
| 607 | Slog.d(TAG, "marking session " + sessionId + " (" + session.appComponentName |
| 608 | + ") for un-whitelisting"); |
| 609 | } |
| 610 | blacklistedSessions.append(sessionId, true); |
| 611 | } |
| 612 | } |
| 613 | final int numBlacklisted = blacklistedSessions.size(); |
| 614 | |
| 615 | if (numBlacklisted <= 0) return; |
| 616 | |
| 617 | synchronized (mLock) { |
| 618 | for (int i = 0; i < numBlacklisted; i++) { |
| 619 | final int sessionId = blacklistedSessions.keyAt(i); |
| 620 | if (mMaster.debug) Slog.d(TAG, "un-whitelisting " + sessionId); |
| 621 | final ContentCaptureServerSession session = mSessions.get(sessionId); |
| 622 | session.setContentCaptureEnabledLocked(false); |
| 623 | } |
| 624 | } |
Felipe Leme | af4e4fd | 2019-01-15 08:32:42 -0800 | [diff] [blame] | 625 | } |
Felipe Leme | 72e83d8 | 2019-02-13 14:51:17 -0800 | [diff] [blame] | 626 | |
| 627 | @Override |
Felipe Leme | a8d33c2 | 2019-03-25 16:36:09 -0700 | [diff] [blame] | 628 | public void setContentCaptureConditions(String packageName, |
| 629 | List<ContentCaptureCondition> conditions) { |
| 630 | if (mMaster.verbose) { |
| 631 | Slog.v(TAG, "setContentCaptureConditions(" + packageName + "): " |
| 632 | + (conditions == null ? "null" : conditions.size() + " conditions")); |
| 633 | } |
| 634 | synchronized (mLock) { |
| 635 | if (conditions == null) { |
| 636 | mConditionsByPkg.remove(packageName); |
| 637 | } else { |
| 638 | mConditionsByPkg.put(packageName, new ArraySet<>(conditions)); |
| 639 | } |
| 640 | } |
Felipe Leme | a8d33c2 | 2019-03-25 16:36:09 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | @Override |
Felipe Leme | 72e83d8 | 2019-02-13 14:51:17 -0800 | [diff] [blame] | 644 | public void disableSelf() { |
| 645 | if (mMaster.verbose) Slog.v(TAG, "disableSelf()"); |
| 646 | |
| 647 | final long token = Binder.clearCallingIdentity(); |
| 648 | try { |
| 649 | Settings.Secure.putStringForUser(getContext().getContentResolver(), |
Felipe Leme | 8db0036 | 2019-03-06 15:38:19 -0800 | [diff] [blame] | 650 | Settings.Secure.CONTENT_CAPTURE_ENABLED, "0", mUserId); |
Felipe Leme | 72e83d8 | 2019-02-13 14:51:17 -0800 | [diff] [blame] | 651 | } finally { |
| 652 | Binder.restoreCallingIdentity(token); |
| 653 | } |
Muhammad Qureshi | e2b2432 | 2020-01-28 10:54:17 -0800 | [diff] [blame] | 654 | writeServiceEvent(FrameworkStatsLog.CONTENT_CAPTURE_SERVICE_EVENTS__EVENT__SET_DISABLED, |
Adam He | 420947c | 2019-01-23 15:59:09 -0800 | [diff] [blame] | 655 | getServiceComponentName()); |
| 656 | } |
| 657 | |
| 658 | @Override |
| 659 | public void writeSessionFlush(int sessionId, ComponentName app, FlushMetrics flushMetrics, |
| 660 | ContentCaptureOptions options, int flushReason) { |
| 661 | ContentCaptureMetricsLogger.writeSessionFlush(sessionId, getServiceComponentName(), app, |
| 662 | flushMetrics, options, flushReason); |
Felipe Leme | 72e83d8 | 2019-02-13 14:51:17 -0800 | [diff] [blame] | 663 | } |
Felipe Leme | af4e4fd | 2019-01-15 08:32:42 -0800 | [diff] [blame] | 664 | } |
Felipe Leme | e348dc3 | 2018-11-05 12:35:29 -0800 | [diff] [blame] | 665 | } |