IBinder.java revision a3d487cb13983643d5024e493e4d56c96736f1eb
1/*
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
19import java.io.FileDescriptor;
20import java.io.PrintWriter;
21
22/**
23 * Base interface for a remotable object, the core part of a lightweight
24 * remote procedure call mechanism designed for high performance when
25 * performing in-process and cross-process calls.  This
26 * interface describes the abstract protocol for interacting with a
27 * remotable object.  Do not implement this interface directly, instead
28 * extend from {@link Binder}.
29 *
30 * <p>The key IBinder API is {@link #transact transact()} matched by
31 * {@link Binder#onTransact Binder.onTransact()}.  These
32 * methods allow you to send a call to an IBinder object and receive a
33 * call coming in to a Binder object, respectively.  This transaction API
34 * is synchronous, such that a call to {@link #transact transact()} does not
35 * return until the target has returned from
36 * {@link Binder#onTransact Binder.onTransact()}; this is the
37 * expected behavior when calling an object that exists in the local
38 * process, and the underlying inter-process communication (IPC) mechanism
39 * ensures that these same semantics apply when going across processes.
40 *
41 * <p>The data sent through transact() is a {@link Parcel}, a generic buffer
42 * of data that also maintains some meta-data about its contents.  The meta
43 * data is used to manage IBinder object references in the buffer, so that those
44 * references can be maintained as the buffer moves across processes.  This
45 * mechanism ensures that when an IBinder is written into a Parcel and sent to
46 * another process, if that other process sends a reference to that same IBinder
47 * back to the original process, then the original process will receive the
48 * same IBinder object back.  These semantics allow IBinder/Binder objects to
49 * be used as a unique identity (to serve as a token or for other purposes)
50 * that can be managed across processes.
51 *
52 * <p>The system maintains a pool of transaction threads in each process that
53 * it runs in.  These threads are used to dispatch all
54 * IPCs coming in from other processes.  For example, when an IPC is made from
55 * process A to process B, the calling thread in A blocks in transact() as
56 * it sends the transaction to process B.  The next available pool thread in
57 * B receives the incoming transaction, calls Binder.onTransact() on the target
58 * object, and replies with the result Parcel.  Upon receiving its result, the
59 * thread in process A returns to allow its execution to continue.  In effect,
60 * other processes appear to use as additional threads that you did not create
61 * executing in your own process.
62 *
63 * <p>The Binder system also supports recursion across processes.  For example
64 * if process A performs a transaction to process B, and process B while
65 * handling that transaction calls transact() on an IBinder that is implemented
66 * in A, then the thread in A that is currently waiting for the original
67 * transaction to finish will take care of calling Binder.onTransact() on the
68 * object being called by B.  This ensures that the recursion semantics when
69 * calling remote binder object are the same as when calling local objects.
70 *
71 * <p>When working with remote objects, you often want to find out when they
72 * are no longer valid.  There are three ways this can be determined:
73 * <ul>
74 * <li> The {@link #transact transact()} method will throw a
75 * {@link RemoteException} exception if you try to call it on an IBinder
76 * whose process no longer exists.
77 * <li> The {@link #pingBinder()} method can be called, and will return false
78 * if the remote process no longer exists.
79 * <li> The {@link #linkToDeath linkToDeath()} method can be used to register
80 * a {@link DeathRecipient} with the IBinder, which will be called when its
81 * containing process goes away.
82 * </ul>
83 *
84 * @see Binder
85 */
86public interface IBinder {
87    /**
88     * The first transaction code available for user commands.
89     */
90    int FIRST_CALL_TRANSACTION  = 0x00000001;
91    /**
92     * The last transaction code available for user commands.
93     */
94    int LAST_CALL_TRANSACTION   = 0x00ffffff;
95
96    /**
97     * IBinder protocol transaction code: pingBinder().
98     */
99    int PING_TRANSACTION        = ('_'<<24)|('P'<<16)|('N'<<8)|'G';
100
101    /**
102     * IBinder protocol transaction code: dump internal state.
103     */
104    int DUMP_TRANSACTION        = ('_'<<24)|('D'<<16)|('M'<<8)|'P';
105
106    /**
107     * IBinder protocol transaction code: interrogate the recipient side
108     * of the transaction for its canonical interface descriptor.
109     */
110    int INTERFACE_TRANSACTION   = ('_'<<24)|('N'<<16)|('T'<<8)|'F';
111
112    /**
113     * IBinder protocol transaction code: send a tweet to the target
114     * object.  The data in the parcel is intended to be delivered to
115     * a shared messaging service associated with the object; it can be
116     * anything, as long as it is not more than 130 UTF-8 characters to
117     * conservatively fit within common messaging services.  As part of
118     * {@link Build.VERSION_CODES#HONEYCOMB_MR2}, all Binder objects are
119     * expected to support this protocol for fully integrated tweeting
120     * across the platform.  To support older code, the default implementation
121     * logs the tweet to the main log as a simple emulation of broadcasting
122     * it publicly over the Internet.
123     *
124     * <p>Also, upon completing the dispatch, the object must make a cup
125     * of tea, return it to the caller, and exclaim "jolly good message
126     * old boy!".
127     */
128    int TWEET_TRANSACTION   = ('_'<<24)|('T'<<16)|('W'<<8)|'T';
129
130    /**
131     * IBinder protocol transaction code: tell an app asynchronously that the
132     * caller likes it.  The app is responsible for incrementing and maintaining
133     * its own like counter, and may display this value to the user to indicate the
134     * quality of the app.  This is an optional command that applications do not
135     * need to handle, so the default implementation is to do nothing.
136     *
137     * <p>There is no response returned and nothing about the
138     * system will be functionally affected by it, but it will improve the
139     * app's self-esteem.
140     */
141    int LIKE_TRANSACTION   = ('_'<<24)|('L'<<16)|('I'<<8)|'K';
142
143    /**
144     * Flag to {@link #transact}: this is a one-way call, meaning that the
145     * caller returns immediately, without waiting for a result from the
146     * callee. Applies only if the caller and callee are in different
147     * processes.
148     */
149    int FLAG_ONEWAY             = 0x00000001;
150
151    /**
152     * Get the canonical name of the interface supported by this binder.
153     */
154    public String getInterfaceDescriptor() throws RemoteException;
155
156    /**
157     * Check to see if the object still exists.
158     *
159     * @return Returns false if the
160     * hosting process is gone, otherwise the result (always by default
161     * true) returned by the pingBinder() implementation on the other
162     * side.
163     */
164    public boolean pingBinder();
165
166    /**
167     * Check to see if the process that the binder is in is still alive.
168     *
169     * @return false if the process is not alive.  Note that if it returns
170     * true, the process may have died while the call is returning.
171     */
172    public boolean isBinderAlive();
173
174    /**
175     * Attempt to retrieve a local implementation of an interface
176     * for this Binder object.  If null is returned, you will need
177     * to instantiate a proxy class to marshall calls through
178     * the transact() method.
179     */
180    public IInterface queryLocalInterface(String descriptor);
181
182    /**
183     * Print the object's state into the given stream.
184     *
185     * @param fd The raw file descriptor that the dump is being sent to.
186     * @param args additional arguments to the dump request.
187     */
188    public void dump(FileDescriptor fd, String[] args) throws RemoteException;
189
190    /**
191     * Like {@link #dump(FileDescriptor, String[])} but always executes
192     * asynchronously.  If the object is local, a new thread is created
193     * to perform the dump.
194     *
195     * @param fd The raw file descriptor that the dump is being sent to.
196     * @param args additional arguments to the dump request.
197     */
198    public void dumpAsync(FileDescriptor fd, String[] args) throws RemoteException;
199
200    /**
201     * Perform a generic operation with the object.
202     *
203     * @param code The action to perform.  This should
204     * be a number between {@link #FIRST_CALL_TRANSACTION} and
205     * {@link #LAST_CALL_TRANSACTION}.
206     * @param data Marshalled data to send to the target.  Must not be null.
207     * If you are not sending any data, you must create an empty Parcel
208     * that is given here.
209     * @param reply Marshalled data to be received from the target.  May be
210     * null if you are not interested in the return value.
211     * @param flags Additional operation flags.  Either 0 for a normal
212     * RPC, or {@link #FLAG_ONEWAY} for a one-way RPC.
213     */
214    public boolean transact(int code, Parcel data, Parcel reply, int flags)
215        throws RemoteException;
216
217    /**
218     * Interface for receiving a callback when the process hosting an IBinder
219     * has gone away.
220     *
221     * @see #linkToDeath
222     */
223    public interface DeathRecipient {
224        public void binderDied();
225    }
226
227    /**
228     * Register the recipient for a notification if this binder
229     * goes away.  If this binder object unexpectedly goes away
230     * (typically because its hosting process has been killed),
231     * then the given {@link DeathRecipient}'s
232     * {@link DeathRecipient#binderDied DeathRecipient.binderDied()} method
233     * will be called.
234     *
235     * <p>You will only receive death notifications for remote binders,
236     * as local binders by definition can't die without you dying as well.
237     *
238     * @throws Throws {@link RemoteException} if the target IBinder's
239     * process has already died.
240     *
241     * @see #unlinkToDeath
242     */
243    public void linkToDeath(DeathRecipient recipient, int flags)
244            throws RemoteException;
245
246    /**
247     * Remove a previously registered death notification.
248     * The recipient will no longer be called if this object
249     * dies.
250     *
251     * @return Returns true if the <var>recipient</var> is successfully
252     * unlinked, assuring you that its
253     * {@link DeathRecipient#binderDied DeathRecipient.binderDied()} method
254     * will not be called.  Returns false if the target IBinder has already
255     * died, meaning the method has been (or soon will be) called.
256     *
257     * @throws Throws {@link java.util.NoSuchElementException} if the given
258     * <var>recipient</var> has not been registered with the IBinder, and
259     * the IBinder is still alive.  Note that if the <var>recipient</var>
260     * was never registered, but the IBinder has already died, then this
261     * exception will <em>not</em> be thrown, and you will receive a false
262     * return value instead.
263     */
264    public boolean unlinkToDeath(DeathRecipient recipient, int flags);
265}
266