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