diff options
| author | 2023-06-21 22:10:03 +0000 | |
|---|---|---|
| committer | 2023-06-21 22:10:03 +0000 | |
| commit | 8a19cadc33e22e5177141d3de1d61b1e599e06fe (patch) | |
| tree | b07952699ba7b189cdaafee786a24b0a56a28bdf | |
| parent | 65fc8649f1e80cd9e0cbc434561655802f34d8b0 (diff) | |
| parent | 042a7a0b193b832dbe080358bf9c1c7a78c16b95 (diff) | |
Merge "Use a strongly typed Uid in WindowInfo" into udc-qpr-dev am: 042a7a0b19
Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/native/+/23620817
Change-Id: I3e03087d9aa239d1cc976287aef5c2b71dc991c7
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
20 files changed, 215 insertions, 157 deletions
diff --git a/libs/gui/WindowInfo.cpp b/libs/gui/WindowInfo.cpp index 6df9ff1664..cd9b424e13 100644 --- a/libs/gui/WindowInfo.cpp +++ b/libs/gui/WindowInfo.cpp @@ -90,8 +90,9 @@ status_t WindowInfo::writeToParcel(android::Parcel* parcel) const { } parcel->writeInt32(1); - // Ensure that the size of the flags that we use is 32 bits for writing into the parcel. + // Ensure that the size of custom types are what we expect for writing into the parcel. static_assert(sizeof(inputConfig) == 4u); + static_assert(sizeof(ownerUid.val()) == 4u); // clang-format off status_t status = parcel->writeStrongBinder(token) ?: @@ -116,7 +117,7 @@ status_t WindowInfo::writeToParcel(android::Parcel* parcel) const { parcel->writeFloat(transform.ty()) ?: parcel->writeInt32(static_cast<int32_t>(touchOcclusionMode)) ?: parcel->writeInt32(ownerPid) ?: - parcel->writeInt32(ownerUid) ?: + parcel->writeInt32(ownerUid.val()) ?: parcel->writeUtf8AsUtf16(packageName) ?: parcel->writeInt32(inputConfig.get()) ?: parcel->writeInt32(displayId) ?: @@ -147,7 +148,7 @@ status_t WindowInfo::readFromParcel(const android::Parcel* parcel) { } float dsdx, dtdx, tx, dtdy, dsdy, ty; - int32_t lpFlags, lpType, touchOcclusionModeInt, inputConfigInt; + int32_t lpFlags, lpType, touchOcclusionModeInt, inputConfigInt, ownerUidInt; sp<IBinder> touchableRegionCropHandleSp; // clang-format off @@ -168,7 +169,7 @@ status_t WindowInfo::readFromParcel(const android::Parcel* parcel) { parcel->readFloat(&ty) ?: parcel->readInt32(&touchOcclusionModeInt) ?: parcel->readInt32(&ownerPid) ?: - parcel->readInt32(&ownerUid) ?: + parcel->readInt32(&ownerUidInt) ?: parcel->readUtf8FromUtf16(&packageName) ?: parcel->readInt32(&inputConfigInt) ?: parcel->readInt32(&displayId) ?: @@ -190,6 +191,7 @@ status_t WindowInfo::readFromParcel(const android::Parcel* parcel) { transform.set({dsdx, dtdx, tx, dtdy, dsdy, ty, 0, 0, 1}); touchOcclusionMode = static_cast<TouchOcclusionMode>(touchOcclusionModeInt); inputConfig = ftl::Flags<InputConfig>(inputConfigInt); + ownerUid = Uid{static_cast<uid_t>(ownerUidInt)}; touchableRegionCropHandle = touchableRegionCropHandleSp; return OK; diff --git a/libs/gui/fuzzer/libgui_surfaceComposerClient_fuzzer.cpp b/libs/gui/fuzzer/libgui_surfaceComposerClient_fuzzer.cpp index 57720dd513..7268e6483a 100644 --- a/libs/gui/fuzzer/libgui_surfaceComposerClient_fuzzer.cpp +++ b/libs/gui/fuzzer/libgui_surfaceComposerClient_fuzzer.cpp @@ -187,7 +187,7 @@ void SurfaceComposerClientFuzzer::getWindowInfo(gui::WindowInfo* windowInfo) { windowInfo->replaceTouchableRegionWithCrop = mFdp.ConsumeBool(); windowInfo->touchOcclusionMode = mFdp.PickValueInArray(kMode); windowInfo->ownerPid = mFdp.ConsumeIntegral<int32_t>(); - windowInfo->ownerUid = mFdp.ConsumeIntegral<int32_t>(); + windowInfo->ownerUid = gui::Uid{mFdp.ConsumeIntegral<uid_t>()}; windowInfo->packageName = mFdp.ConsumeRandomLengthString(kRandomStringMaxBytes); windowInfo->inputConfig = mFdp.PickValueInArray(kFeatures); } diff --git a/libs/gui/include/gui/Uid.h b/libs/gui/include/gui/Uid.h new file mode 100644 index 0000000000..8e45ef53da --- /dev/null +++ b/libs/gui/include/gui/Uid.h @@ -0,0 +1,41 @@ +/* + * Copyright 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include <ftl/mixins.h> +#include <sys/types.h> +#include <string> + +namespace android::gui { + +// Type-safe wrapper for a UID. +// We treat the unsigned equivalent of -1 as a singular invalid value. +struct Uid : ftl::Constructible<Uid, uid_t>, ftl::Equatable<Uid>, ftl::Orderable<Uid> { + using Constructible::Constructible; + + const static Uid INVALID; + + constexpr auto val() const { return ftl::to_underlying(*this); } + + constexpr bool isValid() const { return val() != static_cast<uid_t>(-1); } + + std::string toString() const { return std::to_string(val()); } +}; + +const inline Uid Uid::INVALID{static_cast<uid_t>(-1)}; + +} // namespace android::gui diff --git a/libs/gui/include/gui/WindowInfo.h b/libs/gui/include/gui/WindowInfo.h index 70b2ee8e32..666101eace 100644 --- a/libs/gui/include/gui/WindowInfo.h +++ b/libs/gui/include/gui/WindowInfo.h @@ -21,6 +21,8 @@ #include <binder/Parcel.h> #include <binder/Parcelable.h> #include <ftl/flags.h> +#include <ftl/mixins.h> +#include <gui/Uid.h> #include <gui/constants.h> #include <ui/Rect.h> #include <ui/Region.h> @@ -224,7 +226,7 @@ struct WindowInfo : public Parcelable { TouchOcclusionMode touchOcclusionMode = TouchOcclusionMode::BLOCK_UNTRUSTED; int32_t ownerPid = -1; - int32_t ownerUid = -1; + Uid ownerUid = Uid::INVALID; std::string packageName; ftl::Flags<InputConfig> inputConfig; int32_t displayId = ADISPLAY_ID_NONE; diff --git a/libs/gui/tests/EndToEndNativeInputTest.cpp b/libs/gui/tests/EndToEndNativeInputTest.cpp index 4ec7a06cb8..4d5bd5b3fa 100644 --- a/libs/gui/tests/EndToEndNativeInputTest.cpp +++ b/libs/gui/tests/EndToEndNativeInputTest.cpp @@ -821,7 +821,7 @@ TEST_F(InputSurfacesTest, touch_flag_obscured) { // with flag AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100); nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true); - nonTouchableSurface->mInputInfo.ownerUid = 22222; + nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222}; // Overriding occlusion mode otherwise the touch would be discarded at InputDispatcher by // the default obscured/untrusted touch filter introduced in S. nonTouchableSurface->mInputInfo.touchOcclusionMode = TouchOcclusionMode::ALLOW; @@ -842,8 +842,8 @@ TEST_F(InputSurfacesTest, touch_flag_partially_obscured_with_crop) { std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100); nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true); parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true); - nonTouchableSurface->mInputInfo.ownerUid = 22222; - parentSurface->mInputInfo.ownerUid = 22222; + nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222}; + parentSurface->mInputInfo.ownerUid = gui::Uid{22222}; nonTouchableSurface->showAt(0, 0); parentSurface->showAt(100, 100); @@ -866,8 +866,8 @@ TEST_F(InputSurfacesTest, touch_not_obscured_with_crop) { std::unique_ptr<InputSurface> nonTouchableSurface = makeSurface(100, 100); nonTouchableSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true); parentSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true); - nonTouchableSurface->mInputInfo.ownerUid = 22222; - parentSurface->mInputInfo.ownerUid = 22222; + nonTouchableSurface->mInputInfo.ownerUid = gui::Uid{22222}; + parentSurface->mInputInfo.ownerUid = gui::Uid{22222}; nonTouchableSurface->showAt(0, 0); parentSurface->showAt(50, 50); @@ -886,7 +886,7 @@ TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_bql) { std::unique_ptr<InputSurface> bufferSurface = InputSurface::makeBufferInputSurface(mComposerClient, 0, 0); bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true); - bufferSurface->mInputInfo.ownerUid = 22222; + bufferSurface->mInputInfo.ownerUid = gui::Uid{22222}; surface->showAt(10, 10); bufferSurface->showAt(50, 50, Rect::EMPTY_RECT); @@ -901,7 +901,7 @@ TEST_F(InputSurfacesTest, touch_not_obscured_with_zero_sized_blast) { std::unique_ptr<BlastInputSurface> bufferSurface = BlastInputSurface::makeBlastInputSurface(mComposerClient, 0, 0); bufferSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true); - bufferSurface->mInputInfo.ownerUid = 22222; + bufferSurface->mInputInfo.ownerUid = gui::Uid{22222}; surface->showAt(10, 10); bufferSurface->showAt(50, 50, Rect::EMPTY_RECT); @@ -948,13 +948,13 @@ TEST_F(InputSurfacesTest, strict_unobscured_input_scaled_without_crop_window) { TEST_F(InputSurfacesTest, strict_unobscured_input_obscured_window) { std::unique_ptr<InputSurface> surface = makeSurface(100, 100); - surface->mInputInfo.ownerUid = 11111; + surface->mInputInfo.ownerUid = gui::Uid{11111}; surface->doTransaction( [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); }); surface->showAt(100, 100); std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100); obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true); - obscuringSurface->mInputInfo.ownerUid = 22222; + obscuringSurface->mInputInfo.ownerUid = gui::Uid{22222}; obscuringSurface->showAt(100, 100); injectTap(101, 101); EXPECT_EQ(surface->consumeEvent(100), nullptr); @@ -967,13 +967,13 @@ TEST_F(InputSurfacesTest, strict_unobscured_input_obscured_window) { TEST_F(InputSurfacesTest, strict_unobscured_input_partially_obscured_window) { std::unique_ptr<InputSurface> surface = makeSurface(100, 100); - surface->mInputInfo.ownerUid = 11111; + surface->mInputInfo.ownerUid = gui::Uid{11111}; surface->doTransaction( [&](auto &t, auto &sc) { t.setDropInputMode(sc, gui::DropInputMode::OBSCURED); }); surface->showAt(100, 100); std::unique_ptr<InputSurface> obscuringSurface = makeSurface(100, 100); obscuringSurface->mInputInfo.setInputConfig(WindowInfo::InputConfig::NOT_TOUCHABLE, true); - obscuringSurface->mInputInfo.ownerUid = 22222; + obscuringSurface->mInputInfo.ownerUid = gui::Uid{22222}; obscuringSurface->showAt(190, 190); injectTap(101, 101); diff --git a/libs/gui/tests/WindowInfo_test.cpp b/libs/gui/tests/WindowInfo_test.cpp index 11b87efda7..e3a629e1cb 100644 --- a/libs/gui/tests/WindowInfo_test.cpp +++ b/libs/gui/tests/WindowInfo_test.cpp @@ -62,7 +62,7 @@ TEST(WindowInfo, Parcelling) { i.transform.set({0.4, -1, 100, 0.5, 0, 40, 0, 0, 1}); i.touchOcclusionMode = TouchOcclusionMode::ALLOW; i.ownerPid = 19; - i.ownerUid = 24; + i.ownerUid = gui::Uid{24}; i.packageName = "com.example.package"; i.inputConfig = WindowInfo::InputConfig::NOT_FOCUSABLE; i.displayId = 34; diff --git a/services/inputflinger/InputDeviceMetricsCollector.cpp b/services/inputflinger/InputDeviceMetricsCollector.cpp index 2319d51441..5df95b217e 100644 --- a/services/inputflinger/InputDeviceMetricsCollector.cpp +++ b/services/inputflinger/InputDeviceMetricsCollector.cpp @@ -81,10 +81,11 @@ class : public InputDeviceMetricsLogger { std::vector<int32_t> uids; std::vector<int32_t> durationsPerUid; for (auto& [uid, dur] : report.uidBreakdown) { - uids.push_back(uid); + uids.push_back(uid.val()); int32_t durMillis = std::chrono::duration_cast<std::chrono::milliseconds>(dur).count(); durationsPerUid.push_back(durMillis); - ALOGD_IF(DEBUG, " - uid: %d\t duration: %dms", uid, durMillis); + ALOGD_IF(DEBUG, " - uid: %s\t duration: %dms", uid.toString().c_str(), + durMillis); } util::stats_write(util::INPUTDEVICE_USAGE_REPORTED, identifier.vendor, identifier.product, identifier.version, linuxBusToInputDeviceBusEnum(identifier.bus), @@ -260,12 +261,8 @@ void InputDeviceMetricsCollector::notifyPointerCaptureChanged( } void InputDeviceMetricsCollector::notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp, - const std::set<int32_t>& uids) { - std::set<Uid> typeSafeUids; - for (auto uid : uids) { - typeSafeUids.emplace(uid); - } - mInteractionsQueue.push(DeviceId{deviceId}, timestamp, typeSafeUids); + const std::set<Uid>& uids) { + mInteractionsQueue.push(DeviceId{deviceId}, timestamp, uids); } void InputDeviceMetricsCollector::dump(std::string& dump) { diff --git a/services/inputflinger/InputDeviceMetricsCollector.h b/services/inputflinger/InputDeviceMetricsCollector.h index 387786fe10..c70e6d44ce 100644 --- a/services/inputflinger/InputDeviceMetricsCollector.h +++ b/services/inputflinger/InputDeviceMetricsCollector.h @@ -21,6 +21,7 @@ #include "SyncQueue.h" #include <ftl/mixins.h> +#include <gui/WindowInfo.h> #include <input/InputDevice.h> #include <statslog.h> #include <chrono> @@ -43,7 +44,7 @@ public: * Called from the InputDispatcher thread. */ virtual void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp, - const std::set<int32_t>& uids) = 0; + const std::set<gui::Uid>& uids) = 0; /** * Dump the state of the interaction blocker. * This method may be called on any thread (usually by the input manager on a binder thread). @@ -101,7 +102,7 @@ public: // Describes the breakdown of an input device usage session by the UIDs that it interacted with. using UidUsageBreakdown = - std::vector<std::pair<int32_t /*uid*/, std::chrono::nanoseconds /*duration*/>>; + std::vector<std::pair<gui::Uid, std::chrono::nanoseconds /*duration*/>>; struct DeviceUsageReport { std::chrono::nanoseconds usageDuration; @@ -134,7 +135,7 @@ public: void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override; void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp, - const std::set<int32_t>& uids) override; + const std::set<gui::Uid>& uids) override; void dump(std::string& dump) override; private: @@ -152,13 +153,7 @@ private: return std::to_string(ftl::to_underlying(id)); } - // Type-safe wrapper for a UID. - struct Uid : ftl::Constructible<Uid, std::int32_t>, ftl::Equatable<Uid>, ftl::Orderable<Uid> { - using Constructible::Constructible; - }; - static inline std::string toString(const Uid& src) { - return std::to_string(ftl::to_underlying(src)); - } + using Uid = gui::Uid; std::map<DeviceId, InputDeviceInfo> mLoggedDeviceInfos; diff --git a/services/inputflinger/benchmarks/InputDispatcher_benchmarks.cpp b/services/inputflinger/benchmarks/InputDispatcher_benchmarks.cpp index 06a7352d3c..c5cf0833a8 100644 --- a/services/inputflinger/benchmarks/InputDispatcher_benchmarks.cpp +++ b/services/inputflinger/benchmarks/InputDispatcher_benchmarks.cpp @@ -37,7 +37,7 @@ constexpr int32_t DEVICE_ID = 1; // The default pid and uid for windows created by the test. constexpr int32_t WINDOW_PID = 999; -constexpr int32_t WINDOW_UID = 1001; +constexpr gui::Uid WINDOW_UID{1001}; static constexpr std::chrono::duration INJECT_EVENT_TIMEOUT = 5s; static constexpr std::chrono::nanoseconds DISPATCHING_TIMEOUT = 100ms; @@ -112,7 +112,7 @@ private: void notifyDropWindow(const sp<IBinder>&, float x, float y) override {} void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp, - const std::set<int32_t>& uids) override {} + const std::set<gui::Uid>& uids) override {} InputDispatcherConfiguration mConfig; }; diff --git a/services/inputflinger/dispatcher/InjectionState.cpp b/services/inputflinger/dispatcher/InjectionState.cpp index c2d3ad6b9d..053594b253 100644 --- a/services/inputflinger/dispatcher/InjectionState.cpp +++ b/services/inputflinger/dispatcher/InjectionState.cpp @@ -20,7 +20,7 @@ namespace android::inputdispatcher { -InjectionState::InjectionState(const std::optional<int32_t>& targetUid) +InjectionState::InjectionState(const std::optional<gui::Uid>& targetUid) : refCount(1), targetUid(targetUid), injectionResult(android::os::InputEventInjectionResult::PENDING), diff --git a/services/inputflinger/dispatcher/InjectionState.h b/services/inputflinger/dispatcher/InjectionState.h index d9e27ba50b..3a3f5aefba 100644 --- a/services/inputflinger/dispatcher/InjectionState.h +++ b/services/inputflinger/dispatcher/InjectionState.h @@ -26,12 +26,12 @@ namespace inputdispatcher { struct InjectionState { mutable int32_t refCount; - std::optional<int32_t> targetUid; + std::optional<gui::Uid> targetUid; android::os::InputEventInjectionResult injectionResult; // initially PENDING bool injectionIsAsync; // set to true if injection is not waiting for the result int32_t pendingForegroundDispatches; // the number of foreground dispatches in progress - explicit InjectionState(const std::optional<int32_t>& targetUid); + explicit InjectionState(const std::optional<gui::Uid>& targetUid); void release(); private: diff --git a/services/inputflinger/dispatcher/InputDispatcher.cpp b/services/inputflinger/dispatcher/InputDispatcher.cpp index c6cd4bb781..8ff1f8e49e 100644 --- a/services/inputflinger/dispatcher/InputDispatcher.cpp +++ b/services/inputflinger/dispatcher/InputDispatcher.cpp @@ -126,6 +126,10 @@ inline const std::string binderToString(const sp<IBinder>& binder) { return StringPrintf("%p", binder.get()); } +static std::string uidString(const gui::Uid& uid) { + return uid.toString(); +} + inline int32_t getMotionEventActionPointerIndex(int32_t action) { return (action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT; @@ -557,7 +561,7 @@ bool canReceiveForegroundTouches(const WindowInfo& info) { return !info.inputConfig.test(gui::WindowInfo::InputConfig::NOT_TOUCHABLE) && !info.isSpy(); } -bool isWindowOwnedBy(const sp<WindowInfoHandle>& windowHandle, int32_t pid, int32_t uid) { +bool isWindowOwnedBy(const sp<WindowInfoHandle>& windowHandle, int32_t pid, gui::Uid uid) { if (windowHandle == nullptr) { return false; } @@ -577,14 +581,16 @@ std::optional<std::string> verifyTargetedInjection(const sp<WindowInfoHandle>& w // The event was not injected, or the injected event does not target a window. return {}; } - const int32_t uid = *entry.injectionState->targetUid; + const auto uid = *entry.injectionState->targetUid; if (window == nullptr) { - return StringPrintf("No valid window target for injection into uid %d.", uid); + return StringPrintf("No valid window target for injection into uid %s.", + uid.toString().c_str()); } if (entry.injectionState->targetUid != window->getInfo()->ownerUid) { - return StringPrintf("Injected event targeted at uid %d would be dispatched to window '%s' " - "owned by uid %d.", - uid, window->getName().c_str(), window->getInfo()->ownerUid); + return StringPrintf("Injected event targeted at uid %s would be dispatched to window '%s' " + "owned by uid %s.", + uid.toString().c_str(), window->getName().c_str(), + window->getInfo()->ownerUid.toString().c_str()); } return {}; } @@ -2585,8 +2591,8 @@ std::vector<InputTarget> InputDispatcher::findTouchedWindowTargetsLocked( } if (!errs.empty()) { ALOGW("Dropping targeted injection: At least one touched window is not owned by uid " - "%d:%s", - *entry.injectionState->targetUid, errs.c_str()); + "%s:%s", + entry.injectionState->targetUid->toString().c_str(), errs.c_str()); outInjectionResult = InputEventInjectionResult::TARGET_MISMATCH; return {}; } @@ -2598,7 +2604,7 @@ std::vector<InputTarget> InputDispatcher::findTouchedWindowTargetsLocked( sp<WindowInfoHandle> foregroundWindowHandle = tempTouchState.getFirstForegroundWindowHandle(); if (foregroundWindowHandle) { - const int32_t foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; + const auto foregroundWindowUid = foregroundWindowHandle->getInfo()->ownerUid; for (InputTarget& target : targets) { if (target.flags.test(InputTarget::Flags::DISPATCH_AS_OUTSIDE)) { sp<WindowInfoHandle> targetWindow = @@ -2962,8 +2968,8 @@ InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLo TouchOcclusionInfo info; info.hasBlockingOcclusion = false; info.obscuringOpacity = 0; - info.obscuringUid = -1; - std::map<int32_t, float> opacityByUid; + info.obscuringUid = gui::Uid::INVALID; + std::map<gui::Uid, float> opacityByUid; for (const sp<WindowInfoHandle>& otherHandle : windowHandles) { if (windowHandle == otherHandle) { break; // All future windows are below us. Exit early. @@ -2985,7 +2991,7 @@ InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLo break; } if (otherInfo->touchOcclusionMode == TouchOcclusionMode::USE_OPACITY) { - uint32_t uid = otherInfo->ownerUid; + const auto uid = otherInfo->ownerUid; float opacity = (opacityByUid.find(uid) == opacityByUid.end()) ? 0 : opacityByUid[uid]; // Given windows A and B: @@ -3009,29 +3015,30 @@ InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLo std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info, bool isTouchedWindow) const { - return StringPrintf(INDENT2 "* %spackage=%s/%" PRId32 ", id=%" PRId32 ", mode=%s, alpha=%.2f, " + return StringPrintf(INDENT2 "* %spackage=%s/%s, id=%" PRId32 ", mode=%s, alpha=%.2f, " "frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32 "], touchableRegion=%s, window={%s}, inputConfig={%s}, " "hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n", isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(), - info->ownerUid, info->id, toString(info->touchOcclusionMode).c_str(), - info->alpha, info->frameLeft, info->frameTop, info->frameRight, - info->frameBottom, dumpRegion(info->touchableRegion).c_str(), - info->name.c_str(), info->inputConfig.string().c_str(), - toString(info->token != nullptr), info->applicationInfo.name.c_str(), + info->ownerUid.toString().c_str(), info->id, + toString(info->touchOcclusionMode).c_str(), info->alpha, info->frameLeft, + info->frameTop, info->frameRight, info->frameBottom, + dumpRegion(info->touchableRegion).c_str(), info->name.c_str(), + info->inputConfig.string().c_str(), toString(info->token != nullptr), + info->applicationInfo.name.c_str(), binderToString(info->applicationInfo.token).c_str()); } bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const { if (occlusionInfo.hasBlockingOcclusion) { - ALOGW("Untrusted touch due to occlusion by %s/%d", occlusionInfo.obscuringPackage.c_str(), - occlusionInfo.obscuringUid); + ALOGW("Untrusted touch due to occlusion by %s/%s", occlusionInfo.obscuringPackage.c_str(), + occlusionInfo.obscuringUid.toString().c_str()); return false; } if (occlusionInfo.obscuringOpacity > mMaximumObscuringOpacityForTouch) { - ALOGW("Untrusted touch due to occlusion by %s/%d (obscuring opacity = " + ALOGW("Untrusted touch due to occlusion by %s/%s (obscuring opacity = " "%.2f, maximum allowed = %.2f)", - occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid, + occlusionInfo.obscuringPackage.c_str(), occlusionInfo.obscuringUid.toString().c_str(), occlusionInfo.obscuringOpacity, mMaximumObscuringOpacityForTouch); return false; } @@ -3453,7 +3460,7 @@ void InputDispatcher::processInteractionsLocked(const EventEntry& entry, return; // Not a key or a motion } - std::set<int32_t> interactionUids; + std::set<gui::Uid> interactionUids; std::unordered_set<sp<IBinder>, StrongPointerHash<IBinder>> newConnectionTokens; std::vector<std::shared_ptr<Connection>> newConnections; for (const InputTarget& target : targets) { @@ -4529,7 +4536,7 @@ void InputDispatcher::notifyPointerCaptureChanged(const NotifyPointerCaptureChan } InputEventInjectionResult InputDispatcher::injectInputEvent(const InputEvent* event, - std::optional<int32_t> targetUid, + std::optional<gui::Uid> targetUid, InputEventInjectionSync syncMode, std::chrono::milliseconds timeout, uint32_t policyFlags) { @@ -4540,7 +4547,7 @@ InputEventInjectionResult InputDispatcher::injectInputEvent(const InputEvent* ev } if (debugInboundEventDetails()) { - LOG(DEBUG) << __func__ << ": targetUid=" << toString(targetUid) + LOG(DEBUG) << __func__ << ": targetUid=" << toString(targetUid, &uidString) << ", syncMode=" << ftl::enum_string(syncMode) << ", timeout=" << timeout.count() << "ms, policyFlags=0x" << std::hex << policyFlags << std::dec << ", event=" << *event; @@ -4985,8 +4992,8 @@ bool InputDispatcher::canWindowReceiveMotionLocked(const sp<WindowInfoHandle>& w ALOGD("%s", log.c_str()); } } - ALOGW("Dropping untrusted touch event due to %s/%d", occlusionInfo.obscuringPackage.c_str(), - occlusionInfo.obscuringUid); + ALOGW("Dropping untrusted touch event due to %s/%s", occlusionInfo.obscuringPackage.c_str(), + occlusionInfo.obscuringUid.toString().c_str()); return false; } @@ -5348,15 +5355,16 @@ void InputDispatcher::setInputFilterEnabled(bool enabled) { mLooper->wake(); } -bool InputDispatcher::setInTouchMode(bool inTouchMode, int32_t pid, int32_t uid, bool hasPermission, - int32_t displayId) { +bool InputDispatcher::setInTouchMode(bool inTouchMode, int32_t pid, gui::Uid uid, + bool hasPermission, int32_t displayId) { bool needWake = false; { std::scoped_lock lock(mLock); ALOGD_IF(DEBUG_TOUCH_MODE, - "Request to change touch mode to %s (calling pid=%d, uid=%d, " + "Request to change touch mode to %s (calling pid=%d, uid=%s, " "hasPermission=%s, target displayId=%d, mTouchModePerDisplay[displayId]=%s)", - toString(inTouchMode), pid, uid, toString(hasPermission), displayId, + toString(inTouchMode), pid, uid.toString().c_str(), toString(hasPermission), + displayId, mTouchModePerDisplay.count(displayId) == 0 ? "not set" : std::to_string(mTouchModePerDisplay[displayId]).c_str()); @@ -5368,9 +5376,9 @@ bool InputDispatcher::setInTouchMode(bool inTouchMode, int32_t pid, int32_t uid, if (!hasPermission) { if (!focusedWindowIsOwnedByLocked(pid, uid) && !recentWindowsAreOwnedByLocked(pid, uid)) { - ALOGD("Touch mode switch rejected, caller (pid=%d, uid=%d) doesn't own the focused " + ALOGD("Touch mode switch rejected, caller (pid=%d, uid=%s) doesn't own the focused " "window nor none of the previously interacted window", - pid, uid); + pid, uid.toString().c_str()); return false; } } @@ -5386,7 +5394,7 @@ bool InputDispatcher::setInTouchMode(bool inTouchMode, int32_t pid, int32_t uid, return true; } -bool InputDispatcher::focusedWindowIsOwnedByLocked(int32_t pid, int32_t uid) { +bool InputDispatcher::focusedWindowIsOwnedByLocked(int32_t pid, gui::Uid uid) { const sp<IBinder> focusedToken = mFocusResolver.getFocusedWindowToken(mFocusedDisplayId); if (focusedToken == nullptr) { return false; @@ -5395,7 +5403,7 @@ bool InputDispatcher::focusedWindowIsOwnedByLocked(int32_t pid, int32_t uid) { return isWindowOwnedBy(windowHandle, pid, uid); } -bool InputDispatcher::recentWindowsAreOwnedByLocked(int32_t pid, int32_t uid) { +bool InputDispatcher::recentWindowsAreOwnedByLocked(int32_t pid, gui::Uid uid) { return std::find_if(mInteractionConnectionTokens.begin(), mInteractionConnectionTokens.end(), [&](const sp<IBinder>& connectionToken) REQUIRES(mLock) { const sp<WindowInfoHandle> windowHandle = @@ -5680,10 +5688,11 @@ void InputDispatcher::dumpDispatchStateLocked(std::string& dump) const { windowInfo->applicationInfo.name.c_str(), binderToString(windowInfo->applicationInfo.token).c_str()); dump += dumpRegion(windowInfo->touchableRegion); - dump += StringPrintf(", ownerPid=%d, ownerUid=%d, dispatchingTimeout=%" PRId64 + dump += StringPrintf(", ownerPid=%d, ownerUid=%s, dispatchingTimeout=%" PRId64 "ms, hasToken=%s, " "touchOcclusionMode=%s\n", - windowInfo->ownerPid, windowInfo->ownerUid, + windowInfo->ownerPid, + windowInfo->ownerUid.toString().c_str(), millis(windowInfo->dispatchingTimeout), binderToString(windowInfo->token).c_str(), toString(windowInfo->touchOcclusionMode).c_str()); diff --git a/services/inputflinger/dispatcher/InputDispatcher.h b/services/inputflinger/dispatcher/InputDispatcher.h index 6635df7667..58283c9711 100644 --- a/services/inputflinger/dispatcher/InputDispatcher.h +++ b/services/inputflinger/dispatcher/InputDispatcher.h @@ -104,7 +104,7 @@ public: void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs& args) override; android::os::InputEventInjectionResult injectInputEvent( - const InputEvent* event, std::optional<int32_t> targetUid, + const InputEvent* event, std::optional<gui::Uid> targetUid, android::os::InputEventInjectionSync syncMode, std::chrono::milliseconds timeout, uint32_t policyFlags) override; @@ -119,7 +119,7 @@ public: void setFocusedDisplay(int32_t displayId) override; void setInputDispatchMode(bool enabled, bool frozen) override; void setInputFilterEnabled(bool enabled) override; - bool setInTouchMode(bool inTouchMode, int32_t pid, int32_t uid, bool hasPermission, + bool setInTouchMode(bool inTouchMode, int32_t pid, gui::Uid uid, bool hasPermission, int32_t displayId) override; void setMaximumObscuringOpacityForTouch(float opacity) override; @@ -574,7 +574,7 @@ private: bool hasBlockingOcclusion; float obscuringOpacity; std::string obscuringPackage; - int32_t obscuringUid; + gui::Uid obscuringUid = gui::Uid::INVALID; std::vector<std::string> debugInfo; }; @@ -703,8 +703,8 @@ private: void traceWaitQueueLength(const Connection& connection); // Check window ownership - bool focusedWindowIsOwnedByLocked(int32_t pid, int32_t uid) REQUIRES(mLock); - bool recentWindowsAreOwnedByLocked(int32_t pid, int32_t uid) REQUIRES(mLock); + bool focusedWindowIsOwnedByLocked(int32_t pid, gui::Uid uid) REQUIRES(mLock); + bool recentWindowsAreOwnedByLocked(int32_t pid, gui::Uid uid) REQUIRES(mLock); sp<InputReporterInterface> mReporter; diff --git a/services/inputflinger/dispatcher/include/InputDispatcherInterface.h b/services/inputflinger/dispatcher/include/InputDispatcherInterface.h index c752ddd699..1c8252185f 100644 --- a/services/inputflinger/dispatcher/include/InputDispatcherInterface.h +++ b/services/inputflinger/dispatcher/include/InputDispatcherInterface.h @@ -76,7 +76,7 @@ public: * perform all necessary permission checks prior to injecting events. */ virtual android::os::InputEventInjectionResult injectInputEvent( - const InputEvent* event, std::optional<int32_t> targetUid, + const InputEvent* event, std::optional<gui::Uid> targetUid, android::os::InputEventInjectionSync syncMode, std::chrono::milliseconds timeout, uint32_t policyFlags) = 0; @@ -134,7 +134,7 @@ public: * * Returns true when changing touch mode state. */ - virtual bool setInTouchMode(bool inTouchMode, int32_t pid, int32_t uid, bool hasPermission, + virtual bool setInTouchMode(bool inTouchMode, int32_t pid, gui::Uid uid, bool hasPermission, int32_t displayId) = 0; /** diff --git a/services/inputflinger/dispatcher/include/InputDispatcherPolicyInterface.h b/services/inputflinger/dispatcher/include/InputDispatcherPolicyInterface.h index 15ea6675f1..3fb5c79327 100644 --- a/services/inputflinger/dispatcher/include/InputDispatcherPolicyInterface.h +++ b/services/inputflinger/dispatcher/include/InputDispatcherPolicyInterface.h @@ -139,7 +139,7 @@ public: /* Notifies the policy that there was an input device interaction with apps. */ virtual void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp, - const std::set<int32_t>& uids) = 0; + const std::set<gui::Uid>& uids) = 0; }; } // namespace android diff --git a/services/inputflinger/tests/InputDeviceMetricsCollector_test.cpp b/services/inputflinger/tests/InputDeviceMetricsCollector_test.cpp index c555d95f75..b401ff6c5d 100644 --- a/services/inputflinger/tests/InputDeviceMetricsCollector_test.cpp +++ b/services/inputflinger/tests/InputDeviceMetricsCollector_test.cpp @@ -80,6 +80,14 @@ const InputDeviceInfo ALPHABETIC_KEYBOARD_INFO = const InputDeviceInfo NON_ALPHABETIC_KEYBOARD_INFO = generateTestDeviceInfo(DEVICE_ID, KEY_SOURCES, /*isAlphabetic=*/false); +std::set<gui::Uid> uids(std::initializer_list<int32_t> vals) { + std::set<gui::Uid> set; + for (const auto val : vals) { + set.emplace(val); + } + return set; +} + } // namespace // --- InputDeviceMetricsCollectorDeviceClassificationTest --- @@ -632,7 +640,7 @@ TEST_F(InputDeviceMetricsCollectorTest, UidsNotTrackedWhenThereIsNoActiveSession mMetricsCollector.notifyInputDevicesChanged({/*id=*/0, {generateTestDeviceInfo()}}); // Notify interaction with UIDs before the device is used. - mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), /*uids=*/{1}); + mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), uids({1})); // Use the device. setCurrentTime(TIME + 100ns); @@ -641,12 +649,12 @@ TEST_F(InputDeviceMetricsCollectorTest, UidsNotTrackedWhenThereIsNoActiveSession mMetricsCollector.notifyMotion(generateMotionArgs(DEVICE_ID)); // Notify interaction for the wrong device. - mMetricsCollector.notifyDeviceInteraction(DEVICE_ID_2, currentTime(), /*uids=*/{42}); + mMetricsCollector.notifyDeviceInteraction(DEVICE_ID_2, currentTime(), uids({42})); // Notify interaction after usage session would have expired. // This interaction should not be tracked. setCurrentTime(TIME + 200ns + USAGE_TIMEOUT); - mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), /*uids=*/{2, 3}); + mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), uids({2, 3})); // Use the device again, by starting a new usage session. setCurrentTime(TIME + 300ns + USAGE_TIMEOUT); @@ -665,14 +673,14 @@ TEST_F(InputDeviceMetricsCollectorTest, BreakdownUsageByUid) { UidUsageBreakdown expectedUidBreakdown; mMetricsCollector.notifyMotion(generateMotionArgs(DEVICE_ID)); - mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), /*uids=*/{1}); + mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), uids({1})); setCurrentTime(TIME + 100ns); mMetricsCollector.notifyMotion(generateMotionArgs(DEVICE_ID)); - mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), /*uids=*/{1, 2}); + mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), uids({1, 2})); setCurrentTime(TIME + 200ns); mMetricsCollector.notifyMotion(generateMotionArgs(DEVICE_ID)); - mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), /*uids=*/{1, 2, 3}); + mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), uids({1, 2, 3})); expectedUidBreakdown.emplace_back(1, 200ns); expectedUidBreakdown.emplace_back(2, 100ns); @@ -691,39 +699,39 @@ TEST_F(InputDeviceMetricsCollectorTest, BreakdownUsageByUid_TracksMultipleSessio UidUsageBreakdown expectedUidBreakdown; mMetricsCollector.notifyMotion(generateMotionArgs(DEVICE_ID)); - mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), /*uids=*/{1, 2}); + mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), uids({1, 2})); setCurrentTime(TIME + 100ns); mMetricsCollector.notifyMotion(generateMotionArgs(DEVICE_ID)); - mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), /*uids=*/{1, 2}); + mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), uids({1, 2})); setCurrentTime(TIME + 200ns); mMetricsCollector.notifyMotion(generateMotionArgs(DEVICE_ID)); - mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), /*uids=*/{1}); + mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), uids({1})); setCurrentTime(TIME + 300ns); mMetricsCollector.notifyMotion(generateMotionArgs(DEVICE_ID)); - mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), /*uids=*/{1, 3}); + mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), uids({1, 3})); setCurrentTime(TIME + 400ns); mMetricsCollector.notifyMotion(generateMotionArgs(DEVICE_ID)); - mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), /*uids=*/{1, 3}); + mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), uids({1, 3})); setCurrentTime(TIME + 200ns + USAGE_TIMEOUT); expectedUidBreakdown.emplace_back(2, 100ns); mMetricsCollector.notifyMotion(generateMotionArgs(DEVICE_ID)); - mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), /*uids=*/{4}); + mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), uids({4})); setCurrentTime(TIME + 300ns + USAGE_TIMEOUT); mMetricsCollector.notifyMotion(generateMotionArgs(DEVICE_ID)); - mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), /*uids=*/{1, 4}); + mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), uids({1, 4})); setCurrentTime(TIME + 400ns + USAGE_TIMEOUT); expectedUidBreakdown.emplace_back(3, 100ns); mMetricsCollector.notifyMotion(generateMotionArgs(DEVICE_ID)); - mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), /*uids=*/{2, 3}); + mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), uids({2, 3})); setCurrentTime(TIME + 500ns + USAGE_TIMEOUT); mMetricsCollector.notifyMotion(generateMotionArgs(DEVICE_ID)); - mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), /*uids=*/{3}); + mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), uids({3})); // Remove the device to force the usage session to be logged. mMetricsCollector.notifyInputDevicesChanged({}); @@ -744,17 +752,17 @@ TEST_F(InputDeviceMetricsCollectorTest, BreakdownUsageByUid_TracksUidsByDevice) UidUsageBreakdown expectedUidBreakdown2; mMetricsCollector.notifyMotion(generateMotionArgs(DEVICE_ID)); - mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), /*uids=*/{1, 2}); + mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), uids({1, 2})); setCurrentTime(TIME + 100ns); mMetricsCollector.notifyMotion(generateMotionArgs(DEVICE_ID_2)); - mMetricsCollector.notifyDeviceInteraction(DEVICE_ID_2, currentTime(), /*uids=*/{1, 3}); + mMetricsCollector.notifyDeviceInteraction(DEVICE_ID_2, currentTime(), uids({1, 3})); setCurrentTime(TIME + 200ns); mMetricsCollector.notifyMotion(generateMotionArgs(DEVICE_ID)); - mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), /*uids=*/{1, 2}); + mMetricsCollector.notifyDeviceInteraction(DEVICE_ID, currentTime(), uids({1, 2})); mMetricsCollector.notifyMotion(generateMotionArgs(DEVICE_ID_2)); - mMetricsCollector.notifyDeviceInteraction(DEVICE_ID_2, currentTime(), /*uids=*/{1, 3}); + mMetricsCollector.notifyDeviceInteraction(DEVICE_ID_2, currentTime(), uids({1, 3})); setCurrentTime(TIME + 200ns + USAGE_TIMEOUT); expectedUidBreakdown1.emplace_back(1, 200ns); diff --git a/services/inputflinger/tests/InputDispatcher_test.cpp b/services/inputflinger/tests/InputDispatcher_test.cpp index cef1791ffe..485bf9ada4 100644 --- a/services/inputflinger/tests/InputDispatcher_test.cpp +++ b/services/inputflinger/tests/InputDispatcher_test.cpp @@ -96,11 +96,11 @@ static constexpr int32_t POINTER_2_UP = // The default pid and uid for windows created on the primary display by the test. static constexpr int32_t WINDOW_PID = 999; -static constexpr int32_t WINDOW_UID = 1001; +static constexpr gui::Uid WINDOW_UID{1001}; // The default pid and uid for the windows created on the secondary display by the test. static constexpr int32_t SECONDARY_WINDOW_PID = 1010; -static constexpr int32_t SECONDARY_WINDOW_UID = 1012; +static constexpr gui::Uid SECONDARY_WINDOW_UID{1012}; // An arbitrary pid of the gesture monitor window static constexpr int32_t MONITOR_PID = 2001; @@ -417,7 +417,7 @@ public: ASSERT_FALSE(mPokedUserActivity) << "Expected user activity not to have been poked"; } - void assertNotifyDeviceInteractionWasCalled(int32_t deviceId, std::set<int32_t> uids) { + void assertNotifyDeviceInteractionWasCalled(int32_t deviceId, std::set<gui::Uid> uids) { ASSERT_EQ(std::make_pair(deviceId, uids), mNotifiedInteractions.popWithTimeout(100ms)); } @@ -450,7 +450,7 @@ private: std::chrono::milliseconds mInterceptKeyTimeout = 0ms; - BlockingQueue<std::pair<int32_t /*deviceId*/, std::set<int32_t /*uid*/>>> mNotifiedInteractions; + BlockingQueue<std::pair<int32_t /*deviceId*/, std::set<gui::Uid>>> mNotifiedInteractions; // All three ANR-related callbacks behave the same way, so we use this generic function to wait // for a specific container to become non-empty. When the container is non-empty, return the @@ -624,7 +624,7 @@ private: } void notifyDeviceInteraction(int32_t deviceId, nsecs_t timestamp, - const std::set<int32_t>& uids) override { + const std::set<gui::Uid>& uids) override { ASSERT_TRUE(mNotifiedInteractions.emplace(deviceId, uids)); } @@ -1443,7 +1443,7 @@ public: const std::string& getName() { return mName; } - void setOwnerInfo(int32_t ownerPid, int32_t ownerUid) { + void setOwnerInfo(int32_t ownerPid, gui::Uid ownerUid) { mInfo.ownerPid = ownerPid; mInfo.ownerUid = ownerUid; } @@ -1469,7 +1469,7 @@ static InputEventInjectionResult injectKey( int32_t displayId = ADISPLAY_ID_NONE, InputEventInjectionSync syncMode = InputEventInjectionSync::WAIT_FOR_RESULT, std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, - bool allowKeyRepeat = true, std::optional<int32_t> targetUid = {}, + bool allowKeyRepeat = true, std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { KeyEvent event; nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC); @@ -1510,7 +1510,7 @@ static InputEventInjectionResult injectMotionEvent( const std::unique_ptr<InputDispatcher>& dispatcher, const MotionEvent& event, std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, - std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { + std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { return dispatcher->injectInputEvent(&event, targetUid, injectionMode, injectionTimeout, policyFlags); } @@ -1523,7 +1523,7 @@ static InputEventInjectionResult injectMotionEvent( std::chrono::milliseconds injectionTimeout = INJECT_EVENT_TIMEOUT, InputEventInjectionSync injectionMode = InputEventInjectionSync::WAIT_FOR_RESULT, nsecs_t eventTime = systemTime(SYSTEM_TIME_MONOTONIC), - std::optional<int32_t> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { + std::optional<gui::Uid> targetUid = {}, uint32_t policyFlags = DEFAULT_POLICY_FLAGS) { MotionEventBuilder motionBuilder = MotionEventBuilder(action, source) .displayId(displayId) @@ -5509,7 +5509,7 @@ TEST_F(InputDispatcherTest, DisplayRemoved) { */ TEST_F(InputDispatcherTest, SlipperyWindow_SetsFlagPartiallyObscured) { constexpr int32_t SLIPPERY_PID = WINDOW_PID + 1; - constexpr int32_t SLIPPERY_UID = WINDOW_UID + 1; + constexpr gui::Uid SLIPPERY_UID{WINDOW_UID.val() + 1}; std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); @@ -5590,24 +5590,25 @@ TEST_F(InputDispatcherTest, TouchSlippingIntoWindowThatDropsTouches) { } TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) { + using Uid = gui::Uid; std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); sp<FakeWindowHandle> leftWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Left", ADISPLAY_ID_DEFAULT); leftWindow->setFrame(Rect(0, 0, 100, 100)); - leftWindow->setOwnerInfo(1, 101); + leftWindow->setOwnerInfo(1, Uid{101}); sp<FakeWindowHandle> rightSpy = sp<FakeWindowHandle>::make(application, mDispatcher, "Right spy", ADISPLAY_ID_DEFAULT); rightSpy->setFrame(Rect(100, 0, 200, 100)); - rightSpy->setOwnerInfo(2, 102); + rightSpy->setOwnerInfo(2, Uid{102}); rightSpy->setSpy(true); rightSpy->setTrustedOverlay(true); sp<FakeWindowHandle> rightWindow = sp<FakeWindowHandle>::make(application, mDispatcher, "Right", ADISPLAY_ID_DEFAULT); rightWindow->setFrame(Rect(100, 0, 200, 100)); - rightWindow->setOwnerInfo(3, 103); + rightWindow->setOwnerInfo(3, Uid{103}); mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {rightSpy, rightWindow, leftWindow}}}); @@ -5617,7 +5618,8 @@ TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) { .build()); ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionDown()); mDispatcher->waitForIdle(); - ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {101})); + ASSERT_NO_FATAL_FAILURE( + mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{101}})); // Touch another finger over the right windows mDispatcher->notifyMotion(MotionArgsBuilder(POINTER_1_DOWN, AINPUT_SOURCE_TOUCHSCREEN) @@ -5629,7 +5631,8 @@ TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) { ASSERT_NO_FATAL_FAILURE(leftWindow->consumeMotionMove()); mDispatcher->waitForIdle(); ASSERT_NO_FATAL_FAILURE( - mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {101, 102, 103})); + mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, + {Uid{101}, Uid{102}, Uid{103}})); // Release finger over left window. The UP actions are not treated as device interaction. // The windows that did not receive the UP pointer will receive MOVE events, but since this @@ -5652,7 +5655,7 @@ TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithMotions) { ASSERT_NO_FATAL_FAILURE(rightWindow->consumeMotionMove()); mDispatcher->waitForIdle(); ASSERT_NO_FATAL_FAILURE( - mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {102, 103})); + mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {Uid{102}, Uid{103}})); // Release all fingers mDispatcher->notifyMotion(MotionArgsBuilder(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN) @@ -5670,7 +5673,7 @@ TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) { sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Window", ADISPLAY_ID_DEFAULT); window->setFrame(Rect(0, 0, 100, 100)); - window->setOwnerInfo(1, 101); + window->setOwnerInfo(1, gui::Uid{101}); mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); setFocusedWindow(window); @@ -5679,7 +5682,8 @@ TEST_F(InputDispatcherTest, NotifiesDeviceInteractionsWithKeys) { mDispatcher->notifyKey(KeyArgsBuilder(ACTION_DOWN, AINPUT_SOURCE_KEYBOARD).build()); ASSERT_NO_FATAL_FAILURE(window->consumeKeyDown(ADISPLAY_ID_DEFAULT)); mDispatcher->waitForIdle(); - ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {101})); + ASSERT_NO_FATAL_FAILURE( + mFakePolicy->assertNotifyDeviceInteractionWasCalled(DEVICE_ID, {gui::Uid{101}})); // The UP actions are not treated as device interaction. mDispatcher->notifyKey(KeyArgsBuilder(ACTION_UP, AINPUT_SOURCE_KEYBOARD).build()); @@ -7876,9 +7880,9 @@ protected: static_assert(1 - (1 - OPACITY_FAR_BELOW_THRESHOLD) * (1 - OPACITY_FAR_BELOW_THRESHOLD) < MAXIMUM_OBSCURING_OPACITY); - static const int32_t TOUCHED_APP_UID = 10001; - static const int32_t APP_B_UID = 10002; - static const int32_t APP_C_UID = 10003; + static constexpr gui::Uid TOUCHED_APP_UID{10001}; + static constexpr gui::Uid APP_B_UID{10002}; + static constexpr gui::Uid APP_C_UID{10003}; sp<FakeWindowHandle> mTouchWindow; @@ -7893,7 +7897,7 @@ protected: mTouchWindow.clear(); } - sp<FakeWindowHandle> getOccludingWindow(int32_t uid, std::string name, TouchOcclusionMode mode, + sp<FakeWindowHandle> getOccludingWindow(gui::Uid uid, std::string name, TouchOcclusionMode mode, float alpha = 1.0f) { sp<FakeWindowHandle> window = getWindow(uid, name); window->setTouchable(false); @@ -7902,12 +7906,12 @@ protected: return window; } - sp<FakeWindowHandle> getWindow(int32_t uid, std::string name) { + sp<FakeWindowHandle> getWindow(gui::Uid uid, std::string name) { std::shared_ptr<FakeApplicationHandle> app = std::make_shared<FakeApplicationHandle>(); sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(app, mDispatcher, name, ADISPLAY_ID_DEFAULT); // Generate an arbitrary PID based on the UID - window->setOwnerInfo(1777 + (uid % 10000), uid); + window->setOwnerInfo(1777 + (uid.val() % 10000), uid); return window; } @@ -8753,13 +8757,13 @@ TEST_F(InputDispatcherDropInputFeatureTest, ObscuredWindowDropsInput) { sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", ADISPLAY_ID_DEFAULT); obscuringWindow->setFrame(Rect(0, 0, 50, 50)); - obscuringWindow->setOwnerInfo(111, 111); + obscuringWindow->setOwnerInfo(111, gui::Uid{111}); obscuringWindow->setTouchable(false); std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); window->setDropInputIfObscured(true); - window->setOwnerInfo(222, 222); + window->setOwnerInfo(222, gui::Uid{222}); mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); window->setFocusable(true); mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); @@ -8796,13 +8800,13 @@ TEST_F(InputDispatcherDropInputFeatureTest, UnobscuredWindowGetsInput) { sp<FakeWindowHandle>::make(obscuringApplication, mDispatcher, "obscuringWindow", ADISPLAY_ID_DEFAULT); obscuringWindow->setFrame(Rect(0, 0, 50, 50)); - obscuringWindow->setOwnerInfo(111, 111); + obscuringWindow->setOwnerInfo(111, gui::Uid{111}); obscuringWindow->setTouchable(false); std::shared_ptr<FakeApplicationHandle> application = std::make_shared<FakeApplicationHandle>(); sp<FakeWindowHandle> window = sp<FakeWindowHandle>::make(application, mDispatcher, "Test window", ADISPLAY_ID_DEFAULT); window->setDropInputIfObscured(true); - window->setOwnerInfo(222, 222); + window->setOwnerInfo(222, gui::Uid{222}); mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); window->setFocusable(true); mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {obscuringWindow, window}}}); @@ -8879,7 +8883,7 @@ protected: } } - void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, int32_t uid, + void changeAndVerifyTouchModeInMainDisplayOnly(bool inTouchMode, int32_t pid, gui::Uid uid, bool hasPermission) { ASSERT_TRUE(mDispatcher->setInTouchMode(inTouchMode, pid, uid, hasPermission, ADISPLAY_ID_DEFAULT)); @@ -8899,8 +8903,8 @@ TEST_F(InputDispatcherTouchModeChangedTests, FocusedWindowCanChangeTouchMode) { TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTouchMode) { const WindowInfo& windowInfo = *mWindow->getInfo(); int32_t ownerPid = windowInfo.ownerPid; - int32_t ownerUid = windowInfo.ownerUid; - mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); + gui::Uid ownerUid = windowInfo.ownerUid; + mWindow->setOwnerInfo(/*pid=*/-1, gui::Uid::INVALID); ASSERT_FALSE(mDispatcher->setInTouchMode(InputDispatcher::kDefaultInTouchMode, ownerPid, ownerUid, /*hasPermission=*/false, ADISPLAY_ID_DEFAULT)); @@ -8911,8 +8915,8 @@ TEST_F(InputDispatcherTouchModeChangedTests, NonFocusedWindowOwnerCannotChangeTo TEST_F(InputDispatcherTouchModeChangedTests, NonWindowOwnerMayChangeTouchModeOnPermissionGranted) { const WindowInfo& windowInfo = *mWindow->getInfo(); int32_t ownerPid = windowInfo.ownerPid; - int32_t ownerUid = windowInfo.ownerUid; - mWindow->setOwnerInfo(/* pid */ -1, /* uid */ -1); + gui::Uid ownerUid = windowInfo.ownerUid; + mWindow->setOwnerInfo(/*pid=*/-1, gui::Uid::INVALID); changeAndVerifyTouchModeInMainDisplayOnly(!InputDispatcher::kDefaultInTouchMode, ownerPid, ownerUid, /*hasPermission=*/true); } @@ -9123,10 +9127,10 @@ TEST_F(InputDispatcherSpyWindowTest, TouchableRegion) { */ TEST_F(InputDispatcherSpyWindowTest, WatchOutsideTouches) { auto window = createForeground(); - window->setOwnerInfo(12, 34); + window->setOwnerInfo(12, gui::Uid{34}); auto spy = createSpy(); spy->setWatchOutsideTouch(true); - spy->setOwnerInfo(56, 78); + spy->setOwnerInfo(56, gui::Uid{78}); spy->setFrame(Rect{0, 0, 20, 20}); mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {spy, window}}}); @@ -9539,7 +9543,7 @@ public: sp<FakeWindowHandle>::make(overlayApplication, mDispatcher, "Stylus interceptor window", ADISPLAY_ID_DEFAULT); overlay->setFocusable(false); - overlay->setOwnerInfo(111, 111); + overlay->setOwnerInfo(111, gui::Uid{111}); overlay->setTouchable(false); overlay->setInterceptsStylus(true); overlay->setTrustedOverlay(true); @@ -9550,7 +9554,7 @@ public: sp<FakeWindowHandle>::make(application, mDispatcher, "Application window", ADISPLAY_ID_DEFAULT); window->setFocusable(true); - window->setOwnerInfo(222, 222); + window->setOwnerInfo(222, gui::Uid{222}); mDispatcher->setFocusedApplication(ADISPLAY_ID_DEFAULT, application); mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {overlay, window}}}); @@ -9661,11 +9665,11 @@ TEST_F(InputDispatcherStylusInterceptorTest, StylusHandwritingScenario) { struct User { int32_t mPid; - int32_t mUid; + gui::Uid mUid; uint32_t mPolicyFlags{DEFAULT_POLICY_FLAGS}; std::unique_ptr<InputDispatcher>& mDispatcher; - User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, int32_t uid) + User(std::unique_ptr<InputDispatcher>& dispatcher, int32_t pid, gui::Uid uid) : mPid(pid), mUid(uid), mDispatcher(dispatcher) {} InputEventInjectionResult injectTargetedMotion(int32_t action) const { @@ -9698,7 +9702,7 @@ struct User { using InputDispatcherTargetedInjectionTest = InputDispatcherTest; TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { - auto owner = User(mDispatcher, 10, 11); + auto owner = User(mDispatcher, 10, gui::Uid{11}); auto window = owner.createWindow(); mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); @@ -9715,11 +9719,11 @@ TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedWindow) { } TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { - auto owner = User(mDispatcher, 10, 11); + auto owner = User(mDispatcher, 10, gui::Uid{11}); auto window = owner.createWindow(); mDispatcher->setInputWindows({{ADISPLAY_ID_DEFAULT, {window}}}); - auto rando = User(mDispatcher, 20, 21); + auto rando = User(mDispatcher, 20, gui::Uid{21}); EXPECT_EQ(InputEventInjectionResult::TARGET_MISMATCH, rando.injectTargetedMotion(AMOTION_EVENT_ACTION_DOWN)); @@ -9732,7 +9736,7 @@ TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedWindow) { } TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { - auto owner = User(mDispatcher, 10, 11); + auto owner = User(mDispatcher, 10, gui::Uid{11}); auto window = owner.createWindow(); auto spy = owner.createWindow(); spy->setSpy(true); @@ -9746,10 +9750,10 @@ TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoOwnedSpyWindow) { } TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { - auto owner = User(mDispatcher, 10, 11); + auto owner = User(mDispatcher, 10, gui::Uid{11}); auto window = owner.createWindow(); - auto rando = User(mDispatcher, 20, 21); + auto rando = User(mDispatcher, 20, gui::Uid{21}); auto randosSpy = rando.createWindow(); randosSpy->setSpy(true); randosSpy->setTrustedOverlay(true); @@ -9764,10 +9768,10 @@ TEST_F(InputDispatcherTargetedInjectionTest, CannotInjectIntoUnownedSpyWindow) { } TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTargeting) { - auto owner = User(mDispatcher, 10, 11); + auto owner = User(mDispatcher, 10, gui::Uid{11}); auto window = owner.createWindow(); - auto rando = User(mDispatcher, 20, 21); + auto rando = User(mDispatcher, 20, gui::Uid{21}); auto randosSpy = rando.createWindow(); randosSpy->setSpy(true); randosSpy->setTrustedOverlay(true); @@ -9789,10 +9793,10 @@ TEST_F(InputDispatcherTargetedInjectionTest, CanInjectIntoAnyWindowWhenNotTarget } TEST_F(InputDispatcherTargetedInjectionTest, CannotGenerateActionOutsideToOtherUids) { - auto owner = User(mDispatcher, 10, 11); + auto owner = User(mDispatcher, 10, gui::Uid{11}); auto window = owner.createWindow(); - auto rando = User(mDispatcher, 20, 21); + auto rando = User(mDispatcher, 20, gui::Uid{21}); auto randosWindow = rando.createWindow(); randosWindow->setFrame(Rect{-10, -10, -5, -5}); randosWindow->setWatchOutsideTouch(true); diff --git a/services/surfaceflinger/FrontEnd/LayerSnapshot.cpp b/services/surfaceflinger/FrontEnd/LayerSnapshot.cpp index a9925843df..f469a1f624 100644 --- a/services/surfaceflinger/FrontEnd/LayerSnapshot.cpp +++ b/services/surfaceflinger/FrontEnd/LayerSnapshot.cpp @@ -46,7 +46,7 @@ LayerSnapshot::LayerSnapshot(const RequestedLayerState& state, premultipliedAlpha = state.premultipliedAlpha; inputInfo.name = state.name; inputInfo.id = static_cast<int32_t>(uniqueSequence); - inputInfo.ownerUid = static_cast<int32_t>(state.ownerUid); + inputInfo.ownerUid = gui::Uid{state.ownerUid}; inputInfo.ownerPid = state.ownerPid; uid = state.ownerUid; pid = state.ownerPid; diff --git a/services/surfaceflinger/FrontEnd/LayerSnapshotBuilder.cpp b/services/surfaceflinger/FrontEnd/LayerSnapshotBuilder.cpp index 7213ffadcc..806b5024fe 100644 --- a/services/surfaceflinger/FrontEnd/LayerSnapshotBuilder.cpp +++ b/services/surfaceflinger/FrontEnd/LayerSnapshotBuilder.cpp @@ -1007,7 +1007,7 @@ void LayerSnapshotBuilder::updateInput(LayerSnapshot& snapshot, snapshot.inputInfo = {}; // b/271132344 revisit this and see if we can always use the layers uid/pid snapshot.inputInfo.name = requested.name; - snapshot.inputInfo.ownerUid = static_cast<int32_t>(requested.ownerUid); + snapshot.inputInfo.ownerUid = gui::Uid{requested.ownerUid}; snapshot.inputInfo.ownerPid = requested.ownerPid; } snapshot.touchCropId = requested.touchCropId; diff --git a/services/surfaceflinger/Layer.cpp b/services/surfaceflinger/Layer.cpp index fabcd6168c..a5d7ce7966 100644 --- a/services/surfaceflinger/Layer.cpp +++ b/services/surfaceflinger/Layer.cpp @@ -2452,7 +2452,7 @@ void Layer::handleDropInputMode(gui::WindowInfo& info) const { WindowInfo Layer::fillInputInfo(const InputDisplayArgs& displayArgs) { if (!hasInputInfo()) { mDrawingState.inputInfo.name = getName(); - mDrawingState.inputInfo.ownerUid = mOwnerUid; + mDrawingState.inputInfo.ownerUid = gui::Uid{mOwnerUid}; mDrawingState.inputInfo.ownerPid = mOwnerPid; mDrawingState.inputInfo.inputConfig |= WindowInfo::InputConfig::NO_INPUT_CHANNEL; mDrawingState.inputInfo.displayId = getLayerStack().id; |