blob: 0e7da3f122e56c7e7437c3ac78ad5b82e9e46586 [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"
Gabriel Charette9d44a9b2019-04-29 16:35:5617#include "base/optional.h"
Michael Thiessen781ddeb2017-11-15 17:07:2318#include "base/time/time.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();
dcheng8e3e03e2015-02-06 03:32:2031 ~MessagePumpForUI() override;
[email protected]61c86c62011-08-02 16:11:1632
dcheng8e3e03e2015-02-06 03:32:2033 void Run(Delegate* delegate) override;
34 void Quit() override;
35 void ScheduleWork() override;
36 void ScheduleDelayedWork(const TimeTicks& delayed_work_time) override;
[email protected]61c86c62011-08-02 16:11:1637
Ran Ji3d6ec662018-07-09 21:18:3038 // Attaches |delegate| to this native MessagePump. |delegate| will from then
39 // on be invoked by the native loop to process application tasks.
40 virtual void Attach(Delegate* delegate);
gsenntonebe2e2032016-08-18 22:34:1241
42 // We call Abort when there is a pending JNI exception, meaning that the
43 // current thread will crash when we return to Java.
44 // We can't call any JNI-methods before returning to Java as we would then
45 // cause a native crash (instead of the original Java crash).
46 void Abort() { should_abort_ = true; }
Michael Thiessend7ae7352018-07-10 00:57:1347 bool IsAborted() { return should_abort_; }
48 bool ShouldQuit() const { return should_abort_ || quit_; }
49
50 // Tells the RunLoop to quit when idle, calling the callback when it's safe
51 // for the Thread to stop.
52 void QuitWhenIdle(base::OnceClosure callback);
53
54 // These functions are only public so that the looper callbacks can call them,
55 // and should not be called from outside this class.
56 void OnDelayedLooperCallback();
57 void OnNonDelayedLooperCallback();
58
59 protected:
60 void SetDelegate(Delegate* delegate) { delegate_ = delegate; }
61 virtual bool IsTestImplementation() const;
[email protected]61c86c62011-08-02 16:11:1662
63 private:
Michael Thiessend7ae7352018-07-10 00:57:1364 void DoIdleWork();
65
66 // Unlike other platforms, we don't control the message loop as it's
67 // controlled by the Android Looper, so we can't run a RunLoop to keep the
68 // Thread this pump belongs to alive. However, threads are expected to have an
69 // active run loop, so we manage a RunLoop internally here, starting/stopping
70 // it as necessary.
Michael Thiessen781ddeb2017-11-15 17:07:2371 std::unique_ptr<RunLoop> run_loop_;
Michael Thiessend7ae7352018-07-10 00:57:1372
73 // See Abort().
Michael Thiessendbeca242017-08-28 21:10:0874 bool should_abort_ = false;
Michael Thiessend7ae7352018-07-10 00:57:1375
76 // Whether this message pump is quitting, or has quit.
Michael Thiessendbeca242017-08-28 21:10:0877 bool quit_ = false;
Michael Thiessend7ae7352018-07-10 00:57:1378
79 // The MessageLoop::Delegate for this pump.
Michael Thiessen781ddeb2017-11-15 17:07:2380 Delegate* delegate_ = nullptr;
Michael Thiessend7ae7352018-07-10 00:57:1381
82 // The time at which we are currently scheduled to wake up and perform a
Gabriel Charette9d44a9b2019-04-29 16:35:5683 // delayed task. This avoids redundantly scheduling |delayed_fd_| with the
84 // same timeout when subsequent work phases all go idle on the same pending
85 // delayed task; nullopt if no wakeup is currently scheduled.
86 Optional<TimeTicks> delayed_scheduled_time_;
[email protected]61c86c62011-08-02 16:11:1687
Michael Thiessend7ae7352018-07-10 00:57:1388 // If set, a callback to fire when the message pump is quit.
89 base::OnceClosure on_quit_callback_;
90
91 // The file descriptor used to signal that non-delayed work is available.
92 int non_delayed_fd_;
93
94 // The file descriptor used to signal that delayed work is available.
95 int delayed_fd_;
96
97 // The Android Looper for this thread.
98 ALooper* looper_ = nullptr;
99
Torne (Richard Coles)c6993a032020-02-19 13:23:47100 // The JNIEnv* for this thread, used to check for pending exceptions.
101 JNIEnv* env_;
102
[email protected]61c86c62011-08-02 16:11:16103 DISALLOW_COPY_AND_ASSIGN(MessagePumpForUI);
104};
105
106} // namespace base
107
[email protected]59e69e742013-06-18 20:27:52108#endif // BASE_MESSAGE_LOOP_MESSAGE_PUMP_ANDROID_H_