[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [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 | #ifndef BASE_MESSAGE_LOOP_MESSAGE_PUMP_ANDROID_H_ |
| 6 | #define BASE_MESSAGE_LOOP_MESSAGE_PUMP_ANDROID_H_ |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 7 | |
[email protected] | 4235468 | 2012-07-09 22:12:16 | [diff] [blame] | 8 | #include <jni.h> |
Michael Thiessen | 781ddeb | 2017-11-15 17:07:23 | [diff] [blame] | 9 | #include <memory> |
[email protected] | 4235468 | 2012-07-09 22:12:16 | [diff] [blame] | 10 | |
[email protected] | 3042d1b | 2013-07-02 18:54:05 | [diff] [blame] | 11 | #include "base/android/scoped_java_ref.h" |
[email protected] | be363b2 | 2012-11-01 17:38:47 | [diff] [blame] | 12 | #include "base/base_export.h" |
Michael Thiessen | d7ae735 | 2018-07-10 00:57:13 | [diff] [blame] | 13 | #include "base/callback.h" |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 14 | #include "base/compiler_specific.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 15 | #include "base/macros.h" |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 16 | #include "base/message_loop/message_pump.h" |
Michael Thiessen | 781ddeb | 2017-11-15 17:07:23 | [diff] [blame] | 17 | #include "base/time/time.h" |
Anton Bikineev | 7dd58ad | 2021-05-18 01:01:39 | [diff] [blame] | 18 | #include "third_party/abseil-cpp/absl/types/optional.h" |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 19 | |
Michael Thiessen | d7ae735 | 2018-07-10 00:57:13 | [diff] [blame] | 20 | struct ALooper; |
| 21 | |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 22 | namespace base { |
[email protected] | efbee2031 | 2012-10-10 01:25:05 | [diff] [blame] | 23 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 24 | class RunLoop; |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 25 | |
| 26 | // This class implements a MessagePump needed for TYPE_UI MessageLoops on |
| 27 | // OS_ANDROID platform. |
[email protected] | be363b2 | 2012-11-01 17:38:47 | [diff] [blame] | 28 | class BASE_EXPORT MessagePumpForUI : public MessagePump { |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 29 | public: |
| 30 | MessagePumpForUI(); |
Peter Boström | 7319bbd | 2021-09-15 22:59:38 | [diff] [blame] | 31 | |
| 32 | MessagePumpForUI(const MessagePumpForUI&) = delete; |
| 33 | MessagePumpForUI& operator=(const MessagePumpForUI&) = delete; |
| 34 | |
dcheng | 8e3e03e | 2015-02-06 03:32:20 | [diff] [blame] | 35 | ~MessagePumpForUI() override; |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 36 | |
dcheng | 8e3e03e | 2015-02-06 03:32:20 | [diff] [blame] | 37 | void Run(Delegate* delegate) override; |
| 38 | void Quit() override; |
| 39 | void ScheduleWork() override; |
| 40 | void ScheduleDelayedWork(const TimeTicks& delayed_work_time) override; |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 41 | |
Ran Ji | 3d6ec66 | 2018-07-09 21:18:30 | [diff] [blame] | 42 | // Attaches |delegate| to this native MessagePump. |delegate| will from then |
| 43 | // on be invoked by the native loop to process application tasks. |
| 44 | virtual void Attach(Delegate* delegate); |
gsennton | ebe2e203 | 2016-08-18 22:34:12 | [diff] [blame] | 45 | |
| 46 | // We call Abort when there is a pending JNI exception, meaning that the |
| 47 | // current thread will crash when we return to Java. |
| 48 | // We can't call any JNI-methods before returning to Java as we would then |
| 49 | // cause a native crash (instead of the original Java crash). |
| 50 | void Abort() { should_abort_ = true; } |
Michael Thiessen | d7ae735 | 2018-07-10 00:57:13 | [diff] [blame] | 51 | bool IsAborted() { return should_abort_; } |
| 52 | bool ShouldQuit() const { return should_abort_ || quit_; } |
| 53 | |
| 54 | // Tells the RunLoop to quit when idle, calling the callback when it's safe |
| 55 | // for the Thread to stop. |
| 56 | void QuitWhenIdle(base::OnceClosure callback); |
| 57 | |
| 58 | // These functions are only public so that the looper callbacks can call them, |
| 59 | // and should not be called from outside this class. |
| 60 | void OnDelayedLooperCallback(); |
| 61 | void OnNonDelayedLooperCallback(); |
| 62 | |
| 63 | protected: |
Aaron Colwell | 48c4d507 | 2020-11-13 16:45:03 | [diff] [blame] | 64 | Delegate* SetDelegate(Delegate* delegate); |
| 65 | bool SetQuit(bool quit); |
| 66 | virtual void DoDelayedLooperWork(); |
| 67 | virtual void DoNonDelayedLooperWork(bool do_idle_work); |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 68 | |
| 69 | private: |
Aaron Colwell | 48c4d507 | 2020-11-13 16:45:03 | [diff] [blame] | 70 | void ScheduleWorkInternal(bool do_idle_work); |
Michael Thiessen | d7ae735 | 2018-07-10 00:57:13 | [diff] [blame] | 71 | void DoIdleWork(); |
| 72 | |
| 73 | // Unlike other platforms, we don't control the message loop as it's |
| 74 | // controlled by the Android Looper, so we can't run a RunLoop to keep the |
| 75 | // Thread this pump belongs to alive. However, threads are expected to have an |
| 76 | // active run loop, so we manage a RunLoop internally here, starting/stopping |
| 77 | // it as necessary. |
Michael Thiessen | 781ddeb | 2017-11-15 17:07:23 | [diff] [blame] | 78 | std::unique_ptr<RunLoop> run_loop_; |
Michael Thiessen | d7ae735 | 2018-07-10 00:57:13 | [diff] [blame] | 79 | |
| 80 | // See Abort(). |
Michael Thiessen | dbeca24 | 2017-08-28 21:10:08 | [diff] [blame] | 81 | bool should_abort_ = false; |
Michael Thiessen | d7ae735 | 2018-07-10 00:57:13 | [diff] [blame] | 82 | |
| 83 | // Whether this message pump is quitting, or has quit. |
Michael Thiessen | dbeca24 | 2017-08-28 21:10:08 | [diff] [blame] | 84 | bool quit_ = false; |
Michael Thiessen | d7ae735 | 2018-07-10 00:57:13 | [diff] [blame] | 85 | |
| 86 | // The MessageLoop::Delegate for this pump. |
Michael Thiessen | 781ddeb | 2017-11-15 17:07:23 | [diff] [blame] | 87 | Delegate* delegate_ = nullptr; |
Michael Thiessen | d7ae735 | 2018-07-10 00:57:13 | [diff] [blame] | 88 | |
| 89 | // The time at which we are currently scheduled to wake up and perform a |
Gabriel Charette | 9d44a9b | 2019-04-29 16:35:56 | [diff] [blame] | 90 | // delayed task. This avoids redundantly scheduling |delayed_fd_| with the |
| 91 | // same timeout when subsequent work phases all go idle on the same pending |
| 92 | // delayed task; nullopt if no wakeup is currently scheduled. |
Anton Bikineev | 7dd58ad | 2021-05-18 01:01:39 | [diff] [blame] | 93 | absl::optional<TimeTicks> delayed_scheduled_time_; |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 94 | |
Michael Thiessen | d7ae735 | 2018-07-10 00:57:13 | [diff] [blame] | 95 | // If set, a callback to fire when the message pump is quit. |
| 96 | base::OnceClosure on_quit_callback_; |
| 97 | |
| 98 | // The file descriptor used to signal that non-delayed work is available. |
| 99 | int non_delayed_fd_; |
| 100 | |
| 101 | // The file descriptor used to signal that delayed work is available. |
| 102 | int delayed_fd_; |
| 103 | |
| 104 | // The Android Looper for this thread. |
| 105 | ALooper* looper_ = nullptr; |
| 106 | |
Torne (Richard Coles) | c6993a03 | 2020-02-19 13:23:47 | [diff] [blame] | 107 | // The JNIEnv* for this thread, used to check for pending exceptions. |
| 108 | JNIEnv* env_; |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | } // namespace base |
| 112 | |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 113 | #endif // BASE_MESSAGE_LOOP_MESSAGE_PUMP_ANDROID_H_ |