VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/GuestDnDPrivate.cpp@ 97719

Last change on this file since 97719 was 97719, checked in by vboxsync, 2 years ago

DnD/Main: Added a dedicated COM error for drag and drop operations (VBOX_E_DND_ERROR).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 46.7 KB
Line 
1/* $Id: GuestDnDPrivate.cpp 97719 2022-11-30 16:59:23Z vboxsync $ */
2/** @file
3 * Private guest drag and drop code, used by GuestDnDTarget + GuestDnDSource.
4 */
5
6/*
7 * Copyright (C) 2011-2022 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#define LOG_GROUP LOG_GROUP_GUEST_DND
29#include "LoggingNew.h"
30
31#include "GuestImpl.h"
32#include "AutoCaller.h"
33
34#ifdef VBOX_WITH_DRAG_AND_DROP
35# include "ConsoleImpl.h"
36# include "ProgressImpl.h"
37# include "GuestDnDPrivate.h"
38
39# include <algorithm>
40
41# include <iprt/dir.h>
42# include <iprt/path.h>
43# include <iprt/stream.h>
44# include <iprt/semaphore.h>
45# include <iprt/cpp/utils.h>
46
47# include <VMMDev.h>
48
49# include <VBox/GuestHost/DragAndDrop.h>
50# include <VBox/HostServices/DragAndDropSvc.h>
51# include <VBox/version.h>
52
53/** @page pg_main_dnd Dungeons & Dragons - Overview
54 * Overview:
55 *
56 * Drag and Drop is handled over the internal HGCM service for the host <->
57 * guest communication. Beside that we need to map the Drag and Drop protocols
58 * of the various OS's we support to our internal channels, this is also highly
59 * communicative in both directions. Unfortunately HGCM isn't really designed
60 * for that. Next we have to foul some of the components. This includes to
61 * trick X11 on the guest side, but also Qt needs to be tricked on the host
62 * side a little bit.
63 *
64 * The following components are involved:
65 *
66 * 1. GUI: Uses the Qt classes for Drag and Drop and mainly forward the content
67 * of it to the Main IGuest / IGuestDnDSource / IGuestDnDTarget interfaces.
68 * 2. Main: Public interface for doing Drag and Drop. Also manage the IProgress
69 * interfaces for blocking the caller by showing a progress dialog (see
70 * this file).
71 * 3. HGCM service: Handle all messages from the host to the guest at once and
72 * encapsulate the internal communication details (see dndmanager.cpp and
73 * friends).
74 * 4. Guest Additions: Split into the platform neutral part (see
75 * VBoxGuestR3LibDragAndDrop.cpp) and the guest OS specific parts.
76 * Receive/send message from/to the HGCM service and does all guest specific
77 * operations. For Windows guests VBoxTray is in charge, whereas on UNIX-y guests
78 * VBoxClient will be used.
79 *
80 * Terminology:
81 *
82 * All transfers contain a MIME format and according meta data. This meta data then can
83 * be interpreted either as raw meta data or something else. When raw meta data is
84 * being handled, this gets passed through to the destination (guest / host) without
85 * modification. Other meta data (like URI lists) can and will be modified by the
86 * receiving side before passing to OS. How and when modifications will be applied
87 * depends on the MIME format.
88 *
89 * Host -> Guest:
90 * 1. There are DnD Enter, Move, Leave events which are send exactly like this
91 * to the guest. The info includes the position, MIME types and allowed actions.
92 * The guest has to respond with an action it would accept, so the GUI could
93 * change the cursor accordingly.
94 * 2. On drop, first a drop event is sent. If this is accepted a drop data
95 * event follows. This blocks the GUI and shows some progress indicator.
96 *
97 * Guest -> Host:
98 * 1. The GUI is asking the guest if a DnD event is pending when the user moves
99 * the cursor out of the view window. If so, this returns the mimetypes and
100 * allowed actions.
101 * (2. On every mouse move this is asked again, to make sure the DnD event is
102 * still valid.)
103 * 3. On drop the host request the data from the guest. This blocks the GUI and
104 * shows some progress indicator.
105 *
106 * Implementation hints:
107 * m_strSupportedFormats here in this file defines the allowed mime-types.
108 * This is necessary because we need special handling for some of the
109 * mime-types. E.g. for URI lists we need to transfer the actual dirs and
110 * files. Text EOL may to be changed. Also unknown mime-types may need special
111 * handling as well, which may lead to undefined behavior in the host/guest, if
112 * not done.
113 *
114 * Dropping of a directory, means recursively transferring _all_ the content.
115 *
116 * Directories and files are placed into the user's temporary directory on the
117 * guest (e.g. /tmp/VirtualBox Dropped Files). We can't delete them after the
118 * DnD operation, because we didn't know what the DnD target does with it. E.g.
119 * it could just be opened in place. This could lead ofc to filling up the disk
120 * within the guest. To inform the user about this, a small app could be
121 * developed which scans this directory regularly and inform the user with a
122 * tray icon hint (and maybe the possibility to clean this up instantly). The
123 * same has to be done in the G->H direction when it is implemented.
124 *
125 * Only regular files are supported; symlinks are not allowed.
126 *
127 * Transfers currently are an all-succeed or all-fail operation (see todos).
128 *
129 * On MacOS hosts we had to implement own DnD "promises" support for file transfers,
130 * as Qt does not support this out-of-the-box.
131 *
132 * The code tries to preserve the file modes of the transfered directories / files.
133 * This is useful (and maybe necessary) for two things:
134 * 1. If a file is executable, it should be also after the transfer, so the
135 * user can just execute it, without manually tweaking the modes first.
136 * 2. If a dir/file is not accessible by group/others in the host, it shouldn't
137 * be in the guest.
138 * In any case, the user mode is always set to rwx (so that we can access it
139 * ourself, in e.g. for a cleanup case after cancel).
140 *
141 * ACEs / ACLs currently are not supported.
142 *
143 * Cancelling ongoing transfers is supported in both directions by the guest
144 * and/or host side and cleans up all previous steps. This also involves
145 * removing partially transferred directories / files in the temporary directory.
146 *
147 ** @todo
148 * - ESC doesn't really work (on Windows guests it's already implemented)
149 * ... in any case it seems a little bit difficult to handle from the Qt side.
150 * - Transfers currently do not have any interactive (UI) callbacks / hooks which
151 * e.g. would allow to skip / replace / rename and entry, or abort the operation on failure.
152 * - Add support for more MIME types (especially images, csv)
153 * - Test unusual behavior:
154 * - DnD service crash in the guest during a DnD op (e.g. crash of VBoxClient or X11)
155 * - Not expected order of the events between HGCM and the guest
156 * - Security considerations: We transfer a lot of memory between the guest and
157 * the host and even allow the creation of dirs/files. Maybe there should be
158 * limits introduced to preventing DoS attacks or filling up all the memory
159 * (both in the host and the guest).
160 */
161
162
163/*********************************************************************************************************************************
164 * Internal macros. *
165 ********************************************************************************************************************************/
166
167/** Tries locking the GuestDnD object and returns on failure. */
168#define GUESTDND_LOCK() \
169 { \
170 int rcLock = RTCritSectEnter(&m_CritSect); \
171 if (RT_FAILURE(rcLock)) \
172 return rcLock; \
173 }
174
175/** Tries locking the GuestDnD object and returns a_Ret failure. */
176#define GUESTDND_LOCK_RET(a_Ret) \
177 { \
178 int rcLock = RTCritSectEnter(&m_CritSect); \
179 if (RT_FAILURE(rcLock)) \
180 return a_Ret; \
181 }
182
183/** Unlocks a formerly locked GuestDnD object. */
184#define GUESTDND_UNLOCK() \
185 { \
186 int rcUnlock = RTCritSectLeave(&m_CritSect); RT_NOREF(rcUnlock); \
187 AssertRC(rcUnlock); \
188 }
189
190/*********************************************************************************************************************************
191 * GuestDnDSendCtx implementation. *
192 ********************************************************************************************************************************/
193
194GuestDnDSendCtx::GuestDnDSendCtx(void)
195 : pTarget(NULL)
196 , pState(NULL)
197{
198 reset();
199}
200
201/**
202 * Resets a GuestDnDSendCtx object.
203 */
204void GuestDnDSendCtx::reset(void)
205{
206 if (pState)
207 pState->reset();
208
209 uScreenID = 0;
210
211 Transfer.reset();
212
213 int rc2 = EventCallback.Reset();
214 AssertRC(rc2);
215
216 GuestDnDData::reset();
217}
218
219/*********************************************************************************************************************************
220 * GuestDnDRecvCtx implementation. *
221 ********************************************************************************************************************************/
222
223GuestDnDRecvCtx::GuestDnDRecvCtx(void)
224 : pSource(NULL)
225 , pState(NULL)
226{
227 reset();
228}
229
230/**
231 * Resets a GuestDnDRecvCtx object.
232 */
233void GuestDnDRecvCtx::reset(void)
234{
235 if (pState)
236 pState->reset();
237
238 lstFmtOffered.clear();
239 strFmtReq = "";
240 strFmtRecv = "";
241 enmAction = 0;
242
243 Transfer.reset();
244
245 int rc2 = EventCallback.Reset();
246 AssertRC(rc2);
247
248 GuestDnDData::reset();
249}
250
251/*********************************************************************************************************************************
252 * GuestDnDCallbackEvent implementation. *
253 ********************************************************************************************************************************/
254
255GuestDnDCallbackEvent::~GuestDnDCallbackEvent(void)
256{
257 if (NIL_RTSEMEVENT != m_SemEvent)
258 RTSemEventDestroy(m_SemEvent);
259}
260
261/**
262 * Resets a GuestDnDCallbackEvent object.
263 */
264int GuestDnDCallbackEvent::Reset(void)
265{
266 int rc = VINF_SUCCESS;
267
268 if (NIL_RTSEMEVENT == m_SemEvent)
269 rc = RTSemEventCreate(&m_SemEvent);
270
271 m_Rc = VINF_SUCCESS;
272 return rc;
273}
274
275/**
276 * Completes a callback event by notifying the waiting side.
277 *
278 * @returns VBox status code.
279 * @param rc Result code to use for the event completion.
280 */
281int GuestDnDCallbackEvent::Notify(int rc /* = VINF_SUCCESS */)
282{
283 m_Rc = rc;
284 return RTSemEventSignal(m_SemEvent);
285}
286
287/**
288 * Waits on a callback event for being notified.
289 *
290 * @returns VBox status code.
291 * @param msTimeout Timeout (in ms) to wait for callback event.
292 */
293int GuestDnDCallbackEvent::Wait(RTMSINTERVAL msTimeout)
294{
295 return RTSemEventWait(m_SemEvent, msTimeout);
296}
297
298/********************************************************************************************************************************
299 *
300 ********************************************************************************************************************************/
301
302GuestDnDState::GuestDnDState(const ComObjPtr<Guest>& pGuest)
303 : m_uProtocolVersion(0)
304 , m_fGuestFeatures0(VBOX_DND_GF_NONE)
305 , m_EventSem(NIL_RTSEMEVENT)
306 , m_dndActionDefault(0)
307 , m_dndLstActionsAllowed(0)
308 , m_pParent(pGuest)
309{
310 int rc = RTSemEventCreate(&m_EventSem);
311 if (RT_FAILURE(rc))
312 throw rc;
313}
314
315GuestDnDState::~GuestDnDState(void)
316{
317 reset();
318
319 int rc = RTSemEventDestroy(m_EventSem);
320 AssertRC(rc);
321}
322
323/**
324 * Notifies the waiting side about a guest notification response.
325 */
326int GuestDnDState::notifyAboutGuestResponse(void) const
327{
328 return RTSemEventSignal(m_EventSem);
329}
330
331/**
332 * Resets a GuestDnDResponse object.
333 */
334void GuestDnDState::reset(void)
335{
336 LogFlowThisFuncEnter();
337
338 m_dndActionDefault = 0;
339 m_dndLstActionsAllowed = 0;
340
341 m_lstFormats.clear();
342}
343
344/**
345 * Resets the progress object.
346 *
347 * @returns HRESULT
348 * @param pParent Parent to set for the progress object.
349 */
350HRESULT GuestDnDState::resetProgress(const ComObjPtr<Guest>& pParent)
351{
352 m_pProgress.setNull();
353
354 HRESULT hr = m_pProgress.createObject();
355 if (SUCCEEDED(hr))
356 {
357 hr = m_pProgress->init(static_cast<IGuest *>(pParent),
358 Bstr(tr("Dropping data")).raw(),
359 TRUE /* aCancelable */);
360 }
361
362 return hr;
363}
364
365/**
366 * Returns whether the progress object has been canceled or not.
367 *
368 * @returns \c true if canceled, \c false if not.
369 */
370bool GuestDnDState::isProgressCanceled(void) const
371{
372 BOOL fCanceled;
373 if (!m_pProgress.isNull())
374 {
375 HRESULT hr = m_pProgress->COMGETTER(Canceled)(&fCanceled);
376 AssertComRC(hr);
377 }
378 else
379 fCanceled = TRUE;
380
381 return RT_BOOL(fCanceled);
382}
383
384/**
385 * Sets a callback for a specific HGCM message.
386 *
387 * @returns VBox status code.
388 * @param uMsg HGCM message ID to set callback for.
389 * @param pfnCallback Callback function pointer to use.
390 * @param pvUser User-provided arguments for the callback function. Optional and can be NULL.
391 */
392int GuestDnDState::setCallback(uint32_t uMsg, PFNGUESTDNDCALLBACK pfnCallback, void *pvUser /* = NULL */)
393{
394 GuestDnDCallbackMap::iterator it = m_mapCallbacks.find(uMsg);
395
396 /* Add. */
397 if (pfnCallback)
398 {
399 if (it == m_mapCallbacks.end())
400 {
401 m_mapCallbacks[uMsg] = GuestDnDCallback(pfnCallback, uMsg, pvUser);
402 return VINF_SUCCESS;
403 }
404
405 AssertMsgFailed(("Callback for message %RU32 already registered\n", uMsg));
406 return VERR_ALREADY_EXISTS;
407 }
408
409 /* Remove. */
410 if (it != m_mapCallbacks.end())
411 m_mapCallbacks.erase(it);
412
413 return VINF_SUCCESS;
414}
415
416/**
417 * Sets the progress object to a new state.
418 *
419 * @returns VBox status code.
420 * @param uPercentage Percentage (0-100) to set.
421 * @param uStatus Status (of type DND_PROGRESS_XXX) to set.
422 * @param rcOp IPRT-style result code to set. Optional.
423 * @param strMsg Message to set. Optional.
424 */
425int GuestDnDState::setProgress(unsigned uPercentage, uint32_t uStatus,
426 int rcOp /* = VINF_SUCCESS */, const Utf8Str &strMsg /* = "" */)
427{
428 LogFlowFunc(("uPercentage=%u, uStatus=%RU32, , rcOp=%Rrc, strMsg=%s\n",
429 uPercentage, uStatus, rcOp, strMsg.c_str()));
430
431 HRESULT hr = S_OK;
432
433 BOOL fCompleted = FALSE;
434 BOOL fCanceled = FALSE;
435
436 int rc = VINF_SUCCESS;
437
438 if (!m_pProgress.isNull())
439 {
440 hr = m_pProgress->COMGETTER(Completed)(&fCompleted);
441 AssertComRC(hr);
442
443 hr = m_pProgress->COMGETTER(Canceled)(&fCanceled);
444 AssertComRC(hr);
445
446 LogFlowFunc(("Progress fCompleted=%RTbool, fCanceled=%RTbool\n", fCompleted, fCanceled));
447 }
448
449 switch (uStatus)
450 {
451 case DragAndDropSvc::DND_PROGRESS_ERROR:
452 {
453 LogRel(("DnD: Guest reported error %Rrc\n", rcOp));
454
455 if ( !m_pProgress.isNull()
456 && !fCompleted)
457 hr = m_pProgress->i_notifyComplete(VBOX_E_DND_ERROR,
458 COM_IIDOF(IGuest),
459 m_pParent->getComponentName(), strMsg.c_str());
460 reset();
461 break;
462 }
463
464 case DragAndDropSvc::DND_PROGRESS_CANCELLED:
465 {
466 LogRel2(("DnD: Guest cancelled operation\n"));
467
468 if ( !m_pProgress.isNull()
469 && !fCompleted)
470 {
471 hr = m_pProgress->Cancel();
472 AssertComRC(hr);
473 hr = m_pProgress->i_notifyComplete(S_OK);
474 AssertComRC(hr);
475 }
476
477 reset();
478 break;
479 }
480
481 case DragAndDropSvc::DND_PROGRESS_RUNNING:
482 RT_FALL_THROUGH();
483 case DragAndDropSvc::DND_PROGRESS_COMPLETE:
484 {
485 LogRel2(("DnD: Guest reporting running/completion status with %u%%\n", uPercentage));
486
487 if ( !m_pProgress.isNull()
488 && !fCompleted)
489 {
490 hr = m_pProgress->SetCurrentOperationProgress(uPercentage);
491 AssertComRC(hr);
492 if ( uStatus == DragAndDropSvc::DND_PROGRESS_COMPLETE
493 || uPercentage >= 100)
494 {
495 hr = m_pProgress->i_notifyComplete(S_OK);
496 AssertComRC(hr);
497 }
498 }
499 break;
500 }
501
502 default:
503 break;
504 }
505
506 if (!m_pProgress.isNull())
507 {
508 hr = m_pProgress->COMGETTER(Completed)(&fCompleted);
509 AssertComRC(hr);
510 hr = m_pProgress->COMGETTER(Canceled)(&fCanceled);
511 AssertComRC(hr);
512
513 LogFlowFunc(("Progress fCompleted=%RTbool, fCanceled=%RTbool\n", fCompleted, fCanceled));
514 }
515
516 LogFlowFuncLeaveRC(rc);
517 return rc;
518}
519
520/**
521 * Dispatching function for handling the host service service callback.
522 *
523 * @returns VBox status code.
524 * @param u32Function HGCM message ID to handle.
525 * @param pvParms Pointer to optional data provided for a particular message. Optional.
526 * @param cbParms Size (in bytes) of \a pvParms.
527 */
528int GuestDnDState::onDispatch(uint32_t u32Function, void *pvParms, uint32_t cbParms)
529{
530 LogFlowFunc(("u32Function=%RU32, pvParms=%p, cbParms=%RU32\n", u32Function, pvParms, cbParms));
531
532 int rc = VERR_WRONG_ORDER; /* Play safe. */
533
534 /* Whether or not to try calling host-installed callbacks after successfully processing the message. */
535 bool fTryCallbacks = false;
536
537 switch (u32Function)
538 {
539 case DragAndDropSvc::GUEST_DND_FN_CONNECT:
540 {
541 DragAndDropSvc::PVBOXDNDCBCONNECTDATA pCBData = reinterpret_cast<DragAndDropSvc::PVBOXDNDCBCONNECTDATA>(pvParms);
542 AssertPtr(pCBData);
543 AssertReturn(sizeof(DragAndDropSvc::VBOXDNDCBCONNECTDATA) == cbParms, VERR_INVALID_PARAMETER);
544 AssertReturn(DragAndDropSvc::CB_MAGIC_DND_CONNECT == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
545
546 m_uProtocolVersion = pCBData->uProtocolVersion;
547 /** @todo Handle flags. */
548
549 LogThisFunc(("Client connected, using protocol v%RU32\n", m_uProtocolVersion));
550
551 rc = VINF_SUCCESS;
552 break;
553 }
554
555 case DragAndDropSvc::GUEST_DND_FN_REPORT_FEATURES:
556 {
557 DragAndDropSvc::PVBOXDNDCBREPORTFEATURESDATA pCBData = reinterpret_cast<DragAndDropSvc::PVBOXDNDCBREPORTFEATURESDATA>(pvParms);
558 AssertPtr(pCBData);
559 AssertReturn(sizeof(DragAndDropSvc::VBOXDNDCBREPORTFEATURESDATA) == cbParms, VERR_INVALID_PARAMETER);
560 AssertReturn(DragAndDropSvc::CB_MAGIC_DND_REPORT_FEATURES == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
561
562 m_fGuestFeatures0 = pCBData->fGuestFeatures0;
563
564 LogThisFunc(("Client reported features: %#RX64\n", m_fGuestFeatures0));
565
566 rc = VINF_SUCCESS;
567 break;
568 }
569
570 case DragAndDropSvc::GUEST_DND_FN_DISCONNECT:
571 {
572 LogThisFunc(("Client disconnected\n"));
573 rc = setProgress(100, DND_PROGRESS_CANCELLED, VINF_SUCCESS);
574 break;
575 }
576
577 case DragAndDropSvc::GUEST_DND_FN_HG_ACK_OP:
578 {
579 DragAndDropSvc::PVBOXDNDCBHGACKOPDATA pCBData = reinterpret_cast<DragAndDropSvc::PVBOXDNDCBHGACKOPDATA>(pvParms);
580 AssertPtr(pCBData);
581 AssertReturn(sizeof(DragAndDropSvc::VBOXDNDCBHGACKOPDATA) == cbParms, VERR_INVALID_PARAMETER);
582 AssertReturn(DragAndDropSvc::CB_MAGIC_DND_HG_ACK_OP == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
583
584 LogRel2(("DnD: Guest responded with action '%s' for host->guest drag event\n", DnDActionToStr(pCBData->uAction)));
585
586 setActionDefault(pCBData->uAction);
587 rc = notifyAboutGuestResponse();
588 break;
589 }
590
591 case DragAndDropSvc::GUEST_DND_FN_HG_REQ_DATA:
592 {
593 DragAndDropSvc::PVBOXDNDCBHGREQDATADATA pCBData = reinterpret_cast<DragAndDropSvc::PVBOXDNDCBHGREQDATADATA>(pvParms);
594 AssertPtr(pCBData);
595 AssertReturn(sizeof(DragAndDropSvc::VBOXDNDCBHGREQDATADATA) == cbParms, VERR_INVALID_PARAMETER);
596 AssertReturn(DragAndDropSvc::CB_MAGIC_DND_HG_REQ_DATA == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
597
598 if ( pCBData->cbFormat == 0
599 || pCBData->cbFormat > _64K /** @todo Make this configurable? */
600 || pCBData->pszFormat == NULL)
601 {
602 rc = VERR_INVALID_PARAMETER;
603 }
604 else if (!RTStrIsValidEncoding(pCBData->pszFormat))
605 {
606 rc = VERR_INVALID_PARAMETER;
607 }
608 else
609 {
610 setFormats(GuestDnD::toFormatList(pCBData->pszFormat));
611 rc = VINF_SUCCESS;
612 }
613
614 int rc2 = notifyAboutGuestResponse();
615 if (RT_SUCCESS(rc))
616 rc = rc2;
617 break;
618 }
619
620 case DragAndDropSvc::GUEST_DND_FN_HG_EVT_PROGRESS:
621 {
622 DragAndDropSvc::PVBOXDNDCBHGEVTPROGRESSDATA pCBData =
623 reinterpret_cast<DragAndDropSvc::PVBOXDNDCBHGEVTPROGRESSDATA>(pvParms);
624 AssertPtr(pCBData);
625 AssertReturn(sizeof(DragAndDropSvc::VBOXDNDCBHGEVTPROGRESSDATA) == cbParms, VERR_INVALID_PARAMETER);
626 AssertReturn(DragAndDropSvc::CB_MAGIC_DND_HG_EVT_PROGRESS == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
627
628 rc = setProgress(pCBData->uPercentage, pCBData->uStatus, pCBData->rc);
629 if (RT_SUCCESS(rc))
630 rc = notifyAboutGuestResponse();
631 break;
632 }
633#ifdef VBOX_WITH_DRAG_AND_DROP_GH
634 case DragAndDropSvc::GUEST_DND_FN_GH_ACK_PENDING:
635 {
636 DragAndDropSvc::PVBOXDNDCBGHACKPENDINGDATA pCBData =
637 reinterpret_cast<DragAndDropSvc::PVBOXDNDCBGHACKPENDINGDATA>(pvParms);
638 AssertPtr(pCBData);
639 AssertReturn(sizeof(DragAndDropSvc::VBOXDNDCBGHACKPENDINGDATA) == cbParms, VERR_INVALID_PARAMETER);
640 AssertReturn(DragAndDropSvc::CB_MAGIC_DND_GH_ACK_PENDING == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
641
642 LogRel2(("DnD: Guest responded with pending action '%s' (%RU32 bytes format data) to guest->host drag event\n",
643 DnDActionToStr(pCBData->uDefAction), pCBData->cbFormat));
644
645 if ( pCBData->cbFormat == 0
646 || pCBData->cbFormat > _64K /** @todo Make the maximum size configurable? */
647 || pCBData->pszFormat == NULL)
648 {
649 rc = VERR_INVALID_PARAMETER;
650 }
651 else if (!RTStrIsValidEncoding(pCBData->pszFormat))
652 {
653 rc = VERR_INVALID_PARAMETER;
654 }
655 else
656 {
657 setFormats (GuestDnD::toFormatList(pCBData->pszFormat));
658 setActionDefault (pCBData->uDefAction);
659 setActionsAllowed(pCBData->uAllActions);
660
661 rc = VINF_SUCCESS;
662 }
663
664 int rc2 = notifyAboutGuestResponse();
665 if (RT_SUCCESS(rc))
666 rc = rc2;
667 break;
668 }
669#endif /* VBOX_WITH_DRAG_AND_DROP_GH */
670 default:
671 /* * Try if the event is covered by a registered callback. */
672 fTryCallbacks = true;
673 break;
674 }
675
676 /*
677 * Try the host's installed callbacks (if any).
678 */
679 if (fTryCallbacks)
680 {
681 GuestDnDCallbackMap::const_iterator it = m_mapCallbacks.find(u32Function);
682 if (it != m_mapCallbacks.end())
683 {
684 AssertPtr(it->second.pfnCallback);
685 rc = it->second.pfnCallback(u32Function, pvParms, cbParms, it->second.pvUser);
686 }
687 else
688 {
689 LogFlowFunc(("No callback for function %RU32 defined\n", u32Function));
690 rc = VERR_NOT_SUPPORTED; /* Tell the guest. */
691 }
692 }
693
694 LogFlowFunc(("Returning rc=%Rrc\n", rc));
695 return rc;
696}
697
698/**
699 * Helper function to query the internal progress object to an IProgress interface.
700 *
701 * @returns HRESULT
702 * @param ppProgress Where to query the progress object to.
703 */
704HRESULT GuestDnDState::queryProgressTo(IProgress **ppProgress)
705{
706 return m_pProgress.queryInterfaceTo(ppProgress);
707}
708
709/**
710 * Waits for a guest response to happen.
711 *
712 * @returns VBox status code.
713 * @param msTimeout Timeout (in ms) for waiting. Optional, waits 500 ms if not specified.
714 */
715int GuestDnDState::waitForGuestResponse(RTMSINTERVAL msTimeout /*= 500 */) const
716{
717 int rc = RTSemEventWait(m_EventSem, msTimeout);
718#ifdef DEBUG_andy
719 LogFlowFunc(("msTimeout=%RU32, rc=%Rrc\n", msTimeout, rc));
720#endif
721 return rc;
722}
723
724/*********************************************************************************************************************************
725 * GuestDnD implementation. *
726 ********************************************************************************************************************************/
727
728/** Static (Singleton) instance of the GuestDnD object. */
729GuestDnD* GuestDnD::s_pInstance = NULL;
730
731GuestDnD::GuestDnD(const ComObjPtr<Guest> &pGuest)
732 : m_pGuest(pGuest)
733 , m_cTransfersPending(0)
734{
735 LogFlowFuncEnter();
736
737 try
738 {
739 m_pState = new GuestDnDState(pGuest);
740 }
741 catch (std::bad_alloc &)
742 {
743 throw VERR_NO_MEMORY;
744 }
745
746 int rc = RTCritSectInit(&m_CritSect);
747 if (RT_FAILURE(rc))
748 throw rc;
749
750 /* List of supported default MIME types. */
751 LogRel2(("DnD: Supported default host formats:\n"));
752 const com::Utf8Str arrEntries[] = { VBOX_DND_FORMATS_DEFAULT };
753 for (size_t i = 0; i < RT_ELEMENTS(arrEntries); i++)
754 {
755 m_strDefaultFormats.push_back(arrEntries[i]);
756 LogRel2(("DnD: \t%s\n", arrEntries[i].c_str()));
757 }
758}
759
760GuestDnD::~GuestDnD(void)
761{
762 LogFlowFuncEnter();
763
764 Assert(m_cTransfersPending == 0); /* Sanity. */
765
766 RTCritSectDelete(&m_CritSect);
767
768 if (m_pState)
769 delete m_pState;
770}
771
772/**
773 * Adjusts coordinations to a given screen.
774 *
775 * @returns HRESULT
776 * @param uScreenId ID of screen to adjust coordinates to.
777 * @param puX Pointer to X coordinate to adjust. Will return the adjusted value on success.
778 * @param puY Pointer to Y coordinate to adjust. Will return the adjusted value on success.
779 */
780HRESULT GuestDnD::adjustScreenCoordinates(ULONG uScreenId, ULONG *puX, ULONG *puY) const
781{
782 /** @todo r=andy Save the current screen's shifting coordinates to speed things up.
783 * Only query for new offsets when the screen ID or the screen's resolution has changed. */
784
785 /* For multi-monitor support we need to add shift values to the coordinates
786 * (depending on the screen number). */
787 ComObjPtr<Console> pConsole = m_pGuest->i_getConsole();
788 ComPtr<IDisplay> pDisplay;
789 HRESULT hr = pConsole->COMGETTER(Display)(pDisplay.asOutParam());
790 if (FAILED(hr))
791 return hr;
792
793 ULONG dummy;
794 LONG xShift, yShift;
795 GuestMonitorStatus_T monitorStatus;
796 hr = pDisplay->GetScreenResolution(uScreenId, &dummy, &dummy, &dummy,
797 &xShift, &yShift, &monitorStatus);
798 if (FAILED(hr))
799 return hr;
800
801 if (puX)
802 *puX += xShift;
803 if (puY)
804 *puY += yShift;
805
806 LogFlowFunc(("uScreenId=%RU32, x=%RU32, y=%RU32\n", uScreenId, puX ? *puX : 0, puY ? *puY : 0));
807 return S_OK;
808}
809
810/**
811 * Returns a DnD guest state.
812 *
813 * @returns Pointer to DnD guest state, or NULL if not found / invalid.
814 * @param uID ID of DnD guest state to return.
815 */
816GuestDnDState *GuestDnD::getState(uint32_t uID /* = 0 */) const
817{
818 AssertMsgReturn(uID == 0, ("Only one state (0) is supported at the moment\n"), NULL);
819
820 return m_pState;
821}
822
823/**
824 * Sends a (blocking) message to the host side of the host service.
825 *
826 * @returns VBox status code.
827 * @param u32Function HGCM message ID to send.
828 * @param cParms Number of parameters to send.
829 * @param paParms Array of parameters to send. Must match \c cParms.
830 */
831int GuestDnD::hostCall(uint32_t u32Function, uint32_t cParms, PVBOXHGCMSVCPARM paParms) const
832{
833 Assert(!m_pGuest.isNull());
834 ComObjPtr<Console> pConsole = m_pGuest->i_getConsole();
835
836 /* Forward the information to the VMM device. */
837 Assert(!pConsole.isNull());
838 VMMDev *pVMMDev = pConsole->i_getVMMDev();
839 if (!pVMMDev)
840 return VERR_COM_OBJECT_NOT_FOUND;
841
842 return pVMMDev->hgcmHostCall("VBoxDragAndDropSvc", u32Function, cParms, paParms);
843}
844
845/**
846 * Registers a GuestDnDSource object with the GuestDnD manager.
847 *
848 * Currently only one source is supported at a time.
849 *
850 * @returns VBox status code.
851 * @param Source Source to register.
852 */
853int GuestDnD::registerSource(const ComObjPtr<GuestDnDSource> &Source)
854{
855 GUESTDND_LOCK();
856
857 Assert(m_lstSrc.size() == 0); /* We only support one source at a time at the moment. */
858 m_lstSrc.push_back(Source);
859
860 GUESTDND_UNLOCK();
861 return VINF_SUCCESS;
862}
863
864/**
865 * Unregisters a GuestDnDSource object from the GuestDnD manager.
866 *
867 * @returns VBox status code.
868 * @param Source Source to unregister.
869 */
870int GuestDnD::unregisterSource(const ComObjPtr<GuestDnDSource> &Source)
871{
872 GUESTDND_LOCK();
873
874 GuestDnDSrcList::iterator itSrc = std::find(m_lstSrc.begin(), m_lstSrc.end(), Source);
875 if (itSrc != m_lstSrc.end())
876 m_lstSrc.erase(itSrc);
877
878 GUESTDND_UNLOCK();
879 return VINF_SUCCESS;
880}
881
882/**
883 * Returns the current number of registered sources.
884 *
885 * @returns Current number of registered sources.
886 */
887size_t GuestDnD::getSourceCount(void)
888{
889 GUESTDND_LOCK_RET(0);
890
891 size_t cSources = m_lstSrc.size();
892
893 GUESTDND_UNLOCK();
894 return cSources;
895}
896
897/**
898 * Registers a GuestDnDTarget object with the GuestDnD manager.
899 *
900 * Currently only one target is supported at a time.
901 *
902 * @returns VBox status code.
903 * @param Target Target to register.
904 */
905int GuestDnD::registerTarget(const ComObjPtr<GuestDnDTarget> &Target)
906{
907 GUESTDND_LOCK();
908
909 Assert(m_lstTgt.size() == 0); /* We only support one target at a time at the moment. */
910 m_lstTgt.push_back(Target);
911
912 GUESTDND_UNLOCK();
913 return VINF_SUCCESS;
914}
915
916/**
917 * Unregisters a GuestDnDTarget object from the GuestDnD manager.
918 *
919 * @returns VBox status code.
920 * @param Target Target to unregister.
921 */
922int GuestDnD::unregisterTarget(const ComObjPtr<GuestDnDTarget> &Target)
923{
924 GUESTDND_LOCK();
925
926 GuestDnDTgtList::iterator itTgt = std::find(m_lstTgt.begin(), m_lstTgt.end(), Target);
927 if (itTgt != m_lstTgt.end())
928 m_lstTgt.erase(itTgt);
929
930 GUESTDND_UNLOCK();
931 return VINF_SUCCESS;
932}
933
934/**
935 * Returns the current number of registered targets.
936 *
937 * @returns Current number of registered targets.
938 */
939size_t GuestDnD::getTargetCount(void)
940{
941 GUESTDND_LOCK_RET(0);
942
943 size_t cTargets = m_lstTgt.size();
944
945 GUESTDND_UNLOCK();
946 return cTargets;
947}
948
949/**
950 * Static main dispatcher function to handle callbacks from the DnD host service.
951 *
952 * @returns VBox status code.
953 * @param pvExtension Pointer to service extension.
954 * @param u32Function Callback HGCM message ID.
955 * @param pvParms Pointer to optional data provided for a particular message. Optional.
956 * @param cbParms Size (in bytes) of \a pvParms.
957 */
958/* static */
959DECLCALLBACK(int) GuestDnD::notifyDnDDispatcher(void *pvExtension, uint32_t u32Function,
960 void *pvParms, uint32_t cbParms)
961{
962 LogFlowFunc(("pvExtension=%p, u32Function=%RU32, pvParms=%p, cbParms=%RU32\n",
963 pvExtension, u32Function, pvParms, cbParms));
964
965 GuestDnD *pGuestDnD = reinterpret_cast<GuestDnD*>(pvExtension);
966 AssertPtrReturn(pGuestDnD, VERR_INVALID_POINTER);
967
968 /** @todo In case we need to handle multiple guest DnD responses at a time this
969 * would be the place to lookup and dispatch to those. For the moment we
970 * only have one response -- simple. */
971 if (pGuestDnD->m_pState)
972 return pGuestDnD->m_pState->onDispatch(u32Function, pvParms, cbParms);
973
974 return VERR_NOT_SUPPORTED;
975}
976
977/**
978 * Static helper function to determine whether a format is part of a given MIME list.
979 *
980 * @returns \c true if found, \c false if not.
981 * @param strFormat Format to search for.
982 * @param lstFormats MIME list to search in.
983 */
984/* static */
985bool GuestDnD::isFormatInFormatList(const com::Utf8Str &strFormat, const GuestDnDMIMEList &lstFormats)
986{
987 return std::find(lstFormats.begin(), lstFormats.end(), strFormat) != lstFormats.end();
988}
989
990/**
991 * Static helper function to create a GuestDnDMIMEList out of a format list string.
992 *
993 * @returns MIME list object.
994 * @param strFormats List of formats to convert.
995 * @param strSep Separator to use. If not specified, DND_FORMATS_SEPARATOR_STR will be used.
996 */
997/* static */
998GuestDnDMIMEList GuestDnD::toFormatList(const com::Utf8Str &strFormats, const com::Utf8Str &strSep /* = DND_FORMATS_SEPARATOR_STR */)
999{
1000 GuestDnDMIMEList lstFormats;
1001 RTCList<RTCString> lstFormatsTmp = strFormats.split(strSep);
1002
1003 for (size_t i = 0; i < lstFormatsTmp.size(); i++)
1004 lstFormats.push_back(com::Utf8Str(lstFormatsTmp.at(i)));
1005
1006 return lstFormats;
1007}
1008
1009/**
1010 * Static helper function to create a format list string from a given GuestDnDMIMEList object.
1011 *
1012 * @returns Format list string.
1013 * @param lstFormats GuestDnDMIMEList to convert.
1014 */
1015/* static */
1016com::Utf8Str GuestDnD::toFormatString(const GuestDnDMIMEList &lstFormats)
1017{
1018 com::Utf8Str strFormat;
1019 for (size_t i = 0; i < lstFormats.size(); i++)
1020 {
1021 const com::Utf8Str &f = lstFormats.at(i);
1022 strFormat += f + DND_FORMATS_SEPARATOR_STR;
1023 }
1024
1025 return strFormat;
1026}
1027
1028/**
1029 * Static helper function to create a filtered GuestDnDMIMEList object from supported and wanted formats.
1030 *
1031 * @returns Filtered MIME list object.
1032 * @param lstFormatsSupported MIME list of supported formats.
1033 * @param lstFormatsWanted MIME list of wanted formats in returned object.
1034 */
1035/* static */
1036GuestDnDMIMEList GuestDnD::toFilteredFormatList(const GuestDnDMIMEList &lstFormatsSupported, const GuestDnDMIMEList &lstFormatsWanted)
1037{
1038 GuestDnDMIMEList lstFormats;
1039
1040 for (size_t i = 0; i < lstFormatsWanted.size(); i++)
1041 {
1042 /* Only keep supported format types. */
1043 if (std::find(lstFormatsSupported.begin(),
1044 lstFormatsSupported.end(), lstFormatsWanted.at(i)) != lstFormatsSupported.end())
1045 {
1046 lstFormats.push_back(lstFormatsWanted[i]);
1047 }
1048 }
1049
1050 return lstFormats;
1051}
1052
1053/**
1054 * Static helper function to create a filtered GuestDnDMIMEList object from supported and wanted formats.
1055 *
1056 * @returns Filtered MIME list object.
1057 * @param lstFormatsSupported MIME list of supported formats.
1058 * @param strFormatsWanted Format list string of wanted formats in returned object.
1059 */
1060/* static */
1061GuestDnDMIMEList GuestDnD::toFilteredFormatList(const GuestDnDMIMEList &lstFormatsSupported, const com::Utf8Str &strFormatsWanted)
1062{
1063 GuestDnDMIMEList lstFmt;
1064
1065 RTCList<RTCString> lstFormats = strFormatsWanted.split(DND_FORMATS_SEPARATOR_STR);
1066 size_t i = 0;
1067 while (i < lstFormats.size())
1068 {
1069 /* Only keep allowed format types. */
1070 if (std::find(lstFormatsSupported.begin(),
1071 lstFormatsSupported.end(), lstFormats.at(i)) != lstFormatsSupported.end())
1072 {
1073 lstFmt.push_back(lstFormats[i]);
1074 }
1075 i++;
1076 }
1077
1078 return lstFmt;
1079}
1080
1081/**
1082 * Static helper function to convert a Main DnD action an internal DnD action.
1083 *
1084 * @returns Internal DnD action, or VBOX_DND_ACTION_IGNORE if not found / supported.
1085 * @param enmAction Main DnD action to convert.
1086 */
1087/* static */
1088VBOXDNDACTION GuestDnD::toHGCMAction(DnDAction_T enmAction)
1089{
1090 VBOXDNDACTION dndAction = VBOX_DND_ACTION_IGNORE;
1091 switch (enmAction)
1092 {
1093 case DnDAction_Copy:
1094 dndAction = VBOX_DND_ACTION_COPY;
1095 break;
1096 case DnDAction_Move:
1097 dndAction = VBOX_DND_ACTION_MOVE;
1098 break;
1099 case DnDAction_Link:
1100 /* For now it doesn't seems useful to allow a link
1101 action between host & guest. Later? */
1102 case DnDAction_Ignore:
1103 /* Ignored. */
1104 break;
1105 default:
1106 AssertMsgFailed(("Action %RU32 not recognized!\n", enmAction));
1107 break;
1108 }
1109
1110 return dndAction;
1111}
1112
1113/**
1114 * Static helper function to convert a Main DnD default action and allowed Main actions to their
1115 * corresponding internal representations.
1116 *
1117 * @param enmDnDActionDefault Default Main action to convert.
1118 * @param pDnDActionDefault Where to store the converted default action.
1119 * @param vecDnDActionsAllowed Allowed Main actions to convert.
1120 * @param pDnDLstActionsAllowed Where to store the converted allowed actions.
1121 */
1122/* static */
1123void GuestDnD::toHGCMActions(DnDAction_T enmDnDActionDefault,
1124 VBOXDNDACTION *pDnDActionDefault,
1125 const std::vector<DnDAction_T> vecDnDActionsAllowed,
1126 VBOXDNDACTIONLIST *pDnDLstActionsAllowed)
1127{
1128 VBOXDNDACTIONLIST dndLstActionsAllowed = VBOX_DND_ACTION_IGNORE;
1129 VBOXDNDACTION dndActionDefault = toHGCMAction(enmDnDActionDefault);
1130
1131 if (!vecDnDActionsAllowed.empty())
1132 {
1133 /* First convert the allowed actions to a bit array. */
1134 for (size_t i = 0; i < vecDnDActionsAllowed.size(); i++)
1135 dndLstActionsAllowed |= toHGCMAction(vecDnDActionsAllowed[i]);
1136
1137 /*
1138 * If no default action is set (ignoring), try one of the
1139 * set allowed actions, preferring copy, move (in that order).
1140 */
1141 if (isDnDIgnoreAction(dndActionDefault))
1142 {
1143 if (hasDnDCopyAction(dndLstActionsAllowed))
1144 dndActionDefault = VBOX_DND_ACTION_COPY;
1145 else if (hasDnDMoveAction(dndLstActionsAllowed))
1146 dndActionDefault = VBOX_DND_ACTION_MOVE;
1147 }
1148 }
1149
1150 if (pDnDActionDefault)
1151 *pDnDActionDefault = dndActionDefault;
1152 if (pDnDLstActionsAllowed)
1153 *pDnDLstActionsAllowed = dndLstActionsAllowed;
1154}
1155
1156/**
1157 * Static helper function to convert an internal DnD action to its Main representation.
1158 *
1159 * @returns Converted Main DnD action.
1160 * @param dndAction DnD action to convert.
1161 */
1162/* static */
1163DnDAction_T GuestDnD::toMainAction(VBOXDNDACTION dndAction)
1164{
1165 /* For now it doesn't seems useful to allow a
1166 * link action between host & guest. Maybe later! */
1167 return isDnDCopyAction(dndAction) ? DnDAction_Copy
1168 : isDnDMoveAction(dndAction) ? DnDAction_Move
1169 : DnDAction_Ignore;
1170}
1171
1172/**
1173 * Static helper function to convert an internal DnD action list to its Main representation.
1174 *
1175 * @returns Converted Main DnD action list.
1176 * @param dndActionList DnD action list to convert.
1177 */
1178/* static */
1179std::vector<DnDAction_T> GuestDnD::toMainActions(VBOXDNDACTIONLIST dndActionList)
1180{
1181 std::vector<DnDAction_T> vecActions;
1182
1183 /* For now it doesn't seems useful to allow a
1184 * link action between host & guest. Maybe later! */
1185 RTCList<DnDAction_T> lstActions;
1186 if (hasDnDCopyAction(dndActionList))
1187 lstActions.append(DnDAction_Copy);
1188 if (hasDnDMoveAction(dndActionList))
1189 lstActions.append(DnDAction_Move);
1190
1191 for (size_t i = 0; i < lstActions.size(); ++i)
1192 vecActions.push_back(lstActions.at(i));
1193
1194 return vecActions;
1195}
1196
1197/*********************************************************************************************************************************
1198 * GuestDnDBase implementation. *
1199 ********************************************************************************************************************************/
1200
1201GuestDnDBase::GuestDnDBase(void)
1202 : m_fIsPending(false)
1203{
1204 /* Initialize public attributes. */
1205 m_lstFmtSupported = GuestDnDInst()->defaultFormats();
1206}
1207
1208/**
1209 * Checks whether a given DnD format is supported or not.
1210 *
1211 * @returns \c true if supported, \c false if not.
1212 * @param aFormat DnD format to check.
1213 */
1214bool GuestDnDBase::i_isFormatSupported(const com::Utf8Str &aFormat) const
1215{
1216 return std::find(m_lstFmtSupported.begin(), m_lstFmtSupported.end(), aFormat) != m_lstFmtSupported.end();
1217}
1218
1219/**
1220 * Returns the currently supported DnD formats.
1221 *
1222 * @returns List of the supported DnD formats.
1223 */
1224const GuestDnDMIMEList &GuestDnDBase::i_getFormats(void) const
1225{
1226 return m_lstFmtSupported;
1227}
1228
1229/**
1230 * Adds DnD formats to the supported formats list.
1231 *
1232 * @returns HRESULT
1233 * @param aFormats List of DnD formats to add.
1234 */
1235HRESULT GuestDnDBase::i_addFormats(const GuestDnDMIMEList &aFormats)
1236{
1237 for (size_t i = 0; i < aFormats.size(); ++i)
1238 {
1239 Utf8Str strFormat = aFormats.at(i);
1240 if (std::find(m_lstFmtSupported.begin(),
1241 m_lstFmtSupported.end(), strFormat) == m_lstFmtSupported.end())
1242 {
1243 m_lstFmtSupported.push_back(strFormat);
1244 }
1245 }
1246
1247 return S_OK;
1248}
1249
1250/**
1251 * Removes DnD formats from tehh supported formats list.
1252 *
1253 * @returns HRESULT
1254 * @param aFormats List of DnD formats to remove.
1255 */
1256HRESULT GuestDnDBase::i_removeFormats(const GuestDnDMIMEList &aFormats)
1257{
1258 for (size_t i = 0; i < aFormats.size(); ++i)
1259 {
1260 Utf8Str strFormat = aFormats.at(i);
1261 GuestDnDMIMEList::iterator itFormat = std::find(m_lstFmtSupported.begin(),
1262 m_lstFmtSupported.end(), strFormat);
1263 if (itFormat != m_lstFmtSupported.end())
1264 m_lstFmtSupported.erase(itFormat);
1265 }
1266
1267 return S_OK;
1268}
1269
1270/**
1271 * Adds a new guest DnD message to the internal message queue.
1272 *
1273 * @returns VBox status code.
1274 * @param pMsg Pointer to message to add.
1275 */
1276int GuestDnDBase::msgQueueAdd(GuestDnDMsg *pMsg)
1277{
1278 m_DataBase.lstMsgOut.push_back(pMsg);
1279 return VINF_SUCCESS;
1280}
1281
1282/**
1283 * Returns the next guest DnD message in the internal message queue (FIFO).
1284 *
1285 * @returns Pointer to guest DnD message, or NULL if none found.
1286 */
1287GuestDnDMsg *GuestDnDBase::msgQueueGetNext(void)
1288{
1289 if (m_DataBase.lstMsgOut.empty())
1290 return NULL;
1291 return m_DataBase.lstMsgOut.front();
1292}
1293
1294/**
1295 * Removes the next guest DnD message from the internal message queue.
1296 */
1297void GuestDnDBase::msgQueueRemoveNext(void)
1298{
1299 if (!m_DataBase.lstMsgOut.empty())
1300 {
1301 GuestDnDMsg *pMsg = m_DataBase.lstMsgOut.front();
1302 if (pMsg)
1303 delete pMsg;
1304 m_DataBase.lstMsgOut.pop_front();
1305 }
1306}
1307
1308/**
1309 * Clears the internal message queue.
1310 */
1311void GuestDnDBase::msgQueueClear(void)
1312{
1313 LogFlowFunc(("cMsg=%zu\n", m_DataBase.lstMsgOut.size()));
1314
1315 GuestDnDMsgList::iterator itMsg = m_DataBase.lstMsgOut.begin();
1316 while (itMsg != m_DataBase.lstMsgOut.end())
1317 {
1318 GuestDnDMsg *pMsg = *itMsg;
1319 if (pMsg)
1320 delete pMsg;
1321
1322 itMsg++;
1323 }
1324
1325 m_DataBase.lstMsgOut.clear();
1326}
1327
1328/**
1329 * Sends a request to the guest side to cancel the current DnD operation.
1330 *
1331 * @returns VBox status code.
1332 */
1333int GuestDnDBase::sendCancel(void)
1334{
1335 GuestDnDMsg Msg;
1336 Msg.setType(HOST_DND_FN_CANCEL);
1337 if (m_pState->m_uProtocolVersion >= 3)
1338 Msg.appendUInt32(0); /** @todo ContextID not used yet. */
1339
1340 LogRel2(("DnD: Cancelling operation on guest ...\n"));
1341
1342 int rc = GuestDnDInst()->hostCall(Msg.getType(), Msg.getCount(), Msg.getParms());
1343 if (RT_FAILURE(rc))
1344 LogRel(("DnD: Cancelling operation on guest failed with %Rrc\n", rc));
1345
1346 return rc;
1347}
1348
1349/**
1350 * Helper function to update the progress based on given a GuestDnDData object.
1351 *
1352 * @returns VBox status code.
1353 * @param pData GuestDnDData object to use for accounting.
1354 * @param pState Guest state to update its progress object for.
1355 * @param cbDataAdd By how much data (in bytes) to update the progress.
1356 */
1357int GuestDnDBase::updateProgress(GuestDnDData *pData, GuestDnDState *pState,
1358 size_t cbDataAdd /* = 0 */)
1359{
1360 AssertPtrReturn(pData, VERR_INVALID_POINTER);
1361 AssertPtrReturn(pState, VERR_INVALID_POINTER);
1362 /* cbDataAdd is optional. */
1363
1364 LogFlowFunc(("cbExtra=%zu, cbProcessed=%zu, cbRemaining=%zu, cbDataAdd=%zu\n",
1365 pData->cbExtra, pData->cbProcessed, pData->getRemaining(), cbDataAdd));
1366
1367 if ( !pState
1368 || !cbDataAdd) /* Only update if something really changes. */
1369 return VINF_SUCCESS;
1370
1371 if (cbDataAdd)
1372 pData->addProcessed(cbDataAdd);
1373
1374 const uint8_t uPercent = pData->getPercentComplete();
1375
1376 LogRel2(("DnD: Transfer %RU8%% complete\n", uPercent));
1377
1378 int rc = pState->setProgress(uPercent,
1379 pData->isComplete()
1380 ? DND_PROGRESS_COMPLETE
1381 : DND_PROGRESS_RUNNING);
1382 LogFlowFuncLeaveRC(rc);
1383 return rc;
1384}
1385
1386/**
1387 * Waits for a specific guest callback event to get signalled.
1388 *
1389 * @returns VBox status code. Will return VERR_CANCELLED if the user has cancelled the progress object.
1390 * @param pEvent Callback event to wait for.
1391 * @param pState Guest state to update.
1392 * @param msTimeout Timeout (in ms) to wait.
1393 */
1394int GuestDnDBase::waitForEvent(GuestDnDCallbackEvent *pEvent, GuestDnDState *pState, RTMSINTERVAL msTimeout)
1395{
1396 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
1397 AssertPtrReturn(pState, VERR_INVALID_POINTER);
1398
1399 int rc;
1400
1401 LogFlowFunc(("msTimeout=%RU32\n", msTimeout));
1402
1403 uint64_t tsStart = RTTimeMilliTS();
1404 do
1405 {
1406 /*
1407 * Wait until our desired callback triggered the
1408 * wait event. As we don't want to block if the guest does not
1409 * respond, do busy waiting here.
1410 */
1411 rc = pEvent->Wait(500 /* ms */);
1412 if (RT_SUCCESS(rc))
1413 {
1414 rc = pEvent->Result();
1415 LogFlowFunc(("Callback done, result is %Rrc\n", rc));
1416 break;
1417 }
1418 else if (rc == VERR_TIMEOUT) /* Continue waiting. */
1419 rc = VINF_SUCCESS;
1420
1421 if ( msTimeout != RT_INDEFINITE_WAIT
1422 && RTTimeMilliTS() - tsStart > msTimeout)
1423 {
1424 rc = VERR_TIMEOUT;
1425 LogRel2(("DnD: Error: Guest did not respond within time\n"));
1426 }
1427 else if (pState->isProgressCanceled())
1428 {
1429 LogRel2(("DnD: Operation was canceled by user\n"));
1430 rc = VERR_CANCELLED;
1431 }
1432
1433 } while (RT_SUCCESS(rc));
1434
1435 LogFlowFuncLeaveRC(rc);
1436 return rc;
1437}
1438#endif /* VBOX_WITH_DRAG_AND_DROP */
1439
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