blob: 408037e71120807108e6c43471539e3922d371c1 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 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#ifndef ANDROID_IBINDER_H
18#define ANDROID_IBINDER_H
19
20#include <utils/Errors.h>
21#include <utils/RefBase.h>
22#include <utils/String16.h>
23#include <utils/Vector.h>
24
Bart Searse86316a2015-12-05 03:38:34 +000025
Casey Dahlinb0343a92015-12-14 16:09:06 -080026// linux/binder.h already defines this, but we can't just include it from there
27// because there are host builds that include this file.
28#ifndef B_PACK_CHARS
Bart Searse86316a2015-12-05 03:38:34 +000029#define B_PACK_CHARS(c1, c2, c3, c4) \
30 ((((c1)<<24)) | (((c2)<<16)) | (((c3)<<8)) | (c4))
Casey Dahlinb0343a92015-12-14 16:09:06 -080031#endif // B_PACK_CHARS
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032
33// ---------------------------------------------------------------------------
34namespace android {
35
36class BBinder;
37class BpBinder;
38class IInterface;
39class Parcel;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070040class IResultReceiver;
Dianne Hackborn1941a402016-08-29 12:30:43 -070041class IShellCallback;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042
43/**
44 * Base class and low-level protocol for a remotable object.
45 * You can derive from this class to create an object for which other
46 * processes can hold references to it. Communication between processes
47 * (method calls, property get and set) is down through a low-level
48 * protocol implemented on top of the transact() API.
49 */
Marissa Wall202f6d12018-10-11 12:28:34 -070050class [[clang::lto_visibility_public]] IBinder : public virtual RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051{
52public:
53 enum {
54 FIRST_CALL_TRANSACTION = 0x00000001,
55 LAST_CALL_TRANSACTION = 0x00ffffff,
56
57 PING_TRANSACTION = B_PACK_CHARS('_','P','N','G'),
58 DUMP_TRANSACTION = B_PACK_CHARS('_','D','M','P'),
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070059 SHELL_COMMAND_TRANSACTION = B_PACK_CHARS('_','C','M','D'),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060 INTERFACE_TRANSACTION = B_PACK_CHARS('_', 'N', 'T', 'F'),
Dianne Hackborn555f89d2012-05-08 18:54:22 -070061 SYSPROPS_TRANSACTION = B_PACK_CHARS('_', 'S', 'P', 'R'),
Steven Morelandb8ad08d2019-08-09 14:42:56 -070062 EXTENSION_TRANSACTION = B_PACK_CHARS('_', 'E', 'X', 'T'),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063
Dianne Hackborn8c6cedc2009-12-07 17:59:37 -080064 // Corresponds to TF_ONE_WAY -- an asynchronous call.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065 FLAG_ONEWAY = 0x00000001
66 };
67
Mathias Agopian83c04462009-05-22 19:00:22 -070068 IBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069
70 /**
71 * Check if this IBinder implements the interface named by
72 * @a descriptor. If it does, the base pointer to it is returned,
73 * which you can safely static_cast<> to the concrete C++ interface.
74 */
75 virtual sp<IInterface> queryLocalInterface(const String16& descriptor);
76
77 /**
78 * Return the canonical name of the interface provided by this IBinder
79 * object.
80 */
Mathias Agopian83c04462009-05-22 19:00:22 -070081 virtual const String16& getInterfaceDescriptor() const = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082
83 virtual bool isBinderAlive() const = 0;
84 virtual status_t pingBinder() = 0;
85 virtual status_t dump(int fd, const Vector<String16>& args) = 0;
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070086 static status_t shellCommand(const sp<IBinder>& target, int in, int out, int err,
Dianne Hackborn1941a402016-08-29 12:30:43 -070087 Vector<String16>& args, const sp<IShellCallback>& callback,
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070088 const sp<IResultReceiver>& resultReceiver);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089
Steven Morelandb8ad08d2019-08-09 14:42:56 -070090 /**
91 * This allows someone to add their own additions to an interface without
92 * having to modify the original interface.
93 *
94 * For instance, imagine if we have this interface:
95 * interface IFoo { void doFoo(); }
96 *
97 * If an unrelated owner (perhaps in a downstream codebase) wants to make a
98 * change to the interface, they have two options:
99 *
100 * A). Historical option that has proven to be BAD! Only the original
101 * author of an interface should change an interface. If someone
102 * downstream wants additional functionality, they should not ever
103 * change the interface or use this method.
104 *
105 * BAD TO DO: interface IFoo { BAD TO DO
106 * BAD TO DO: void doFoo(); BAD TO DO
107 * BAD TO DO: + void doBar(); // adding a method BAD TO DO
108 * BAD TO DO: } BAD TO DO
109 *
110 * B). Option that this method enables!
111 * Leave the original interface unchanged (do not change IFoo!).
112 * Instead, create a new interface in a downstream package:
113 *
114 * package com.<name>; // new functionality in a new package
115 * interface IBar { void doBar(); }
116 *
117 * When registering the interface, add:
118 * sp<MyFoo> foo = new MyFoo; // class in AOSP codebase
119 * sp<MyBar> bar = new MyBar; // custom extension class
120 * foo->setExtension(bar); // use method in BBinder
121 *
122 * Then, clients of IFoo can get this extension:
123 * sp<IBinder> binder = ...;
124 * sp<IFoo> foo = interface_cast<IFoo>(binder); // handle if null
125 * sp<IBinder> barBinder;
126 * ... handle error ... = binder->getExtension(&barBinder);
127 * sp<IBar> bar = interface_cast<IBar>(barBinder);
128 * // if bar is null, then there is no extension or a different
129 * // type of extension
130 */
131 status_t getExtension(sp<IBinder>* out);
132
Jiyong Parkb86c8662018-10-29 23:01:57 +0900133 // NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134 virtual status_t transact( uint32_t code,
135 const Parcel& data,
136 Parcel* reply,
137 uint32_t flags = 0) = 0;
138
Colin Cross97b64db2016-09-26 13:48:02 -0700139 // DeathRecipient is pure abstract, there is no virtual method
140 // implementation to put in a translation unit in order to silence the
141 // weak vtables warning.
142 #if defined(__clang__)
143 #pragma clang diagnostic push
144 #pragma clang diagnostic ignored "-Wweak-vtables"
145 #endif
146
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147 class DeathRecipient : public virtual RefBase
148 {
149 public:
150 virtual void binderDied(const wp<IBinder>& who) = 0;
151 };
152
Colin Cross97b64db2016-09-26 13:48:02 -0700153 #if defined(__clang__)
154 #pragma clang diagnostic pop
155 #endif
156
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800157 /**
158 * Register the @a recipient for a notification if this binder
159 * goes away. If this binder object unexpectedly goes away
160 * (typically because its hosting process has been killed),
Christopher Tate71f64dd2011-02-17 13:00:38 -0800161 * then DeathRecipient::binderDied() will be called with a reference
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 * to this.
163 *
164 * The @a cookie is optional -- if non-NULL, it should be a
165 * memory address that you own (that is, you know it is unique).
166 *
167 * @note You will only receive death notifications for remote binders,
168 * as local binders by definition can't die without you dying as well.
169 * Trying to use this function on a local binder will result in an
170 * INVALID_OPERATION code being returned and nothing happening.
171 *
172 * @note This link always holds a weak reference to its recipient.
173 *
174 * @note You will only receive a weak reference to the dead
175 * binder. You should not try to promote this to a strong reference.
176 * (Nor should you need to, as there is nothing useful you can
177 * directly do with it now that it has passed on.)
178 */
Jiyong Parkb86c8662018-10-29 23:01:57 +0900179 // NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180 virtual status_t linkToDeath(const sp<DeathRecipient>& recipient,
Yi Kong87d465c2018-07-24 01:14:06 -0700181 void* cookie = nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182 uint32_t flags = 0) = 0;
183
184 /**
185 * Remove a previously registered death notification.
186 * The @a recipient will no longer be called if this object
187 * dies. The @a cookie is optional. If non-NULL, you can
188 * supply a NULL @a recipient, and the recipient previously
189 * added with that cookie will be unlinked.
Steven Moreland64127ca2019-03-13 09:25:44 -0700190 *
191 * If the binder is dead, this will return DEAD_OBJECT. Deleting
192 * the object will also unlink all death recipients.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193 */
Jiyong Parkb86c8662018-10-29 23:01:57 +0900194 // NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195 virtual status_t unlinkToDeath( const wp<DeathRecipient>& recipient,
Yi Kong87d465c2018-07-24 01:14:06 -0700196 void* cookie = nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197 uint32_t flags = 0,
Yi Kong87d465c2018-07-24 01:14:06 -0700198 wp<DeathRecipient>* outRecipient = nullptr) = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199
200 virtual bool checkSubclass(const void* subclassID) const;
201
202 typedef void (*object_cleanup_func)(const void* id, void* obj, void* cleanupCookie);
203
Steven Moreland20380082018-09-13 15:43:10 -0700204 /**
205 * This object is attached for the lifetime of this binder object. When
206 * this binder object is destructed, the cleanup function of all attached
207 * objects are invoked with their respective objectID, object, and
208 * cleanupCookie. Access to these APIs can be made from multiple threads,
209 * but calls from different threads are allowed to be interleaved.
210 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211 virtual void attachObject( const void* objectID,
212 void* object,
213 void* cleanupCookie,
214 object_cleanup_func func) = 0;
Steven Moreland20380082018-09-13 15:43:10 -0700215 /**
216 * Returns object attached with attachObject.
217 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218 virtual void* findObject(const void* objectID) const = 0;
Steven Moreland20380082018-09-13 15:43:10 -0700219 /**
220 * WARNING: this API does not call the cleanup function for legacy reasons.
221 * It also does not return void* for legacy reasons. If you need to detach
222 * an object and destroy it, there are two options:
223 * - if you can, don't call detachObject and instead wait for the destructor
224 * to clean it up.
225 * - manually retrieve and destruct the object (if multiple of your threads
226 * are accessing these APIs, you must guarantee that attachObject isn't
227 * called after findObject and before detachObject is called).
228 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800229 virtual void detachObject(const void* objectID) = 0;
230
231 virtual BBinder* localBinder();
232 virtual BpBinder* remoteBinder();
233
234protected:
Mathias Agopian83c04462009-05-22 19:00:22 -0700235 virtual ~IBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800236
237private:
238};
239
240}; // namespace android
241
242// ---------------------------------------------------------------------------
243
244#endif // ANDROID_IBINDER_H