blob: 64604b74f0342195dfe8f67f8204b2d614ef1571 [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
Steven Moreland28723ae2019-04-01 18:52:30 -070025// linux/binder.h defines this, but we don't want to include it here in order to
26// avoid exporting the kernel headers
Casey Dahlinb0343a92015-12-14 16:09:06 -080027#ifndef B_PACK_CHARS
Bart Searse86316a2015-12-05 03:38:34 +000028#define B_PACK_CHARS(c1, c2, c3, c4) \
29 ((((c1)<<24)) | (((c2)<<16)) | (((c3)<<8)) | (c4))
Casey Dahlinb0343a92015-12-14 16:09:06 -080030#endif // B_PACK_CHARS
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031
32// ---------------------------------------------------------------------------
33namespace android {
34
35class BBinder;
36class BpBinder;
37class IInterface;
38class Parcel;
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070039class IResultReceiver;
Dianne Hackborn1941a402016-08-29 12:30:43 -070040class IShellCallback;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041
42/**
43 * Base class and low-level protocol for a remotable object.
44 * You can derive from this class to create an object for which other
45 * processes can hold references to it. Communication between processes
46 * (method calls, property get and set) is down through a low-level
47 * protocol implemented on top of the transact() API.
48 */
Marissa Wall202f6d12018-10-11 12:28:34 -070049class [[clang::lto_visibility_public]] IBinder : public virtual RefBase
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080050{
51public:
52 enum {
53 FIRST_CALL_TRANSACTION = 0x00000001,
54 LAST_CALL_TRANSACTION = 0x00ffffff,
55
56 PING_TRANSACTION = B_PACK_CHARS('_','P','N','G'),
57 DUMP_TRANSACTION = B_PACK_CHARS('_','D','M','P'),
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070058 SHELL_COMMAND_TRANSACTION = B_PACK_CHARS('_','C','M','D'),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059 INTERFACE_TRANSACTION = B_PACK_CHARS('_', 'N', 'T', 'F'),
Dianne Hackborn555f89d2012-05-08 18:54:22 -070060 SYSPROPS_TRANSACTION = B_PACK_CHARS('_', 'S', 'P', 'R'),
Steven Morelandb8ad08d2019-08-09 14:42:56 -070061 EXTENSION_TRANSACTION = B_PACK_CHARS('_', 'E', 'X', 'T'),
Steven Moreland86080b82019-09-23 15:41:18 -070062 DEBUG_PID_TRANSACTION = B_PACK_CHARS('_', 'P', 'I', 'D'),
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.
Steven Moreland46b5fea2019-10-15 11:22:18 -070065 FLAG_ONEWAY = 0x00000001,
66
67 // Private userspace flag for transaction which is being requested from
68 // a vendor context.
69 FLAG_PRIVATE_VENDOR = 0x10000000,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070 };
71
Mathias Agopian83c04462009-05-22 19:00:22 -070072 IBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073
74 /**
75 * Check if this IBinder implements the interface named by
76 * @a descriptor. If it does, the base pointer to it is returned,
77 * which you can safely static_cast<> to the concrete C++ interface.
78 */
79 virtual sp<IInterface> queryLocalInterface(const String16& descriptor);
80
81 /**
82 * Return the canonical name of the interface provided by this IBinder
83 * object.
84 */
Mathias Agopian83c04462009-05-22 19:00:22 -070085 virtual const String16& getInterfaceDescriptor() const = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080086
87 virtual bool isBinderAlive() const = 0;
88 virtual status_t pingBinder() = 0;
89 virtual status_t dump(int fd, const Vector<String16>& args) = 0;
Dianne Hackbornf2bf93b2015-10-14 15:13:02 -070090 static status_t shellCommand(const sp<IBinder>& target, int in, int out, int err,
Dianne Hackborn1941a402016-08-29 12:30:43 -070091 Vector<String16>& args, const sp<IShellCallback>& callback,
Dianne Hackborn23eb1e22015-10-07 17:35:27 -070092 const sp<IResultReceiver>& resultReceiver);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093
Steven Morelandb8ad08d2019-08-09 14:42:56 -070094 /**
95 * This allows someone to add their own additions to an interface without
96 * having to modify the original interface.
97 *
98 * For instance, imagine if we have this interface:
99 * interface IFoo { void doFoo(); }
100 *
101 * If an unrelated owner (perhaps in a downstream codebase) wants to make a
102 * change to the interface, they have two options:
103 *
104 * A). Historical option that has proven to be BAD! Only the original
105 * author of an interface should change an interface. If someone
106 * downstream wants additional functionality, they should not ever
107 * change the interface or use this method.
108 *
109 * BAD TO DO: interface IFoo { BAD TO DO
110 * BAD TO DO: void doFoo(); BAD TO DO
111 * BAD TO DO: + void doBar(); // adding a method BAD TO DO
112 * BAD TO DO: } BAD TO DO
113 *
114 * B). Option that this method enables!
115 * Leave the original interface unchanged (do not change IFoo!).
116 * Instead, create a new interface in a downstream package:
117 *
118 * package com.<name>; // new functionality in a new package
119 * interface IBar { void doBar(); }
120 *
121 * When registering the interface, add:
122 * sp<MyFoo> foo = new MyFoo; // class in AOSP codebase
123 * sp<MyBar> bar = new MyBar; // custom extension class
124 * foo->setExtension(bar); // use method in BBinder
125 *
126 * Then, clients of IFoo can get this extension:
127 * sp<IBinder> binder = ...;
128 * sp<IFoo> foo = interface_cast<IFoo>(binder); // handle if null
129 * sp<IBinder> barBinder;
130 * ... handle error ... = binder->getExtension(&barBinder);
131 * sp<IBar> bar = interface_cast<IBar>(barBinder);
132 * // if bar is null, then there is no extension or a different
133 * // type of extension
134 */
135 status_t getExtension(sp<IBinder>* out);
136
Steven Moreland86080b82019-09-23 15:41:18 -0700137 /**
138 * Dump PID for a binder, for debugging.
139 */
140 status_t getDebugPid(pid_t* outPid);
141
Jiyong Parkb86c8662018-10-29 23:01:57 +0900142 // NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143 virtual status_t transact( uint32_t code,
144 const Parcel& data,
145 Parcel* reply,
146 uint32_t flags = 0) = 0;
147
Colin Cross97b64db2016-09-26 13:48:02 -0700148 // DeathRecipient is pure abstract, there is no virtual method
149 // implementation to put in a translation unit in order to silence the
150 // weak vtables warning.
151 #if defined(__clang__)
152 #pragma clang diagnostic push
153 #pragma clang diagnostic ignored "-Wweak-vtables"
154 #endif
155
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156 class DeathRecipient : public virtual RefBase
157 {
158 public:
159 virtual void binderDied(const wp<IBinder>& who) = 0;
160 };
161
Colin Cross97b64db2016-09-26 13:48:02 -0700162 #if defined(__clang__)
163 #pragma clang diagnostic pop
164 #endif
165
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 /**
167 * Register the @a recipient for a notification if this binder
168 * goes away. If this binder object unexpectedly goes away
169 * (typically because its hosting process has been killed),
Christopher Tate71f64dd2011-02-17 13:00:38 -0800170 * then DeathRecipient::binderDied() will be called with a reference
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171 * to this.
172 *
173 * The @a cookie is optional -- if non-NULL, it should be a
174 * memory address that you own (that is, you know it is unique).
175 *
176 * @note You will only receive death notifications for remote binders,
177 * as local binders by definition can't die without you dying as well.
178 * Trying to use this function on a local binder will result in an
179 * INVALID_OPERATION code being returned and nothing happening.
180 *
181 * @note This link always holds a weak reference to its recipient.
182 *
183 * @note You will only receive a weak reference to the dead
184 * binder. You should not try to promote this to a strong reference.
185 * (Nor should you need to, as there is nothing useful you can
186 * directly do with it now that it has passed on.)
187 */
Jiyong Parkb86c8662018-10-29 23:01:57 +0900188 // NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800189 virtual status_t linkToDeath(const sp<DeathRecipient>& recipient,
Yi Kong87d465c2018-07-24 01:14:06 -0700190 void* cookie = nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191 uint32_t flags = 0) = 0;
192
193 /**
194 * Remove a previously registered death notification.
195 * The @a recipient will no longer be called if this object
196 * dies. The @a cookie is optional. If non-NULL, you can
197 * supply a NULL @a recipient, and the recipient previously
198 * added with that cookie will be unlinked.
Steven Moreland64127ca2019-03-13 09:25:44 -0700199 *
200 * If the binder is dead, this will return DEAD_OBJECT. Deleting
201 * the object will also unlink all death recipients.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202 */
Jiyong Parkb86c8662018-10-29 23:01:57 +0900203 // NOLINTNEXTLINE(google-default-arguments)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204 virtual status_t unlinkToDeath( const wp<DeathRecipient>& recipient,
Yi Kong87d465c2018-07-24 01:14:06 -0700205 void* cookie = nullptr,
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206 uint32_t flags = 0,
Yi Kong87d465c2018-07-24 01:14:06 -0700207 wp<DeathRecipient>* outRecipient = nullptr) = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208
209 virtual bool checkSubclass(const void* subclassID) const;
210
211 typedef void (*object_cleanup_func)(const void* id, void* obj, void* cleanupCookie);
212
Steven Moreland20380082018-09-13 15:43:10 -0700213 /**
214 * This object is attached for the lifetime of this binder object. When
215 * this binder object is destructed, the cleanup function of all attached
216 * objects are invoked with their respective objectID, object, and
217 * cleanupCookie. Access to these APIs can be made from multiple threads,
218 * but calls from different threads are allowed to be interleaved.
219 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800220 virtual void attachObject( const void* objectID,
221 void* object,
222 void* cleanupCookie,
223 object_cleanup_func func) = 0;
Steven Moreland20380082018-09-13 15:43:10 -0700224 /**
225 * Returns object attached with attachObject.
226 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227 virtual void* findObject(const void* objectID) const = 0;
Steven Moreland20380082018-09-13 15:43:10 -0700228 /**
229 * WARNING: this API does not call the cleanup function for legacy reasons.
230 * It also does not return void* for legacy reasons. If you need to detach
231 * an object and destroy it, there are two options:
232 * - if you can, don't call detachObject and instead wait for the destructor
233 * to clean it up.
234 * - manually retrieve and destruct the object (if multiple of your threads
235 * are accessing these APIs, you must guarantee that attachObject isn't
236 * called after findObject and before detachObject is called).
237 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800238 virtual void detachObject(const void* objectID) = 0;
239
240 virtual BBinder* localBinder();
241 virtual BpBinder* remoteBinder();
242
243protected:
Mathias Agopian83c04462009-05-22 19:00:22 -0700244 virtual ~IBinder();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800245
246private:
247};
248
Steven Moreland61ff8492019-09-26 16:05:45 -0700249} // namespace android
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800250
251// ---------------------------------------------------------------------------
252
253#endif // ANDROID_IBINDER_H