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 #pragma once 18 19 #include <binder/Binder.h> 20 #include <binder/Common.h> 21 22 #include <assert.h> 23 24 namespace android { 25 26 // ---------------------------------------------------------------------- 27 28 class LIBBINDER_EXPORTED IInterface : public virtual RefBase { 29 public: 30 IInterface(); 31 static sp<IBinder> asBinder(const IInterface*); 32 static sp<IBinder> asBinder(const sp<IInterface>&); 33 34 protected: 35 virtual ~IInterface(); 36 virtual IBinder* onAsBinder() = 0; 37 }; 38 39 // ---------------------------------------------------------------------- 40 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 */ 47 template<typename INTERFACE> 48 inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj) 49 { 50 return INTERFACE::asInterface(obj); 51 } 52 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 67 // ---------------------------------------------------------------------- 68 69 template <typename INTERFACE> 70 class LIBBINDER_EXPORTED BnInterface : public INTERFACE, public BBinder { 71 public: 72 virtual sp<IInterface> queryLocalInterface(const String16& _descriptor); 73 virtual const String16& getInterfaceDescriptor() const; 74 typedef INTERFACE BaseInterface; 75 76 protected: 77 virtual IBinder* onAsBinder(); 78 }; 79 80 // ---------------------------------------------------------------------- 81 82 template <typename INTERFACE> 83 class LIBBINDER_EXPORTED BpInterface : public INTERFACE, public BpRefBase { 84 public: 85 explicit BpInterface(const sp<IBinder>& remote); 86 typedef INTERFACE BaseInterface; 87 88 protected: 89 virtual IBinder* onAsBinder(); 90 }; 91 92 // ---------------------------------------------------------------------- 93 94 #define DECLARE_META_INTERFACE(INTERFACE) \ 95 public: \ 96 static const ::android::String16 descriptor; \ 97 static ::android::sp<I##INTERFACE> asInterface(const ::android::sp<::android::IBinder>& obj); \ 98 virtual const ::android::String16& getInterfaceDescriptor() const; \ 99 I##INTERFACE(); \ 100 virtual ~I##INTERFACE(); \ 101 static bool setDefaultImpl(::android::sp<I##INTERFACE> impl); \ 102 static const ::android::sp<I##INTERFACE>& getDefaultImpl(); \ 103 \ 104 private: \ 105 static ::android::sp<I##INTERFACE> default_impl; \ 106 \ 107 public: 108 109 #define __IINTF_CONCAT(x, y) (x ## y) 110 111 #ifndef DO_NOT_CHECK_MANUAL_BINDER_INTERFACES 112 113 #define IMPLEMENT_META_INTERFACE(INTERFACE, NAME) \ 114 static_assert(internal::allowedManualInterface(NAME), \ 115 "b/64223827: Manually written binder interfaces are " \ 116 "considered error prone and frequently have bugs. " \ 117 "The preferred way to add interfaces is to define " \ 118 "an .aidl file to auto-generate the interface. If " \ 119 "an interface must be manually written, add its " \ 120 "name to the allowlist."); \ 121 DO_NOT_DIRECTLY_USE_ME_IMPLEMENT_META_INTERFACE(INTERFACE, NAME) 122 123 #else 124 125 #define IMPLEMENT_META_INTERFACE(INTERFACE, NAME) \ 126 DO_NOT_DIRECTLY_USE_ME_IMPLEMENT_META_INTERFACE(INTERFACE, NAME) \ 127 128 #endif 129 130 // Macro to be used by both IMPLEMENT_META_INTERFACE and IMPLEMENT_META_NESTED_INTERFACE 131 #define DO_NOT_DIRECTLY_USE_ME_IMPLEMENT_META_INTERFACE0(ITYPE, INAME, BPTYPE) \ 132 const ::android::String16& ITYPE::getInterfaceDescriptor() const { return ITYPE::descriptor; } \ 133 ::android::sp<ITYPE> ITYPE::asInterface(const ::android::sp<::android::IBinder>& obj) { \ 134 ::android::sp<ITYPE> intr; \ 135 if (obj != nullptr) { \ 136 intr = ::android::sp<ITYPE>::cast(obj->queryLocalInterface(ITYPE::descriptor)); \ 137 if (intr == nullptr) { \ 138 intr = ::android::sp<BPTYPE>::make(obj); \ 139 } \ 140 } \ 141 return intr; \ 142 } \ 143 ::android::sp<ITYPE> ITYPE::default_impl; \ 144 bool ITYPE::setDefaultImpl(::android::sp<ITYPE> impl) { \ 145 /* Only one user of this interface can use this function */ \ 146 /* at a time. This is a heuristic to detect if two different */ \ 147 /* users in the same process use this function. */ \ 148 assert(!ITYPE::default_impl); \ 149 if (impl) { \ 150 ITYPE::default_impl = std::move(impl); \ 151 return true; \ 152 } \ 153 return false; \ 154 } \ 155 const ::android::sp<ITYPE>& ITYPE::getDefaultImpl() { return ITYPE::default_impl; } \ 156 ITYPE::INAME() {} \ 157 ITYPE::~INAME() {} 158 159 // Macro for an interface type. 160 #define DO_NOT_DIRECTLY_USE_ME_IMPLEMENT_META_INTERFACE(INTERFACE, NAME) \ 161 const ::android::StaticString16 I##INTERFACE##_descriptor_static_str16( \ 162 __IINTF_CONCAT(u, NAME)); \ 163 const ::android::String16 I##INTERFACE::descriptor(I##INTERFACE##_descriptor_static_str16); \ 164 DO_NOT_DIRECTLY_USE_ME_IMPLEMENT_META_INTERFACE0(I##INTERFACE, I##INTERFACE, Bp##INTERFACE) 165 166 // Macro for "nested" interface type. 167 // For example, 168 // class Parent .. { class INested .. { }; }; 169 // DO_NOT_DIRECTLY_USE_ME_IMPLEMENT_META_NESTED_INTERFACE(Parent, Nested, "Parent.INested") 170 #define DO_NOT_DIRECTLY_USE_ME_IMPLEMENT_META_NESTED_INTERFACE(PARENT, INTERFACE, NAME) \ 171 const ::android::String16 PARENT::I##INTERFACE::descriptor(NAME); \ 172 DO_NOT_DIRECTLY_USE_ME_IMPLEMENT_META_INTERFACE0(PARENT::I##INTERFACE, I##INTERFACE, \ 173 PARENT::Bp##INTERFACE) 174 175 #define CHECK_INTERFACE(interface, data, reply) \ 176 do { \ 177 if (!(data).checkInterface(this)) { return PERMISSION_DENIED; } \ 178 } while (false) \ 179 180 181 // ---------------------------------------------------------------------- 182 // No user-serviceable parts after this... 183 184 template<typename INTERFACE> 185 inline sp<IInterface> BnInterface<INTERFACE>::queryLocalInterface( 186 const String16& _descriptor) 187 { 188 if (_descriptor == INTERFACE::descriptor) return sp<IInterface>::fromExisting(this); 189 return nullptr; 190 } 191 192 template<typename INTERFACE> 193 inline const String16& BnInterface<INTERFACE>::getInterfaceDescriptor() const 194 { 195 return INTERFACE::getInterfaceDescriptor(); 196 } 197 198 template<typename INTERFACE> 199 IBinder* BnInterface<INTERFACE>::onAsBinder() 200 { 201 return this; 202 } 203 204 template<typename INTERFACE> 205 inline BpInterface<INTERFACE>::BpInterface(const sp<IBinder>& remote) 206 : BpRefBase(remote) 207 { 208 } 209 210 template<typename INTERFACE> 211 inline IBinder* BpInterface<INTERFACE>::onAsBinder() 212 { 213 return remote(); 214 } 215 216 // ---------------------------------------------------------------------- 217 218 namespace internal { 219 constexpr const char* const kManualInterfaces[] = { 220 "android.app.IActivityManager", 221 "android.app.IUidObserver", 222 "android.drm.IDrm", 223 "android.gfx.tests.ICallback", 224 "android.gfx.tests.IIPCTest", 225 "android.gfx.tests.ISafeInterfaceTest", 226 "android.graphicsenv.IGpuService", 227 "android.gui.IConsumerListener", 228 "android.gui.IGraphicBufferConsumer", 229 "android.gui.ITransactionComposerListener", 230 "android.gui.SensorEventConnection", 231 "android.gui.SensorServer", 232 "android.hardware.ICamera", 233 "android.hardware.ICameraClient", 234 "android.hardware.ICameraRecordingProxy", 235 "android.hardware.ICameraRecordingProxyListener", 236 "android.hardware.ICrypto", 237 "android.hardware.IOMXObserver", 238 "android.hardware.IStreamListener", 239 "android.hardware.IStreamSource", 240 "android.media.IAudioService", 241 "android.media.IDataSource", 242 "android.media.IDrmClient", 243 "android.media.IMediaCodecList", 244 "android.media.IMediaDrmService", 245 "android.media.IMediaExtractor", 246 "android.media.IMediaExtractorService", 247 "android.media.IMediaHTTPConnection", 248 "android.media.IMediaHTTPService", 249 "android.media.IMediaLogService", 250 "android.media.IMediaMetadataRetriever", 251 "android.media.IMediaMetricsService", 252 "android.media.IMediaPlayer", 253 "android.media.IMediaPlayerClient", 254 "android.media.IMediaPlayerService", 255 "android.media.IMediaRecorder", 256 "android.media.IMediaRecorderClient", 257 "android.media.IMediaResourceMonitor", 258 "android.media.IMediaSource", 259 "android.media.IRemoteDisplay", 260 "android.media.IRemoteDisplayClient", 261 "android.media.IResourceManagerClient", 262 "android.media.IResourceManagerService", 263 "android.os.IComplexTypeInterface", 264 "android.os.IPermissionController", 265 "android.os.IProcessInfoService", 266 "android.os.ISchedulingPolicyService", 267 "android.os.storage.IObbActionListener", 268 "android.os.storage.IStorageEventListener", 269 "android.os.storage.IStorageManager", 270 "android.os.storage.IStorageShutdownObserver", 271 "android.ui.ISurfaceComposer", 272 "android.utils.IMemory", 273 "android.utils.IMemoryHeap", 274 "com.android.car.procfsinspector.IProcfsInspector", 275 "com.android.internal.app.IAppOpsCallback", 276 "com.android.internal.app.IAppOpsService", 277 "com.android.internal.app.IBatteryStats", 278 "com.android.internal.os.IResultReceiver", 279 "com.android.internal.os.IShellCallback", 280 "drm.IDrmManagerService", 281 "drm.IDrmServiceListener", 282 nullptr, 283 }; 284 285 constexpr const char* const kDownstreamManualInterfaces[] = { 286 // Add downstream interfaces here. 287 nullptr, 288 }; 289 290 constexpr bool equals(const char* a, const char* b) { 291 if (*a != *b) return false; 292 if (*a == '\0') return true; 293 return equals(a + 1, b + 1); 294 } 295 296 constexpr bool inList(const char* a, const char* const* allowlist) { 297 if (*allowlist == nullptr) return false; 298 if (equals(a, *allowlist)) return true; 299 return inList(a, allowlist + 1); 300 } 301 302 constexpr bool allowedManualInterface(const char* name) { 303 return inList(name, kManualInterfaces) || 304 inList(name, kDownstreamManualInterfaces); 305 } 306 307 } // namespace internal 308 } // namespace android 309