[email protected] | 449019ca | 2012-03-14 22:17:00 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 5 | #include "base/message_loop/message_pump_android.h" |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 6 | |
Michael Thiessen | d7ae735 | 2018-07-10 00:57:13 | [diff] [blame] | 7 | #include <android/looper.h> |
| 8 | #include <errno.h> |
| 9 | #include <fcntl.h> |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 10 | #include <jni.h> |
Michael Thiessen | d7ae735 | 2018-07-10 00:57:13 | [diff] [blame] | 11 | #include <sys/eventfd.h> |
| 12 | #include <sys/syscall.h> |
| 13 | #include <sys/types.h> |
| 14 | #include <unistd.h> |
| 15 | #include <utility> |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 16 | |
| 17 | #include "base/android/jni_android.h" |
[email protected] | 449019ca | 2012-03-14 22:17:00 | [diff] [blame] | 18 | #include "base/android/scoped_java_ref.h" |
Michael Thiessen | d7ae735 | 2018-07-10 00:57:13 | [diff] [blame] | 19 | #include "base/callback_helpers.h" |
[email protected] | 449019ca | 2012-03-14 22:17:00 | [diff] [blame] | 20 | #include "base/lazy_instance.h" |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 21 | #include "base/logging.h" |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 22 | #include "base/run_loop.h" |
Michael Thiessen | 6fd4390 | 2018-08-30 23:14:15 | [diff] [blame^] | 23 | #include "build/build_config.h" |
Michael Thiessen | d7ae735 | 2018-07-10 00:57:13 | [diff] [blame] | 24 | |
| 25 | // Android stripped sys/timerfd.h out of their platform headers, so we have to |
| 26 | // use syscall to make use of timerfd. Once the min API level is 20, we can |
| 27 | // directly use timerfd.h. |
| 28 | #ifndef __NR_timerfd_create |
| 29 | #error "Unable to find syscall for __NR_timerfd_create" |
| 30 | #endif |
| 31 | |
| 32 | #ifndef TFD_TIMER_ABSTIME |
| 33 | #define TFD_TIMER_ABSTIME (1 << 0) |
| 34 | #endif |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 35 | |
torne | 8656011 | 2016-08-04 15:59:04 | [diff] [blame] | 36 | using base::android::JavaParamRef; |
[email protected] | 449019ca | 2012-03-14 22:17:00 | [diff] [blame] | 37 | using base::android::ScopedJavaLocalRef; |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 38 | |
Michael Thiessen | 781ddeb | 2017-11-15 17:07:23 | [diff] [blame] | 39 | namespace base { |
| 40 | |
Michael Thiessen | d7ae735 | 2018-07-10 00:57:13 | [diff] [blame] | 41 | namespace { |
Michael Thiessen | 781ddeb | 2017-11-15 17:07:23 | [diff] [blame] | 42 | |
Michael Thiessen | d7ae735 | 2018-07-10 00:57:13 | [diff] [blame] | 43 | // See sys/timerfd.h |
| 44 | int timerfd_create(int clockid, int flags) { |
| 45 | return syscall(__NR_timerfd_create, clockid, flags); |
Michael Thiessen | 781ddeb | 2017-11-15 17:07:23 | [diff] [blame] | 46 | } |
| 47 | |
Michael Thiessen | d7ae735 | 2018-07-10 00:57:13 | [diff] [blame] | 48 | // See sys/timerfd.h |
| 49 | int timerfd_settime(int ufc, |
| 50 | int flags, |
| 51 | const struct itimerspec* utmr, |
| 52 | struct itimerspec* otmr) { |
| 53 | return syscall(__NR_timerfd_settime, ufc, flags, utmr, otmr); |
| 54 | } |
Michael Thiessen | 781ddeb | 2017-11-15 17:07:23 | [diff] [blame] | 55 | |
Michael Thiessen | 6fd4390 | 2018-08-30 23:14:15 | [diff] [blame^] | 56 | // https://crbug.com/873588. The stack may not be aligned when the ALooper calls |
| 57 | // into our code due to the inconsistent ABI on older Android OS versions. |
| 58 | #if defined(ARCH_CPU_X86) |
| 59 | #define STACK_ALIGN __attribute__((force_align_arg_pointer)) |
| 60 | #else |
| 61 | #define STACK_ALIGN |
| 62 | #endif |
| 63 | |
| 64 | STACK_ALIGN int NonDelayedLooperCallback(int fd, int events, void* data) { |
Michael Thiessen | d7ae735 | 2018-07-10 00:57:13 | [diff] [blame] | 65 | if (events & ALOOPER_EVENT_HANGUP) |
| 66 | return 0; |
| 67 | |
| 68 | DCHECK(events & ALOOPER_EVENT_INPUT); |
| 69 | MessagePumpForUI* pump = reinterpret_cast<MessagePumpForUI*>(data); |
| 70 | pump->OnNonDelayedLooperCallback(); |
| 71 | return 1; // continue listening for events |
| 72 | } |
| 73 | |
Michael Thiessen | 6fd4390 | 2018-08-30 23:14:15 | [diff] [blame^] | 74 | STACK_ALIGN int DelayedLooperCallback(int fd, int events, void* data) { |
Michael Thiessen | d7ae735 | 2018-07-10 00:57:13 | [diff] [blame] | 75 | if (events & ALOOPER_EVENT_HANGUP) |
| 76 | return 0; |
|
|