VirtualBox

source: vbox/trunk/src/VBox/Main/include/GuestDnDPrivate.h@ 108632

Last change on this file since 108632 was 108632, checked in by vboxsync, 4 weeks ago

Main/include/GuestDnDPrivate.h: Make it work for hosts where we don't know the host page size when building, bugref:10391

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 30.8 KB
Line 
1/* $Id: GuestDnDPrivate.h 108632 2025-03-20 11:18:13Z vboxsync $ */
2/** @file
3 * Private guest drag and drop code, used by GuestDnDTarget +
4 * GuestDnDSource.
5 */
6
7/*
8 * Copyright (C) 2011-2024 Oracle and/or its affiliates.
9 *
10 * This file is part of VirtualBox base platform packages, as
11 * available from https://www.virtualbox.org.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation, in version 3 of the
16 * License.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, see <https://www.gnu.org/licenses>.
25 *
26 * SPDX-License-Identifier: GPL-3.0-only
27 */
28
29#ifndef MAIN_INCLUDED_GuestDnDPrivate_h
30#define MAIN_INCLUDED_GuestDnDPrivate_h
31#ifndef RT_WITHOUT_PRAGMA_ONCE
32# pragma once
33#endif
34
35#include <iprt/dir.h>
36#include <iprt/file.h>
37#include <iprt/path.h>
38#include <iprt/system.h>
39
40#include <VBox/hgcmsvc.h> /* For PVBOXHGCMSVCPARM. */
41#include <VBox/GuestHost/DragAndDrop.h>
42#include <VBox/GuestHost/DragAndDropDefs.h>
43#include <VBox/HostServices/DragAndDropSvc.h>
44
45/**
46 * Forward prototype declarations.
47 */
48class Guest;
49class GuestDnDBase;
50class GuestDnDState;
51class GuestDnDSource;
52class GuestDnDTarget;
53class Progress;
54
55/**
56 * Type definitions.
57 */
58
59/** List (vector) of MIME types. */
60typedef std::vector<com::Utf8Str> GuestDnDMIMEList;
61
62/**
63 * Class to handle a guest DnD callback event.
64 */
65class GuestDnDCallbackEvent
66{
67public:
68
69 GuestDnDCallbackEvent(void)
70 : m_SemEvent(NIL_RTSEMEVENT)
71 , m_vrc(VINF_SUCCESS) { }
72
73 virtual ~GuestDnDCallbackEvent(void);
74
75public:
76
77 int Reset(void);
78
79 int Notify(int vrc = VINF_SUCCESS);
80
81 int Result(void) const { return m_vrc; }
82
83 int Wait(RTMSINTERVAL msTimeout);
84
85protected:
86
87 /** Event semaphore to notify on error/completion. */
88 RTSEMEVENT m_SemEvent;
89 /** Callback result. */
90 int m_vrc;
91};
92
93/**
94 * Struct for handling the (raw) meta data.
95 */
96struct GuestDnDMetaData
97{
98 GuestDnDMetaData(void)
99 : pvData(NULL)
100 , cbData(0)
101 , cbAllocated(0)
102 , cbAnnounced(0) { }
103
104 virtual ~GuestDnDMetaData(void)
105 {
106 reset();
107 }
108
109 /**
110 * Adds new meta data.
111 *
112 * @returns New (total) meta data size in bytes.
113 * @param pvDataAdd Pointer of data to add.
114 * @param cbDataAdd Size (in bytes) of data to add.
115 */
116 size_t add(const void *pvDataAdd, size_t cbDataAdd)
117 {
118 LogFlowThisFunc(("cbAllocated=%zu, cbAnnounced=%zu, pvDataAdd=%p, cbDataAdd=%zu\n",
119 cbAllocated, cbAnnounced, pvDataAdd, cbDataAdd));
120 if (!cbDataAdd)
121 return 0;
122 AssertPtrReturn(pvDataAdd, 0);
123
124 const size_t cbAllocatedTmp = cbData + cbDataAdd;
125 if (cbAllocatedTmp > cbAllocated)
126 {
127 int vrc = resize(cbAllocatedTmp);
128 if (RT_FAILURE(vrc))
129 return 0;
130 }
131
132 Assert(cbAllocated >= cbData + cbDataAdd);
133 memcpy((uint8_t *)pvData + cbData, pvDataAdd, cbDataAdd);
134
135 cbData += cbDataAdd;
136 cbAnnounced = cbData;
137
138 return cbData;
139 }
140
141 /**
142 * Adds new meta data.
143 *
144 * @returns New (total) meta data size in bytes.
145 * @param vecAdd Meta data to add.
146 */
147 size_t add(const std::vector<BYTE> &vecAdd)
148 {
149 if (!vecAdd.size())
150 return 0;
151
152 if (vecAdd.size() > UINT32_MAX) /* Paranoia. */
153 return 0;
154
155 return add(&vecAdd.front(), (uint32_t)vecAdd.size());
156 }
157
158 /**
159 * Resets (clears) all data.
160 */
161 void reset(void)
162 {
163 strFmt = "";
164
165 if (pvData)
166 {
167 Assert(cbAllocated);
168 RTMemFree(pvData);
169 pvData = NULL;
170 }
171
172 cbData = 0;
173 cbAllocated = 0;
174 cbAnnounced = 0;
175 }
176
177 /**
178 * Resizes the allocation size.
179 *
180 * @returns VBox status code.
181 * @param cbSize New allocation size (in bytes).
182 */
183 int resize(size_t cbSize)
184 {
185 if (!cbSize)
186 {
187 reset();
188 return VINF_SUCCESS;
189 }
190
191 if (cbSize == cbAllocated)
192 return VINF_SUCCESS;
193
194 cbSize = RT_ALIGN_Z(cbSize, RTSystemGetPageSize());
195
196 if (cbSize > _32M) /* Meta data can be up to 32MB. */
197 return VERR_BUFFER_OVERFLOW;
198
199 void *pvTmp = NULL;
200 if (!cbAllocated)
201 {
202 Assert(cbData == 0);
203 pvTmp = RTMemAllocZ(cbSize);
204 }
205 else
206 {
207 AssertPtr(pvData);
208 pvTmp = RTMemRealloc(pvData, cbSize);
209 }
210
211 if (pvTmp)
212 {
213 pvData = pvTmp;
214 cbAllocated = cbSize;
215 return VINF_SUCCESS;
216 }
217
218 return VERR_NO_MEMORY;
219 }
220
221 /** Format string of this meta data. */
222 com::Utf8Str strFmt;
223 /** Pointer to allocated meta data. */
224 void *pvData;
225 /** Used bytes of meta data. Must not exceed cbAllocated. */
226 size_t cbData;
227 /** Size (in bytes) of allocated meta data. */
228 size_t cbAllocated;
229 /** Size (in bytes) of announced meta data. */
230 size_t cbAnnounced;
231};
232
233/**
234 * Struct for accounting shared DnD data to be sent/received.
235 */
236struct GuestDnDData
237{
238 GuestDnDData(void)
239 : cbExtra(0)
240 , cbProcessed(0) { }
241
242 virtual ~GuestDnDData(void)
243 {
244 reset();
245 }
246
247 /**
248 * Adds processed data to the internal accounting.
249 *
250 * @returns New processed data size.
251 * @param cbDataAdd Bytes to add as done processing.
252 */
253 size_t addProcessed(size_t cbDataAdd)
254 {
255 const size_t cbTotal = getTotalAnnounced(); RT_NOREF(cbTotal);
256 AssertReturn(cbProcessed + cbDataAdd <= cbTotal, 0);
257 cbProcessed += cbDataAdd;
258 return cbProcessed;
259 }
260
261 /**
262 * Returns whether all data has been processed or not.
263 *
264 * @returns \c true if all data has been processed, \c false if not.
265 */
266 bool isComplete(void) const
267 {
268 const size_t cbTotal = getTotalAnnounced();
269 LogFlowFunc(("cbProcessed=%zu, cbTotal=%zu\n", cbProcessed, cbTotal));
270 AssertReturn(cbProcessed <= cbTotal, true);
271 return (cbProcessed == cbTotal);
272 }
273
274 /**
275 * Returns the percentage (0-100) of the already processed data.
276 *
277 * @returns Percentage (0-100) of the already processed data.
278 */
279 uint8_t getPercentComplete(void) const
280 {
281 const size_t cbTotal = getTotalAnnounced();
282 return (uint8_t)(cbProcessed * 100 / RT_MAX(cbTotal, 1));
283 }
284
285 /**
286 * Returns the remaining (outstanding) data left for processing.
287 *
288 * @returns Remaining (outstanding) data (in bytes) left for processing.
289 */
290 size_t getRemaining(void) const
291 {
292 const size_t cbTotal = getTotalAnnounced();
293 AssertReturn(cbProcessed <= cbTotal, 0);
294 return cbTotal - cbProcessed;
295 }
296
297 /**
298 * Returns the total data size (in bytes) announced.
299 *
300 * @returns Total data size (in bytes) announced.
301 */
302 size_t getTotalAnnounced(void) const
303 {
304 return Meta.cbAnnounced + cbExtra;
305 }
306
307 /**
308 * Returns the total data size (in bytes) available.
309 * For receiving data, this represents the already received data.
310 * For sending data, this represents the data left to send.
311 *
312 * @returns Total data size (in bytes) available.
313 */
314 size_t getTotalAvailable(void) const
315 {
316 return Meta.cbData + cbExtra;
317 }
318
319 /**
320 * Resets all data.
321 */
322 void reset(void)
323 {
324 Meta.reset();
325
326 cbExtra = 0;
327 cbProcessed = 0;
328 }
329
330 /** For storing the actual meta data.
331 * This might be an URI list or just plain raw data,
332 * according to the format being sent. */
333 GuestDnDMetaData Meta;
334 /** Extra data to send/receive (in bytes). Can be 0 for raw data.
335 * For (file) transfers this is the total size for all files. */
336 size_t cbExtra;
337 /** Overall size (in bytes) of processed data. */
338 size_t cbProcessed;
339};
340
341/** Initial object context state / no state set. */
342#define DND_OBJ_STATE_NONE 0
343/** The header was received / sent. */
344#define DND_OBJ_STATE_HAS_HDR RT_BIT(0)
345/** Validation mask for object context state. */
346#define DND_OBJ_STATE_VALID_MASK UINT32_C(0x00000001)
347
348/**
349 * Base class for keeping around DnD (file) transfer data.
350 * Used for sending / receiving transfer data.
351 */
352struct GuestDnDTransferData
353{
354
355public:
356
357 GuestDnDTransferData(void)
358 : cObjToProcess(0)
359 , cObjProcessed(0)
360 , pvScratchBuf(NULL)
361 , cbScratchBuf(0) { }
362
363 virtual ~GuestDnDTransferData(void)
364 {
365 destroy();
366 }
367
368 /**
369 * Initializes a transfer data object.
370 *
371 * @param cbBuf Scratch buffer size (in bytes) to use.
372 * If not specified, DND_DEFAULT_CHUNK_SIZE will be used.
373 */
374 int init(size_t cbBuf = DND_DEFAULT_CHUNK_SIZE)
375 {
376 reset();
377
378 pvScratchBuf = RTMemAlloc(cbBuf);
379 if (!pvScratchBuf)
380 return VERR_NO_MEMORY;
381
382 cbScratchBuf = cbBuf;
383 return VINF_SUCCESS;
384 }
385
386 /**
387 * Destroys a transfer data object.
388 */
389 void destroy(void)
390 {
391 reset();
392
393 if (pvScratchBuf)
394 {
395 Assert(cbScratchBuf);
396 RTMemFree(pvScratchBuf);
397 pvScratchBuf = NULL;
398 }
399 cbScratchBuf = 0;
400 }
401
402 /**
403 * Resets a transfer data object.
404 */
405 void reset(void)
406 {
407 LogFlowFuncEnter();
408
409 cObjToProcess = 0;
410 cObjProcessed = 0;
411 }
412
413 /**
414 * Returns whether this transfer object is complete or not.
415 *
416 * @returns \c true if complete, \c false if not.
417 */
418 bool isComplete(void) const
419 {
420 return (cObjProcessed == cObjToProcess);
421 }
422
423 /** Number of objects to process. */
424 uint64_t cObjToProcess;
425 /** Number of objects already processed. */
426 uint64_t cObjProcessed;
427 /** Pointer to an optional scratch buffer to use for
428 * doing the actual chunk transfers. */
429 void *pvScratchBuf;
430 /** Size (in bytes) of scratch buffer. */
431 size_t cbScratchBuf;
432};
433
434/**
435 * Class for keeping around DnD transfer send data (Host -> Guest).
436 */
437struct GuestDnDTransferSendData : public GuestDnDTransferData
438{
439 GuestDnDTransferSendData()
440 : fObjState(0)
441 {
442 RT_ZERO(List);
443 int vrc2 = DnDTransferListInit(&List);
444 AssertRC(vrc2);
445 }
446
447 virtual ~GuestDnDTransferSendData()
448 {
449 destroy();
450 }
451
452 /**
453 * Destroys the object.
454 */
455 void destroy(void)
456 {
457 DnDTransferListDestroy(&List);
458 }
459
460 /**
461 * Resets the object.
462 */
463 void reset(void)
464 {
465 DnDTransferListReset(&List);
466 fObjState = 0;
467
468 GuestDnDTransferData::reset();
469 }
470
471 /** Transfer List to handle. */
472 DNDTRANSFERLIST List;
473 /** Current state of object in transfer.
474 * This is needed for keeping compatibility to old(er) DnD HGCM protocols.
475 *
476 * At the moment we only support transferring one object at a time. */
477 uint32_t fObjState;
478};
479
480/**
481 * Context structure for sending data to the guest.
482 */
483struct GuestDnDSendCtx : public GuestDnDData
484{
485 GuestDnDSendCtx(void);
486
487 /**
488 * Resets the object.
489 */
490 void reset(void);
491
492 /** Pointer to guest target class this context belongs to. */
493 GuestDnDTarget *pTarget;
494 /** Pointer to guest state this context belongs to. */
495 GuestDnDState *pState;
496 /** Target (VM) screen ID. */
497 uint32_t uScreenID;
498 /** Transfer data structure. */
499 GuestDnDTransferSendData Transfer;
500 /** Callback event to use. */
501 GuestDnDCallbackEvent EventCallback;
502};
503
504struct GuestDnDTransferRecvData : public GuestDnDTransferData
505{
506 GuestDnDTransferRecvData()
507 {
508 RT_ZERO(DroppedFiles);
509 int vrc2 = DnDDroppedFilesInit(&DroppedFiles);
510 AssertRC(vrc2);
511
512 RT_ZERO(List);
513 vrc2 = DnDTransferListInit(&List);
514 AssertRC(vrc2);
515
516 RT_ZERO(ObjCur);
517 vrc2 = DnDTransferObjectInit(&ObjCur);
518 AssertRC(vrc2);
519 }
520
521 virtual ~GuestDnDTransferRecvData()
522 {
523 destroy();
524 }
525
526 /**
527 * Destroys the object.
528 */
529 void destroy(void)
530 {
531 DnDTransferListDestroy(&List);
532 }
533
534 /**
535 * Resets the object.
536 */
537 void reset(void)
538 {
539 DnDDroppedFilesClose(&DroppedFiles);
540 DnDTransferListReset(&List);
541 DnDTransferObjectReset(&ObjCur);
542
543 GuestDnDTransferData::reset();
544 }
545
546 /** The "VirtualBox Dropped Files" directory on the host we're going
547 * to utilize for transferring files from guest to the host. */
548 DNDDROPPEDFILES DroppedFiles;
549 /** Transfer List to handle.
550 * Currently we only support one transfer list at a time. */
551 DNDTRANSFERLIST List;
552 /** Current transfer object being handled.
553 * Currently we only support one transfer object at a time. */
554 DNDTRANSFEROBJECT ObjCur;
555};
556
557/**
558 * Context structure for receiving data from the guest.
559 */
560struct GuestDnDRecvCtx : public GuestDnDData
561{
562 GuestDnDRecvCtx(void);
563
564 /**
565 * Resets the object.
566 */
567 void reset(void);
568
569 /** Pointer to guest source class this context belongs to. */
570 GuestDnDSource *pSource;
571 /** Pointer to guest state this context belongs to. */
572 GuestDnDState *pState;
573 /** Formats offered by the guest (and supported by the host). */
574 GuestDnDMIMEList lstFmtOffered;
575 /** Original drop format requested to receive from the guest. */
576 com::Utf8Str strFmtReq;
577 /** Intermediate drop format to be received from the guest.
578 * Some original drop formats require a different intermediate
579 * drop format:
580 *
581 * Receiving a file link as "text/plain" requires still to
582 * receive the file from the guest as "text/uri-list" first,
583 * then pointing to the file path on the host with the data
584 * in "text/plain" format returned. */
585 com::Utf8Str strFmtRecv;
586 /** Desired drop action to perform on the host.
587 * Needed to tell the guest if data has to be
588 * deleted e.g. when moving instead of copying. */
589 VBOXDNDACTION enmAction;
590 /** Transfer data structure. */
591 GuestDnDTransferRecvData Transfer;
592 /** Callback event to use. */
593 GuestDnDCallbackEvent EventCallback;
594};
595
596/**
597 * Class for maintainig a (buffered) guest DnD message.
598 */
599class GuestDnDMsg
600{
601public:
602
603 GuestDnDMsg(void)
604 : uMsg(0)
605 , cParms(0)
606 , cParmsAlloc(0)
607 , paParms(NULL) { }
608
609 virtual ~GuestDnDMsg(void)
610 {
611 reset();
612 }
613
614public:
615
616 /**
617 * Appends a new HGCM parameter to the message and returns the pointer to it.
618 */
619 PVBOXHGCMSVCPARM getNextParam(void)
620 {
621 if (cParms >= cParmsAlloc)
622 {
623 if (!paParms)
624 paParms = (PVBOXHGCMSVCPARM)RTMemAlloc(4 * sizeof(VBOXHGCMSVCPARM));
625 else
626 paParms = (PVBOXHGCMSVCPARM)RTMemRealloc(paParms, (cParmsAlloc + 4) * sizeof(VBOXHGCMSVCPARM));
627 if (!paParms)
628 throw VERR_NO_MEMORY;
629 RT_BZERO(&paParms[cParmsAlloc], 4 * sizeof(VBOXHGCMSVCPARM));
630 cParmsAlloc += 4;
631 }
632
633 return &paParms[cParms++];
634 }
635
636 /**
637 * Returns the current parameter count.
638 *
639 * @returns Current parameter count.
640 */
641 uint32_t getCount(void) const { return cParms; }
642
643 /**
644 * Returns the pointer to the beginning of the HGCM parameters array. Use with care.
645 *
646 * @returns Pointer to the beginning of the HGCM parameters array.
647 */
648 PVBOXHGCMSVCPARM getParms(void) const { return paParms; }
649
650 /**
651 * Returns the message type.
652 *
653 * @returns Message type.
654 */
655 uint32_t getType(void) const { return uMsg; }
656
657 /**
658 * Resets the object.
659 */
660 void reset(void)
661 {
662 if (paParms)
663 {
664 /* Remove deep copies. */
665 for (uint32_t i = 0; i < cParms; i++)
666 {
667 if ( paParms[i].type == VBOX_HGCM_SVC_PARM_PTR
668 && paParms[i].u.pointer.size)
669 {
670 AssertPtr(paParms[i].u.pointer.addr);
671 RTMemFree(paParms[i].u.pointer.addr);
672 }
673 }
674
675 RTMemFree(paParms);
676 paParms = NULL;
677 }
678
679 uMsg = cParms = cParmsAlloc = 0;
680 }
681
682 /**
683 * Appends a new message parameter of type pointer.
684 *
685 * @returns VBox status code.
686 * @param pvBuf Pointer to data to use.
687 * @param cbBuf Size (in bytes) of data to use.
688 */
689 int appendPointer(void *pvBuf, uint32_t cbBuf)
690 {
691 PVBOXHGCMSVCPARM pParm = getNextParam();
692 if (!pParm)
693 return VERR_NO_MEMORY;
694
695 void *pvTmp = NULL;
696 if (cbBuf)
697 {
698 AssertPtr(pvBuf);
699 pvTmp = RTMemDup(pvBuf, cbBuf);
700 if (!pvTmp)
701 return VERR_NO_MEMORY;
702 }
703
704 HGCMSvcSetPv(pParm, pvTmp, cbBuf);
705 return VINF_SUCCESS;
706 }
707
708 /**
709 * Appends a new message parameter of type string.
710 *
711 * @returns VBox status code.
712 * @param pszString Pointer to string data to use.
713 */
714 int appendString(const char *pszString)
715 {
716 PVBOXHGCMSVCPARM pParm = getNextParam();
717 if (!pParm)
718 return VERR_NO_MEMORY;
719
720 char *pszTemp = RTStrDup(pszString);
721 if (!pszTemp)
722 return VERR_NO_MEMORY;
723
724 HGCMSvcSetStr(pParm, pszTemp);
725 return VINF_SUCCESS;
726 }
727
728 /**
729 * Appends a new message parameter of type uint32_t.
730 *
731 * @returns VBox status code.
732 * @param u32Val uint32_t value to use.
733 */
734 int appendUInt32(uint32_t u32Val)
735 {
736 PVBOXHGCMSVCPARM pParm = getNextParam();
737 if (!pParm)
738 return VERR_NO_MEMORY;
739
740 HGCMSvcSetU32(pParm, u32Val);
741 return VINF_SUCCESS;
742 }
743
744 /**
745 * Appends a new message parameter of type uint64_t.
746 *
747 * @returns VBox status code.
748 * @param u64Val uint64_t value to use.
749 */
750 int appendUInt64(uint64_t u64Val)
751 {
752 PVBOXHGCMSVCPARM pParm = getNextParam();
753 if (!pParm)
754 return VERR_NO_MEMORY;
755
756 HGCMSvcSetU64(pParm, u64Val);
757 return VINF_SUCCESS;
758 }
759
760 /**
761 * Sets the HGCM message type (function number).
762 *
763 * @param uMsgType Message type to set.
764 */
765 void setType(uint32_t uMsgType) { uMsg = uMsgType; }
766
767protected:
768
769 /** Message type. */
770 uint32_t uMsg;
771 /** Message parameters. */
772 uint32_t cParms;
773 /** Size of array. */
774 uint32_t cParmsAlloc;
775 /** Array of HGCM parameters */
776 PVBOXHGCMSVCPARM paParms;
777};
778
779/** Guest DnD callback function definition. */
780typedef DECLCALLBACKPTR(int, PFNGUESTDNDCALLBACK,(uint32_t uMsg, void *pvParms, size_t cbParms, void *pvUser));
781
782/**
783 * Structure for keeping a guest DnD callback.
784 * Each callback can handle one HGCM message, however, multiple HGCM messages can be registered
785 * to the same callback (function).
786 */
787typedef struct GuestDnDCallback
788{
789 GuestDnDCallback(void)
790 : uMessgage(0)
791 , pfnCallback(NULL)
792 , pvUser(NULL) { }
793
794 GuestDnDCallback(PFNGUESTDNDCALLBACK pvCB, uint32_t uMsg, void *pvUsr = NULL)
795 : uMessgage(uMsg)
796 , pfnCallback(pvCB)
797 , pvUser(pvUsr) { }
798
799 /** The HGCM message ID to handle. */
800 uint32_t uMessgage;
801 /** Pointer to callback function. */
802 PFNGUESTDNDCALLBACK pfnCallback;
803 /** Pointer to user-supplied data. */
804 void *pvUser;
805} GuestDnDCallback;
806
807/** Contains registered callback pointers for specific HGCM message types. */
808typedef std::map<uint32_t, GuestDnDCallback> GuestDnDCallbackMap;
809
810/**
811 * Class for keeping a DnD guest state around.
812 */
813class GuestDnDState
814{
815
816public:
817 DECLARE_TRANSLATE_METHODS(GuestDnDState)
818
819 GuestDnDState(const ComObjPtr<Guest>& pGuest);
820 virtual ~GuestDnDState(void);
821
822public:
823
824 VBOXDNDSTATE get(void) const { return m_enmState; }
825 int set(VBOXDNDSTATE enmState) { LogRel3(("DnD: State %s -> %s\n", DnDStateToStr(m_enmState), DnDStateToStr(enmState))); m_enmState = enmState; return 0; }
826 void lock() { RTCritSectEnter(&m_CritSect); };
827 void unlock() { RTCritSectLeave(&m_CritSect); };
828
829 /** @name Guest response handling.
830 * @{ */
831 int notifyAboutGuestResponse(int vrcGuest = VINF_SUCCESS);
832 int waitForGuestResponseEx(RTMSINTERVAL msTimeout = 3000, int *pvrcGuest = NULL);
833 int waitForGuestResponse(int *pvrcGuest = NULL);
834 /** @} */
835
836 void setActionsAllowed(VBOXDNDACTIONLIST a) { m_dndLstActionsAllowed = a; }
837 VBOXDNDACTIONLIST getActionsAllowed(void) const { return m_dndLstActionsAllowed; }
838
839 void setActionDefault(VBOXDNDACTION a) { m_dndActionDefault = a; }
840 VBOXDNDACTION getActionDefault(void) const { return m_dndActionDefault; }
841
842 void setFormats(const GuestDnDMIMEList &lstFormats) { m_lstFormats = lstFormats; }
843 GuestDnDMIMEList formats(void) const { return m_lstFormats; }
844
845 void reset(void);
846
847 /** @name Callback handling.
848 * @{ */
849 static DECLCALLBACK(int) i_defaultCallback(uint32_t uMsg, void *pvParms, size_t cbParms, void *pvUser);
850 int setCallback(uint32_t uMsg, PFNGUESTDNDCALLBACK pfnCallback, void *pvUser = NULL);
851 /** @} */
852
853 /** @name Progress handling.
854 * @{ */
855 bool isProgressCanceled(void) const;
856 bool isProgressRunning(void) const;
857 int setProgress(unsigned uPercentage, uint32_t uStatus, int vrcOp = VINF_SUCCESS, const Utf8Str &strMsg = Utf8Str::Empty);
858 HRESULT resetProgress(const ComObjPtr<Guest>& pParent, const Utf8Str &strDesc);
859 HRESULT queryProgressTo(IProgress **ppProgress);
860 /** @} */
861
862public:
863
864 /** @name HGCM callback handling.
865 @{ */
866 int onDispatch(uint32_t u32Function, void *pvParms, uint32_t cbParms);
867 /** @} */
868
869public:
870
871 /** Pointer to context this class is tied to. */
872 void *m_pvCtx;
873 RTCRITSECT m_CritSect;
874 /** The current state we're in. */
875 VBOXDNDSTATE m_enmState;
876 /** The DnD protocol version to use, depending on the
877 * installed Guest Additions. See DragAndDropSvc.h for
878 * a protocol changelog. */
879 uint32_t m_uProtocolVersion;
880 /** The guest feature flags reported to the host (VBOX_DND_GF_XXX). */
881 uint64_t m_fGuestFeatures0;
882 /** Event for waiting for response. */
883 RTSEMEVENT m_EventSem;
884 /** Last error reported from guest.
885 * Set to VERR_IPE_UNINITIALIZED_STATUS if not set yet. */
886 int m_vrcGuest;
887 /** Default action to perform in case of a
888 * successful drop. */
889 VBOXDNDACTION m_dndActionDefault;
890 /** Actions supported by the guest in case of a successful drop. */
891 VBOXDNDACTIONLIST m_dndLstActionsAllowed;
892 /** Format(s) requested/supported from the guest. */
893 GuestDnDMIMEList m_lstFormats;
894 /** Pointer to IGuest parent object. */
895 ComObjPtr<Guest> m_pParent;
896 /** Pointer to associated progress object. Optional. */
897 ComObjPtr<Progress> m_pProgress;
898 /** Callback map. */
899 GuestDnDCallbackMap m_mapCallbacks;
900};
901
902/**
903 * Private singleton class for the guest's DnD implementation.
904 *
905 * Can't be instanciated directly, only via the factory pattern.
906 * Keeps track of all ongoing DnD transfers.
907 */
908class GuestDnD
909{
910public:
911
912 /**
913 * Creates the Singleton GuestDnD object.
914 *
915 * @returns Newly created Singleton object, or NULL on failure.
916 */
917 static GuestDnD *createInstance(const ComObjPtr<Guest>& pGuest)
918 {
919 Assert(NULL == GuestDnD::s_pInstance);
920 GuestDnD::s_pInstance = new GuestDnD(pGuest);
921 return GuestDnD::s_pInstance;
922 }
923
924 /**
925 * Destroys the Singleton GuestDnD object.
926 */
927 static void destroyInstance(void)
928 {
929 if (GuestDnD::s_pInstance)
930 {
931 delete GuestDnD::s_pInstance;
932 GuestDnD::s_pInstance = NULL;
933 }
934 }
935
936 /**
937 * Returns the Singleton GuestDnD object.
938 *
939 * @returns Pointer to Singleton GuestDnD object, or NULL if not created yet.
940 */
941 static inline GuestDnD *getInstance(void)
942 {
943 AssertPtr(GuestDnD::s_pInstance);
944 return GuestDnD::s_pInstance;
945 }
946
947protected:
948
949 /** List of registered DnD sources. */
950 typedef std::list< ComObjPtr<GuestDnDSource> > GuestDnDSrcList;
951 /** List of registered DnD targets. */
952 typedef std::list< ComObjPtr<GuestDnDTarget> > GuestDnDTgtList;
953
954 /** Constructor; will throw vrc on failure. */
955 GuestDnD(const ComObjPtr<Guest>& pGuest);
956 virtual ~GuestDnD(void);
957
958public:
959
960 /** @name Public helper functions.
961 * @{ */
962 HRESULT adjustScreenCoordinates(ULONG uScreenId, ULONG *puX, ULONG *puY) const;
963 GuestDnDState *getState(uint32_t = 0) const;
964 int hostCall(uint32_t u32Function, uint32_t cParms, PVBOXHGCMSVCPARM paParms) const;
965 GuestDnDMIMEList defaultFormats(void) const { return m_strDefaultFormats; }
966 /** @} */
967
968 /** @name Source / target management.
969 * @{ */
970 int registerSource(const ComObjPtr<GuestDnDSource> &Source);
971 int unregisterSource(const ComObjPtr<GuestDnDSource> &Source);
972 size_t getSourceCount(void);
973
974 int registerTarget(const ComObjPtr<GuestDnDTarget> &Target);
975 int unregisterTarget(const ComObjPtr<GuestDnDTarget> &Target);
976 size_t getTargetCount(void);
977 /** @} */
978
979public:
980
981 /** @name Static low-level HGCM callback handler.
982 * @{ */
983 static DECLCALLBACK(int) notifyDnDDispatcher(void *pvExtension, uint32_t u32Function, void *pvParms, uint32_t cbParms);
984 /** @} */
985
986 /** @name Static helper methods.
987 * @{ */
988 static bool isFormatInFormatList(const com::Utf8Str &strFormat, const GuestDnDMIMEList &lstFormats);
989 static GuestDnDMIMEList toFormatList(const com::Utf8Str &strFormats, const com::Utf8Str &strSep = DND_FORMATS_SEPARATOR_STR);
990 static com::Utf8Str toFormatString(const GuestDnDMIMEList &lstFormats, const com::Utf8Str &strSep = DND_FORMATS_SEPARATOR_STR);
991 static GuestDnDMIMEList toFilteredFormatList(const GuestDnDMIMEList &lstFormatsSupported, const GuestDnDMIMEList &lstFormatsWanted);
992 static GuestDnDMIMEList toFilteredFormatList(const GuestDnDMIMEList &lstFormatsSupported, const com::Utf8Str &strFormatsWanted);
993 static DnDAction_T toMainAction(VBOXDNDACTION dndAction);
994 static std::vector<DnDAction_T> toMainActions(VBOXDNDACTIONLIST dndActionList);
995 static VBOXDNDACTION toHGCMAction(DnDAction_T enmAction);
996 static void toHGCMActions(DnDAction_T enmDefAction, VBOXDNDACTION *pDefAction, const std::vector<DnDAction_T> vecAllowedActions, VBOXDNDACTIONLIST *pLstAllowedActions);
997 /** @} */
998
999protected:
1000
1001 /** @name Singleton properties.
1002 * @{ */
1003 /** List of supported default MIME/Content-type formats. */
1004 GuestDnDMIMEList m_strDefaultFormats;
1005 /** Pointer to guest implementation. */
1006 const ComObjPtr<Guest> m_pGuest;
1007 /** The current state from the guest. At the
1008 * moment we only support only state a time (ARQ-style). */
1009 GuestDnDState *m_pState;
1010 /** Critical section to serialize access. */
1011 RTCRITSECT m_CritSect;
1012 /** Number of active transfers (guest->host or host->guest). */
1013 uint32_t m_cTransfersPending;
1014 GuestDnDSrcList m_lstSrc;
1015 GuestDnDTgtList m_lstTgt;
1016 /** @} */
1017
1018private:
1019
1020 /** Static pointer to singleton instance. */
1021 static GuestDnD *s_pInstance;
1022};
1023
1024/** Access to the GuestDnD's singleton instance. */
1025#define GuestDnDInst() GuestDnD::getInstance()
1026
1027/** List of pointers to guest DnD Messages. */
1028typedef std::list<GuestDnDMsg *> GuestDnDMsgList;
1029
1030/**
1031 * IDnDBase class implementation for sharing code between
1032 * IGuestDnDSource and IGuestDnDTarget implementation.
1033 */
1034class GuestDnDBase
1035{
1036protected:
1037
1038 GuestDnDBase(VirtualBoxBase *pBase);
1039
1040 virtual ~GuestDnDBase(void);
1041
1042protected:
1043
1044 /** Shared (internal) IDnDBase method implementations.
1045 * @{ */
1046 bool i_isFormatSupported(const com::Utf8Str &aFormat) const;
1047 const GuestDnDMIMEList &i_getFormats(void) const;
1048 HRESULT i_addFormats(const GuestDnDMIMEList &aFormats);
1049 HRESULT i_removeFormats(const GuestDnDMIMEList &aFormats);
1050 /** @} */
1051
1052 /** @name Error handling.
1053 * @{ */
1054 HRESULT i_setErrorV(int vrc, const char *pcszMsgFmt, va_list va);
1055 HRESULT i_setError(int vrc, const char *pcszMsgFmt, ...);
1056 HRESULT i_setErrorAndReset(const char *pcszMsgFmt, ...);
1057 HRESULT i_setErrorAndReset(int vrc, const char *pcszMsgFmt, ...);
1058 /** @} */
1059
1060protected:
1061
1062 /** @name Pure virtual functions needed to be implemented by the actual (derived) implementation.
1063 * @{ */
1064 virtual void i_reset(void) = 0;
1065 /** @} */
1066
1067protected:
1068
1069 /** @name Functions for handling a simple host HGCM message queue.
1070 * @{ */
1071 int msgQueueAdd(GuestDnDMsg *pMsg);
1072 GuestDnDMsg *msgQueueGetNext(void);
1073 void msgQueueRemoveNext(void);
1074 void msgQueueClear(void);
1075 /** @} */
1076
1077 int sendCancel(void);
1078 int updateProgress(GuestDnDData *pData, GuestDnDState *pState, size_t cbDataAdd = 0);
1079 int waitForEvent(GuestDnDCallbackEvent *pEvent, GuestDnDState *pState, RTMSINTERVAL msTimeout);
1080
1081protected:
1082
1083 /** Pointer to base class to use for stuff like error handlng. */
1084 VirtualBoxBase *m_pBase;
1085 /** @name Public attributes (through getters/setters).
1086 * @{ */
1087 /** Pointer to guest implementation. */
1088 const ComObjPtr<Guest> m_pGuest;
1089 /** List of supported MIME types by the source. */
1090 GuestDnDMIMEList m_lstFmtSupported;
1091 /** List of offered MIME types to the counterpart. */
1092 GuestDnDMIMEList m_lstFmtOffered;
1093 /** Whether the object still is in pending state. */
1094 bool m_fIsPending;
1095 /** Pointer to state bound to this object. */
1096 GuestDnDState *m_pState;
1097 /** @} */
1098
1099 /**
1100 * Internal stuff.
1101 */
1102 struct
1103 {
1104 /** Outgoing message queue (FIFO). */
1105 GuestDnDMsgList lstMsgOut;
1106 } m_DataBase;
1107};
1108#endif /* !MAIN_INCLUDED_GuestDnDPrivate_h */
1109
Note: See TracBrowser for help on using the repository browser.

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