[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 | |
| 9 | #include "base/android/jni_android.h" |
[email protected] | 449019ca | 2012-03-14 22:17:00 | [diff] [blame] | 10 | #include "base/android/scoped_java_ref.h" |
| 11 | #include "base/lazy_instance.h" |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 12 | #include "base/logging.h" |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 13 | #include "base/run_loop.h" |
[email protected] | e46f6615 | 2012-07-19 20:02:55 | [diff] [blame] | 14 | #include "jni/SystemMessageHandler_jni.h" |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 15 | |
torne | 8656011 | 2016-08-04 15:59:04 | [diff] [blame] | 16 | using base::android::JavaParamRef; |
[email protected] | 449019ca | 2012-03-14 22:17:00 | [diff] [blame] | 17 | using base::android::ScopedJavaLocalRef; |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 18 | |
Michael Thiessen | 781ddeb | 2017-11-15 17:07:23 | [diff] [blame] | 19 | namespace base { |
| 20 | |
| 21 | MessagePumpForUI::MessagePumpForUI() = default; |
| 22 | MessagePumpForUI::~MessagePumpForUI() = default; |
| 23 | |
| 24 | // This is called by the java SystemMessageHandler whenever the message queue |
| 25 | // detects an idle state (as in, control returns to the looper and there are no |
| 26 | // tasks available to be run immediately). |
| 27 | // See the comments in DoRunLoopOnce for how this differs from the |
| 28 | // implementation on other platforms. |
| 29 | void MessagePumpForUI::DoIdleWork(JNIEnv* env, |
| 30 | const JavaParamRef<jobject>& obj) { |
| 31 | delegate_->DoIdleWork(); |
| 32 | } |
| 33 | |
| 34 | void MessagePumpForUI::DoRunLoopOnce(JNIEnv* env, |
| 35 | const JavaParamRef<jobject>& obj, |
| 36 | jboolean delayed) { |
| 37 | if (delayed) |
| 38 | delayed_scheduled_time_ = base::TimeTicks(); |
| 39 | |
Michael Thiessen | fc7067fe | 2017-11-01 22:33:01 | [diff] [blame] | 40 | // If the pump has been aborted, tasks may continue to be queued up, but |
| 41 | // shouldn't run. |
Michael Thiessen | 781ddeb | 2017-11-15 17:07:23 | [diff] [blame] | 42 | if (ShouldAbort()) |
Michael Thiessen | fc7067fe | 2017-11-01 22:33:01 | [diff] [blame] | 43 | return; |
| 44 | |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 45 | // This is based on MessagePumpForUI::DoRunLoop() from desktop. |
| 46 | // Note however that our system queue is handled in the java side. |
| 47 | // In desktop we inspect and process a single system message and then |
Michael Thiessen | 7681cfee | 2017-08-18 16:55:37 | [diff] [blame] | 48 | // we call DoWork() / DoDelayedWork(). This is then wrapped in a for loop and |
| 49 | // 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] | 50 | // On Android, the java message queue may contain messages for other handlers |
| 51 | // that will be processed before calling here again. |
Michael Thiessen | 7681cfee | 2017-08-18 16:55:37 | [diff] [blame] | 52 | // This means that unlike Desktop, we can't wrap a for loop around this |
| 53 | // function and keep processing tasks until we have no work left to do - we |
| 54 | // have to return control back to the Android Looper after each message. This |
| 55 | // also means we have to perform idle detection differently, which is why we |
| 56 | // add an IdleHandler to the message queue in SystemMessageHandler.java, which |
| 57 | // calls DoIdleWork whenever control returns back to the looper and there are |
| 58 | // no tasks queued up to run immediately. |
Michael Thiessen | 781ddeb | 2017-11-15 17:07:23 | [diff] [blame] | 59 | delegate_->DoWork(); |
| 60 | if (ShouldAbort()) { |
gsennton | ebe2e203 | 2016-08-18 22:34:12 | [diff] [blame] | 61 | // There is a pending JNI exception, return to Java so that the exception is |
| 62 | // thrown correctly. |
| 63 | return; |
| 64 | } |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 65 | |
[email protected] | 97532f3f | 2014-03-11 09:24:42 | [diff] [blame] | 66 | base::TimeTicks next_delayed_work_time; |
Michael Thiessen | 781ddeb | 2017-11-15 17:07:23 | [diff] [blame] | 67 | delegate_->DoDelayedWork(&next_delayed_work_time); |
| 68 | if (ShouldAbort()) { |
gsennton | ebe2e203 | 2016-08-18 22:34:12 | [diff] [blame] | 69 | // There is a pending JNI exception, return to Java so that the exception is |
| 70 | // thrown correctly |
| 71 | return; |
| 72 | } |
[email protected] | d1d787eff | 2013-07-12 03:19:55 | [diff] [blame] | 73 | |
Michael Thiessen | 781ddeb | 2017-11-15 17:07:23 | [diff] [blame] | 74 | if (!next_delayed_work_time.is_null()) |
| 75 | ScheduleDelayedWork(next_delayed_work_time); |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 76 | } |
| 77 | |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 78 | void MessagePumpForUI::Run(Delegate* delegate) { |
| 79 | NOTREACHED() << "UnitTests should rely on MessagePumpForUIStub in" |
Michael Thiessen | 781ddeb | 2017-11-15 17:07:23 | [diff] [blame] | 80 | " test_stub_android.h"; |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 81 | } |
| 82 | |
Ran Ji | 3d6ec66 | 2018-07-09 21:18:30 | [diff] [blame^] | 83 | void MessagePumpForUI::Attach(Delegate* delegate) { |
Michael Thiessen | dbeca24 | 2017-08-28 21:10:08 | [diff] [blame] | 84 | DCHECK(!quit_); |
Michael Thiessen | 781ddeb | 2017-11-15 17:07:23 | [diff] [blame] | 85 | delegate_ = delegate; |
| 86 | run_loop_ = std::make_unique<RunLoop>(); |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 87 | // Since the RunLoop was just created above, BeforeRun should be guaranteed to |
| 88 | // return true (it only returns false if the RunLoop has been Quit already). |
| 89 | if (!run_loop_->BeforeRun()) |
| 90 | NOTREACHED(); |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 91 | |
[email protected] | 3042d1b | 2013-07-02 18:54:05 | [diff] [blame] | 92 | DCHECK(system_message_handler_obj_.is_null()); |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 93 | |
| 94 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 95 | DCHECK(env); |
[email protected] | 3042d1b | 2013-07-02 18:54:05 | [diff] [blame] | 96 | system_message_handler_obj_.Reset( |
Michael Thiessen | 781ddeb | 2017-11-15 17:07:23 | [diff] [blame] | 97 | Java_SystemMessageHandler_create(env, reinterpret_cast<jlong>(this))); |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void MessagePumpForUI::Quit() { |
Michael Thiessen | dbeca24 | 2017-08-28 21:10:08 | [diff] [blame] | 101 | quit_ = true; |
Michael Thiessen | 781ddeb | 2017-11-15 17:07:23 | [diff] [blame] | 102 | |
[email protected] | 3042d1b | 2013-07-02 18:54:05 | [diff] [blame] | 103 | if (!system_message_handler_obj_.is_null()) { |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 104 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 105 | DCHECK(env); |
| 106 | |
Michael Thiessen | 781ddeb | 2017-11-15 17:07:23 | [diff] [blame] | 107 | Java_SystemMessageHandler_shutdown(env, system_message_handler_obj_); |
[email protected] | 3042d1b | 2013-07-02 18:54:05 | [diff] [blame] | 108 | system_message_handler_obj_.Reset(); |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 109 | } |
| 110 | |
[email protected] | 8e937c1e | 2012-06-28 22:57:30 | [diff] [blame] | 111 | if (run_loop_) { |
| 112 | run_loop_->AfterRun(); |
Michael Thiessen | 781ddeb | 2017-11-15 17:07:23 | [diff] [blame] | 113 | run_loop_ = nullptr; |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | void MessagePumpForUI::ScheduleWork() { |
Michael Thiessen | dbeca24 | 2017-08-28 21:10:08 | [diff] [blame] | 118 | if (quit_) |
| 119 | return; |
[email protected] | 3042d1b | 2013-07-02 18:54:05 | [diff] [blame] | 120 | DCHECK(!system_message_handler_obj_.is_null()); |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 121 | |
| 122 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 123 | DCHECK(env); |
| 124 | |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 125 | Java_SystemMessageHandler_scheduleWork(env, system_message_handler_obj_); |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks& delayed_work_time) { |
Michael Thiessen | dbeca24 | 2017-08-28 21:10:08 | [diff] [blame] | 129 | if (quit_) |
| 130 | return; |
Michael Thiessen | 781ddeb | 2017-11-15 17:07:23 | [diff] [blame] | 131 | // In the java side, |SystemMessageHandler| keeps a single "delayed" message. |
| 132 | // It's an expensive operation to |removeMessage| there, so this is optimized |
| 133 | // to avoid those calls. |
| 134 | // |
| 135 | // At this stage, |delayed_work_time| can be: |
| 136 | // 1) The same as previously scheduled: nothing to be done, move along. This |
| 137 | // is the typical case, since this method is called for every single message. |
| 138 | // |
| 139 | // 2) Not previously scheduled: just post a new message in java. |
| 140 | // |
| 141 | // 3) Shorter than previously scheduled: far less common. In this case, |
| 142 | // |removeMessage| and post a new one. |
| 143 | // |
| 144 | // 4) Longer than previously scheduled (or null): nothing to be done, move |
| 145 | // along. |
| 146 | if (!delayed_scheduled_time_.is_null() && |
| 147 | delayed_work_time >= delayed_scheduled_time_) { |
| 148 | return; |
| 149 | } |
| 150 | DCHECK(!delayed_work_time.is_null()); |
[email protected] | 3042d1b | 2013-07-02 18:54:05 | [diff] [blame] | 151 | DCHECK(!system_message_handler_obj_.is_null()); |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 152 | |
| 153 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 154 | DCHECK(env); |
| 155 | |
| 156 | jlong millis = |
[email protected] | 59e69e74 | 2013-06-18 20:27:52 | [diff] [blame] | 157 | (delayed_work_time - TimeTicks::Now()).InMillisecondsRoundedUp(); |
Michael Thiessen | 781ddeb | 2017-11-15 17:07:23 | [diff] [blame] | 158 | delayed_scheduled_time_ = delayed_work_time; |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 159 | // Note that we're truncating to milliseconds as required by the java side, |
| 160 | // even though delayed_work_time is microseconds resolution. |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 161 | Java_SystemMessageHandler_scheduleDelayedWork( |
Michael Thiessen | 781ddeb | 2017-11-15 17:07:23 | [diff] [blame] | 162 | env, system_message_handler_obj_, millis); |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 163 | } |
| 164 | |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 165 | } // namespace base |