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 | |
| 5 | #include <stdio.h> |
| 6 | #include <glib.h> |
| 7 | |
| 8 | #include "shill/shill_event.h" |
| 9 | |
| 10 | namespace shill { |
| 11 | |
| 12 | EventQueueItem::EventQueueItem(EventDispatcher *dispatcher) |
| 13 | : dispatcher_(dispatcher) { |
| 14 | dispatcher->RegisterCallbackQueue(this); |
| 15 | } |
| 16 | |
| 17 | EventQueueItem::~EventQueueItem() { |
| 18 | dispatcher_->UnregisterCallbackQueue(this); |
| 19 | } |
| 20 | |
| 21 | void EventQueueItem::AlertDispatcher() { |
| 22 | dispatcher_->ExecuteOnIdle(); |
| 23 | } |
| 24 | |
| 25 | void EventDispatcher::DispatchEvents() { |
| 26 | for (size_t idx = 0; idx < queue_list_.size(); ++idx) |
| 27 | queue_list_[idx]->Dispatch(); |
| 28 | } |
| 29 | |
| 30 | static gboolean DispatchEventsHandler(gpointer data) { |
| 31 | EventDispatcher *dispatcher = static_cast<EventDispatcher *>(data); |
| 32 | dispatcher->DispatchEvents(); |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | void EventDispatcher::ExecuteOnIdle() { |
| 37 | g_idle_add(&DispatchEventsHandler, this); |
| 38 | } |
| 39 | |
| 40 | void EventDispatcher::RegisterCallbackQueue(EventQueueItem *queue) { |
| 41 | queue_list_.push_back(queue); |
| 42 | } |
| 43 | |
| 44 | void EventDispatcher::UnregisterCallbackQueue(EventQueueItem *queue) { |
| 45 | for (size_t idx = 0; idx < queue_list_.size(); ++idx) { |
| 46 | if (queue_list_[idx] == queue) { |
| 47 | queue_list_.erase(queue_list_.begin() + idx); |
| 48 | return; |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | } // namespace shill |