events.c 13 KB
Newer Older
1
/*
2
 * Implementation of event-related interfaces for WebBrowser control:
3 4 5
 *
 *  - IConnectionPointContainer
 *  - IConnectionPoint
6
 *
7
 * Copyright 2001 John R. Sheets (for CodeWeavers)
8
 * Copyright 2006 Jacek Caban for CodeWeavers
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 24
 */

25
#include <string.h>
26 27 28

#include "ieframe.h"

29
#include "wine/debug.h"
30

31
WINE_DEFAULT_DEBUG_CHANNEL(ieframe);
32

33
struct ConnectionPoint {
34
    IConnectionPoint IConnectionPoint_iface;
35

36
    IConnectionPointContainer *container;
37

38 39 40
    IDispatch **sinks;
    DWORD sinks_size;

41
    IID iid;
42
};
43

44 45 46
/**********************************************************************
 * Implement the IConnectionPointContainer interface
 */
47

48 49
static inline ConnectionPointContainer *impl_from_IConnectionPointContainer(IConnectionPointContainer *iface)
{
50
    return CONTAINING_RECORD(iface, ConnectionPointContainer, IConnectionPointContainer_iface);
51
}
52

53
static HRESULT WINAPI ConnectionPointContainer_QueryInterface(IConnectionPointContainer *iface,
54
        REFIID riid, LPVOID *ppv)
55
{
56
    ConnectionPointContainer *This = impl_from_IConnectionPointContainer(iface);
57
    return IUnknown_QueryInterface(This->impl, riid, ppv);
58
}
59

60
static ULONG WINAPI ConnectionPointContainer_AddRef(IConnectionPointContainer *iface)
61
{
62
    ConnectionPointContainer *This = impl_from_IConnectionPointContainer(iface);
63
    return IUnknown_AddRef(This->impl);
64
}
65

66
static ULONG WINAPI ConnectionPointContainer_Release(IConnectionPointContainer *iface)
67
{
68
    ConnectionPointContainer *This = impl_from_IConnectionPointContainer(iface);
69
    return IUnknown_Release(This->impl);
70
}
71

72 73
static HRESULT WINAPI ConnectionPointContainer_EnumConnectionPoints(IConnectionPointContainer *iface,
        LPENUMCONNECTIONPOINTS *ppEnum)
74
{
75
    ConnectionPointContainer *This = impl_from_IConnectionPointContainer(iface);
76 77
    FIXME("(%p)->(%p)\n", This, ppEnum);
    return E_NOTIMPL;
78
}
79

80 81
static HRESULT WINAPI ConnectionPointContainer_FindConnectionPoint(IConnectionPointContainer *iface,
        REFIID riid, LPCONNECTIONPOINT *ppCP)
82
{
83
    ConnectionPointContainer *This = impl_from_IConnectionPointContainer(iface);
84

85 86 87 88
    if(!ppCP) {
        WARN("ppCP == NULL\n");
        return E_POINTER;
    }
89

90 91 92 93
    *ppCP = NULL;

    if(IsEqualGUID(&DIID_DWebBrowserEvents2, riid)) {
        TRACE("(%p)->(DIID_DWebBrowserEvents2 %p)\n", This, ppCP);
94
        *ppCP = &This->wbe2->IConnectionPoint_iface;
95 96
    }else if(IsEqualGUID(&DIID_DWebBrowserEvents, riid)) {
        TRACE("(%p)->(DIID_DWebBrowserEvents %p)\n", This, ppCP);
97
        *ppCP = &This->wbe->IConnectionPoint_iface;
98 99
    }else if(IsEqualGUID(&IID_IPropertyNotifySink, riid)) {
        TRACE("(%p)->(IID_IPropertyNotifySink %p)\n", This, ppCP);
100
        *ppCP = &This->pns->IConnectionPoint_iface;
101
    }
102 103 104

    if(*ppCP) {
        IConnectionPoint_AddRef(*ppCP);
105 106 107
        return S_OK;
    }

108
    WARN("Unsupported IID %s\n", debugstr_guid(riid));
109
    return CONNECT_E_NOCONNECTION;
110 111
}

112
static const IConnectionPointContainerVtbl ConnectionPointContainerVtbl =
113
{
114 115 116 117 118
    ConnectionPointContainer_QueryInterface,
    ConnectionPointContainer_AddRef,
    ConnectionPointContainer_Release,
    ConnectionPointContainer_EnumConnectionPoints,
    ConnectionPointContainer_FindConnectionPoint
119 120
};

121 122 123 124 125

/**********************************************************************
 * Implement the IConnectionPoint interface
 */

126 127
static inline ConnectionPoint *impl_from_IConnectionPoint(IConnectionPoint *iface)
{
128
    return CONTAINING_RECORD(iface, ConnectionPoint, IConnectionPoint_iface);
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
typedef struct {
    IEnumConnections IEnumConnections_iface;

    LONG ref;

    ConnectionPoint *cp;
    DWORD iter;
} EnumConnections;

static inline EnumConnections *impl_from_IEnumConnections(IEnumConnections *iface)
{
    return CONTAINING_RECORD(iface, EnumConnections, IEnumConnections_iface);
}

static HRESULT WINAPI EnumConnections_QueryInterface(IEnumConnections *iface, REFIID riid, void **ppv)
{
    EnumConnections *This = impl_from_IEnumConnections(iface);

    if(IsEqualGUID(&IID_IUnknown, riid)) {
        TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
        *ppv = &This->IEnumConnections_iface;
    }else if(IsEqualGUID(&IID_IEnumConnections, riid)) {
        TRACE("(%p)->(IID_IEnumConnections %p)\n", This, ppv);
        *ppv = &This->IEnumConnections_iface;
    }else {
        WARN("Unsupported interface %s\n", debugstr_guid(riid));
        *ppv = NULL;
        return E_NOINTERFACE;
    }

    IUnknown_AddRef((IUnknown*)*ppv);
    return S_OK;
}

static ULONG WINAPI EnumConnections_AddRef(IEnumConnections *iface)
{
    EnumConnections *This = impl_from_IEnumConnections(iface);
    LONG ref = InterlockedIncrement(&This->ref);

    TRACE("(%p) ref=%d\n", This, ref);

    return ref;
}

static ULONG WINAPI EnumConnections_Release(IEnumConnections *iface)
{
    EnumConnections *This = impl_from_IEnumConnections(iface);
    LONG ref = InterlockedDecrement(&This->ref);

    TRACE("(%p) ref=%d\n", This, ref);

    if(!ref) {
        IConnectionPoint_Release(&This->cp->IConnectionPoint_iface);
        heap_free(This);
    }

    return ref;
}

static HRESULT WINAPI EnumConnections_Next(IEnumConnections *iface, ULONG cConnections, CONNECTDATA *pgcd, ULONG *pcFetched)
{
    EnumConnections *This = impl_from_IEnumConnections(iface);
    ULONG cnt = 0;

    TRACE("(%p)->(%u %p %p)\n", This, cConnections, pgcd, pcFetched);

    while(cConnections--) {
        while(This->iter < This->cp->sinks_size && !This->cp->sinks[This->iter])
            This->iter++;
        if(This->iter == This->cp->sinks_size)
            break;

        pgcd[cnt].pUnk = (IUnknown*)This->cp->sinks[This->iter];
        pgcd[cnt].dwCookie = cnt+1;
        This->iter++;
        cnt++;
    }

    if(pcFetched)
        *pcFetched = cnt;
    return cnt ? S_OK : S_FALSE;
}

static HRESULT WINAPI EnumConnections_Skip(IEnumConnections *iface, ULONG cConnections)
{
    EnumConnections *This = impl_from_IEnumConnections(iface);
    FIXME("(%p)->(%u)\n", This, cConnections);
    return E_NOTIMPL;
}

static HRESULT WINAPI EnumConnections_Reset(IEnumConnections *iface)
{
    EnumConnections *This = impl_from_IEnumConnections(iface);
    FIXME("(%p)\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI EnumConnections_Clone(IEnumConnections *iface, IEnumConnections **ppEnum)
{
    EnumConnections *This = impl_from_IEnumConnections(iface);
    FIXME("(%p)->(%p)\n", This, ppEnum);
    return E_NOTIMPL;
}

static const IEnumConnectionsVtbl EnumConnectionsVtbl = {
    EnumConnections_QueryInterface,
    EnumConnections_AddRef,
    EnumConnections_Release,
    EnumConnections_Next,
    EnumConnections_Skip,
    EnumConnections_Reset,
    EnumConnections_Clone
};

245 246
static HRESULT WINAPI ConnectionPoint_QueryInterface(IConnectionPoint *iface,
                                                     REFIID riid, LPVOID *ppv)
247
{
248
    ConnectionPoint *This = impl_from_IConnectionPoint(iface);
249

250
    *ppv = NULL;
251

252 253
    if(IsEqualGUID(&IID_IUnknown, riid)) {
        TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
254
        *ppv = &This->IConnectionPoint_iface;
255 256
    }else if(IsEqualGUID(&IID_IConnectionPoint, riid)) {
        TRACE("(%p)->(IID_IConnectionPoint %p)\n", This, ppv);
257
        *ppv = &This->IConnectionPoint_iface;
258
    }
259

260
    if(*ppv) {
261
        IConnectionPointContainer_AddRef(This->container);
262 263 264 265 266
        return S_OK;
    }

    WARN("Unsupported interface %s\n", debugstr_guid(riid));
    return E_NOINTERFACE;
267 268
}

269
static ULONG WINAPI ConnectionPoint_AddRef(IConnectionPoint *iface)
270
{
271
    ConnectionPoint *This = impl_from_IConnectionPoint(iface);
272
    return IConnectionPointContainer_AddRef(This->container);
273 274
}

275
static ULONG WINAPI ConnectionPoint_Release(IConnectionPoint *iface)
276
{
277
    ConnectionPoint *This = impl_from_IConnectionPoint(iface);
278
    return IConnectionPointContainer_Release(This->container);
279 280
}

281
static HRESULT WINAPI ConnectionPoint_GetConnectionInterface(IConnectionPoint *iface, IID *pIID)
282
{
283
    ConnectionPoint *This = impl_from_IConnectionPoint(iface);
284 285 286

    TRACE("(%p)->(%p)\n", This, pIID);

287
    *pIID = This->iid;
288
    return S_OK;
289 290
}

291 292
static HRESULT WINAPI ConnectionPoint_GetConnectionPointContainer(IConnectionPoint *iface,
        IConnectionPointContainer **ppCPC)
293
{
294
    ConnectionPoint *This = impl_from_IConnectionPoint(iface);
295

296
    TRACE("(%p)->(%p)\n", This, ppCPC);
297

298 299
    *ppCPC = This->container;
    IConnectionPointContainer_AddRef(This->container);
300
    return S_OK;
301 302
}

303 304
static HRESULT WINAPI ConnectionPoint_Advise(IConnectionPoint *iface, IUnknown *pUnkSink,
                                             DWORD *pdwCookie)
305
{
306
    ConnectionPoint *This = impl_from_IConnectionPoint(iface);
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
    IDispatch *disp;
    DWORD i;
    HRESULT hres;

    TRACE("(%p)->(%p %p)\n", This, pUnkSink, pdwCookie);

    hres = IUnknown_QueryInterface(pUnkSink, &This->iid, (void**)&disp);
    if(FAILED(hres)) {
        hres = IUnknown_QueryInterface(pUnkSink, &IID_IDispatch, (void**)&disp);
        if(FAILED(hres))
            return CONNECT_E_CANNOTCONNECT;
    }

    if(This->sinks) {
        for(i=0; i<This->sinks_size; i++) {
            if(!This->sinks[i])
                break;
        }

        if(i == This->sinks_size)
327
            This->sinks = heap_realloc(This->sinks,
328
                                          (++This->sinks_size)*sizeof(*This->sinks));
329
    }else {
330
        This->sinks = heap_alloc(sizeof(*This->sinks));
331 332 333 334 335 336 337 338
        This->sinks_size = 1;
        i = 0;
    }

    This->sinks[i] = disp;
    *pdwCookie = i+1;

    return S_OK;
339 340
}

341
static HRESULT WINAPI ConnectionPoint_Unadvise(IConnectionPoint *iface, DWORD dwCookie)
342
{
343
    ConnectionPoint *This = impl_from_IConnectionPoint(iface);
344

345
    TRACE("(%p)->(%d)\n", This, dwCookie);
346 347 348 349 350 351 352 353

    if(!dwCookie || dwCookie > This->sinks_size || !This->sinks[dwCookie-1])
        return CONNECT_E_NOCONNECTION;

    IDispatch_Release(This->sinks[dwCookie-1]);
    This->sinks[dwCookie-1] = NULL;

    return S_OK;
354 355
}

356 357 358
static HRESULT WINAPI ConnectionPoint_EnumConnections(IConnectionPoint *iface,
                                                      IEnumConnections **ppEnum)
{
359
    ConnectionPoint *This = impl_from_IConnectionPoint(iface);
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
    EnumConnections *ret;

    TRACE("(%p)->(%p)\n", This, ppEnum);

    ret = heap_alloc(sizeof(*ret));
    if(!ret)
        return E_OUTOFMEMORY;

    ret->IEnumConnections_iface.lpVtbl = &EnumConnectionsVtbl;
    ret->ref = 1;
    ret->iter = 0;

    IConnectionPoint_AddRef(&This->IConnectionPoint_iface);
    ret->cp = This;

    *ppEnum = &ret->IEnumConnections_iface;
    return S_OK;
377
}
378

379
static const IConnectionPointVtbl ConnectionPointVtbl =
380
{
381 382 383 384 385 386 387 388
    ConnectionPoint_QueryInterface,
    ConnectionPoint_AddRef,
    ConnectionPoint_Release,
    ConnectionPoint_GetConnectionInterface,
    ConnectionPoint_GetConnectionPointContainer,
    ConnectionPoint_Advise,
    ConnectionPoint_Unadvise,
    ConnectionPoint_EnumConnections
389 390
};

391 392 393 394 395 396 397 398 399 400 401
void call_sink(ConnectionPoint *This, DISPID dispid, DISPPARAMS *dispparams)
{
    DWORD i;

    for(i=0; i<This->sinks_size; i++) {
        if(This->sinks[i])
            IDispatch_Invoke(This->sinks[i], dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT,
                             DISPATCH_METHOD, dispparams, NULL, NULL, NULL);
    }
}

402 403
static void ConnectionPoint_Create(REFIID riid, ConnectionPoint **cp,
                                   IConnectionPointContainer *container)
404
{
405
    ConnectionPoint *ret = heap_alloc(sizeof(ConnectionPoint));
406

407
    ret->IConnectionPoint_iface.lpVtbl = &ConnectionPointVtbl;
408 409 410

    ret->sinks = NULL;
    ret->sinks_size = 0;
411
    ret->container = container;
412

413
    ret->iid = *riid;
414

415 416 417 418 419
    *cp = ret;
}

static void ConnectionPoint_Destroy(ConnectionPoint *This)
{
420
    DWORD i;
421 422 423 424 425 426

    for(i=0; i<This->sinks_size; i++) {
        if(This->sinks[i])
            IDispatch_Release(This->sinks[i]);
    }

427 428
    heap_free(This->sinks);
    heap_free(This);
429
}
430

431
void ConnectionPointContainer_Init(ConnectionPointContainer *This, IUnknown *impl)
432
{
433
    This->IConnectionPointContainer_iface.lpVtbl = &ConnectionPointContainerVtbl;
434

435 436 437
    ConnectionPoint_Create(&DIID_DWebBrowserEvents2, &This->wbe2, &This->IConnectionPointContainer_iface);
    ConnectionPoint_Create(&DIID_DWebBrowserEvents,  &This->wbe,  &This->IConnectionPointContainer_iface);
    ConnectionPoint_Create(&IID_IPropertyNotifySink, &This->pns,  &This->IConnectionPointContainer_iface);
438 439

    This->impl = impl;
440
}
441

442
void ConnectionPointContainer_Destroy(ConnectionPointContainer *This)
443
{
444 445 446
    ConnectionPoint_Destroy(This->wbe2);
    ConnectionPoint_Destroy(This->wbe);
    ConnectionPoint_Destroy(This->pns);
447
}