blob: 988508eabac8d6eb5b593681fba8b42aff8d0d9e [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;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075
76protected:
Pawin Vongmasabd768952019-02-08 17:38:49 -080077 typedef INTERFACE BaseInterface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078 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);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088
89protected:
Pawin Vongmasabd768952019-02-08 17:38:49 -080090 typedef INTERFACE BaseInterface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091 virtual IBinder* onAsBinder();
92};
93
94// ----------------------------------------------------------------------
95
96#define DECLARE_META_INTERFACE(INTERFACE) \
Jiyong Park4acdf912018-07-04 13:36:54 +090097public: \
Joe Onoratoc0f74802016-11-22 17:43:22 -080098 static const ::android::String16 descriptor; \
99 static ::android::sp<I##INTERFACE> asInterface( \
100 const ::android::sp<::android::IBinder>& obj); \
101 virtual const ::android::String16& getInterfaceDescriptor() const; \
Mathias Agopian83c04462009-05-22 19:00:22 -0700102 I##INTERFACE(); \
103 virtual ~I##INTERFACE(); \
Jiyong Park4acdf912018-07-04 13:36:54 +0900104 static bool setDefaultImpl(std::unique_ptr<I##INTERFACE> impl); \
105 static const std::unique_ptr<I##INTERFACE>& getDefaultImpl(); \
106private: \
107 static std::unique_ptr<I##INTERFACE> default_impl; \
108public: \
Mathias Agopian83c04462009-05-22 19:00:22 -0700109
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
132#define DO_NOT_DIRECTLY_USE_ME_IMPLEMENT_META_INTERFACE(INTERFACE, NAME)\
Vic Yang5cb73912019-09-24 20:05:14 -0700133 const ::android::StaticString16 \
134 I##INTERFACE##_descriptor_static_str16(__IINTF_CONCAT(u, NAME));\
135 const ::android::String16 I##INTERFACE::descriptor( \
136 I##INTERFACE##_descriptor_static_str16); \
Joe Onoratoc0f74802016-11-22 17:43:22 -0800137 const ::android::String16& \
Michael Richardsona3a884d2009-11-03 17:01:28 -0500138 I##INTERFACE::getInterfaceDescriptor() const { \
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 return I##INTERFACE::descriptor; \
140 } \
Joe Onoratoc0f74802016-11-22 17:43:22 -0800141 ::android::sp<I##INTERFACE> I##INTERFACE::asInterface( \
142 const ::android::sp<::android::IBinder>& obj) \
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143 { \
Joe Onoratoc0f74802016-11-22 17:43:22 -0800144 ::android::sp<I##INTERFACE> intr; \
Yi Kong87d465c2018-07-24 01:14:06 -0700145 if (obj != nullptr) { \
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146 intr = static_cast<I##INTERFACE*>( \
147 obj->queryLocalInterface( \
148 I##INTERFACE::descriptor).get()); \
Yi Kong87d465c2018-07-24 01:14:06 -0700149 if (intr == nullptr) { \
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800150 intr = new Bp##INTERFACE(obj); \
151 } \
152 } \
153 return intr; \
154 } \
Jiyong Park4acdf912018-07-04 13:36:54 +0900155 std::unique_ptr<I##INTERFACE> I##INTERFACE::default_impl; \
156 bool I##INTERFACE::setDefaultImpl(std::unique_ptr<I##INTERFACE> impl)\
157 { \
Jooyung Han81087392020-05-13 17:24:09 +0900158 /* Only one user of this interface can use this function */ \
159 /* at a time. This is a heuristic to detect if two different */ \
160 /* users in the same process use this function. */ \
161 assert(!I##INTERFACE::default_impl); \
162 if (impl) { \
Jiyong Park4acdf912018-07-04 13:36:54 +0900163 I##INTERFACE::default_impl = std::move(impl); \
164 return true; \
165 } \
166 return false; \
167 } \
168 const std::unique_ptr<I##INTERFACE>& I##INTERFACE::getDefaultImpl() \
169 { \
170 return I##INTERFACE::default_impl; \
171 } \
Mathias Agopian83c04462009-05-22 19:00:22 -0700172 I##INTERFACE::I##INTERFACE() { } \
173 I##INTERFACE::~I##INTERFACE() { } \
174
175
176#define CHECK_INTERFACE(interface, data, reply) \
Stephen Hinesf326c9b2019-01-09 15:49:59 -0800177 do { \
178 if (!(data).checkInterface(this)) { return PERMISSION_DENIED; } \
179 } while (false) \
Mathias Agopian83c04462009-05-22 19:00:22 -0700180
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181
182// ----------------------------------------------------------------------
Mathias Agopian83c04462009-05-22 19:00:22 -0700183// No user-serviceable parts after this...
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184
185template<typename INTERFACE>
186inline sp<IInterface> BnInterface<INTERFACE>::queryLocalInterface(
187 const String16& _descriptor)
188{
189 if (_descriptor == INTERFACE::descriptor) return this;
Yi Kong87d465c2018-07-24 01:14:06 -0700190 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800191}
192
193template<typename INTERFACE>
Mathias Agopian83c04462009-05-22 19:00:22 -0700194inline const String16& BnInterface<INTERFACE>::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195{
196 return INTERFACE::getInterfaceDescriptor();
197}
198
199template<typename INTERFACE>
200IBinder* BnInterface<INTERFACE>::onAsBinder()
201{
202 return this;
203}
204
205template<typename INTERFACE>
206inline BpInterface<INTERFACE>::BpInterface(const sp<IBinder>& remote)
207 : BpRefBase(remote)
208{
209}
210
211template<typename INTERFACE>
212inline IBinder* BpInterface<INTERFACE>::onAsBinder()
213{
214 return remote();
215}
Yifan Hongd5ee11a2018-05-25 12:57:04 -0700216
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217// ----------------------------------------------------------------------
218
Ivan Lozano9d92e6a2019-11-25 08:25:14 -0800219namespace internal {
220constexpr const char* const kManualInterfaces[] = {
221 "android.app.IActivityManager",
222 "android.app.IUidObserver",
223 "android.drm.IDrm",
224 "android.dvr.IVsyncCallback",
225 "android.dvr.IVsyncService",
226 "android.gfx.tests.ICallback",
227 "android.gfx.tests.IIPCTest",
228 "android.gfx.tests.ISafeInterfaceTest",
229 "android.graphicsenv.IGpuService",
230 "android.gui.DisplayEventConnection",
231 "android.gui.IConsumerListener",
232 "android.gui.IGraphicBufferConsumer",
233 "android.gui.IRegionSamplingListener",
234 "android.gui.ITransactionComposerListener",
chaviw85bf55a2020-08-17 17:54:24 -0700235 "android.gui.IScreenCaptureListener",
Ivan Lozano9d92e6a2019-11-25 08:25:14 -0800236 "android.gui.SensorEventConnection",
237 "android.gui.SensorServer",
238 "android.hardware.ICamera",
239 "android.hardware.ICameraClient",
240 "android.hardware.ICameraRecordingProxy",
241 "android.hardware.ICameraRecordingProxyListener",
242 "android.hardware.ICrypto",
243 "android.hardware.IOMXObserver",
244 "android.hardware.ISoundTrigger",
245 "android.hardware.ISoundTriggerClient",
246 "android.hardware.ISoundTriggerHwService",
247 "android.hardware.IStreamListener",
248 "android.hardware.IStreamSource",
Ivan Lozano9d92e6a2019-11-25 08:25:14 -0800249 "android.media.IAudioFlinger",
250 "android.media.IAudioFlingerClient",
251 "android.media.IAudioPolicyService",
252 "android.media.IAudioPolicyServiceClient",
253 "android.media.IAudioService",
254 "android.media.IAudioTrack",
255 "android.media.IDataSource",
256 "android.media.IDrmClient",
Ivan Lozano9d92e6a2019-11-25 08:25:14 -0800257 "android.media.IMediaCodecList",
258 "android.media.IMediaDrmService",
259 "android.media.IMediaExtractor",
260 "android.media.IMediaExtractorService",
261 "android.media.IMediaHTTPConnection",
262 "android.media.IMediaHTTPService",
263 "android.media.IMediaLogService",
264 "android.media.IMediaMetadataRetriever",
Ray Essickb08ff672019-12-07 06:16:53 -0800265 "android.media.IMediaMetricsService",
Ivan Lozano9d92e6a2019-11-25 08:25:14 -0800266 "android.media.IMediaPlayer",
267 "android.media.IMediaPlayerClient",
268 "android.media.IMediaPlayerService",
269 "android.media.IMediaRecorder",
270 "android.media.IMediaRecorderClient",
271 "android.media.IMediaResourceMonitor",
272 "android.media.IMediaSource",
273 "android.media.IRemoteDisplay",
274 "android.media.IRemoteDisplayClient",
275 "android.media.IResourceManagerClient",
276 "android.media.IResourceManagerService",
277 "android.os.IComplexTypeInterface",
278 "android.os.IPermissionController",
279 "android.os.IPingResponder",
Ivan Lozano9d92e6a2019-11-25 08:25:14 -0800280 "android.os.IProcessInfoService",
281 "android.os.ISchedulingPolicyService",
282 "android.os.IStringConstants",
283 "android.os.storage.IObbActionListener",
284 "android.os.storage.IStorageEventListener",
285 "android.os.storage.IStorageManager",
286 "android.os.storage.IStorageShutdownObserver",
287 "android.service.vr.IPersistentVrStateCallbacks",
288 "android.service.vr.IVrManager",
289 "android.service.vr.IVrStateCallbacks",
290 "android.ui.ISurfaceComposer",
291 "android.ui.ISurfaceComposerClient",
292 "android.utils.IMemory",
293 "android.utils.IMemoryHeap",
294 "com.android.car.procfsinspector.IProcfsInspector",
295 "com.android.internal.app.IAppOpsCallback",
296 "com.android.internal.app.IAppOpsService",
297 "com.android.internal.app.IBatteryStats",
298 "com.android.internal.os.IResultReceiver",
299 "com.android.internal.os.IShellCallback",
300 "drm.IDrmManagerService",
301 "drm.IDrmServiceListener",
302 "IAAudioClient",
303 "IAAudioService",
304 "VtsFuzzer",
305 nullptr,
306};
307
308constexpr const char* const kDownstreamManualInterfaces[] = {
309 // Add downstream interfaces here.
310 nullptr,
311};
312
313constexpr bool equals(const char* a, const char* b) {
314 if (*a != *b) return false;
315 if (*a == '\0') return true;
316 return equals(a + 1, b + 1);
317}
318
319constexpr bool inList(const char* a, const char* const* whitelist) {
320 if (*whitelist == nullptr) return false;
321 if (equals(a, *whitelist)) return true;
322 return inList(a, whitelist + 1);
323}
324
325constexpr bool allowedManualInterface(const char* name) {
326 return inList(name, kManualInterfaces) ||
327 inList(name, kDownstreamManualInterfaces);
328}
329
330} // namespace internal
Steven Moreland61ff8492019-09-26 16:05:45 -0700331} // namespace android