blob: dc572ac953470e1aeedefb8c82f50d4c42516e59 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-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
Steven Morelandc7a871e2020-11-10 21:56:57 +000017#pragma once
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080018
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070019#include <binder/Binder.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020
Jooyung Han81087392020-05-13 17:24:09 +090021#include <assert.h>
22
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023namespace android {
24
25// ----------------------------------------------------------------------
26
27class IInterface : public virtual RefBase
28{
29public:
Mathias Agopian83c04462009-05-22 19:00:22 -070030 IInterface();
Marco Nelissen2ea926b2014-11-14 08:01:01 -080031 static sp<IBinder> asBinder(const IInterface*);
32 static sp<IBinder> asBinder(const sp<IInterface>&);
33
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034protected:
Mathias Agopian83c04462009-05-22 19:00:22 -070035 virtual ~IInterface();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036 virtual IBinder* onAsBinder() = 0;
37};
38
39// ----------------------------------------------------------------------
40
Steven Moreland21560ab2019-11-20 17:24:21 -080041/**
42 * If this is a local object and the descriptor matches, this will return the
43 * actual local object which is implementing the interface. Otherwise, this will
44 * return a proxy to the interface without checking the interface descriptor.
45 * This means that subsequent calls may fail with BAD_TYPE.
46 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047template<typename INTERFACE>
48inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj)
49{
50 return INTERFACE::asInterface(obj);
51}
52
Steven Moreland21560ab2019-11-20 17:24:21 -080053/**
54 * This is the same as interface_cast, except that it always checks to make sure
55 * the descriptor matches, and if it doesn't match, it will return nullptr.
56 */
57template<typename INTERFACE>
58inline sp<INTERFACE> checked_interface_cast(const sp<IBinder>& obj)
59{
60 if (obj->getInterfaceDescriptor() != INTERFACE::descriptor) {
61 return nullptr;
62 }
63
64 return interface_cast<INTERFACE>(obj);
65}
66
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067// ----------------------------------------------------------------------
68
69template<typename INTERFACE>
70class BnInterface : public INTERFACE, public BBinder
71{
72public:
73 virtual sp<IInterface> queryLocalInterface(const String16& _descriptor);
Mathias Agopian83c04462009-05-22 19:00:22 -070074 virtual const String16& getInterfaceDescriptor() const;
Devin Moore3e3161d2022-06-23 00:35:53 +000075 typedef INTERFACE BaseInterface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080076
77protected:
78 virtual IBinder* onAsBinder();
79};
80
81// ----------------------------------------------------------------------
82
83template<typename INTERFACE>
84class BpInterface : public INTERFACE, public BpRefBase
85{
86public:
Chih-Hung Hsiehe3ab0e82016-09-01 11:44:54 -070087 explicit BpInterface(const sp<IBinder>& remote);
Devin Moore3e3161d2022-06-23 00:35:53 +000088 typedef INTERFACE BaseInterface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089
90protected:
91 virtual IBinder* onAsBinder();
92};
93
94// ----------------------------------------------------------------------
95
Devin Mooref19b0922022-01-14 21:06:34 +000096#define DECLARE_META_INTERFACE(INTERFACE) \
97public: \
98 static const ::android::String16 descriptor; \
99 static ::android::sp<I##INTERFACE> asInterface(const ::android::sp<::android::IBinder>& obj); \
100 virtual const ::android::String16& getInterfaceDescriptor() const; \
101 I##INTERFACE(); \
102 virtual ~I##INTERFACE(); \
103 static bool setDefaultImpl(::android::sp<I##INTERFACE> impl); \
104 static const ::android::sp<I##INTERFACE>& getDefaultImpl(); \
105 \
106private: \
107 static ::android::sp<I##INTERFACE> default_impl; \
108 \
109public:
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110
Vic Yang5cb73912019-09-24 20:05:14 -0700111#define __IINTF_CONCAT(x, y) (x ## y)
Ivan Lozano9d92e6a2019-11-25 08:25:14 -0800112
113#ifndef DO_NOT_CHECK_MANUAL_BINDER_INTERFACES
114
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115#define IMPLEMENT_META_INTERFACE(INTERFACE, NAME) \
Ivan Lozano9d92e6a2019-11-25 08:25:14 -0800116 static_assert(internal::allowedManualInterface(NAME), \
117 "b/64223827: Manually written binder interfaces are " \
118 "considered error prone and frequently have bugs. " \
119 "The preferred way to add interfaces is to define " \
120 "an .aidl file to auto-generate the interface. If " \
121 "an interface must be manually written, add its " \
122 "name to the whitelist."); \
123 DO_NOT_DIRECTLY_USE_ME_IMPLEMENT_META_INTERFACE(INTERFACE, NAME) \
124
125#else
126
127#define IMPLEMENT_META_INTERFACE(INTERFACE, NAME) \
128 DO_NOT_DIRECTLY_USE_ME_IMPLEMENT_META_INTERFACE(INTERFACE, NAME) \
129
130#endif
131
Jooyung Han9e2cf7f2021-10-29 18:28:47 +0900132// Macro to be used by both IMPLEMENT_META_INTERFACE and IMPLEMENT_META_NESTED_INTERFACE
133#define DO_NOT_DIRECTLY_USE_ME_IMPLEMENT_META_INTERFACE0(ITYPE, INAME, BPTYPE) \
134 const ::android::String16& ITYPE::getInterfaceDescriptor() const { return ITYPE::descriptor; } \
135 ::android::sp<ITYPE> ITYPE::asInterface(const ::android::sp<::android::IBinder>& obj) { \
136 ::android::sp<ITYPE> intr; \
137 if (obj != nullptr) { \
138 intr = ::android::sp<ITYPE>::cast(obj->queryLocalInterface(ITYPE::descriptor)); \
139 if (intr == nullptr) { \
140 intr = ::android::sp<BPTYPE>::make(obj); \
141 } \
142 } \
143 return intr; \
144 } \
Devin Mooref19b0922022-01-14 21:06:34 +0000145 ::android::sp<ITYPE> ITYPE::default_impl; \
146 bool ITYPE::setDefaultImpl(::android::sp<ITYPE> impl) { \
Jooyung Han9e2cf7f2021-10-29 18:28:47 +0900147 /* Only one user of this interface can use this function */ \
148 /* at a time. This is a heuristic to detect if two different */ \
149 /* users in the same process use this function. */ \
150 assert(!ITYPE::default_impl); \
151 if (impl) { \
152 ITYPE::default_impl = std::move(impl); \
153 return true; \
154 } \
155 return false; \
156 } \
Devin Mooref19b0922022-01-14 21:06:34 +0000157 const ::android::sp<ITYPE>& ITYPE::getDefaultImpl() { return ITYPE::default_impl; } \
Jooyung Han9e2cf7f2021-10-29 18:28:47 +0900158 ITYPE::INAME() {} \
159 ITYPE::~INAME() {}
Mathias Agopian83c04462009-05-22 19:00:22 -0700160
Jooyung Han9e2cf7f2021-10-29 18:28:47 +0900161// Macro for an interface type.
162#define DO_NOT_DIRECTLY_USE_ME_IMPLEMENT_META_INTERFACE(INTERFACE, NAME) \
163 const ::android::StaticString16 I##INTERFACE##_descriptor_static_str16( \
164 __IINTF_CONCAT(u, NAME)); \
165 const ::android::String16 I##INTERFACE::descriptor(I##INTERFACE##_descriptor_static_str16); \
166 DO_NOT_DIRECTLY_USE_ME_IMPLEMENT_META_INTERFACE0(I##INTERFACE, I##INTERFACE, Bp##INTERFACE)
167
168// Macro for "nested" interface type.
169// For example,
170// class Parent .. { class INested .. { }; };
171// DO_NOT_DIRECTLY_USE_ME_IMPLEMENT_META_NESTED_INTERFACE(Parent, Nested, "Parent.INested")
172#define DO_NOT_DIRECTLY_USE_ME_IMPLEMENT_META_NESTED_INTERFACE(PARENT, INTERFACE, NAME) \
173 const ::android::String16 PARENT::I##INTERFACE::descriptor(NAME); \
174 DO_NOT_DIRECTLY_USE_ME_IMPLEMENT_META_INTERFACE0(PARENT::I##INTERFACE, I##INTERFACE, \
175 PARENT::Bp##INTERFACE)
Mathias Agopian83c04462009-05-22 19:00:22 -0700176
177#define CHECK_INTERFACE(interface, data, reply) \
Stephen Hinesf326c9b2019-01-09 15:49:59 -0800178 do { \
179 if (!(data).checkInterface(this)) { return PERMISSION_DENIED; } \
180 } while (false) \
Mathias Agopian83c04462009-05-22 19:00:22 -0700181
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182
183// ----------------------------------------------------------------------
Mathias Agopian83c04462009-05-22 19:00:22 -0700184// No user-serviceable parts after this...
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185
186template<typename INTERFACE>
187inline sp<IInterface> BnInterface<INTERFACE>::queryLocalInterface(
188 const String16& _descriptor)
189{
Steven Morelandb0983182021-04-02 03:14:04 +0000190 if (_descriptor == INTERFACE::descriptor) return sp<IInterface>::fromExisting(this);
Yi Kong87d465c2018-07-24 01:14:06 -0700191 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192}
193
194template<typename INTERFACE>
Mathias Agopian83c04462009-05-22 19:00:22 -0700195inline const String16& BnInterface<INTERFACE>::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800196{
197 return INTERFACE::getInterfaceDescriptor();
198}
199
200template<typename INTERFACE>
201IBinder* BnInterface<INTERFACE>::onAsBinder()
202{
203 return this;
204}
205
206template<typename INTERFACE>
207inline BpInterface<INTERFACE>::BpInterface(const sp<IBinder>& remote)
208 : BpRefBase(remote)
209{
210}
211
212template<typename INTERFACE>
213inline IBinder* BpInterface<INTERFACE>::onAsBinder()
214{
215 return remote();
216}
Yifan Hongd5ee11a2018-05-25 12:57:04 -0700217
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800218// ----------------------------------------------------------------------
219
Ivan Lozano9d92e6a2019-11-25 08:25:14 -0800220namespace internal {
221constexpr const char* const kManualInterfaces[] = {
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800222 "android.app.IActivityManager",
223 "android.app.IUidObserver",
224 "android.drm.IDrm",
225 "android.dvr.IVsyncCallback",
226 "android.dvr.IVsyncService",
227 "android.gfx.tests.ICallback",
228 "android.gfx.tests.IIPCTest",
229 "android.gfx.tests.ISafeInterfaceTest",
230 "android.graphicsenv.IGpuService",
231 "android.gui.IConsumerListener",
232 "android.gui.IGraphicBufferConsumer",
Huihong Luoffee3bc2023-01-17 16:14:35 +0000233 "android.gui.ITransactionComposerListener",
Huihong Luod3d8f8e2022-03-08 14:48:46 -0800234 "android.gui.SensorEventConnection",
235 "android.gui.SensorServer",
236 "android.hardware.ICamera",
237 "android.hardware.ICameraClient",
238 "android.hardware.ICameraRecordingProxy",
239 "android.hardware.ICameraRecordingProxyListener",
240 "android.hardware.ICrypto",
241 "android.hardware.IOMXObserver",
242 "android.hardware.IStreamListener",
243 "android.hardware.IStreamSource",
244 "android.media.IAudioService",
245 "android.media.IDataSource",
246 "android.media.IDrmClient",
247 "android.media.IMediaCodecList",
248 "android.media.IMediaDrmService",
249 "android.media.IMediaExtractor",
250 "android.media.IMediaExtractorService",
251 "android.media.IMediaHTTPConnection",
252 "android.media.IMediaHTTPService",
253 "android.media.IMediaLogService",
254 "android.media.IMediaMetadataRetriever",
255 "android.media.IMediaMetricsService",
256 "android.media.IMediaPlayer",
257 "android.media.IMediaPlayerClient",
258 "android.media.IMediaPlayerService",
259 "android.media.IMediaRecorder",
260 "android.media.IMediaRecorderClient",
261 "android.media.IMediaResourceMonitor",
262 "android.media.IMediaSource",
263 "android.media.IRemoteDisplay",
264 "android.media.IRemoteDisplayClient",
265 "android.media.IResourceManagerClient",
266 "android.media.IResourceManagerService",
267 "android.os.IComplexTypeInterface",
268 "android.os.IPermissionController",
269 "android.os.IPingResponder",
270 "android.os.IProcessInfoService",
271 "android.os.ISchedulingPolicyService",
272 "android.os.IStringConstants",
273 "android.os.storage.IObbActionListener",
274 "android.os.storage.IStorageEventListener",
275 "android.os.storage.IStorageManager",
276 "android.os.storage.IStorageShutdownObserver",
277 "android.service.vr.IPersistentVrStateCallbacks",
278 "android.service.vr.IVrManager",
279 "android.service.vr.IVrStateCallbacks",
280 "android.ui.ISurfaceComposer",
281 "android.utils.IMemory",
282 "android.utils.IMemoryHeap",
283 "com.android.car.procfsinspector.IProcfsInspector",
284 "com.android.internal.app.IAppOpsCallback",
285 "com.android.internal.app.IAppOpsService",
286 "com.android.internal.app.IBatteryStats",
287 "com.android.internal.os.IResultReceiver",
288 "com.android.internal.os.IShellCallback",
289 "drm.IDrmManagerService",
290 "drm.IDrmServiceListener",
291 "IAAudioClient",
292 "IAAudioService",
293 "VtsFuzzer",
294 nullptr,
Ivan Lozano9d92e6a2019-11-25 08:25:14 -0800295};
296
297constexpr const char* const kDownstreamManualInterfaces[] = {
298 // Add downstream interfaces here.
299 nullptr,
300};
301
302constexpr bool equals(const char* a, const char* b) {
303 if (*a != *b) return false;
304 if (*a == '\0') return true;
305 return equals(a + 1, b + 1);
306}
307
308constexpr bool inList(const char* a, const char* const* whitelist) {
309 if (*whitelist == nullptr) return false;
310 if (equals(a, *whitelist)) return true;
311 return inList(a, whitelist + 1);
312}
313
314constexpr bool allowedManualInterface(const char* name) {
315 return inList(name, kManualInterfaces) ||
316 inList(name, kDownstreamManualInterfaces);
317}
318
319} // namespace internal
Steven Moreland61ff8492019-09-26 16:05:45 -0700320} // namespace android