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