VirtualBox

source: vbox/trunk/include/VBox/com/EventQueue.h@ 31372

Last change on this file since 31372 was 31372, checked in by vboxsync, 15 years ago

Main: fix memory leak in VirtualBox::postEvent() when posting the event fails

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.0 KB
Line 
1/** @file
2 * MS COM / XPCOM Abstraction Layer:
3 * Event and EventQueue class declaration
4 */
5
6/*
7 * Copyright (C) 2006-2007 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27#ifndef ___VBox_com_EventQueue_h
28#define ___VBox_com_EventQueue_h
29
30#if !defined (VBOX_WITH_XPCOM)
31# include <Windows.h>
32#else
33# include <nsEventQueueUtils.h>
34#endif
35
36#include <VBox/com/defs.h>
37#include <VBox/com/assert.h>
38
39namespace com
40{
41
42class EventQueue;
43
44/**
45 * Base class for all events. Intended to be subclassed to introduce new events
46 * and handlers for them.
47 *
48 * Subclasses usually reimplement virtual #handler() (that does nothing by
49 * default) and add new data members describing the event.
50 */
51class Event
52{
53public:
54
55 Event() {}
56 virtual ~Event() {};
57
58protected:
59
60 /**
61 * Event handler. Called in the context of the event queue's thread.
62 * Always reimplemented by subclasses
63 *
64 * @return reserved, should be NULL.
65 */
66 virtual void *handler() { return NULL; }
67
68 friend class EventQueue;
69};
70
71/**
72 * Simple event queue.
73 *
74 * When using XPCOM, this will map onto the default XPCOM queue for the thread.
75 * So, if a queue is created on the main thread, it automatically processes
76 * XPCOM/IPC events while waiting for its own (Event) events.
77 *
78 * When using Windows, Darwin and OS/2, this will map onto the native thread
79 * queue/runloop. So, windows messages and what not will be processed while
80 * waiting for events.
81 */
82class EventQueue
83{
84public:
85
86 EventQueue();
87 ~EventQueue();
88
89 BOOL postEvent (Event *event);
90 BOOL waitForEvent (Event **event);
91 BOOL handleEvent (Event *event);
92 int processEventQueue(uint32_t cMsTimeout);
93 int interruptEventQueueProcessing();
94 int getSelectFD();
95 static int init();
96 static int uninit();
97 static EventQueue *getMainEventQueue();
98
99#ifdef VBOX_WITH_XPCOM
100 already_AddRefed<nsIEventQueue> getIEventQueue()
101 {
102 return mEventQ.get();
103 }
104#endif
105
106private:
107 static EventQueue *mMainQueue;
108
109#if !defined (VBOX_WITH_XPCOM)
110
111 /** The thread which the queue belongs to. */
112 DWORD mThreadId;
113 /** Duplicated thread handle for MsgWaitForMultipleObjects. */
114 HANDLE mhThread;
115
116#else
117
118 /** Whether it was created (and thus needs destroying) or if a queue already
119 * associated with the thread was used. */
120 BOOL mEQCreated;
121
122 nsCOMPtr <nsIEventQueue> mEventQ;
123 nsCOMPtr <nsIEventQueueService> mEventQService;
124
125 Event *mLastEvent;
126 BOOL mGotEvent;
127
128 struct MyPLEvent : public PLEvent
129 {
130 MyPLEvent (Event *e) : event (e) {}
131 Event *event;
132 };
133
134 static void * PR_CALLBACK plEventHandler (PLEvent* self)
135 {
136 // nsIEventQueue doesn't expose PL_GetEventOwner(), so use an internal
137 // field of PLEvent directly (hackish, but doesn' require an extra lib)
138 EventQueue *owner = (EventQueue *) self->owner;
139 Assert (owner);
140 owner->mLastEvent = ((MyPLEvent *) self)->event;
141 owner->mGotEvent = TRUE;
142 return 0;
143 }
144
145 static void PR_CALLBACK plEventDestructor (PLEvent* self) { delete self; }
146
147#endif
148};
149
150} /* namespace com */
151
152#endif
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette