Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e886cfdd authored by Jiyong Park's avatar Jiyong Park
Browse files

Libbinder APIs are guarded

The following API was added for the API level 30 and beyond.  Currently,
its existence is tested using the null check which is done regardless of
the min sdk version of the compilation unit. (which in turn required us
to mark the API symbol weak regardless of the min sdk version.)

API 30:
* AStatus_getDescription
* AIBinder_Class_setHandleShellCommand

API 31:
* AParcel_getDataSize
* AParcel_reset
* AParcel_appendFrom

Now, we have a better way of testing the API availability;
__builtin_available. The null check is replaced with the call to the
compiler-provided macro which determines if the code is running in a
version of OS where the API is known to exist.

Also, this change removes __ANDROID_API__ guards in favor of
__INTRODUCED_IN macros.

Bug: 150860940
Bug: 134795810
Test: m

Change-Id: I78d2fdc0bf60bd8c5a82ec58884422dc534e752f
parent 38effc4f
Loading
Loading
Loading
Loading
+7 −4
Original line number Diff line number Diff line
@@ -259,11 +259,14 @@ class ScopedAStatus : public impl::ScopedAResource<AStatus*, AStatus_delete, nul
    const char* getMessage() const { return AStatus_getMessage(get()); }

    std::string getDescription() const {
        if (__builtin_available(android 30, *)) {
            const char* cStr = AStatus_getDescription(get());
            std::string ret = cStr;
            AStatus_deleteDescription(cStr);
            return ret;
        }
        return "(not available)";
    }

    /**
     * Convenience methods for creating scoped statuses.
+1 −1
Original line number Diff line number Diff line
@@ -246,7 +246,7 @@ AIBinder_Class* ICInterface::defineClass(const char* interfaceDescriptor,
    // ourselves. The defaults are harmless.
    AIBinder_Class_setOnDump(clazz, ICInterfaceData::onDump);
#ifdef HAS_BINDER_SHELL_COMMAND
    if (AIBinder_Class_setHandleShellCommand != nullptr) {
    if (__builtin_available(android 30, *)) {
        AIBinder_Class_setHandleShellCommand(clazz, ICInterfaceData::handleShellCommand);
    }
#endif
+39 −10
Original line number Diff line number Diff line
@@ -51,14 +51,27 @@ class AParcelableHolder {

    binder_status_t writeToParcel(AParcel* parcel) const {
        RETURN_ON_FAILURE(AParcel_writeInt32(parcel, static_cast<int32_t>(this->mStability)));
        RETURN_ON_FAILURE(AParcel_writeInt32(parcel, AParcel_getDataSize(this->mParcel.get())));
        RETURN_ON_FAILURE(AParcel_appendFrom(this->mParcel.get(), parcel, 0,
                                             AParcel_getDataSize(this->mParcel.get())));
        if (__builtin_available(android 31, *)) {
            int32_t size = AParcel_getDataSize(this->mParcel.get());
            RETURN_ON_FAILURE(AParcel_writeInt32(parcel, size));
        } else {
            return STATUS_INVALID_OPERATION;
        }
        if (__builtin_available(android 31, *)) {
            int32_t size = AParcel_getDataSize(this->mParcel.get());
            RETURN_ON_FAILURE(AParcel_appendFrom(this->mParcel.get(), parcel, 0, size));
        } else {
            return STATUS_INVALID_OPERATION;
        }
        return STATUS_OK;
    }

    binder_status_t readFromParcel(const AParcel* parcel) {
        if (__builtin_available(android 31, *)) {
            AParcel_reset(mParcel.get());
        } else {
            return STATUS_INVALID_OPERATION;
        }

        RETURN_ON_FAILURE(AParcel_readInt32(parcel, &this->mStability));
        int32_t dataSize;
@@ -74,7 +87,11 @@ class AParcelableHolder {
            return STATUS_BAD_VALUE;
        }

        if (__builtin_available(android 31, *)) {
            status = AParcel_appendFrom(parcel, mParcel.get(), dataStartPos, dataSize);
        } else {
            status = STATUS_INVALID_OPERATION;
        }
        if (status != STATUS_OK) {
            return status;
        }
@@ -86,7 +103,11 @@ class AParcelableHolder {
        if (this->mStability > T::_aidl_stability) {
            return STATUS_BAD_VALUE;
        }
        if (__builtin_available(android 31, *)) {
            AParcel_reset(mParcel.get());
        } else {
            return STATUS_INVALID_OPERATION;
        }
        AParcel_writeString(mParcel.get(), T::descriptor, strlen(T::descriptor));
        p.writeToParcel(mParcel.get());
        return STATUS_OK;
@@ -96,10 +117,14 @@ class AParcelableHolder {
    binder_status_t getParcelable(std::optional<T>* ret) const {
        const std::string parcelableDesc(T::descriptor);
        AParcel_setDataPosition(mParcel.get(), 0);
        if (__builtin_available(android 31, *)) {
            if (AParcel_getDataSize(mParcel.get()) == 0) {
                *ret = std::nullopt;
                return STATUS_OK;
            }
        } else {
            return STATUS_INVALID_OPERATION;
        }
        std::string parcelableDescInParcel;
        binder_status_t status = AParcel_readString(mParcel.get(), &parcelableDescInParcel);
        if (status != STATUS_OK || parcelableDesc != parcelableDescInParcel) {
@@ -115,7 +140,11 @@ class AParcelableHolder {
        return STATUS_OK;
    }

    void reset() { AParcel_reset(mParcel.get()); }
    void reset() {
        if (__builtin_available(android 31, *)) {
            AParcel_reset(mParcel.get());
        }
    }

    inline bool operator!=(const AParcelableHolder& rhs) const { return this != &rhs; }
    inline bool operator<(const AParcelableHolder& rhs) const { return this < &rhs; }
+0 −2
Original line number Diff line number Diff line
@@ -1120,7 +1120,6 @@ binder_status_t AParcel_readByteArray(const AParcel* parcel, void* arrayData,
// @END-PRIMITIVE-READ-WRITE

#endif  //__ANDROID_API__ >= 29
#if __ANDROID_API__ >= 31
/**
 * Reset the parcel to the initial status.
 *
@@ -1166,7 +1165,6 @@ binder_status_t AParcel_appendFrom(const AParcel* from, AParcel* to, int32_t sta
 * \return A parcel which is not related to any IBinder objects.
 */
AParcel* AParcel_create() __INTRODUCED_IN(31);
#endif  //__ANDROID_API__ >= 31
__END_DECLS

/** @} */