blob: 71161549517e01fad4f0722ffc8e7aa2d87a6020 [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
17//
18#ifndef ANDROID_IINTERFACE_H
19#define ANDROID_IINTERFACE_H
20
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070021#include <binder/Binder.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022
Jooyung Han81087392020-05-13 17:24:09 +090023#include <assert.h>
24
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025namespace android {
26
27// ----------------------------------------------------------------------
28
29class IInterface : public virtual RefBase
30{
31public:
Mathias Agopian83c04462009-05-22 19:00:22 -070032 IInterface();
Marco Nelissen2ea926b2014-11-14 08:01:01 -080033 static sp<IBinder> asBinder(const IInterface*);
34 static sp<IBinder> asBinder(const sp<IInterface>&);
35
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036protected:
Mathias Agopian83c04462009-05-22 19:00:22 -070037 virtual ~IInterface();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038 virtual IBinder* onAsBinder() = 0;
39};
40
41// ----------------------------------------------------------------------
42
Steven Moreland21560ab2019-11-20 17:24:21 -080043/**
44 * If this is a local object and the descriptor matches, this will return the
45 * actual local object which is implementing the interface. Otherwise, this will
46 * return a proxy to the interface without checking the interface descriptor.
47 * This means that subsequent calls may fail with BAD_TYPE.
48 */
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049template<typename INTERFACE>
50inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj)
51{
52 return INTERFACE::asInterface(obj);
53}
54
Steven Moreland21560ab2019-11-20 17:24:21 -080055/**
56 * This is the same as interface_cast, except that it always checks to make sure
57 * the descriptor matches, and if it doesn't match, it will return nullptr.
58 */
59template<typename INTERFACE>
60inline sp<INTERFACE> checked_interface_cast(const sp<IBinder>& obj)
61{
62 if (obj->getInterfaceDescriptor() != INTERFACE::descriptor) {
63 return nullptr;
64 }
65
66 return interface_cast<INTERFACE>(obj);
67}
68
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080069// ----------------------------------------------------------------------
70
71template<typename INTERFACE>
72class BnInterface : public INTERFACE, public BBinder
73{
74public:
75 virtual sp<IInterface> queryLocalInterface(const String16& _descriptor);
Mathias Agopian83c04462009-05-22 19:00:22 -070076 virtual const String16& getInterfaceDescriptor() const;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077
78protected:
Pawin Vongmasabd768952019-02-08 17:38:49 -080079 typedef INTERFACE BaseInterface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080 virtual IBinder* onAsBinder();
81};
82
83// ----------------------------------------------------------------------
84
85template<typename INTERFACE>
86class BpInterface : public INTERFACE, public BpRefBase
87{
88public:
Chih-Hung Hsiehe3ab0e82016-09-01 11:44:54 -070089 explicit BpInterface(const sp<IBinder>& remote);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090
91protected:
Pawin Vongmasabd768952019-02-08 17:38:49 -080092 typedef INTERFACE BaseInterface;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 virtual IBinder* onAsBinder();
94};
95
96// ----------------------------------------------------------------------
97
98#define DECLARE_META_INTERFACE(INTERFACE) \
Jiyong Park4acdf912018-07-04 13:36:54 +090099public: \
Joe Onoratoc0f74802016-11-22 17:43:22 -0800100 static const ::android::String16 descriptor; \
101 static ::android::sp<I##INTERFACE> asInterface( \
102 const ::android::sp<::android::IBinder>& obj); \
103 virtual const ::android::String16& getInterfaceDescriptor() const; \
Mathias Agopian83c04462009-05-22 19:00:22 -0700104 I##INTERFACE(); \
105 virtual ~I##INTERFACE(); \
Jiyong Park4acdf912018-07-04 13:36:54 +0900106 static bool setDefaultImpl(std::unique_ptr<I##INTERFACE> impl); \
107 static const std::unique_ptr<I##INTERFACE>& getDefaultImpl(); \
108private: \
109 static std::unique_ptr<I##INTERFACE> default_impl; \
110public: \
Mathias Agopian83c04462009-05-22 19:00:22 -0700111
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112
Vic Yang5cb73912019-09-24 20:05:14 -0700113#define __IINTF_CONCAT(x, y) (x ## y)
Ivan Lozano9d92e6a2019-11-25 08:25:14 -0800114
115#ifndef DO_NOT_CHECK_MANUAL_BINDER_INTERFACES
116
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117#define IMPLEMENT_META_INTERFACE(INTERFACE, NAME) \
Ivan Lozano9d92e6a2019-11-25 08:25:14 -0800118 static_assert(internal::allowedManualInterface(NAME), \
119 "b/64223827: Manually written binder interfaces are " \
120 "considered error prone and frequently have bugs. " \
121 "The preferred way to add interfaces is to define " \
122 "an .aidl file to auto-generate the interface. If " \
123 "an interface must be manually written, add its " \
124 "name to the whitelist."); \
125 DO_NOT_DIRECTLY_USE_ME_IMPLEMENT_META_INTERFACE(INTERFACE, NAME) \
126
127#else
128
129#define IMPLEMENT_META_INTERFACE(INTERFACE, NAME) \
130 DO_NOT_DIRECTLY_USE_ME_IMPLEMENT_META_INTERFACE(INTERFACE, NAME) \
131
132#endif
133
134#define DO_NOT_DIRECTLY_USE_ME_IMPLEMENT_META_INTERFACE(INTERFACE, NAME)\
Vic Yang5cb73912019-09-24 20:05:14 -0700135 const ::android::StaticString16 \
136 I##INTERFACE##_descriptor_static_str16(__IINTF_CONCAT(u, NAME));\
137 const ::android::String16 I##INTERFACE::descriptor( \
138 I##INTERFACE##_descriptor_static_str16); \
Joe Onoratoc0f74802016-11-22 17:43:22 -0800139 const ::android::String16& \
Michael Richardsona3a884d2009-11-03 17:01:28 -0500140 I##INTERFACE::getInterfaceDescriptor() const { \
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800141 return I##INTERFACE::descriptor; \
142 } \
Joe Onoratoc0f74802016-11-22 17:43:22 -0800143 ::android::sp<I##INTERFACE> I##INTERFACE::asInterface( \
144 const ::android::sp<::android::IBinder>& obj) \
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145 { \
Joe Onoratoc0f74802016-11-22 17:43:22 -0800146 ::android::sp<I##INTERFACE> intr; \
Yi Kong87d465c2018-07-24 01:14:06 -0700147 if (obj != nullptr) { \
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148 intr = static_cast<I##INTERFACE*>( \
149 obj->queryLocalInterface( \
150 I##INTERFACE::descriptor).get()); \
Yi Kong87d465c2018-07-24 01:14:06 -0700151 if (intr == nullptr) { \
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152 intr = new Bp##INTERFACE(obj); \
153 } \
154 } \
155 return intr; \
156 } \
Jiyong Park4acdf912018-07-04 13:36:54 +0900157 std::unique_ptr<I##INTERFACE> I##INTERFACE::default_impl; \
158 bool I##INTERFACE::setDefaultImpl(std::unique_ptr<I##INTERFACE> impl)\
159 { \
Jooyung Han81087392020-05-13 17:24:09 +0900160 /* Only one user of this interface can use this function */ \
161 /* at a time. This is a heuristic to detect if two different */ \
162 /* users in the same process use this function. */ \
163 assert(!I##INTERFACE::default_impl); \
164 if (impl) { \
Jiyong Park4acdf912018-07-04 13:36:54 +0900165 I##INTERFACE::default_impl = std::move(impl); \
166 return true; \
167 } \
168 return false; \
169 } \
170 const std::unique_ptr<I##INTERFACE>& I##INTERFACE::getDefaultImpl() \
171 { \
172 return I##INTERFACE::default_impl; \
173 } \
Mathias Agopian83c04462009-05-22 19:00:22 -0700174 I##INTERFACE::I##INTERFACE() { } \
175 I##INTERFACE::~I##INTERFACE() { } \
176
177
178#define CHECK_INTERFACE(interface, data, reply) \
Stephen Hinesf326c9b2019-01-09 15:49:59 -0800179 do { \
180 if (!(data).checkInterface(this)) { return PERMISSION_DENIED; } \
181 } while (false) \
Mathias Agopian83c04462009-05-22 19:00:22 -0700182
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183
184// ----------------------------------------------------------------------
Mathias Agopian83c04462009-05-22 19:00:22 -0700185// No user-serviceable parts after this...
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186
187template<typename INTERFACE>
188inline sp<IInterface> BnInterface<INTERFACE>::queryLocalInterface(
189 const String16& _descriptor)
190{
191 if (_descriptor == INTERFACE::descriptor) return this;
Yi Kong87d465c2018-07-24 01:14:06 -0700192 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193}
194
195template<typename INTERFACE>
Mathias Agopian83c04462009-05-22 19:00:22 -0700196inline const String16& BnInterface<INTERFACE>::getInterfaceDescriptor() const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197{
198 return INTERFACE::getInterfaceDescriptor();
199}
200
201template<typename INTERFACE>
202IBinder* BnInterface<INTERFACE>::onAsBinder()
203{
204 return this;
205}
206
207template<typename INTERFACE>
208inline BpInterface<INTERFACE>::BpInterface(const sp<IBinder>& remote)
209 : BpRefBase(remote)
210{
211}
212
213template<typename INTERFACE>
214inline IBinder* BpInterface<INTERFACE>::onAsBinder()
215{
216 return remote();
217}
Yifan Hongd5ee11a2018-05-25 12:57:04 -0700218
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219// ----------------------------------------------------------------------
220
Ivan Lozano9d92e6a2019-11-25 08:25:14 -0800221namespace internal {
222constexpr const char* const kManualInterfaces[] = {
223 "android.app.IActivityManager",
224 "android.app.IUidObserver",
225 "android.drm.IDrm",
226 "android.dvr.IVsyncCallback",
227 "android.dvr.IVsyncService",
228 "android.gfx.tests.ICallback",
229 "android.gfx.tests.IIPCTest",
230 "android.gfx.tests.ISafeInterfaceTest",
231 "android.graphicsenv.IGpuService",
232 "android.gui.DisplayEventConnection",
233 "android.gui.IConsumerListener",
234 "android.gui.IGraphicBufferConsumer",
235 "android.gui.IRegionSamplingListener",
236 "android.gui.ITransactionComposerListener",
237 "android.gui.SensorEventConnection",
238 "android.gui.SensorServer",
239 "android.hardware.ICamera",
240 "android.hardware.ICameraClient",
241 "android.hardware.ICameraRecordingProxy",
242 "android.hardware.ICameraRecordingProxyListener",
243 "android.hardware.ICrypto",
244 "android.hardware.IOMXObserver",
245 "android.hardware.ISoundTrigger",
246 "android.hardware.ISoundTriggerClient",
247 "android.hardware.ISoundTriggerHwService",
248 "android.hardware.IStreamListener",
249 "android.hardware.IStreamSource",
250 "android.input.IInputFlinger",
251 "android.input.ISetInputWindowsListener",
252 "android.media.IAudioFlinger",
253 "android.media.IAudioFlingerClient",
254 "android.media.IAudioPolicyService",
255 "android.media.IAudioPolicyServiceClient",
256 "android.media.IAudioService",
257 "android.media.IAudioTrack",
258 "android.media.IDataSource",
259 "android.media.IDrmClient",
260 "android.media.IEffect",
261 "android.media.IEffectClient",
Ivan Lozano9d92e6a2019-11-25 08:25:14 -0800262 "android.media.IMediaCodecList",
263 "android.media.IMediaDrmService",
264 "android.media.IMediaExtractor",
265 "android.media.IMediaExtractorService",
266 "android.media.IMediaHTTPConnection",
267 "android.media.IMediaHTTPService",
268 "android.media.IMediaLogService",
269 "android.media.IMediaMetadataRetriever",
Ray Essickb08ff672019-12-07 06:16:53 -0800270 "android.media.IMediaMetricsService",
Ivan Lozano9d92e6a2019-11-25 08:25:14 -0800271 "android.media.IMediaPlayer",
272 "android.media.IMediaPlayerClient",
273 "android.media.IMediaPlayerService",
274 "android.media.IMediaRecorder",
275 "android.media.IMediaRecorderClient",
276 "android.media.IMediaResourceMonitor",
277 "android.media.IMediaSource",
278 "android.media.IRemoteDisplay",
279 "android.media.IRemoteDisplayClient",
280 "android.media.IResourceManagerClient",
281 "android.media.IResourceManagerService",
282 "android.os.IComplexTypeInterface",
283 "android.os.IPermissionController",
284 "android.os.IPingResponder",
285 "android.os.IPowerManager",
286 "android.os.IProcessInfoService",
287 "android.os.ISchedulingPolicyService",
288 "android.os.IStringConstants",
289 "android.os.storage.IObbActionListener",
290 "android.os.storage.IStorageEventListener",
291 "android.os.storage.IStorageManager",
292 "android.os.storage.IStorageShutdownObserver",
293 "android.service.vr.IPersistentVrStateCallbacks",
294 "android.service.vr.IVrManager",
295 "android.service.vr.IVrStateCallbacks",
296 "android.ui.ISurfaceComposer",
297 "android.ui.ISurfaceComposerClient",
298 "android.utils.IMemory",
299 "android.utils.IMemoryHeap",
300 "com.android.car.procfsinspector.IProcfsInspector",
301 "com.android.internal.app.IAppOpsCallback",
302 "com.android.internal.app.IAppOpsService",
303 "com.android.internal.app.IBatteryStats",
304 "com.android.internal.os.IResultReceiver",
305 "com.android.internal.os.IShellCallback",
306 "drm.IDrmManagerService",
307 "drm.IDrmServiceListener",
308 "IAAudioClient",
309 "IAAudioService",
310 "VtsFuzzer",
311 nullptr,
312};
313
314constexpr const char* const kDownstreamManualInterfaces[] = {
315 // Add downstream interfaces here.
316 nullptr,
317};
318
319constexpr bool equals(const char* a, const char* b) {
320 if (*a != *b) return false;
321 if (*a == '\0') return true;
322 return equals(a + 1, b + 1);
323}
324
325constexpr bool inList(const char* a, const char* const* whitelist) {
326 if (*whitelist == nullptr) return false;
327 if (equals(a, *whitelist)) return true;
328 return inList(a, whitelist + 1);
329}
330
331constexpr bool allowedManualInterface(const char* name) {
332 return inList(name, kManualInterfaces) ||
333 inList(name, kDownstreamManualInterfaces);
334}
335
336} // namespace internal
Steven Moreland61ff8492019-09-26 16:05:45 -0700337} // namespace android
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800338
339#endif // ANDROID_IINTERFACE_H