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 namespace { | |
20 | |
21 base::LazyInstance<base::android::ScopedJavaGlobalRef<jobject> > | |
22 g_system_message_handler_obj = LAZY_INSTANCE_INITIALIZER; | |
23 | |
24 } // namespace | |
25 | |
26 // ---------------------------------------------------------------------------- | 19 // ---------------------------------------------------------------------------- |
27 // Native JNI methods called by Java. | 20 // Native JNI methods called by Java. |
28 // ---------------------------------------------------------------------------- | 21 // ---------------------------------------------------------------------------- |
29 // 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 |
30 // 'static' in system_message_handler_jni.h. | 23 // 'static' in system_message_handler_jni.h. |
31 static jboolean DoRunLoopOnce(JNIEnv* env, jobject obj, jint native_delegate) { | 24 static jboolean DoRunLoopOnce(JNIEnv* env, jobject obj, jint native_delegate) { |
32 base::MessagePump::Delegate* delegate = | 25 base::MessagePump::Delegate* delegate = |
33 reinterpret_cast<base::MessagePump::Delegate*>(native_delegate); | 26 reinterpret_cast<base::MessagePump::Delegate*>(native_delegate); |
34 DCHECK(delegate); | 27 DCHECK(delegate); |
35 // This is based on MessagePumpForUI::DoRunLoop() from desktop. | 28 // This is based on MessagePumpForUI::DoRunLoop() from desktop. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 " test_stub_android.h"; | 67 " test_stub_android.h"; |
75 } | 68 } |
76 | 69 |
77 void MessagePumpForUI::Start(Delegate* delegate) { | 70 void MessagePumpForUI::Start(Delegate* delegate) { |
78 run_loop_ = new RunLoop(); | 71 run_loop_ = new RunLoop(); |
79 // Since the RunLoop was just created above, BeforeRun should be guaranteed to | 72 // Since the RunLoop was just created above, BeforeRun should be guaranteed to |
80 // return true (it only returns false if the RunLoop has been Quit already). | 73 // return true (it only returns false if the RunLoop has been Quit already). |
81 if (!run_loop_->BeforeRun()) | 74 if (!run_loop_->BeforeRun()) |
82 NOTREACHED(); | 75 NOTREACHED(); |
83 | 76 |
84 DCHECK(g_system_message_handler_obj.Get().is_null()); | 77 DCHECK(system_message_handler_obj_.is_null()); |
85 | 78 |
86 JNIEnv* env = base::android::AttachCurrentThread(); | 79 JNIEnv* env = base::android::AttachCurrentThread(); |
87 DCHECK(env); | 80 DCHECK(env); |
88 | 81 |
89 g_system_message_handler_obj.Get().Reset( | 82 system_message_handler_obj_.Reset( |
90 Java_SystemMessageHandler_create(env, reinterpret_cast<jint>(delegate))); | 83 Java_SystemMessageHandler_create(env, reinterpret_cast<jint>(delegate))); |
91 } | 84 } |
92 | 85 |
93 void MessagePumpForUI::Quit() { | 86 void MessagePumpForUI::Quit() { |
94 if (!g_system_message_handler_obj.Get().is_null()) { | 87 if (!system_message_handler_obj_.is_null()) { |
95 JNIEnv* env = base::android::AttachCurrentThread(); | 88 JNIEnv* env = base::android::AttachCurrentThread(); |
96 DCHECK(env); | 89 DCHECK(env); |
97 | 90 |
98 Java_SystemMessageHandler_removeTimer(env, | 91 Java_SystemMessageHandler_removeTimer(env, |
99 g_system_message_handler_obj.Get().obj()); | 92 system_message_handler_obj_.obj()); |
100 g_system_message_handler_obj.Get().Reset(); | 93 system_message_handler_obj_.Reset(); |
101 } | 94 } |
102 | 95 |
103 if (run_loop_) { | 96 if (run_loop_) { |
104 run_loop_->AfterRun(); | 97 run_loop_->AfterRun(); |
105 delete run_loop_; | 98 delete run_loop_; |
106 run_loop_ = NULL; | 99 run_loop_ = NULL; |
107 } | 100 } |
108 } | 101 } |
109 | 102 |
110 void MessagePumpForUI::ScheduleWork() { | 103 void MessagePumpForUI::ScheduleWork() { |
111 DCHECK(!g_system_message_handler_obj.Get().is_null()); | 104 DCHECK(!system_message_handler_obj_.is_null()); |
112 | 105 |
113 JNIEnv* env = base::android::AttachCurrentThread(); | 106 JNIEnv* env = base::android::AttachCurrentThread(); |
114 DCHECK(env); | 107 DCHECK(env); |
115 | 108 |
116 Java_SystemMessageHandler_setTimer(env, | 109 Java_SystemMessageHandler_setTimer(env, |
117 g_system_message_handler_obj.Get().obj()); | 110 system_message_handler_obj_.obj()); |
118 } | 111 } |
119 | 112 |
120 void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks& delayed_work_time) { | 113 void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks& delayed_work_time) { |
121 DCHECK(!g_system_message_handler_obj.Get().is_null()); | 114 DCHECK(!system_message_handler_obj_.is_null()); |
122 | 115 |
123 JNIEnv* env = base::android::AttachCurrentThread(); | 116 JNIEnv* env = base::android::AttachCurrentThread(); |
124 DCHECK(env); | 117 DCHECK(env); |
125 | 118 |
126 jlong millis = | 119 jlong millis = |
127 (delayed_work_time - TimeTicks::Now()).InMillisecondsRoundedUp(); | 120 (delayed_work_time - TimeTicks::Now()).InMillisecondsRoundedUp(); |
128 // Note that we're truncating to milliseconds as required by the java side, | 121 // Note that we're truncating to milliseconds as required by the java side, |
129 // even though delayed_work_time is microseconds resolution. | 122 // even though delayed_work_time is microseconds resolution. |
130 Java_SystemMessageHandler_setDelayedTimer(env, | 123 Java_SystemMessageHandler_setDelayedTimer(env, |
131 g_system_message_handler_obj.Get().obj(), millis); | 124 system_message_handler_obj_.obj(), millis); |
132 } | 125 } |
133 | 126 |
134 // static | 127 // static |
135 bool MessagePumpForUI::RegisterBindings(JNIEnv* env) { | 128 bool MessagePumpForUI::RegisterBindings(JNIEnv* env) { |
136 return RegisterNativesImpl(env); | 129 return RegisterNativesImpl(env); |
137 } | 130 } |
138 | 131 |
139 } // namespace base | 132 } // namespace base |
OLD | NEW |