The Android Open Source Project | edbf3b6 | 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 | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 21 | #include <binder/Binder.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | |
Jooyung Han | 8108739 | 2020-05-13 17:24:09 +0900 | [diff] [blame] | 23 | #include <assert.h> |
| 24 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | namespace android { |
| 26 | |
| 27 | // ---------------------------------------------------------------------- |
| 28 | |
| 29 | class IInterface : public virtual RefBase |
| 30 | { |
| 31 | public: |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 32 | IInterface(); |
Marco Nelissen | 2ea926b | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 33 | static sp<IBinder> asBinder(const IInterface*); |
| 34 | static sp<IBinder> asBinder(const sp<IInterface>&); |
| 35 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | protected: |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 37 | virtual ~IInterface(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | virtual IBinder* onAsBinder() = 0; |
| 39 | }; |
| 40 | |
| 41 | // ---------------------------------------------------------------------- |
| 42 | |
Steven Moreland | 21560ab | 2019-11-20 17:24:21 -0800 | [diff] [blame] | 43 | /** |
| 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 Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | template<typename INTERFACE> |
| 50 | inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj) |
| 51 | { |
| 52 | return INTERFACE::asInterface(obj); |
| 53 | } |
| 54 | |
Steven Moreland | 21560ab | 2019-11-20 17:24:21 -0800 | [diff] [blame] | 55 | /** |
| 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 | */ |
| 59 | template<typename INTERFACE> |
| 60 | inline 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 Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | // ---------------------------------------------------------------------- |
| 70 | |
| 71 | template<typename INTERFACE> |
| 72 | class BnInterface : public INTERFACE, public BBinder |
| 73 | { |
| 74 | public: |
| 75 | virtual sp<IInterface> queryLocalInterface(const String16& _descriptor); |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 76 | virtual const String16& getInterfaceDescriptor() const; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | |
| 78 | protected: |
Pawin Vongmasa | bd76895 | 2019-02-08 17:38:49 -0800 | [diff] [blame] | 79 | typedef INTERFACE BaseInterface; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | virtual IBinder* onAsBinder(); |
| 81 | }; |
| 82 | |
| 83 | // ---------------------------------------------------------------------- |
| 84 | |
| 85 | template<typename INTERFACE> |
| 86 | class BpInterface : public INTERFACE, public BpRefBase |
| 87 | { |
| 88 | public: |
Chih-Hung Hsieh | e3ab0e8 | 2016-09-01 11:44:54 -0700 | [diff] [blame] | 89 | explicit BpInterface(const sp<IBinder>& remote); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | |
| 91 | protected: |
Pawin Vongmasa | bd76895 | 2019-02-08 17:38:49 -0800 | [diff] [blame] | 92 | typedef INTERFACE BaseInterface; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | virtual IBinder* onAsBinder(); |
| 94 | }; |
| 95 | |
| 96 | // ---------------------------------------------------------------------- |
| 97 | |
| 98 | #define DECLARE_META_INTERFACE(INTERFACE) \ |
Jiyong Park | 4acdf91 | 2018-07-04 13:36:54 +0900 | [diff] [blame] | 99 | public: \ |
Joe Onorato | c0f7480 | 2016-11-22 17:43:22 -0800 | [diff] [blame] | 100 | 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 Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 104 | I##INTERFACE(); \ |
| 105 | virtual ~I##INTERFACE(); \ |
Jiyong Park | 4acdf91 | 2018-07-04 13:36:54 +0900 | [diff] [blame] | 106 | static bool setDefaultImpl(std::unique_ptr<I##INTERFACE> impl); \ |
| 107 | static const std::unique_ptr<I##INTERFACE>& getDefaultImpl(); \ |
| 108 | private: \ |
| 109 | static std::unique_ptr<I##INTERFACE> default_impl; \ |
| 110 | public: \ |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 111 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | |
Vic Yang | 5cb7391 | 2019-09-24 20:05:14 -0700 | [diff] [blame] | 113 | #define __IINTF_CONCAT(x, y) (x ## y) |
Ivan Lozano | 9d92e6a | 2019-11-25 08:25:14 -0800 | [diff] [blame] | 114 | |
| 115 | #ifndef DO_NOT_CHECK_MANUAL_BINDER_INTERFACES |
| 116 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | #define IMPLEMENT_META_INTERFACE(INTERFACE, NAME) \ |
Ivan Lozano | 9d92e6a | 2019-11-25 08:25:14 -0800 | [diff] [blame] | 118 | 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 Yang | 5cb7391 | 2019-09-24 20:05:14 -0700 | [diff] [blame] | 135 | 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 Onorato | c0f7480 | 2016-11-22 17:43:22 -0800 | [diff] [blame] | 139 | const ::android::String16& \ |
Michael Richardson | a3a884d | 2009-11-03 17:01:28 -0500 | [diff] [blame] | 140 | I##INTERFACE::getInterfaceDescriptor() const { \ |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | return I##INTERFACE::descriptor; \ |
| 142 | } \ |
Joe Onorato | c0f7480 | 2016-11-22 17:43:22 -0800 | [diff] [blame] | 143 | ::android::sp<I##INTERFACE> I##INTERFACE::asInterface( \ |
| 144 | const ::android::sp<::android::IBinder>& obj) \ |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | { \ |
Joe Onorato | c0f7480 | 2016-11-22 17:43:22 -0800 | [diff] [blame] | 146 | ::android::sp<I##INTERFACE> intr; \ |
Yi Kong | 87d465c | 2018-07-24 01:14:06 -0700 | [diff] [blame] | 147 | if (obj != nullptr) { \ |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | intr = static_cast<I##INTERFACE*>( \ |
| 149 | obj->queryLocalInterface( \ |
| 150 | I##INTERFACE::descriptor).get()); \ |
Yi Kong | 87d465c | 2018-07-24 01:14:06 -0700 | [diff] [blame] | 151 | if (intr == nullptr) { \ |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | intr = new Bp##INTERFACE(obj); \ |
| 153 | } \ |
| 154 | } \ |
| 155 | return intr; \ |
| 156 | } \ |
Jiyong Park | 4acdf91 | 2018-07-04 13:36:54 +0900 | [diff] [blame] | 157 | std::unique_ptr<I##INTERFACE> I##INTERFACE::default_impl; \ |
| 158 | bool I##INTERFACE::setDefaultImpl(std::unique_ptr<I##INTERFACE> impl)\ |
| 159 | { \ |
Jooyung Han | 8108739 | 2020-05-13 17:24:09 +0900 | [diff] [blame] | 160 | /* 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 Park | 4acdf91 | 2018-07-04 13:36:54 +0900 | [diff] [blame] | 165 | 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 Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 174 | I##INTERFACE::I##INTERFACE() { } \ |
| 175 | I##INTERFACE::~I##INTERFACE() { } \ |
| 176 | |
| 177 | |
| 178 | #define CHECK_INTERFACE(interface, data, reply) \ |
Stephen Hines | f326c9b | 2019-01-09 15:49:59 -0800 | [diff] [blame] | 179 | do { \ |
| 180 | if (!(data).checkInterface(this)) { return PERMISSION_DENIED; } \ |
| 181 | } while (false) \ |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 182 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | |
| 184 | // ---------------------------------------------------------------------- |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 185 | // No user-serviceable parts after this... |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | |
| 187 | template<typename INTERFACE> |
| 188 | inline sp<IInterface> BnInterface<INTERFACE>::queryLocalInterface( |
| 189 | const String16& _descriptor) |
| 190 | { |
| 191 | if (_descriptor == INTERFACE::descriptor) return this; |
Yi Kong | 87d465c | 2018-07-24 01:14:06 -0700 | [diff] [blame] | 192 | return nullptr; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | template<typename INTERFACE> |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 196 | inline const String16& BnInterface<INTERFACE>::getInterfaceDescriptor() const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | { |
| 198 | return INTERFACE::getInterfaceDescriptor(); |
| 199 | } |
| 200 | |
| 201 | template<typename INTERFACE> |
| 202 | IBinder* BnInterface<INTERFACE>::onAsBinder() |
| 203 | { |
| 204 | return this; |
| 205 | } |
| 206 | |
| 207 | template<typename INTERFACE> |
| 208 | inline BpInterface<INTERFACE>::BpInterface(const sp<IBinder>& remote) |
| 209 | : BpRefBase(remote) |
| 210 | { |
| 211 | } |
| 212 | |
| 213 | template<typename INTERFACE> |
| 214 | inline IBinder* BpInterface<INTERFACE>::onAsBinder() |
| 215 | { |
| 216 | return remote(); |
| 217 | } |
Yifan Hong | d5ee11a | 2018-05-25 12:57:04 -0700 | [diff] [blame] | 218 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | // ---------------------------------------------------------------------- |
| 220 | |
Ivan Lozano | 9d92e6a | 2019-11-25 08:25:14 -0800 | [diff] [blame] | 221 | namespace internal { |
| 222 | constexpr 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 Lozano | 9d92e6a | 2019-11-25 08:25:14 -0800 | [diff] [blame] | 262 | "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 Essick | b08ff67 | 2019-12-07 06:16:53 -0800 | [diff] [blame] | 270 | "android.media.IMediaMetricsService", |
Ivan Lozano | 9d92e6a | 2019-11-25 08:25:14 -0800 | [diff] [blame] | 271 | "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 | |
| 314 | constexpr const char* const kDownstreamManualInterfaces[] = { |
| 315 | // Add downstream interfaces here. |
| 316 | nullptr, |
| 317 | }; |
| 318 | |
| 319 | constexpr 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 | |
| 325 | constexpr 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 | |
| 331 | constexpr bool allowedManualInterface(const char* name) { |
| 332 | return inList(name, kManualInterfaces) || |
| 333 | inList(name, kDownstreamManualInterfaces); |
| 334 | } |
| 335 | |
| 336 | } // namespace internal |
Steven Moreland | 61ff849 | 2019-09-26 16:05:45 -0700 | [diff] [blame] | 337 | } // namespace android |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | |
| 339 | #endif // ANDROID_IINTERFACE_H |