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 | |
Bart Sears | e86316a | 2015-12-05 03:38:34 +0000 | [diff] [blame] | 25 | |
Casey Dahlin | b0343a9 | 2015-12-14 16:09:06 -0800 | [diff] [blame] | 26 | // 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 Sears | e86316a | 2015-12-05 03:38:34 +0000 | [diff] [blame] | 29 | #define B_PACK_CHARS(c1, c2, c3, c4) \ |
| 30 | ((((c1)<<24)) | (((c2)<<16)) | (((c3)<<8)) | (c4)) |
Casey Dahlin | b0343a9 | 2015-12-14 16:09:06 -0800 | [diff] [blame] | 31 | #endif // B_PACK_CHARS |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
| 33 | // --------------------------------------------------------------------------- |
| 34 | namespace android { |
| 35 | |
| 36 | class BBinder; |
| 37 | class BpBinder; |
| 38 | class IInterface; |
| 39 | class Parcel; |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 40 | class IResultReceiver; |
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 | */ |
| 49 | class IBinder : public virtual RefBase |
| 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'), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | |
Dianne Hackborn | 8c6cedc | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 62 | // Corresponds to TF_ONE_WAY -- an asynchronous call. |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | FLAG_ONEWAY = 0x00000001 |
| 64 | }; |
| 65 | |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 66 | IBinder(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Check if this IBinder implements the interface named by |
| 70 | * @a descriptor. If it does, the base pointer to it is returned, |
| 71 | * which you can safely static_cast<> to the concrete C++ interface. |
| 72 | */ |
| 73 | virtual sp<IInterface> queryLocalInterface(const String16& descriptor); |
| 74 | |
| 75 | /** |
| 76 | * Return the canonical name of the interface provided by this IBinder |
| 77 | * object. |
| 78 | */ |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 79 | virtual const String16& getInterfaceDescriptor() const = 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | |
| 81 | virtual bool isBinderAlive() const = 0; |
| 82 | virtual status_t pingBinder() = 0; |
| 83 | virtual status_t dump(int fd, const Vector<String16>& args) = 0; |
Dianne Hackborn | f2bf93b | 2015-10-14 15:13:02 -0700 | [diff] [blame] | 84 | static status_t shellCommand(const sp<IBinder>& target, int in, int out, int err, |
| 85 | Vector<String16>& args, |
Dianne Hackborn | 23eb1e2 | 2015-10-07 17:35:27 -0700 | [diff] [blame] | 86 | const sp<IResultReceiver>& resultReceiver); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | |
| 88 | virtual status_t transact( uint32_t code, |
| 89 | const Parcel& data, |
| 90 | Parcel* reply, |
| 91 | uint32_t flags = 0) = 0; |
| 92 | |
Colin Cross | 97b64db | 2016-09-26 13:48:02 -0700 | [diff] [blame] | 93 | // DeathRecipient is pure abstract, there is no virtual method |
| 94 | // implementation to put in a translation unit in order to silence the |
| 95 | // weak vtables warning. |
| 96 | #if defined(__clang__) |
| 97 | #pragma clang diagnostic push |
| 98 | #pragma clang diagnostic ignored "-Wweak-vtables" |
| 99 | #endif |
| 100 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | class DeathRecipient : public virtual RefBase |
| 102 | { |
| 103 | public: |
| 104 | virtual void binderDied(const wp<IBinder>& who) = 0; |
| 105 | }; |
| 106 | |
Colin Cross | 97b64db | 2016-09-26 13:48:02 -0700 | [diff] [blame] | 107 | #if defined(__clang__) |
| 108 | #pragma clang diagnostic pop |
| 109 | #endif |
| 110 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | /** |
| 112 | * Register the @a recipient for a notification if this binder |
| 113 | * goes away. If this binder object unexpectedly goes away |
| 114 | * (typically because its hosting process has been killed), |
Christopher Tate | 71f64dd | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 115 | * then DeathRecipient::binderDied() will be called with a reference |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | * to this. |
| 117 | * |
| 118 | * The @a cookie is optional -- if non-NULL, it should be a |
| 119 | * memory address that you own (that is, you know it is unique). |
| 120 | * |
| 121 | * @note You will only receive death notifications for remote binders, |
| 122 | * as local binders by definition can't die without you dying as well. |
| 123 | * Trying to use this function on a local binder will result in an |
| 124 | * INVALID_OPERATION code being returned and nothing happening. |
| 125 | * |
| 126 | * @note This link always holds a weak reference to its recipient. |
| 127 | * |
| 128 | * @note You will only receive a weak reference to the dead |
| 129 | * binder. You should not try to promote this to a strong reference. |
| 130 | * (Nor should you need to, as there is nothing useful you can |
| 131 | * directly do with it now that it has passed on.) |
| 132 | */ |
| 133 | virtual status_t linkToDeath(const sp<DeathRecipient>& recipient, |
| 134 | void* cookie = NULL, |
| 135 | uint32_t flags = 0) = 0; |
| 136 | |
| 137 | /** |
| 138 | * Remove a previously registered death notification. |
| 139 | * The @a recipient will no longer be called if this object |
| 140 | * dies. The @a cookie is optional. If non-NULL, you can |
| 141 | * supply a NULL @a recipient, and the recipient previously |
| 142 | * added with that cookie will be unlinked. |
| 143 | */ |
| 144 | virtual status_t unlinkToDeath( const wp<DeathRecipient>& recipient, |
| 145 | void* cookie = NULL, |
| 146 | uint32_t flags = 0, |
| 147 | wp<DeathRecipient>* outRecipient = NULL) = 0; |
| 148 | |
| 149 | virtual bool checkSubclass(const void* subclassID) const; |
| 150 | |
| 151 | typedef void (*object_cleanup_func)(const void* id, void* obj, void* cleanupCookie); |
| 152 | |
| 153 | virtual void attachObject( const void* objectID, |
| 154 | void* object, |
| 155 | void* cleanupCookie, |
| 156 | object_cleanup_func func) = 0; |
| 157 | virtual void* findObject(const void* objectID) const = 0; |
| 158 | virtual void detachObject(const void* objectID) = 0; |
| 159 | |
| 160 | virtual BBinder* localBinder(); |
| 161 | virtual BpBinder* remoteBinder(); |
| 162 | |
| 163 | protected: |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 164 | virtual ~IBinder(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | |
| 166 | private: |
| 167 | }; |
| 168 | |
| 169 | }; // namespace android |
| 170 | |
| 171 | // --------------------------------------------------------------------------- |
| 172 | |
| 173 | #endif // ANDROID_IBINDER_H |