message_pump_android.h 3.6 KB

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