| OLD | NEW |
| 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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_glib.h" | 5 #include "base/message_pump_glib.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <math.h> | 8 #include <math.h> |
| 9 | 9 |
| 10 #include <gtk/gtk.h> | 10 #include <gtk/gtk.h> |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 // first iteration of the loop, so RunAllPending() works correctly. | 200 // first iteration of the loop, so RunAllPending() works correctly. |
| 201 bool more_work_is_plausible = true; | 201 bool more_work_is_plausible = true; |
| 202 | 202 |
| 203 // We run our own loop instead of using g_main_loop_quit in one of the | 203 // We run our own loop instead of using g_main_loop_quit in one of the |
| 204 // callbacks. This is so we only quit our own loops, and we don't quit | 204 // callbacks. This is so we only quit our own loops, and we don't quit |
| 205 // nested loops run by others. TODO(deanm): Is this what we want? | 205 // nested loops run by others. TODO(deanm): Is this what we want? |
| 206 for (;;) { | 206 for (;;) { |
| 207 // Don't block if we think we have more work to do. | 207 // Don't block if we think we have more work to do. |
| 208 bool block = !more_work_is_plausible; | 208 bool block = !more_work_is_plausible; |
| 209 | 209 |
| 210 more_work_is_plausible = RunOnce(context_, block); | 210 // g_main_context_iteration returns true if events have been dispatched. |
| 211 more_work_is_plausible = g_main_context_iteration(context_, block); |
| 211 if (state_->should_quit) | 212 if (state_->should_quit) |
| 212 break; | 213 break; |
| 213 | 214 |
| 214 more_work_is_plausible |= state_->delegate->DoWork(); | 215 more_work_is_plausible |= state_->delegate->DoWork(); |
| 215 if (state_->should_quit) | 216 if (state_->should_quit) |
| 216 break; | 217 break; |
| 217 | 218 |
| 218 more_work_is_plausible |= | 219 more_work_is_plausible |= |
| 219 state_->delegate->DoDelayedWork(&delayed_work_time_); | 220 state_->delegate->DoDelayedWork(&delayed_work_time_); |
| 220 if (state_->should_quit) | 221 if (state_->should_quit) |
| 221 break; | 222 break; |
| 222 | 223 |
| 223 if (more_work_is_plausible) | 224 if (more_work_is_plausible) |
| 224 continue; | 225 continue; |
| 225 | 226 |
| 226 more_work_is_plausible = state_->delegate->DoIdleWork(); | 227 more_work_is_plausible = state_->delegate->DoIdleWork(); |
| 227 if (state_->should_quit) | 228 if (state_->should_quit) |
| 228 break; | 229 break; |
| 229 } | 230 } |
| 230 | 231 |
| 231 state_ = previous_state; | 232 state_ = previous_state; |
| 232 } | 233 } |
| 233 | 234 |
| 234 bool MessagePumpForUI::RunOnce(GMainContext* context, bool block) { | |
| 235 // g_main_context_iteration returns true if events have been dispatched. | |
| 236 return g_main_context_iteration(context, block); | |
| 237 } | |
| 238 | |
| 239 // Return the timeout we want passed to poll. | 235 // Return the timeout we want passed to poll. |
| 240 int MessagePumpForUI::HandlePrepare() { | 236 int MessagePumpForUI::HandlePrepare() { |
| 241 // We know we have work, but we haven't called HandleDispatch yet. Don't let | 237 // We know we have work, but we haven't called HandleDispatch yet. Don't let |
| 242 // the pump block so that we can do some processing. | 238 // the pump block so that we can do some processing. |
| 243 if (state_ && // state_ may be null during tests. | 239 if (state_ && // state_ may be null during tests. |
| 244 state_->has_work) | 240 state_->has_work) |
| 245 return 0; | 241 return 0; |
| 246 | 242 |
| 247 // We don't think we have work to do, but make sure not to block | 243 // We don't think we have work to do, but make sure not to block |
| 248 // longer than the next time we need to run delayed work. | 244 // longer than the next time we need to run delayed work. |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 } | 325 } |
| 330 } | 326 } |
| 331 | 327 |
| 332 void MessagePumpForUI::ScheduleDelayedWork(const Time& delayed_work_time) { | 328 void MessagePumpForUI::ScheduleDelayedWork(const Time& delayed_work_time) { |
| 333 // We need to wake up the loop in case the poll timeout needs to be | 329 // We need to wake up the loop in case the poll timeout needs to be |
| 334 // adjusted. This will cause us to try to do work, but that's ok. | 330 // adjusted. This will cause us to try to do work, but that's ok. |
| 335 delayed_work_time_ = delayed_work_time; | 331 delayed_work_time_ = delayed_work_time; |
| 336 ScheduleWork(); | 332 ScheduleWork(); |
| 337 } | 333 } |
| 338 | 334 |
| 339 void MessagePumpForUI::DispatchEvents(GdkEvent* event) { | |
| 340 WillProcessEvent(event); | |
| 341 if (state_ && state_->dispatcher) { // state_ may be null during tests. | |
| 342 if (!state_->dispatcher->Dispatch(event)) | |
| 343 state_->should_quit = true; | |
| 344 } else { | |
| 345 gtk_main_do_event(event); | |
| 346 } | |
| 347 DidProcessEvent(event); | |
| 348 } | |
| 349 | |
| 350 // static | 335 // static |
| 351 void MessagePumpForUI::EventDispatcher(GdkEvent* event, gpointer data) { | 336 void MessagePumpForUI::EventDispatcher(GdkEvent* event, gpointer data) { |
| 352 MessagePumpForUI* message_pump = reinterpret_cast<MessagePumpForUI*>(data); | 337 MessagePumpForUI* message_pump = reinterpret_cast<MessagePumpForUI*>(data); |
| 353 message_pump->DispatchEvents(event); | 338 |
| 339 message_pump->WillProcessEvent(event); |
| 340 if (message_pump->state_ && // state_ may be null during tests. |
| 341 message_pump->state_->dispatcher) { |
| 342 if (!message_pump->state_->dispatcher->Dispatch(event)) |
| 343 message_pump->state_->should_quit = true; |
| 344 } else { |
| 345 gtk_main_do_event(event); |
| 346 } |
| 347 message_pump->DidProcessEvent(event); |
| 354 } | 348 } |
| 355 | 349 |
| 356 } // namespace base | 350 } // namespace base |
| OLD | NEW |