connpt.c 16.6 KB
Newer Older
1 2 3 4 5
/*
 * Implementation of a generic ConnectionPoint object.
 *
 * Copyright 2000 Huw D M Davies for CodeWeavers
 *
6 7 8 9 10 11 12 13 14 15 16 17
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20
 *
 * NOTES:
21 22 23 24 25
 * See one exported function here is CreateConnectionPoint, see
 * comments just above that function for information.
 */

#include <assert.h>
26
#include <stdarg.h>
27
#include <string.h>
28 29 30

#define COBJMACROS

31
#include "winerror.h"
32
#include "windef.h"
33 34
#include "winbase.h"
#include "wingdi.h"
35 36
#include "winuser.h"
#include "ole2.h"
37 38 39
#include "olectl.h"
#include "connpt.h"

40
#include "wine/debug.h"
41

42
WINE_DEFAULT_DEBUG_CHANNEL(ole);
43 44 45 46 47 48 49 50

#define MAXSINKS 10

/************************************************************************
 * Implementation of IConnectionPoint
 */
typedef struct ConnectionPointImpl {

51
  IConnectionPoint IConnectionPoint_iface;
52 53 54 55 56

  /* IUnknown of our main object*/
  IUnknown *Obj;

  /* Reference count */
57
  LONG ref;
58 59 60 61 62 63 64 65 66 67 68

  /* IID of sink interface */
  IID iid;

  /* Array of sink IUnknowns */
  IUnknown **sinks;
  DWORD maxSinks;

  DWORD nSinks;
} ConnectionPointImpl;

69
static const IConnectionPointVtbl ConnectionPointImpl_VTable;
70 71 72 73 74 75 76


/************************************************************************
 * Implementation of IEnumConnections
 */
typedef struct EnumConnectionsImpl {

77
  IEnumConnections IEnumConnections_iface;
78

79
  LONG ref;
80 81 82 83 84 85 86

  /* IUnknown of ConnectionPoint, used for ref counting */
  IUnknown *pUnk;

  /* Connection Data */
  CONNECTDATA *pCD;
  DWORD nConns;
87

88 89 90 91 92 93 94 95 96
  /* Next connection to enumerate from */
  DWORD nCur;

} EnumConnectionsImpl;

static EnumConnectionsImpl *EnumConnectionsImpl_Construct(IUnknown *pUnk,
							  DWORD nSinks,
							  CONNECTDATA *pCD);

97 98 99 100 101 102 103 104 105
static inline ConnectionPointImpl *impl_from_IConnectionPoint(IConnectionPoint *iface)
{
  return CONTAINING_RECORD(iface, ConnectionPointImpl, IConnectionPoint_iface);
}

static inline EnumConnectionsImpl *impl_from_IEnumConnections(IEnumConnections *iface)
{
  return CONTAINING_RECORD(iface, EnumConnectionsImpl, IEnumConnections_iface);
}
106 107 108 109 110 111 112 113 114 115

/************************************************************************
 * ConnectionPointImpl_Construct
 */
static ConnectionPointImpl *ConnectionPointImpl_Construct(IUnknown *pUnk,
							  REFIID riid)
{
  ConnectionPointImpl *Obj;

  Obj = HeapAlloc(GetProcessHeap(), 0, sizeof(*Obj));
116
  Obj->IConnectionPoint_iface.lpVtbl = &ConnectionPointImpl_VTable;
117 118 119 120
  Obj->Obj = pUnk;
  Obj->ref = 1;
  Obj->iid =  *riid;
  Obj->maxSinks = MAXSINKS;
121
  Obj->sinks = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
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
			 sizeof(IUnknown*) * MAXSINKS);
  Obj->nSinks = 0;
  return Obj;
}

/************************************************************************
 * ConnectionPointImpl_Destroy
 */
static void ConnectionPointImpl_Destroy(ConnectionPointImpl *Obj)
{
  DWORD i;
  for(i = 0; i < Obj->maxSinks; i++) {
    if(Obj->sinks[i]) {
      IUnknown_Release(Obj->sinks[i]);
      Obj->sinks[i] = NULL;
    }
  }
  HeapFree(GetProcessHeap(), 0, Obj->sinks);
  HeapFree(GetProcessHeap(), 0, Obj);
  return;
}

static ULONG WINAPI ConnectionPointImpl_AddRef(IConnectionPoint* iface);
/************************************************************************
 * ConnectionPointImpl_QueryInterface (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
static HRESULT WINAPI ConnectionPointImpl_QueryInterface(
  IConnectionPoint*  iface,
  REFIID  riid,
  void**  ppvObject)
{
155
  ConnectionPointImpl *This = impl_from_IConnectionPoint(iface);
156 157 158 159 160 161 162
  TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppvObject);

  /*
   * Perform a sanity check on the parameters.
   */
  if ( (This==0) || (ppvObject==0) )
    return E_INVALIDARG;
163

164 165 166 167
  /*
   * Initialize the return parameter.
   */
  *ppvObject = 0;
168

169 170 171
  /*
   * Compare the riid with the interface IDs implemented by this object.
   */
172
  if (IsEqualIID(&IID_IUnknown, riid))
173
    *ppvObject = This;
174
  else if (IsEqualIID(&IID_IConnectionPoint, riid))
175
    *ppvObject = This;
176

177 178 179 180 181 182 183 184
  /*
   * Check that we obtained an interface.
   */
  if ((*ppvObject)==0)
  {
    FIXME("() : asking for un supported interface %s\n",debugstr_guid(riid));
    return E_NOINTERFACE;
  }
185

186 187 188 189
  /*
   * Query Interface always increases the reference count by one when it is
   * successful
   */
190
  ConnectionPointImpl_AddRef(&This->IConnectionPoint_iface);
191

192
  return S_OK;
193 194 195 196 197 198 199 200 201 202
}


/************************************************************************
 * ConnectionPointImpl_AddRef (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
static ULONG WINAPI ConnectionPointImpl_AddRef(IConnectionPoint* iface)
{
203
  ConnectionPointImpl *This = impl_from_IConnectionPoint(iface);
204 205
  ULONG refCount = InterlockedIncrement(&This->ref);

206
  TRACE("(%p)->(ref before=%d)\n", This, refCount - 1);
207 208

  return refCount;
209
}
210

211 212 213 214 215
/************************************************************************
 * ConnectionPointImpl_Release (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
216
static ULONG WINAPI ConnectionPointImpl_Release(
217 218
      IConnectionPoint* iface)
{
219
  ConnectionPointImpl *This = impl_from_IConnectionPoint(iface);
220
  ULONG refCount = InterlockedDecrement(&This->ref);
221

222
  TRACE("(%p)->(ref before=%d)\n", This, refCount + 1);
223 224 225 226

  /*
   * If the reference count goes down to 0, perform suicide.
   */
227
  if (!refCount) ConnectionPointImpl_Destroy(This);
228

229
  return refCount;
230 231 232 233 234 235 236 237 238 239
}

/************************************************************************
 * ConnectionPointImpl_GetConnectionInterface (IConnectionPoint)
 *
 */
static HRESULT WINAPI ConnectionPointImpl_GetConnectionInterface(
					       IConnectionPoint *iface,
					       IID              *piid)
{
240
  ConnectionPointImpl *This = impl_from_IConnectionPoint(iface);
241 242 243 244 245 246 247 248 249 250 251 252 253
  TRACE("(%p)->(%p) returning %s\n", This, piid, debugstr_guid(&(This->iid)));
  *piid = This->iid;
  return S_OK;
}

/************************************************************************
 * ConnectionPointImpl_GetConnectionPointContainer (IConnectionPoint)
 *
 */
static HRESULT WINAPI ConnectionPointImpl_GetConnectionPointContainer(
				      IConnectionPoint           *iface,
				      IConnectionPointContainer  **ppCPC)
{
254
  ConnectionPointImpl *This = impl_from_IConnectionPoint(iface);
255 256
  TRACE("(%p)->(%p)\n", This, ppCPC);

257
  return IUnknown_QueryInterface(This->Obj, &IID_IConnectionPointContainer, (void**)ppCPC);
258 259 260 261 262 263 264 265 266 267 268
}

/************************************************************************
 * ConnectionPointImpl_Advise (IConnectionPoint)
 *
 */
static HRESULT WINAPI ConnectionPointImpl_Advise(IConnectionPoint *iface,
						 IUnknown *lpUnk,
						 DWORD *pdwCookie)
{
  DWORD i;
269
  ConnectionPointImpl *This = impl_from_IConnectionPoint(iface);
270 271 272 273
  IUnknown *lpSink;
  TRACE("(%p)->(%p, %p)\n", This, lpUnk, pdwCookie);

  *pdwCookie = 0;
274
  if(FAILED(IUnknown_QueryInterface(lpUnk, &This->iid, (void**)&lpSink)))
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
    return CONNECT_E_CANNOTCONNECT;

  for(i = 0; i < This->maxSinks; i++) {
    if(This->sinks[i] == NULL)
      break;
  }
  if(i == This->maxSinks) {
    This->maxSinks += MAXSINKS;
    This->sinks = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->sinks,
			      This->maxSinks * sizeof(IUnknown *));
  }
  This->sinks[i] = lpSink;
  This->nSinks++;
  *pdwCookie = i + 1;
  return S_OK;
}


/************************************************************************
 * ConnectionPointImpl_Unadvise (IConnectionPoint)
 *
 */
static HRESULT WINAPI ConnectionPointImpl_Unadvise(IConnectionPoint *iface,
						   DWORD dwCookie)
{
300
  ConnectionPointImpl *This = impl_from_IConnectionPoint(iface);
301
  TRACE("(%p)->(%d)\n", This, dwCookie);
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319

  if(dwCookie == 0 || dwCookie > This->maxSinks) return E_INVALIDARG;

  if(This->sinks[dwCookie-1] == NULL) return CONNECT_E_NOCONNECTION;

  IUnknown_Release(This->sinks[dwCookie-1]);
  This->sinks[dwCookie-1] = NULL;
  This->nSinks--;
  return S_OK;
}

/************************************************************************
 * ConnectionPointImpl_EnumConnections (IConnectionPoint)
 *
 */
static HRESULT WINAPI ConnectionPointImpl_EnumConnections(
						    IConnectionPoint *iface,
						    LPENUMCONNECTIONS *ppEnum)
320
{
321
  ConnectionPointImpl *This = impl_from_IConnectionPoint(iface);
322 323 324 325 326 327
  CONNECTDATA *pCD;
  DWORD i, nextslot;
  EnumConnectionsImpl *EnumObj;
  HRESULT hr;

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

329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
  *ppEnum = NULL;

  if(This->nSinks == 0) return OLE_E_NOCONNECTION;

  pCD = HeapAlloc(GetProcessHeap(), 0, sizeof(CONNECTDATA) * This->nSinks);

  for(i = 0, nextslot = 0; i < This->maxSinks; i++) {
    if(This->sinks[i] != NULL) {
      pCD[nextslot].pUnk = This->sinks[i];
      pCD[nextslot].dwCookie = i + 1;
      nextslot++;
    }
  }
  assert(nextslot == This->nSinks);

  /* Bump the ref count of this object up by one.  It gets Released in
     IEnumConnections_Release */
  IUnknown_AddRef((IUnknown*)This);

  EnumObj = EnumConnectionsImpl_Construct((IUnknown*)This, This->nSinks, pCD);
349
  hr = IEnumConnections_QueryInterface(&EnumObj->IEnumConnections_iface,
350
                                       &IID_IEnumConnections, (void**)ppEnum);
351
  IEnumConnections_Release(&EnumObj->IEnumConnections_iface);
352 353 354 355 356

  HeapFree(GetProcessHeap(), 0, pCD);
  return hr;
}

357
static const IConnectionPointVtbl ConnectionPointImpl_VTable =
358 359 360 361 362 363 364 365 366 367 368 369
{
  ConnectionPointImpl_QueryInterface,
  ConnectionPointImpl_AddRef,
  ConnectionPointImpl_Release,
  ConnectionPointImpl_GetConnectionInterface,
  ConnectionPointImpl_GetConnectionPointContainer,
  ConnectionPointImpl_Advise,
  ConnectionPointImpl_Unadvise,
  ConnectionPointImpl_EnumConnections
};


370
static const IEnumConnectionsVtbl EnumConnectionsImpl_VTable;
371 372 373 374 375 376 377 378 379 380 381 382
static ULONG WINAPI EnumConnectionsImpl_AddRef(IEnumConnections* iface);

/************************************************************************
 * EnumConnectionsImpl_Construct
 */
static EnumConnectionsImpl *EnumConnectionsImpl_Construct(IUnknown *pUnk,
							  DWORD nSinks,
							  CONNECTDATA *pCD)
{
  EnumConnectionsImpl *Obj = HeapAlloc(GetProcessHeap(), 0, sizeof(*Obj));
  DWORD i;

383
  Obj->IEnumConnections_iface.lpVtbl = &EnumConnectionsImpl_VTable;
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
  Obj->ref = 1;
  Obj->pUnk = pUnk;
  Obj->pCD = HeapAlloc(GetProcessHeap(), 0, nSinks * sizeof(CONNECTDATA));
  Obj->nConns = nSinks;
  Obj->nCur = 0;

  for(i = 0; i < nSinks; i++) {
    Obj->pCD[i] = pCD[i];
    IUnknown_AddRef(Obj->pCD[i].pUnk);
  }
  return Obj;
}

/************************************************************************
 * EnumConnectionsImpl_Destroy
 */
static void EnumConnectionsImpl_Destroy(EnumConnectionsImpl *Obj)
{
  DWORD i;

  for(i = 0; i < Obj->nConns; i++)
    IUnknown_Release(Obj->pCD[i].pUnk);

  HeapFree(GetProcessHeap(), 0, Obj->pCD);
  HeapFree(GetProcessHeap(), 0, Obj);
  return;
}

/************************************************************************
 * EnumConnectionsImpl_QueryInterface (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
static HRESULT WINAPI EnumConnectionsImpl_QueryInterface(
  IEnumConnections*  iface,
  REFIID  riid,
  void**  ppvObject)
{
422
  ConnectionPointImpl *This = (ConnectionPointImpl *)iface;
423 424 425 426 427 428 429
  TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppvObject);

  /*
   * Perform a sanity check on the parameters.
   */
  if ( (This==0) || (ppvObject==0) )
    return E_INVALIDARG;
430

431 432 433 434
  /*
   * Initialize the return parameter.
   */
  *ppvObject = 0;
435

436 437 438
  /*
   * Compare the riid with the interface IDs implemented by this object.
   */
439
  if (IsEqualIID(&IID_IUnknown, riid))
440
    *ppvObject = This;
441
  else if (IsEqualIID(&IID_IEnumConnections, riid))
442
    *ppvObject = This;
443

444 445 446 447 448 449 450 451
  /*
   * Check that we obtained an interface.
   */
  if ((*ppvObject)==0)
  {
    FIXME("() : asking for un supported interface %s\n",debugstr_guid(riid));
    return E_NOINTERFACE;
  }
452

453 454 455 456 457 458
  /*
   * Query Interface always increases the reference count by one when it is
   * successful
   */
  EnumConnectionsImpl_AddRef((IEnumConnections*)This);

459
  return S_OK;
460 461 462 463 464 465 466 467 468 469
}


/************************************************************************
 * EnumConnectionsImpl_AddRef (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
static ULONG WINAPI EnumConnectionsImpl_AddRef(IEnumConnections* iface)
{
470
  EnumConnectionsImpl *This = impl_from_IEnumConnections(iface);
471 472
  ULONG refCount = InterlockedIncrement(&This->ref);

473
  TRACE("(%p)->(ref before=%d)\n", This, refCount - 1);
474

475
  IUnknown_AddRef(This->pUnk);
476
  return refCount;
477
}
478

479 480 481 482 483 484 485
/************************************************************************
 * EnumConnectionsImpl_Release (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
static ULONG WINAPI EnumConnectionsImpl_Release(IEnumConnections* iface)
{
486
  EnumConnectionsImpl *This = impl_from_IEnumConnections(iface);
487
  ULONG refCount = InterlockedDecrement(&This->ref);
488

489
  TRACE("(%p)->(ref before=%d)\n", This, refCount + 1);
490

491
  IUnknown_Release(This->pUnk);
492 493 494 495

  /*
   * If the reference count goes down to 0, perform suicide.
   */
496
  if (!refCount) EnumConnectionsImpl_Destroy(This);
497

498
  return refCount;
499 500 501 502 503 504 505 506 507 508
}

/************************************************************************
 * EnumConnectionsImpl_Next (IEnumConnections)
 *
 */
static HRESULT WINAPI EnumConnectionsImpl_Next(IEnumConnections* iface,
					       ULONG cConn, LPCONNECTDATA pCD,
					       ULONG *pEnum)
{
509
  EnumConnectionsImpl *This = impl_from_IEnumConnections(iface);
510
  DWORD nRet = 0;
511
  TRACE("(%p)->(%d, %p, %p)\n", This, cConn, pCD, pEnum);
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543

  if(pEnum == NULL) {
    if(cConn != 1)
      return E_POINTER;
  } else
    *pEnum = 0;

  if(This->nCur >= This->nConns)
    return S_FALSE;

  while(This->nCur < This->nConns && cConn) {
    *pCD++ = This->pCD[This->nCur];
    IUnknown_AddRef(This->pCD[This->nCur].pUnk);
    This->nCur++;
    cConn--;
    nRet++;
  }

  if(pEnum)
    *pEnum = nRet;

  return S_OK;
}


/************************************************************************
 * EnumConnectionsImpl_Skip (IEnumConnections)
 *
 */
static HRESULT WINAPI EnumConnectionsImpl_Skip(IEnumConnections* iface,
					       ULONG cSkip)
{
544
  EnumConnectionsImpl *This = impl_from_IEnumConnections(iface);
545
  TRACE("(%p)->(%d)\n", This, cSkip);
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561

  if(This->nCur + cSkip >= This->nConns)
    return S_FALSE;

  This->nCur += cSkip;

  return S_OK;
}


/************************************************************************
 * EnumConnectionsImpl_Reset (IEnumConnections)
 *
 */
static HRESULT WINAPI EnumConnectionsImpl_Reset(IEnumConnections* iface)
{
562
  EnumConnectionsImpl *This = impl_from_IEnumConnections(iface);
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
  TRACE("(%p)\n", This);

  This->nCur = 0;

  return S_OK;
}


/************************************************************************
 * EnumConnectionsImpl_Clone (IEnumConnections)
 *
 */
static HRESULT WINAPI EnumConnectionsImpl_Clone(IEnumConnections* iface,
						LPENUMCONNECTIONS *ppEnum)
{
578
  EnumConnectionsImpl *This = impl_from_IEnumConnections(iface);
579 580
  EnumConnectionsImpl *newObj;
  TRACE("(%p)->(%p)\n", This, ppEnum);
581

582 583 584 585 586 587
  newObj = EnumConnectionsImpl_Construct(This->pUnk, This->nConns, This->pCD);
  newObj->nCur = This->nCur;
  *ppEnum = (LPENUMCONNECTIONS)newObj;
  IUnknown_AddRef(This->pUnk);
  return S_OK;
}
588

589
static const IEnumConnectionsVtbl EnumConnectionsImpl_VTable =
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
{
  EnumConnectionsImpl_QueryInterface,
  EnumConnectionsImpl_AddRef,
  EnumConnectionsImpl_Release,
  EnumConnectionsImpl_Next,
  EnumConnectionsImpl_Skip,
  EnumConnectionsImpl_Reset,
  EnumConnectionsImpl_Clone
};

/************************************************************************
 *
 *  The exported function to create the connection point.
 *  NB not a windows API
 *
 * PARAMS
 * pUnk [in] IUnknown of object to which the ConnectionPoint is associated.
 *           Needed to access IConnectionPointContainer.
 *
 * riid [in] IID of sink interface that this ConnectionPoint manages
 *
 * pCP [out] returns IConnectionPoint
 *
 */
HRESULT CreateConnectionPoint(IUnknown *pUnk, REFIID riid,
			      IConnectionPoint **pCP)
{
  ConnectionPointImpl *Obj;
  HRESULT hr;

  Obj = ConnectionPointImpl_Construct(pUnk, riid);
  if(!Obj) return E_OUTOFMEMORY;

623
  hr = IConnectionPoint_QueryInterface(&Obj->IConnectionPoint_iface,
624
                                       &IID_IConnectionPoint, (void**)pCP);
625
  IConnectionPoint_Release(&Obj->IConnectionPoint_iface);
626 627
  return hr;
}