Merge "[SF] Fix unittest crash"
diff --git a/libs/binder/ndk/include_ndk/android/binder_auto_utils.h b/libs/binder/ndk/include_ndk/android/binder_auto_utils.h
index 11b1e8e..d947e7b 100644
--- a/libs/binder/ndk/include_ndk/android/binder_auto_utils.h
+++ b/libs/binder/ndk/include_ndk/android/binder_auto_utils.h
@@ -34,39 +34,39 @@
 
 #include <cstddef>
 
-namespace android {
+namespace ndk {
 
 /**
  * Represents one strong pointer to an AIBinder object.
  */
-class AutoAIBinder {
+class SpAIBinder {
 public:
     /**
      * Takes ownership of one strong refcount of binder.
      */
-    explicit AutoAIBinder(AIBinder* binder = nullptr) : mBinder(binder) {}
+    explicit SpAIBinder(AIBinder* binder = nullptr) : mBinder(binder) {}
 
     /**
-     * Convenience operator for implicitly constructing an AutoAIBinder from nullptr. This is not
+     * Convenience operator for implicitly constructing an SpAIBinder from nullptr. This is not
      * explicit because it is not taking ownership of anything.
      */
-    AutoAIBinder(std::nullptr_t) : AutoAIBinder() {}
+    SpAIBinder(std::nullptr_t) : SpAIBinder() {}
 
     /**
      * This will delete the underlying object if it exists. See operator=.
      */
-    AutoAIBinder(const AutoAIBinder& other) { *this = other; }
+    SpAIBinder(const SpAIBinder& other) { *this = other; }
 
     /**
      * This deletes the underlying object if it exists. See set.
      */
-    ~AutoAIBinder() { set(nullptr); }
+    ~SpAIBinder() { set(nullptr); }
 
     /**
      * This takes ownership of a binder from another AIBinder object but it does not affect the
      * ownership of that other object.
      */
-    AutoAIBinder& operator=(const AutoAIBinder& other) {
+    SpAIBinder& operator=(const SpAIBinder& other) {
         AIBinder_incStrong(other.mBinder);
         set(other.mBinder);
         return *this;
@@ -82,7 +82,7 @@
 
     /**
      * This returns the underlying binder object for transactions. If it is used to create another
-     * AutoAIBinder object, it should first be incremented.
+     * SpAIBinder object, it should first be incremented.
      */
     AIBinder* get() const { return mBinder; }
 
@@ -92,7 +92,7 @@
      * ownership to the object that is put in here.
      *
      * Recommended use is like this:
-     *   AutoAIBinder a;  // will be nullptr
+     *   SpAIBinder a;  // will be nullptr
      *   SomeInitFunction(a.getR());  // value is initialized with refcount
      *
      * Other usecases are discouraged.
@@ -108,17 +108,17 @@
  * This baseclass owns a single object, used to make various classes RAII.
  */
 template <typename T, void (*Destroy)(T*)>
-class AutoA {
+class ScopedAResource {
 public:
     /**
      * Takes ownership of t.
      */
-    explicit AutoA(T* t = nullptr) : mT(t) {}
+    explicit ScopedAResource(T* t = nullptr) : mT(t) {}
 
     /**
      * This deletes the underlying object if it exists. See set.
      */
-    ~AutoA() { set(nullptr); }
+    ~ScopedAResource() { set(nullptr); }
 
     /**
      * Takes ownership of t.
@@ -144,7 +144,7 @@
      * ownership to the object that is put in here.
      *
      * Recommended use is like this:
-     *   AutoA<T> a; // will be nullptr
+     *   ScopedAResource<T> a; // will be nullptr
      *   SomeInitFunction(a.getR()); // value is initialized with refcount
      *
      * Other usecases are discouraged.
@@ -153,12 +153,12 @@
     T** getR() { return &mT; }
 
     // copy-constructing, or move/copy assignment is disallowed
-    AutoA(const AutoA&) = delete;
-    AutoA& operator=(const AutoA&) = delete;
-    AutoA& operator=(AutoA&&) = delete;
+    ScopedAResource(const ScopedAResource&) = delete;
+    ScopedAResource& operator=(const ScopedAResource&) = delete;
+    ScopedAResource& operator=(ScopedAResource&&) = delete;
 
     // move-constructing is okay
-    AutoA(AutoA&&) = default;
+    ScopedAResource(ScopedAResource&&) = default;
 
 private:
     T* mT;
@@ -167,27 +167,27 @@
 /**
  * Convenience wrapper. See AParcel.
  */
-class AutoAParcel : public AutoA<AParcel, AParcel_delete> {
+class ScopedAParcel : public ScopedAResource<AParcel, AParcel_delete> {
 public:
     /**
      * Takes ownership of a.
      */
-    explicit AutoAParcel(AParcel* a = nullptr) : AutoA(a) {}
-    ~AutoAParcel() {}
-    AutoAParcel(AutoAParcel&&) = default;
+    explicit ScopedAParcel(AParcel* a = nullptr) : ScopedAResource(a) {}
+    ~ScopedAParcel() {}
+    ScopedAParcel(ScopedAParcel&&) = default;
 };
 
 /**
  * Convenience wrapper. See AStatus.
  */
-class AutoAStatus : public AutoA<AStatus, AStatus_delete> {
+class ScopedAStatus : public ScopedAResource<AStatus, AStatus_delete> {
 public:
     /**
      * Takes ownership of a.
      */
-    explicit AutoAStatus(AStatus* a = nullptr) : AutoA(a) {}
-    ~AutoAStatus() {}
-    AutoAStatus(AutoAStatus&&) = default;
+    explicit ScopedAStatus(AStatus* a = nullptr) : ScopedAResource(a) {}
+    ~ScopedAStatus() {}
+    ScopedAStatus(ScopedAStatus&&) = default;
 
     /**
      * See AStatus_isOk.
@@ -198,36 +198,37 @@
 /**
  * Convenience wrapper. See AIBinder_DeathRecipient.
  */
-class AutoAIBinder_DeathRecipient
-      : public AutoA<AIBinder_DeathRecipient, AIBinder_DeathRecipient_delete> {
+class ScopedAIBinder_DeathRecipient
+      : public ScopedAResource<AIBinder_DeathRecipient, AIBinder_DeathRecipient_delete> {
 public:
     /**
      * Takes ownership of a.
      */
-    explicit AutoAIBinder_DeathRecipient(AIBinder_DeathRecipient* a = nullptr) : AutoA(a) {}
-    ~AutoAIBinder_DeathRecipient() {}
-    AutoAIBinder_DeathRecipient(AutoAIBinder_DeathRecipient&&) = default;
+    explicit ScopedAIBinder_DeathRecipient(AIBinder_DeathRecipient* a = nullptr)
+          : ScopedAResource(a) {}
+    ~ScopedAIBinder_DeathRecipient() {}
+    ScopedAIBinder_DeathRecipient(ScopedAIBinder_DeathRecipient&&) = default;
 };
 
 /**
  * Convenience wrapper. See AIBinder_Weak.
  */
-class AutoAIBinder_Weak : public AutoA<AIBinder_Weak, AIBinder_Weak_delete> {
+class ScopedAIBinder_Weak : public ScopedAResource<AIBinder_Weak, AIBinder_Weak_delete> {
 public:
     /**
      * Takes ownership of a.
      */
-    explicit AutoAIBinder_Weak(AIBinder_Weak* a = nullptr) : AutoA(a) {}
-    ~AutoAIBinder_Weak() {}
-    AutoAIBinder_Weak(AutoAIBinder_Weak&&) = default;
+    explicit ScopedAIBinder_Weak(AIBinder_Weak* a = nullptr) : ScopedAResource(a) {}
+    ~ScopedAIBinder_Weak() {}
+    ScopedAIBinder_Weak(ScopedAIBinder_Weak&&) = default;
 
     /**
      * See AIBinder_Weak_promote.
      */
-    AutoAIBinder promote() { return AutoAIBinder(AIBinder_Weak_promote(get())); }
+    SpAIBinder promote() { return SpAIBinder(AIBinder_Weak_promote(get())); }
 };
 
-} // namespace android
+} // namespace ndk
 
 #endif // __cplusplus
 
diff --git a/libs/binder/ndk/include_ndk/android/binder_interface_utils.h b/libs/binder/ndk/include_ndk/android/binder_interface_utils.h
index d7e1566..6782ce0 100644
--- a/libs/binder/ndk/include_ndk/android/binder_interface_utils.h
+++ b/libs/binder/ndk/include_ndk/android/binder_interface_utils.h
@@ -35,7 +35,7 @@
 #include <memory>
 #include <mutex>
 
-namespace android {
+namespace ndk {
 
 // analog using std::shared_ptr for RefBase-like semantics
 class SharedRefBase {
@@ -56,6 +56,15 @@
         return std::static_pointer_cast<CHILD>(ref());
     }
 
+    /**
+     * Convenience method for making an object directly with a reference.
+     */
+    template<class T, class... Args>
+    static std::shared_ptr<T> make(Args&&... args) {
+        T* t = new T(std::forward<Args>(args)...);
+        return t->template ref<T>();
+    }
+
 private:
     std::once_flag mFlagThis;
     std::weak_ptr<SharedRefBase> mThis;
@@ -68,7 +77,13 @@
     virtual ~ICInterface() {}
 
     // This either returns the single existing implementation or creates a new implementation.
-    virtual AutoAIBinder asBinder() = 0;
+    virtual SpAIBinder asBinder() = 0;
+
+    /**
+     * Returns whether this interface is in a remote process. If it cannot be determined locally,
+     * this will be checked using AIBinder_isRemote.
+     */
+    virtual bool isRemote() = 0;
 };
 
 // wrapper analog to BnInterface
@@ -78,36 +93,40 @@
     BnCInterface() {}
     virtual ~BnCInterface() {}
 
-    AutoAIBinder asBinder() override;
+    SpAIBinder asBinder() override;
+
+    bool isRemote() override { return true; }
 
 protected:
     // This function should only be called by asBinder. Otherwise, there is a possibility of
     // multiple AIBinder* objects being created for the same instance of an object.
-    virtual AutoAIBinder createBinder() = 0;
+    virtual SpAIBinder createBinder() = 0;
 
 private:
     std::mutex mMutex; // for asBinder
-    AutoAIBinder_Weak mWeakBinder;
+    ScopedAIBinder_Weak mWeakBinder;
 };
 
 // wrapper analog to BpInterfae
 template <typename INTERFACE>
 class BpCInterface : public INTERFACE {
 public:
-    BpCInterface(const AutoAIBinder& binder) : mBinder(binder) {}
+    BpCInterface(const SpAIBinder& binder) : mBinder(binder) {}
     virtual ~BpCInterface() {}
 
-    AutoAIBinder asBinder() override;
+    SpAIBinder asBinder() override;
+
+    bool isRemote() override { return AIBinder_isRemote(mBinder.get()); }
 
 private:
-    AutoAIBinder mBinder;
+    SpAIBinder mBinder;
 };
 
 template <typename INTERFACE>
-AutoAIBinder BnCInterface<INTERFACE>::asBinder() {
+SpAIBinder BnCInterface<INTERFACE>::asBinder() {
     std::lock_guard<std::mutex> l(mMutex);
 
-    AutoAIBinder binder;
+    SpAIBinder binder;
     if (mWeakBinder.get() != nullptr) {
         binder.set(AIBinder_Weak_promote(mWeakBinder.get()));
     }
@@ -120,12 +139,12 @@
 }
 
 template <typename INTERFACE>
-AutoAIBinder BpCInterface<INTERFACE>::asBinder() {
+SpAIBinder BpCInterface<INTERFACE>::asBinder() {
     return mBinder;
 }
 
-#endif // __cplusplus
+} // namespace ndk
 
-} // namespace android
+#endif // __cplusplus
 
 /** @} */
diff --git a/libs/binder/ndk/include_ndk/android/binder_parcel_utils.h b/libs/binder/ndk/include_ndk/android/binder_parcel_utils.h
index 7b261d6..d3e6cae 100644
--- a/libs/binder/ndk/include_ndk/android/binder_parcel_utils.h
+++ b/libs/binder/ndk/include_ndk/android/binder_parcel_utils.h
@@ -32,6 +32,8 @@
 
 #include <string>
 
+namespace ndk {
+
 /**
  * Takes a std::string and reallocates it to the specified length. For use with AParcel_readString.
  * See use below in AParcel_readString.
@@ -66,6 +68,8 @@
                               &stringData);
 }
 
+} // namespace ndk
+
 #endif // __cplusplus
 
 /** @} */