aboutsummaryrefslogtreecommitdiffstats
path: root/src/native/windows/msofficecomm/ConnectionPoint.h
blob: 6c629367c8d88327e8b1bfa2bcef9867d486e807 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
#ifndef _JMSOFFICECOMM_CONNECTIONPOINT_H_
#define _JMSOFFICECOMM_CONNECTIONPOINT_H_

#include <ocidl.h>
#include <olectl.h>
#include <stdint.h>
#include "UnknownImpl.h"

template <class T, REFIID IID_T>
class ConnectionPoint
    : public IConnectionPoint,
      public T
{
public:
    ConnectionPoint(IConnectionPointContainer *container)
            : _container(container),
              _sinkCount(0),
              _sinks(NULL)
        {
        }

    virtual ~ConnectionPoint()
        {
            if (_sinks)
                ::free(_sinks);
        }

    // IUnknown
    STDMETHODIMP QueryInterface(REFIID iid, PVOID *obj)
        {
            HRESULT hr;

            if (!obj)
                hr = E_POINTER;
            else if (IID_IUnknown == iid)
            {
                AddRef();
                *obj
                    = static_cast<LPUNKNOWN>(
                            static_cast<IConnectionPoint *>(this));
                hr = S_OK;
            }
            else if (IID_IConnectionPoint == iid)
            {
                AddRef();
                *obj = static_cast<IConnectionPoint *>(this);
                hr = S_OK;
            }
            else
            {
                *obj = NULL;
                hr = E_NOINTERFACE;
            }
            return hr;
        }

    STDMETHODIMP_(ULONG) AddRef() { return _container->AddRef(); }
    STDMETHODIMP_(ULONG) Release() { return _container->Release(); }

    // IDispatch
    STDMETHODIMP GetTypeInfoCount(UINT *)
        STDMETHODIMP_E_NOTIMPL_STUB
    STDMETHODIMP GetTypeInfo(UINT, LCID, LPTYPEINFO *)
        STDMETHODIMP_E_NOTIMPL_STUB
    STDMETHODIMP GetIDsOfNames(REFIID, LPOLESTR *, UINT, LCID, DISPID *)
        STDMETHODIMP_E_NOTIMPL_STUB
    STDMETHODIMP Invoke(DISPID, REFIID, LCID, WORD, DISPPARAMS *, VARIANT *, EXCEPINFO *, UINT *)
        STDMETHODIMP_E_NOTIMPL_STUB

    // IConnectionPoint
    STDMETHODIMP GetConnectionInterface(IID *pIID)
        {
            HRESULT hr;

            if (pIID)
            {
                *pIID = IID_T;
                hr = S_OK;
            }
            else
                hr = E_POINTER;
            return hr;
        }

    STDMETHODIMP GetConnectionPointContainer(IConnectionPointContainer **ppCPC)
        {
            HRESULT hr;

            if (ppCPC)
            {
                _container->AddRef();
                *ppCPC = _container;
                hr = S_OK;
            }
            else
                hr = E_POINTER;
            return hr;
        }

    STDMETHODIMP Advise(IUnknown *pUnkSink, DWORD *pdwCookie)
        {
            HRESULT hr;

            if (pdwCookie)
            {
                if (pUnkSink)
                {
                    T *t;

                    if (SUCCEEDED(
                            pUnkSink->QueryInterface(IID_T, (PVOID *) &t)))
                    {
                        LPDISPATCH iDispatch;

                        if (SUCCEEDED(
                                t->QueryInterface(
                                        IID_IDispatch,
                                        (PVOID *) &iDispatch)))
                        {
                            if (addSink(iDispatch))
                            {
                                *pdwCookie
                                    = (DWORD)
                                        (((intptr_t) iDispatch) & 0xffffffff);
                                hr = S_OK;
                            }
                            else
                            {
                                *pdwCookie = 0;
                                hr = CONNECT_E_CANNOTCONNECT;
                            }
                            iDispatch->Release();
                        }
                        else
                        {
                            *pdwCookie = 0;
                            hr = CONNECT_E_CANNOTCONNECT;
                        }

                        t->Release();
                    }
                    else
                    {
                        *pdwCookie = 0;
                        hr = CONNECT_E_CANNOTCONNECT;
                    }
                }
                else
                {
                    *pdwCookie = 0;
                    hr = E_POINTER; 
                }
            }
            else
                hr = E_POINTER;
            return hr;
        }

    STDMETHODIMP Unadvise(DWORD dwCookie)
        {
            size_t i = 0;
            LPDISPATCH *ptr = _sinks;
            HRESULT hr = E_POINTER;

            for (; i < _sinkCount; i++, ptr++)
            {
                LPDISPATCH iDispatch = *ptr;

                if (iDispatch
                        && (dwCookie
                                == (DWORD)
                                    (((intptr_t) iDispatch) & 0xffffffff)))
                {
                    *ptr = NULL;
                    iDispatch->Release();

                    _sinkCount--;
                    /*
                     * Move the emptied slot of the _sinks storage at the end
                     * where it is not accessible given the value of _sinkCount.
                     * Its memory is retained but it will either be used during
                     * a subsequent addSink(LPDISPATCH) or be freed upon
                     * deleting this ConnectionPoint.
                     */
                    for (; i < _sinkCount; i++)
                    {
                        LPDISPATCH *nextPtr = ptr + 1;

                        *ptr = *nextPtr;
                        ptr = nextPtr;
                    }

                    hr = S_OK;
                    break;
                }
            }
            return hr;
        }

    STDMETHODIMP EnumConnections(IEnumConnections **ppEnum)
        STDMETHODIMP_E_NOTIMPL_STUB

protected:
    HRESULT Invoke(DISPID dispIdMember, DISPPARAMS *pDispParams)
        {
            LPDISPATCH *sinks = getSinks();
            HRESULT hr;

            if (sinks)
            {
                for (LPDISPATCH sink, *sinkIt = sinks;
                        (sink = *sinkIt);
                        sinkIt++)
                {
                    hr
                        = sink->Invoke(
                                dispIdMember,
                                IID_NULL,
                                0,
                                DISPATCH_METHOD,
                                pDispParams,
                                NULL,
                                NULL,
                                NULL);
                    sink->Release();
                }
                ::free(sinks);

                hr = S_OK;
            }
            else
                hr = E_OUTOFMEMORY;
            return hr;
        }

private:
    BOOL addSink(LPDISPATCH sink)
        {
            BOOL b;

            if (containsSink(sink))
                b = FALSE;
            else
            {
                size_t newSinkCount = _sinkCount + 1;
                LPDISPATCH *newSinks
                    = (LPDISPATCH *)
                        ::realloc(_sinks, newSinkCount * sizeof(LPDISPATCH));

                if (newSinks)
                {
                    sink->AddRef();
                    newSinks[newSinkCount - 1] = sink;
                    _sinkCount = newSinkCount;
                    _sinks = newSinks;
                    b = TRUE;
                }
                else
                    b = FALSE;
            }
            return b;
        }

    BOOL containsSink(const LPDISPATCH sink)
        {
            size_t i = 0;
            LPDISPATCH *ptr = _sinks;

            for (; i < _sinkCount; i++, ptr++)
                if (sink == *ptr)
                    return TRUE;
            return FALSE;
        }

    LPDISPATCH *getSinks()
        {
            LPDISPATCH *sinks
                = (LPDISPATCH *)
                    ::malloc((_sinkCount + 1) * sizeof(LPDISPATCH));

            if (sinks)
            {
                size_t i = 0;
                LPDISPATCH *dst = sinks;
                LPDISPATCH *src = _sinks;

                for (; i < _sinkCount; i++, src++, dst++)
                {
                    LPDISPATCH sink = *src;

                    sink->AddRef();
                    *dst = sink;
                }
                *dst = NULL;
            }
            return sinks;
        }

    IConnectionPointContainer *_container;
    size_t _sinkCount;
    LPDISPATCH *_sinks;
};

#endif /* #ifndef _JMSOFFICECOMM_CONNECTIONPOINT_H_ */