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