The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Moreland | 28723ae | 2019-04-01 18:52:30 -0700 | [diff] [blame] | 25 | // linux/binder.h defines this, but we don't want to include it here in order to |
| 26 | // avoid exporting the kernel headers |
Casey Dahlin | b0343a9 | 2015-12-14 16:09:06 -0800 | [diff] [blame] | 27 | #ifndef B_PACK_CHARS |
Bart Sears | e86316a | 2015-12-05 03:38:34 +0000 | [diff] [blame] | 28 | #define B_PACK_CHARS(c1, c2, c3, c4) \ |
| 29 | ((((c1)<<24)) | (((c2)<<16)) | (((c3)<<8)) | (c4)) |
Casey Dahlin | b0343a9 | 2015-12-14 16:09:06 -0800 | [diff] [blame] | 30 | #endif // B_PACK_CHARS |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | |
| 32 | // --------------------------------------------------------------------------- |
| 33 | namespace android { |
| 34 | |
| 35 | class BBinder; |
| 36 | class BpBinder; |
| 37 | class IInterface; |
| 38 | class Parcel; |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 39 | class IResultReceiver; |
Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 40 | class IShellCallback; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | |
| 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 Wall | 202f6d1 | 2018-10-11 12:28:34 -0700 | [diff] [blame] | 49 | class [[clang::lto_visibility_public]] IBinder : public virtual RefBase |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | { |
| 51 | public: |
| 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 Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 58 | SHELL_COMMAND_TRANSACTION = B_PACK_CHARS('_','C','M','D'), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | INTERFACE_TRANSACTION = B_PACK_CHARS('_', 'N', 'T', 'F'), |
Dianne Hackborn | 555f89d | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 60 | SYSPROPS_TRANSACTION = B_PACK_CHARS('_', 'S', 'P', 'R'), |
Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 61 | EXTENSION_TRANSACTION = B_PACK_CHARS('_', 'E', 'X', 'T'), |
Steven Moreland | 86080b8 | 2019-09-23 15:41:18 -0700 | [diff] [blame] | 62 | DEBUG_PID_TRANSACTION = B_PACK_CHARS('_', 'P', 'I', 'D'), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | |
Dianne Hackborn | 8c6cedc | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 64 | // Corresponds to TF_ONE_WAY -- an asynchronous call. |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | FLAG_ONEWAY = 0x00000001 |
| 66 | }; |
| 67 | |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 68 | IBinder(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | |
| 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 Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 81 | virtual const String16& getInterfaceDescriptor() const = 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | |
| 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 Hackborn | f2bf93b | 2015-10-14 15:13:02 -0700 | [diff] [blame] | 86 | static status_t shellCommand(const sp<IBinder>& target, int in, int out, int err, |
Dianne Hackborn | 1941a40 | 2016-08-29 12:30:43 -0700 | [diff] [blame] | 87 | Vector<String16>& args, const sp<IShellCallback>& callback, |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 88 | const sp<IResultReceiver>& resultReceiver); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | |
Steven Moreland | b8ad08d | 2019-08-09 14:42:56 -0700 | [diff] [blame] | 90 | /** |
| 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 | |
Steven Moreland | 86080b8 | 2019-09-23 15:41:18 -0700 | [diff] [blame] | 133 | /** |
| 134 | * Dump PID for a binder, for debugging. |
| 135 | */ |
| 136 | status_t getDebugPid(pid_t* outPid); |
| 137 | |
Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 138 | // NOLINTNEXTLINE(google-default-arguments) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | virtual status_t transact( uint32_t code, |
| 140 | const Parcel& data, |
| 141 | Parcel* reply, |
| 142 | uint32_t flags = 0) = 0; |
| 143 | |
Colin Cross | 97b64db | 2016-09-26 13:48:02 -0700 | [diff] [blame] | 144 | // DeathRecipient is pure abstract, there is no virtual method |
| 145 | // implementation to put in a translation unit in order to silence the |
| 146 | // weak vtables warning. |
| 147 | #if defined(__clang__) |
| 148 | #pragma clang diagnostic push |
| 149 | #pragma clang diagnostic ignored "-Wweak-vtables" |
| 150 | #endif |
| 151 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | class DeathRecipient : public virtual RefBase |
| 153 | { |
| 154 | public: |
| 155 | virtual void binderDied(const wp<IBinder>& who) = 0; |
| 156 | }; |
| 157 | |
Colin Cross | 97b64db | 2016-09-26 13:48:02 -0700 | [diff] [blame] | 158 | #if defined(__clang__) |
| 159 | #pragma clang diagnostic pop |
| 160 | #endif |
| 161 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | /** |
| 163 | * Register the @a recipient for a notification if this binder |
| 164 | * goes away. If this binder object unexpectedly goes away |
| 165 | * (typically because its hosting process has been killed), |
Christopher Tate | 71f64dd | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 166 | * then DeathRecipient::binderDied() will be called with a reference |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | * to this. |
| 168 | * |
| 169 | * The @a cookie is optional -- if non-NULL, it should be a |
| 170 | * memory address that you own (that is, you know it is unique). |
| 171 | * |
| 172 | * @note You will only receive death notifications for remote binders, |
| 173 | * as local binders by definition can't die without you dying as well. |
| 174 | * Trying to use this function on a local binder will result in an |
| 175 | * INVALID_OPERATION code being returned and nothing happening. |
| 176 | * |
| 177 | * @note This link always holds a weak reference to its recipient. |
| 178 | * |
| 179 | * @note You will only receive a weak reference to the dead |
| 180 | * binder. You should not try to promote this to a strong reference. |
| 181 | * (Nor should you need to, as there is nothing useful you can |
| 182 | * directly do with it now that it has passed on.) |
| 183 | */ |
Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 184 | // NOLINTNEXTLINE(google-default-arguments) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | virtual status_t linkToDeath(const sp<DeathRecipient>& recipient, |
Yi Kong | 87d465c | 2018-07-24 01:14:06 -0700 | [diff] [blame] | 186 | void* cookie = nullptr, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | uint32_t flags = 0) = 0; |
| 188 | |
| 189 | /** |
| 190 | * Remove a previously registered death notification. |
| 191 | * The @a recipient will no longer be called if this object |
| 192 | * dies. The @a cookie is optional. If non-NULL, you can |
| 193 | * supply a NULL @a recipient, and the recipient previously |
| 194 | * added with that cookie will be unlinked. |
Steven Moreland | 64127ca | 2019-03-13 09:25:44 -0700 | [diff] [blame] | 195 | * |
| 196 | * If the binder is dead, this will return DEAD_OBJECT. Deleting |
| 197 | * the object will also unlink all death recipients. |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | */ |
Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 199 | // NOLINTNEXTLINE(google-default-arguments) |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | virtual status_t unlinkToDeath( const wp<DeathRecipient>& recipient, |
Yi Kong | 87d465c | 2018-07-24 01:14:06 -0700 | [diff] [blame] | 201 | void* cookie = nullptr, |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | uint32_t flags = 0, |
Yi Kong | 87d465c | 2018-07-24 01:14:06 -0700 | [diff] [blame] | 203 | wp<DeathRecipient>* outRecipient = nullptr) = 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | |
| 205 | virtual bool checkSubclass(const void* subclassID) const; |
| 206 | |
| 207 | typedef void (*object_cleanup_func)(const void* id, void* obj, void* cleanupCookie); |
| 208 | |
Steven Moreland | 2038008 | 2018-09-13 15:43:10 -0700 | [diff] [blame] | 209 | /** |
| 210 | * This object is attached for the lifetime of this binder object. When |
| 211 | * this binder object is destructed, the cleanup function of all attached |
| 212 | * objects are invoked with their respective objectID, object, and |
| 213 | * cleanupCookie. Access to these APIs can be made from multiple threads, |
| 214 | * but calls from different threads are allowed to be interleaved. |
| 215 | */ |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | virtual void attachObject( const void* objectID, |
| 217 | void* object, |
| 218 | void* cleanupCookie, |
| 219 | object_cleanup_func func) = 0; |
Steven Moreland | 2038008 | 2018-09-13 15:43:10 -0700 | [diff] [blame] | 220 | /** |
| 221 | * Returns object attached with attachObject. |
| 222 | */ |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | virtual void* findObject(const void* objectID) const = 0; |
Steven Moreland | 2038008 | 2018-09-13 15:43:10 -0700 | [diff] [blame] | 224 | /** |
| 225 | * WARNING: this API does not call the cleanup function for legacy reasons. |
| 226 | * It also does not return void* for legacy reasons. If you need to detach |
| 227 | * an object and destroy it, there are two options: |
| 228 | * - if you can, don't call detachObject and instead wait for the destructor |
| 229 | * to clean it up. |
| 230 | * - manually retrieve and destruct the object (if multiple of your threads |
| 231 | * are accessing these APIs, you must guarantee that attachObject isn't |
| 232 | * called after findObject and before detachObject is called). |
| 233 | */ |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 234 | virtual void detachObject(const void* objectID) = 0; |
| 235 | |
| 236 | virtual BBinder* localBinder(); |
| 237 | virtual BpBinder* remoteBinder(); |
| 238 | |
| 239 | protected: |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 240 | virtual ~IBinder(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | |
| 242 | private: |
| 243 | }; |
| 244 | |
Steven Moreland | 61ff849 | 2019-09-26 16:05:45 -0700 | [diff] [blame^] | 245 | } // namespace android |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | |
| 247 | // --------------------------------------------------------------------------- |
| 248 | |
| 249 | #endif // ANDROID_IBINDER_H |