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_pump_android.h" | 5 #include "base/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 "jni/system_message_handler_jni.h" | 14 #include "jni/system_message_handler_jni.h" |
| 14 | 15 |
| 15 using base::android::ScopedJavaLocalRef; | 16 using base::android::ScopedJavaLocalRef; |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 base::LazyInstance<base::android::ScopedJavaGlobalRef<jobject> > | 20 base::LazyInstance<base::android::ScopedJavaGlobalRef<jobject> > |
| 20 g_system_message_handler_obj = LAZY_INSTANCE_INITIALIZER; | 21 g_system_message_handler_obj = LAZY_INSTANCE_INITIALIZER; |
| 21 | 22 |
| 22 } // namespace | 23 } // namespace |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 54 jlong millis = | 55 jlong millis = |
| 55 (delayed_work_time - base::TimeTicks::Now()).InMillisecondsRoundedUp(); | 56 (delayed_work_time - base::TimeTicks::Now()).InMillisecondsRoundedUp(); |
| 56 Java_SystemMessageHandler_setDelayedTimer(env, obj, millis); | 57 Java_SystemMessageHandler_setDelayedTimer(env, obj, millis); |
| 57 } | 58 } |
| 58 return more_work_is_plausible; | 59 return more_work_is_plausible; |
| 59 } | 60 } |
| 60 | 61 |
| 61 namespace base { | 62 namespace base { |
| 62 | 63 |
| 63 MessagePumpForUI::MessagePumpForUI() | 64 MessagePumpForUI::MessagePumpForUI() |
| 64 : state_(NULL) { | 65 : run_loop_(NULL) { |
| 65 } | 66 } |
| 66 | 67 |
| 67 MessagePumpForUI::~MessagePumpForUI() { | 68 MessagePumpForUI::~MessagePumpForUI() { |
| 68 } | 69 } |
| 69 | 70 |
| 70 void MessagePumpForUI::Run(Delegate* delegate) { | 71 void MessagePumpForUI::Run(Delegate* delegate) { |
| 71 NOTREACHED() << "UnitTests should rely on MessagePumpForUIStub in" | 72 NOTREACHED() << "UnitTests should rely on MessagePumpForUIStub in" |
| 72 " test_stub_android.h"; | 73 " test_stub_android.h"; |
| 73 } | 74 } |
| 74 | 75 |
| 75 void MessagePumpForUI::Start(Delegate* delegate) { | 76 void MessagePumpForUI::Start(Delegate* delegate) { |
| 76 state_ = new MessageLoop::AutoRunState(MessageLoop::current()); | 77 run_loop_ = new base::RunLoop(); |
| 78 if (!run_loop_->BeforeRun()) | |
| 79 NOTREACHED(); | |
|
jar (doing other things)
2012/06/25 21:19:43
Should you actually return? I the semantics of t
jbates
2012/06/27 02:06:37
Added some comments.
| |
| 77 | 80 |
| 78 DCHECK(g_system_message_handler_obj.Get().is_null()); | 81 DCHECK(g_system_message_handler_obj.Get().is_null()); |
| 79 | 82 |
| 80 JNIEnv* env = base::android::AttachCurrentThread(); | 83 JNIEnv* env = base::android::AttachCurrentThread(); |
| 81 DCHECK(env); | 84 DCHECK(env); |
| 82 | 85 |
| 83 g_system_message_handler_obj.Get().Reset( | 86 g_system_message_handler_obj.Get().Reset( |
| 84 Java_SystemMessageHandler_create(env, reinterpret_cast<jint>(delegate))); | 87 Java_SystemMessageHandler_create(env, reinterpret_cast<jint>(delegate))); |
| 85 } | 88 } |
| 86 | 89 |
| 87 void MessagePumpForUI::Quit() { | 90 void MessagePumpForUI::Quit() { |
| 88 if (!g_system_message_handler_obj.Get().is_null()) { | 91 if (!g_system_message_handler_obj.Get().is_null()) { |
| 89 JNIEnv* env = base::android::AttachCurrentThread(); | 92 JNIEnv* env = base::android::AttachCurrentThread(); |
| 90 DCHECK(env); | 93 DCHECK(env); |
| 91 | 94 |
| 92 Java_SystemMessageHandler_removeTimer(env, | 95 Java_SystemMessageHandler_removeTimer(env, |
| 93 g_system_message_handler_obj.Get().obj()); | 96 g_system_message_handler_obj.Get().obj()); |
| 94 g_system_message_handler_obj.Get().Reset(); | 97 g_system_message_handler_obj.Get().Reset(); |
| 95 } | 98 } |
| 96 | 99 |
| 97 if (state_) { | 100 if (run_loop_) { |
| 98 delete state_; | 101 run_loop_->AfterRun(); |
| 99 state_ = NULL; | 102 delete run_loop_; |
| 103 run_loop_ = NULL; | |
| 100 } | 104 } |
| 101 } | 105 } |
| 102 | 106 |
| 103 void MessagePumpForUI::ScheduleWork() { | 107 void MessagePumpForUI::ScheduleWork() { |
| 104 if (g_system_message_handler_obj.Get().is_null()) | 108 if (g_system_message_handler_obj.Get().is_null()) |
| 105 return; | 109 return; |
| 106 | 110 |
| 107 JNIEnv* env = base::android::AttachCurrentThread(); | 111 JNIEnv* env = base::android::AttachCurrentThread(); |
| 108 DCHECK(env); | 112 DCHECK(env); |
| 109 | 113 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 125 Java_SystemMessageHandler_setDelayedTimer(env, | 129 Java_SystemMessageHandler_setDelayedTimer(env, |
| 126 g_system_message_handler_obj.Get().obj(), millis); | 130 g_system_message_handler_obj.Get().obj(), millis); |
| 127 } | 131 } |
| 128 | 132 |
| 129 // Register native methods | 133 // Register native methods |
| 130 bool RegisterSystemMessageHandler(JNIEnv* env) { | 134 bool RegisterSystemMessageHandler(JNIEnv* env) { |
| 131 return RegisterNativesImpl(env); | 135 return RegisterNativesImpl(env); |
| 132 } | 136 } |
| 133 | 137 |
| 134 } // namespace base | 138 } // namespace base |
| OLD | NEW |