summaryrefslogtreecommitdiff
path: root/src/vm/comconnectionpoints.h
blob: 1a6b644834490d7e510af0c1e2938f568b9da3b4 (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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
// ===========================================================================
// File: ComConnectionPoints.h
//

// ===========================================================================
// Declaration of the classes used to expose connection points to COM.
// ===========================================================================


#pragma once

#ifndef FEATURE_COMINTEROP
#error FEATURE_COMINTEROP is required for this file
#endif // FEATURE_COMINTEROP

#include "vars.hpp"
#include "comcallablewrapper.h"
#include "comdelegate.h"

//------------------------------------------------------------------------------------------
//      Definition of helper class used to expose connection points
//------------------------------------------------------------------------------------------

// Structure containing information regarding the methods that make up an event.
struct EventMethodInfo
{
    MethodDesc*     m_pEventMethod;
    MethodDesc*     m_pAddMethod;
    MethodDesc*     m_pRemoveMethod;
};


// Structure passed out as a cookie when Advise is called.
struct ConnectionCookie
{
    ConnectionCookie(OBJECTHANDLE hndEventProvObj) : m_hndEventProvObj(hndEventProvObj)
    {
        CONTRACTL
        {
            NOTHROW;
            GC_NOTRIGGER;
            MODE_ANY;
            PRECONDITION(NULL != hndEventProvObj);
        }
        CONTRACTL_END;
    }

    ~ConnectionCookie()
    {
        WRAPPER_NO_CONTRACT;
        DestroyHandle(m_hndEventProvObj);
    }

    // Currently called only from Cooperative mode.
    static ConnectionCookie* CreateConnectionCookie(OBJECTHANDLE hndEventProvObj)
    {
        CONTRACT (ConnectionCookie*)
        {
            THROWS;
            GC_NOTRIGGER;
            MODE_ANY;
            INJECT_FAULT(COMPlusThrowOM());
            PRECONDITION(NULL != hndEventProvObj);
        }
        CONTRACT_END;
        
        RETURN (new ConnectionCookie(hndEventProvObj));
    }

    SLink           m_Link;
    OBJECTHANDLE    m_hndEventProvObj;
    DWORD           m_id;
};

FORCEINLINE void ConnectionCookieRelease(ConnectionCookie* p)
{
    WRAPPER_NO_CONTRACT;
    
    delete p;
}

// Connection cookie holder used to ensure the cookies are deleted when required.
class ConnectionCookieHolder : public Wrapper<ConnectionCookie*, ConnectionCookieDoNothing, ConnectionCookieRelease, NULL>
{
public:
    ConnectionCookieHolder(ConnectionCookie* p = NULL)
        : Wrapper<ConnectionCookie*, ConnectionCookieDoNothing, ConnectionCookieRelease, NULL>(p)
    {
        WRAPPER_NO_CONTRACT;
    }
    
    FORCEINLINE void operator=(ConnectionCookie* p)
    {
        WRAPPER_NO_CONTRACT;
        Wrapper<ConnectionCookie*, ConnectionCookieDoNothing, ConnectionCookieRelease, NULL>::operator=(p);
    }
};

// List of connection cookies.
typedef SList<ConnectionCookie, true> CONNECTIONCOOKIELIST;

// ConnectionPoint class. This class implements IConnectionPoint and does the mapping 
// from a CP handler to a TCE provider.
class ConnectionPoint : public IConnectionPoint 
{
public:
    // Encapsulate CrstHolder, so that clients of our lock don't have to know the
    // details of its implementation.
    class LockHolder : public CrstHolder
    {
    public:
        LockHolder(ConnectionPoint *pCP) : CrstHolder(&pCP->m_Lock)
        {
            WRAPPER_NO_CONTRACT;
        }
    };

    ConnectionPoint( ComCallWrapper *pWrap, MethodTable *pEventMT );
    ~ConnectionPoint();

    HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
    ULONG __stdcall AddRef();
    ULONG __stdcall Release();

    HRESULT __stdcall GetConnectionInterface( IID *pIID );
    HRESULT __stdcall GetConnectionPointContainer( IConnectionPointContainer **ppCPC );
    HRESULT __stdcall Advise( IUnknown *pUnk, DWORD *pdwCookie );
    HRESULT __stdcall Unadvise( DWORD dwCookie );
    HRESULT __stdcall EnumConnections( IEnumConnections **ppEnum );

    REFIID GetIID()
    {
        LIMITED_METHOD_CONTRACT;
        return m_rConnectionIID;
    }

    CONNECTIONCOOKIELIST *GetCookieList()
    {
        LIMITED_METHOD_CONTRACT;
        return &m_ConnectionList;
    }

private:   
    // Structures used for the AD callback wrappers.
    struct GetConnectionPointContainer_Args
    {
        ConnectionPoint *pThis;
        IConnectionPointContainer **ppCPC;
    };

    struct Advise_Args
    {
        ConnectionPoint *pThis;
        IUnknown *pUnk;
        DWORD *pdwCookie;
    };

    struct Unadvise_Args
    {
        ConnectionPoint *pThis;
        DWORD dwCookie;
    };
      
    // Worker methods.
    void AdviseWorker(IUnknown *pUnk, DWORD *pdwCookie);
    void UnadviseWorker( DWORD dwCookie );
    IConnectionPointContainer *GetConnectionPointContainerWorker();

    // AD callback wrappers.
    static void GetConnectionPointContainer_Wrapper(LPVOID ptr);
    static void Advise_Wrapper(LPVOID ptr);
    static void Unadvise_Wrapper(LPVOID ptr);

    // Helper methods.
    void SetupEventMethods();
    MethodDesc *FindProviderMethodDesc( MethodDesc *pEventMethodDesc, EnumEventMethods MethodType );
    void InvokeProviderMethod( OBJECTREF pProvider, OBJECTREF pSubscriber, MethodDesc *pProvMethodDesc, MethodDesc *pEventMethodDesc );
    void InsertWithLock(ConnectionCookie* pConCookie);
    void FindAndRemoveWithLock(ConnectionCookie* pConCookie);
    ConnectionCookie* FindWithLock(DWORD idOfCookie);
    
    ComCallWrapper*                 m_pOwnerWrap;
    GUID                            m_rConnectionIID;
    MethodTable*                    m_pTCEProviderMT;
    MethodTable*                    m_pEventItfMT;
    Crst                            m_Lock;
    CONNECTIONCOOKIELIST            m_ConnectionList;
    EventMethodInfo*                m_apEventMethods;
    int                             m_NumEventMethods;
    ULONG                           m_cbRefCount;
    ConnectionCookie*               m_pLastInserted;

    const static DWORD      idUpperLimit        = 0xFFFFFFFF;
};

// Enumeration of connection points.
class ConnectionPointEnum : IEnumConnectionPoints
{
public:
    // Encapsulate CrstHolder, so that clients of our lock don't have to know the
    // details of its implementation.
    class LockHolder : public CrstHolder
    {
    public:
        LockHolder(ConnectionPointEnum *pCP) : CrstHolder(&pCP->m_Lock)
        {
            WRAPPER_NO_CONTRACT;
        }
    };

    ConnectionPointEnum(ComCallWrapper *pOwnerWrap, CQuickArray<ConnectionPoint*> *pCPList);
    ~ConnectionPointEnum();

    HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
    ULONG __stdcall AddRef();
    ULONG __stdcall Release();

    HRESULT __stdcall Next(ULONG cConnections, IConnectionPoint **ppCP, ULONG *pcFetched);   
    HRESULT __stdcall Skip(ULONG cConnections);    
    HRESULT __stdcall Reset();    
    HRESULT __stdcall Clone(IEnumConnectionPoints **ppEnum);

private:
    ComCallWrapper*                 m_pOwnerWrap;
    CQuickArray<ConnectionPoint*>*  m_pCPList;
    UINT                            m_CurrPos;
    ULONG                           m_cbRefCount;
    Crst                            m_Lock;
};

// Enumeration of connections.
class ConnectionEnum : IEnumConnections
{
public:
    ConnectionEnum(ConnectionPoint *pConnectionPoint);
    ~ConnectionEnum();

    HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
    ULONG __stdcall AddRef();
    ULONG __stdcall Release();

    HRESULT __stdcall Next(ULONG cConnections, CONNECTDATA* rgcd, ULONG *pcFetched);       
    HRESULT __stdcall Skip(ULONG cConnections);
    HRESULT __stdcall Reset();
    HRESULT __stdcall Clone(IEnumConnections **ppEnum);

private:
    ConnectionPoint*                m_pConnectionPoint;
    ConnectionCookie*               m_CurrCookie;
    ULONG                           m_cbRefCount;
};