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