1 /* 2 * Copyright 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.timezonedetector; 18 19 import android.annotation.UserIdInt; 20 import android.app.ActivityManager; 21 import android.os.Binder; 22 import android.os.UserHandle; 23 24 /** 25 * An interface to wrap various difficult-to-intercept calls that services make to access / manage 26 * caller identity, e.g. {@link Binder#clearCallingIdentity()}. 27 */ 28 public interface CallerIdentityInjector { 29 30 /** A singleton for the real implementation of {@link CallerIdentityInjector}. */ 31 CallerIdentityInjector REAL = new Real(); 32 33 /** 34 * A {@link ActivityManager#handleIncomingUser} call. This can be used to map the abstract 35 * user ID value USER_CURRENT to the actual user ID. 36 */ resolveUserId(@serIdInt int userId, String debugInfo)37 @UserIdInt int resolveUserId(@UserIdInt int userId, String debugInfo); 38 39 /** A {@link UserHandle#getCallingUserId()} call. */ getCallingUserId()40 @UserIdInt int getCallingUserId(); 41 42 /** A {@link Binder#clearCallingIdentity()} call. */ clearCallingIdentity()43 long clearCallingIdentity(); 44 45 /** A {@link Binder#restoreCallingIdentity(long)} ()} call. */ restoreCallingIdentity(long token)46 void restoreCallingIdentity(long token); 47 48 /** The real implementation of {@link CallerIdentityInjector}. */ 49 class Real implements CallerIdentityInjector { 50 Real()51 protected Real() { 52 } 53 54 @Override resolveUserId(@serIdInt int userId, String debugName)55 public int resolveUserId(@UserIdInt int userId, String debugName) { 56 return ActivityManager.handleIncomingUser(Binder.getCallingPid(), 57 Binder.getCallingUid(), userId, false, false, debugName, null); 58 } 59 60 @Override getCallingUserId()61 public int getCallingUserId() { 62 return UserHandle.getCallingUserId(); 63 } 64 65 @Override clearCallingIdentity()66 public long clearCallingIdentity() { 67 return Binder.clearCallingIdentity(); 68 } 69 70 @Override restoreCallingIdentity(long token)71 public void restoreCallingIdentity(long token) { 72 Binder.restoreCallingIdentity(token); 73 } 74 } 75 } 76