1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
/*
* Copyright (C) 2010 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.
*/
#define LOG_TAG "InputManager"
//#define LOG_NDEBUG 0
#include "InputManager.h"
#include "InputDispatcherFactory.h"
#include "InputReaderFactory.h"
#include "UnwantedInteractionBlocker.h"
#include <aidl/com/android/server/inputflinger/IInputFlingerRust.h>
#include <android/binder_interface_utils.h>
#include <android/sysprop/InputProperties.sysprop.h>
#include <binder/IPCThreadState.h>
#include <com_android_input_flags.h>
#include <inputflinger_bootstrap.rs.h>
#include <log/log.h>
#include <private/android_filesystem_config.h>
namespace input_flags = com::android::input::flags;
namespace android {
namespace {
const bool ENABLE_INPUT_DEVICE_USAGE_METRICS =
sysprop::InputProperties::enable_input_device_usage_metrics().value_or(true);
const bool ENABLE_POINTER_CHOREOGRAPHER = input_flags::enable_pointer_choreographer();
const bool ENABLE_INPUT_FILTER_RUST = input_flags::enable_input_filter_rust_impl();
int32_t exceptionCodeFromStatusT(status_t status) {
switch (status) {
case OK:
return binder::Status::EX_NONE;
case INVALID_OPERATION:
return binder::Status::EX_UNSUPPORTED_OPERATION;
case BAD_VALUE:
case BAD_TYPE:
case NAME_NOT_FOUND:
return binder::Status::EX_ILLEGAL_ARGUMENT;
case NO_INIT:
return binder::Status::EX_ILLEGAL_STATE;
case PERMISSION_DENIED:
return binder::Status::EX_SECURITY;
default:
return binder::Status::EX_TRANSACTION_FAILED;
}
}
// Convert a binder interface into a raw pointer to an AIBinder.
using IInputFlingerRustBootstrapCallback = aidl::com::android::server::inputflinger::
IInputFlingerRust::IInputFlingerRustBootstrapCallback;
IInputFlingerRustBootstrapCallbackAIBinder* binderToPointer(
IInputFlingerRustBootstrapCallback& interface) {
ndk::SpAIBinder spAIBinder = interface.asBinder();
auto* ptr = spAIBinder.get();
AIBinder_incStrong(ptr);
return ptr;
}
// Create the Rust component of InputFlinger that uses AIDL interfaces as a the foreign function
// interface (FFI). The bootstraping process for IInputFlingerRust is as follows:
// - Create BnInputFlingerRustBootstrapCallback in C++.
// - Use the cxxbridge ffi interface to call the Rust function `create_inputflinger_rust()`, and
// pass the callback binder object as a raw pointer.
// - The Rust implementation will create the implementation of IInputFlingerRust, and pass it
// to C++ through the callback.
// - After the Rust function returns, the binder interface provided to the callback will be the
// only strong reference to the IInputFlingerRust.
std::shared_ptr<IInputFlingerRust> createInputFlingerRust() {
using namespace aidl::com::android::server::inputflinger;
class Callback : public IInputFlingerRust::BnInputFlingerRustBootstrapCallback {
ndk::ScopedAStatus onProvideInputFlingerRust(
const std::shared_ptr<IInputFlingerRust>& inputFlingerRust) override {
mService = inputFlingerRust;
return ndk::ScopedAStatus::ok();
}
public:
std::shared_ptr<IInputFlingerRust> consumeInputFlingerRust() {
auto service = mService;
mService.reset();
return service;
}
private:
std::shared_ptr<IInputFlingerRust> mService;
};
auto callback = ndk::SharedRefBase::make<Callback>();
create_inputflinger_rust(binderToPointer(*callback));
auto service = callback->consumeInputFlingerRust();
LOG_ALWAYS_FATAL_IF(!service,
"create_inputflinger_rust did not provide the IInputFlingerRust "
"implementation through the callback.");
return service;
}
} // namespace
/**
* The event flow is via the "InputListener" interface, as follows:
* InputReader
* -> UnwantedInteractionBlocker
* -> InputFilter
* -> PointerChoreographer
* -> InputProcessor
* -> InputDeviceMetricsCollector
* -> InputDispatcher
*/
InputManager::InputManager(const sp<InputReaderPolicyInterface>& readerPolicy,
InputDispatcherPolicyInterface& dispatcherPolicy,
PointerChoreographerPolicyInterface& choreographerPolicy,
InputFilterPolicyInterface& inputFilterPolicy) {
mInputFlingerRust = createInputFlingerRust();
mDispatcher = createInputDispatcher(dispatcherPolicy);
mTracingStages.emplace_back(
std::make_unique<TracedInputListener>("InputDispatcher", *mDispatcher));
if (ENABLE_INPUT_FILTER_RUST) {
mInputFilter = std::make_unique<InputFilter>(*mTracingStages.back(), *mInputFlingerRust,
inputFilterPolicy);
mTracingStages.emplace_back(
std::make_unique<TracedInputListener>("InputFilter", *mInputFilter));
}
if (ENABLE_INPUT_DEVICE_USAGE_METRICS) {
mCollector = std::make_unique<InputDeviceMetricsCollector>(*mTracingStages.back());
mTracingStages.emplace_back(
std::make_unique<TracedInputListener>("MetricsCollector", *mCollector));
}
mProcessor = std::make_unique<InputProcessor>(*mTracingStages.back());
mTracingStages.emplace_back(
std::make_unique<TracedInputListener>("InputProcessor", *mProcessor));
if (ENABLE_POINTER_CHOREOGRAPHER) {
mChoreographer =
std::make_unique<PointerChoreographer>(*mTracingStages.back(), choreographerPolicy);
mTracingStages.emplace_back(
std::make_unique<TracedInputListener>("PointerChoreographer", *mChoreographer));
}
mBlocker = std::make_unique<UnwantedInteractionBlocker>(*mTracingStages.back());
mTracingStages.emplace_back(
std::make_unique<TracedInputListener>("UnwantedInteractionBlocker", *mBlocker));
mReader = createInputReader(readerPolicy, *mTracingStages.back());
}
InputManager::~InputManager() {
stop();
}
status_t InputManager::start() {
status_t result = mDispatcher->start();
if (result) {
ALOGE("Could not start InputDispatcher thread due to error %d.", result);
return result;
}
result = mReader->start();
if (result) {
ALOGE("Could not start InputReader due to error %d.", result);
mDispatcher->stop();
return result;
}
return OK;
}
status_t InputManager::stop() {
status_t status = OK;
status_t result = mReader->stop();
if (result) {
ALOGW("Could not stop InputReader due to error %d.", result);
status = result;
}
result = mDispatcher->stop();
if (result) {
ALOGW("Could not stop InputDispatcher thread due to error %d.", result);
status = result;
}
return status;
}
InputReaderInterface& InputManager::getReader() {
return *mReader;
}
PointerChoreographerInterface& InputManager::getChoreographer() {
return *mChoreographer;
}
InputProcessorInterface& InputManager::getProcessor() {
return *mProcessor;
}
InputDeviceMetricsCollectorInterface& InputManager::getMetricsCollector() {
return *mCollector;
}
InputDispatcherInterface& InputManager::getDispatcher() {
return *mDispatcher;
}
InputFilterInterface& InputManager::getInputFilter() {
return *mInputFilter;
}
void InputManager::monitor() {
mReader->monitor();
mBlocker->monitor();
mProcessor->monitor();
if (ENABLE_INPUT_DEVICE_USAGE_METRICS) {
mCollector->monitor();
}
mDispatcher->monitor();
}
void InputManager::dump(std::string& dump) {
mReader->dump(dump);
dump += '\n';
mBlocker->dump(dump);
dump += '\n';
if (ENABLE_POINTER_CHOREOGRAPHER) {
mChoreographer->dump(dump);
dump += '\n';
}
mProcessor->dump(dump);
dump += '\n';
if (ENABLE_INPUT_DEVICE_USAGE_METRICS) {
mCollector->dump(dump);
dump += '\n';
}
mDispatcher->dump(dump);
dump += '\n';
}
// Used by tests only.
binder::Status InputManager::createInputChannel(const std::string& name,
android::os::InputChannelCore* outChannel) {
IPCThreadState* ipc = IPCThreadState::self();
const uid_t uid = ipc->getCallingUid();
if (uid != AID_SHELL && uid != AID_ROOT) {
LOG(ERROR) << __func__ << " can only be called by SHELL or ROOT users, "
<< "but was called from UID " << uid;
return binder::Status::
fromExceptionCode(EX_SECURITY,
"This uid is not allowed to call createInputChannel");
}
base::Result<std::unique_ptr<InputChannel>> channel = mDispatcher->createInputChannel(name);
if (!channel.ok()) {
return binder::Status::fromExceptionCode(exceptionCodeFromStatusT(channel.error().code()),
channel.error().message().c_str());
}
InputChannel::moveChannel(std::move(*channel), *outChannel);
return binder::Status::ok();
}
binder::Status InputManager::removeInputChannel(const sp<IBinder>& connectionToken) {
mDispatcher->removeInputChannel(connectionToken);
return binder::Status::ok();
}
status_t InputManager::dump(int fd, const Vector<String16>& args) {
std::string dump;
dump += " InputFlinger dump\n";
TEMP_FAILURE_RETRY(::write(fd, dump.c_str(), dump.size()));
return NO_ERROR;
}
binder::Status InputManager::setFocusedWindow(const gui::FocusRequest& request) {
mDispatcher->setFocusedWindow(request);
return binder::Status::ok();
}
} // namespace android
|