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> |
11 #include <glib.h> | 11 #include <glib.h> |
12 | 12 |
13 #include "base/eintr_wrapper.h" | 13 #include "base/eintr_wrapper.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/platform_thread.h" | 15 #include "base/platform_thread.h" |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 // We send a byte across a pipe to wakeup the event loop. | 19 // We send a byte across a pipe to wakeup the event loop. |
20 const char kWorkScheduled = '\0'; | 20 const char kWorkScheduled = '\0'; |
21 | 21 |
22 // Return a timeout suitable for the glib loop, -1 to block forever, | 22 // Return a timeout suitable for the glib loop, -1 to block forever, |
23 // 0 to return right away, or a timeout in milliseconds from now. | 23 // 0 to return right away, or a timeout in milliseconds from now. |
24 int GetTimeIntervalMilliseconds(base::Time from) { | 24 int GetTimeIntervalMilliseconds(const base::TimeTicks& from) { |
25 if (from.is_null()) | 25 if (from.is_null()) |
26 return -1; | 26 return -1; |
27 | 27 |
28 // Be careful here. TimeDelta has a precision of microseconds, but we want a | 28 // Be careful here. TimeDelta has a precision of microseconds, but we want a |
29 // value in milliseconds. If there are 5.5ms left, should the delay be 5 or | 29 // value in milliseconds. If there are 5.5ms left, should the delay be 5 or |
30 // 6? It should be 6 to avoid executing delayed work too early. | 30 // 6? It should be 6 to avoid executing delayed work too early. |
31 int delay = static_cast<int>( | 31 int delay = static_cast<int>( |
32 ceil((from - base::Time::Now()).InMillisecondsF())); | 32 ceil((from - base::TimeTicks::Now()).InMillisecondsF())); |
33 | 33 |
34 // If this value is negative, then we need to run delayed work soon. | 34 // If this value is negative, then we need to run delayed work soon. |
35 return delay < 0 ? 0 : delay; | 35 return delay < 0 ? 0 : delay; |
36 } | 36 } |
37 | 37 |
38 // A brief refresher on GLib: | 38 // A brief refresher on GLib: |
39 // GLib sources have four callbacks: Prepare, Check, Dispatch and Finalize. | 39 // GLib sources have four callbacks: Prepare, Check, Dispatch and Finalize. |
40 // On each iteration of the GLib pump, it calls each source's Prepare function. | 40 // On each iteration of the GLib pump, it calls each source's Prepare function. |
41 // This function should return TRUE if it wants GLib to call its Dispatch, and | 41 // This function should return TRUE if it wants GLib to call its Dispatch, and |
42 // FALSE otherwise. It can also set a timeout in this case for the next time | 42 // FALSE otherwise. It can also set a timeout in this case for the next time |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 void MessagePumpForUI::ScheduleWork() { | 326 void MessagePumpForUI::ScheduleWork() { |
327 // This can be called on any thread, so we don't want to touch any state | 327 // This can be called on any thread, so we don't want to touch any state |
328 // variables as we would then need locks all over. This ensures that if | 328 // variables as we would then need locks all over. This ensures that if |
329 // we are sleeping in a poll that we will wake up. | 329 // we are sleeping in a poll that we will wake up. |
330 char msg = '!'; | 330 char msg = '!'; |
331 if (HANDLE_EINTR(write(wakeup_pipe_write_, &msg, 1)) != 1) { | 331 if (HANDLE_EINTR(write(wakeup_pipe_write_, &msg, 1)) != 1) { |
332 NOTREACHED() << "Could not write to the UI message loop wakeup pipe!"; | 332 NOTREACHED() << "Could not write to the UI message loop wakeup pipe!"; |
333 } | 333 } |
334 } | 334 } |
335 | 335 |
336 void MessagePumpForUI::ScheduleDelayedWork(const Time& delayed_work_time) { | 336 void MessagePumpForUI::ScheduleDelayedWork(const TimeTicks& delayed_work_time) { |
337 // We need to wake up the loop in case the poll timeout needs to be | 337 // We need to wake up the loop in case the poll timeout needs to be |
338 // adjusted. This will cause us to try to do work, but that's ok. | 338 // adjusted. This will cause us to try to do work, but that's ok. |
339 delayed_work_time_ = delayed_work_time; | 339 delayed_work_time_ = delayed_work_time; |
340 ScheduleWork(); | 340 ScheduleWork(); |
341 } | 341 } |
342 | 342 |
343 void MessagePumpForUI::DispatchEvents(GdkEvent* event) { | 343 void MessagePumpForUI::DispatchEvents(GdkEvent* event) { |
344 WillProcessEvent(event); | 344 WillProcessEvent(event); |
345 if (state_ && state_->dispatcher) { // state_ may be null during tests. | 345 if (state_ && state_->dispatcher) { // state_ may be null during tests. |
346 if (!state_->dispatcher->Dispatch(event)) | 346 if (!state_->dispatcher->Dispatch(event)) |
347 state_->should_quit = true; | 347 state_->should_quit = true; |
348 } else { | 348 } else { |
349 gtk_main_do_event(event); | 349 gtk_main_do_event(event); |
350 } | 350 } |
351 DidProcessEvent(event); | 351 DidProcessEvent(event); |
352 } | 352 } |
353 | 353 |
354 // static | 354 // static |
355 void MessagePumpForUI::EventDispatcher(GdkEvent* event, gpointer data) { | 355 void MessagePumpForUI::EventDispatcher(GdkEvent* event, gpointer data) { |
356 MessagePumpForUI* message_pump = reinterpret_cast<MessagePumpForUI*>(data); | 356 MessagePumpForUI* message_pump = reinterpret_cast<MessagePumpForUI*>(data); |
357 message_pump->DispatchEvents(event); | 357 message_pump->DispatchEvents(event); |
358 } | 358 } |
359 | 359 |
360 } // namespace base | 360 } // namespace base |
OLD | NEW |