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