VirtualBox

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

Last change on this file since 100484 was 100461, checked in by vboxsync, 19 months ago

Shared Clipboard: More fine-grained control for Windows data object uninit vs. destruction. bugref:9437

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