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

Commit cb2581e6 authored by Siarhei Vishniakou's avatar Siarhei Vishniakou
Browse files

Receive owned copy of KeyCharacterMap in InputDevice

To prevent incorrect usage of the KeyCharacterMap API's, transfer the
ownership of the native KeyCharacterMap object when creating the java
KCM object.

Bug: 274058082
Test: presubmit
Change-Id: I8e412de1791d5bb874ee709441ea12028d64857c
parent 8100afc1
Loading
Loading
Loading
Loading
+6 −8
Original line number Diff line number Diff line
@@ -42,13 +42,6 @@ jobject android_view_InputDevice_create(JNIEnv* env, const InputDeviceInfo& devi
        return NULL;
    }

    // b/274058082: Pass a copy of the key character map to avoid concurrent
    // access
    std::shared_ptr<KeyCharacterMap> map = deviceInfo.getKeyCharacterMap();
    if (map != nullptr) {
        map = std::make_shared<KeyCharacterMap>(*map);
    }

    ScopedLocalRef<jstring> descriptorObj(env,
            env->NewStringUTF(deviceInfo.getIdentifier().descriptor.c_str()));
    if (!descriptorObj.get()) {
@@ -67,9 +60,14 @@ jobject android_view_InputDevice_create(JNIEnv* env, const InputDeviceInfo& devi
                                                                  ? layoutInfo->layoutType.c_str()
                                                                  : NULL));

    std::shared_ptr<KeyCharacterMap> map = deviceInfo.getKeyCharacterMap();
    std::unique_ptr<KeyCharacterMap> mapCopy;
    if (map != nullptr) {
        mapCopy = std::make_unique<KeyCharacterMap>(*map);
    }
    ScopedLocalRef<jobject> kcmObj(env,
                                   android_view_KeyCharacterMap_create(env, deviceInfo.getId(),
                                                                       map));
                                                                       std::move(mapCopy)));
    if (!kcmObj.get()) {
        return NULL;
    }
+11 −9
Original line number Diff line number Diff line
@@ -48,7 +48,7 @@ static struct {

class NativeKeyCharacterMap {
public:
    NativeKeyCharacterMap(int32_t deviceId, std::shared_ptr<KeyCharacterMap> map)
    NativeKeyCharacterMap(int32_t deviceId, std::unique_ptr<KeyCharacterMap> map)
          : mDeviceId(deviceId), mMap(std::move(map)) {}

    ~NativeKeyCharacterMap() {
@@ -58,16 +58,18 @@ public:
        return mDeviceId;
    }

    inline const std::shared_ptr<KeyCharacterMap> getMap() const { return mMap; }
    inline const std::unique_ptr<KeyCharacterMap>& getMap() const {
        return mMap;
    }

private:
    int32_t mDeviceId;
    std::shared_ptr<KeyCharacterMap> mMap;
    std::unique_ptr<KeyCharacterMap> mMap;
};

jobject android_view_KeyCharacterMap_create(JNIEnv* env, int32_t deviceId,
                                            const std::shared_ptr<KeyCharacterMap> kcm) {
    NativeKeyCharacterMap* nativeMap = new NativeKeyCharacterMap(deviceId, kcm);
                                            std::unique_ptr<KeyCharacterMap> kcm) {
    NativeKeyCharacterMap* nativeMap = new NativeKeyCharacterMap(deviceId, std::move(kcm));
    if (!nativeMap) {
        return nullptr;
    }
@@ -91,7 +93,7 @@ static jlong nativeReadFromParcel(JNIEnv *env, jobject clazz, jobject parcelObj)
        return 0;
    }

    std::shared_ptr<KeyCharacterMap> kcm = nullptr;
    std::unique_ptr<KeyCharacterMap> kcm;
    // Check if map is a null character map
    if (parcel->readBool()) {
        kcm = KeyCharacterMap::readFromParcel(parcel);
@@ -99,7 +101,7 @@ static jlong nativeReadFromParcel(JNIEnv *env, jobject clazz, jobject parcelObj)
            return 0;
        }
    }
    NativeKeyCharacterMap* map = new NativeKeyCharacterMap(deviceId, kcm);
    NativeKeyCharacterMap* map = new NativeKeyCharacterMap(deviceId, std::move(kcm));
    return reinterpret_cast<jlong>(map);
}

@@ -230,9 +232,9 @@ static jobjectArray nativeGetEvents(JNIEnv *env, jobject clazz, jlong ptr,
}

static jboolean nativeEquals(JNIEnv* env, jobject clazz, jlong ptr1, jlong ptr2) {
    const std::shared_ptr<KeyCharacterMap>& map1 =
    const std::unique_ptr<KeyCharacterMap>& map1 =
            (reinterpret_cast<NativeKeyCharacterMap*>(ptr1))->getMap();
    const std::shared_ptr<KeyCharacterMap>& map2 =
    const std::unique_ptr<KeyCharacterMap>& map2 =
            (reinterpret_cast<NativeKeyCharacterMap*>(ptr2))->getMap();
    if (map1 == nullptr || map2 == nullptr) {
        return map1 == map2;
+1 −1
Original line number Diff line number Diff line
@@ -25,7 +25,7 @@ namespace android {

/* Creates a KeyCharacterMap object from the given information. */
extern jobject android_view_KeyCharacterMap_create(JNIEnv* env, int32_t deviceId,
                                                   const std::shared_ptr<KeyCharacterMap> kcm);
                                                   std::unique_ptr<KeyCharacterMap> kcm);

} // namespace android