blob: 273d92231fe2eff7854cf043353d4d206219a5c9 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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 Agopian07952722009-05-19 19:08:10 -070021#include <binder/Binder.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022
23namespace android {
24
25// ----------------------------------------------------------------------
26
27class IInterface : public virtual RefBase
28{
29public:
Mathias Agopianaaf834a2009-05-22 19:00:22 -070030 IInterface();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031 sp<IBinder> asBinder();
32 sp<const IBinder> asBinder() const;
Mathias Agopianaaf834a2009-05-22 19:00:22 -070033
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034protected:
Mathias Agopianaaf834a2009-05-22 19:00:22 -070035 virtual ~IInterface();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036 virtual IBinder* onAsBinder() = 0;
37};
38
39// ----------------------------------------------------------------------
40
41template<typename INTERFACE>
42inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj)
43{
44 return INTERFACE::asInterface(obj);
45}
46
47// ----------------------------------------------------------------------
48
49template<typename INTERFACE>
50class BnInterface : public INTERFACE, public BBinder
51{
52public:
53 virtual sp<IInterface> queryLocalInterface(const String16& _descriptor);
Mathias Agopianaaf834a2009-05-22 19:00:22 -070054 virtual const String16& getInterfaceDescriptor() const;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055
56protected:
57 virtual IBinder* onAsBinder();
58};
59
60// ----------------------------------------------------------------------
61
62template<typename INTERFACE>
63class BpInterface : public INTERFACE, public BpRefBase
64{
65public:
66 BpInterface(const sp<IBinder>& remote);
67
68protected:
69 virtual IBinder* onAsBinder();
70};
71
72// ----------------------------------------------------------------------
73
74#define DECLARE_META_INTERFACE(INTERFACE) \
75 static const String16 descriptor; \
76 static sp<I##INTERFACE> asInterface(const sp<IBinder>& obj); \
Mathias Agopianaaf834a2009-05-22 19:00:22 -070077 virtual const String16& getInterfaceDescriptor() const; \
78 I##INTERFACE(); \
79 virtual ~I##INTERFACE(); \
80
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081
82#define IMPLEMENT_META_INTERFACE(INTERFACE, NAME) \
83 const String16 I##INTERFACE::descriptor(NAME); \
Mathias Agopianaaf834a2009-05-22 19:00:22 -070084 const String16& I##INTERFACE::getInterfaceDescriptor() const { \
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 return I##INTERFACE::descriptor; \
86 } \
87 sp<I##INTERFACE> I##INTERFACE::asInterface(const sp<IBinder>& obj) \
88 { \
89 sp<I##INTERFACE> intr; \
90 if (obj != NULL) { \
91 intr = static_cast<I##INTERFACE*>( \
92 obj->queryLocalInterface( \
93 I##INTERFACE::descriptor).get()); \
94 if (intr == NULL) { \
95 intr = new Bp##INTERFACE(obj); \
96 } \
97 } \
98 return intr; \
99 } \
Mathias Agopianaaf834a2009-05-22 19:00:22 -0700100 I##INTERFACE::I##INTERFACE() { } \
101 I##INTERFACE::~I##INTERFACE() { } \
102
103
104#define CHECK_INTERFACE(interface, data, reply) \
105 if (!data.checkInterface(this)) { return PERMISSION_DENIED; } \
106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107
108// ----------------------------------------------------------------------
Mathias Agopianaaf834a2009-05-22 19:00:22 -0700109// No user-serviceable parts after this...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110
111template<typename INTERFACE>
112inline sp<IInterface> BnInterface<INTERFACE>::queryLocalInterface(
113 const String16& _descriptor)
114{
115 if (_descriptor == INTERFACE::descriptor) return this;
116 return NULL;
117}
118
119template<typename INTERFACE>
Mathias Agopianaaf834a2009-05-22 19:00:22 -0700120inline const String16& BnInterface<INTERFACE>::getInterfaceDescriptor() const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121{
122 return INTERFACE::getInterfaceDescriptor();
123}
124
125template<typename INTERFACE>
126IBinder* BnInterface<INTERFACE>::onAsBinder()
127{
128 return this;
129}
130
131template<typename INTERFACE>
132inline BpInterface<INTERFACE>::BpInterface(const sp<IBinder>& remote)
133 : BpRefBase(remote)
134{
135}
136
137template<typename INTERFACE>
138inline IBinder* BpInterface<INTERFACE>::onAsBinder()
139{
140 return remote();
141}
142
143// ----------------------------------------------------------------------
144
145}; // namespace android
146
147#endif // ANDROID_IINTERFACE_H