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 
10 #include "base/android/scoped_java_ref.h"
11 #include "base/base_export.h"
12 #include "base/compiler_specific.h"
13 #include "base/macros.h"
14 #include "base/message_loop/message_pump.h"
15 
16 namespace base {
17 
18 namespace android {
19 class JavaMessageHandlerFactory;
20 }
21 
22 class RunLoop;
23 class TimeTicks;
24 class WaitableEvent;
25 
26 // This class implements a MessagePump needed for TYPE_UI MessageLoops on
27 // OS_ANDROID platform.
28 class BASE_EXPORT MessagePumpForUI : public MessagePump {
29  public:
30   MessagePumpForUI();
31   ~MessagePumpForUI() override;
32 
33   void Run(Delegate* delegate) override;
34   void Quit() override;
35   void ScheduleWork() override;
36   void ScheduleDelayedWork(const TimeTicks& delayed_work_time) override;
37 
38   virtual void Start(Delegate* delegate);
39   void StartForUnitTest(Delegate* delegate,
40                         base::android::JavaMessageHandlerFactory* factory,
41                         WaitableEvent* test_done_event);
42 
43   // We call Abort when there is a pending JNI exception, meaning that the
44   // current thread will crash when we return to Java.
45   // We can't call any JNI-methods before returning to Java as we would then
46   // cause a native crash (instead of the original Java crash).
Abort()47   void Abort() { should_abort_ = true; }
ShouldAbort()48   bool ShouldAbort() const { return should_abort_; }
49 
50   static bool RegisterBindings(JNIEnv* env);
51 
52  private:
53   JNIEnv* StartInternal();
54 
55   RunLoop* run_loop_;
56   base::android::ScopedJavaGlobalRef<jobject> system_message_handler_obj_;
57   bool should_abort_;
58 
59   DISALLOW_COPY_AND_ASSIGN(MessagePumpForUI);
60 };
61 
62 }  // namespace base
63 
64 #endif  // BASE_MESSAGE_LOOP_MESSAGE_PUMP_ANDROID_H_
65