VirtualBox

source: vbox/trunk/src/VBox/GuestHost/SharedClipboard/ClipboardDataObjectImpl-win.cpp@ 100510

Last change on this file since 100510 was 100510, checked in by vboxsync, 17 months ago

Shared Clipboard: Also propagate the last error set via SharedClipboardWinDataObject::SetStatus(). bugref:9437

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 42.5 KB
Line 
1/* $Id: ClipboardDataObjectImpl-win.cpp 100510 2023-07-11 11:54:05Z vboxsync $ */
2/** @file
3 * ClipboardDataObjectImpl-win.cpp - Shared Clipboard IDataObject implementation.
4 */
5
6/*
7 * Copyright (C) 2019-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_SHARED_CLIPBOARD
33#include <VBox/GuestHost/SharedClipboard-win.h>
34#include <VBox/GuestHost/SharedClipboard-transfers.h>
35
36#include <iprt/win/windows.h>
37#include <iprt/win/shlobj.h>
38#include <iprt/win/shlwapi.h>
39
40#include <iprt/thread.h> // REMOVE
41
42#include <iprt/asm.h>
43#include <iprt/errcore.h>
44#include <iprt/path.h>
45#include <iprt/semaphore.h>
46#include <iprt/uri.h>
47#include <iprt/utf16.h>
48
49#include <iprt/errcore.h>
50#include <VBox/log.h>
51
52/** @todo Also handle Unicode entries.
53 * !!! WARNING: Buggy, doesn't work yet (some memory corruption / garbage in the file name descriptions) !!! */
54//#define VBOX_CLIPBOARD_WITH_UNICODE_SUPPORT 1
55
56SharedClipboardWinDataObject::SharedClipboardWinDataObject(void)
57 : m_pCtx(NULL)
58 , m_enmStatus(Uninitialized)
59 , m_rcStatus(VERR_IPE_UNINITIALIZED_STATUS)
60 , m_lRefCount(0)
61 , m_cFormats(0)
62 , m_pTransfer(NULL)
63 , m_pStream(NULL)
64 , m_uObjIdx(0)
65 , m_EventListComplete(NIL_RTSEMEVENT)
66 , m_EventStatusChanged(NIL_RTSEMEVENT)
67{
68}
69
70SharedClipboardWinDataObject::~SharedClipboardWinDataObject(void)
71{
72 Destroy();
73
74 LogFlowFunc(("mRefCount=%RI32\n", m_lRefCount));
75}
76
77/**
78 * Initializes a data object instance.
79 *
80 * @returns VBox status code.
81 * @param pCtx Opaque Shared Clipboard context to use.
82 * @param pCallbacks Callbacks table to use.
83 * @param pFormatEtc FormatETC to use. Optional.
84 * @param pStgMed Storage medium to use. Optional.
85 * @param cFormats Number of formats in \a pFormatEtc and \a pStgMed. Optional.
86 */
87int SharedClipboardWinDataObject::Init(PSHCLCONTEXT pCtx, SharedClipboardWinDataObject::PCALLBACKS pCallbacks,
88 LPFORMATETC pFormatEtc /* = NULL */, LPSTGMEDIUM pStgMed /* = NULL */,
89 ULONG cFormats /* = 0 */)
90{
91 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
92 AssertPtrReturn(pCallbacks, VERR_INVALID_POINTER);
93 AssertReturn(cFormats == 0 || (RT_VALID_PTR(pFormatEtc) && RT_VALID_PTR(pStgMed)), VERR_INVALID_POINTER);
94
95 int rc = VINF_SUCCESS;
96
97 m_pCtx = pCtx; /* Save opaque context. */
98
99 /*
100 * Set up callback context + table.
101 */
102 memcpy(&m_Callbacks, pCallbacks, sizeof(SharedClipboardWinDataObject::CALLBACKS));
103 m_CallbackCtx.pvUser = pCtx;
104 m_CallbackCtx.pThis = this;
105
106 /*
107 * Set up / register handled formats.
108 */
109 ULONG cFixedFormats = 3; /* CFSTR_FILEDESCRIPTORA + CFSTR_FILECONTENTS + CFSTR_PERFORMEDDROPEFFECT */
110#ifdef VBOX_CLIPBOARD_WITH_UNICODE_SUPPORT
111 cFixedFormats++; /* CFSTR_FILEDESCRIPTORW */
112#endif
113 const ULONG cAllFormats = cFormats + cFixedFormats;
114
115 m_pFormatEtc = new FORMATETC[cAllFormats];
116 AssertPtrReturn(m_pFormatEtc, VERR_NO_MEMORY);
117 RT_BZERO(m_pFormatEtc, sizeof(FORMATETC) * cAllFormats);
118 m_pStgMedium = new STGMEDIUM[cAllFormats];
119 AssertPtrReturn(m_pStgMedium, VERR_NO_MEMORY);
120 RT_BZERO(m_pStgMedium, sizeof(STGMEDIUM) * cAllFormats);
121
122 /** @todo Do we need CFSTR_FILENAME / CFSTR_SHELLIDLIST here? */
123
124 /*
125 * Register fixed formats.
126 */
127 unsigned uIdx = 0;
128
129 LogFlowFunc(("Registering CFSTR_FILEDESCRIPTORA ...\n"));
130 m_cfFileDescriptorA = RegisterClipboardFormat(CFSTR_FILEDESCRIPTORA);
131 registerFormat(&m_pFormatEtc[uIdx++], m_cfFileDescriptorA);
132#ifdef VBOX_CLIPBOARD_WITH_UNICODE_SUPPORT
133 LogFlowFunc(("Registering CFSTR_FILEDESCRIPTORW ...\n"));
134 m_cfFileDescriptorW = RegisterClipboardFormat(CFSTR_FILEDESCRIPTORW);
135 registerFormat(&m_pFormatEtc[uIdx++], m_cfFileDescriptorW);
136#endif
137
138 /* IStream interface, implemented in ClipboardStreamImpl-win.cpp. */
139 LogFlowFunc(("Registering CFSTR_FILECONTENTS ...\n"));
140 m_cfFileContents = RegisterClipboardFormat(CFSTR_FILECONTENTS);
141 registerFormat(&m_pFormatEtc[uIdx++], m_cfFileContents, TYMED_ISTREAM, 0 /* lIndex */);
142
143 /* We want to know from the target what the outcome of the operation was to react accordingly (e.g. abort a transfer). */
144 LogFlowFunc(("Registering CFSTR_PERFORMEDDROPEFFECT ...\n"));
145 m_cfPerformedDropEffect = RegisterClipboardFormat(CFSTR_PERFORMEDDROPEFFECT);
146 registerFormat(&m_pFormatEtc[uIdx++], m_cfPerformedDropEffect, TYMED_HGLOBAL, -1 /* lIndex */, DVASPECT_CONTENT);
147
148 /*
149 * Registration of dynamic formats needed?
150 */
151 LogFlowFunc(("%RU32 dynamic formats\n", cFormats));
152 if (cFormats)
153 {
154 for (ULONG i = 0; i < cFormats; i++)
155 {
156 LogFlowFunc(("Format %RU32: cfFormat=%RI16, tyMed=%RU32, dwAspect=%RU32\n",
157 i, pFormatEtc[i].cfFormat, pFormatEtc[i].tymed, pFormatEtc[i].dwAspect));
158 m_pFormatEtc[cFixedFormats + i] = pFormatEtc[i];
159 m_pStgMedium[cFixedFormats + i] = pStgMed[i];
160 }
161 }
162
163 if (RT_SUCCESS(rc))
164 {
165 m_cFormats = cAllFormats;
166 m_enmStatus = Initialized;
167
168 rc = RTCritSectInit(&m_CritSect);
169 if (RT_SUCCESS(rc))
170 {
171 rc = RTSemEventCreate(&m_EventListComplete);
172 if (RT_SUCCESS(rc))
173 rc = RTSemEventCreate(&m_EventStatusChanged);
174 }
175 }
176
177 LogFlowFunc(("cAllFormats=%RU32, rc=%Rrc\n", cAllFormats, rc));
178 return rc;
179}
180
181/**
182 * Uninitialized a data object instance, internal version.
183 */
184void SharedClipboardWinDataObject::uninitInternal(void)
185{
186 LogFlowFuncEnter();
187
188 /* Let waiters know. */
189 setStatusLocked(Uninitialized, VINF_SUCCESS);
190
191 /* Make sure to release the transfer. */
192 setTransferLocked(NULL);
193}
194
195/**
196 * Uninitialized a data object instance.
197 */
198void SharedClipboardWinDataObject::Uninit(void)
199{
200 LogFlowFuncEnter();
201
202 lock();
203
204 uninitInternal();
205
206 unlock();
207}
208
209/**
210 * Destroys a data object instance.
211 */
212void SharedClipboardWinDataObject::Destroy(void)
213{
214 LogFlowFuncEnter();
215
216 if (m_enmStatus == Uninitialized) /* Crit sect not available anymore. */
217 return;
218
219 lock();
220
221 uninitInternal();
222
223 unlock();
224
225 int rc = RTCritSectDelete(&m_CritSect);
226 AssertRC(rc);
227
228 if (m_EventListComplete != NIL_RTSEMEVENT)
229 {
230 rc = RTSemEventDestroy(m_EventListComplete);
231 AssertRC(rc);
232 m_EventListComplete = NIL_RTSEMEVENT;
233 }
234
235 if (m_EventStatusChanged != NIL_RTSEMEVENT)
236 {
237 rc = RTSemEventDestroy(m_EventStatusChanged);
238 AssertRC(rc);
239 m_EventStatusChanged = NIL_RTSEMEVENT;
240 }
241
242 if (m_pStream)
243 {
244 m_pStream->Release();
245 m_pStream = NULL;
246 }
247
248 if (m_pFormatEtc)
249 {
250 delete[] m_pFormatEtc;
251 m_pFormatEtc = NULL;
252 }
253
254 if (m_pStgMedium)
255 {
256 delete[] m_pStgMedium;
257 m_pStgMedium = NULL;
258 }
259
260 if (m_pTransfer)
261 ShClTransferRelease(m_pTransfer);
262
263 FsObjEntryList::const_iterator itRoot = m_lstEntries.cbegin();
264 while (itRoot != m_lstEntries.end())
265 {
266 RTStrFree(itRoot->pszPath);
267 ++itRoot;
268 }
269 m_lstEntries.clear();
270}
271
272
273/*********************************************************************************************************************************
274 * IUnknown methods.
275 ********************************************************************************************************************************/
276
277STDMETHODIMP_(ULONG) SharedClipboardWinDataObject::AddRef(void)
278{
279 LONG lCount = InterlockedIncrement(&m_lRefCount);
280 LogFlowFunc(("lCount=%RI32\n", lCount));
281 return lCount;
282}
283
284STDMETHODIMP_(ULONG) SharedClipboardWinDataObject::Release(void)
285{
286 LONG lCount = InterlockedDecrement(&m_lRefCount);
287 LogFlowFunc(("lCount=%RI32\n", m_lRefCount));
288 if (lCount == 0)
289 {
290 delete this;
291 return 0;
292 }
293
294 return lCount;
295}
296
297STDMETHODIMP SharedClipboardWinDataObject::QueryInterface(REFIID iid, void **ppvObject)
298{
299 AssertPtrReturn(ppvObject, E_INVALIDARG);
300
301 if ( iid == IID_IDataObject
302 || iid == IID_IUnknown)
303 {
304 AddRef();
305 *ppvObject = this;
306 return S_OK;
307 }
308
309 *ppvObject = 0;
310 return E_NOINTERFACE;
311}
312
313/**
314 * Copies a chunk of data into a HGLOBAL object.
315 *
316 * @returns VBox status code.
317 * @param pvData Data to copy.
318 * @param cbData Size (in bytes) to copy.
319 * @param fFlags GlobalAlloc flags, used for allocating the HGLOBAL block.
320 * @param phGlobal Where to store the allocated HGLOBAL object.
321 */
322int SharedClipboardWinDataObject::copyToHGlobal(const void *pvData, size_t cbData, UINT fFlags, HGLOBAL *phGlobal)
323{
324 AssertPtrReturn(phGlobal, VERR_INVALID_POINTER);
325
326 HGLOBAL hGlobal = GlobalAlloc(fFlags, cbData);
327 if (!hGlobal)
328 return VERR_NO_MEMORY;
329
330 void *pvAlloc = GlobalLock(hGlobal);
331 if (pvAlloc)
332 {
333 CopyMemory(pvAlloc, pvData, cbData);
334 GlobalUnlock(hGlobal);
335
336 *phGlobal = hGlobal;
337
338 return VINF_SUCCESS;
339 }
340
341 GlobalFree(hGlobal);
342 return VERR_ACCESS_DENIED;
343}
344
345inline int SharedClipboardWinDataObject::lock(void)
346{
347 int rc = RTCritSectEnter(&m_CritSect);
348 AssertRCReturn(rc, rc);
349
350 return rc;
351}
352
353inline int SharedClipboardWinDataObject::unlock(void)
354{
355 int rc = RTCritSectLeave(&m_CritSect);
356 AssertRCReturn(rc, rc);
357
358 return rc;
359}
360
361/**
362 * Reads (handles) a specific directory reursively and inserts its entry into the
363 * objects's entry list.
364 *
365 * @returns VBox status code.
366 * @param pTransfer Shared Clipboard transfer object to handle.
367 * @param strDir Directory path to handle.
368 */
369int SharedClipboardWinDataObject::readDir(PSHCLTRANSFER pTransfer, const Utf8Str &strDir)
370{
371 LogFlowFunc(("strDir=%s\n", strDir.c_str()));
372
373 SHCLLISTOPENPARMS openParmsList;
374 int rc = ShClTransferListOpenParmsInit(&openParmsList);
375 if (RT_SUCCESS(rc))
376 {
377 rc = RTStrCopy(openParmsList.pszPath, openParmsList.cbPath, strDir.c_str());
378 if (RT_SUCCESS(rc))
379 {
380 SHCLLISTHANDLE hList;
381 rc = ShClTransferListOpen(pTransfer, &openParmsList, &hList);
382 if (RT_SUCCESS(rc))
383 {
384 LogFlowFunc(("strDir=%s -> hList=%RU64\n", strDir.c_str(), hList));
385
386 SHCLLISTHDR hdrList;
387 rc = ShClTransferListGetHeader(pTransfer, hList, &hdrList);
388 if (RT_SUCCESS(rc))
389 {
390 LogFlowFunc(("cTotalObjects=%RU64, cbTotalSize=%RU64\n\n",
391 hdrList.cEntries, hdrList.cbTotalSize));
392
393 for (uint64_t o = 0; o < hdrList.cEntries; o++)
394 {
395 SHCLLISTENTRY entryList;
396 rc = ShClTransferListEntryInit(&entryList);
397 if (RT_SUCCESS(rc))
398 {
399 rc = ShClTransferListRead(pTransfer, hList, &entryList);
400 if (RT_SUCCESS(rc))
401 {
402 if (ShClTransferListEntryIsValid(&entryList))
403 {
404 PSHCLFSOBJINFO pFsObjInfo = (PSHCLFSOBJINFO)entryList.pvInfo;
405 Assert(entryList.cbInfo == sizeof(SHCLFSOBJINFO));
406
407 Utf8Str strPath = strDir + Utf8Str("\\") + Utf8Str(entryList.pszName);
408
409 LogFlowFunc(("\t%s (%RU64 bytes) -> %s\n",
410 entryList.pszName, pFsObjInfo->cbObject, strPath.c_str()));
411
412 if ( RTFS_IS_DIRECTORY(pFsObjInfo->Attr.fMode)
413 || RTFS_IS_FILE (pFsObjInfo->Attr.fMode))
414 {
415 FSOBJENTRY objEntry;
416 objEntry.pszPath = RTStrDup(strPath.c_str());
417 AssertPtrBreakStmt(objEntry.pszPath, rc = VERR_NO_MEMORY);
418 objEntry.objInfo = *pFsObjInfo;
419
420 lock();
421 m_lstEntries.push_back(objEntry); /** @todo Can this throw? */
422 unlock();
423 }
424 else /* Not fatal, just skip. */
425 LogRel(("Shared Clipboard: Warning: File system object '%s' of type %#x not supported, skipping\n",
426 strPath.c_str(), pFsObjInfo->Attr.fMode & RTFS_TYPE_MASK));
427
428 /** @todo Handle symlinks. */
429 }
430 else
431 rc = VERR_INVALID_PARAMETER;
432 }
433
434 ShClTransferListEntryDestroy(&entryList);
435 }
436
437 if ( RT_FAILURE(rc)
438 && pTransfer->Thread.fStop)
439 break;
440 }
441 }
442
443 ShClTransferListClose(pTransfer, hList);
444 }
445 }
446
447 ShClTransferListOpenParmsDestroy(&openParmsList);
448 }
449
450 if (RT_FAILURE(rc))
451 LogRel(("Shared Clipboard: Reading directory '%s' failed with %Rrc\n", strDir.c_str(), rc));
452
453 LogFlowFuncLeaveRC(rc);
454 return rc;
455}
456
457/**
458 * Thread for reading transfer data.
459 * The data object needs the (high level, root) transfer listing at the time of ::GetData(), so we need
460 * to block and wait until we have this data (via this thread) and continue.
461 *
462 * @returns VBox status code.
463 * @param ThreadSelf Thread handle. Unused at the moment.
464 * @param pvUser Pointer to user-provided data. Of type SharedClipboardWinDataObject.
465 */
466/* static */
467DECLCALLBACK(int) SharedClipboardWinDataObject::readThread(RTTHREAD ThreadSelf, void *pvUser)
468{
469 RT_NOREF(ThreadSelf);
470
471 LogFlowFuncEnter();
472
473 SharedClipboardWinDataObject *pThis = (SharedClipboardWinDataObject *)pvUser;
474
475 PSHCLTRANSFER pTransfer = pThis->m_pTransfer;
476 AssertPtr(pTransfer);
477
478 pTransfer->Thread.fStarted = true;
479 pTransfer->Thread.fStop = false;
480
481 RTThreadUserSignal(RTThreadSelf());
482
483 LogRel2(("Shared Clipboard: Calculating transfer ...\n"));
484
485 int rc = ShClTransferRootListRead(pTransfer);
486 if (RT_SUCCESS(rc))
487 {
488 uint64_t const cRoots = ShClTransferRootsCount(pTransfer);
489
490 LogFlowFunc(("cRoots=%RU64\n\n", cRoots));
491
492 for (uint32_t i = 0; i < cRoots; i++)
493 {
494 PCSHCLLISTENTRY pRootEntry = ShClTransferRootsEntryGet(pTransfer, i);
495
496 AssertBreakStmt(pRootEntry->cbInfo == sizeof(SHCLFSOBJINFO), rc = VERR_INVALID_PARAMETER);
497 PSHCLFSOBJINFO const pFsObjInfo = (PSHCLFSOBJINFO)pRootEntry->pvInfo;
498
499 LogFlowFunc(("pszRoot=%s, fMode=0x%x (type %#x)\n",
500 pRootEntry->pszName, pFsObjInfo->Attr.fMode, (pFsObjInfo->Attr.fMode & RTFS_TYPE_MASK)));
501
502 if (RTFS_IS_DIRECTORY(pFsObjInfo->Attr.fMode))
503 {
504 FSOBJENTRY objEntry;
505 objEntry.pszPath = RTStrDup(pRootEntry->pszName);
506 AssertPtrBreakStmt(objEntry.pszPath, rc = VERR_NO_MEMORY);
507 objEntry.objInfo = *pFsObjInfo;
508
509 pThis->lock();
510 pThis->m_lstEntries.push_back(objEntry); /** @todo Can this throw? */
511 pThis->unlock();
512
513 rc = pThis->readDir(pTransfer, pRootEntry->pszName);
514 }
515 else if (RTFS_IS_FILE(pFsObjInfo->Attr.fMode))
516 {
517 FSOBJENTRY objEntry;
518 objEntry.pszPath = RTStrDup(pRootEntry->pszName);
519 AssertPtrBreakStmt(objEntry.pszPath, rc = VERR_NO_MEMORY);
520 objEntry.objInfo = *pFsObjInfo;
521
522 pThis->lock();
523 pThis->m_lstEntries.push_back(objEntry); /** @todo Can this throw? */
524 pThis->unlock();
525 }
526 else
527 {
528 LogRel(("Shared Clipboard: Root entry '%s': File type %#x not supported\n",
529 pRootEntry->pszName, (pFsObjInfo->Attr.fMode & RTFS_TYPE_MASK)));
530 rc = VERR_NOT_SUPPORTED;
531 }
532
533 if (ASMAtomicReadBool(&pTransfer->Thread.fStop))
534 {
535 LogRel2(("Shared Clipboard: Stopping transfer calculation ...\n"));
536 break;
537 }
538
539 if (RT_FAILURE(rc))
540 break;
541 }
542
543 if ( RT_SUCCESS(rc)
544 && !ASMAtomicReadBool(&pTransfer->Thread.fStop))
545 {
546 LogRel2(("Shared Clipboard: Transfer calculation complete (%zu root entries)\n", pThis->m_lstEntries.size()));
547
548 /*
549 * Signal the "list complete" event so that this data object can return (valid) data via ::GetData().
550 * This in turn then will create IStream instances (by the OS) for each file system object to handle.
551 */
552 rc = RTSemEventSignal(pThis->m_EventListComplete);
553 if (RT_SUCCESS(rc))
554 {
555 pThis->lock();
556
557 AssertReleaseMsg(pThis->m_lstEntries.size(),
558 ("Shared Clipboard: No transfer root entries found -- should not happen, please file a bug report\n"));
559
560 LogRel2(("Shared Clipboard: Waiting for transfer to complete ...\n"));
561
562 for (;;)
563 {
564 pThis->unlock();
565
566 /* Transferring stuff can take a while, so don't use any timeout here. */
567 rc = RTSemEventWait(pThis->m_EventStatusChanged, RT_INDEFINITE_WAIT);
568
569 pThis->lock();
570
571 if (RT_FAILURE(rc))
572 break;
573
574 switch (pThis->m_enmStatus)
575 {
576 case Uninitialized: /* Can happen due to transfer erros. */
577 LogRel2(("Shared Clipboard: Data object was unitialized\n"));
578 break;
579
580 case Initialized:
581 AssertFailed(); /* State machine error -- debug this! */
582 break;
583
584 case Running:
585 continue;
586
587 case Completed:
588 LogRel2(("Shared Clipboard: Data object: Transfer complete\n"));
589 break;
590
591 case Canceled:
592 LogRel2(("Shared Clipboard: Data object: Transfer canceled\n"));
593 break;
594
595 case Error:
596 LogRel(("Shared Clipboard: Data object: Transfer error %Rrc occurred\n", pThis->m_rcStatus));
597 break;
598
599 default:
600 AssertFailed();
601 break;
602 }
603
604 break;
605 }
606
607 pThis->unlock();
608 }
609 }
610 }
611
612 if (RT_FAILURE(rc))
613 LogRel(("Shared Clipboard: Transfer failed with %Rrc\n", rc));
614
615 LogFlowFuncLeaveRC(rc);
616 return rc;
617}
618
619/**
620 * Creates a FILEGROUPDESCRIPTOR object from a given Shared Clipboard transfer and stores the result into an HGLOBAL object.
621 *
622 * @returns VBox status code.
623 * @param pTransfer Shared Clipboard transfer to create file grou desciprtor for.
624 * @param fUnicode Whether the FILEGROUPDESCRIPTOR object shall contain Unicode data or not.
625 * @param phGlobal Where to store the allocated HGLOBAL object on success.
626 */
627int SharedClipboardWinDataObject::createFileGroupDescriptorFromTransfer(PSHCLTRANSFER pTransfer,
628 bool fUnicode, HGLOBAL *phGlobal)
629{
630 AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
631 AssertPtrReturn(phGlobal, VERR_INVALID_POINTER);
632
633 LogFlowFuncEnter();
634
635 const size_t cbFileGroupDescriptor = fUnicode ? sizeof(FILEGROUPDESCRIPTORW) : sizeof(FILEGROUPDESCRIPTORA);
636 const size_t cbFileDescriptor = fUnicode ? sizeof(FILEDESCRIPTORW) : sizeof(FILEDESCRIPTORA);
637
638 const UINT cItems = (UINT)m_lstEntries.size(); /** UINT vs. size_t. */
639 if (!cItems)
640 return VERR_NOT_FOUND;
641
642 UINT curIdx = 0; /* Current index of the handled file group descriptor (FGD). */
643
644 const size_t cbFGD = cbFileGroupDescriptor + (cbFileDescriptor * (cItems - 1));
645
646 LogFunc(("fUnicode=%RTbool, cItems=%u, cbFileDescriptor=%zu\n", fUnicode, cItems, cbFileDescriptor));
647
648 /* FILEGROUPDESCRIPTORA / FILEGROUPDESCRIPTOR matches except the cFileName member (TCHAR vs. WCHAR). */
649 FILEGROUPDESCRIPTOR *pFGD = (FILEGROUPDESCRIPTOR *)RTMemAllocZ(cbFGD);
650 if (!pFGD)
651 return VERR_NO_MEMORY;
652
653 int rc = VINF_SUCCESS;
654
655 pFGD->cItems = cItems;
656
657 char *pszFileSpec = NULL;
658
659 FsObjEntryList::const_iterator itRoot = m_lstEntries.cbegin();
660 while (itRoot != m_lstEntries.end())
661 {
662 FILEDESCRIPTOR *pFD = &pFGD->fgd[curIdx];
663 RT_BZERO(pFD, cbFileDescriptor);
664
665 const char *pszFile = itRoot->pszPath;
666 AssertPtr(pszFile);
667
668 pszFileSpec = RTStrDup(pszFile);
669 AssertBreakStmt(pszFileSpec != NULL, rc = VERR_NO_MEMORY);
670
671 if (fUnicode)
672 {
673 PRTUTF16 pwszFileSpec;
674 rc = RTStrToUtf16(pszFileSpec, &pwszFileSpec);
675 if (RT_SUCCESS(rc))
676 {
677 rc = RTUtf16CopyEx((PRTUTF16 )pFD->cFileName, sizeof(pFD->cFileName) / sizeof(WCHAR),
678 pwszFileSpec, RTUtf16Len(pwszFileSpec));
679 RTUtf16Free(pwszFileSpec);
680
681 LogFlowFunc(("pFD->cFileNameW=%ls\n", pFD->cFileName));
682 }
683 }
684 else
685 {
686 rc = RTStrCopy(pFD->cFileName, sizeof(pFD->cFileName), pszFileSpec);
687 LogFlowFunc(("pFD->cFileNameA=%s\n", pFD->cFileName));
688 }
689
690 RTStrFree(pszFileSpec);
691 pszFileSpec = NULL;
692
693 if (RT_FAILURE(rc))
694 break;
695
696 pFD->dwFlags = FD_PROGRESSUI | FD_ATTRIBUTES;
697 if (fUnicode) /** @todo Only >= Vista. */
698 pFD->dwFlags |= FD_UNICODE;
699 pFD->dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
700
701 const SHCLFSOBJINFO *pObjInfo = &itRoot->objInfo;
702
703 if (RTFS_IS_DIRECTORY(pObjInfo->Attr.fMode))
704 {
705 pFD->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
706 }
707 else if (RTFS_IS_FILE(pObjInfo->Attr.fMode))
708 {
709 pFD->dwFlags |= FD_FILESIZE;
710
711 const uint64_t cbObjSize = pObjInfo->cbObject;
712
713 pFD->nFileSizeHigh = RT_HI_U32(cbObjSize);
714 pFD->nFileSizeLow = RT_LO_U32(cbObjSize);
715 }
716 else if (RTFS_IS_SYMLINK(pObjInfo->Attr.fMode))
717 {
718 /** @todo Implement. */
719 }
720#if 0 /** @todo Implement this. */
721 pFD->dwFlags = FD_ATTRIBUTES | FD_CREATETIME | FD_ACCESSTIME | FD_WRITESTIME | FD_FILESIZE;
722 pFD->dwFileAttributes =
723 pFD->ftCreationTime =
724 pFD->ftLastAccessTime =
725 pFD->ftLastWriteTime =
726#endif
727 ++curIdx;
728 ++itRoot;
729 }
730
731 if (pszFileSpec)
732 RTStrFree(pszFileSpec);
733
734 if (RT_SUCCESS(rc))
735 rc = copyToHGlobal(pFGD, cbFGD, GMEM_MOVEABLE, phGlobal);
736
737 RTMemFree(pFGD);
738
739 LogFlowFuncLeaveRC(rc);
740 return rc;
741}
742
743/**
744 * Retrieves the data stored in this object and store the result in pMedium.
745 *
746 * @return HRESULT
747 * @param pFormatEtc Format to retrieve.
748 * @param pMedium Where to store the data on success.
749 *
750 * @thread Windows event thread.
751 */
752STDMETHODIMP SharedClipboardWinDataObject::GetData(LPFORMATETC pFormatEtc, LPSTGMEDIUM pMedium)
753{
754 AssertPtrReturn(pFormatEtc, DV_E_FORMATETC);
755 AssertPtrReturn(pMedium, DV_E_FORMATETC);
756
757 lock();
758
759 LogFlowFunc(("lIndex=%RI32, enmStatus=%#x\n", pFormatEtc->lindex, m_enmStatus));
760
761 /* If the object is not ready (anymore), bail out early. */
762 if ( m_enmStatus != Initialized
763 && m_enmStatus != Running)
764 {
765 unlock();
766 return E_UNEXPECTED;
767 }
768
769 /*
770 * Initialize default values.
771 */
772 RT_BZERO(pMedium, sizeof(STGMEDIUM));
773
774 HRESULT hr = DV_E_FORMATETC; /* Play safe. */
775
776 int rc = VINF_SUCCESS;
777
778 /* Pre-check -- see if the data object still is alive. */
779 if (m_enmStatus == Uninitialized)
780 rc = VERR_OBJECT_DESTROYED;
781
782 if ( RT_SUCCESS(rc)
783 && ( pFormatEtc->cfFormat == m_cfFileDescriptorA
784#ifdef VBOX_CLIPBOARD_WITH_UNICODE_SUPPORT
785 || pFormatEtc->cfFormat == m_cfFileDescriptorW
786#endif
787 )
788 )
789 {
790 switch (m_enmStatus)
791 {
792 case Initialized:
793 {
794 LogRel2(("Shared Clipboard: Requesting data for IDataObject ...\n"));
795
796 /* Leave lock while requesting + waiting. */
797 unlock();
798
799 /* Start the transfer. */
800 AssertPtrBreak(m_Callbacks.pfnTransferStart);
801 rc = m_Callbacks.pfnTransferStart(&m_CallbackCtx);
802 AssertRCBreak(rc);
803
804 LogRel2(("Shared Clipboard: Waiting for IDataObject started status ...\n"));
805
806 /* Note: Keep the timeout low here (instead of using SHCL_TIMEOUT_DEFAULT_MS), as this will make
807 * Windows Explorer unresponsive (i.e. "ghost window") when waiting for too long. */
808 rc = RTSemEventWait(m_EventStatusChanged, RT_MS_10SEC);
809
810 /* Re-acquire lock. */
811 lock();
812
813 if (RT_FAILURE(rc))
814 {
815 LogRel(("Shared Clipboard: Waiting for IDataObject status status failed, rc=%Rrc\n", rc));
816 break;
817 }
818
819 if (m_enmStatus != Running)
820 {
821 LogRel(("Shared Clipboard: Received wrong IDataObject status (%#x)\n", m_enmStatus));
822 rc = VERR_WRONG_ORDER;
823 break;
824 }
825
826 /* There now must be a transfer assigned. */
827 AssertPtrBreakStmt(m_pTransfer, rc = VERR_WRONG_ORDER);
828
829 RT_FALL_THROUGH();
830 }
831
832 case Running:
833 {
834 const bool fUnicode = pFormatEtc->cfFormat == m_cfFileDescriptorW;
835
836 SHCLTRANSFERSTATUS const enmTransferStatus = ShClTransferGetStatus(m_pTransfer);
837 RT_NOREF(enmTransferStatus);
838
839 LogFlowFunc(("FormatIndex_FileDescriptor%s, enmTransferStatus=%s\n",
840 fUnicode ? "W" : "A", ShClTransferStatusToStr(enmTransferStatus)));
841
842 /* The caller can call GetData() several times, so make sure we don't do the same transfer multiple times. */
843 if (ShClTransferGetStatus(m_pTransfer) != SHCLTRANSFERSTATUS_STARTED)
844 {
845 /* Start the transfer + run it asynchronously in a separate thread. */
846 rc = ShClTransferStart(m_pTransfer);
847 if (RT_SUCCESS(rc))
848 {
849 rc = ShClTransferRun(m_pTransfer, &SharedClipboardWinDataObject::readThread, this /* pvUser */);
850 if (RT_SUCCESS(rc))
851 {
852 /* Leave lock while waiting. */
853 unlock();
854
855 /* Don't block for too long here, as this also will screw other apps running on the OS. */
856 LogRel2(("Shared Clipboard: Waiting for IDataObject listing to arrive ...\n"));
857 rc = RTSemEventWait(m_EventListComplete, RT_MS_10SEC);
858
859 /* Re-acquire lock. */
860 lock();
861
862 if ( m_pTransfer == NULL
863 || m_enmStatus != Running) /* Still in running state? */
864 {
865 rc = VERR_OBJECT_DESTROYED;
866 break;
867 }
868 }
869 }
870 }
871
872 if (RT_SUCCESS(rc))
873 {
874 HGLOBAL hGlobal;
875 rc = createFileGroupDescriptorFromTransfer(m_pTransfer, fUnicode, &hGlobal);
876 if (RT_SUCCESS(rc))
877 {
878 pMedium->tymed = TYMED_HGLOBAL;
879 pMedium->hGlobal = hGlobal;
880 /* Note: hGlobal now is being owned by pMedium / the caller. */
881
882 hr = S_OK;
883 }
884 }
885
886 break;
887 }
888
889 default:
890 AssertFailedStmt(rc = VERR_STATE_CHANGED);
891 break;
892 }
893
894 if (RT_FAILURE(rc))
895 {
896 LogRel(("Shared Clipboard: Error getting data for IDataObject, rc=%Rrc\n", rc));
897 hr = E_UNEXPECTED; /* We can't tell any better to the caller, unfortunately. */
898 }
899 }
900
901 Log2Func(("enmStatus=%#x, pTransfer=%p, rc=%Rrc\n", m_enmStatus, m_pTransfer, rc));
902
903 if (RT_SUCCESS(rc))
904 {
905 if (pFormatEtc->cfFormat == m_cfFileContents)
906 {
907 if ( pFormatEtc->lindex >= 0
908 && (ULONG)pFormatEtc->lindex < m_lstEntries.size())
909 {
910 m_uObjIdx = pFormatEtc->lindex; /* lIndex of FormatEtc contains the actual index to the object being handled. */
911
912 FSOBJENTRY &fsObjEntry = m_lstEntries.at(m_uObjIdx);
913
914 LogFlowFunc(("FormatIndex_FileContents: m_uObjIdx=%u (entry '%s')\n", m_uObjIdx, fsObjEntry.pszPath));
915
916 LogRel2(("Shared Clipboard: Receiving object '%s' ...\n", fsObjEntry.pszPath));
917
918 /* Hand-in the provider so that our IStream implementation can continue working with it. */
919 hr = SharedClipboardWinStreamImpl::Create(this /* pParent */, m_pTransfer,
920 fsObjEntry.pszPath /* File name */, &fsObjEntry.objInfo /* PSHCLFSOBJINFO */,
921 &m_pStream);
922 if (SUCCEEDED(hr))
923 {
924 /* Hand over the stream to the caller. */
925 pMedium->tymed = TYMED_ISTREAM;
926 pMedium->pstm = m_pStream;
927 }
928 }
929 }
930 else if (pFormatEtc->cfFormat == m_cfPerformedDropEffect)
931 {
932 HGLOBAL hGlobal = GlobalAlloc(GHND, sizeof(DWORD));
933
934 DWORD* pdwDropEffect = (DWORD*)GlobalLock(hGlobal);
935 *pdwDropEffect = DROPEFFECT_COPY;
936
937 GlobalUnlock(hGlobal);
938
939 pMedium->tymed = TYMED_HGLOBAL;
940 pMedium->hGlobal = hGlobal;
941 pMedium->pUnkForRelease = NULL;
942 }
943
944 if ( FAILED(hr)
945 && hr != DV_E_FORMATETC) /* Can happen if the caller queries unknown / unhandled formats. */
946 {
947 LogRel(("Shared Clipboard: Error returning data from data object (%Rhrc)\n", hr));
948 }
949 }
950
951 unlock();
952
953 LogFlowFunc(("LEAVE hr=%Rhrc\n", hr));
954 return hr;
955}
956
957/**
958 * Only required for IStream / IStorage interfaces.
959 *
960 * @return IPRT status code.
961 * @return HRESULT
962 * @param pFormatEtc
963 * @param pMedium
964 */
965STDMETHODIMP SharedClipboardWinDataObject::GetDataHere(LPFORMATETC pFormatEtc, LPSTGMEDIUM pMedium)
966{
967 RT_NOREF(pFormatEtc, pMedium);
968 LogFlowFunc(("\n"));
969 return E_NOTIMPL;
970}
971
972/**
973 * Query if this objects supports a specific format.
974 *
975 * @return IPRT status code.
976 * @return HRESULT
977 * @param pFormatEtc
978 */
979STDMETHODIMP SharedClipboardWinDataObject::QueryGetData(LPFORMATETC pFormatEtc)
980{
981 LogFlowFunc(("\n"));
982 return lookupFormatEtc(pFormatEtc, NULL /* puIndex */) ? S_OK : DV_E_FORMATETC;
983}
984
985STDMETHODIMP SharedClipboardWinDataObject::GetCanonicalFormatEtc(LPFORMATETC pFormatEtc, LPFORMATETC pFormatEtcOut)
986{
987 RT_NOREF(pFormatEtc);
988 LogFlowFunc(("\n"));
989
990 /* Set this to NULL in any case. */
991 pFormatEtcOut->ptd = NULL;
992 return E_NOTIMPL;
993}
994
995STDMETHODIMP SharedClipboardWinDataObject::SetData(LPFORMATETC pFormatEtc, LPSTGMEDIUM pMedium, BOOL fRelease)
996{
997 if ( pFormatEtc == NULL
998 || pMedium == NULL)
999 return E_INVALIDARG;
1000
1001 if (pFormatEtc->lindex != -1)
1002 return DV_E_LINDEX;
1003
1004 if (pFormatEtc->tymed != TYMED_HGLOBAL)
1005 return DV_E_TYMED;
1006
1007 if (pFormatEtc->dwAspect != DVASPECT_CONTENT)
1008 return DV_E_DVASPECT;
1009
1010 LogFlowFunc(("cfFormat=%RU16, lookupFormatEtc=%RTbool\n",
1011 pFormatEtc->cfFormat, lookupFormatEtc(pFormatEtc, NULL /* puIndex */)));
1012
1013 /* CFSTR_PERFORMEDDROPEFFECT is used by the drop target (caller of this IDataObject) to communicate
1014 * the outcome of the overall operation. */
1015 if ( pFormatEtc->cfFormat == m_cfPerformedDropEffect
1016 && pMedium->tymed == TYMED_HGLOBAL)
1017 {
1018 DWORD dwEffect = *(DWORD *)GlobalLock(pMedium->hGlobal);
1019 GlobalUnlock(pMedium->hGlobal);
1020
1021 LogFlowFunc(("dwEffect=%RI32\n", dwEffect));
1022
1023 /* Did the user cancel the operation via UI (shell)? This also might happen when overwriting an existing file
1024 * and the user doesn't want to allow this. */
1025 if (dwEffect == DROPEFFECT_NONE)
1026 {
1027 LogRel2(("Shared Clipboard: Transfer canceled by user interaction\n"));
1028
1029 SetStatus(Canceled);
1030 }
1031 /** @todo Detect move / overwrite actions here. */
1032
1033 if (fRelease)
1034 ReleaseStgMedium(pMedium);
1035
1036 return S_OK;
1037 }
1038
1039 return E_NOTIMPL;
1040}
1041
1042STDMETHODIMP SharedClipboardWinDataObject::EnumFormatEtc(DWORD dwDirection, IEnumFORMATETC **ppEnumFormatEtc)
1043{
1044 LogFlowFunc(("dwDirection=%RI32, mcFormats=%RI32, mpFormatEtc=%p\n", dwDirection, m_cFormats, m_pFormatEtc));
1045
1046 HRESULT hr;
1047 if (dwDirection == DATADIR_GET)
1048 hr = SharedClipboardWinEnumFormatEtc::CreateEnumFormatEtc(m_cFormats, m_pFormatEtc, ppEnumFormatEtc);
1049 else
1050 hr = E_NOTIMPL;
1051
1052 LogFlowFunc(("hr=%Rhrc\n", hr));
1053 return hr;
1054}
1055
1056STDMETHODIMP SharedClipboardWinDataObject::DAdvise(LPFORMATETC pFormatEtc, DWORD fAdvise, IAdviseSink *pAdvSink, DWORD *pdwConnection)
1057{
1058 RT_NOREF(pFormatEtc, fAdvise, pAdvSink, pdwConnection);
1059 return OLE_E_ADVISENOTSUPPORTED;
1060}
1061
1062STDMETHODIMP SharedClipboardWinDataObject::DUnadvise(DWORD dwConnection)
1063{
1064 RT_NOREF(dwConnection);
1065 return OLE_E_ADVISENOTSUPPORTED;
1066}
1067
1068STDMETHODIMP SharedClipboardWinDataObject::EnumDAdvise(IEnumSTATDATA **ppEnumAdvise)
1069{
1070 RT_NOREF(ppEnumAdvise);
1071 return OLE_E_ADVISENOTSUPPORTED;
1072}
1073
1074#ifdef VBOX_WITH_SHARED_CLIPBOARD_WIN_ASYNC
1075/*
1076 * IDataObjectAsyncCapability methods.
1077 */
1078
1079STDMETHODIMP SharedClipboardWinDataObject::EndOperation(HRESULT hResult, IBindCtx *pbcReserved, DWORD dwEffects)
1080{
1081 RT_NOREF(hResult, pbcReserved, dwEffects);
1082 return E_NOTIMPL;
1083}
1084
1085STDMETHODIMP SharedClipboardWinDataObject::GetAsyncMode(BOOL *pfIsOpAsync)
1086{
1087 RT_NOREF(pfIsOpAsync);
1088 return E_NOTIMPL;
1089}
1090
1091STDMETHODIMP SharedClipboardWinDataObject::InOperation(BOOL *pfInAsyncOp)
1092{
1093 RT_NOREF(pfInAsyncOp);
1094 return E_NOTIMPL;
1095}
1096
1097STDMETHODIMP SharedClipboardWinDataObject::SetAsyncMode(BOOL fDoOpAsync)
1098{
1099 RT_NOREF(fDoOpAsync);
1100 return E_NOTIMPL;
1101}
1102
1103STDMETHODIMP SharedClipboardWinDataObject::StartOperation(IBindCtx *pbcReserved)
1104{
1105 RT_NOREF(pbcReserved);
1106 return E_NOTIMPL;
1107}
1108#endif /* VBOX_WITH_SHARED_CLIPBOARD_WIN_ASYNC */
1109
1110/*
1111 * Own stuff.
1112 */
1113
1114/**
1115 * Assigns a transfer object for the data object, internal version.
1116 *
1117 * @returns VBox status code.
1118 * @param pTransfer Transfer to assign.
1119 * Must be in INITIALIZED state.
1120 * When set to NULL, the transfer will be released from the object.
1121 */
1122int SharedClipboardWinDataObject::setTransferLocked(PSHCLTRANSFER pTransfer)
1123{
1124 AssertReturn(RTCritSectIsOwned(&m_CritSect), VERR_WRONG_ORDER);
1125
1126 LogFlowFunc(("pTransfer=%p\n", pTransfer));
1127
1128 int rc = VINF_SUCCESS;
1129
1130 if (pTransfer) /* Set */
1131 {
1132 Assert(m_pTransfer == NULL); /* Transfer already set? */
1133
1134 if (m_enmStatus == Initialized)
1135 {
1136 SHCLTRANSFERSTATUS const enmSts = ShClTransferGetStatus(pTransfer);
1137 AssertMsgStmt(enmSts == SHCLTRANSFERSTATUS_INITIALIZED, /* Transfer must not be started yet. */
1138 ("Transfer has wrong status (%#x)\n", enmSts), rc = VERR_WRONG_ORDER);
1139 if (RT_SUCCESS(rc))
1140 {
1141 m_pTransfer = pTransfer;
1142
1143 SharedClipboardWinTransferCtx *pWinURITransferCtx = (SharedClipboardWinTransferCtx *)pTransfer->pvUser;
1144 AssertPtr(pWinURITransferCtx);
1145
1146 pWinURITransferCtx->pDataObj = this; /* Save a backref to this object. */
1147
1148 ShClTransferAcquire(pTransfer);
1149 }
1150 }
1151 else
1152 AssertFailedStmt(rc = VERR_WRONG_ORDER);
1153 }
1154 else /* Unset */
1155 {
1156 if (m_pTransfer)
1157 {
1158 SharedClipboardWinTransferCtx *pWinURITransferCtx = (SharedClipboardWinTransferCtx *)m_pTransfer->pvUser;
1159 AssertPtr(pWinURITransferCtx);
1160
1161 pWinURITransferCtx->pDataObj = NULL; /* Release backref to this object. */
1162
1163 ShClTransferRelease(m_pTransfer);
1164 m_pTransfer = NULL;
1165
1166 /* Make sure to notify any waiters. */
1167 rc = RTSemEventSignal(m_EventListComplete);
1168 AssertRC(rc);
1169 }
1170 }
1171
1172 return rc;
1173}
1174
1175/**
1176 * Assigns a transfer object for the data object.
1177 *
1178 * @returns VBox status code.
1179 * @param pTransfer Transfer to assign.
1180 * Must be in INITIALIZED state.
1181 * When set to NULL, the transfer will be released from the object.
1182 */
1183int SharedClipboardWinDataObject::SetTransfer(PSHCLTRANSFER pTransfer)
1184{
1185 lock();
1186
1187 int rc = setTransferLocked(pTransfer);
1188
1189 unlock();
1190
1191 return rc;
1192}
1193
1194/**
1195 * Sets a new status to the data object and signals its waiter.
1196 *
1197 * @returns VBox status code.
1198 * @param enmStatus New status to signal.
1199 * @param rcSts Result code. Optional.
1200 *
1201 * @note Called by the main clipboard thread + SharedClipboardWinStreamImpl.
1202 */
1203int SharedClipboardWinDataObject::SetStatus(Status enmStatus, int rcSts /* = VINF_SUCCESS */)
1204{
1205 lock();
1206
1207 int rc = setStatusLocked(enmStatus, rcSts);
1208
1209 unlock();
1210 return rc;
1211}
1212
1213/* static */
1214void SharedClipboardWinDataObject::logFormat(CLIPFORMAT fmt)
1215{
1216 char szFormat[128];
1217 if (GetClipboardFormatName(fmt, szFormat, sizeof(szFormat)))
1218 {
1219 LogFlowFunc(("clipFormat=%RI16 -> %s\n", fmt, szFormat));
1220 }
1221 else
1222 LogFlowFunc(("clipFormat=%RI16 is unknown\n", fmt));
1223}
1224
1225bool SharedClipboardWinDataObject::lookupFormatEtc(LPFORMATETC pFormatEtc, ULONG *puIndex)
1226{
1227 AssertReturn(pFormatEtc, false);
1228 /* puIndex is optional. */
1229
1230 for (ULONG i = 0; i < m_cFormats; i++)
1231 {
1232 if( (pFormatEtc->tymed & m_pFormatEtc[i].tymed)
1233 && pFormatEtc->cfFormat == m_pFormatEtc[i].cfFormat)
1234 /* Note: Do *not* compare dwAspect here, as this can be dynamic, depending on how the object should be represented. */
1235 //&& pFormatEtc->dwAspect == m_pFormatEtc[i].dwAspect)
1236 {
1237 LogRel2(("Shared Clipboard: Format found: tyMed=%RI32, cfFormat=%RI16, dwAspect=%RI32, ulIndex=%RU32\n",
1238 pFormatEtc->tymed, pFormatEtc->cfFormat, pFormatEtc->dwAspect, i));
1239 if (puIndex)
1240 *puIndex = i;
1241 return true;
1242 }
1243 }
1244
1245 LogRel2(("Shared Clipboard: Format NOT found: tyMed=%RI32, cfFormat=%RI16, dwAspect=%RI32\n",
1246 pFormatEtc->tymed, pFormatEtc->cfFormat, pFormatEtc->dwAspect));
1247
1248 logFormat(pFormatEtc->cfFormat);
1249
1250 return false;
1251}
1252
1253void SharedClipboardWinDataObject::registerFormat(LPFORMATETC pFormatEtc, CLIPFORMAT clipFormat,
1254 TYMED tyMed, LONG lIndex, DWORD dwAspect,
1255 DVTARGETDEVICE *pTargetDevice)
1256{
1257 AssertPtr(pFormatEtc);
1258
1259 pFormatEtc->cfFormat = clipFormat;
1260 pFormatEtc->tymed = tyMed;
1261 pFormatEtc->lindex = lIndex;
1262 pFormatEtc->dwAspect = dwAspect;
1263 pFormatEtc->ptd = pTargetDevice;
1264
1265 LogFlowFunc(("Registered format=%ld\n", pFormatEtc->cfFormat));
1266
1267 logFormat(pFormatEtc->cfFormat);
1268}
1269
1270/**
1271 * Sets a new status to the data object and signals its waiter.
1272 *
1273 * @returns VBox status code.
1274 * @param enmStatus New status to signal.
1275 * @param rc Result code. Optional.
1276 * Errors only accepted when status also is 'Error'.
1277 *
1278 * @note Caller must have taken the critical section.
1279 */
1280int SharedClipboardWinDataObject::setStatusLocked(Status enmStatus, int rc /* = VINF_SUCCESS */)
1281{
1282 AssertReturn(enmStatus == Error || RT_SUCCESS(rc), VERR_INVALID_PARAMETER);
1283 AssertReturn(RTCritSectIsOwned(&m_CritSect), VERR_WRONG_ORDER);
1284
1285 LogFlowFunc(("enmStatus=%#x, rc=%Rrc (current is: %#x)\n", enmStatus, rc, m_enmStatus));
1286
1287 int rc2 = VINF_SUCCESS;
1288
1289 m_rcStatus = rc;
1290
1291 switch (enmStatus)
1292 {
1293 case Completed:
1294 {
1295 LogFlowFunc(("m_uObjIdx=%RU32 (total: %zu)\n", m_uObjIdx, m_lstEntries.size()));
1296
1297 const bool fComplete = m_uObjIdx == m_lstEntries.size() - 1 /* Object index is zero-based */;
1298 if (fComplete)
1299 m_enmStatus = Completed;
1300 break;
1301 }
1302
1303 default:
1304 {
1305 m_enmStatus = enmStatus;
1306 break;
1307 }
1308 }
1309
1310 if (RT_FAILURE(rc))
1311 LogRel(("Shared Clipboard: Data object received error %Rrc (status %#x)\n", rc, enmStatus));
1312
1313 if (m_EventStatusChanged != NIL_RTSEMEVENT)
1314 rc2 = RTSemEventSignal(m_EventStatusChanged);
1315
1316 return rc2;
1317}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette