1 // 2 // Copyright (C) 2012 The Android Open Source Project 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #include "shill/event_dispatcher.h" 18 19 #include <stdio.h> 20 21 #include <base/callback.h> 22 #include <base/location.h> 23 #include <base/message_loop/message_loop.h> 24 #include <base/run_loop.h> 25 #include <base/time/time.h> 26 27 using base::Callback; 28 using base::Closure; 29 30 namespace shill { 31 32 EventDispatcher::EventDispatcher() 33 : io_handler_factory_( 34 IOHandlerFactoryContainer::GetInstance()->GetIOHandlerFactory()) { 35 } 36 37 EventDispatcher::~EventDispatcher() {} 38 39 void EventDispatcher::DispatchForever() { 40 base::MessageLoop::current()->Run(); 41 } 42 43 void EventDispatcher::DispatchPendingEvents() { 44 base::RunLoop().RunUntilIdle(); 45 } 46 47 void EventDispatcher::PostTask(const Closure& task) { 48 base::MessageLoop::current()->PostTask(FROM_HERE, task); 49 } 50 51 void EventDispatcher::PostDelayedTask(const Closure& task, int64_t delay_ms) { 52 base::MessageLoop::current()->PostDelayedTask( 53 FROM_HERE, task, base::TimeDelta::FromMilliseconds(delay_ms)); 54 } 55 56 // TODO(zqiu): Remove all reference to this function and use the 57 // IOHandlerFactory function directly. Delete this function once 58 // all references are removed. 59 IOHandler* EventDispatcher::CreateInputHandler( 60 int fd, 61 const IOHandler::InputCallback& input_callback, 62 const IOHandler::ErrorCallback& error_callback) { 63 return io_handler_factory_->CreateIOInputHandler( 64 fd, input_callback, error_callback); 65 } 66 67 // TODO(zqiu): Remove all reference to this function and use the 68 // IOHandlerFactory function directly. Delete this function once 69 // all references are removed. 70 IOHandler* EventDispatcher::CreateReadyHandler( 71 int fd, 72 IOHandler::ReadyMode mode, 73 const Callback<void(int)>& ready_callback) { 74 return io_handler_factory_->CreateIOReadyHandler( 75 fd, mode, ready_callback); 76 } 77 78 } // namespace shill 79