[email protected] | 449019ca | 2012-03-14 22:17:00 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 5 | #include "base/message_loop/message_pump_android.h" |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 6 | |
| 7 | #include <jni.h> |
| 8 | |
gsennton | ebe2e203 | 2016-08-18 22:34:12 | [diff] [blame] | 9 | #include "base/android/java_message_handler_factory.h" |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 10 | #include "base/android/jni_android.h" |
[email protected] | 449019ca | 2012-03-14 22:17:00 | [diff] [blame] | 11 | #include "base/android/scoped_java_ref.h" |
| 12 | #include "base/lazy_instance.h" |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 13 | #include "base/logging.h" |
gsennton | ebe2e203 | 2016-08-18 22:34:12 | [diff] [blame] | 14 | #include "base/message_loop/message_loop.h" |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 15 | #include "base/run_loop.h" |
[email protected] | 99084f6 | 2013-06-28 00:49:07 | [diff] [blame] | 16 | #include "base/time/time.h" |
[email protected] | e46f6615 | 2012-07-19 20:02:55 | [diff] [blame] | 17 | #include "jni/SystemMessageHandler_jni.h" |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 18 | |
torne | 8656011 | 2016-08-04 15:59:04 | [diff] [blame] | 19 | using base::android::JavaParamRef; |
[email protected] | 449019ca | 2012-03-14 22:17:00 | [diff] [blame] | 20 | using base::android::ScopedJavaLocalRef; |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 21 | |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 22 | // ---------------------------------------------------------------------------- |
| 23 | // Native JNI methods called by Java. |
| 24 | // ---------------------------------------------------------------------------- |
| 25 | // This method can not move to anonymous namespace as it has been declared as |
| 26 | // 'static' in system_message_handler_jni.h. |
torne | 89cc5d9 | 2015-09-04 11:16:35 | [diff] [blame] | 27 | static void DoRunLoopOnce(JNIEnv* env, |
| 28 | const JavaParamRef<jobject>& obj, |
| 29 | jlong native_delegate, |
gsennton | ebe2e203 | 2016-08-18 22:34:12 | [diff] [blame] | 30 | jlong native_message_pump, |
torne | 89cc5d9 | 2015-09-04 11:16:35 | [diff] [blame] | 31 | jlong delayed_scheduled_time_ticks) { |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 32 | base::MessagePump::Delegate* delegate = |
| 33 | reinterpret_cast<base::MessagePump::Delegate*>(native_delegate); |
| 34 | DCHECK(delegate); |
gsennton | ebe2e203 | 2016-08-18 22:34:12 | [diff] [blame] | 35 | base::MessagePumpForUI* pump = |
| 36 | reinterpret_cast<base::MessagePumpForUI*>(native_message_pump); |
| 37 | DCHECK(pump); |
Michael Thiessen | fc7067fe | 2017-11-01 22:33:01 | [diff] [blame^] | 38 | // If the pump has been aborted, tasks may continue to be queued up, but |
| 39 | // shouldn't run. |
| 40 | if (pump->ShouldAbort()) |
| 41 | return; |
| 42 | |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 43 | // This is based on MessagePumpForUI::DoRunLoop() from desktop. |
| 44 | // Note however that our system queue is handled in the java side. |
| 45 | // In desktop we inspect and process a single system message and then |
Michael Thiessen | 7681cfee | 2017-08-18 16:55:37 | [diff] [blame] | 46 | // we call DoWork() / DoDelayedWork(). This is then wrapped in a for loop and |
| 47 | // repeated until no work is left to do, at which point DoIdleWork is called. |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 48 | // On Android, the java message queue may contain messages for other handlers |
| 49 | // that will be processed before calling here again. |
Michael Thiessen | 7681cfee | 2017-08-18 16:55:37 | [diff] [blame] | 50 | // This means that unlike Desktop, we can't wrap a for loop around this |
| 51 | // function and keep processing tasks until we have no work left to do - we |
| 52 | // have to return control back to the Android Looper after each message. This |
| 53 | // also means we have to perform idle detection differently, which is why we |
| 54 | // add an IdleHandler to the message queue in SystemMessageHandler.java, which |
| 55 | // calls DoIdleWork whenever control returns back to the looper and there are |
| 56 | // no tasks queued up to run immediately. |
| 57 | delegate->DoWork(); |
gsennton | ebe2e203 | 2016-08-18 22:34:12 | [diff] [blame] | 58 | if (pump->ShouldAbort()) { |
| 59 | // There is a pending JNI exception, return to Java so that the exception is |
| 60 | // thrown correctly. |
| 61 | return; |
| 62 | } |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 63 | |
[email protected] | 97532f3f | 2014-03-11 09:24:42 | [diff] [blame] | 64 | // In the java side, |SystemMessageHandler| keeps a single "delayed" message. |
| 65 | // It's an expensive operation to |removeMessage| there, so this is optimized |
| 66 | // to avoid those calls. |
| 67 | // |
| 68 | // At this stage, |next_delayed_work_time| can be: |
| 69 | // 1) The same as previously scheduled: nothing to be done, move along. This |
| 70 | // is the typical case, since this method is called for every single message. |
| 71 | // |
| 72 | // 2) Not previously scheduled: just post a new message in java. |
| 73 | // |
| 74 | // 3) Shorter than previously scheduled: far less common. In this case, |
| 75 | // |removeMessage| and post a new one. |
| 76 | // |
| 77 | // 4) Longer than previously scheduled (or null): nothing to be done, move |
| 78 | // along. |
| 79 | // |
| 80 | // Side note: base::TimeTicks is a C++ representation and can't be |
| 81 | // compared in java. When calling |scheduleDelayedWork|, pass the |
| 82 | // |InternalValue()| to java and then back to C++ so the comparisons can be |
| 83 | // done here. |
| 84 | // This roundtrip allows comparing TimeTicks directly (cheap) and |
| 85 | // avoid comparisons with TimeDelta / Now() (expensive). |
| 86 | base::TimeTicks next_delayed_work_time; |
Michael Thiessen | 7681cfee | 2017-08-18 16:55:37 | [diff] [blame] | 87 | delegate->DoDelayedWork(&next_delayed_work_time); |
gsennton | ebe2e203 | 2016-08-18 22:34:12 | [diff] [blame] | 88 | if (pump->ShouldAbort()) { |
| 89 | // There is a pending JNI exception, return to Java so that the exception is |
| 90 | // thrown correctly |
| 91 | return; |
| 92 | } |
[email protected] | d1d787eff | 2013-07-12 03:19:55 | [diff] [blame] | 93 | |
[email protected] | 97532f3f | 2014-03-11 09:24:42 | [diff] [blame] | 94 | if (!next_delayed_work_time.is_null()) { |
| 95 | // Schedule a new message if there's nothing already scheduled or there's a |
| 96 | // shorter delay than previously scheduled (see (2) and (3) above). |
| 97 | if (delayed_scheduled_time_ticks == 0 || |
| 98 | next_delayed_work_time < base::TimeTicks::FromInternalValue( |
| 99 | delayed_scheduled_time_ticks)) { |
| 100 | Java_SystemMessageHandler_scheduleDelayedWork(env, obj, |
| 101 | next_delayed_work_time.ToInternalValue(), |
| 102 | (next_delayed_work_time - |
| 103 | base::TimeTicks::Now()).InMillisecondsRoundedUp()); |
| 104 | } |
[email protected] | d1d787eff | 2013-07-12 03:19:55 | [diff] [blame] | 105 | } |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 106 | } |
| 107 | |
Michael Thiessen | 7681cfee | 2017-08-18 16:55:37 | [diff] [blame] | 108 | // This is called by the java SystemMessageHandler whenever the message queue |
| 109 | // detects an idle state (as in, control returns to the looper and there are no |
| 110 | // tasks available to be run immediately). |
| 111 | // See the comments in DoRunLoopOnce for how this differs from the |
| 112 | // implementation on other platforms. |
| 113 | static void DoIdleWork(JNIEnv* env, |
| 114 | const JavaParamRef<jobject>& obj, |
| 115 | jlong native_delegate, |
| 116 | jlong native_message_pump) { |
| 117 | base::MessagePump::Delegate* delegate = |
| 118 | reinterpret_cast<base::MessagePump::Delegate*>(native_delegate); |
| 119 | DCHECK(delegate); |
| 120 | delegate->DoIdleWork(); |
| 121 | }; |
| 122 | |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 123 | namespace base { |
| 124 | |
Michael Thiessen | dbeca24 | 2017-08-28 21:10:08 | [diff] [blame] | 125 | MessagePumpForUI::MessagePumpForUI() = default; |
| 126 | MessagePumpForUI::~MessagePumpForUI() = default; |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 127 | |
| 128 | void MessagePumpForUI::Run(Delegate* delegate) { |
| 129 | NOTREACHED() << "UnitTests should rely on MessagePumpForUIStub in" |
| 130 | " test_stub_android.h"; |
| 131 | } |
| 132 | |
gsennton | ebe2e203 | 2016-08-18 22:34:12 | [diff] [blame] | 133 | JNIEnv* MessagePumpForUI::StartInternal() { |
Michael Thiessen | dbeca24 | 2017-08-28 21:10:08 | [diff] [blame] | 134 | DCHECK(!quit_); |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 135 | run_loop_ = new RunLoop(); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 136 | // Since the RunLoop was just created above, BeforeRun should be guaranteed to |
| 137 | // return true (it only returns false if the RunLoop has been Quit already). |
| 138 | if (!run_loop_->BeforeRun()) |
| 139 | NOTREACHED(); |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 140 | |
[email protected] | 3042d1b | 2013-07-02 18:54:05 | [diff] [blame] | 141 | DCHECK(system_message_handler_obj_.is_null()); |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 142 | |
| 143 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 144 | DCHECK(env); |
gsennton | ebe2e203 | 2016-08-18 22:34:12 | [diff] [blame] | 145 | return env; |
| 146 | } |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 147 | |
gsennton | ebe2e203 | 2016-08-18 22:34:12 | [diff] [blame] | 148 | void MessagePumpForUI::Start(Delegate* delegate) { |
| 149 | JNIEnv* env = StartInternal(); |
| 150 | system_message_handler_obj_.Reset(Java_SystemMessageHandler_create( |
| 151 | env, reinterpret_cast<intptr_t>(delegate), |
| 152 | reinterpret_cast<intptr_t>(this))); |
| 153 | } |
| 154 | |
| 155 | void MessagePumpForUI::StartForUnitTest( |
| 156 | Delegate* delegate, |
| 157 | base::android::JavaMessageHandlerFactory* factory, |
| 158 | WaitableEvent* test_done_event) { |
| 159 | JNIEnv* env = StartInternal(); |
[email protected] | 3042d1b | 2013-07-02 18:54:05 | [diff] [blame] | 160 | system_message_handler_obj_.Reset( |
gsennton | ebe2e203 | 2016-08-18 22:34:12 | [diff] [blame] | 161 | factory->CreateMessageHandler(env, delegate, this, test_done_event)); |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void MessagePumpForUI::Quit() { |
Michael Thiessen | dbeca24 | 2017-08-28 21:10:08 | [diff] [blame] | 165 | quit_ = true; |
[email protected] | 3042d1b | 2013-07-02 18:54:05 | [diff] [blame] | 166 | if (!system_message_handler_obj_.is_null()) { |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 167 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 168 | DCHECK(env); |
| 169 | |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 170 | Java_SystemMessageHandler_removeAllPendingMessages( |
| 171 | env, system_message_handler_obj_); |
[email protected] | 3042d1b | 2013-07-02 18:54:05 | [diff] [blame] | 172 | system_message_handler_obj_.Reset(); |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 173 | } |
| 174 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 175 | if (run_loop_) { |
| 176 | run_loop_->AfterRun(); |
| 177 | delete run_loop_; |
| 178 | run_loop_ = NULL; |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | void MessagePumpForUI::ScheduleWork() { |
Michael Thiessen | dbeca24 | 2017-08-28 21:10:08 | [diff] [blame] | 183 | if (quit_) |
| 184 | return; |
[email protected] | 3042d1b | 2013-07-02 18:54:05 | [diff] [blame] | 185 | DCHECK(!system_message_handler_obj_.is_null()); |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 186 | |
| 187 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 188 | DCHECK(env); |
| 189 | |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 190 | Java_SystemMessageHandler_scheduleWork(env, system_message_handler_obj_); |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks& delayed_work_time) { |
Michael Thiessen | dbeca24 | 2017-08-28 21:10:08 | [diff] [blame] | 194 | if (quit_) |
| 195 | return; |
[email protected] | 3042d1b | 2013-07-02 18:54:05 | [diff] [blame] | 196 | DCHECK(!system_message_handler_obj_.is_null()); |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 197 | |
| 198 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 199 | DCHECK(env); |
| 200 | |
| 201 | jlong millis = |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 202 | (delayed_work_time - TimeTicks::Now()).InMillisecondsRoundedUp(); |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 203 | // Note that we're truncating to milliseconds as required by the java side, |
| 204 | // even though delayed_work_time is microseconds resolution. |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 205 | Java_SystemMessageHandler_scheduleDelayedWork( |
| 206 | env, system_message_handler_obj_, delayed_work_time.ToInternalValue(), |
| 207 | millis); |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 208 | } |
| 209 | |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 210 | } // namespace base |