OSDN Git Service

Add readStrongBinder that takes an interface
authorCasey Dahlin <sadmac@google.com>
Wed, 28 Oct 2015 01:33:56 +0000 (18:33 -0700)
committerAlex Deymo <deymo@google.com>
Wed, 18 Nov 2015 01:49:23 +0000 (17:49 -0800)
We use a template function to let us pass arbitrary interfaces, all of which
can be cast to IBinder.

Test: unit tests
Bug: 23600713
Signed-off-by: Casey Dahlin <sadmac@google.com>
(cherry picked from commit f0c13772d95486d98f034361883b2415bb26a614)

Change-Id: I69e8b534343c299c25cfb591dbdf122860c9ccc5

include/binder/Parcel.h
libs/binder/Parcel.cpp

index be3e9b3..ae76ffb 100644 (file)
@@ -27,6 +27,8 @@
 #include <utils/Flattenable.h>
 #include <linux/binder.h>
 
+#include <binder/IInterface.h>
+
 // ---------------------------------------------------------------------------
 namespace android {
 
@@ -196,8 +198,12 @@ public:
     status_t            readString16(String16* pArg) const;
     const char16_t*     readString16Inplace(size_t* outLen) const;
     sp<IBinder>         readStrongBinder() const;
+    status_t            readStrongBinder(sp<IBinder>* val) const;
     wp<IBinder>         readWeakBinder() const;
 
+    template<typename T>
+    status_t readStrongBinder(sp<T>* val) const;
+
     status_t            readByteVector(std::vector<int8_t>* val) const;
     status_t            readInt32Vector(std::vector<int32_t>* val) const;
     status_t            readInt64Vector(std::vector<int64_t>* val) const;
@@ -432,6 +438,22 @@ status_t Parcel::read(LightFlattenable<T>& val) const {
     return NO_ERROR;
 }
 
+template<typename T>
+status_t Parcel::readStrongBinder(sp<T>* val) const {
+    sp<IBinder> tmp;
+    status_t ret = readStrongBinder(&tmp);
+
+    if (ret == OK) {
+        *val = interface_cast<T>(tmp);
+
+        if (val->get() == nullptr) {
+            return UNKNOWN_ERROR;
+        }
+    }
+
+    return ret;
+}
+
 // ---------------------------------------------------------------------------
 
 inline TextOutput& operator<<(TextOutput& to, const Parcel& parcel)
index 29cd8cb..4c80f43 100644 (file)
@@ -1805,10 +1805,15 @@ const char16_t* Parcel::readString16Inplace(size_t* outLen) const
     return NULL;
 }
 
+status_t Parcel::readStrongBinder(sp<IBinder>* val) const
+{
+    return unflatten_binder(ProcessState::self(), *this, val);
+}
+
 sp<IBinder> Parcel::readStrongBinder() const
 {
     sp<IBinder> val;
-    unflatten_binder(ProcessState::self(), *this, &val);
+    readStrongBinder(&val);
     return val;
 }