xref: /aosp_15_r20/external/cronet/base/message_loop/message_pump_android.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 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 #ifndef BASE_MESSAGE_LOOP_MESSAGE_PUMP_ANDROID_H_
6 #define BASE_MESSAGE_LOOP_MESSAGE_PUMP_ANDROID_H_
7 
8 #include <jni.h>
9 
10 #include <optional>
11 
12 #include "base/base_export.h"
13 #include "base/functional/callback.h"
14 #include "base/memory/raw_ptr.h"
15 #include "base/message_loop/message_pump.h"
16 #include "base/time/time.h"
17 
18 struct ALooper;
19 
20 namespace base {
21 
22 class RunLoop;
23 
24 // This class implements a MessagePump needed for MessagePumpType::UI and
25 // MessagePumpType::JAVA MessageLoops on OS_ANDROID platform.
26 //
27 // It works by registering two file descriptors for the Looper to additionally
28 // poll: one for delayed work and one for non-delayed work. For queueing
29 // immediate work within the Looper it writes to the eventfd(2). For delayed
30 // work it performs timerfd_settime(2).
31 //
32 // See: https://developer.android.com/ndk/reference/group/looper.
33 class BASE_EXPORT MessagePumpAndroid : public MessagePump {
34  public:
35   MessagePumpAndroid();
36 
37   MessagePumpAndroid(const MessagePumpAndroid&) = delete;
38   MessagePumpAndroid& operator=(const MessagePumpAndroid&) = delete;
39 
40   ~MessagePumpAndroid() override;
41 
42   void Run(Delegate* delegate) override;
43   void Quit() override;
44   void ScheduleWork() override;
45   void ScheduleDelayedWork(
46       const Delegate::NextWorkInfo& next_work_info) override;
47 
48   // Attaches |delegate| to this native MessagePump. |delegate| will from then
49   // on be invoked by the native loop to process application tasks.
50   virtual void Attach(Delegate* delegate);
51 
52   // We call Abort when there is a pending JNI exception, meaning that the
53   // current thread will crash when we return to Java.
54   // We can't call any JNI-methods before returning to Java as we would then
55   // cause a native crash (instead of the original Java crash).
Abort()56   void Abort() { should_abort_ = true; }
IsAborted()57   bool IsAborted() { return should_abort_; }
ShouldQuit()58   bool ShouldQuit() const { return should_abort_ || quit_; }
59 
60   // Tells the RunLoop to quit when idle, calling the callback when it's safe
61   // for the Thread to stop.
62   void QuitWhenIdle(base::OnceClosure callback);
63 
64   // These functions are only public so that the looper callbacks can call them,
65   // and should not be called from outside this class.
66   void OnDelayedLooperCallback();
67   virtual void OnNonDelayedLooperCallback();  // Overridden for testing.
68 
set_is_type_ui(bool is_type_ui)69   void set_is_type_ui(bool is_type_ui) { is_type_ui_ = is_type_ui; }
70 
71  protected:
72   Delegate* SetDelegate(Delegate* delegate);
73   bool SetQuit(bool quit);
74   virtual void DoDelayedLooperWork();
75   virtual void DoNonDelayedLooperWork(bool do_idle_work);
76 
77  private:
78   void ScheduleWorkInternal(bool do_idle_work);
79   void DoIdleWork();
80 
81   // Unlike other platforms, we don't control the message loop as it's
82   // controlled by the Android Looper, so we can't run a RunLoop to keep the
83   // Thread this pump belongs to alive. However, threads are expected to have an
84   // active run loop, so we manage a RunLoop internally here, starting/stopping
85   // it as necessary.
86   std::unique_ptr<RunLoop> run_loop_;
87 
88   // See Abort().
89   bool should_abort_ = false;
90 
91   // Whether this message pump is quitting, or has quit.
92   bool quit_ = false;
93 
94   // The MessageLoop::Delegate for this pump.
95   raw_ptr<Delegate> delegate_ = nullptr;
96 
97   // The time at which we are currently scheduled to wake up and perform a
98   // delayed task. This avoids redundantly scheduling |delayed_fd_| with the
99   // same timeout when subsequent work phases all go idle on the same pending
100   // delayed task; nullopt if no wakeup is currently scheduled.
101   std::optional<TimeTicks> delayed_scheduled_time_;
102 
103   // If set, a callback to fire when the message pump is quit.
104   base::OnceClosure on_quit_callback_;
105 
106   // The file descriptor used to signal that non-delayed work is available.
107   int non_delayed_fd_;
108 
109   // The file descriptor used to signal that delayed work is available.
110   int delayed_fd_;
111 
112   // The Android Looper for this thread.
113   raw_ptr<ALooper> looper_ = nullptr;
114 
115   // The JNIEnv* for this thread, used to check for pending exceptions.
116   raw_ptr<JNIEnv> env_;
117 
118   // Whether this message serves a MessagePumpType::UI, and therefore can
119   // consult with the input hint living on the UI thread.
120   bool is_type_ui_ = false;
121 };
122 
123 }  // namespace base
124 
125 #endif  // BASE_MESSAGE_LOOP_MESSAGE_PUMP_ANDROID_H_
126