VirtualBox

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

Last change on this file since 98299 was 98299, checked in by vboxsync, 23 months ago

Main: doxygen fix attempt. bugref:10223

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