blob: f336fdab5941cec33a9275520f5eccc6223ddc1d [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 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
17package android.os;
18
Dianne Hackborn4cd650c2017-07-31 17:38:53 -070019import android.annotation.NonNull;
20import android.annotation.Nullable;
Artur Satayevafdb23a2019-12-10 17:47:53 +000021import android.compat.annotation.UnsupportedAppUsage;
Dianne Hackborn4cd650c2017-07-31 17:38:53 -070022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25/**
26 * Base interface for a remotable object, the core part of a lightweight
27 * remote procedure call mechanism designed for high performance when
28 * performing in-process and cross-process calls. This
29 * interface describes the abstract protocol for interacting with a
30 * remotable object. Do not implement this interface directly, instead
31 * extend from {@link Binder}.
32 *
33 * <p>The key IBinder API is {@link #transact transact()} matched by
34 * {@link Binder#onTransact Binder.onTransact()}. These
35 * methods allow you to send a call to an IBinder object and receive a
36 * call coming in to a Binder object, respectively. This transaction API
37 * is synchronous, such that a call to {@link #transact transact()} does not
38 * return until the target has returned from
39 * {@link Binder#onTransact Binder.onTransact()}; this is the
40 * expected behavior when calling an object that exists in the local
41 * process, and the underlying inter-process communication (IPC) mechanism
42 * ensures that these same semantics apply when going across processes.
43 *
44 * <p>The data sent through transact() is a {@link Parcel}, a generic buffer
45 * of data that also maintains some meta-data about its contents. The meta
46 * data is used to manage IBinder object references in the buffer, so that those
47 * references can be maintained as the buffer moves across processes. This
48 * mechanism ensures that when an IBinder is written into a Parcel and sent to
49 * another process, if that other process sends a reference to that same IBinder
50 * back to the original process, then the original process will receive the
51 * same IBinder object back. These semantics allow IBinder/Binder objects to
52 * be used as a unique identity (to serve as a token or for other purposes)
53 * that can be managed across processes.
54 *
55 * <p>The system maintains a pool of transaction threads in each process that
56 * it runs in. These threads are used to dispatch all
57 * IPCs coming in from other processes. For example, when an IPC is made from
58 * process A to process B, the calling thread in A blocks in transact() as
59 * it sends the transaction to process B. The next available pool thread in
60 * B receives the incoming transaction, calls Binder.onTransact() on the target
61 * object, and replies with the result Parcel. Upon receiving its result, the
62 * thread in process A returns to allow its execution to continue. In effect,
63 * other processes appear to use as additional threads that you did not create
64 * executing in your own process.
65 *
66 * <p>The Binder system also supports recursion across processes. For example
67 * if process A performs a transaction to process B, and process B while
68 * handling that transaction calls transact() on an IBinder that is implemented
69 * in A, then the thread in A that is currently waiting for the original
70 * transaction to finish will take care of calling Binder.onTransact() on the
71 * object being called by B. This ensures that the recursion semantics when
72 * calling remote binder object are the same as when calling local objects.
73 *
74 * <p>When working with remote objects, you often want to find out when they
75 * are no longer valid. There are three ways this can be determined:
76 * <ul>
77 * <li> The {@link #transact transact()} method will throw a
78 * {@link RemoteException} exception if you try to call it on an IBinder
79 * whose process no longer exists.
80 * <li> The {@link #pingBinder()} method can be called, and will return false
81 * if the remote process no longer exists.
82 * <li> The {@link #linkToDeath linkToDeath()} method can be used to register
83 * a {@link DeathRecipient} with the IBinder, which will be called when its
84 * containing process goes away.
85 * </ul>
86 *
87 * @see Binder
88 */
89public interface IBinder {
90 /**
91 * The first transaction code available for user commands.
92 */
93 int FIRST_CALL_TRANSACTION = 0x00000001;
94 /**
95 * The last transaction code available for user commands.
96 */
97 int LAST_CALL_TRANSACTION = 0x00ffffff;
98
99 /**
100 * IBinder protocol transaction code: pingBinder().
101 */
102 int PING_TRANSACTION = ('_'<<24)|('P'<<16)|('N'<<8)|'G';
103
104 /**
105 * IBinder protocol transaction code: dump internal state.
106 */
107 int DUMP_TRANSACTION = ('_'<<24)|('D'<<16)|('M'<<8)|'P';
108
109 /**
Dianne Hackborn9461b6f2015-10-07 17:33:16 -0700110 * IBinder protocol transaction code: execute a shell command.
111 * @hide
112 */
113 int SHELL_COMMAND_TRANSACTION = ('_'<<24)|('C'<<16)|('M'<<8)|'D';
114
115 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 * IBinder protocol transaction code: interrogate the recipient side
117 * of the transaction for its canonical interface descriptor.
118 */
119 int INTERFACE_TRANSACTION = ('_'<<24)|('N'<<16)|('T'<<8)|'F';
120
121 /**
Dianne Hackborn0d4b9c92011-06-15 11:41:48 -0700122 * IBinder protocol transaction code: send a tweet to the target
123 * object. The data in the parcel is intended to be delivered to
124 * a shared messaging service associated with the object; it can be
125 * anything, as long as it is not more than 130 UTF-8 characters to
126 * conservatively fit within common messaging services. As part of
127 * {@link Build.VERSION_CODES#HONEYCOMB_MR2}, all Binder objects are
128 * expected to support this protocol for fully integrated tweeting
129 * across the platform. To support older code, the default implementation
130 * logs the tweet to the main log as a simple emulation of broadcasting
131 * it publicly over the Internet.
132 *
133 * <p>Also, upon completing the dispatch, the object must make a cup
134 * of tea, return it to the caller, and exclaim "jolly good message
135 * old boy!".
136 */
137 int TWEET_TRANSACTION = ('_'<<24)|('T'<<16)|('W'<<8)|'T';
138
139 /**
Dianne Hackborna3d487c2011-12-01 18:42:24 -0800140 * IBinder protocol transaction code: tell an app asynchronously that the
141 * caller likes it. The app is responsible for incrementing and maintaining
142 * its own like counter, and may display this value to the user to indicate the
143 * quality of the app. This is an optional command that applications do not
144 * need to handle, so the default implementation is to do nothing.
145 *
146 * <p>There is no response returned and nothing about the
147 * system will be functionally affected by it, but it will improve the
148 * app's self-esteem.
149 */
150 int LIKE_TRANSACTION = ('_'<<24)|('L'<<16)|('I'<<8)|'K';
151
Dianne Hackborna53de062012-05-08 18:53:51 -0700152 /** @hide */
Andrei Onea24ec3212019-03-15 17:35:05 +0000153 @UnsupportedAppUsage
Dianne Hackborna53de062012-05-08 18:53:51 -0700154 int SYSPROPS_TRANSACTION = ('_'<<24)|('S'<<16)|('P'<<8)|'R';
155
Dianne Hackborna3d487c2011-12-01 18:42:24 -0800156 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 * Flag to {@link #transact}: this is a one-way call, meaning that the
158 * caller returns immediately, without waiting for a result from the
Dirk Doughertyd76ef972010-03-29 14:20:41 -0700159 * callee. Applies only if the caller and callee are in different
160 * processes.
Dianne Hackborn6f12f222017-07-31 17:02:51 -0700161 *
162 * <p>The system provides special ordering semantics for multiple oneway calls
163 * being made to the same IBinder object: these calls will be dispatched in the
164 * other process one at a time, with the same order as the original calls. These
165 * are still dispatched by the IPC thread pool, so may execute on different threads,
166 * but the next one will not be dispatched until the previous one completes. This
167 * ordering is not guaranteed for calls on different IBinder objects or when mixing
168 * oneway and non-oneway calls on the same IBinder object.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 */
170 int FLAG_ONEWAY = 0x00000001;
Dianne Hackborn86cbc2b2015-07-28 18:07:13 -0700171
172 /**
Philip P. Moltmann2b08aaf2019-06-10 08:49:11 -0700173 * @hide
174 */
175 int FLAG_COLLECT_NOTED_APP_OPS = 0x00000002;
176
177 /**
Dianne Hackborn86cbc2b2015-07-28 18:07:13 -0700178 * Limit that should be placed on IPC sizes to keep them safely under the
179 * transaction buffer limit.
180 * @hide
181 */
182 public static final int MAX_IPC_SIZE = 64 * 1024;
183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 /**
David Su87e22202019-12-16 15:43:47 -0800185 * Limit that should be placed on IPC sizes, in bytes, to keep them safely under the transaction
186 * buffer limit.
187 */
188 static int getSuggestedMaxIpcSizeBytes() {
189 return MAX_IPC_SIZE;
190 }
191
192 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * Get the canonical name of the interface supported by this binder.
194 */
Dianne Hackborn4cd650c2017-07-31 17:38:53 -0700195 public @Nullable String getInterfaceDescriptor() throws RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196
197 /**
198 * Check to see if the object still exists.
199 *
200 * @return Returns false if the
201 * hosting process is gone, otherwise the result (always by default
202 * true) returned by the pingBinder() implementation on the other
203 * side.
204 */
205 public boolean pingBinder();
206
207 /**
208 * Check to see if the process that the binder is in is still alive.
209 *
210 * @return false if the process is not alive. Note that if it returns
211 * true, the process may have died while the call is returning.
212 */
213 public boolean isBinderAlive();
214
215 /**
216 * Attempt to retrieve a local implementation of an interface
217 * for this Binder object. If null is returned, you will need
218 * to instantiate a proxy class to marshall calls through
219 * the transact() method.
220 */
Dianne Hackborn4cd650c2017-07-31 17:38:53 -0700221 public @Nullable IInterface queryLocalInterface(@NonNull String descriptor);
Dianne Hackborn9461b6f2015-10-07 17:33:16 -0700222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223 /**
224 * Print the object's state into the given stream.
225 *
226 * @param fd The raw file descriptor that the dump is being sent to.
227 * @param args additional arguments to the dump request.
228 */
Dianne Hackborn4cd650c2017-07-31 17:38:53 -0700229 public void dump(@NonNull FileDescriptor fd, @Nullable String[] args) throws RemoteException;
Dianne Hackborn9461b6f2015-10-07 17:33:16 -0700230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 /**
Dianne Hackborn9a849832011-04-07 15:11:57 -0700232 * Like {@link #dump(FileDescriptor, String[])} but always executes
233 * asynchronously. If the object is local, a new thread is created
234 * to perform the dump.
235 *
236 * @param fd The raw file descriptor that the dump is being sent to.
237 * @param args additional arguments to the dump request.
238 */
Dianne Hackborn4cd650c2017-07-31 17:38:53 -0700239 public void dumpAsync(@NonNull FileDescriptor fd, @Nullable String[] args)
240 throws RemoteException;
Dianne Hackborn9a849832011-04-07 15:11:57 -0700241
242 /**
Dianne Hackborn9461b6f2015-10-07 17:33:16 -0700243 * Execute a shell command on this object. This may be performed asynchrously from the caller;
244 * the implementation must always call resultReceiver when finished.
245 *
246 * @param in The raw file descriptor that an input data stream can be read from.
247 * @param out The raw file descriptor that normal command messages should be written to.
248 * @param err The raw file descriptor that command error messages should be written to.
249 * @param args Command-line arguments.
Dianne Hackborn354736e2016-08-22 17:00:05 -0700250 * @param shellCallback Optional callback to the caller's shell to perform operations in it.
Dianne Hackborn9461b6f2015-10-07 17:33:16 -0700251 * @param resultReceiver Called when the command has finished executing, with the result code.
252 * @hide
253 */
Dianne Hackborn4cd650c2017-07-31 17:38:53 -0700254 public void shellCommand(@Nullable FileDescriptor in, @Nullable FileDescriptor out,
255 @Nullable FileDescriptor err,
256 @NonNull String[] args, @Nullable ShellCallback shellCallback,
257 @NonNull ResultReceiver resultReceiver) throws RemoteException;
Dianne Hackborn9461b6f2015-10-07 17:33:16 -0700258
259 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 * Perform a generic operation with the object.
261 *
262 * @param code The action to perform. This should
263 * be a number between {@link #FIRST_CALL_TRANSACTION} and
264 * {@link #LAST_CALL_TRANSACTION}.
Glenn Kasten334031c2010-11-09 21:54:38 -0800265 * @param data Marshalled data to send to the target. Must not be null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 * If you are not sending any data, you must create an empty Parcel
267 * that is given here.
268 * @param reply Marshalled data to be received from the target. May be
269 * null if you are not interested in the return value.
270 * @param flags Additional operation flags. Either 0 for a normal
271 * RPC, or {@link #FLAG_ONEWAY} for a one-way RPC.
Dianne Hackborn4cd650c2017-07-31 17:38:53 -0700272 *
273 * @return Returns the result from {@link Binder#onTransact}. A successful call
274 * generally returns true; false generally means the transaction code was not
275 * understood.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 */
Dianne Hackborn4cd650c2017-07-31 17:38:53 -0700277 public boolean transact(int code, @NonNull Parcel data, @Nullable Parcel reply, int flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 throws RemoteException;
279
280 /**
281 * Interface for receiving a callback when the process hosting an IBinder
282 * has gone away.
283 *
284 * @see #linkToDeath
285 */
286 public interface DeathRecipient {
287 public void binderDied();
288 }
289
290 /**
291 * Register the recipient for a notification if this binder
292 * goes away. If this binder object unexpectedly goes away
293 * (typically because its hosting process has been killed),
294 * then the given {@link DeathRecipient}'s
295 * {@link DeathRecipient#binderDied DeathRecipient.binderDied()} method
296 * will be called.
297 *
298 * <p>You will only receive death notifications for remote binders,
299 * as local binders by definition can't die without you dying as well.
300 *
John Spurlock6098c5d2013-06-17 10:32:46 -0400301 * @throws RemoteException if the target IBinder's
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 * process has already died.
303 *
304 * @see #unlinkToDeath
305 */
Dianne Hackborn4cd650c2017-07-31 17:38:53 -0700306 public void linkToDeath(@NonNull DeathRecipient recipient, int flags)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 throws RemoteException;
308
309 /**
310 * Remove a previously registered death notification.
311 * The recipient will no longer be called if this object
312 * dies.
313 *
John Spurlock6098c5d2013-06-17 10:32:46 -0400314 * @return {@code true} if the <var>recipient</var> is successfully
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 * unlinked, assuring you that its
316 * {@link DeathRecipient#binderDied DeathRecipient.binderDied()} method
John Spurlock6098c5d2013-06-17 10:32:46 -0400317 * will not be called; {@code false} if the target IBinder has already
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 * died, meaning the method has been (or soon will be) called.
319 *
John Spurlock6098c5d2013-06-17 10:32:46 -0400320 * @throws java.util.NoSuchElementException if the given
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 * <var>recipient</var> has not been registered with the IBinder, and
322 * the IBinder is still alive. Note that if the <var>recipient</var>
323 * was never registered, but the IBinder has already died, then this
324 * exception will <em>not</em> be thrown, and you will receive a false
325 * return value instead.
326 */
Dianne Hackborn4cd650c2017-07-31 17:38:53 -0700327 public boolean unlinkToDeath(@NonNull DeathRecipient recipient, int flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328}