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

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: phajdan feedback Created 8 years, 5 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
« no previous file with comments | « base/message_pump_android.h ('k') | base/message_pump_dispatcher.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 // Since the RunLoop was just created above, BeforeRun should be guaranteed to
79 // return true (it only returns false if the RunLoop has been Quit already).
80 if (!run_loop_->BeforeRun())
81 NOTREACHED();
77 82
78 DCHECK(g_system_message_handler_obj.Get().is_null()); 83 DCHECK(g_system_message_handler_obj.Get().is_null());
79 84
80 JNIEnv* env = base::android::AttachCurrentThread(); 85 JNIEnv* env = base::android::AttachCurrentThread();
81 DCHECK(env); 86 DCHECK(env);
82 87
83 g_system_message_handler_obj.Get().Reset( 88 g_system_message_handler_obj.Get().Reset(
84 Java_SystemMessageHandler_create(env, reinterpret_cast<jint>(delegate))); 89 Java_SystemMessageHandler_create(env, reinterpret_cast<jint>(delegate)));
85 } 90 }
86 91
87 void MessagePumpForUI::Quit() { 92 void MessagePumpForUI::Quit() {
88 if (!g_system_message_handler_obj.Get().is_null()) { 93 if (!g_system_message_handler_obj.Get().is_null()) {
89 JNIEnv* env = base::android::AttachCurrentThread(); 94 JNIEnv* env = base::android::AttachCurrentThread();
90 DCHECK(env); 95 DCHECK(env);
91 96
92 Java_SystemMessageHandler_removeTimer(env, 97 Java_SystemMessageHandler_removeTimer(env,
93 g_system_message_handler_obj.Get().obj()); 98 g_system_message_handler_obj.Get().obj());
94 g_system_message_handler_obj.Get().Reset(); 99 g_system_message_handler_obj.Get().Reset();
95 } 100 }
96 101
97 if (state_) { 102 if (run_loop_) {
98 delete state_; 103 run_loop_->AfterRun();
99 state_ = NULL; 104 delete run_loop_;
105 run_loop_ = NULL;
100 } 106 }
101 } 107 }
102 108
103 void MessagePumpForUI::ScheduleWork() { 109 void MessagePumpForUI::ScheduleWork() {
104 if (g_system_message_handler_obj.Get().is_null()) 110 if (g_system_message_handler_obj.Get().is_null())
105 return; 111 return;
106 112
107 JNIEnv* env = base::android::AttachCurrentThread(); 113 JNIEnv* env = base::android::AttachCurrentThread();
108 DCHECK(env); 114 DCHECK(env);
109 115
(...skipping 15 matching lines...) Expand all
125 Java_SystemMessageHandler_setDelayedTimer(env, 131 Java_SystemMessageHandler_setDelayedTimer(env,
126 g_system_message_handler_obj.Get().obj(), millis); 132 g_system_message_handler_obj.Get().obj(), millis);
127 } 133 }
128 134
129 // Register native methods 135 // Register native methods
130 bool RegisterSystemMessageHandler(JNIEnv* env) { 136 bool RegisterSystemMessageHandler(JNIEnv* env) {
131 return RegisterNativesImpl(env); 137 return RegisterNativesImpl(env);
132 } 138 }
133 139
134 } // namespace base 140 } // namespace base
OLDNEW
« no previous file with comments | « base/message_pump_android.h ('k') | base/message_pump_dispatcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698