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