OLD | NEW |
1 // Copyright (c) 2011 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" |
| 11 #include "base/lazy_instance.h" |
10 #include "base/logging.h" | 12 #include "base/logging.h" |
11 #include "jni/system_message_handler_jni.h" | 13 #include "jni/system_message_handler_jni.h" |
12 | 14 |
13 using base::android::ScopedJavaReference; | 15 using base::android::ScopedJavaLocalRef; |
14 | 16 |
15 namespace { | 17 namespace { |
16 | 18 |
17 const char* kClassPathName = "com/android/chromeview/base/SystemMessageHandler"; | 19 base::LazyInstance<base::android::ScopedJavaGlobalRef<jobject> > |
18 | 20 g_system_message_handler_obj = LAZY_INSTANCE_INITIALIZER; |
19 jobject g_system_message_handler_obj = NULL; | |
20 | 21 |
21 } // namespace | 22 } // namespace |
22 | 23 |
23 // ---------------------------------------------------------------------------- | 24 // ---------------------------------------------------------------------------- |
24 // Native JNI methods called by Java. | 25 // Native JNI methods called by Java. |
25 // ---------------------------------------------------------------------------- | 26 // ---------------------------------------------------------------------------- |
26 // This method can not move to anonymous namespace as it has been declared as | 27 // This method can not move to anonymous namespace as it has been declared as |
27 // 'static' in system_message_handler_jni.h. | 28 // 'static' in system_message_handler_jni.h. |
28 static jboolean DoRunLoopOnce(JNIEnv* env, jobject obj, jint native_delegate) { | 29 static jboolean DoRunLoopOnce(JNIEnv* env, jobject obj, jint native_delegate) { |
29 base::MessagePump::Delegate* delegate = | 30 base::MessagePump::Delegate* delegate = |
(...skipping 16 matching lines...) Expand all Loading... |
46 // the callstack, and post a message to call us back soon. | 47 // the callstack, and post a message to call us back soon. |
47 if (more_work_is_plausible) | 48 if (more_work_is_plausible) |
48 return true; | 49 return true; |
49 | 50 |
50 more_work_is_plausible = delegate->DoIdleWork(); | 51 more_work_is_plausible = delegate->DoIdleWork(); |
51 if (!more_work_is_plausible && !delayed_work_time.is_null()) { | 52 if (!more_work_is_plausible && !delayed_work_time.is_null()) { |
52 // We only set the timer here as returning true would post a message. | 53 // We only set the timer here as returning true would post a message. |
53 jlong millis = | 54 jlong millis = |
54 (delayed_work_time - base::TimeTicks::Now()).InMillisecondsRoundedUp(); | 55 (delayed_work_time - base::TimeTicks::Now()).InMillisecondsRoundedUp(); |
55 Java_SystemMessageHandler_setDelayedTimer(env, obj, millis); | 56 Java_SystemMessageHandler_setDelayedTimer(env, obj, millis); |
56 base::android::CheckException(env); | |
57 } | 57 } |
58 return more_work_is_plausible; | 58 return more_work_is_plausible; |
59 } | 59 } |
60 | 60 |
61 namespace base { | 61 namespace base { |
62 | 62 |
63 MessagePumpForUI::MessagePumpForUI() | 63 MessagePumpForUI::MessagePumpForUI() |
64 : state_(NULL) { | 64 : state_(NULL) { |
65 } | 65 } |
66 | 66 |
67 MessagePumpForUI::~MessagePumpForUI() { | 67 MessagePumpForUI::~MessagePumpForUI() { |
68 } | 68 } |
69 | 69 |
70 void MessagePumpForUI::Run(Delegate* delegate) { | 70 void MessagePumpForUI::Run(Delegate* delegate) { |
71 NOTREACHED() << "UnitTests should rely on MessagePumpForUIStub in" | 71 NOTREACHED() << "UnitTests should rely on MessagePumpForUIStub in" |
72 " test_stub_android.h"; | 72 " test_stub_android.h"; |
73 } | 73 } |
74 | 74 |
75 void MessagePumpForUI::Start(Delegate* delegate) { | 75 void MessagePumpForUI::Start(Delegate* delegate) { |
76 state_ = new MessageLoop::AutoRunState(MessageLoop::current()); | 76 state_ = new MessageLoop::AutoRunState(MessageLoop::current()); |
77 | 77 |
78 DCHECK(!g_system_message_handler_obj); | 78 DCHECK(g_system_message_handler_obj.Get().is_null()); |
79 | 79 |
80 JNIEnv* env = base::android::AttachCurrentThread(); | 80 JNIEnv* env = base::android::AttachCurrentThread(); |
81 DCHECK(env); | 81 DCHECK(env); |
82 | 82 |
83 jclass clazz = env->FindClass(kClassPathName); | 83 g_system_message_handler_obj.Get().Reset( |
84 DCHECK(clazz); | 84 Java_SystemMessageHandler_create(env, reinterpret_cast<jint>(delegate))); |
85 | |
86 jmethodID constructor = base::android::GetMethodID(env, clazz, "<init>", | |
87 "(I)V"); | |
88 ScopedJavaReference<jobject> client(env, env->NewObject(clazz, constructor, | |
89 delegate)); | |
90 DCHECK(client.obj()); | |
91 | |
92 g_system_message_handler_obj = env->NewGlobalRef(client.obj()); | |
93 | |
94 base::android::CheckException(env); | |
95 } | 85 } |
96 | 86 |
97 void MessagePumpForUI::Quit() { | 87 void MessagePumpForUI::Quit() { |
98 if (g_system_message_handler_obj) { | 88 if (!g_system_message_handler_obj.Get().is_null()) { |
99 JNIEnv* env = base::android::AttachCurrentThread(); | 89 JNIEnv* env = base::android::AttachCurrentThread(); |
100 DCHECK(env); | 90 DCHECK(env); |
101 | 91 |
102 Java_SystemMessageHandler_removeTimer(env, g_system_message_handler_obj); | 92 Java_SystemMessageHandler_removeTimer(env, |
103 env->DeleteGlobalRef(g_system_message_handler_obj); | 93 g_system_message_handler_obj.Get().obj()); |
104 base::android::CheckException(env); | 94 g_system_message_handler_obj.Get().Reset(); |
105 g_system_message_handler_obj = NULL; | |
106 } | 95 } |
107 | 96 |
108 if (state_) { | 97 if (state_) { |
109 delete state_; | 98 delete state_; |
110 state_ = NULL; | 99 state_ = NULL; |
111 } | 100 } |
112 } | 101 } |
113 | 102 |
114 void MessagePumpForUI::ScheduleWork() { | 103 void MessagePumpForUI::ScheduleWork() { |
115 if (!g_system_message_handler_obj) | 104 if (g_system_message_handler_obj.Get().is_null()) |
116 return; | 105 return; |
117 | 106 |
118 JNIEnv* env = base::android::AttachCurrentThread(); | 107 JNIEnv* env = base::android::AttachCurrentThread(); |
119 DCHECK(env); | 108 DCHECK(env); |
120 | 109 |
121 Java_SystemMessageHandler_setTimer(env, g_system_message_handler_obj); | 110 Java_SystemMessageHandler_setTimer(env, |
122 base::android::CheckException(env); | 111 g_system_message_handler_obj.Get().obj()); |
123 | |
124 } | 112 } |
125 | 113 |
126 void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks& delayed_work_time) { | 114 void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks& delayed_work_time) { |
127 if (!g_system_message_handler_obj) | 115 if (g_system_message_handler_obj.Get().is_null()) |
128 return; | 116 return; |
129 | 117 |
130 JNIEnv* env = base::android::AttachCurrentThread(); | 118 JNIEnv* env = base::android::AttachCurrentThread(); |
131 DCHECK(env); | 119 DCHECK(env); |
132 | 120 |
133 jlong millis = | 121 jlong millis = |
134 (delayed_work_time - base::TimeTicks::Now()).InMillisecondsRoundedUp(); | 122 (delayed_work_time - base::TimeTicks::Now()).InMillisecondsRoundedUp(); |
135 // Note that we're truncating to milliseconds as required by the java side, | 123 // Note that we're truncating to milliseconds as required by the java side, |
136 // even though delayed_work_time is microseconds resolution. | 124 // even though delayed_work_time is microseconds resolution. |
137 Java_SystemMessageHandler_setDelayedTimer(env, g_system_message_handler_obj, | 125 Java_SystemMessageHandler_setDelayedTimer(env, |
138 millis); | 126 g_system_message_handler_obj.Get().obj(), millis); |
139 base::android::CheckException(env); | |
140 } | 127 } |
141 | 128 |
142 // Register native methods | 129 // Register native methods |
143 bool RegisterSystemMessageHandler(JNIEnv* env) { | 130 bool RegisterSystemMessageHandler(JNIEnv* env) { |
144 return RegisterNativesImpl(env); | 131 return RegisterNativesImpl(env); |
145 } | 132 } |
146 | 133 |
147 } // namespace base | 134 } // namespace base |
OLD | NEW |