blob: 70e72f0e0b5b8ec05aeb31ba705f70650f5406a9 [file] [log] [blame]
[email protected]449019ca2012-03-14 22:17:001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]61c86c62011-08-02 16:11:162// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]59e69e742013-06-18 20:27:525#include "base/message_loop/message_pump_android.h"
[email protected]61c86c62011-08-02 16:11:166
Michael Thiessend7ae7352018-07-10 00:57:137#include <android/looper.h>
8#include <errno.h>
9#include <fcntl.h>
[email protected]61c86c62011-08-02 16:11:1610#include <jni.h>
Michael Thiessend7ae7352018-07-10 00:57:1311#include <sys/eventfd.h>
12#include <sys/syscall.h>
13#include <sys/types.h>
14#include <unistd.h>
15#include <utility>
[email protected]61c86c62011-08-02 16:11:1616
17#include "base/android/jni_android.h"
[email protected]449019ca2012-03-14 22:17:0018#include "base/android/scoped_java_ref.h"
Michael Thiessend7ae7352018-07-10 00:57:1319#include "base/callback_helpers.h"
[email protected]449019ca2012-03-14 22:17:0020#include "base/lazy_instance.h"
[email protected]61c86c62011-08-02 16:11:1621#include "base/logging.h"
[email protected]8e937c1e2012-06-28 22:57:3022#include "base/run_loop.h"
Michael Thiessen6fd43902018-08-30 23:14:1523#include "build/build_config.h"
Michael Thiessend7ae7352018-07-10 00:57:1324
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]61c86c62011-08-02 16:11:1635
torne86560112016-08-04 15:59:0436using base::android::JavaParamRef;
[email protected]449019ca2012-03-14 22:17:0037using base::android::ScopedJavaLocalRef;
[email protected]61c86c62011-08-02 16:11:1638
Michael Thiessen781ddeb2017-11-15 17:07:2339namespace base {
40
Michael Thiessend7ae7352018-07-10 00:57:1341namespace {
Michael Thiessen781ddeb2017-11-15 17:07:2342
Michael Thiessend7ae7352018-07-10 00:57:1343// See sys/timerfd.h
44int timerfd_create(int clockid, int flags) {
45 return syscall(__NR_timerfd_create, clockid, flags);
Michael Thiessen781ddeb2017-11-15 17:07:2346}
47
Michael Thiessend7ae7352018-07-10 00:57:1348// See sys/timerfd.h
49int 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 Thiessen781ddeb2017-11-15 17:07:2355
Michael Thiessen6fd43902018-08-30 23:14:1556// 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
64STACK_ALIGN int NonDelayedLooperCallback(int fd, int events, void* data) {
Michael Thiessend7ae7352018-07-10 00:57:1365 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 Thiessen6fd43902018-08-30 23:14:1574STACK_ALIGN int DelayedLooperCallback(int fd, int events, void* data) {
Michael Thiessend7ae7352018-07-10 00:57:1375 if (events & ALOOPER_EVENT_HANGUP)
76 return 0;