defaulthandler.c 61.5 KB
Newer Older
1 2 3 4
/*
 *	OLE 2 default object handler
 *
 *      Copyright 1999  Francis Beaudet
5
 *      Copyright 2000  Abey George
6
 *
7 8 9 10 11 12 13 14 15 16 17 18
 * 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
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20
 *
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 * NOTES:
 *    The OLE2 default object handler supports a whole whack of
 *    interfaces including:
 *       IOleObject, IDataObject, IPersistStorage, IViewObject2,
 *       IRunnableObject, IOleCache2, IOleCacheControl and much more.
 *
 *    All the implementation details are taken from: Inside OLE
 *    second edition by Kraig Brockschmidt,
 *
 * TODO
 * - This implementation of the default handler does not launch the
 *   server in the DoVerb, Update, GetData, GetDataHere and Run
 *   methods. When it is fixed to do so, all the methods will have
 *   to be  revisited to allow delegating to the running object
 *
 * - All methods in the class that use the class ID should be
 *   aware that it is possible for a class to be treated as
 *   another one and go into emulation mode. Nothing has been
 *   done in this area.
 *
41
 * - Some functions still return E_NOTIMPL they have to be
42
 *   implemented. Most of those are related to the running of the
43 44
 *   actual server.
 *
45
 * - All the methods related to notification and advise sinks are
46 47 48
 *   in place but no notifications are sent to the sinks yet.
 */
#include <assert.h>
49
#include <stdarg.h>
50
#include <string.h>
51

52 53
#define COBJMACROS

54
#include "windef.h"
55
#include "winbase.h"
56
#include "winuser.h"
57 58
#include "winerror.h"
#include "ole2.h"
59 60 61 62

#include "compobj_private.h"

#include "wine/unicode.h"
63
#include "wine/debug.h"
64

65
WINE_DEFAULT_DEBUG_CHANNEL(ole);
66

67 68 69 70 71 72 73
enum storage_state
{
    storage_state_uninitialised,
    storage_state_initialised,
    storage_state_loaded
};

74 75 76 77 78 79
enum object_state
{
    object_state_not_running,
    object_state_running
};

80 81 82 83 84 85
/****************************************************************************
 * DefaultHandler
 *
 */
struct DefaultHandler
{
86 87 88 89 90 91
  IOleObject        IOleObject_iface;
  IUnknown          IUnknown_iface;
  IDataObject       IDataObject_iface;
  IRunnableObject   IRunnableObject_iface;
  IAdviseSink       IAdviseSink_iface;
  IPersistStorage   IPersistStorage_iface;
92

93
  /* Reference count of this object */
94
  LONG ref;
95

96
  /* IUnknown implementation of the outer object. */
97 98
  IUnknown* outerUnknown;

99
  /* Class Id that this handler object represents. */
100 101
  CLSID clsid;

102
  /* IUnknown implementation of the datacache. */
103
  IUnknown* dataCache;
104 105
  /* IPersistStorage implementation of the datacache. */
  IPersistStorage* dataCache_PersistStg;
106

107
  /* Client site for the embedded object. */
108 109 110 111 112 113 114 115 116
  IOleClientSite* clientSite;

  /*
   * The IOleAdviseHolder maintains the connections
   * on behalf of the default handler.
   */
  IOleAdviseHolder* oleAdviseHolder;

  /*
117
   * The IDataAdviseHolder maintains the data
118 119 120 121
   * connections on behalf of the default handler.
   */
  IDataAdviseHolder* dataAdviseHolder;

122
  /* Name of the container and object contained */
123
  LPWSTR containerApp;
124
  LPWSTR containerObj;
125

126 127 128 129
  /* IOleObject delegate */
  IOleObject *pOleDelegate;
  /* IPersistStorage delegate */
  IPersistStorage *pPSDelegate;
130 131
  /* IDataObject delegate */
  IDataObject *pDataDelegate;
132
  enum object_state object_state;
133 134 135

  /* connection cookie for the advise on the delegate OLE object */
  DWORD dwAdvConn;
136 137 138 139

  /* storage passed to Load or InitNew */
  IStorage *storage;
  enum storage_state storage_state;
140 141 142 143 144

  /* optional class factory for object */
  IClassFactory *pCFObject;
  /* TRUE if acting as an inproc server instead of an inproc handler */
  BOOL inproc_server;
145 146 147 148
};

typedef struct DefaultHandler DefaultHandler;

149 150
static inline DefaultHandler *impl_from_IOleObject( IOleObject *iface )
{
151
    return CONTAINING_RECORD(iface, DefaultHandler, IOleObject_iface);
152 153
}

154
static inline DefaultHandler *impl_from_IUnknown( IUnknown *iface )
155
{
156
    return CONTAINING_RECORD(iface, DefaultHandler, IUnknown_iface);
157 158 159 160
}

static inline DefaultHandler *impl_from_IDataObject( IDataObject *iface )
{
161
    return CONTAINING_RECORD(iface, DefaultHandler, IDataObject_iface);
162 163 164 165
}

static inline DefaultHandler *impl_from_IRunnableObject( IRunnableObject *iface )
{
166
    return CONTAINING_RECORD(iface, DefaultHandler, IRunnableObject_iface);
167 168
}

169 170
static inline DefaultHandler *impl_from_IAdviseSink( IAdviseSink *iface )
{
171
    return CONTAINING_RECORD(iface, DefaultHandler, IAdviseSink_iface);
172 173
}

174 175
static inline DefaultHandler *impl_from_IPersistStorage( IPersistStorage *iface )
{
176
    return CONTAINING_RECORD(iface, DefaultHandler, IPersistStorage_iface);
177 178
}

179
static void DefaultHandler_Destroy(DefaultHandler* This);
180

181 182
static inline BOOL object_is_running(DefaultHandler *This)
{
183
    return IRunnableObject_IsRunning(&This->IRunnableObject_iface);
184
}
185 186 187 188 189 190 191 192 193 194 195

/*********************************************************
 * Method implementation for the  non delegating IUnknown
 * part of the DefaultHandler class.
 */

/************************************************************************
 * DefaultHandler_NDIUnknown_QueryInterface (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 *
Huw Davies's avatar
Huw Davies committed
196
 * This version of QueryInterface will not delegate its implementation
197 198 199 200 201 202 203
 * to the outer unknown.
 */
static HRESULT WINAPI DefaultHandler_NDIUnknown_QueryInterface(
            IUnknown*      iface,
            REFIID         riid,
            void**         ppvObject)
{
204
  DefaultHandler *This = impl_from_IUnknown(iface);
205

206
  if (!ppvObject)
207
    return E_INVALIDARG;
208

209
  *ppvObject = NULL;
210

211
  if (IsEqualIID(&IID_IUnknown, riid))
212
    *ppvObject = iface;
213
  else if (IsEqualIID(&IID_IOleObject, riid))
214
    *ppvObject = &This->IOleObject_iface;
215
  else if (IsEqualIID(&IID_IDataObject, riid))
216
    *ppvObject = &This->IDataObject_iface;
217
  else if (IsEqualIID(&IID_IRunnableObject, riid))
218
    *ppvObject = &This->IRunnableObject_iface;
219
  else if (IsEqualIID(&IID_IPersist, riid) ||
220
           IsEqualIID(&IID_IPersistStorage, riid))
221
    *ppvObject = &This->IPersistStorage_iface;
222
  else if (IsEqualIID(&IID_IViewObject, riid) ||
223 224 225
           IsEqualIID(&IID_IViewObject2, riid) ||
           IsEqualIID(&IID_IOleCache, riid) ||
           IsEqualIID(&IID_IOleCache2, riid))
226
  {
227 228 229
    HRESULT hr = IUnknown_QueryInterface(This->dataCache, riid, ppvObject);
    if (FAILED(hr)) FIXME("interface %s not implemented by data cache\n", debugstr_guid(riid));
    return hr;
230
  }
231 232
  else if (This->inproc_server && This->pOleDelegate)
  {
233
    return IUnknown_QueryInterface(This->pOleDelegate, riid, ppvObject);
234
  }
235

236 237
  /* Check that we obtained an interface. */
  if (*ppvObject == NULL)
238
  {
239
    WARN( "() : asking for un supported interface %s\n", debugstr_guid(riid));
240 241
    return E_NOINTERFACE;
  }
242

243 244
  /*
   * Query Interface always increases the reference count by one when it is
245
   * successful.
246 247 248
   */
  IUnknown_AddRef((IUnknown*)*ppvObject);

249
  return S_OK;
250 251 252 253 254 255 256
}

/************************************************************************
 * DefaultHandler_NDIUnknown_AddRef (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 *
Huw Davies's avatar
Huw Davies committed
257
 * This version of QueryInterface will not delegate its implementation
258 259
 * to the outer unknown.
 */
260
static ULONG WINAPI DefaultHandler_NDIUnknown_AddRef(
261 262
            IUnknown*      iface)
{
263
  DefaultHandler *This = impl_from_IUnknown(iface);
264
  return InterlockedIncrement(&This->ref);
265 266 267 268 269 270 271
}

/************************************************************************
 * DefaultHandler_NDIUnknown_Release (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 *
Huw Davies's avatar
Huw Davies committed
272
 * This version of QueryInterface will not delegate its implementation
273 274
 * to the outer unknown.
 */
275
static ULONG WINAPI DefaultHandler_NDIUnknown_Release(
276 277
            IUnknown*      iface)
{
278
  DefaultHandler *This = impl_from_IUnknown(iface);
279
  ULONG ref;
280

281
  ref = InterlockedDecrement(&This->ref);
282

283
  if (!ref) DefaultHandler_Destroy(This);
284

285
  return ref;
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
}

/*********************************************************
 * Methods implementation for the IOleObject part of
 * the DefaultHandler class.
 */

/************************************************************************
 * DefaultHandler_QueryInterface (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
static HRESULT WINAPI DefaultHandler_QueryInterface(
            IOleObject*      iface,
            REFIID           riid,
            void**           ppvObject)
{
303
  DefaultHandler *This = impl_from_IOleObject(iface);
304

305
  return IUnknown_QueryInterface(This->outerUnknown, riid, ppvObject);
306 307 308 309 310 311 312
}

/************************************************************************
 * DefaultHandler_AddRef (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
313
static ULONG WINAPI DefaultHandler_AddRef(
314 315
            IOleObject*        iface)
{
316
  DefaultHandler *This = impl_from_IOleObject(iface);
317

318
  return IUnknown_AddRef(This->outerUnknown);
319 320 321 322 323 324 325
}

/************************************************************************
 * DefaultHandler_Release (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
326
static ULONG WINAPI DefaultHandler_Release(
327 328
            IOleObject*        iface)
{
329
  DefaultHandler *This = impl_from_IOleObject(iface);
330

331
  return IUnknown_Release(This->outerUnknown);
332 333 334 335 336 337 338 339 340 341 342 343 344 345
}

/************************************************************************
 * DefaultHandler_SetClientSite (IOleObject)
 *
 * The default handler's implementation of this method only keeps the
 * client site pointer for future reference.
 *
 * See Windows documentation for more details on IOleObject methods.
 */
static HRESULT WINAPI DefaultHandler_SetClientSite(
	    IOleObject*        iface,
	    IOleClientSite*    pClientSite)
{
346
  DefaultHandler *This = impl_from_IOleObject(iface);
347
  HRESULT hr = S_OK;
348

349
  TRACE("(%p, %p)\n", iface, pClientSite);
350

351
  if (object_is_running(This))
352 353
    hr = IOleObject_SetClientSite(This->pOleDelegate, pClientSite);

354 355 356 357
  /*
   * Make sure we release the previous client site if there
   * was one.
   */
358 359
  if (This->clientSite)
    IOleClientSite_Release(This->clientSite);
360

361
  This->clientSite = pClientSite;
362

363 364
  if (This->clientSite)
    IOleClientSite_AddRef(This->clientSite);
365

366
  return hr;
367 368 369 370 371 372 373 374 375 376 377 378 379 380
}

/************************************************************************
 * DefaultHandler_GetClientSite (IOleObject)
 *
 * The default handler's implementation of this method returns the
 * last pointer set in IOleObject_SetClientSite.
 *
 * See Windows documentation for more details on IOleObject methods.
 */
static HRESULT WINAPI DefaultHandler_GetClientSite(
	    IOleObject*        iface,
	    IOleClientSite**   ppClientSite)
{
381
  DefaultHandler *This = impl_from_IOleObject(iface);
382

383
  if (!ppClientSite)
384 385
    return E_POINTER;

386
  *ppClientSite = This->clientSite;
387

388 389
  if (This->clientSite)
    IOleClientSite_AddRef(This->clientSite);
390 391 392 393 394 395 396 397 398 399 400 401 402 403

  return S_OK;
}

/************************************************************************
 * DefaultHandler_SetHostNames (IOleObject)
 *
 * The default handler's implementation of this method just stores
 * the strings and returns S_OK.
 *
 * See Windows documentation for more details on IOleObject methods.
 */
static HRESULT WINAPI DefaultHandler_SetHostNames(
	    IOleObject*        iface,
404
	    LPCOLESTR          szContainerApp,
405 406
	    LPCOLESTR          szContainerObj)
{
407
  DefaultHandler *This = impl_from_IOleObject(iface);
408

409
  TRACE("(%p, %s, %s)\n",
410
	iface,
411
	debugstr_w(szContainerApp),
412 413
	debugstr_w(szContainerObj));

414
  if (object_is_running(This))
415 416
    IOleObject_SetHostNames(This->pOleDelegate, szContainerApp, szContainerObj);

Austin English's avatar
Austin English committed
417
  /* Be sure to cleanup before re-assigning the strings. */
418 419 420 421
  HeapFree( GetProcessHeap(), 0, This->containerApp );
  This->containerApp = NULL;
  HeapFree( GetProcessHeap(), 0, This->containerObj );
  This->containerObj = NULL;
422

423
  if (szContainerApp)
424
  {
425
      if ((This->containerApp = HeapAlloc( GetProcessHeap(), 0,
426
                                           (lstrlenW(szContainerApp) + 1) * sizeof(WCHAR) )))
427
          strcpyW( This->containerApp, szContainerApp );
428
  }
429

430
  if (szContainerObj)
431
  {
432
      if ((This->containerObj = HeapAlloc( GetProcessHeap(), 0,
433
                                           (lstrlenW(szContainerObj) + 1) * sizeof(WCHAR) )))
434
          strcpyW( This->containerObj, szContainerObj );
435
  }
436 437 438
  return S_OK;
}

439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
static void release_delegates(DefaultHandler *This)
{
    if (This->pDataDelegate)
    {
        IDataObject_Release(This->pDataDelegate);
        This->pDataDelegate = NULL;
    }
    if (This->pPSDelegate)
    {
        IPersistStorage_Release(This->pPSDelegate);
        This->pPSDelegate = NULL;
    }
    if (This->pOleDelegate)
    {
        IOleObject_Release(This->pOleDelegate);
        This->pOleDelegate = NULL;
    }
}

458 459
/* undoes the work done by DefaultHandler_Run */
static void DefaultHandler_Stop(DefaultHandler *This)
460
{
461
  if (!object_is_running(This))
462 463 464 465 466 467
    return;

  IOleObject_Unadvise(This->pOleDelegate, This->dwAdvConn);

  /* FIXME: call IOleCache_OnStop */

468 469
  if (This->dataAdviseHolder)
    DataAdviseHolder_OnDisconnect(This->dataAdviseHolder);
470

471
  This->object_state = object_state_not_running;
472 473
}

474 475 476 477 478 479 480 481 482
/************************************************************************
 * DefaultHandler_Close (IOleObject)
 *
 * The default handler's implementation of this method is meaningless
 * without a running server so it does nothing.
 *
 * See Windows documentation for more details on IOleObject methods.
 */
static HRESULT WINAPI DefaultHandler_Close(
483
	    IOleObject*        iface,
484 485
	    DWORD              dwSaveOption)
{
486 487 488
  DefaultHandler *This = impl_from_IOleObject(iface);
  HRESULT hr;

489
  TRACE("(%d)\n", dwSaveOption);
490

491
  if (!object_is_running(This))
492 493 494 495 496
    return S_OK;

  hr = IOleObject_Close(This->pOleDelegate, dwSaveOption);

  DefaultHandler_Stop(This);
497
  release_delegates(This);
498 499

  return hr;
500 501 502 503 504 505 506 507 508 509
}

/************************************************************************
 * DefaultHandler_SetMoniker (IOleObject)
 *
 * The default handler's implementation of this method does nothing.
 *
 * See Windows documentation for more details on IOleObject methods.
 */
static HRESULT WINAPI DefaultHandler_SetMoniker(
510
	    IOleObject*        iface,
511 512 513
	    DWORD              dwWhichMoniker,
	    IMoniker*          pmk)
{
514 515
  DefaultHandler *This = impl_from_IOleObject(iface);

516
  TRACE("(%p, %d, %p)\n",
517 518
	iface,
	dwWhichMoniker,
519 520
	pmk);

521
  if (object_is_running(This))
522 523
    return IOleObject_SetMoniker(This->pOleDelegate, dwWhichMoniker, pmk);

524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
  return S_OK;
}

/************************************************************************
 * DefaultHandler_GetMoniker (IOleObject)
 *
 * Delegate this request to the client site if we have one.
 *
 * See Windows documentation for more details on IOleObject methods.
 */
static HRESULT WINAPI DefaultHandler_GetMoniker(
	    IOleObject*        iface,
	    DWORD              dwAssign,
	    DWORD              dwWhichMoniker,
	    IMoniker**         ppmk)
{
540
  DefaultHandler *This = impl_from_IOleObject(iface);
541

542
  TRACE("(%p, %d, %d, %p)\n",
543 544
	iface, dwAssign, dwWhichMoniker, ppmk);

545
  if (object_is_running(This))
546 547 548 549
    return IOleObject_GetMoniker(This->pOleDelegate, dwAssign, dwWhichMoniker,
                                 ppmk);

  /* FIXME: dwWhichMoniker == OLEWHICHMK_CONTAINER only? */
550
  if (This->clientSite)
551
  {
552
    return IOleClientSite_GetMoniker(This->clientSite,
553 554 555
				     dwAssign,
				     dwWhichMoniker,
				     ppmk);
556

557 558
  }

559
  return E_FAIL;
560 561 562 563 564 565
}

/************************************************************************
 * DefaultHandler_InitFromData (IOleObject)
 *
 * This method is meaningless if the server is not running
566
 *
567 568 569
 * See Windows documentation for more details on IOleObject methods.
 */
static HRESULT WINAPI DefaultHandler_InitFromData(
570 571
	    IOleObject*        iface,
	    IDataObject*       pDataObject,
572 573 574
	    BOOL               fCreation,
	    DWORD              dwReserved)
{
575 576
  DefaultHandler *This = impl_from_IOleObject(iface);

577
  TRACE("(%p, %p, %d, %d)\n",
578 579
	iface, pDataObject, fCreation, dwReserved);

580
  if (object_is_running(This))
581 582
    return IOleObject_InitFromData(This->pOleDelegate, pDataObject, fCreation,
		                   dwReserved);
583 584 585 586 587 588 589
  return OLE_E_NOTRUNNING;
}

/************************************************************************
 * DefaultHandler_GetClipboardData (IOleObject)
 *
 * This method is meaningless if the server is not running
590
 *
591 592 593
 * See Windows documentation for more details on IOleObject methods.
 */
static HRESULT WINAPI DefaultHandler_GetClipboardData(
594 595
	    IOleObject*        iface,
	    DWORD              dwReserved,
596 597
	    IDataObject**      ppDataObject)
{
598 599
  DefaultHandler *This = impl_from_IOleObject(iface);

600
  TRACE("(%p, %d, %p)\n",
601 602
	iface, dwReserved, ppDataObject);

603
  if (object_is_running(This))
604 605 606
    return IOleObject_GetClipboardData(This->pOleDelegate, dwReserved,
                                       ppDataObject);

607 608 609 610
  return OLE_E_NOTRUNNING;
}

static HRESULT WINAPI DefaultHandler_DoVerb(
611 612 613 614 615 616
	    IOleObject*        iface,
	    LONG               iVerb,
	    struct tagMSG*     lpmsg,
	    IOleClientSite*    pActiveSite,
	    LONG               lindex,
	    HWND               hwndParent,
617 618
	    LPCRECT            lprcPosRect)
{
619
  DefaultHandler *This = impl_from_IOleObject(iface);
620
  IRunnableObject *pRunnableObj = &This->IRunnableObject_iface;
621 622
  HRESULT hr;

623
  TRACE("(%d, %p, %p, %d, %p, %s)\n", iVerb, lpmsg, pActiveSite, lindex, hwndParent, wine_dbgstr_rect(lprcPosRect));
624 625 626 627 628 629

  hr = IRunnableObject_Run(pRunnableObj, NULL);
  if (FAILED(hr)) return hr;

  return IOleObject_DoVerb(This->pOleDelegate, iVerb, lpmsg, pActiveSite,
                           lindex, hwndParent, lprcPosRect);
630 631 632 633 634 635 636
}

/************************************************************************
 * DefaultHandler_EnumVerbs (IOleObject)
 *
 * The default handler implementation of this method simply delegates
 * to OleRegEnumVerbs
637
 *
638 639 640
 * See Windows documentation for more details on IOleObject methods.
 */
static HRESULT WINAPI DefaultHandler_EnumVerbs(
641
	    IOleObject*        iface,
642 643
	    IEnumOLEVERB**     ppEnumOleVerb)
{
644
  DefaultHandler *This = impl_from_IOleObject(iface);
645
  HRESULT hr = OLE_S_USEREG;
646

647
  TRACE("(%p, %p)\n", iface, ppEnumOleVerb);
648

649
  if (object_is_running(This))
650 651 652 653 654 655
    hr = IOleObject_EnumVerbs(This->pOleDelegate, ppEnumOleVerb);

  if (hr == OLE_S_USEREG)
    return OleRegEnumVerbs(&This->clsid, ppEnumOleVerb);
  else
    return hr;
656 657 658 659 660
}

static HRESULT WINAPI DefaultHandler_Update(
	    IOleObject*        iface)
{
661 662 663 664 665 666 667 668 669
    DefaultHandler *This = impl_from_IOleObject(iface);
    TRACE("(%p)\n", iface);

    if (!object_is_running(This))
    {
        FIXME("Should run object\n");
        return E_NOTIMPL;
    }
    return IOleObject_Update(This->pOleDelegate);
670 671 672 673 674 675
}

/************************************************************************
 * DefaultHandler_IsUpToDate (IOleObject)
 *
 * This method is meaningless if the server is not running
676
 *
677 678 679 680 681
 * See Windows documentation for more details on IOleObject methods.
 */
static HRESULT WINAPI DefaultHandler_IsUpToDate(
	    IOleObject*        iface)
{
682 683
    DefaultHandler *This = impl_from_IOleObject(iface);
    TRACE("(%p)\n", iface);
684

685 686 687 688
    if (object_is_running(This))
        return IOleObject_IsUpToDate(This->pOleDelegate);

    return OLE_E_NOTRUNNING;
689 690 691 692 693 694
}

/************************************************************************
 * DefaultHandler_GetUserClassID (IOleObject)
 *
 * TODO: Map to a new class ID if emulation is active.
695
 *
696 697 698
 * See Windows documentation for more details on IOleObject methods.
 */
static HRESULT WINAPI DefaultHandler_GetUserClassID(
699
	    IOleObject*        iface,
700 701
	    CLSID*             pClsid)
{
702
  DefaultHandler *This = impl_from_IOleObject(iface);
703

704
  TRACE("(%p, %p)\n", iface, pClsid);
705

706
  if (object_is_running(This))
707 708
    return IOleObject_GetUserClassID(This->pOleDelegate, pClsid);

709
  if (!pClsid)
710 711
    return E_POINTER;

712
  *pClsid = This->clsid;
713 714 715 716 717 718 719 720 721

  return S_OK;
}

/************************************************************************
 * DefaultHandler_GetUserType (IOleObject)
 *
 * The default handler implementation of this method simply delegates
 * to OleRegGetUserType
722
 *
723 724 725
 * See Windows documentation for more details on IOleObject methods.
 */
static HRESULT WINAPI DefaultHandler_GetUserType(
726 727
	    IOleObject*        iface,
	    DWORD              dwFormOfType,
728 729
	    LPOLESTR*          pszUserType)
{
730
  DefaultHandler *This = impl_from_IOleObject(iface);
731

732
  TRACE("(%p, %d, %p)\n", iface, dwFormOfType, pszUserType);
733 734
  if (object_is_running(This))
    return IOleObject_GetUserType(This->pOleDelegate, dwFormOfType, pszUserType);
735

736
  return OleRegGetUserType(&This->clsid, dwFormOfType, pszUserType);
737 738 739 740 741 742 743 744 745 746
}

/************************************************************************
 * DefaultHandler_SetExtent (IOleObject)
 *
 * This method is meaningless if the server is not running
 *
 * See Windows documentation for more details on IOleObject methods.
 */
static HRESULT WINAPI DefaultHandler_SetExtent(
747 748
	    IOleObject*        iface,
	    DWORD              dwDrawAspect,
749 750
	    SIZEL*             psizel)
{
751 752
  DefaultHandler *This = impl_from_IOleObject(iface);

753
  TRACE("(%p, %x, (%d x %d))\n", iface,
Gerald Pfeifer's avatar
Gerald Pfeifer committed
754
        dwDrawAspect, psizel->cx, psizel->cy);
755

756
  if (object_is_running(This))
757
    return IOleObject_SetExtent(This->pOleDelegate, dwDrawAspect, psizel);
758

759 760 761 762 763 764 765 766 767 768 769 770
  return OLE_E_NOTRUNNING;
}

/************************************************************************
 * DefaultHandler_GetExtent (IOleObject)
 *
 * The default handler's implementation of this method returns uses
 * the cache to locate the aspect and extract the extent from it.
 *
 * See Windows documentation for more details on IOleObject methods.
 */
static HRESULT WINAPI DefaultHandler_GetExtent(
771 772
	    IOleObject*        iface,
	    DWORD              dwDrawAspect,
773 774 775 776 777 778
	    SIZEL*             psizel)
{
  DVTARGETDEVICE* targetDevice;
  IViewObject2*   cacheView = NULL;
  HRESULT         hres;

779
  DefaultHandler *This = impl_from_IOleObject(iface);
780

781
  TRACE("(%p, %x, %p)\n", iface, dwDrawAspect, psizel);
782

783
  if (object_is_running(This))
784
    return IOleObject_GetExtent(This->pOleDelegate, dwDrawAspect, psizel);
785

786
  hres = IUnknown_QueryInterface(This->dataCache, &IID_IViewObject2, (void**)&cacheView);
787 788 789 790 791 792 793
  if (FAILED(hres))
    return E_UNEXPECTED;

  /*
   * Prepare the call to the cache's GetExtent method.
   *
   * Here we would build a valid DVTARGETDEVICE structure
794
   * but, since we are calling into the data cache, we
Huw Davies's avatar
Huw Davies committed
795
   * know its implementation and we'll skip this
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
   * extra work until later.
   */
  targetDevice = NULL;

  hres = IViewObject2_GetExtent(cacheView,
				dwDrawAspect,
				-1,
				targetDevice,
				psizel);

  IViewObject2_Release(cacheView);

  return hres;
}

/************************************************************************
 * DefaultHandler_Advise (IOleObject)
 *
 * The default handler's implementation of this method simply
 * delegates to the OleAdviseHolder.
 *
 * See Windows documentation for more details on IOleObject methods.
 */
static HRESULT WINAPI DefaultHandler_Advise(
820 821
	    IOleObject*        iface,
	    IAdviseSink*       pAdvSink,
822 823 824
	    DWORD*             pdwConnection)
{
  HRESULT hres = S_OK;
825
  DefaultHandler *This = impl_from_IOleObject(iface);
826

827
  TRACE("(%p, %p, %p)\n", iface, pAdvSink, pdwConnection);
828

829 830 831
  /* Make sure we have an advise holder before we start. */
  if (!This->oleAdviseHolder)
    hres = CreateOleAdviseHolder(&This->oleAdviseHolder);
832 833

  if (SUCCEEDED(hres))
834
    hres = IOleAdviseHolder_Advise(This->oleAdviseHolder,
835
				   pAdvSink,
836 837 838 839 840 841 842 843 844 845 846 847 848 849
				   pdwConnection);

  return hres;
}

/************************************************************************
 * DefaultHandler_Unadvise (IOleObject)
 *
 * The default handler's implementation of this method simply
 * delegates to the OleAdviseHolder.
 *
 * See Windows documentation for more details on IOleObject methods.
 */
static HRESULT WINAPI DefaultHandler_Unadvise(
850
	    IOleObject*        iface,
851 852
	    DWORD              dwConnection)
{
853
  DefaultHandler *This = impl_from_IOleObject(iface);
854

855
  TRACE("(%p, %d)\n", iface, dwConnection);
856 857 858 859 860

  /*
   * If we don't have an advise holder yet, it means we don't have
   * a connection.
   */
861
  if (!This->oleAdviseHolder)
862 863
    return OLE_E_NOCONNECTION;

864
  return IOleAdviseHolder_Unadvise(This->oleAdviseHolder,
865 866 867 868 869 870 871 872 873 874 875 876
				   dwConnection);
}

/************************************************************************
 * DefaultHandler_EnumAdvise (IOleObject)
 *
 * The default handler's implementation of this method simply
 * delegates to the OleAdviseHolder.
 *
 * See Windows documentation for more details on IOleObject methods.
 */
static HRESULT WINAPI DefaultHandler_EnumAdvise(
877
	    IOleObject*        iface,
878 879
	    IEnumSTATDATA**    ppenumAdvise)
{
880
  DefaultHandler *This = impl_from_IOleObject(iface);
881

882
  TRACE("(%p, %p)\n", iface, ppenumAdvise);
883

884
  if (!ppenumAdvise)
885 886 887 888
    return E_POINTER;

  *ppenumAdvise = NULL;

889
  if (!This->oleAdviseHolder)
890
      return S_OK;
891

892
  return IOleAdviseHolder_EnumAdvise(This->oleAdviseHolder, ppenumAdvise);
893 894 895 896 897 898 899 900 901 902 903
}

/************************************************************************
 * DefaultHandler_GetMiscStatus (IOleObject)
 *
 * The default handler's implementation of this method simply delegates
 * to OleRegGetMiscStatus.
 *
 * See Windows documentation for more details on IOleObject methods.
 */
static HRESULT WINAPI DefaultHandler_GetMiscStatus(
904 905
	    IOleObject*        iface,
	    DWORD              dwAspect,
906 907 908
	    DWORD*             pdwStatus)
{
  HRESULT hres;
909
  DefaultHandler *This = impl_from_IOleObject(iface);
910

911
  TRACE("(%p, %x, %p)\n", iface, dwAspect, pdwStatus);
912

913
  if (object_is_running(This))
914 915
    return IOleObject_GetMiscStatus(This->pOleDelegate, dwAspect, pdwStatus);

916
  hres = OleRegGetMiscStatus(&This->clsid, dwAspect, pdwStatus);
917 918 919 920

  if (FAILED(hres))
    *pdwStatus = 0;

921
  return hres;
922 923 924
}

/************************************************************************
925
 * DefaultHandler_SetColorScheme (IOleObject)
926 927 928 929 930 931
 *
 * This method is meaningless if the server is not running
 *
 * See Windows documentation for more details on IOleObject methods.
 */
static HRESULT WINAPI DefaultHandler_SetColorScheme(
932 933
	    IOleObject*           iface,
	    struct tagLOGPALETTE* pLogpal)
934
{
935 936
  DefaultHandler *This = impl_from_IOleObject(iface);

937
  TRACE("(%p, %p))\n", iface, pLogpal);
938

939
  if (object_is_running(This))
940 941
    return IOleObject_SetColorScheme(This->pOleDelegate, pLogpal);

942 943 944 945 946 947 948 949 950 951 952 953 954 955
  return OLE_E_NOTRUNNING;
}

/*********************************************************
 * Methods implementation for the IDataObject part of
 * the DefaultHandler class.
 */

/************************************************************************
 * DefaultHandler_IDataObject_QueryInterface (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
static HRESULT WINAPI DefaultHandler_IDataObject_QueryInterface(
956
            IDataObject*     iface,
957 958 959
           REFIID           riid,
            void**           ppvObject)
{
960
  DefaultHandler *This = impl_from_IDataObject(iface);
961

962
  return IUnknown_QueryInterface(This->outerUnknown, riid, ppvObject);
963 964 965 966 967 968 969
}

/************************************************************************
 * DefaultHandler_IDataObject_AddRef (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
970
static ULONG WINAPI DefaultHandler_IDataObject_AddRef(
971 972
            IDataObject*     iface)
{
973
  DefaultHandler *This = impl_from_IDataObject(iface);
974

975
  return IUnknown_AddRef(This->outerUnknown);
976 977 978 979 980 981 982
}

/************************************************************************
 * DefaultHandler_IDataObject_Release (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
983
static ULONG WINAPI DefaultHandler_IDataObject_Release(
984 985
            IDataObject*     iface)
{
986
  DefaultHandler *This = impl_from_IDataObject(iface);
987

988
  return IUnknown_Release(This->outerUnknown);
989 990
}

991 992 993 994 995 996 997
/************************************************************************
 * DefaultHandler_GetData
 *
 * Get Data from a source dataobject using format pformatetcIn->cfFormat
 * See Windows documentation for more details on GetData.
 * Default handler's implementation of this method delegates to the cache.
 */
998 999
static HRESULT WINAPI DefaultHandler_GetData(
	    IDataObject*     iface,
1000
	    LPFORMATETC      pformatetcIn,
1001 1002
	    STGMEDIUM*       pmedium)
{
1003 1004 1005
  IDataObject* cacheDataObject = NULL;
  HRESULT      hres;

1006
  DefaultHandler *This = impl_from_IDataObject(iface);
1007 1008 1009

  TRACE("(%p, %p, %p)\n", iface, pformatetcIn, pmedium);

1010
  hres = IUnknown_QueryInterface(This->dataCache,
1011
				 &IID_IDataObject,
1012
				 (void**)&cacheDataObject);
1013 1014 1015 1016 1017 1018 1019

  if (FAILED(hres))
    return E_UNEXPECTED;

  hres = IDataObject_GetData(cacheDataObject,
			     pformatetcIn,
			     pmedium);
1020

1021
  IDataObject_Release(cacheDataObject);
1022

1023 1024 1025
  if (FAILED(hres) && This->pDataDelegate)
    hres = IDataObject_GetData(This->pDataDelegate, pformatetcIn, pmedium);

1026
  return hres;
1027 1028 1029
}

static HRESULT WINAPI DefaultHandler_GetDataHere(
1030
	    IDataObject*     iface,
1031 1032 1033
	    LPFORMATETC      pformatetc,
	    STGMEDIUM*       pmedium)
{
1034
  FIXME(": Stub\n");
1035 1036 1037 1038 1039 1040
  return E_NOTIMPL;
}

/************************************************************************
 * DefaultHandler_QueryGetData (IDataObject)
 *
1041
 * The default handler's implementation of this method delegates to
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
 * the cache.
 *
 * See Windows documentation for more details on IDataObject methods.
 */
static HRESULT WINAPI DefaultHandler_QueryGetData(
	    IDataObject*     iface,
	    LPFORMATETC      pformatetc)
{
  IDataObject* cacheDataObject = NULL;
  HRESULT      hres;

1053
  DefaultHandler *This = impl_from_IDataObject(iface);
1054

1055
  TRACE("(%p, %p)\n", iface, pformatetc);
1056

1057
  hres = IUnknown_QueryInterface(This->dataCache,
1058
				 &IID_IDataObject,
1059
				 (void**)&cacheDataObject);
1060 1061 1062 1063 1064 1065 1066 1067

  if (FAILED(hres))
    return E_UNEXPECTED;

  hres = IDataObject_QueryGetData(cacheDataObject,
				  pformatetc);

  IDataObject_Release(cacheDataObject);
1068

1069 1070 1071
  if (FAILED(hres) && This->pDataDelegate)
    hres = IDataObject_QueryGetData(This->pDataDelegate, pformatetc);

1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
  return hres;
}

/************************************************************************
 * DefaultHandler_GetCanonicalFormatEtc (IDataObject)
 *
 * This method is meaningless if the server is not running
 *
 * See Windows documentation for more details on IDataObject methods.
 */
static HRESULT WINAPI DefaultHandler_GetCanonicalFormatEtc(
1083
	    IDataObject*     iface,
1084
	    LPFORMATETC      pformatetcIn,
1085 1086
	    LPFORMATETC      pformatetcOut)
{
1087
  DefaultHandler *This = impl_from_IDataObject(iface);
1088

1089 1090
  TRACE("(%p, %p, %p)\n", iface, pformatetcIn, pformatetcOut);

1091
  if (!This->pDataDelegate)
1092 1093
    return OLE_E_NOTRUNNING;

1094
  return IDataObject_GetCanonicalFormatEtc(This->pDataDelegate, pformatetcIn, pformatetcOut);
1095 1096 1097 1098 1099
}

/************************************************************************
 * DefaultHandler_SetData (IDataObject)
 *
1100
 * The default handler's implementation of this method delegates to
1101 1102 1103 1104 1105 1106
 * the cache.
 *
 * See Windows documentation for more details on IDataObject methods.
 */
static HRESULT WINAPI DefaultHandler_SetData(
	    IDataObject*     iface,
1107 1108
	    LPFORMATETC      pformatetc,
	    STGMEDIUM*       pmedium,
1109 1110
	    BOOL             fRelease)
{
1111
  DefaultHandler *This = impl_from_IDataObject(iface);
1112 1113 1114
  IDataObject* cacheDataObject = NULL;
  HRESULT      hres;

1115
  TRACE("(%p, %p, %p, %d)\n", iface, pformatetc, pmedium, fRelease);
1116

1117
  hres = IUnknown_QueryInterface(This->dataCache,
1118
				 &IID_IDataObject,
1119
				 (void**)&cacheDataObject);
1120 1121 1122 1123 1124 1125 1126 1127

  if (FAILED(hres))
    return E_UNEXPECTED;

  hres = IDataObject_SetData(cacheDataObject,
			     pformatetc,
			     pmedium,
			     fRelease);
1128

1129
  IDataObject_Release(cacheDataObject);
1130

1131 1132 1133 1134 1135 1136
  return hres;
}

/************************************************************************
 * DefaultHandler_EnumFormatEtc (IDataObject)
 *
1137
 * The default handler's implementation of This method simply delegates
1138 1139 1140 1141 1142
 * to OleRegEnumFormatEtc.
 *
 * See Windows documentation for more details on IDataObject methods.
 */
static HRESULT WINAPI DefaultHandler_EnumFormatEtc(
1143
	    IDataObject*     iface,
1144 1145 1146
	    DWORD            dwDirection,
	    IEnumFORMATETC** ppenumFormatEtc)
{
1147
  DefaultHandler *This = impl_from_IDataObject(iface);
1148

1149
  TRACE("(%p, %x, %p)\n", iface, dwDirection, ppenumFormatEtc);
1150

1151
  return OleRegEnumFormatEtc(&This->clsid, dwDirection, ppenumFormatEtc);
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
}

/************************************************************************
 * DefaultHandler_DAdvise (IDataObject)
 *
 * The default handler's implementation of this method simply
 * delegates to the DataAdviseHolder.
 *
 * See Windows documentation for more details on IDataObject methods.
 */
static HRESULT WINAPI DefaultHandler_DAdvise(
1163 1164 1165 1166
	    IDataObject*     iface,
	    FORMATETC*       pformatetc,
	    DWORD            advf,
	    IAdviseSink*     pAdvSink,
1167 1168 1169
	    DWORD*           pdwConnection)
{
  HRESULT hres = S_OK;
1170
  DefaultHandler *This = impl_from_IDataObject(iface);
1171

1172
  TRACE("(%p, %p, %d, %p, %p)\n",
1173 1174
	iface, pformatetc, advf, pAdvSink, pdwConnection);

1175 1176
  /* Make sure we have a data advise holder before we start. */
  if (!This->dataAdviseHolder)
1177
  {
1178
    hres = CreateDataAdviseHolder(&This->dataAdviseHolder);
1179 1180 1181
    if (SUCCEEDED(hres) && This->pDataDelegate)
      DataAdviseHolder_OnConnect(This->dataAdviseHolder, This->pDataDelegate);
  }
1182 1183

  if (SUCCEEDED(hres))
1184
    hres = IDataAdviseHolder_Advise(This->dataAdviseHolder,
1185
				    iface,
1186 1187 1188
				    pformatetc,
				    advf,
				    pAdvSink,
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
				    pdwConnection);

  return hres;
}

/************************************************************************
 * DefaultHandler_DUnadvise (IDataObject)
 *
 * The default handler's implementation of this method simply
 * delegates to the DataAdviseHolder.
 *
 * See Windows documentation for more details on IDataObject methods.
 */
static HRESULT WINAPI DefaultHandler_DUnadvise(
	    IDataObject*     iface,
	    DWORD            dwConnection)
{
1206
  DefaultHandler *This = impl_from_IDataObject(iface);
1207

1208
  TRACE("(%p, %d)\n", iface, dwConnection);
1209 1210 1211 1212 1213

  /*
   * If we don't have a data advise holder yet, it means that
   * we don't have any connections..
   */
1214
  if (!This->dataAdviseHolder)
1215 1216
    return OLE_E_NOCONNECTION;

1217
  return IDataAdviseHolder_Unadvise(This->dataAdviseHolder,
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
				    dwConnection);
}

/************************************************************************
 * DefaultHandler_EnumDAdvise (IDataObject)
 *
 * The default handler's implementation of this method simply
 * delegates to the DataAdviseHolder.
 *
 * See Windows documentation for more details on IDataObject methods.
 */
static HRESULT WINAPI DefaultHandler_EnumDAdvise(
	    IDataObject*     iface,
	    IEnumSTATDATA**  ppenumAdvise)
{
1233
  DefaultHandler *This = impl_from_IDataObject(iface);
1234

1235
  TRACE("(%p, %p)\n", iface, ppenumAdvise);
1236

1237
  if (!ppenumAdvise)
1238 1239 1240 1241
    return E_POINTER;

  *ppenumAdvise = NULL;

1242 1243 1244
  /* If we have a data advise holder object, delegate. */
  if (This->dataAdviseHolder)
    return IDataAdviseHolder_EnumAdvise(This->dataAdviseHolder,
1245 1246 1247 1248 1249 1250
					ppenumAdvise);

  return S_OK;
}

/*********************************************************
1251
 * Methods implementation for the IRunnableObject part
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
 * of the DefaultHandler class.
 */

/************************************************************************
 * DefaultHandler_IRunnableObject_QueryInterface (IUnknown)
 *
 * See Windows documentation for more details on IUnknown methods.
 */
static HRESULT WINAPI DefaultHandler_IRunnableObject_QueryInterface(
            IRunnableObject*     iface,
            REFIID               riid,
            void**               ppvObject)
{
1265
  DefaultHandler *This = impl_from_IRunnableObject(iface);
1266

1267
  return IUnknown_QueryInterface(This->outerUnknown, riid, ppvObject);
1268 1269 1270
}

/************************************************************************
Huw Davies's avatar
Huw Davies committed
1271
 * DefaultHandler_IRunnableObject_AddRef (IUnknown)
1272 1273 1274
 *
 * See Windows documentation for more details on IUnknown methods.
 */
1275
static ULONG WINAPI DefaultHandler_IRunnableObject_AddRef(
1276 1277
            IRunnableObject*     iface)
{
1278
  DefaultHandler *This = impl_from_IRunnableObject(iface);
1279

1280
  return IUnknown_AddRef(This->outerUnknown);
1281 1282 1283
}

/************************************************************************
Huw Davies's avatar
Huw Davies committed
1284
 * DefaultHandler_IRunnableObject_Release (IUnknown)
1285 1286 1287
 *
 * See Windows documentation for more details on IUnknown methods.
 */
1288
static ULONG WINAPI DefaultHandler_IRunnableObject_Release(
1289 1290
            IRunnableObject*     iface)
{
1291
  DefaultHandler *This = impl_from_IRunnableObject(iface);
1292

1293
  return IUnknown_Release(This->outerUnknown);
1294 1295 1296 1297 1298 1299 1300
}

/************************************************************************
 * DefaultHandler_GetRunningClass (IRunnableObject)
 *
 * See Windows documentation for more details on IRunnableObject methods.
 */
1301 1302
static HRESULT WINAPI DefaultHandler_GetRunningClass(
            IRunnableObject*     iface,
1303 1304
	    LPCLSID              lpClsid)
{
1305
  FIXME("()\n");
1306 1307 1308
  return S_OK;
}

1309
static HRESULT WINAPI DefaultHandler_Run(
1310 1311 1312
            IRunnableObject*     iface,
	    IBindCtx*            pbc)
{
1313 1314 1315 1316 1317 1318
  DefaultHandler *This = impl_from_IRunnableObject(iface);
  HRESULT hr;

  FIXME("(%p): semi-stub\n", pbc);

  /* already running? if so nothing to do */
1319
  if (object_is_running(This))
1320 1321
    return S_OK;

1322 1323
  release_delegates(This);

1324 1325
  hr = CoCreateInstance(&This->clsid, NULL, CLSCTX_LOCAL_SERVER,
                        &IID_IOleObject, (void **)&This->pOleDelegate);
1326 1327 1328
  if (FAILED(hr))
    return hr;

1329 1330
  This->object_state = object_state_running;

1331
  hr = IOleObject_Advise(This->pOleDelegate, &This->IAdviseSink_iface, &This->dwAdvConn);
1332 1333

  if (SUCCEEDED(hr) && This->clientSite)
1334 1335 1336 1337
    hr = IOleObject_SetClientSite(This->pOleDelegate, This->clientSite);

  if (SUCCEEDED(hr))
  {
1338 1339
    IOleObject_QueryInterface(This->pOleDelegate, &IID_IPersistStorage,
                              (void **)&This->pPSDelegate);
1340
    if (This->pPSDelegate)
1341 1342 1343 1344 1345 1346
    {
      if(This->storage_state == storage_state_initialised)
        hr = IPersistStorage_InitNew(This->pPSDelegate, This->storage);
      else if(This->storage_state == storage_state_loaded)
        hr = IPersistStorage_Load(This->pPSDelegate, This->storage);
    }
1347 1348 1349
  }

  if (SUCCEEDED(hr) && This->containerApp)
1350 1351
    hr = IOleObject_SetHostNames(This->pOleDelegate, This->containerApp,
                                 This->containerObj);
1352 1353 1354 1355

  /* FIXME: do more stuff here:
   * - IOleObject_GetMiscStatus
   * - IOleObject_GetMoniker
1356
   * - IOleCache_OnRun
1357 1358
   */

1359 1360 1361 1362 1363 1364 1365
  if (SUCCEEDED(hr))
    hr = IOleObject_QueryInterface(This->pOleDelegate, &IID_IDataObject,
                                   (void **)&This->pDataDelegate);

  if (SUCCEEDED(hr) && This->dataAdviseHolder)
    hr = DataAdviseHolder_OnConnect(This->dataAdviseHolder, This->pDataDelegate);

1366
  if (FAILED(hr))
1367
  {
1368
    DefaultHandler_Stop(This);
1369 1370
    release_delegates(This);
  }
1371 1372

  return hr;
1373 1374 1375 1376 1377 1378 1379
}

/************************************************************************
 * DefaultHandler_IsRunning (IRunnableObject)
 *
 * See Windows documentation for more details on IRunnableObject methods.
 */
1380
static BOOL    WINAPI DefaultHandler_IsRunning(
1381 1382
            IRunnableObject*     iface)
{
1383 1384
  DefaultHandler *This = impl_from_IRunnableObject(iface);

1385
  TRACE("()\n");
1386

1387
  if (This->object_state == object_state_running)
1388 1389 1390
    return TRUE;
  else
    return FALSE;
1391 1392 1393 1394 1395 1396 1397
}

/************************************************************************
 * DefaultHandler_LockRunning (IRunnableObject)
 *
 * See Windows documentation for more details on IRunnableObject methods.
 */
1398 1399 1400
static HRESULT WINAPI DefaultHandler_LockRunning(
            IRunnableObject*     iface,
	    BOOL                 fLock,
1401 1402
	    BOOL                 fLastUnlockCloses)
{
1403
  FIXME("()\n");
1404 1405 1406 1407 1408 1409 1410 1411
  return S_OK;
}

/************************************************************************
 * DefaultHandler_SetContainedObject (IRunnableObject)
 *
 * See Windows documentation for more details on IRunnableObject methods.
 */
1412 1413
static HRESULT WINAPI DefaultHandler_SetContainedObject(
            IRunnableObject*     iface,
1414 1415
	    BOOL                 fContained)
{
1416
  FIXME("()\n");
1417 1418
  return S_OK;
}
1419

1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
static HRESULT WINAPI DefaultHandler_IAdviseSink_QueryInterface(
    IAdviseSink *iface,
    REFIID riid,
    void **ppvObject)
{
    if (IsEqualIID(riid, &IID_IUnknown) ||
        IsEqualIID(riid, &IID_IAdviseSink))
    {
        *ppvObject = iface;
        IAdviseSink_AddRef(iface);
        return S_OK;
    }

    return E_NOINTERFACE;
}

static ULONG WINAPI DefaultHandler_IAdviseSink_AddRef(
    IAdviseSink *iface)
{
    DefaultHandler *This = impl_from_IAdviseSink(iface);

1441
    return IUnknown_AddRef(&This->IUnknown_iface);
1442 1443 1444 1445 1446 1447 1448
}

static ULONG WINAPI DefaultHandler_IAdviseSink_Release(
            IAdviseSink *iface)
{
    DefaultHandler *This = impl_from_IAdviseSink(iface);

1449
    return IUnknown_Release(&This->IUnknown_iface);
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
}

static void WINAPI DefaultHandler_IAdviseSink_OnDataChange(
    IAdviseSink *iface,
    FORMATETC *pFormatetc,
    STGMEDIUM *pStgmed)
{
    FIXME(": stub\n");
}

static void WINAPI DefaultHandler_IAdviseSink_OnViewChange(
    IAdviseSink *iface,
    DWORD dwAspect,
    LONG lindex)
{
    FIXME(": stub\n");
}

static void WINAPI DefaultHandler_IAdviseSink_OnRename(
    IAdviseSink *iface,
    IMoniker *pmk)
{
1472 1473 1474 1475 1476 1477
    DefaultHandler *This = impl_from_IAdviseSink(iface);

    TRACE("(%p)\n", pmk);

    if (This->oleAdviseHolder)
        IOleAdviseHolder_SendOnRename(This->oleAdviseHolder, pmk);
1478 1479 1480 1481 1482
}

static void WINAPI DefaultHandler_IAdviseSink_OnSave(
    IAdviseSink *iface)
{
1483 1484 1485 1486 1487 1488
    DefaultHandler *This = impl_from_IAdviseSink(iface);

    TRACE("()\n");

    if (This->oleAdviseHolder)
        IOleAdviseHolder_SendOnSave(This->oleAdviseHolder);
1489 1490 1491 1492 1493
}

static void WINAPI DefaultHandler_IAdviseSink_OnClose(
    IAdviseSink *iface)
{
1494 1495 1496 1497
    DefaultHandler *This = impl_from_IAdviseSink(iface);
    
    TRACE("()\n");

1498 1499
    if (This->oleAdviseHolder)
        IOleAdviseHolder_SendOnClose(This->oleAdviseHolder);
1500 1501

    DefaultHandler_Stop(This);
1502 1503
}

1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550

/************************************************************************
 * DefaultHandler_IPersistStorage_QueryInterface
 *
 */
static HRESULT WINAPI DefaultHandler_IPersistStorage_QueryInterface(
            IPersistStorage*     iface,
            REFIID               riid,
            void**               ppvObject)
{
  DefaultHandler *This = impl_from_IPersistStorage(iface);

  return IUnknown_QueryInterface(This->outerUnknown, riid, ppvObject);
}

/************************************************************************
 * DefaultHandler_IPersistStorage_AddRef
 *
 */
static ULONG WINAPI DefaultHandler_IPersistStorage_AddRef(
            IPersistStorage*     iface)
{
  DefaultHandler *This = impl_from_IPersistStorage(iface);

  return IUnknown_AddRef(This->outerUnknown);
}

/************************************************************************
 * DefaultHandler_IPersistStorage_Release
 *
 */
static ULONG WINAPI DefaultHandler_IPersistStorage_Release(
            IPersistStorage*     iface)
{
  DefaultHandler *This = impl_from_IPersistStorage(iface);

  return IUnknown_Release(This->outerUnknown);
}

/************************************************************************
 * DefaultHandler_IPersistStorage_GetClassID
 *
 */
static HRESULT WINAPI DefaultHandler_IPersistStorage_GetClassID(
            IPersistStorage*     iface,
            CLSID*               clsid)
{
1551 1552 1553 1554
    DefaultHandler *This = impl_from_IPersistStorage(iface);
    HRESULT hr;

    TRACE("(%p)->(%p)\n", iface, clsid);
1555

1556 1557 1558 1559 1560 1561
    if(object_is_running(This))
        hr = IPersistStorage_GetClassID(This->pPSDelegate, clsid);
    else
        hr = IPersistStorage_GetClassID(This->dataCache_PersistStg, clsid);

    return hr;
1562 1563 1564 1565 1566 1567 1568 1569 1570
}

/************************************************************************
 * DefaultHandler_IPersistStorage_IsDirty
 *
 */
static HRESULT WINAPI DefaultHandler_IPersistStorage_IsDirty(
            IPersistStorage*     iface)
{
1571 1572 1573 1574 1575 1576 1577
    DefaultHandler *This = impl_from_IPersistStorage(iface);
    HRESULT hr;

    TRACE("(%p)\n", iface);

    hr = IPersistStorage_IsDirty(This->dataCache_PersistStg);
    if(hr != S_FALSE) return hr;
1578

1579 1580 1581 1582
    if(object_is_running(This))
        hr = IPersistStorage_IsDirty(This->pPSDelegate);

    return hr;
1583 1584
}

1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
/***********************************************************************
 *   init_ole_stream
 *
 * Creates the '\1Ole' stream.
 * The format of this stream is as follows:
 *
 * DWORD Version == 0x02000001
 * DWORD Flags - low bit set indicates the object is a link otherwise it's embedded.
 * DWORD LinkupdateOption - [MS-OLEDS describes this as an implementation specific hint
 *                           supplied by the app that creates the data structure.  May be
 *                           ignored on processing].
 *
 * DWORD Reserved == 0
 * DWORD MonikerStreamSize - size of the rest of the data (ie CLSID + moniker stream data).
 * CLSID clsid - class id of object capable of processing the moniker
 * BYTE  data[] - moniker data for a link
 */

static const WCHAR OleStream[] = {1,'O','l','e',0};
typedef struct
{
    DWORD version;
    DWORD flags;
    DWORD link_update_opt;
    DWORD res;
    DWORD moniker_size;
} ole_stream_header_t;
static const DWORD ole_stream_version = 0x02000001;

static void init_ole_stream(IStorage *storage)
{
    HRESULT hr;
    IStream *stream;

    hr = IStorage_CreateStream(storage, OleStream, STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stream);
    if(SUCCEEDED(hr))
    {
        DWORD written;
        ole_stream_header_t header;

        header.version         = ole_stream_version;
        header.flags           = 0;
        header.link_update_opt = 0;
        header.res             = 0;
        header.moniker_size    = 0;

        IStream_Write(stream, &header, sizeof(header), &written);
        IStream_Release(stream);
    }
    return;
}

1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672
static HRESULT load_ole_stream(DefaultHandler *This, IStorage *storage)
{
    IStream *stream;
    HRESULT hr;

    hr = IStorage_OpenStream(storage, OleStream, NULL, STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stream);

    if(SUCCEEDED(hr))
    {
        DWORD read;
        ole_stream_header_t header;

        hr = IStream_Read(stream, &header, sizeof(header), &read);
        if(hr == S_OK && read == sizeof(header) && header.version == ole_stream_version)
        {
            if(header.flags & 1)
            {
                /* FIXME: Read the moniker and deal with the link */
                FIXME("Linked objects are not supported yet\n");
            }
        }
        else
        {
            WARN("Incorrect OleStream header\n");
            hr = DV_E_CLIPFORMAT;
        }
        IStream_Release(stream);
    }
    else
    {
        init_ole_stream(storage);
        hr = S_OK;
    }
    return hr;
}

1673 1674 1675 1676 1677 1678 1679 1680
/************************************************************************
 * DefaultHandler_IPersistStorage_InitNew
 *
 */
static HRESULT WINAPI DefaultHandler_IPersistStorage_InitNew(
           IPersistStorage*     iface,
           IStorage*            pStg)
{
1681 1682 1683 1684
    DefaultHandler *This = impl_from_IPersistStorage(iface);
    HRESULT hr;

    TRACE("(%p)->(%p)\n", iface, pStg);
1685
    init_ole_stream(pStg);
1686

1687 1688 1689 1690 1691
    hr = IPersistStorage_InitNew(This->dataCache_PersistStg, pStg);

    if(SUCCEEDED(hr) && object_is_running(This))
        hr = IPersistStorage_InitNew(This->pPSDelegate, pStg);

1692 1693 1694 1695 1696 1697 1698
    if(SUCCEEDED(hr))
    {
        IStorage_AddRef(pStg);
        This->storage = pStg;
        This->storage_state = storage_state_initialised;
    }

1699
    return hr;
1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710
}


/************************************************************************
 * DefaultHandler_IPersistStorage_Load
 *
 */
static HRESULT WINAPI DefaultHandler_IPersistStorage_Load(
           IPersistStorage*     iface,
           IStorage*            pStg)
{
1711 1712 1713 1714 1715
    DefaultHandler *This = impl_from_IPersistStorage(iface);
    HRESULT hr;

    TRACE("(%p)->(%p)\n", iface, pStg);

1716 1717 1718 1719
    hr = load_ole_stream(This, pStg);

    if(SUCCEEDED(hr))
        hr = IPersistStorage_Load(This->dataCache_PersistStg, pStg);
1720

1721 1722 1723
    if(SUCCEEDED(hr) && object_is_running(This))
        hr = IPersistStorage_Load(This->pPSDelegate, pStg);

1724 1725 1726 1727 1728 1729
    if(SUCCEEDED(hr))
    {
        IStorage_AddRef(pStg);
        This->storage = pStg;
        This->storage_state = storage_state_loaded;
    }
1730
    return hr;
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740
}


/************************************************************************
 * DefaultHandler_IPersistStorage_Save
 *
 */
static HRESULT WINAPI DefaultHandler_IPersistStorage_Save(
           IPersistStorage*     iface,
           IStorage*            pStgSave,
1741
           BOOL                 fSameAsLoad)
1742
{
1743 1744 1745 1746
    DefaultHandler *This = impl_from_IPersistStorage(iface);
    HRESULT hr;

    TRACE("(%p)->(%p, %d)\n", iface, pStgSave, fSameAsLoad);
1747

1748 1749 1750 1751 1752
    hr = IPersistStorage_Save(This->dataCache_PersistStg, pStgSave, fSameAsLoad);
    if(SUCCEEDED(hr) && object_is_running(This))
        hr = IPersistStorage_Save(This->pPSDelegate, pStgSave, fSameAsLoad);

    return hr;
1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763
}


/************************************************************************
 * DefaultHandler_IPersistStorage_SaveCompleted
 *
 */
static HRESULT WINAPI DefaultHandler_IPersistStorage_SaveCompleted(
           IPersistStorage*     iface,
           IStorage*            pStgNew)
{
1764 1765 1766 1767
    DefaultHandler *This = impl_from_IPersistStorage(iface);
    HRESULT hr;

    TRACE("(%p)->(%p)\n", iface, pStgNew);
1768

1769 1770 1771 1772 1773
    hr = IPersistStorage_SaveCompleted(This->dataCache_PersistStg, pStgNew);

    if(SUCCEEDED(hr) && object_is_running(This))
        hr = IPersistStorage_SaveCompleted(This->pPSDelegate, pStgNew);

1774 1775 1776 1777 1778 1779 1780 1781
    if(pStgNew)
    {
        IStorage_AddRef(pStgNew);
        if(This->storage) IStorage_Release(This->storage);
        This->storage = pStgNew;
        This->storage_state = storage_state_loaded;
    }

1782
    return hr;
1783 1784 1785 1786 1787 1788 1789 1790 1791 1792
}


/************************************************************************
 * DefaultHandler_IPersistStorage_HandsOffStorage
 *
 */
static HRESULT WINAPI DefaultHandler_IPersistStorage_HandsOffStorage(
            IPersistStorage*     iface)
{
1793 1794 1795 1796
    DefaultHandler *This = impl_from_IPersistStorage(iface);
    HRESULT hr;

    TRACE("(%p)\n", iface);
1797

1798 1799 1800 1801 1802
    hr = IPersistStorage_HandsOffStorage(This->dataCache_PersistStg);

    if(SUCCEEDED(hr) && object_is_running(This))
        hr = IPersistStorage_HandsOffStorage(This->pPSDelegate);

1803 1804 1805 1806
    if(This->storage) IStorage_Release(This->storage);
    This->storage = NULL;
    This->storage_state = storage_state_uninitialised;

1807
    return hr;
1808 1809 1810
}


1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876
/*
 * Virtual function tables for the DefaultHandler class.
 */
static const IOleObjectVtbl DefaultHandler_IOleObject_VTable =
{
  DefaultHandler_QueryInterface,
  DefaultHandler_AddRef,
  DefaultHandler_Release,
  DefaultHandler_SetClientSite,
  DefaultHandler_GetClientSite,
  DefaultHandler_SetHostNames,
  DefaultHandler_Close,
  DefaultHandler_SetMoniker,
  DefaultHandler_GetMoniker,
  DefaultHandler_InitFromData,
  DefaultHandler_GetClipboardData,
  DefaultHandler_DoVerb,
  DefaultHandler_EnumVerbs,
  DefaultHandler_Update,
  DefaultHandler_IsUpToDate,
  DefaultHandler_GetUserClassID,
  DefaultHandler_GetUserType,
  DefaultHandler_SetExtent,
  DefaultHandler_GetExtent,
  DefaultHandler_Advise,
  DefaultHandler_Unadvise,
  DefaultHandler_EnumAdvise,
  DefaultHandler_GetMiscStatus,
  DefaultHandler_SetColorScheme
};

static const IUnknownVtbl DefaultHandler_NDIUnknown_VTable =
{
  DefaultHandler_NDIUnknown_QueryInterface,
  DefaultHandler_NDIUnknown_AddRef,
  DefaultHandler_NDIUnknown_Release,
};

static const IDataObjectVtbl DefaultHandler_IDataObject_VTable =
{
  DefaultHandler_IDataObject_QueryInterface,
  DefaultHandler_IDataObject_AddRef,
  DefaultHandler_IDataObject_Release,
  DefaultHandler_GetData,
  DefaultHandler_GetDataHere,
  DefaultHandler_QueryGetData,
  DefaultHandler_GetCanonicalFormatEtc,
  DefaultHandler_SetData,
  DefaultHandler_EnumFormatEtc,
  DefaultHandler_DAdvise,
  DefaultHandler_DUnadvise,
  DefaultHandler_EnumDAdvise
};

static const IRunnableObjectVtbl DefaultHandler_IRunnableObject_VTable =
{
  DefaultHandler_IRunnableObject_QueryInterface,
  DefaultHandler_IRunnableObject_AddRef,
  DefaultHandler_IRunnableObject_Release,
  DefaultHandler_GetRunningClass,
  DefaultHandler_Run,
  DefaultHandler_IsRunning,
  DefaultHandler_LockRunning,
  DefaultHandler_SetContainedObject
};

1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888
static const IAdviseSinkVtbl DefaultHandler_IAdviseSink_VTable =
{
  DefaultHandler_IAdviseSink_QueryInterface,
  DefaultHandler_IAdviseSink_AddRef,
  DefaultHandler_IAdviseSink_Release,
  DefaultHandler_IAdviseSink_OnDataChange,
  DefaultHandler_IAdviseSink_OnViewChange,
  DefaultHandler_IAdviseSink_OnRename,
  DefaultHandler_IAdviseSink_OnSave,
  DefaultHandler_IAdviseSink_OnClose
};

1889
static const IPersistStorageVtbl DefaultHandler_IPersistStorage_VTable =
1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902
{
  DefaultHandler_IPersistStorage_QueryInterface,
  DefaultHandler_IPersistStorage_AddRef,
  DefaultHandler_IPersistStorage_Release,
  DefaultHandler_IPersistStorage_GetClassID,
  DefaultHandler_IPersistStorage_IsDirty,
  DefaultHandler_IPersistStorage_InitNew,
  DefaultHandler_IPersistStorage_Load,
  DefaultHandler_IPersistStorage_Save,
  DefaultHandler_IPersistStorage_SaveCompleted,
  DefaultHandler_IPersistStorage_HandsOffStorage
};

1903 1904 1905 1906 1907
/*********************************************************
 * Methods implementation for the DefaultHandler class.
 */
static DefaultHandler* DefaultHandler_Construct(
  REFCLSID  clsid,
1908 1909 1910
  LPUNKNOWN pUnkOuter,
  DWORD flags,
  IClassFactory *pCF)
1911
{
1912
  DefaultHandler* This = NULL;
1913
  HRESULT hr;
1914

1915
  This = HeapAlloc(GetProcessHeap(), 0, sizeof(DefaultHandler));
1916

1917 1918
  if (!This)
    return This;
1919

1920 1921 1922 1923 1924 1925
  This->IOleObject_iface.lpVtbl = &DefaultHandler_IOleObject_VTable;
  This->IUnknown_iface.lpVtbl = &DefaultHandler_NDIUnknown_VTable;
  This->IDataObject_iface.lpVtbl = &DefaultHandler_IDataObject_VTable;
  This->IRunnableObject_iface.lpVtbl = &DefaultHandler_IRunnableObject_VTable;
  This->IAdviseSink_iface.lpVtbl = &DefaultHandler_IAdviseSink_VTable;
  This->IPersistStorage_iface.lpVtbl = &DefaultHandler_IPersistStorage_VTable;
1926

1927 1928
  This->inproc_server = (flags & EMBDHLP_INPROC_SERVER) ? TRUE : FALSE;

1929 1930 1931 1932
  /*
   * Start with one reference count. The caller of this function
   * must release the interface pointer when it is done.
   */
1933
  This->ref = 1;
1934 1935 1936 1937

  /*
   * Initialize the outer unknown
   * We don't keep a reference on the outer unknown since, the way
Huw Davies's avatar
Huw Davies committed
1938
   * aggregation works, our lifetime is at least as large as its
1939 1940
   * lifetime.
   */
1941
  if (!pUnkOuter)
1942
    pUnkOuter = &This->IUnknown_iface;
1943

1944
  This->outerUnknown = pUnkOuter;
1945 1946 1947 1948 1949 1950

  /*
   * Create a datacache object.
   * We aggregate with the datacache. Make sure we pass our outer
   * unknown as the datacache's outer unknown.
   */
1951 1952 1953 1954 1955
  hr = CreateDataCache(This->outerUnknown,
                       clsid,
                       &IID_IUnknown,
                       (void**)&This->dataCache);
  if(SUCCEEDED(hr))
1956
  {
1957
    hr = IUnknown_QueryInterface(This->dataCache, &IID_IPersistStorage, (void**)&This->dataCache_PersistStg);
1958 1959 1960 1961 1962 1963 1964
    /* keeping a reference to This->dataCache_PersistStg causes us to keep a
     * reference on the outer object */
    if (SUCCEEDED(hr))
        IUnknown_Release(This->outerUnknown);
    else
        IUnknown_Release(This->dataCache);
  }
1965
  if(FAILED(hr))
1966
  {
1967
    ERR("Unexpected error creating data cache\n");
1968 1969 1970
    HeapFree(GetProcessHeap(), 0, This);
    return NULL;
  }
1971

1972
  This->clsid = *clsid;
1973 1974 1975 1976 1977
  This->clientSite = NULL;
  This->oleAdviseHolder = NULL;
  This->dataAdviseHolder = NULL;
  This->containerApp = NULL;
  This->containerObj = NULL;
1978 1979
  This->pOleDelegate = NULL;
  This->pPSDelegate = NULL;
1980
  This->pDataDelegate = NULL;
1981
  This->object_state = object_state_not_running;
1982

1983
  This->dwAdvConn = 0;
1984 1985
  This->storage = NULL;
  This->storage_state = storage_state_uninitialised;
1986

1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010
  if (This->inproc_server && !(flags & EMBDHLP_DELAYCREATE))
  {
    HRESULT hr;
    This->pCFObject = NULL;
    if (pCF)
      hr = IClassFactory_CreateInstance(pCF, NULL, &IID_IOleObject, (void **)&This->pOleDelegate);
    else
      hr = CoCreateInstance(&This->clsid, NULL, CLSCTX_INPROC_SERVER,
                            &IID_IOleObject, (void **)&This->pOleDelegate);
    if (SUCCEEDED(hr))
      hr = IOleObject_QueryInterface(This->pOleDelegate, &IID_IPersistStorage, (void **)&This->pPSDelegate);
    if (SUCCEEDED(hr))
      hr = IOleObject_QueryInterface(This->pOleDelegate, &IID_IDataObject, (void **)&This->pDataDelegate);
    if (SUCCEEDED(hr))
      This->object_state = object_state_running;
    if (FAILED(hr))
      WARN("object creation failed with error %08x\n", hr);
  }
  else
  {
    This->pCFObject = pCF;
    if (pCF) IClassFactory_AddRef(pCF);
  }

2011
  return This;
2012 2013 2014
}

static void DefaultHandler_Destroy(
2015
  DefaultHandler* This)
2016
{
2017 2018 2019 2020 2021 2022 2023
  TRACE("(%p)\n", This);

  /* AddRef/Release may be called on this object during destruction.
   * Prevent the object being destroyed recursively by artificially raising
   * the reference count. */
  This->ref = 10000;

2024 2025
  /* release delegates */
  DefaultHandler_Stop(This);
2026
  release_delegates(This);
2027

2028 2029 2030 2031 2032 2033
  HeapFree( GetProcessHeap(), 0, This->containerApp );
  This->containerApp = NULL;
  HeapFree( GetProcessHeap(), 0, This->containerObj );
  This->containerObj = NULL;

  if (This->dataCache)
2034
  {
2035 2036 2037
    /* to balance out the release of dataCache_PersistStg which will result
     * in a reference being released from the outer unknown */
    IUnknown_AddRef(This->outerUnknown);
2038
    IPersistStorage_Release(This->dataCache_PersistStg);
2039
    IUnknown_Release(This->dataCache);
2040
    This->dataCache_PersistStg = NULL;
2041
    This->dataCache = NULL;
2042 2043
  }

2044
  if (This->clientSite)
2045
  {
2046 2047
    IOleClientSite_Release(This->clientSite);
    This->clientSite = NULL;
2048 2049
  }

2050
  if (This->oleAdviseHolder)
2051
  {
2052 2053
    IOleAdviseHolder_Release(This->oleAdviseHolder);
    This->oleAdviseHolder = NULL;
2054 2055
  }

2056
  if (This->dataAdviseHolder)
2057
  {
2058 2059
    IDataAdviseHolder_Release(This->dataAdviseHolder);
    This->dataAdviseHolder = NULL;
2060 2061
  }

2062 2063 2064 2065 2066 2067
  if (This->storage)
  {
    IStorage_Release(This->storage);
    This->storage = NULL;
  }

2068 2069 2070 2071 2072 2073
  if (This->pCFObject)
  {
    IClassFactory_Release(This->pCFObject);
    This->pCFObject = NULL;
  }

2074
  HeapFree(GetProcessHeap(), 0, This);
2075 2076 2077
}

/******************************************************************************
2078
 * OleCreateEmbeddingHelper [OLE32.@]
2079
 */
2080
HRESULT WINAPI OleCreateEmbeddingHelper(
2081 2082
  REFCLSID  clsid,
  LPUNKNOWN pUnkOuter,
2083 2084
  DWORD     flags,
  IClassFactory *pCF,
2085 2086 2087 2088 2089 2090
  REFIID    riid,
  LPVOID*   ppvObj)
{
  DefaultHandler* newHandler = NULL;
  HRESULT         hr         = S_OK;

2091
  TRACE("(%s, %p, %08x, %p, %s, %p)\n", debugstr_guid(clsid), pUnkOuter, flags, pCF, debugstr_guid(riid), ppvObj);
2092 2093 2094 2095 2096 2097 2098

  if (!ppvObj)
    return E_POINTER;

  *ppvObj = NULL;

  /*
2099
   * If This handler is constructed for aggregation, make sure
2100 2101 2102 2103
   * the caller is requesting the IUnknown interface.
   * This is necessary because it's the only time the non-delegating
   * IUnknown pointer can be returned to the outside.
   */
2104
  if (pUnkOuter && !IsEqualIID(&IID_IUnknown, riid))
2105 2106 2107 2108 2109
    return CLASS_E_NOAGGREGATION;

  /*
   * Try to construct a new instance of the class.
   */
2110
  newHandler = DefaultHandler_Construct(clsid, pUnkOuter, flags, pCF);
2111 2112 2113 2114 2115 2116 2117

  if (!newHandler)
    return E_OUTOFMEMORY;

  /*
   * Make sure it supports the interface required by the caller.
   */
2118
  hr = IUnknown_QueryInterface(&newHandler->IUnknown_iface, riid, ppvObj);
2119 2120 2121 2122 2123

  /*
   * Release the reference obtained in the constructor. If
   * the QueryInterface was unsuccessful, it will free the class.
   */
2124
  IUnknown_Release(&newHandler->IUnknown_iface);
2125 2126 2127

  return hr;
}
2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139


/******************************************************************************
 * OleCreateDefaultHandler [OLE32.@]
 */
HRESULT WINAPI OleCreateDefaultHandler(REFCLSID clsid, LPUNKNOWN pUnkOuter,
                                       REFIID riid, LPVOID* ppvObj)
{
    TRACE("(%s, %p, %s, %p)\n", debugstr_guid(clsid), pUnkOuter,debugstr_guid(riid), ppvObj);
    return OleCreateEmbeddingHelper(clsid, pUnkOuter, EMBDHLP_INPROC_HANDLER | EMBDHLP_CREATENOW,
                                    NULL, riid, ppvObj);
}
2140 2141 2142

typedef struct HandlerCF
{
2143
    IClassFactory IClassFactory_iface;
2144 2145 2146 2147
    LONG refs;
    CLSID clsid;
} HandlerCF;

2148 2149 2150 2151 2152
static inline HandlerCF *impl_from_IClassFactory(IClassFactory *iface)
{
    return CONTAINING_RECORD(iface, HandlerCF, IClassFactory_iface);
}

2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168
static HRESULT WINAPI
HandlerCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid, LPVOID *ppv)
{
    *ppv = NULL;
    if (IsEqualIID(riid,&IID_IUnknown) ||
        IsEqualIID(riid,&IID_IClassFactory))
    {
        *ppv = iface;
        IClassFactory_AddRef(iface);
        return S_OK;
    }
    return E_NOINTERFACE;
}

static ULONG WINAPI HandlerCF_AddRef(LPCLASSFACTORY iface)
{
2169
    HandlerCF *This = impl_from_IClassFactory(iface);
2170 2171 2172 2173 2174
    return InterlockedIncrement(&This->refs);
}

static ULONG WINAPI HandlerCF_Release(LPCLASSFACTORY iface)
{
2175
    HandlerCF *This = impl_from_IClassFactory(iface);
2176 2177 2178 2179 2180 2181 2182 2183 2184 2185
    ULONG refs = InterlockedDecrement(&This->refs);
    if (!refs)
        HeapFree(GetProcessHeap(), 0, This);
    return refs;
}

static HRESULT WINAPI
HandlerCF_CreateInstance(LPCLASSFACTORY iface, LPUNKNOWN pUnk,
                         REFIID riid, LPVOID *ppv)
{
2186
    HandlerCF *This = impl_from_IClassFactory(iface);
2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208
    return OleCreateDefaultHandler(&This->clsid, pUnk, riid, ppv);
}

static HRESULT WINAPI HandlerCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
{
    FIXME("(%d), stub!\n",fLock);
    return S_OK;
}

static const IClassFactoryVtbl HandlerClassFactoryVtbl = {
    HandlerCF_QueryInterface,
    HandlerCF_AddRef,
    HandlerCF_Release,
    HandlerCF_CreateInstance,
    HandlerCF_LockServer
};

HRESULT HandlerCF_Create(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
{
    HRESULT hr;
    HandlerCF *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
    if (!This) return E_OUTOFMEMORY;
2209
    This->IClassFactory_iface.lpVtbl = &HandlerClassFactoryVtbl;
2210 2211 2212
    This->refs = 0;
    This->clsid = *rclsid;

2213
    hr = IUnknown_QueryInterface((IUnknown *)&This->IClassFactory_iface, riid, ppv);
2214 2215 2216 2217 2218
    if (FAILED(hr))
        HeapFree(GetProcessHeap(), 0, This);

    return hr;
}