diff options
author | Steven Moreland <smoreland@google.com> | 2019-11-25 18:06:24 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2019-11-25 18:06:24 +0000 |
commit | 8eec43905a9e58fef95aaa54844c71625ca2e5ed (patch) | |
tree | 7d7b38184f4c9bb64ef24a432c40329ac9b88775 | |
parent | 945fa5c957ebcd5461abee409fc5c1a6f6de6e42 (diff) | |
parent | 21560ab7fa447aef2f90816e250aa1e06ba5948a (diff) | |
download | native-8eec43905a9e58fef95aaa54844c71625ca2e5ed.tar.gz |
Merge "libbinder: checked_interface_cast"
-rw-r--r-- | libs/binder/include/binder/IInterface.h | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/libs/binder/include/binder/IInterface.h b/libs/binder/include/binder/IInterface.h index 28ffa48e32..8d72a6b98b 100644 --- a/libs/binder/include/binder/IInterface.h +++ b/libs/binder/include/binder/IInterface.h @@ -38,12 +38,32 @@ protected: // ---------------------------------------------------------------------- +/** + * If this is a local object and the descriptor matches, this will return the + * actual local object which is implementing the interface. Otherwise, this will + * return a proxy to the interface without checking the interface descriptor. + * This means that subsequent calls may fail with BAD_TYPE. + */ template<typename INTERFACE> inline sp<INTERFACE> interface_cast(const sp<IBinder>& obj) { return INTERFACE::asInterface(obj); } +/** + * This is the same as interface_cast, except that it always checks to make sure + * the descriptor matches, and if it doesn't match, it will return nullptr. + */ +template<typename INTERFACE> +inline sp<INTERFACE> checked_interface_cast(const sp<IBinder>& obj) +{ + if (obj->getInterfaceDescriptor() != INTERFACE::descriptor) { + return nullptr; + } + + return interface_cast<INTERFACE>(obj); +} + // ---------------------------------------------------------------------- template<typename INTERFACE> |