This source file includes following definitions.
- DoRunLoopOnce
- Run
- Start
- Quit
- ScheduleWork
- ScheduleDelayedWork
- RegisterBindings
#include "base/message_loop/message_pump_android.h"
#include <jni.h>
#include "base/android/jni_android.h"
#include "base/android/scoped_java_ref.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/run_loop.h"
#include "base/time/time.h"
#include "jni/SystemMessageHandler_jni.h"
using base::android::ScopedJavaLocalRef;
static void DoRunLoopOnce(JNIEnv* env, jobject obj, jlong native_delegate,
jlong delayed_scheduled_time_ticks) {
base::MessagePump::Delegate* delegate =
reinterpret_cast<base::MessagePump::Delegate*>(native_delegate);
DCHECK(delegate);
bool did_work = delegate->DoWork();
base::TimeTicks next_delayed_work_time;
did_work |= delegate->DoDelayedWork(&next_delayed_work_time);
if (!next_delayed_work_time.is_null()) {
if (delayed_scheduled_time_ticks == 0 ||
next_delayed_work_time < base::TimeTicks::FromInternalValue(
delayed_scheduled_time_ticks)) {
Java_SystemMessageHandler_scheduleDelayedWork(env, obj,
next_delayed_work_time.ToInternalValue(),
(next_delayed_work_time -
base::TimeTicks::Now()).InMillisecondsRoundedUp());
}
}
if (did_work)
return;
delegate->DoIdleWork();
}
namespace base {
MessagePumpForUI::MessagePumpForUI()
: run_loop_(NULL) {
}
MessagePumpForUI::~MessagePumpForUI() {
}
void MessagePumpForUI::Run(Delegate* delegate) {
NOTREACHED() << "UnitTests should rely on MessagePumpForUIStub in"
" test_stub_android.h";
}
void MessagePumpForUI::Start(Delegate* delegate) {
run_loop_ = new RunLoop();
if (!run_loop_->BeforeRun())
NOTREACHED();
DCHECK(system_message_handler_obj_.is_null());
JNIEnv* env = base::android::AttachCurrentThread();
DCHECK(env);
system_message_handler_obj_.Reset(
Java_SystemMessageHandler_create(
env, reinterpret_cast<intptr_t>(delegate)));
}
void MessagePumpForUI::Quit() {
if (!system_message_handler_obj_.is_null()) {
JNIEnv* env = base::android::AttachCurrentThread();
DCHECK(env);
Java_SystemMessageHandler_removeScheduledWork(env,
system_message_handler_obj_.obj());
system_message_handler_obj_.Reset();
}
if (run_loop_) {
run_loop_->AfterRun();
delete run_loop_;
run_loop_ = NULL;
}
}
void MessagePumpForUI::ScheduleWork() {
DCHECK(!system_message_handler_obj_.is_null());
JNIEnv* env = base::android::AttachCurrentThread();
DCHECK(env);
Java_SystemMessageHandler_scheduleWork(env,
system_message_handler_obj_.obj());
}
void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks& delayed_work_time) {
DCHECK(!system_message_handler_obj_.is_null());
JNIEnv* env = base::android::AttachCurrentThread();
DCHECK(env);
jlong millis =
(delayed_work_time - TimeTicks::Now()).InMillisecondsRoundedUp();
Java_SystemMessageHandler_scheduleDelayedWork(env,
system_message_handler_obj_.obj(),
delayed_work_time.ToInternalValue(), millis);
}
bool MessagePumpForUI::RegisterBindings(JNIEnv* env) {
return RegisterNativesImpl(env);
}
}