Paul Stewart | 5f06a0e | 2012-12-20 11:11:33 -0800 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 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 <glib.h> |
Alex Vakulenko | a41ab51 | 2014-07-23 14:24:23 -0700 | [diff] [blame] | 8 | #include <stdio.h> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 9 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 10 | #include <base/callback.h> |
| 11 | #include <base/location.h> |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 12 | #include <base/message_loop/message_loop_proxy.h> |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 13 | #include <base/run_loop.h> |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 14 | #include <base/time/time.h> |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 15 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 16 | using base::Callback; |
| 17 | using base::Closure; |
| 18 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 19 | namespace shill { |
| 20 | |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 21 | EventDispatcher::EventDispatcher() |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 22 | : dont_use_directly_(new base::MessageLoopForUI), |
Peter Qiu | 37a2b60 | 2014-10-27 10:00:43 -0700 | [diff] [blame] | 23 | message_loop_proxy_(base::MessageLoopProxy::current()), |
Peter Qiu | 40a0148 | 2014-10-31 11:54:23 -0700 | [diff] [blame] | 24 | io_handler_factory_( |
| 25 | IOHandlerFactoryContainer::GetInstance()->GetIOHandlerFactory()) { |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 26 | } |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 27 | |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 28 | EventDispatcher::~EventDispatcher() {} |
| 29 | |
| 30 | void EventDispatcher::DispatchForever() { |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 31 | base::MessageLoop::current()->Run(); |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 34 | void EventDispatcher::DispatchPendingEvents() { |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 35 | base::RunLoop().RunUntilIdle(); |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 38 | bool EventDispatcher::PostTask(const Closure& task) { |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 39 | return message_loop_proxy_->PostTask(FROM_HERE, task); |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 42 | bool EventDispatcher::PostDelayedTask(const Closure& task, int64_t delay_ms) { |
Paul Stewart | 5ad1606 | 2013-02-21 18:10:48 -0800 | [diff] [blame] | 43 | return message_loop_proxy_->PostDelayedTask( |
| 44 | FROM_HERE, task, base::TimeDelta::FromMilliseconds(delay_ms)); |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Peter Qiu | 37a2b60 | 2014-10-27 10:00:43 -0700 | [diff] [blame] | 47 | // TODO(zqiu): Remove all reference to this function and use the |
| 48 | // IOHandlerFactory function directly. Delete this function once |
| 49 | // all references are removed. |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 50 | IOHandler* EventDispatcher::CreateInputHandler( |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 51 | int fd, |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 52 | const IOHandler::InputCallback& input_callback, |
| 53 | const IOHandler::ErrorCallback& error_callback) { |
Peter Qiu | 37a2b60 | 2014-10-27 10:00:43 -0700 | [diff] [blame] | 54 | return io_handler_factory_->CreateIOInputHandler( |
| 55 | fd, input_callback, error_callback); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Peter Qiu | 37a2b60 | 2014-10-27 10:00:43 -0700 | [diff] [blame] | 58 | // TODO(zqiu): Remove all reference to this function and use the |
| 59 | // IOHandlerFactory function directly. Delete this function once |
| 60 | // all references are removed. |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 61 | IOHandler* EventDispatcher::CreateReadyHandler( |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 62 | int fd, |
| 63 | IOHandler::ReadyMode mode, |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 64 | const Callback<void(int)>& ready_callback) { |
Peter Qiu | 37a2b60 | 2014-10-27 10:00:43 -0700 | [diff] [blame] | 65 | return io_handler_factory_->CreateIOReadyHandler( |
| 66 | fd, mode, ready_callback); |
Paul Stewart | a43d923 | 2011-05-10 11:40:22 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 69 | } // namespace shill |