1 | /** @file
|
---|
2 | *
|
---|
3 | * VirtualBox COM IEvent implementation
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. 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 |
|
---|
18 | #ifndef ____H_EVENTIMPL
|
---|
19 | #define ____H_EVENTIMPL
|
---|
20 |
|
---|
21 | #include "VirtualBoxBase.h"
|
---|
22 | #include <iprt/asm.h>
|
---|
23 |
|
---|
24 | class ATL_NO_VTABLE VBoxEvent :
|
---|
25 | public VirtualBoxBase,
|
---|
26 | VBOX_SCRIPTABLE_IMPL(IEvent)
|
---|
27 | {
|
---|
28 | public:
|
---|
29 | VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT(VBoxEvent, IEvent)
|
---|
30 |
|
---|
31 | DECLARE_NOT_AGGREGATABLE(VBoxEvent)
|
---|
32 |
|
---|
33 | DECLARE_PROTECT_FINAL_CONSTRUCT()
|
---|
34 |
|
---|
35 | BEGIN_COM_MAP(VBoxEvent)
|
---|
36 | COM_INTERFACE_ENTRY(ISupportErrorInfo)
|
---|
37 | COM_INTERFACE_ENTRY(IEvent)
|
---|
38 | COM_INTERFACE_ENTRY(IDispatch)
|
---|
39 | END_COM_MAP()
|
---|
40 |
|
---|
41 | VBoxEvent() {}
|
---|
42 | virtual ~VBoxEvent() {}
|
---|
43 |
|
---|
44 | HRESULT FinalConstruct();
|
---|
45 | void FinalRelease();
|
---|
46 |
|
---|
47 | // public initializer/uninitializer for internal purposes only
|
---|
48 | HRESULT init (IEventSource *aSource, VBoxEventType_T aType, BOOL aWaitable);
|
---|
49 | void uninit();
|
---|
50 |
|
---|
51 | // IEvent properties
|
---|
52 | STDMETHOD(COMGETTER(Type)) (VBoxEventType_T *aType);
|
---|
53 | STDMETHOD(COMGETTER(Source)) (IEventSource * *aSource);
|
---|
54 | STDMETHOD(COMGETTER(Waitable)) (BOOL *aWaitable);
|
---|
55 |
|
---|
56 | // IEvent methods
|
---|
57 | STDMETHOD(SetProcessed)();
|
---|
58 | STDMETHOD(WaitProcessed)(LONG aTimeout, BOOL *aResult);
|
---|
59 |
|
---|
60 | private:
|
---|
61 | struct Data;
|
---|
62 |
|
---|
63 | Data* m;
|
---|
64 | };
|
---|
65 |
|
---|
66 | class ATL_NO_VTABLE VBoxVetoEvent :
|
---|
67 | public VBoxEvent,
|
---|
68 | VBOX_SCRIPTABLE_IMPL(IVetoEvent)
|
---|
69 | {
|
---|
70 | public:
|
---|
71 | VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT(VBoxVetoEvent, IVetoEvent)
|
---|
72 |
|
---|
73 | DECLARE_NOT_AGGREGATABLE(VBoxVetoEvent)
|
---|
74 |
|
---|
75 | DECLARE_PROTECT_FINAL_CONSTRUCT()
|
---|
76 |
|
---|
77 | BEGIN_COM_MAP(VBoxVetoEvent)
|
---|
78 | COM_INTERFACE_ENTRY(ISupportErrorInfo)
|
---|
79 | COM_INTERFACE_ENTRY2(IEvent, IVetoEvent)
|
---|
80 | COM_INTERFACE_ENTRY(IVetoEvent)
|
---|
81 | COM_INTERFACE_ENTRY2(IDispatch, IVetoEvent)
|
---|
82 | END_COM_MAP()
|
---|
83 |
|
---|
84 | VBoxVetoEvent() {}
|
---|
85 | virtual ~VBoxVetoEvent() {}
|
---|
86 |
|
---|
87 | HRESULT FinalConstruct();
|
---|
88 | void FinalRelease();
|
---|
89 |
|
---|
90 | // public initializer/uninitializer for internal purposes only
|
---|
91 | HRESULT init (IEventSource *aSource, VBoxEventType_T aType);
|
---|
92 | void uninit();
|
---|
93 |
|
---|
94 | // IEvent properties
|
---|
95 | STDMETHOD(COMGETTER(Type)) (VBoxEventType_T *aType)
|
---|
96 | {
|
---|
97 | return VBoxEvent::COMGETTER(Type)(aType);
|
---|
98 | }
|
---|
99 | STDMETHOD(COMGETTER(Source)) (IEventSource * *aSource)
|
---|
100 | {
|
---|
101 | return VBoxEvent::COMGETTER(Source)(aSource);
|
---|
102 | }
|
---|
103 | STDMETHOD(COMGETTER(Waitable)) (BOOL *aWaitable)
|
---|
104 | {
|
---|
105 | return VBoxEvent::COMGETTER(Waitable)(aWaitable);
|
---|
106 | }
|
---|
107 |
|
---|
108 | // IEvent methods
|
---|
109 | STDMETHOD(SetProcessed)()
|
---|
110 | {
|
---|
111 | return VBoxEvent::SetProcessed();
|
---|
112 | }
|
---|
113 | STDMETHOD(WaitProcessed)(LONG aTimeout, BOOL *aResult)
|
---|
114 | {
|
---|
115 | return VBoxEvent::WaitProcessed(aTimeout, aResult);
|
---|
116 | }
|
---|
117 |
|
---|
118 | // IVetoEvent methods
|
---|
119 | STDMETHOD(AddVeto)(IN_BSTR aVeto);
|
---|
120 | STDMETHOD(IsVetoed)(BOOL *aResult);
|
---|
121 | STDMETHOD(GetVetos)(ComSafeArrayOut(BSTR, aVetos));
|
---|
122 |
|
---|
123 | private:
|
---|
124 | struct Data;
|
---|
125 |
|
---|
126 | Data* m;
|
---|
127 | };
|
---|
128 |
|
---|
129 | class ATL_NO_VTABLE EventSource :
|
---|
130 | public VirtualBoxBase,
|
---|
131 | VBOX_SCRIPTABLE_IMPL(IEventSource)
|
---|
132 | {
|
---|
133 | public:
|
---|
134 |
|
---|
135 | VIRTUALBOXBASE_ADD_ERRORINFO_SUPPORT(EventSource, IEventSource)
|
---|
136 |
|
---|
137 | DECLARE_NOT_AGGREGATABLE(EventSource)
|
---|
138 |
|
---|
139 | DECLARE_PROTECT_FINAL_CONSTRUCT()
|
---|
140 |
|
---|
141 | BEGIN_COM_MAP(EventSource)
|
---|
142 | COM_INTERFACE_ENTRY(ISupportErrorInfo)
|
---|
143 | COM_INTERFACE_ENTRY(IEventSource)
|
---|
144 | COM_INTERFACE_ENTRY(IDispatch)
|
---|
145 | END_COM_MAP()
|
---|
146 |
|
---|
147 | DECLARE_EMPTY_CTOR_DTOR (EventSource)
|
---|
148 |
|
---|
149 | HRESULT FinalConstruct();
|
---|
150 | void FinalRelease();
|
---|
151 |
|
---|
152 | // public initializer/uninitializer for internal purposes only
|
---|
153 | HRESULT init (IUnknown * aParent);
|
---|
154 | void uninit();
|
---|
155 |
|
---|
156 | // IEventSource methods
|
---|
157 | STDMETHOD(CreateListener)(IEventListener ** aListener);
|
---|
158 | STDMETHOD(CreateAggregator)(ComSafeArrayIn(IEventSource*, aSubordinates),
|
---|
159 | IEventSource ** aAggregator);
|
---|
160 | STDMETHOD(RegisterListener)(IEventListener * aListener,
|
---|
161 | ComSafeArrayIn(VBoxEventType_T, aInterested),
|
---|
162 | BOOL aActive);
|
---|
163 | STDMETHOD(UnregisterListener)(IEventListener * aListener);
|
---|
164 | STDMETHOD(FireEvent)(IEvent * aEvent,
|
---|
165 | LONG aTimeout,
|
---|
166 | BOOL *aProcessed);
|
---|
167 | STDMETHOD(GetEvent)(IEventListener * aListener,
|
---|
168 | LONG aTimeout,
|
---|
169 | IEvent * *aEvent);
|
---|
170 | STDMETHOD(EventProcessed)(IEventListener * aListener,
|
---|
171 | IEvent * aEvent);
|
---|
172 |
|
---|
173 | private:
|
---|
174 | struct Data;
|
---|
175 |
|
---|
176 | Data* m;
|
---|
177 |
|
---|
178 | friend class ListenerRecord;
|
---|
179 | };
|
---|
180 |
|
---|
181 | class VBoxEventDesc
|
---|
182 | {
|
---|
183 | public:
|
---|
184 | VBoxEventDesc()
|
---|
185 | : mEvent(0), mEventSource(0)
|
---|
186 | {}
|
---|
187 | ~VBoxEventDesc()
|
---|
188 | {}
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * This function to be used with some care, as arguments order must match attribute declaration order
|
---|
192 | * event class and its superclasses up to IEvent. If unsure, consult implementation in
|
---|
193 | * generated VBoxEvents.cpp.
|
---|
194 | */
|
---|
195 | HRESULT init(IEventSource* aSource, VBoxEventType_T aType, ...);
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * Function similar to the above, but assumes that init() for this type already called once,
|
---|
199 | * so no need to allocate memory, and only reinit fields. Assumes event is subtype of
|
---|
200 | * IReusableEvent, asserts otherwise.
|
---|
201 | */
|
---|
202 | HRESULT reinit(VBoxEventType_T aType, ...);
|
---|
203 |
|
---|
204 | void uninit()
|
---|
205 | {
|
---|
206 | mEvent.setNull();
|
---|
207 | mEventSource.setNull();
|
---|
208 | }
|
---|
209 |
|
---|
210 | void getEvent(IEvent ** aEvent)
|
---|
211 | {
|
---|
212 | mEvent.queryInterfaceTo(aEvent);
|
---|
213 | }
|
---|
214 |
|
---|
215 | BOOL fire(LONG aTimeout)
|
---|
216 | {
|
---|
217 | if (mEventSource && mEvent)
|
---|
218 | {
|
---|
219 | BOOL fDelivered = FALSE;
|
---|
220 | int rc = mEventSource->FireEvent(mEvent, aTimeout, &fDelivered);
|
---|
221 | AssertRCReturn(rc, FALSE);
|
---|
222 | return fDelivered;
|
---|
223 | }
|
---|
224 | return FALSE;
|
---|
225 | }
|
---|
226 |
|
---|
227 | private:
|
---|
228 | ComPtr<IEvent> mEvent;
|
---|
229 | ComPtr<IEventSource> mEventSource;
|
---|
230 | };
|
---|
231 |
|
---|
232 | #endif // ____H_EVENTIMPL
|
---|