| 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/java_message_handler_factory.h" |
| 9 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
| 10 #include "base/android/scoped_java_ref.h" | 11 #include "base/android/scoped_java_ref.h" |
| 11 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/message_loop/message_loop.h" |
| 13 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 14 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 15 #include "jni/SystemMessageHandler_jni.h" | 17 #include "jni/SystemMessageHandler_jni.h" |
| 16 | 18 |
| 17 using base::android::JavaParamRef; | 19 using base::android::JavaParamRef; |
| 18 using base::android::ScopedJavaLocalRef; | 20 using base::android::ScopedJavaLocalRef; |
| 19 | 21 |
| 20 // ---------------------------------------------------------------------------- | 22 // ---------------------------------------------------------------------------- |
| 21 // Native JNI methods called by Java. | 23 // Native JNI methods called by Java. |
| 22 // ---------------------------------------------------------------------------- | 24 // ---------------------------------------------------------------------------- |
| 23 // This method can not move to anonymous namespace as it has been declared as | 25 // This method can not move to anonymous namespace as it has been declared as |
| 24 // 'static' in system_message_handler_jni.h. | 26 // 'static' in system_message_handler_jni.h. |
| 25 static void DoRunLoopOnce(JNIEnv* env, | 27 static void DoRunLoopOnce(JNIEnv* env, |
| 26 const JavaParamRef<jobject>& obj, | 28 const JavaParamRef<jobject>& obj, |
| 27 jlong native_delegate, | 29 jlong native_delegate, |
| 30 jlong native_message_pump, |
| 28 jlong delayed_scheduled_time_ticks) { | 31 jlong delayed_scheduled_time_ticks) { |
| 29 base::MessagePump::Delegate* delegate = | 32 base::MessagePump::Delegate* delegate = |
| 30 reinterpret_cast<base::MessagePump::Delegate*>(native_delegate); | 33 reinterpret_cast<base::MessagePump::Delegate*>(native_delegate); |
| 31 DCHECK(delegate); | 34 DCHECK(delegate); |
| 35 base::MessagePumpForUI* pump = |
| 36 reinterpret_cast<base::MessagePumpForUI*>(native_message_pump); |
| 37 DCHECK(pump); |
| 32 // This is based on MessagePumpForUI::DoRunLoop() from desktop. | 38 // This is based on MessagePumpForUI::DoRunLoop() from desktop. |
| 33 // Note however that our system queue is handled in the java side. | 39 // 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 | 40 // In desktop we inspect and process a single system message and then |
| 35 // we call DoWork() / DoDelayedWork(). | 41 // we call DoWork() / DoDelayedWork(). |
| 36 // On Android, the java message queue may contain messages for other handlers | 42 // On Android, the java message queue may contain messages for other handlers |
| 37 // that will be processed before calling here again. | 43 // that will be processed before calling here again. |
| 38 bool did_work = delegate->DoWork(); | 44 bool did_work = delegate->DoWork(); |
| 45 if (pump->ShouldAbort()) { |
| 46 // There is a pending JNI exception, return to Java so that the exception is |
| 47 // thrown correctly. |
| 48 return; |
| 49 } |
| 39 | 50 |
| 40 // In the java side, |SystemMessageHandler| keeps a single "delayed" message. | 51 // In the java side, |SystemMessageHandler| keeps a single "delayed" message. |
| 41 // It's an expensive operation to |removeMessage| there, so this is optimized | 52 // It's an expensive operation to |removeMessage| there, so this is optimized |
| 42 // to avoid those calls. | 53 // to avoid those calls. |
| 43 // | 54 // |
| 44 // At this stage, |next_delayed_work_time| can be: | 55 // At this stage, |next_delayed_work_time| can be: |
| 45 // 1) The same as previously scheduled: nothing to be done, move along. This | 56 // 1) The same as previously scheduled: nothing to be done, move along. This |
| 46 // is the typical case, since this method is called for every single message. | 57 // is the typical case, since this method is called for every single message. |
| 47 // | 58 // |
| 48 // 2) Not previously scheduled: just post a new message in java. | 59 // 2) Not previously scheduled: just post a new message in java. |
| 49 // | 60 // |
| 50 // 3) Shorter than previously scheduled: far less common. In this case, | 61 // 3) Shorter than previously scheduled: far less common. In this case, |
| 51 // |removeMessage| and post a new one. | 62 // |removeMessage| and post a new one. |
| 52 // | 63 // |
| 53 // 4) Longer than previously scheduled (or null): nothing to be done, move | 64 // 4) Longer than previously scheduled (or null): nothing to be done, move |
| 54 // along. | 65 // along. |
| 55 // | 66 // |
| 56 // Side note: base::TimeTicks is a C++ representation and can't be | 67 // Side note: base::TimeTicks is a C++ representation and can't be |
| 57 // compared in java. When calling |scheduleDelayedWork|, pass the | 68 // compared in java. When calling |scheduleDelayedWork|, pass the |
| 58 // |InternalValue()| to java and then back to C++ so the comparisons can be | 69 // |InternalValue()| to java and then back to C++ so the comparisons can be |
| 59 // done here. | 70 // done here. |
| 60 // This roundtrip allows comparing TimeTicks directly (cheap) and | 71 // This roundtrip allows comparing TimeTicks directly (cheap) and |
| 61 // avoid comparisons with TimeDelta / Now() (expensive). | 72 // avoid comparisons with TimeDelta / Now() (expensive). |
| 62 base::TimeTicks next_delayed_work_time; | 73 base::TimeTicks next_delayed_work_time; |
| 63 did_work |= delegate->DoDelayedWork(&next_delayed_work_time); | 74 did_work |= delegate->DoDelayedWork(&next_delayed_work_time); |
| 75 if (pump->ShouldAbort()) { |
| 76 // There is a pending JNI exception, return to Java so that the exception is |
| 77 // thrown correctly |
| 78 return; |
| 79 } |
| 64 | 80 |
| 65 if (!next_delayed_work_time.is_null()) { | 81 if (!next_delayed_work_time.is_null()) { |
| 66 // Schedule a new message if there's nothing already scheduled or there's a | 82 // Schedule a new message if there's nothing already scheduled or there's a |
| 67 // shorter delay than previously scheduled (see (2) and (3) above). | 83 // shorter delay than previously scheduled (see (2) and (3) above). |
| 68 if (delayed_scheduled_time_ticks == 0 || | 84 if (delayed_scheduled_time_ticks == 0 || |
| 69 next_delayed_work_time < base::TimeTicks::FromInternalValue( | 85 next_delayed_work_time < base::TimeTicks::FromInternalValue( |
| 70 delayed_scheduled_time_ticks)) { | 86 delayed_scheduled_time_ticks)) { |
| 71 Java_SystemMessageHandler_scheduleDelayedWork(env, obj, | 87 Java_SystemMessageHandler_scheduleDelayedWork(env, obj, |
| 72 next_delayed_work_time.ToInternalValue(), | 88 next_delayed_work_time.ToInternalValue(), |
| 73 (next_delayed_work_time - | 89 (next_delayed_work_time - |
| 74 base::TimeTicks::Now()).InMillisecondsRoundedUp()); | 90 base::TimeTicks::Now()).InMillisecondsRoundedUp()); |
| 75 } | 91 } |
| 76 } | 92 } |
| 77 | 93 |
| 78 // This is a major difference between android and other platforms: since we | 94 // This is a major difference between android and other platforms: since we |
| 79 // can't inspect it and process just one single message, instead we'll yeld | 95 // can't inspect it and process just one single message, instead we'll yeld |
| 80 // the callstack. | 96 // the callstack. |
| 81 if (did_work) | 97 if (did_work) |
| 82 return; | 98 return; |
| 83 | 99 |
| 84 delegate->DoIdleWork(); | 100 delegate->DoIdleWork(); |
| 101 // Note that we do not check whether we should abort here since we are |
| 102 // returning to the JVM anyway. If, in the future, we add any more code after |
| 103 // the call to DoIdleWork() here, we should add an abort-check and return |
| 104 // immediately if the check passes. |
| 85 } | 105 } |
| 86 | 106 |
| 87 namespace base { | 107 namespace base { |
| 88 | 108 |
| 89 MessagePumpForUI::MessagePumpForUI() | 109 MessagePumpForUI::MessagePumpForUI() |
| 90 : run_loop_(NULL) { | 110 : run_loop_(nullptr), should_abort_(false) {} |
| 91 } | |
| 92 | 111 |
| 93 MessagePumpForUI::~MessagePumpForUI() { | 112 MessagePumpForUI::~MessagePumpForUI() { |
| 94 } | 113 } |
| 95 | 114 |
| 96 void MessagePumpForUI::Run(Delegate* delegate) { | 115 void MessagePumpForUI::Run(Delegate* delegate) { |
| 97 NOTREACHED() << "UnitTests should rely on MessagePumpForUIStub in" | 116 NOTREACHED() << "UnitTests should rely on MessagePumpForUIStub in" |
| 98 " test_stub_android.h"; | 117 " test_stub_android.h"; |
| 99 } | 118 } |
| 100 | 119 |
| 101 void MessagePumpForUI::Start(Delegate* delegate) { | 120 JNIEnv* MessagePumpForUI::StartInternal() { |
| 102 run_loop_ = new RunLoop(); | 121 run_loop_ = new RunLoop(); |
| 103 // Since the RunLoop was just created above, BeforeRun should be guaranteed to | 122 // Since the RunLoop was just created above, BeforeRun should be guaranteed to |
| 104 // return true (it only returns false if the RunLoop has been Quit already). | 123 // return true (it only returns false if the RunLoop has been Quit already). |
| 105 if (!run_loop_->BeforeRun()) | 124 if (!run_loop_->BeforeRun()) |
| 106 NOTREACHED(); | 125 NOTREACHED(); |
| 107 | 126 |
| 108 DCHECK(system_message_handler_obj_.is_null()); | 127 DCHECK(system_message_handler_obj_.is_null()); |
| 109 | 128 |
| 110 JNIEnv* env = base::android::AttachCurrentThread(); | 129 JNIEnv* env = base::android::AttachCurrentThread(); |
| 111 DCHECK(env); | 130 DCHECK(env); |
| 131 return env; |
| 132 } |
| 112 | 133 |
| 134 void MessagePumpForUI::Start(Delegate* delegate) { |
| 135 JNIEnv* env = StartInternal(); |
| 136 system_message_handler_obj_.Reset(Java_SystemMessageHandler_create( |
| 137 env, reinterpret_cast<intptr_t>(delegate), |
| 138 reinterpret_cast<intptr_t>(this))); |
| 139 } |
| 140 |
| 141 void MessagePumpForUI::StartForUnitTest( |
| 142 Delegate* delegate, |
| 143 base::android::JavaMessageHandlerFactory* factory, |
| 144 WaitableEvent* test_done_event) { |
| 145 JNIEnv* env = StartInternal(); |
| 113 system_message_handler_obj_.Reset( | 146 system_message_handler_obj_.Reset( |
| 114 Java_SystemMessageHandler_create( | 147 factory->CreateMessageHandler(env, delegate, this, test_done_event)); |
| 115 env, reinterpret_cast<intptr_t>(delegate))); | |
| 116 } | 148 } |
| 117 | 149 |
| 118 void MessagePumpForUI::Quit() { | 150 void MessagePumpForUI::Quit() { |
| 119 if (!system_message_handler_obj_.is_null()) { | 151 if (!system_message_handler_obj_.is_null()) { |
| 120 JNIEnv* env = base::android::AttachCurrentThread(); | 152 JNIEnv* env = base::android::AttachCurrentThread(); |
| 121 DCHECK(env); | 153 DCHECK(env); |
| 122 | 154 |
| 123 Java_SystemMessageHandler_removeAllPendingMessages( | 155 Java_SystemMessageHandler_removeAllPendingMessages( |
| 124 env, system_message_handler_obj_); | 156 env, system_message_handler_obj_); |
| 125 system_message_handler_obj_.Reset(); | 157 system_message_handler_obj_.Reset(); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 155 env, system_message_handler_obj_, delayed_work_time.ToInternalValue(), | 187 env, system_message_handler_obj_, delayed_work_time.ToInternalValue(), |
| 156 millis); | 188 millis); |
| 157 } | 189 } |
| 158 | 190 |
| 159 // static | 191 // static |
| 160 bool MessagePumpForUI::RegisterBindings(JNIEnv* env) { | 192 bool MessagePumpForUI::RegisterBindings(JNIEnv* env) { |
| 161 return RegisterNativesImpl(env); | 193 return RegisterNativesImpl(env); |
| 162 } | 194 } |
| 163 | 195 |
| 164 } // namespace base | 196 } // namespace base |
| OLD | NEW |