From d2188aaa4f51c4bdf13a7d608a89c8ae8a7d34a7 Mon Sep 17 00:00:00 2001 From: Siarhei Vishniakou Date: Mon, 25 Feb 2019 16:06:37 -0600 Subject: [PATCH] Use unordered_map in InputEventSender There are some potential issues with the current usage of KeyedVector (see bug). To work around these issues, use the official std::unordered_map. Bug: 126161446 Test: presubmit Change-Id: I86873a3929ba068c8bb9e60699978d7726a1a444 --- core/jni/android_view_InputEventReceiver.cpp | 3 +- core/jni/android_view_InputEventSender.cpp | 66 ++++++++++---------- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/core/jni/android_view_InputEventReceiver.cpp b/core/jni/android_view_InputEventReceiver.cpp index fb6dd9392e825..7975c86759543 100644 --- a/core/jni/android_view_InputEventReceiver.cpp +++ b/core/jni/android_view_InputEventReceiver.cpp @@ -23,10 +23,9 @@ #include #include -#include +#include #include #include -#include #include #include "android_os_MessageQueue.h" #include "android_view_InputChannel.h" diff --git a/core/jni/android_view_InputEventSender.cpp b/core/jni/android_view_InputEventSender.cpp index aa10a2f98a7e1..2542286635e67 100644 --- a/core/jni/android_view_InputEventSender.cpp +++ b/core/jni/android_view_InputEventSender.cpp @@ -21,10 +21,8 @@ #include #include -#include +#include #include -#include -#include #include #include "android_os_MessageQueue.h" #include "android_view_InputChannel.h" @@ -32,6 +30,7 @@ #include "android_view_MotionEvent.h" #include +#include #include "core_jni_helpers.h" @@ -65,7 +64,8 @@ private: jobject mSenderWeakGlobal; InputPublisher mInputPublisher; sp mMessageQueue; - KeyedVector mPublishedSeqMap; + std::unordered_map mPublishedSeqMap; + uint32_t mNextPublishedSeq; const std::string getInputChannelName() { @@ -122,7 +122,7 @@ status_t NativeInputEventSender::sendKeyEvent(uint32_t seq, const KeyEvent* even getInputChannelName().c_str(), status); return status; } - mPublishedSeqMap.add(publishedSeq, seq); + mPublishedSeqMap.emplace(publishedSeq, seq); return OK; } @@ -150,7 +150,7 @@ status_t NativeInputEventSender::sendMotionEvent(uint32_t seq, const MotionEvent return status; } } - mPublishedSeqMap.add(publishedSeq, seq); + mPublishedSeqMap.emplace(publishedSeq, seq); return OK; } @@ -199,35 +199,37 @@ status_t NativeInputEventSender::receiveFinishedSignals(JNIEnv* env) { return status; } - ssize_t index = mPublishedSeqMap.indexOfKey(publishedSeq); - if (index >= 0) { - uint32_t seq = mPublishedSeqMap.valueAt(index); - mPublishedSeqMap.removeItemsAt(index); + auto it = mPublishedSeqMap.find(publishedSeq); + if (it == mPublishedSeqMap.end()) { + continue; + } - if (kDebugDispatchCycle) { - ALOGD("channel '%s' ~ Received finished signal, seq=%u, handled=%s, " - "pendingEvents=%zu.", - getInputChannelName().c_str(), seq, handled ? "true" : "false", - mPublishedSeqMap.size()); + uint32_t seq = it->second; + mPublishedSeqMap.erase(it); + + if (kDebugDispatchCycle) { + ALOGD("channel '%s' ~ Received finished signal, seq=%u, handled=%s, " + "pendingEvents=%zu.", + getInputChannelName().c_str(), seq, handled ? "true" : "false", + mPublishedSeqMap.size()); + } + + if (!skipCallbacks) { + if (!senderObj.get()) { + senderObj.reset(jniGetReferent(env, mSenderWeakGlobal)); + if (!senderObj.get()) { + ALOGW("channel '%s' ~ Sender object was finalized " + "without being disposed.", getInputChannelName().c_str()); + return DEAD_OBJECT; + } } - if (!skipCallbacks) { - if (!senderObj.get()) { - senderObj.reset(jniGetReferent(env, mSenderWeakGlobal)); - if (!senderObj.get()) { - ALOGW("channel '%s' ~ Sender object was finalized " - "without being disposed.", getInputChannelName().c_str()); - return DEAD_OBJECT; - } - } - - env->CallVoidMethod(senderObj.get(), - gInputEventSenderClassInfo.dispatchInputEventFinished, - jint(seq), jboolean(handled)); - if (env->ExceptionCheck()) { - ALOGE("Exception dispatching finished signal."); - skipCallbacks = true; - } + env->CallVoidMethod(senderObj.get(), + gInputEventSenderClassInfo.dispatchInputEventFinished, + jint(seq), jboolean(handled)); + if (env->ExceptionCheck()) { + ALOGE("Exception dispatching finished signal."); + skipCallbacks = true; } } }