blob: 7666b410ea414c31817135591646e7a313530666 [file] [log] [blame]
[email protected]8e937c1e2012-06-28 22:57:301// 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#ifndef BASE_MESSAGE_LOOP_MESSAGE_PUMP_ANDROID_H_
6#define BASE_MESSAGE_LOOP_MESSAGE_PUMP_ANDROID_H_
[email protected]61c86c62011-08-02 16:11:167
[email protected]42354682012-07-09 22:12:168#include <jni.h>
Michael Thiessen781ddeb2017-11-15 17:07:239#include <memory>
[email protected]42354682012-07-09 22:12:1610
[email protected]3042d1b2013-07-02 18:54:0511#include "base/android/scoped_java_ref.h"
[email protected]be363b22012-11-01 17:38:4712#include "base/base_export.h"
Michael Thiessend7ae7352018-07-10 00:57:1313#include "base/callback.h"
[email protected]61c86c62011-08-02 16:11:1614#include "base/compiler_specific.h"
avi9b6f42932015-12-26 22:15:1415#include "base/macros.h"
[email protected]59e69e742013-06-18 20:27:5216#include "base/message_loop/message_pump.h"
Michael Thiessen781ddeb2017-11-15 17:07:2317#include "base/time/time.h"
Anton Bikineev7dd58ad2021-05-18 01:01:3918#include "third_party/abseil-cpp/absl/types/optional.h"
[email protected]61c86c62011-08-02 16:11:1619
Michael Thiessend7ae7352018-07-10 00:57:1320struct ALooper;
21
[email protected]61c86c62011-08-02 16:11:1622namespace base {
[email protected]efbee20312012-10-10 01:25:0523
[email protected]8e937c1e2012-06-28 22:57:3024class RunLoop;
[email protected]61c86c62011-08-02 16:11:1625
26// This class implements a MessagePump needed for TYPE_UI MessageLoops on
27// OS_ANDROID platform.
[email protected]be363b22012-11-01 17:38:4728class BASE_EXPORT MessagePumpForUI : public MessagePump {
[email protected]61c86c62011-08-02 16:11:1629 public:
30 MessagePumpForUI();
Peter Boström7319bbd2021-09-15 22:59:3831
32 MessagePumpForUI(const MessagePumpForUI&) = delete;
33 MessagePumpForUI& operator=(const MessagePumpForUI&) = delete;
34
dcheng8e3e03e2015-02-06 03:32:2035 ~MessagePumpForUI() override;
[email protected]61c86c62011-08-02 16:11:1636
dcheng8e3e03e2015-02-06 03:32:2037 void Run(Delegate* delegate) override;
38 void Quit() override;
39 void ScheduleWork() override;
40 void ScheduleDelayedWork(const TimeTicks& delayed_work_time) override;
[email protected]61c86c62011-08-02 16:11:1641
Ran Ji3d6ec662018-07-09 21:18:3042 // 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);
gsenntonebe2e2032016-08-18 22:34:1245
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 Thiessend7ae7352018-07-10 00:57:1351 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 Colwell48c4d5072020-11-13 16:45:0364 Delegate* SetDelegate(Delegate* delegate);
65 bool SetQuit(bool quit);
66 virtual void DoDelayedLooperWork();
67 virtual void DoNonDelayedLooperWork(bool do_idle_work);
[email protected]61c86c62011-08-02 16:11:1668
69 private:
Aaron Colwell48c4d5072020-11-13 16:45:0370 void ScheduleWorkInternal(bool do_idle_work);
Michael Thiessend7ae7352018-07-10 00:57:1371 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 Thiessen781ddeb2017-11-15 17:07:2378 std::unique_ptr<RunLoop> run_loop_;
Michael Thiessend7ae7352018-07-10 00:57:1379
80 // See Abort().
Michael Thiessendbeca242017-08-28 21:10:0881 bool should_abort_ = false;
Michael Thiessend7ae7352018-07-10 00:57:1382
83 // Whether this message pump is quitting, or has quit.
Michael Thiessendbeca242017-08-28 21:10:0884 bool quit_ = false;
Michael Thiessend7ae7352018-07-10 00:57:1385
86 // The MessageLoop::Delegate for this pump.
Michael Thiessen781ddeb2017-11-15 17:07:2387 Delegate* delegate_ = nullptr;
Michael Thiessend7ae7352018-07-10 00:57:1388
89 // The time at which we are currently scheduled to wake up and perform a
Gabriel Charette9d44a9b2019-04-29 16:35:5690 // 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 Bikineev7dd58ad2021-05-18 01:01:3993 absl::optional<TimeTicks> delayed_scheduled_time_;
[email protected]61c86c62011-08-02 16:11:1694
Michael Thiessend7ae7352018-07-10 00:57:1395 // 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)c6993a032020-02-19 13:23:47107 // The JNIEnv* for this thread, used to check for pending exceptions.
108 JNIEnv* env_;
[email protected]61c86c62011-08-02 16:11:16109};
110
111} // namespace base
112
[email protected]59e69e742013-06-18 20:27:52113#endif // BASE_MESSAGE_LOOP_MESSAGE_PUMP_ANDROID_H_