blob: 5ec87678ad13e0b1332155b33743f9095354d0a3 [file] [log] [blame]
Kartar Singh17e57592024-12-09 08:05:401// Copyright 2024 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/android/scoped_input_event.h"
6
7#include "base/android/build_info.h"
8#include "base/check.h"
9#include "base/notreached.h"
10
11namespace base::android {
12
13// The class calls AInputEvent_release which was added only in Android S(31).
14#ifndef SCOPED_INPUT_EVENT_MIN_API
15#define SCOPED_INPUT_EVENT_MIN_API 31
16#endif
17
18ScopedInputEvent::ScopedInputEvent(const AInputEvent* event) {
19 CHECK(base::android::BuildInfo::GetInstance()->sdk_int() >=
20 SCOPED_INPUT_EVENT_MIN_API);
21 CHECK(event);
22 a_input_event_ = event;
23}
24
25ScopedInputEvent::~ScopedInputEvent() {
26 DestroyIfNeeded();
27}
28
29ScopedInputEvent::ScopedInputEvent(ScopedInputEvent&& other)
30 : a_input_event_(other.a_input_event_) {
31 other.a_input_event_ = nullptr;
32}
33
34ScopedInputEvent& ScopedInputEvent::operator=(ScopedInputEvent&& other) {
35 if (this != &other) {
36 DestroyIfNeeded();
37 a_input_event_ = other.a_input_event_;
38 other.a_input_event_ = nullptr;
39 }
40 return *this;
41}
42
43void ScopedInputEvent::DestroyIfNeeded() {
44 if (a_input_event_ == nullptr) {
45 return;
46 }
47 // If check to suppress the compiler warning.
48 if (__builtin_available(android SCOPED_INPUT_EVENT_MIN_API, *)) {
49 AInputEvent_release(a_input_event_);
50 a_input_event_ = nullptr;
51 return;
52 }
53 NOTREACHED();
54}
55
56} // namespace base::android