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 17 package android.os; 18 19 import java.io.FileDescriptor; 20 import 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 */ 86 public 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 * Flag to {@link #transact}: this is a one-way call, meaning that the 114 * caller returns immediately, without waiting for a result from the 115 * callee. 116 */ 117 int FLAG_ONEWAY = 0x00000001; 118 119 /** 120 * Get the canonical name of the interface supported by this binder. 121 */ getInterfaceDescriptor()122 public String getInterfaceDescriptor() throws RemoteException; 123 124 /** 125 * Check to see if the object still exists. 126 * 127 * @return Returns false if the 128 * hosting process is gone, otherwise the result (always by default 129 * true) returned by the pingBinder() implementation on the other 130 * side. 131 */ pingBinder()132 public boolean pingBinder(); 133 134 /** 135 * Check to see if the process that the binder is in is still alive. 136 * 137 * @return false if the process is not alive. Note that if it returns 138 * true, the process may have died while the call is returning. 139 */ isBinderAlive()140 public boolean isBinderAlive(); 141 142 /** 143 * Attempt to retrieve a local implementation of an interface 144 * for this Binder object. If null is returned, you will need 145 * to instantiate a proxy class to marshall calls through 146 * the transact() method. 147 */ queryLocalInterface(String descriptor)148 public IInterface queryLocalInterface(String descriptor); 149 150 /** 151 * Print the object's state into the given stream. 152 * 153 * @param fd The raw file descriptor that the dump is being sent to. 154 * @param args additional arguments to the dump request. 155 */ dump(FileDescriptor fd, String[] args)156 public void dump(FileDescriptor fd, String[] args) throws RemoteException; 157 158 /** 159 * Perform a generic operation with the object. 160 * 161 * @param code The action to perform. This should 162 * be a number between {@link #FIRST_CALL_TRANSACTION} and 163 * {@link #LAST_CALL_TRANSACTION}. 164 * @param data Marshalled data to send to the target. Most not be null. 165 * If you are not sending any data, you must create an empty Parcel 166 * that is given here. 167 * @param reply Marshalled data to be received from the target. May be 168 * null if you are not interested in the return value. 169 * @param flags Additional operation flags. Either 0 for a normal 170 * RPC, or {@link #FLAG_ONEWAY} for a one-way RPC. 171 */ transact(int code, Parcel data, Parcel reply, int flags)172 public boolean transact(int code, Parcel data, Parcel reply, int flags) 173 throws RemoteException; 174 175 /** 176 * Interface for receiving a callback when the process hosting an IBinder 177 * has gone away. 178 * 179 * @see #linkToDeath 180 */ 181 public interface DeathRecipient { binderDied()182 public void binderDied(); 183 } 184 185 /** 186 * Register the recipient for a notification if this binder 187 * goes away. If this binder object unexpectedly goes away 188 * (typically because its hosting process has been killed), 189 * then the given {@link DeathRecipient}'s 190 * {@link DeathRecipient#binderDied DeathRecipient.binderDied()} method 191 * will be called. 192 * 193 * <p>You will only receive death notifications for remote binders, 194 * as local binders by definition can't die without you dying as well. 195 * 196 * @throws Throws {@link RemoteException} if the target IBinder's 197 * process has already died. 198 * 199 * @see #unlinkToDeath 200 */ linkToDeath(DeathRecipient recipient, int flags)201 public void linkToDeath(DeathRecipient recipient, int flags) 202 throws RemoteException; 203 204 /** 205 * Remove a previously registered death notification. 206 * The recipient will no longer be called if this object 207 * dies. 208 * 209 * @return Returns true if the <var>recipient</var> is successfully 210 * unlinked, assuring you that its 211 * {@link DeathRecipient#binderDied DeathRecipient.binderDied()} method 212 * will not be called. Returns false if the target IBinder has already 213 * died, meaning the method has been (or soon will be) called. 214 * 215 * @throws Throws {@link java.util.NoSuchElementException} if the given 216 * <var>recipient</var> has not been registered with the IBinder, and 217 * the IBinder is still alive. Note that if the <var>recipient</var> 218 * was never registered, but the IBinder has already died, then this 219 * exception will <em>not</em> be thrown, and you will receive a false 220 * return value instead. 221 */ unlinkToDeath(DeathRecipient recipient, int flags)222 public boolean unlinkToDeath(DeathRecipient recipient, int flags); 223 } 224