WebKit merge 94748:94770
[chromium-blink-merge.git] / base / message_pump_android.cc
blob7136eff62eeec3124c85b224eec65153c24a1d6b
1 // Copyright (c) 2011 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.
5 #include "base/message_pump_android.h"
7 #include <jni.h>
9 #include "base/android/jni_android.h"
10 #include "base/logging.h"
11 #include "jni/system_message_handler_jni.h"
13 using base::android::ScopedJavaReference;
15 namespace {
17 const char* kClassPathName = "com/android/chromeview/base/SystemMessageHandler";
19 jobject g_system_message_handler_obj = NULL;
21 } // namespace
23 // ----------------------------------------------------------------------------
24 // Native JNI methods called by Java.
25 // ----------------------------------------------------------------------------
26 // This method can not move to anonymous namespace as it has been declared as
27 // 'static' in system_message_handler_jni.h.
28 static jboolean DoRunLoopOnce(JNIEnv* env, jobject obj, jint native_delegate) {
29 base::MessagePump::Delegate* delegate =
30 reinterpret_cast<base::MessagePump::Delegate*>(native_delegate);
31 DCHECK(delegate);
32 // This is based on MessagePumpForUI::DoRunLoop() from desktop.
33 // Note however that our system queue is handled in the java side.
34 // In desktop we inspect and process a single system message and then
35 // we call DoWork() / DoDelayedWork().
36 // On Android, the java message queue may contain messages for other handlers
37 // that will be processed before calling here again.
38 bool more_work_is_plausible = delegate->DoWork();
40 // This is the time when we need to do delayed work.
41 base::TimeTicks delayed_work_time;
42 more_work_is_plausible |= delegate->DoDelayedWork(&delayed_work_time);
44 // This is a major difference between android and other platforms: since we
45 // can't inspect it and process just one single message, instead we'll yeld
46 // the callstack, and post a message to call us back soon.
47 if (more_work_is_plausible)
48 return true;
50 more_work_is_plausible = delegate->DoIdleWork();
51 if (!more_work_is_plausible && !delayed_work_time.is_null()) {
52 // We only set the timer here as returning true would post a message.
53 jlong millis =
54 (delayed_work_time - base::TimeTicks::Now()).InMillisecondsRoundedUp();
55 Java_SystemMessageHandler_setDelayedTimer(env, obj, millis);
56 base::android::CheckException(env);
58 return more_work_is_plausible;
61 namespace base {
63 MessagePumpForUI::MessagePumpForUI()
64 : state_(NULL) {
67 MessagePumpForUI::~MessagePumpForUI() {
70 void MessagePumpForUI::Run(Delegate* delegate) {
71 NOTREACHED() << "UnitTests should rely on MessagePumpForUIStub in"
72 " test_stub_android.h";
75 void MessagePumpForUI::Start(Delegate* delegate) {
76 state_ = new MessageLoop::AutoRunState(MessageLoop::current());
78 DCHECK(!g_system_message_handler_obj);
80 JNIEnv* env = base::android::AttachCurrentThread();
81 DCHECK(env);
83 jclass clazz = env->FindClass(kClassPathName);
84 DCHECK(clazz);
86 jmethodID constructor = base::android::GetMethodID(env, clazz, "<init>",
87 "(I)V");
88 ScopedJavaReference<jobject> client(env, env->NewObject(clazz, constructor,
89 delegate));
90 DCHECK(client.obj());
92 g_system_message_handler_obj = env->NewGlobalRef(client.obj());
94 base::android::CheckException(env);
97 void MessagePumpForUI::Quit() {
98 if (g_system_message_handler_obj) {
99 JNIEnv* env = base::android::AttachCurrentThread();
100 DCHECK(env);
102 Java_SystemMessageHandler_removeTimer(env, g_system_message_handler_obj);
103 env->DeleteGlobalRef(g_system_message_handler_obj);
104 base::android::CheckException(env);
105 g_system_message_handler_obj = NULL;
108 if (state_) {
109 delete state_;
110 state_ = NULL;
114 void MessagePumpForUI::ScheduleWork() {
115 if (!g_system_message_handler_obj)
116 return;
118 JNIEnv* env = base::android::AttachCurrentThread();
119 DCHECK(env);
121 Java_SystemMessageHandler_setTimer(env, g_system_message_handler_obj);
122 base::android::CheckException(env);
126 void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks& delayed_work_time) {
127 if (!g_system_message_handler_obj)
128 return;
130 JNIEnv* env = base::android::AttachCurrentThread();
131 DCHECK(env);
133 jlong millis =
134 (delayed_work_time - base::TimeTicks::Now()).InMillisecondsRoundedUp();
135 // Note that we're truncating to milliseconds as required by the java side,
136 // even though delayed_work_time is microseconds resolution.
137 Java_SystemMessageHandler_setDelayedTimer(env, g_system_message_handler_obj,
138 millis);
139 base::android::CheckException(env);
142 // Register native methods
143 bool RegisterSystemMessageHandler(JNIEnv* env) {
144 return RegisterNativesImpl(env);
147 } // namespace base