Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: base/message_pump_android.cc

Issue 10479018: Add base::RunLoop and update ui_test_utils to use it to reduce flakiness (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: darin feedback Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 run_loop_->BeforeRun();
jar (doing other things) 2012/06/23 02:43:19 Do we need to check return value here?
jbates 2012/06/25 20:04:57 Done.
77 79
78 DCHECK(g_system_message_handler_obj.Get().is_null()); 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 g_system_message_handler_obj.Get().Reset( 85 g_system_message_handler_obj.Get().Reset(
84 Java_SystemMessageHandler_create(env, reinterpret_cast<jint>(delegate))); 86 Java_SystemMessageHandler_create(env, reinterpret_cast<jint>(delegate)));
85 } 87 }
86 88
87 void MessagePumpForUI::Quit() { 89 void MessagePumpForUI::Quit() {
88 if (!g_system_message_handler_obj.Get().is_null()) { 90 if (!g_system_message_handler_obj.Get().is_null()) {
89 JNIEnv* env = base::android::AttachCurrentThread(); 91 JNIEnv* env = base::android::AttachCurrentThread();
90 DCHECK(env); 92 DCHECK(env);
91 93
92 Java_SystemMessageHandler_removeTimer(env, 94 Java_SystemMessageHandler_removeTimer(env,
93 g_system_message_handler_obj.Get().obj()); 95 g_system_message_handler_obj.Get().obj());
94 g_system_message_handler_obj.Get().Reset(); 96 g_system_message_handler_obj.Get().Reset();
95 } 97 }
96 98
97 if (state_) { 99 if (run_loop_) {
98 delete state_; 100 run_loop_->AfterRun();
99 state_ = NULL; 101 delete run_loop_;
102 run_loop_ = NULL;
100 } 103 }
101 } 104 }
102 105
103 void MessagePumpForUI::ScheduleWork() { 106 void MessagePumpForUI::ScheduleWork() {
104 if (g_system_message_handler_obj.Get().is_null()) 107 if (g_system_message_handler_obj.Get().is_null())
105 return; 108 return;
106 109
107 JNIEnv* env = base::android::AttachCurrentThread(); 110 JNIEnv* env = base::android::AttachCurrentThread();
108 DCHECK(env); 111 DCHECK(env);
109 112
(...skipping 15 matching lines...) Expand all
125 Java_SystemMessageHandler_setDelayedTimer(env, 128 Java_SystemMessageHandler_setDelayedTimer(env,
126 g_system_message_handler_obj.Get().obj(), millis); 129 g_system_message_handler_obj.Get().obj(), millis);
127 } 130 }
128 131
129 // Register native methods 132 // Register native methods
130 bool RegisterSystemMessageHandler(JNIEnv* env) { 133 bool RegisterSystemMessageHandler(JNIEnv* env) {
131 return RegisterNativesImpl(env); 134 return RegisterNativesImpl(env);
132 } 135 }
133 136
134 } // namespace base 137 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698