1 | /* $Id: listeners.h 101967 2023-11-08 12:44:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * MS COM / XPCOM Abstraction Layer - Listener helpers.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 | #ifndef VBOX_INCLUDED_com_listeners_h
|
---|
38 | #define VBOX_INCLUDED_com_listeners_h
|
---|
39 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
40 | # pragma once
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | #include <VBox/com/com.h>
|
---|
44 | #include <VBox/com/VirtualBox.h>
|
---|
45 |
|
---|
46 |
|
---|
47 | /** @defgroup grp_com_listeners Listener Helpers
|
---|
48 | * @ingroup grp_com
|
---|
49 | * @{
|
---|
50 | */
|
---|
51 |
|
---|
52 |
|
---|
53 | #ifdef VBOX_WITH_XPCOM
|
---|
54 | # define NS_IMPL_QUERY_HEAD_INLINE() \
|
---|
55 | NS_IMETHODIMP QueryInterface(REFNSIID aIID, void **aInstancePtr) \
|
---|
56 | { \
|
---|
57 | NS_ASSERTION(aInstancePtr, "QueryInterface requires a non-NULL destination!"); \
|
---|
58 | nsISupports *foundInterface;
|
---|
59 |
|
---|
60 | # define NS_INTERFACE_MAP_BEGIN_INLINE() NS_IMPL_QUERY_HEAD_INLINE()
|
---|
61 |
|
---|
62 | # define NS_IMPL_QUERY_INTERFACE1_INLINE(a_i1) \
|
---|
63 | NS_INTERFACE_MAP_BEGIN_INLINE() \
|
---|
64 | NS_INTERFACE_MAP_ENTRY(a_i1) \
|
---|
65 | NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, a_i1) \
|
---|
66 | NS_INTERFACE_MAP_END
|
---|
67 | #endif
|
---|
68 |
|
---|
69 | template <class T, class TParam = void *>
|
---|
70 | class ListenerImpl :
|
---|
71 | public ATL::CComObjectRootEx<ATL::CComMultiThreadModel>,
|
---|
72 | VBOX_SCRIPTABLE_IMPL(IEventListener)
|
---|
73 | {
|
---|
74 | T* mListener;
|
---|
75 |
|
---|
76 | #ifdef RT_OS_WINDOWS
|
---|
77 | /* FTM stuff */
|
---|
78 | ComPtr<IUnknown> m_pUnkMarshaler;
|
---|
79 | #else
|
---|
80 | nsAutoRefCnt mRefCnt;
|
---|
81 | NS_DECL_OWNINGTHREAD
|
---|
82 | #endif
|
---|
83 |
|
---|
84 | public:
|
---|
85 | ListenerImpl()
|
---|
86 | {
|
---|
87 | }
|
---|
88 |
|
---|
89 | virtual ~ListenerImpl()
|
---|
90 | {
|
---|
91 | }
|
---|
92 |
|
---|
93 | HRESULT init(T* aListener, TParam param)
|
---|
94 | {
|
---|
95 | mListener = aListener;
|
---|
96 | return mListener->init(param);
|
---|
97 | }
|
---|
98 |
|
---|
99 | HRESULT init(T* aListener)
|
---|
100 | {
|
---|
101 | mListener = aListener;
|
---|
102 | return mListener->init();
|
---|
103 | }
|
---|
104 |
|
---|
105 | void uninit()
|
---|
106 | {
|
---|
107 | if (mListener)
|
---|
108 | {
|
---|
109 | mListener->uninit();
|
---|
110 | delete mListener;
|
---|
111 | mListener = 0;
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | HRESULT FinalConstruct()
|
---|
116 | {
|
---|
117 | #ifdef RT_OS_WINDOWS
|
---|
118 | return CoCreateFreeThreadedMarshaler(this, &m_pUnkMarshaler.m_p);
|
---|
119 | #else
|
---|
120 | return S_OK;
|
---|
121 | #endif
|
---|
122 | }
|
---|
123 |
|
---|
124 | void FinalRelease()
|
---|
125 | {
|
---|
126 | uninit();
|
---|
127 | #ifdef RT_OS_WINDOWS
|
---|
128 | m_pUnkMarshaler.setNull();
|
---|
129 | #endif
|
---|
130 | }
|
---|
131 |
|
---|
132 | T* getWrapped()
|
---|
133 | {
|
---|
134 | return mListener;
|
---|
135 | }
|
---|
136 |
|
---|
137 | DECLARE_NOT_AGGREGATABLE(ListenerImpl)
|
---|
138 |
|
---|
139 | DECLARE_PROTECT_FINAL_CONSTRUCT()
|
---|
140 |
|
---|
141 | #ifdef RT_OS_WINDOWS
|
---|
142 | BEGIN_COM_MAP(ListenerImpl)
|
---|
143 | COM_INTERFACE_ENTRY(IEventListener)
|
---|
144 | COM_INTERFACE_ENTRY2(IDispatch, IEventListener)
|
---|
145 | COM_INTERFACE_ENTRY_AGGREGATE(IID_IMarshal, m_pUnkMarshaler.m_p)
|
---|
146 | END_COM_MAP()
|
---|
147 | #else
|
---|
148 | NS_IMETHOD_(nsrefcnt) AddRef(void)
|
---|
149 | {
|
---|
150 | NS_PRECONDITION(PRInt32(mRefCnt) >= 0, "illegal refcnt");
|
---|
151 | nsrefcnt count;
|
---|
152 | count = ASMAtomicIncU32((volatile uint32_t *)&mRefCnt);
|
---|
153 | NS_LOG_ADDREF(this, count, "ListenerImpl", sizeof(*this));
|
---|
154 | return count;
|
---|
155 | }
|
---|
156 |
|
---|
157 | NS_IMETHOD_(nsrefcnt) Release(void)
|
---|
158 | {
|
---|
159 | nsrefcnt count;
|
---|
160 | NS_PRECONDITION(0 != mRefCnt, "dup release");
|
---|
161 | count = ASMAtomicDecU32((volatile uint32_t *)&mRefCnt);
|
---|
162 | NS_LOG_RELEASE(this, count, "ListenerImpl");
|
---|
163 | if (0 == count) {
|
---|
164 | mRefCnt = 1; /* stabilize */
|
---|
165 | /* enable this to find non-threadsafe destructors: */
|
---|
166 | /* NS_ASSERT_OWNINGTHREAD(_class); */
|
---|
167 | NS_DELETEXPCOM(this);
|
---|
168 | return 0;
|
---|
169 | }
|
---|
170 | return count;
|
---|
171 | }
|
---|
172 |
|
---|
173 | NS_IMPL_QUERY_INTERFACE1_INLINE(IEventListener)
|
---|
174 | #endif
|
---|
175 |
|
---|
176 |
|
---|
177 | STDMETHOD(HandleEvent)(IEvent * aEvent)
|
---|
178 | {
|
---|
179 | VBoxEventType_T aType = VBoxEventType_Invalid;
|
---|
180 | HRESULT hrc = aEvent->COMGETTER(Type)(&aType);
|
---|
181 | AssertMsg(SUCCEEDED(hrc), ("hrc=%Rhrc\n", hrc)); RT_NOREF(hrc);
|
---|
182 | return mListener->HandleEvent(aType, aEvent);
|
---|
183 | }
|
---|
184 | };
|
---|
185 |
|
---|
186 | #ifdef VBOX_WITH_XPCOM
|
---|
187 | # define VBOX_LISTENER_DECLARE(klazz) NS_DECL_CLASSINFO(klazz)
|
---|
188 | #else
|
---|
189 | # define VBOX_LISTENER_DECLARE(klazz)
|
---|
190 | #endif
|
---|
191 |
|
---|
192 | /** @} */
|
---|
193 | #endif /* !VBOX_INCLUDED_com_listeners_h */
|
---|
194 |
|
---|