1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
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/callback.h"
14 #include "base/compiler_specific.h"
15 #include "base/macros.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 RunLoop;
24 
25 // This class implements a MessagePump needed for TYPE_UI MessageLoops on
26 // OS_ANDROID platform.
27 class BASE_EXPORT MessagePumpForUI : public MessagePump {
28  public:
29   MessagePumpForUI();
30   ~MessagePumpForUI() override;
31 
32   void Run(Delegate* delegate) override;
33   void Quit() override;
34   void ScheduleWork() override;
35   void ScheduleDelayedWork(const TimeTicks& delayed_work_time) override;
36 
37   // Attaches |delegate| to this native MessagePump. |delegate| will from then
38   // on be invoked by the native loop to process application tasks.
39   virtual void Attach(Delegate* delegate);
40 
41   // We call Abort when there is a pending JNI exception, meaning that the
42   // current thread will crash when we return to Java.
43   // We can't call any JNI-methods before returning to Java as we would then
44   // cause a native crash (instead of the original Java crash).
Abort()45   void Abort() { should_abort_ = true; }
IsAborted()46   bool IsAborted() { return should_abort_; }
ShouldQuit()47   bool ShouldQuit() const { return should_abort_ || quit_; }
48 
49   // Tells the RunLoop to quit when idle, calling the callback when it's safe
50   // for the Thread to stop.
51   void QuitWhenIdle(base::OnceClosure callback);
52 
53   // These functions are only public so that the looper callbacks can call them,
54   // and should not be called from outside this class.
55   void OnDelayedLooperCallback();
56   void OnNonDelayedLooperCallback();
57 
58  protected:
SetDelegate(Delegate * delegate)59   void SetDelegate(Delegate* delegate) { delegate_ = delegate; }
60   virtual bool IsTestImplementation() const;
61 
62  private:
63   void DoIdleWork();
64 
65   // Unlike other platforms, we don't control the message loop as it's
66   // controlled by the Android Looper, so we can't run a RunLoop to keep the
67   // Thread this pump belongs to alive. However, threads are expected to have an
68   // active run loop, so we manage a RunLoop internally here, starting/stopping
69   // it as necessary.
70   std::unique_ptr<RunLoop> run_loop_;
71 
72   // See Abort().
73   bool should_abort_ = false;
74 
75   // Whether this message pump is quitting, or has quit.
76   bool quit_ = false;
77 
78   // The MessageLoop::Delegate for this pump.
79   Delegate* delegate_ = nullptr;
80 
81   // The time at which we are currently scheduled to wake up and perform a
82   // delayed task.
83   base::TimeTicks delayed_scheduled_time_;
84 
85   // If set, a callback to fire when the message pump is quit.
86   base::OnceClosure on_quit_callback_;
87 
88   // The file descriptor used to signal that non-delayed work is available.
89   int non_delayed_fd_;
90 
91   // The file descriptor used to signal that delayed work is available.
92   int delayed_fd_;
93 
94   // The Android Looper for this thread.
95   ALooper* looper_ = nullptr;
96 
97   DISALLOW_COPY_AND_ASSIGN(MessagePumpForUI);
98 };
99 
100 }  // namespace base
101 
102 #endif  // BASE_MESSAGE_LOOP_MESSAGE_PUMP_ANDROID_H_
103