Felipe Leme | 749b889 | 2018-12-03 16:30:30 -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 | package android.service.contentcapture; |
| 17 | |
| 18 | import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage; |
| 19 | |
| 20 | import android.annotation.CallSuper; |
| 21 | import android.annotation.NonNull; |
| 22 | import android.annotation.Nullable; |
| 23 | import android.annotation.SystemApi; |
Felipe Leme | 19652c0 | 2019-02-04 13:01:29 -0800 | [diff] [blame] | 24 | import android.annotation.TestApi; |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 25 | import android.app.Service; |
| 26 | import android.content.ComponentName; |
| 27 | import android.content.Intent; |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 28 | import android.content.pm.ParceledListSlice; |
| 29 | import android.os.Binder; |
| 30 | import android.os.Bundle; |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 31 | import android.os.Handler; |
| 32 | import android.os.IBinder; |
| 33 | import android.os.Looper; |
| 34 | import android.os.RemoteException; |
Felipe Leme | bb98ed6 | 2018-12-21 09:29:27 -0800 | [diff] [blame] | 35 | import android.service.autofill.AutofillService; |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 36 | import android.util.ArrayMap; |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 37 | import android.util.Log; |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 38 | import android.util.Slog; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 39 | import android.view.contentcapture.ContentCaptureContext; |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 40 | import android.view.contentcapture.ContentCaptureEvent; |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 41 | import android.view.contentcapture.ContentCaptureManager; |
| 42 | import android.view.contentcapture.ContentCaptureSession; |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 43 | import android.view.contentcapture.ContentCaptureSessionId; |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 44 | import android.view.contentcapture.IContentCaptureDirectManager; |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 45 | import android.view.contentcapture.MainContentCaptureSession; |
Felipe Leme | 62e45b0 | 2019-01-10 10:22:00 -0800 | [diff] [blame] | 46 | import android.view.contentcapture.UserDataRemovalRequest; |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 47 | |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 48 | import com.android.internal.os.IResultReceiver; |
| 49 | |
| 50 | import java.io.FileDescriptor; |
| 51 | import java.io.PrintWriter; |
Felipe Leme | 7a3c9f5 | 2019-02-13 16:32:49 -0800 | [diff] [blame] | 52 | import java.util.ArrayList; |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 53 | import java.util.List; |
Felipe Leme | 7a3c9f5 | 2019-02-13 16:32:49 -0800 | [diff] [blame] | 54 | import java.util.Set; |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * A service used to capture the content of the screen to provide contextual data in other areas of |
| 58 | * the system such as Autofill. |
| 59 | * |
| 60 | * @hide |
| 61 | */ |
| 62 | @SystemApi |
Felipe Leme | 19652c0 | 2019-02-04 13:01:29 -0800 | [diff] [blame] | 63 | @TestApi |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 64 | public abstract class ContentCaptureService extends Service { |
| 65 | |
| 66 | private static final String TAG = ContentCaptureService.class.getSimpleName(); |
| 67 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 68 | // TODO(b/121044306): STOPSHIP use dynamic value, or change to false |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 69 | static final boolean DEBUG = true; |
| 70 | static final boolean VERBOSE = false; |
| 71 | |
| 72 | /** |
| 73 | * The {@link Intent} that must be declared as handled by the service. |
| 74 | * |
| 75 | * <p>To be supported, the service must also require the |
| 76 | * {@link android.Manifest.permission#BIND_CONTENT_CAPTURE_SERVICE} permission so |
| 77 | * that other applications can not abuse it. |
| 78 | */ |
| 79 | public static final String SERVICE_INTERFACE = |
| 80 | "android.service.contentcapture.ContentCaptureService"; |
| 81 | |
| 82 | private Handler mHandler; |
Felipe Leme | af4e4fd | 2019-01-15 08:32:42 -0800 | [diff] [blame] | 83 | private IContentCaptureServiceCallback mCallback; |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 84 | |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 85 | /** |
| 86 | * Binder that receives calls from the system server. |
| 87 | */ |
| 88 | private final IContentCaptureService mServerInterface = new IContentCaptureService.Stub() { |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 89 | |
| 90 | @Override |
Felipe Leme | af4e4fd | 2019-01-15 08:32:42 -0800 | [diff] [blame] | 91 | public void onConnected(IBinder callback) { |
| 92 | mHandler.sendMessage(obtainMessage(ContentCaptureService::handleOnConnected, |
| 93 | ContentCaptureService.this, callback)); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void onDisconnected() { |
| 98 | mHandler.sendMessage(obtainMessage(ContentCaptureService::handleOnDisconnected, |
| 99 | ContentCaptureService.this)); |
Felipe Leme | bb98ed6 | 2018-12-21 09:29:27 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 103 | public void onSessionStarted(ContentCaptureContext context, String sessionId, int uid, |
| 104 | IResultReceiver clientReceiver) { |
| 105 | mHandler.sendMessage(obtainMessage(ContentCaptureService::handleOnCreateSession, |
| 106 | ContentCaptureService.this, context, sessionId, uid, clientReceiver)); |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public void onActivitySnapshot(String sessionId, SnapshotData snapshotData) { |
| 111 | mHandler.sendMessage( |
| 112 | obtainMessage(ContentCaptureService::handleOnActivitySnapshot, |
| 113 | ContentCaptureService.this, sessionId, snapshotData)); |
| 114 | } |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 115 | |
| 116 | @Override |
| 117 | public void onSessionFinished(String sessionId) { |
| 118 | mHandler.sendMessage(obtainMessage(ContentCaptureService::handleFinishSession, |
| 119 | ContentCaptureService.this, sessionId)); |
| 120 | } |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 121 | |
| 122 | @Override |
| 123 | public void onUserDataRemovalRequest(UserDataRemovalRequest request) { |
| 124 | mHandler.sendMessage( |
| 125 | obtainMessage(ContentCaptureService::handleOnUserDataRemovalRequest, |
| 126 | ContentCaptureService.this, request)); |
| 127 | } |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 128 | }; |
| 129 | |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 130 | /** |
| 131 | * Binder that receives calls from the app. |
| 132 | */ |
| 133 | private final IContentCaptureDirectManager mClientInterface = |
| 134 | new IContentCaptureDirectManager.Stub() { |
| 135 | |
| 136 | @Override |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 137 | public void sendEvents(@SuppressWarnings("rawtypes") ParceledListSlice events) { |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 138 | mHandler.sendMessage(obtainMessage(ContentCaptureService::handleSendEvents, |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 139 | ContentCaptureService.this, Binder.getCallingUid(), events)); |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 140 | } |
| 141 | }; |
| 142 | |
| 143 | /** |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 144 | * UIDs associated with each session. |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 145 | * |
| 146 | * <p>This map is populated when an session is started, which is called by the system server |
| 147 | * and can be trusted. Then subsequent calls made by the app are verified against this map. |
| 148 | */ |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 149 | private final ArrayMap<String, Integer> mSessionUids = new ArrayMap<>(); |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 150 | |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 151 | @CallSuper |
| 152 | @Override |
| 153 | public void onCreate() { |
| 154 | super.onCreate(); |
| 155 | mHandler = new Handler(Looper.getMainLooper(), null, true); |
| 156 | } |
| 157 | |
| 158 | /** @hide */ |
| 159 | @Override |
| 160 | public final IBinder onBind(Intent intent) { |
| 161 | if (SERVICE_INTERFACE.equals(intent.getAction())) { |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 162 | return mServerInterface.asBinder(); |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 163 | } |
| 164 | Log.w(TAG, "Tried to bind to wrong intent (should be " + SERVICE_INTERFACE + ": " + intent); |
| 165 | return null; |
| 166 | } |
| 167 | |
| 168 | /** |
Felipe Leme | 7a3c9f5 | 2019-02-13 16:32:49 -0800 | [diff] [blame] | 169 | * @deprecated use {@link #setContentCaptureWhitelist(Set, Set)} instead |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 170 | */ |
Felipe Leme | 7a3c9f5 | 2019-02-13 16:32:49 -0800 | [diff] [blame] | 171 | @Deprecated |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 172 | public final void setContentCaptureWhitelist(@Nullable List<String> packages, |
| 173 | @Nullable List<ComponentName> activities) { |
Felipe Leme | af4e4fd | 2019-01-15 08:32:42 -0800 | [diff] [blame] | 174 | final IContentCaptureServiceCallback callback = mCallback; |
| 175 | if (callback == null) { |
| 176 | Log.w(TAG, "setContentCaptureWhitelist(): no server callback"); |
| 177 | return; |
| 178 | } |
| 179 | try { |
| 180 | callback.setContentCaptureWhitelist(packages, activities); |
| 181 | } catch (RemoteException e) { |
| 182 | e.rethrowFromSystemServer(); |
| 183 | } |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /** |
Felipe Leme | 7a3c9f5 | 2019-02-13 16:32:49 -0800 | [diff] [blame] | 187 | * Explicitly limits content capture to the given packages and activities. |
| 188 | * |
| 189 | * <p>To reset the whitelist, call it passing {@code null} to both arguments. |
| 190 | * |
| 191 | * <p>Useful when the service wants to restrict content capture to a category of apps, like |
| 192 | * chat apps. For example, if the service wants to support view captures on all activities of |
| 193 | * app {@code ChatApp1} and just activities {@code act1} and {@code act2} of {@code ChatApp2}, |
| 194 | * it would call: {@code setContentCaptureWhitelist(Sets.newArraySet("ChatApp1"), |
| 195 | * Sets.newArraySet(new ComponentName("ChatApp2", "act1"), |
| 196 | * new ComponentName("ChatApp2", "act2")));} |
| 197 | */ |
| 198 | public final void setContentCaptureWhitelist(@Nullable Set<String> packages, |
| 199 | @Nullable Set<ComponentName> activities) { |
| 200 | setContentCaptureWhitelist(toList(packages), toList(activities)); |
| 201 | } |
| 202 | |
| 203 | private <T> ArrayList<T> toList(@Nullable Set<T> set) { |
| 204 | return set == null ? null : new ArrayList<T>(set); |
| 205 | } |
| 206 | |
| 207 | /** |
Felipe Leme | bb98ed6 | 2018-12-21 09:29:27 -0800 | [diff] [blame] | 208 | * Called when the Android system connects to service. |
| 209 | * |
| 210 | * <p>You should generally do initialization here rather than in {@link #onCreate}. |
| 211 | */ |
| 212 | public void onConnected() { |
| 213 | Slog.i(TAG, "bound to " + getClass().getName()); |
| 214 | } |
| 215 | |
| 216 | /** |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 217 | * Creates a new content capture session. |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 218 | * |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 219 | * @param context content capture context |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 220 | * @param sessionId the session's Id |
| 221 | */ |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 222 | public void onCreateContentCaptureSession(@NonNull ContentCaptureContext context, |
| 223 | @NonNull ContentCaptureSessionId sessionId) { |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 224 | if (VERBOSE) { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 225 | Log.v(TAG, "onCreateContentCaptureSession(id=" + sessionId + ", ctx=" + context + ")"); |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | /** |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 230 | * |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 231 | * @deprecated use {@link #onContentCaptureEvent(ContentCaptureSessionId, ContentCaptureEvent)} |
| 232 | * instead. |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 233 | */ |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 234 | @Deprecated |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 235 | public void onContentCaptureEventsRequest(@NonNull ContentCaptureSessionId sessionId, |
| 236 | @NonNull ContentCaptureEventsRequest request) { |
| 237 | if (VERBOSE) Log.v(TAG, "onContentCaptureEventsRequest(id=" + sessionId + ")"); |
| 238 | } |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 239 | |
| 240 | /** |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 241 | * Notifies the service of {@link ContentCaptureEvent events} associated with a content capture |
| 242 | * session. |
| 243 | * |
| 244 | * @param sessionId the session's Id |
| 245 | * @param event the event |
| 246 | */ |
| 247 | public void onContentCaptureEvent(@NonNull ContentCaptureSessionId sessionId, |
| 248 | @NonNull ContentCaptureEvent event) { |
| 249 | if (VERBOSE) Log.v(TAG, "onContentCaptureEventsRequest(id=" + sessionId + ")"); |
| 250 | onContentCaptureEventsRequest(sessionId, new ContentCaptureEventsRequest(event)); |
| 251 | } |
Felipe Leme | 62e45b0 | 2019-01-10 10:22:00 -0800 | [diff] [blame] | 252 | |
| 253 | /** |
| 254 | * Notifies the service that the app requested to remove data associated with the user. |
| 255 | * |
| 256 | * @param request the user data requested to be removed |
| 257 | */ |
| 258 | public void onUserDataRemovalRequest(@NonNull UserDataRemovalRequest request) { |
| 259 | if (VERBOSE) Log.v(TAG, "onUserDataRemovalRequest()"); |
| 260 | } |
| 261 | |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 262 | /** |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 263 | * Notifies the service of {@link SnapshotData snapshot data} associated with a session. |
| 264 | * |
| 265 | * @param sessionId the session's Id |
| 266 | * @param snapshotData the data |
| 267 | */ |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 268 | public void onActivitySnapshot(@NonNull ContentCaptureSessionId sessionId, |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 269 | @NonNull SnapshotData snapshotData) {} |
| 270 | |
| 271 | /** |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 272 | * Destroys the content capture session. |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 273 | * |
| 274 | * @param sessionId the id of the session to destroy |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 275 | * */ |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 276 | public void onDestroyContentCaptureSession(@NonNull ContentCaptureSessionId sessionId) { |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 277 | if (VERBOSE) Log.v(TAG, "onDestroyContentCaptureSession(id=" + sessionId + ")"); |
| 278 | } |
| 279 | |
Felipe Leme | bb98ed6 | 2018-12-21 09:29:27 -0800 | [diff] [blame] | 280 | /** |
| 281 | * Called when the Android system disconnects from the service. |
| 282 | * |
| 283 | * <p> At this point this service may no longer be an active {@link AutofillService}. |
| 284 | */ |
| 285 | public void onDisconnected() { |
| 286 | Slog.i(TAG, "unbinding from " + getClass().getName()); |
| 287 | } |
| 288 | |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 289 | @Override |
| 290 | @CallSuper |
| 291 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 292 | final int size = mSessionUids.size(); |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 293 | pw.print("Number sessions: "); pw.println(size); |
| 294 | if (size > 0) { |
| 295 | final String prefix = " "; |
| 296 | for (int i = 0; i < size; i++) { |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 297 | pw.print(prefix); pw.print(mSessionUids.keyAt(i)); |
| 298 | pw.print(": uid="); pw.println(mSessionUids.valueAt(i)); |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 299 | } |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
Felipe Leme | af4e4fd | 2019-01-15 08:32:42 -0800 | [diff] [blame] | 303 | private void handleOnConnected(@NonNull IBinder callback) { |
| 304 | mCallback = IContentCaptureServiceCallback.Stub.asInterface(callback); |
| 305 | onConnected(); |
| 306 | } |
| 307 | |
| 308 | private void handleOnDisconnected() { |
| 309 | onDisconnected(); |
| 310 | mCallback = null; |
Felipe Leme | bb98ed6 | 2018-12-21 09:29:27 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 313 | //TODO(b/111276913): consider caching the InteractionSessionId for the lifetime of the session, |
| 314 | // so we don't need to create a temporary InteractionSessionId for each event. |
| 315 | |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 316 | private void handleOnCreateSession(@NonNull ContentCaptureContext context, |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 317 | @NonNull String sessionId, int uid, IResultReceiver clientReceiver) { |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 318 | mSessionUids.put(sessionId, uid); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 319 | onCreateContentCaptureSession(context, new ContentCaptureSessionId(sessionId)); |
Adam He | 328c0e3 | 2019-01-03 15:19:22 -0800 | [diff] [blame] | 320 | |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 321 | final int clientFlags = context.getFlags(); |
| 322 | int stateFlags = 0; |
| 323 | if ((clientFlags & ContentCaptureContext.FLAG_DISABLED_BY_FLAG_SECURE) != 0) { |
| 324 | stateFlags |= ContentCaptureSession.STATE_FLAG_SECURE; |
Adam He | 328c0e3 | 2019-01-03 15:19:22 -0800 | [diff] [blame] | 325 | } |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 326 | if ((clientFlags & ContentCaptureContext.FLAG_DISABLED_BY_APP) != 0) { |
| 327 | stateFlags |= ContentCaptureSession.STATE_BY_APP; |
Adam He | 6079d15 | 2019-01-10 11:37:17 -0800 | [diff] [blame] | 328 | } |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 329 | if (stateFlags == 0) { |
| 330 | stateFlags = ContentCaptureSession.STATE_ACTIVE; |
| 331 | } else { |
| 332 | stateFlags |= ContentCaptureSession.STATE_DISABLED; |
Adam He | 328c0e3 | 2019-01-03 15:19:22 -0800 | [diff] [blame] | 333 | |
Felipe Leme | 01b8749 | 2019-01-15 13:26:52 -0800 | [diff] [blame] | 334 | } |
| 335 | setClientState(clientReceiver, stateFlags, mClientInterface.asBinder()); |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 338 | private void handleSendEvents(int uid, |
| 339 | @NonNull ParceledListSlice<ContentCaptureEvent> parceledEvents) { |
| 340 | |
| 341 | // Most events belong to the same session, so we can keep a reference to the last one |
| 342 | // to avoid creating too many ContentCaptureSessionId objects |
| 343 | String lastSessionId = null; |
| 344 | ContentCaptureSessionId sessionId = null; |
| 345 | |
| 346 | final List<ContentCaptureEvent> events = parceledEvents.getList(); |
| 347 | for (int i = 0; i < events.size(); i++) { |
| 348 | final ContentCaptureEvent event = events.get(i); |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 349 | if (!handleIsRightCallerFor(event, uid)) continue; |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 350 | String sessionIdString = event.getSessionId(); |
| 351 | if (!sessionIdString.equals(lastSessionId)) { |
Felipe Leme | fc24bea | 2018-12-18 13:19:01 -0800 | [diff] [blame] | 352 | sessionId = new ContentCaptureSessionId(sessionIdString); |
| 353 | lastSessionId = sessionIdString; |
| 354 | } |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 355 | switch (event.getType()) { |
| 356 | case ContentCaptureEvent.TYPE_SESSION_STARTED: |
Felipe Leme | 4eecbe6 | 2019-02-11 17:50:17 -0800 | [diff] [blame] | 357 | final ContentCaptureContext clientContext = event.getContentCaptureContext(); |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 358 | clientContext.setParentSessionId(event.getParentSessionId()); |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 359 | mSessionUids.put(sessionIdString, uid); |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 360 | onCreateContentCaptureSession(clientContext, sessionId); |
| 361 | break; |
| 362 | case ContentCaptureEvent.TYPE_SESSION_FINISHED: |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 363 | mSessionUids.remove(sessionIdString); |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 364 | onDestroyContentCaptureSession(sessionId); |
| 365 | break; |
| 366 | default: |
| 367 | onContentCaptureEvent(sessionId, event); |
| 368 | } |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 369 | } |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | private void handleOnActivitySnapshot(@NonNull String sessionId, |
| 373 | @NonNull SnapshotData snapshotData) { |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 374 | onActivitySnapshot(new ContentCaptureSessionId(sessionId), snapshotData); |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 375 | } |
| 376 | |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 377 | private void handleFinishSession(@NonNull String sessionId) { |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 378 | mSessionUids.remove(sessionId); |
Felipe Leme | aa5088e | 2018-12-10 14:53:58 -0800 | [diff] [blame] | 379 | onDestroyContentCaptureSession(new ContentCaptureSessionId(sessionId)); |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 380 | } |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 381 | |
Adam He | 3d0409b | 2019-01-15 14:22:04 -0800 | [diff] [blame] | 382 | private void handleOnUserDataRemovalRequest(@NonNull UserDataRemovalRequest request) { |
| 383 | onUserDataRemovalRequest(request); |
| 384 | } |
| 385 | |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 386 | /** |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 387 | * Checks if the given {@code uid} owns the session associated with the event. |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 388 | */ |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 389 | private boolean handleIsRightCallerFor(@NonNull ContentCaptureEvent event, int uid) { |
| 390 | final String sessionId; |
| 391 | switch (event.getType()) { |
| 392 | case ContentCaptureEvent.TYPE_SESSION_STARTED: |
| 393 | case ContentCaptureEvent.TYPE_SESSION_FINISHED: |
| 394 | sessionId = event.getParentSessionId(); |
| 395 | break; |
| 396 | default: |
| 397 | sessionId = event.getSessionId(); |
| 398 | } |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 399 | final Integer rightUid = mSessionUids.get(sessionId); |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 400 | if (rightUid == null) { |
Felipe Leme | 4eecbe6 | 2019-02-11 17:50:17 -0800 | [diff] [blame] | 401 | if (VERBOSE) { |
| 402 | Log.v(TAG, "handleIsRightCallerFor(" + event + "): no session for " + sessionId |
Felipe Leme | e127c9a | 2019-01-04 14:56:38 -0800 | [diff] [blame] | 403 | + ": " + mSessionUids); |
| 404 | } |
| 405 | // Just ignore, as the session could have been finished already |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 406 | return false; |
| 407 | } |
| 408 | if (rightUid != uid) { |
| 409 | Log.e(TAG, "invalid call from UID " + uid + ": session " + sessionId + " belongs to " |
| 410 | + rightUid); |
| 411 | //TODO(b/111276913): log metrics as this could be a malicious app forging a sessionId |
| 412 | return false; |
| 413 | } |
| 414 | return true; |
| 415 | |
| 416 | } |
| 417 | |
| 418 | /** |
Adam He | 328c0e3 | 2019-01-03 15:19:22 -0800 | [diff] [blame] | 419 | * Sends the state of the {@link ContentCaptureManager} in the client app. |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 420 | * |
| 421 | * @param clientReceiver receiver in the client app. |
Adam He | 328c0e3 | 2019-01-03 15:19:22 -0800 | [diff] [blame] | 422 | * @param sessionState state of the session |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 423 | * @param binder handle to the {@code IContentCaptureDirectManager} object that resides in the |
| 424 | * service. |
| 425 | * @hide |
| 426 | */ |
| 427 | public static void setClientState(@NonNull IResultReceiver clientReceiver, |
Adam He | 328c0e3 | 2019-01-03 15:19:22 -0800 | [diff] [blame] | 428 | int sessionState, @Nullable IBinder binder) { |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 429 | try { |
| 430 | final Bundle extras; |
| 431 | if (binder != null) { |
| 432 | extras = new Bundle(); |
Felipe Leme | 87a9dc9 | 2018-12-18 14:28:07 -0800 | [diff] [blame] | 433 | extras.putBinder(MainContentCaptureSession.EXTRA_BINDER, binder); |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 434 | } else { |
| 435 | extras = null; |
| 436 | } |
Adam He | 328c0e3 | 2019-01-03 15:19:22 -0800 | [diff] [blame] | 437 | clientReceiver.send(sessionState, extras); |
Felipe Leme | b9687849 | 2018-12-17 12:22:29 -0800 | [diff] [blame] | 438 | } catch (RemoteException e) { |
| 439 | Slog.w(TAG, "Error async reporting result to client: " + e); |
| 440 | } |
| 441 | } |
Felipe Leme | 749b889 | 2018-12-03 16:30:30 -0800 | [diff] [blame] | 442 | } |