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 | |
Christopher Wiley | 3eab1b4 | 2015-12-04 14:15:27 -0800 | [diff] [blame^] | 20 | #include <cstdlib> // Defines types needed for linux/binder.h |
| 21 | #include <linux/binder.h> // Needed for B_PACK_CHARS |
| 22 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | #include <utils/Errors.h> |
| 24 | #include <utils/RefBase.h> |
| 25 | #include <utils/String16.h> |
| 26 | #include <utils/Vector.h> |
| 27 | |
Christopher Wiley | 3eab1b4 | 2015-12-04 14:15:27 -0800 | [diff] [blame^] | 28 | #ifndef B_PACK_CHARS |
| 29 | #error "linux/binder.h no longer defines B_PACK_CHARS" |
| 30 | #endif |
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; |
| 39 | |
| 40 | /** |
| 41 | * Base class and low-level protocol for a remotable object. |
| 42 | * You can derive from this class to create an object for which other |
| 43 | * processes can hold references to it. Communication between processes |
| 44 | * (method calls, property get and set) is down through a low-level |
| 45 | * protocol implemented on top of the transact() API. |
| 46 | */ |
| 47 | class IBinder : public virtual RefBase |
| 48 | { |
| 49 | public: |
| 50 | enum { |
| 51 | FIRST_CALL_TRANSACTION = 0x00000001, |
| 52 | LAST_CALL_TRANSACTION = 0x00ffffff, |
| 53 | |
| 54 | PING_TRANSACTION = B_PACK_CHARS('_','P','N','G'), |
| 55 | DUMP_TRANSACTION = B_PACK_CHARS('_','D','M','P'), |
| 56 | INTERFACE_TRANSACTION = B_PACK_CHARS('_', 'N', 'T', 'F'), |
Dianne Hackborn | 555f89d | 2012-05-08 18:54:22 -0700 | [diff] [blame] | 57 | SYSPROPS_TRANSACTION = B_PACK_CHARS('_', 'S', 'P', 'R'), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | |
Dianne Hackborn | 8c6cedc | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 59 | // Corresponds to TF_ONE_WAY -- an asynchronous call. |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | FLAG_ONEWAY = 0x00000001 |
| 61 | }; |
| 62 | |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 63 | IBinder(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Check if this IBinder implements the interface named by |
| 67 | * @a descriptor. If it does, the base pointer to it is returned, |
| 68 | * which you can safely static_cast<> to the concrete C++ interface. |
| 69 | */ |
| 70 | virtual sp<IInterface> queryLocalInterface(const String16& descriptor); |
| 71 | |
| 72 | /** |
| 73 | * Return the canonical name of the interface provided by this IBinder |
| 74 | * object. |
| 75 | */ |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 76 | virtual const String16& getInterfaceDescriptor() const = 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | |
| 78 | virtual bool isBinderAlive() const = 0; |
| 79 | virtual status_t pingBinder() = 0; |
| 80 | virtual status_t dump(int fd, const Vector<String16>& args) = 0; |
| 81 | |
| 82 | virtual status_t transact( uint32_t code, |
| 83 | const Parcel& data, |
| 84 | Parcel* reply, |
| 85 | uint32_t flags = 0) = 0; |
| 86 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | class DeathRecipient : public virtual RefBase |
| 88 | { |
| 89 | public: |
| 90 | virtual void binderDied(const wp<IBinder>& who) = 0; |
| 91 | }; |
| 92 | |
| 93 | /** |
| 94 | * Register the @a recipient for a notification if this binder |
| 95 | * goes away. If this binder object unexpectedly goes away |
| 96 | * (typically because its hosting process has been killed), |
Christopher Tate | 71f64dd | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 97 | * then DeathRecipient::binderDied() will be called with a reference |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | * to this. |
| 99 | * |
| 100 | * The @a cookie is optional -- if non-NULL, it should be a |
| 101 | * memory address that you own (that is, you know it is unique). |
| 102 | * |
| 103 | * @note You will only receive death notifications for remote binders, |
| 104 | * as local binders by definition can't die without you dying as well. |
| 105 | * Trying to use this function on a local binder will result in an |
| 106 | * INVALID_OPERATION code being returned and nothing happening. |
| 107 | * |
| 108 | * @note This link always holds a weak reference to its recipient. |
| 109 | * |
| 110 | * @note You will only receive a weak reference to the dead |
| 111 | * binder. You should not try to promote this to a strong reference. |
| 112 | * (Nor should you need to, as there is nothing useful you can |
| 113 | * directly do with it now that it has passed on.) |
| 114 | */ |
| 115 | virtual status_t linkToDeath(const sp<DeathRecipient>& recipient, |
| 116 | void* cookie = NULL, |
| 117 | uint32_t flags = 0) = 0; |
| 118 | |
| 119 | /** |
| 120 | * Remove a previously registered death notification. |
| 121 | * The @a recipient will no longer be called if this object |
| 122 | * dies. The @a cookie is optional. If non-NULL, you can |
| 123 | * supply a NULL @a recipient, and the recipient previously |
| 124 | * added with that cookie will be unlinked. |
| 125 | */ |
| 126 | virtual status_t unlinkToDeath( const wp<DeathRecipient>& recipient, |
| 127 | void* cookie = NULL, |
| 128 | uint32_t flags = 0, |
| 129 | wp<DeathRecipient>* outRecipient = NULL) = 0; |
| 130 | |
| 131 | virtual bool checkSubclass(const void* subclassID) const; |
| 132 | |
| 133 | typedef void (*object_cleanup_func)(const void* id, void* obj, void* cleanupCookie); |
| 134 | |
| 135 | virtual void attachObject( const void* objectID, |
| 136 | void* object, |
| 137 | void* cleanupCookie, |
| 138 | object_cleanup_func func) = 0; |
| 139 | virtual void* findObject(const void* objectID) const = 0; |
| 140 | virtual void detachObject(const void* objectID) = 0; |
| 141 | |
| 142 | virtual BBinder* localBinder(); |
| 143 | virtual BpBinder* remoteBinder(); |
| 144 | |
| 145 | protected: |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 146 | virtual ~IBinder(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | |
| 148 | private: |
| 149 | }; |
| 150 | |
| 151 | }; // namespace android |
| 152 | |
| 153 | // --------------------------------------------------------------------------- |
| 154 | |
| 155 | #endif // ANDROID_IBINDER_H |