VirtualBox

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

Last change on this file since 100664 was 100664, checked in by vboxsync, 15 months ago

Shared Clipboard: Windows data object locking fixes. bugref:9437

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