1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.location.listeners; 18 19 20 import static com.android.internal.util.ConcurrentUtils.DIRECT_EXECUTOR; 21 22 import android.annotation.Nullable; 23 import android.location.util.identity.CallerIdentity; 24 import android.os.Process; 25 26 import com.android.internal.annotations.VisibleForTesting; 27 import com.android.server.FgThread; 28 29 import java.util.Objects; 30 import java.util.concurrent.Executor; 31 32 /** 33 * A listener registration representing a remote (possibly from a different process) listener. 34 * Listeners from a different process will be run on a direct executor, since the x-process listener 35 * invocation should already be asynchronous. Listeners from the same process will be run on a 36 * normal executor, since in-process listener invocation may be synchronous. 37 * 38 * @param <TRequest> request type 39 * @param <TListener> listener type 40 */ 41 public abstract class RemoteListenerRegistration<TRequest, TListener> extends 42 RemovableListenerRegistration<TRequest, TListener> { 43 44 @VisibleForTesting 45 public static final Executor IN_PROCESS_EXECUTOR = FgThread.getExecutor(); 46 chooseExecutor(CallerIdentity identity)47 private static Executor chooseExecutor(CallerIdentity identity) { 48 // if a client is in the same process as us, binder calls will execute synchronously and 49 // we shouldn't run callbacks directly since they might be run under lock and deadlock 50 if (identity.getPid() == Process.myPid()) { 51 // there's a slight loophole here for pending intents - pending intent callbacks can 52 // always be run on the direct executor since they're always asynchronous, but honestly 53 // you shouldn't be using pending intent callbacks within the same process anyways 54 return IN_PROCESS_EXECUTOR; 55 } else { 56 return DIRECT_EXECUTOR; 57 } 58 } 59 60 private final CallerIdentity mIdentity; 61 RemoteListenerRegistration(@ullable TRequest request, CallerIdentity identity, TListener listener)62 protected RemoteListenerRegistration(@Nullable TRequest request, CallerIdentity identity, 63 TListener listener) { 64 super(chooseExecutor(identity), request, listener); 65 mIdentity = Objects.requireNonNull(identity); 66 } 67 68 /** 69 * Returns the listener identity. 70 */ getIdentity()71 public final CallerIdentity getIdentity() { 72 return mIdentity; 73 } 74 } 75 76