1 /*
2  * Copyright (C) 2021 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.net.module.util;
18 
19 import android.annotation.NonNull;
20 import android.os.Binder;
21 
22 import java.util.function.Supplier;
23 
24 /**
25  * Collection of utilities for {@link Binder} and related classes.
26  * @hide
27  */
28 public class BinderUtils {
29     /**
30      * Convenience method for running the provided action enclosed in
31      * {@link Binder#clearCallingIdentity}/{@link Binder#restoreCallingIdentity}
32      *
33      * Any exception thrown by the given action will be caught and rethrown after the call to
34      * {@link Binder#restoreCallingIdentity}
35      *
36      * Note that this is copied from Binder#withCleanCallingIdentity with minor changes
37      * since it is not public.
38      *
39      * @hide
40      */
withCleanCallingIdentity( @onNull ThrowingRunnable<T> action)41     public static final <T extends Exception> void withCleanCallingIdentity(
42             @NonNull ThrowingRunnable<T> action) throws T {
43         final long callingIdentity = Binder.clearCallingIdentity();
44         try {
45             action.run();
46         } finally {
47             Binder.restoreCallingIdentity(callingIdentity);
48         }
49     }
50 
51     /**
52      * Like a Runnable, but declared to throw an exception.
53      *
54      * @param <T> The exception class which is declared to be thrown.
55      */
56     @FunctionalInterface
57     public interface ThrowingRunnable<T extends Exception> {
58         /** @see java.lang.Runnable */
run()59         void run() throws T;
60     }
61 
62     /**
63      * Convenience method for running the provided action enclosed in
64      * {@link Binder#clearCallingIdentity}/{@link Binder#restoreCallingIdentity} returning the
65      * result.
66      *
67      * <p>Any exception thrown by the given action will be caught and rethrown after
68      * the call to {@link Binder#restoreCallingIdentity}.
69      *
70      * Note that this is copied from Binder#withCleanCallingIdentity with minor changes
71      * since it is not public.
72      *
73      * @hide
74      */
withCleanCallingIdentity( @onNull ThrowingSupplier<T, E> action)75     public static final <T, E extends Exception> T withCleanCallingIdentity(
76             @NonNull ThrowingSupplier<T, E> action) throws E {
77         final long callingIdentity = Binder.clearCallingIdentity();
78         try {
79             return action.get();
80         } finally {
81             Binder.restoreCallingIdentity(callingIdentity);
82         }
83     }
84 
85     /**
86      * An equivalent of {@link Supplier}
87      *
88      * @param <T> The class which is declared to be returned.
89      * @param <E> The exception class which is declared to be thrown.
90      */
91     @FunctionalInterface
92     public interface ThrowingSupplier<T, E extends Exception> {
93         /** @see java.util.function.Supplier */
get()94         T get() throws E;
95     }
96 }
97