| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/message_loop/message_pump_android.h" | 5 #include "base/message_loop/message_pump_android.h" |
| 6 | 6 |
| 7 #include <jni.h> | 7 #include <jni.h> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/scoped_java_ref.h" | 10 #include "base/android/scoped_java_ref.h" |
| 11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "jni/SystemMessageHandler_jni.h" | 15 #include "jni/SystemMessageHandler_jni.h" |
| 16 | 16 |
| 17 using base::android::ScopedJavaLocalRef; | 17 using base::android::ScopedJavaLocalRef; |
| 18 | 18 |
| 19 // ---------------------------------------------------------------------------- | 19 // ---------------------------------------------------------------------------- |
| 20 // Native JNI methods called by Java. | 20 // Native JNI methods called by Java. |
| 21 // ---------------------------------------------------------------------------- | 21 // ---------------------------------------------------------------------------- |
| 22 // This method can not move to anonymous namespace as it has been declared as | 22 // This method can not move to anonymous namespace as it has been declared as |
| 23 // 'static' in system_message_handler_jni.h. | 23 // 'static' in system_message_handler_jni.h. |
| 24 static jboolean DoRunLoopOnce(JNIEnv* env, jobject obj, jint native_delegate) { | 24 static void DoRunLoopOnce(JNIEnv* env, jobject obj, jint native_delegate) { |
| 25 base::MessagePump::Delegate* delegate = | 25 base::MessagePump::Delegate* delegate = |
| 26 reinterpret_cast<base::MessagePump::Delegate*>(native_delegate); | 26 reinterpret_cast<base::MessagePump::Delegate*>(native_delegate); |
| 27 DCHECK(delegate); | 27 DCHECK(delegate); |
| 28 // This is based on MessagePumpForUI::DoRunLoop() from desktop. | 28 // This is based on MessagePumpForUI::DoRunLoop() from desktop. |
| 29 // Note however that our system queue is handled in the java side. | 29 // Note however that our system queue is handled in the java side. |
| 30 // In desktop we inspect and process a single system message and then | 30 // In desktop we inspect and process a single system message and then |
| 31 // we call DoWork() / DoDelayedWork(). | 31 // we call DoWork() / DoDelayedWork(). |
| 32 // On Android, the java message queue may contain messages for other handlers | 32 // On Android, the java message queue may contain messages for other handlers |
| 33 // that will be processed before calling here again. | 33 // that will be processed before calling here again. |
| 34 bool more_work_is_plausible = delegate->DoWork(); | 34 bool did_work = delegate->DoWork(); |
| 35 | 35 |
| 36 // This is the time when we need to do delayed work. | 36 // This is the time when we need to do delayed work. |
| 37 base::TimeTicks delayed_work_time; | 37 base::TimeTicks delayed_work_time; |
| 38 more_work_is_plausible |= delegate->DoDelayedWork(&delayed_work_time); | 38 did_work |= delegate->DoDelayedWork(&delayed_work_time); |
| 39 |
| 40 // Always call this if there is a delayed message waiting in the queue |
| 41 // since is at most one delayed message in the Java message handler, and this |
| 42 // function call may be the result of that message being handled. |
| 43 if (!delayed_work_time.is_null()) { |
| 44 Java_SystemMessageHandler_setDelayedTimer(env, obj, |
| 45 (delayed_work_time - base::TimeTicks::Now()).InMillisecondsRoundedUp()); |
| 46 } |
| 39 | 47 |
| 40 // This is a major difference between android and other platforms: since we | 48 // This is a major difference between android and other platforms: since we |
| 41 // can't inspect it and process just one single message, instead we'll yeld | 49 // can't inspect it and process just one single message, instead we'll yeld |
| 42 // the callstack, and post a message to call us back soon. | 50 // the callstack. |
| 43 if (more_work_is_plausible) | 51 if (did_work) |
| 44 return true; | 52 return; |
| 45 | 53 |
| 46 more_work_is_plausible = delegate->DoIdleWork(); | 54 delegate->DoIdleWork(); |
| 47 if (!more_work_is_plausible && !delayed_work_time.is_null()) { | |
| 48 // We only set the timer here as returning true would post a message. | |
| 49 jlong millis = | |
| 50 (delayed_work_time - base::TimeTicks::Now()).InMillisecondsRoundedUp(); | |
| 51 Java_SystemMessageHandler_setDelayedTimer(env, obj, millis); | |
| 52 } | |
| 53 return more_work_is_plausible; | |
| 54 } | 55 } |
| 55 | 56 |
| 56 namespace base { | 57 namespace base { |
| 57 | 58 |
| 58 MessagePumpForUI::MessagePumpForUI() | 59 MessagePumpForUI::MessagePumpForUI() |
| 59 : run_loop_(NULL) { | 60 : run_loop_(NULL) { |
| 60 } | 61 } |
| 61 | 62 |
| 62 MessagePumpForUI::~MessagePumpForUI() { | 63 MessagePumpForUI::~MessagePumpForUI() { |
| 63 } | 64 } |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 Java_SystemMessageHandler_setDelayedTimer(env, | 124 Java_SystemMessageHandler_setDelayedTimer(env, |
| 124 system_message_handler_obj_.obj(), millis); | 125 system_message_handler_obj_.obj(), millis); |
| 125 } | 126 } |
| 126 | 127 |
| 127 // static | 128 // static |
| 128 bool MessagePumpForUI::RegisterBindings(JNIEnv* env) { | 129 bool MessagePumpForUI::RegisterBindings(JNIEnv* env) { |
| 129 return RegisterNativesImpl(env); | 130 return RegisterNativesImpl(env); |
| 130 } | 131 } |
| 131 | 132 |
| 132 } // namespace base | 133 } // namespace base |
| OLD | NEW |