blob: ae0bf8897899e4df4723f8e24096cab73052448b [file] [log] [blame]
Paul Stewart75897df2011-04-27 09:05:53 -07001// 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 Stewart26b327e2011-10-19 11:38:09 -07005#include "shill/event_dispatcher.h"
Darin Petkov67d8ecf2011-07-26 16:03:30 -07006
Paul Stewart75897df2011-04-27 09:05:53 -07007#include <stdio.h>
8#include <glib.h>
9
Eric Shienbrood3e20a232012-02-16 11:35:56 -050010#include <base/callback.h>
11#include <base/location.h>
Chris Masone0e1d1042011-05-09 18:07:03 -070012#include <base/message_loop_proxy.h>
13
Paul Stewartf0aae102011-10-19 12:11:44 -070014#include "shill/glib_io_input_handler.h"
15#include "shill/glib_io_ready_handler.h"
Paul Stewart75897df2011-04-27 09:05:53 -070016
Eric Shienbrood3e20a232012-02-16 11:35:56 -050017using base::Callback;
18using base::Closure;
19
Paul Stewart75897df2011-04-27 09:05:53 -070020namespace shill {
21
Chris Masone0e1d1042011-05-09 18:07:03 -070022EventDispatcher::EventDispatcher()
23 : dont_use_directly_(new MessageLoopForUI),
Eric Shienbrood3e20a232012-02-16 11:35:56 -050024 message_loop_proxy_(base::MessageLoopProxy::current()) {
Chris Masone0e1d1042011-05-09 18:07:03 -070025}
Paul Stewarta43d9232011-05-10 11:40:22 -070026
Chris Masone0e1d1042011-05-09 18:07:03 -070027EventDispatcher::~EventDispatcher() {}
28
29void EventDispatcher::DispatchForever() {
30 MessageLoop::current()->Run();
31}
32
Darin Petkov67d8ecf2011-07-26 16:03:30 -070033void EventDispatcher::DispatchPendingEvents() {
34 MessageLoop::current()->RunAllPending();
35}
36
Eric Shienbrood3e20a232012-02-16 11:35:56 -050037bool EventDispatcher::PostTask(const Closure &task) {
mukesh agrawalf60e4062011-05-27 13:13:41 -070038 return message_loop_proxy_->PostTask(FROM_HERE, task);
Chris Masone0e1d1042011-05-09 18:07:03 -070039}
40
Eric Shienbrood3e20a232012-02-16 11:35:56 -050041bool EventDispatcher::PostDelayedTask(const Closure &task, int64 delay_ms) {
mukesh agrawalf60e4062011-05-27 13:13:41 -070042 return message_loop_proxy_->PostDelayedTask(FROM_HERE, task, delay_ms);
Chris Masone0e1d1042011-05-09 18:07:03 -070043}
44
Paul Stewart26b327e2011-10-19 11:38:09 -070045IOHandler *EventDispatcher::CreateInputHandler(
Chris Masone0e1d1042011-05-09 18:07:03 -070046 int fd,
Eric Shienbrood3e20a232012-02-16 11:35:56 -050047 const Callback<void(InputData *)> &callback) {
Paul Stewartf0aae102011-10-19 12:11:44 -070048 IOHandler *handler = new GlibIOInputHandler(fd, callback);
49 handler->Start();
50 return handler;
51}
52
53IOHandler *EventDispatcher::CreateReadyHandler(
54 int fd,
55 IOHandler::ReadyMode mode,
Eric Shienbrood3e20a232012-02-16 11:35:56 -050056 const Callback<void(int)> &callback) {
Paul Stewartf0aae102011-10-19 12:11:44 -070057 IOHandler *handler = new GlibIOReadyHandler(fd, mode, callback);
58 handler->Start();
59 return handler;
Paul Stewarta43d9232011-05-10 11:40:22 -070060}
61
Paul Stewart75897df2011-04-27 09:05:53 -070062} // namespace shill