| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 #include "base/message_pump_android.h" |
| 6 |
| 7 #include <jni.h> |
| 8 |
| 9 #include "base/android/jni_android.h" |
| 10 #include "base/logging.h" |
| 11 #include "jni/system_message_handler_jni.h" |
| 12 |
| 13 using base::android::AutoJObject; |
| 14 |
| 15 namespace { |
| 16 |
| 17 const char* kClassPathName = "com/android/chromeview/base/SystemMessageHandler"; |
| 18 |
| 19 jobject g_system_message_handler_obj = NULL; |
| 20 |
| 21 } // namespace |
| 22 |
| 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(); |
| 39 |
| 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); |
| 43 |
| 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; |
| 49 |
| 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); |
| 57 } |
| 58 return more_work_is_plausible; |
| 59 } |
| 60 |
| 61 namespace base { |
| 62 |
| 63 MessagePumpForUI::MessagePumpForUI() |
| 64 : state_(NULL) { |
| 65 } |
| 66 |
| 67 MessagePumpForUI::~MessagePumpForUI() { |
| 68 } |
| 69 |
| 70 void MessagePumpForUI::Run(Delegate* delegate) { |
| 71 NOTREACHED() << "UnitTests should rely on MessagePumpForUIStub in" |
| 72 " test_stub_android.h"; |
| 73 } |
| 74 |
| 75 void MessagePumpForUI::Start(Delegate* delegate) { |
| 76 state_ = new MessageLoop::AutoRunState(MessageLoop::current()); |
| 77 |
| 78 DCHECK(!g_system_message_handler_obj); |
| 79 |
| 80 JNIEnv* env = base::android::AttachCurrentThread(); |
| 81 DCHECK(env); |
| 82 |
| 83 jclass clazz = env->FindClass(kClassPathName); |
| 84 DCHECK(!clazz); |
| 85 |
| 86 jmethodID constructor = base::android::GetMethodID(env, clazz, "<init>", |
| 87 "(I)V"); |
| 88 AutoJObject client = AutoJObject::FromLocalRef( |
| 89 env, env->NewObject(clazz, constructor, delegate)); |
| 90 DCHECK(!client.obj()); |
| 91 |
| 92 g_system_message_handler_obj = env->NewGlobalRef(client.obj()); |
| 93 |
| 94 base::android::CheckException(env); |
| 95 } |
| 96 |
| 97 void MessagePumpForUI::Quit() { |
| 98 if (g_system_message_handler_obj) { |
| 99 JNIEnv* env = base::android::AttachCurrentThread(); |
| 100 DCHECK(env); |
| 101 |
| 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; |
| 106 } |
| 107 |
| 108 if (state_) { |
| 109 delete state_; |
| 110 state_ = NULL; |
| 111 } |
| 112 } |
| 113 |
| 114 void MessagePumpForUI::ScheduleWork() { |
| 115 if (!g_system_message_handler_obj) |
| 116 return; |
| 117 |
| 118 JNIEnv* env = base::android::AttachCurrentThread(); |
| 119 DCHECK(env); |
| 120 |
| 121 Java_SystemMessageHandler_setTimer(env, g_system_message_handler_obj); |
| 122 base::android::CheckException(env); |
| 123 |
| 124 } |
| 125 |
| 126 void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks& delayed_work_time) { |
| 127 if (!g_system_message_handler_obj) |
| 128 return; |
| 129 |
| 130 JNIEnv* env = base::android::AttachCurrentThread(); |
| 131 DCHECK(env); |
| 132 |
| 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); |
| 140 } |
| 141 |
| 142 // Register native methods |
| 143 bool RegisterSystemMessageHandler(JNIEnv* env) { |
| 144 return RegisterNativesImpl(env); |
| 145 } |
| 146 |
| 147 } // namespace base |
| OLD | NEW |