Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 5 | #include "shill/event_dispatcher.h" |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 6 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 7 | #include <stdio.h> |
| 8 | #include <glib.h> |
| 9 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 10 | #include <base/callback.h> |
| 11 | #include <base/location.h> |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 12 | #include <base/message_loop_proxy.h> |
| 13 | |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 14 | #include "shill/glib_io_input_handler.h" |
| 15 | #include "shill/glib_io_ready_handler.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 16 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 17 | using base::Callback; |
| 18 | using base::Closure; |
| 19 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 20 | namespace shill { |
| 21 | |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 22 | EventDispatcher::EventDispatcher() |
| 23 | : dont_use_directly_(new MessageLoopForUI), |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 24 | message_loop_proxy_(base::MessageLoopProxy::current()) { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 25 | } |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 26 | |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 27 | EventDispatcher::~EventDispatcher() {} |
| 28 | |
| 29 | void EventDispatcher::DispatchForever() { |
| 30 | MessageLoop::current()->Run(); |
| 31 | } |
| 32 | |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 33 | void EventDispatcher::DispatchPendingEvents() { |
| 34 | MessageLoop::current()->RunAllPending(); |
| 35 | } |
| 36 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 37 | bool EventDispatcher::PostTask(const Closure &task) { |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 38 | return message_loop_proxy_->PostTask(FROM_HERE, task); |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 41 | bool EventDispatcher::PostDelayedTask(const Closure &task, int64 delay_ms) { |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 42 | return message_loop_proxy_->PostDelayedTask(FROM_HERE, task, delay_ms); |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 45 | IOHandler *EventDispatcher::CreateInputHandler( |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 46 | int fd, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 47 | const Callback<void(InputData *)> &callback) { |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 48 | IOHandler *handler = new GlibIOInputHandler(fd, callback); |
| 49 | handler->Start(); |
| 50 | return handler; |
| 51 | } |
| 52 | |
| 53 | IOHandler *EventDispatcher::CreateReadyHandler( |
| 54 | int fd, |
| 55 | IOHandler::ReadyMode mode, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 56 | const Callback<void(int)> &callback) { |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 57 | IOHandler *handler = new GlibIOReadyHandler(fd, mode, callback); |
| 58 | handler->Start(); |
| 59 | return handler; |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 62 | } // namespace shill |