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