The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 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 | // |
| 18 | #ifndef ANDROID_IINTERFACE_H |
| 19 | #define ANDROID_IINTERFACE_H |
| 20 | |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 21 | #include <binder/Binder.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | |
| 23 | namespace android { |
| 24 | |
| 25 | // ---------------------------------------------------------------------- |
| 26 | |
| 27 | class IInterface : public virtual RefBase |
| 28 | { |
| 29 | public: |
Mathias Agopian | aaf834a | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 30 | IInterface(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | sp<IBinder> asBinder(); |
| 32 | sp<const IBinder> asBinder() const; |
Mathias Agopian | aaf834a | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 33 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | protected: |
Mathias Agopian | aaf834a | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 35 | virtual ~IInterface(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | virtual IBinder* onAsBinder() = 0; |
| 37 | }; |
| 38 | |
| 39 | // ---------------------------------------------------------------------- |
| 40 | |
| 41 | template<typename INTERFACE> |
| 42 | inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj) |
| 43 | { |
| 44 | return INTERFACE::asInterface(obj); |
| 45 | } |
| 46 | |
| 47 | // ---------------------------------------------------------------------- |
| 48 | |
| 49 | template<typename INTERFACE> |
| 50 | class BnInterface : public INTERFACE, public BBinder |
| 51 | { |
| 52 | public: |
| 53 | virtual sp<IInterface> queryLocalInterface(const String16& _descriptor); |
Mathias Agopian | aaf834a | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 54 | virtual const String16& getInterfaceDescriptor() const; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | |
| 56 | protected: |
| 57 | virtual IBinder* onAsBinder(); |
| 58 | }; |
| 59 | |
| 60 | // ---------------------------------------------------------------------- |
| 61 | |
| 62 | template<typename INTERFACE> |
| 63 | class BpInterface : public INTERFACE, public BpRefBase |
| 64 | { |
| 65 | public: |
| 66 | BpInterface(const sp<IBinder>& remote); |
| 67 | |
| 68 | protected: |
| 69 | virtual IBinder* onAsBinder(); |
| 70 | }; |
| 71 | |
| 72 | // ---------------------------------------------------------------------- |
| 73 | |
| 74 | #define DECLARE_META_INTERFACE(INTERFACE) \ |
Michael Richardson | 5a870fe | 2009-11-03 17:01:28 -0500 | [diff] [blame] | 75 | static const android::String16 descriptor; \ |
| 76 | static android::sp<I##INTERFACE> asInterface( \ |
| 77 | const android::sp<android::IBinder>& obj); \ |
| 78 | virtual const android::String16& getInterfaceDescriptor() const; \ |
Mathias Agopian | aaf834a | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 79 | I##INTERFACE(); \ |
| 80 | virtual ~I##INTERFACE(); \ |
| 81 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | |
| 83 | #define IMPLEMENT_META_INTERFACE(INTERFACE, NAME) \ |
Michael Richardson | 5a870fe | 2009-11-03 17:01:28 -0500 | [diff] [blame] | 84 | const android::String16 I##INTERFACE::descriptor(NAME); \ |
| 85 | const android::String16& \ |
| 86 | I##INTERFACE::getInterfaceDescriptor() const { \ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | return I##INTERFACE::descriptor; \ |
| 88 | } \ |
Michael Richardson | 5a870fe | 2009-11-03 17:01:28 -0500 | [diff] [blame] | 89 | android::sp<I##INTERFACE> I##INTERFACE::asInterface( \ |
| 90 | const android::sp<android::IBinder>& obj) \ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | { \ |
Michael Richardson | 5a870fe | 2009-11-03 17:01:28 -0500 | [diff] [blame] | 92 | android::sp<I##INTERFACE> intr; \ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | if (obj != NULL) { \ |
| 94 | intr = static_cast<I##INTERFACE*>( \ |
| 95 | obj->queryLocalInterface( \ |
| 96 | I##INTERFACE::descriptor).get()); \ |
| 97 | if (intr == NULL) { \ |
| 98 | intr = new Bp##INTERFACE(obj); \ |
| 99 | } \ |
| 100 | } \ |
| 101 | return intr; \ |
| 102 | } \ |
Mathias Agopian | aaf834a | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 103 | I##INTERFACE::I##INTERFACE() { } \ |
| 104 | I##INTERFACE::~I##INTERFACE() { } \ |
| 105 | |
| 106 | |
| 107 | #define CHECK_INTERFACE(interface, data, reply) \ |
| 108 | if (!data.checkInterface(this)) { return PERMISSION_DENIED; } \ |
| 109 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | |
| 111 | // ---------------------------------------------------------------------- |
Mathias Agopian | aaf834a | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 112 | // No user-serviceable parts after this... |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | |
| 114 | template<typename INTERFACE> |
| 115 | inline sp<IInterface> BnInterface<INTERFACE>::queryLocalInterface( |
| 116 | const String16& _descriptor) |
| 117 | { |
| 118 | if (_descriptor == INTERFACE::descriptor) return this; |
| 119 | return NULL; |
| 120 | } |
| 121 | |
| 122 | template<typename INTERFACE> |
Mathias Agopian | aaf834a | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 123 | inline const String16& BnInterface<INTERFACE>::getInterfaceDescriptor() const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | { |
| 125 | return INTERFACE::getInterfaceDescriptor(); |
| 126 | } |
| 127 | |
| 128 | template<typename INTERFACE> |
| 129 | IBinder* BnInterface<INTERFACE>::onAsBinder() |
| 130 | { |
| 131 | return this; |
| 132 | } |
| 133 | |
| 134 | template<typename INTERFACE> |
| 135 | inline BpInterface<INTERFACE>::BpInterface(const sp<IBinder>& remote) |
| 136 | : BpRefBase(remote) |
| 137 | { |
| 138 | } |
| 139 | |
| 140 | template<typename INTERFACE> |
| 141 | inline IBinder* BpInterface<INTERFACE>::onAsBinder() |
| 142 | { |
| 143 | return remote(); |
| 144 | } |
| 145 | |
| 146 | // ---------------------------------------------------------------------- |
| 147 | |
| 148 | }; // namespace android |
| 149 | |
| 150 | #endif // ANDROID_IINTERFACE_H |