VirtualBox

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

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

DnD/Main: Don't set any (new) progress if the progress object already has been completed and/or cancelled.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 46.5 KB
Line 
1/* $Id: GuestDnDPrivate.cpp 97760 2022-12-07 15:13:42Z 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 if (m_pProgress.isNull())
434 return VINF_SUCCESS;
435
436 BOOL fCompleted = FALSE;
437 hr = m_pProgress->COMGETTER(Completed)(&fCompleted);
438 AssertComRCReturn(hr, VERR_COM_UNEXPECTED);
439
440 BOOL fCanceled = FALSE;
441 hr = m_pProgress->COMGETTER(Canceled)(&fCanceled);
442 AssertComRCReturn(hr, VERR_COM_UNEXPECTED);
443
444 LogFlowFunc(("Progress fCompleted=%RTbool, fCanceled=%RTbool\n", fCompleted, fCanceled));
445
446 int rc = VINF_SUCCESS;
447
448 switch (uStatus)
449 {
450 case DragAndDropSvc::DND_PROGRESS_ERROR:
451 {
452 LogRel(("DnD: Guest reported error %Rrc\n", rcOp));
453
454 if ( !fCompleted
455 && !fCanceled)
456 hr = m_pProgress->i_notifyComplete(VBOX_E_DND_ERROR,
457 COM_IIDOF(IGuest),
458 m_pParent->getComponentName(), strMsg.c_str());
459 reset();
460 break;
461 }
462
463 case DragAndDropSvc::DND_PROGRESS_CANCELLED:
464 {
465 LogRel2(("DnD: Guest cancelled operation\n"));
466
467 if ( !fCompleted
468 && !fCanceled)
469 {
470 hr = m_pProgress->Cancel();
471 AssertComRC(hr);
472 hr = m_pProgress->i_notifyComplete(S_OK);
473 AssertComRC(hr);
474 }
475
476 reset();
477 break;
478 }
479
480 case DragAndDropSvc::DND_PROGRESS_RUNNING:
481 RT_FALL_THROUGH();
482 case DragAndDropSvc::DND_PROGRESS_COMPLETE:
483 {
484 LogRel2(("DnD: Guest reporting running/completion status with %u%%\n", uPercentage));
485
486 if ( !fCompleted
487 && !fCanceled)
488 {
489 hr = m_pProgress->SetCurrentOperationProgress(uPercentage);
490 AssertComRCReturn(hr, VERR_COM_UNEXPECTED);
491 if ( uStatus == DragAndDropSvc::DND_PROGRESS_COMPLETE
492 || uPercentage >= 100)
493 {
494 hr = m_pProgress->i_notifyComplete(S_OK);
495 AssertComRCReturn(hr, VERR_COM_UNEXPECTED);
496 }
497 }
498 break;
499 }
500
501 default:
502 break;
503 }
504
505 LogFlowFuncLeaveRC(rc);
506 return rc;
507}
508
509/**
510 * Dispatching function for handling the host service service callback.
511 *
512 * @returns VBox status code.
513 * @param u32Function HGCM message ID to handle.
514 * @param pvParms Pointer to optional data provided for a particular message. Optional.
515 * @param cbParms Size (in bytes) of \a pvParms.
516 */
517int GuestDnDState::onDispatch(uint32_t u32Function, void *pvParms, uint32_t cbParms)
518{
519 LogFlowFunc(("u32Function=%RU32, pvParms=%p, cbParms=%RU32\n", u32Function, pvParms, cbParms));
520
521 int rc = VERR_WRONG_ORDER; /* Play safe. */
522
523 /* Whether or not to try calling host-installed callbacks after successfully processing the message. */
524 bool fTryCallbacks = false;
525
526 switch (u32Function)
527 {
528 case DragAndDropSvc::GUEST_DND_FN_CONNECT:
529 {
530 DragAndDropSvc::PVBOXDNDCBCONNECTDATA pCBData = reinterpret_cast<DragAndDropSvc::PVBOXDNDCBCONNECTDATA>(pvParms);
531 AssertPtr(pCBData);
532 AssertReturn(sizeof(DragAndDropSvc::VBOXDNDCBCONNECTDATA) == cbParms, VERR_INVALID_PARAMETER);
533 AssertReturn(DragAndDropSvc::CB_MAGIC_DND_CONNECT == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
534
535 m_uProtocolVersion = pCBData->uProtocolVersion;
536 /** @todo Handle flags. */
537
538 LogThisFunc(("Client connected, using protocol v%RU32\n", m_uProtocolVersion));
539
540 rc = VINF_SUCCESS;
541 break;
542 }
543
544 case DragAndDropSvc::GUEST_DND_FN_REPORT_FEATURES:
545 {
546 DragAndDropSvc::PVBOXDNDCBREPORTFEATURESDATA pCBData = reinterpret_cast<DragAndDropSvc::PVBOXDNDCBREPORTFEATURESDATA>(pvParms);
547 AssertPtr(pCBData);
548 AssertReturn(sizeof(DragAndDropSvc::VBOXDNDCBREPORTFEATURESDATA) == cbParms, VERR_INVALID_PARAMETER);
549 AssertReturn(DragAndDropSvc::CB_MAGIC_DND_REPORT_FEATURES == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
550
551 m_fGuestFeatures0 = pCBData->fGuestFeatures0;
552
553 LogThisFunc(("Client reported features: %#RX64\n", m_fGuestFeatures0));
554
555 rc = VINF_SUCCESS;
556 break;
557 }
558
559 case DragAndDropSvc::GUEST_DND_FN_DISCONNECT:
560 {
561 LogThisFunc(("Client disconnected\n"));
562 rc = setProgress(100, DND_PROGRESS_CANCELLED, VINF_SUCCESS);
563 break;
564 }
565
566 case DragAndDropSvc::GUEST_DND_FN_HG_ACK_OP:
567 {
568 DragAndDropSvc::PVBOXDNDCBHGACKOPDATA pCBData = reinterpret_cast<DragAndDropSvc::PVBOXDNDCBHGACKOPDATA>(pvParms);
569 AssertPtr(pCBData);
570 AssertReturn(sizeof(DragAndDropSvc::VBOXDNDCBHGACKOPDATA) == cbParms, VERR_INVALID_PARAMETER);
571 AssertReturn(DragAndDropSvc::CB_MAGIC_DND_HG_ACK_OP == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
572
573 LogRel2(("DnD: Guest responded with action '%s' for host->guest drag event\n", DnDActionToStr(pCBData->uAction)));
574
575 setActionDefault(pCBData->uAction);
576 rc = notifyAboutGuestResponse();
577 break;
578 }
579
580 case DragAndDropSvc::GUEST_DND_FN_HG_REQ_DATA:
581 {
582 DragAndDropSvc::PVBOXDNDCBHGREQDATADATA pCBData = reinterpret_cast<DragAndDropSvc::PVBOXDNDCBHGREQDATADATA>(pvParms);
583 AssertPtr(pCBData);
584 AssertReturn(sizeof(DragAndDropSvc::VBOXDNDCBHGREQDATADATA) == cbParms, VERR_INVALID_PARAMETER);
585 AssertReturn(DragAndDropSvc::CB_MAGIC_DND_HG_REQ_DATA == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
586
587 if ( pCBData->cbFormat == 0
588 || pCBData->cbFormat > _64K /** @todo Make this configurable? */
589 || pCBData->pszFormat == NULL)
590 {
591 rc = VERR_INVALID_PARAMETER;
592 }
593 else if (!RTStrIsValidEncoding(pCBData->pszFormat))
594 {
595 rc = VERR_INVALID_PARAMETER;
596 }
597 else
598 {
599 setFormats(GuestDnD::toFormatList(pCBData->pszFormat));
600 rc = VINF_SUCCESS;
601 }
602
603 int rc2 = notifyAboutGuestResponse();
604 if (RT_SUCCESS(rc))
605 rc = rc2;
606 break;
607 }
608
609 case DragAndDropSvc::GUEST_DND_FN_HG_EVT_PROGRESS:
610 {
611 DragAndDropSvc::PVBOXDNDCBHGEVTPROGRESSDATA pCBData =
612 reinterpret_cast<DragAndDropSvc::PVBOXDNDCBHGEVTPROGRESSDATA>(pvParms);
613 AssertPtr(pCBData);
614 AssertReturn(sizeof(DragAndDropSvc::VBOXDNDCBHGEVTPROGRESSDATA) == cbParms, VERR_INVALID_PARAMETER);
615 AssertReturn(DragAndDropSvc::CB_MAGIC_DND_HG_EVT_PROGRESS == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
616
617 rc = setProgress(pCBData->uPercentage, pCBData->uStatus, pCBData->rc);
618 if (RT_SUCCESS(rc))
619 rc = notifyAboutGuestResponse();
620 break;
621 }
622#ifdef VBOX_WITH_DRAG_AND_DROP_GH
623 case DragAndDropSvc::GUEST_DND_FN_GH_ACK_PENDING:
624 {
625 DragAndDropSvc::PVBOXDNDCBGHACKPENDINGDATA pCBData =
626 reinterpret_cast<DragAndDropSvc::PVBOXDNDCBGHACKPENDINGDATA>(pvParms);
627 AssertPtr(pCBData);
628 AssertReturn(sizeof(DragAndDropSvc::VBOXDNDCBGHACKPENDINGDATA) == cbParms, VERR_INVALID_PARAMETER);
629 AssertReturn(DragAndDropSvc::CB_MAGIC_DND_GH_ACK_PENDING == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
630
631 LogRel2(("DnD: Guest responded with pending action '%s' (%RU32 bytes format data) to guest->host drag event\n",
632 DnDActionToStr(pCBData->uDefAction), pCBData->cbFormat));
633
634 if ( pCBData->cbFormat == 0
635 || pCBData->cbFormat > _64K /** @todo Make the maximum size configurable? */
636 || pCBData->pszFormat == NULL)
637 {
638 rc = VERR_INVALID_PARAMETER;
639 }
640 else if (!RTStrIsValidEncoding(pCBData->pszFormat))
641 {
642 rc = VERR_INVALID_PARAMETER;
643 }
644 else
645 {
646 setFormats (GuestDnD::toFormatList(pCBData->pszFormat));
647 setActionDefault (pCBData->uDefAction);
648 setActionsAllowed(pCBData->uAllActions);
649
650 rc = VINF_SUCCESS;
651 }
652
653 int rc2 = notifyAboutGuestResponse();
654 if (RT_SUCCESS(rc))
655 rc = rc2;
656 break;
657 }
658#endif /* VBOX_WITH_DRAG_AND_DROP_GH */
659 default:
660 /* * Try if the event is covered by a registered callback. */
661 fTryCallbacks = true;
662 break;
663 }
664
665 /*
666 * Try the host's installed callbacks (if any).
667 */
668 if (fTryCallbacks)
669 {
670 GuestDnDCallbackMap::const_iterator it = m_mapCallbacks.find(u32Function);
671 if (it != m_mapCallbacks.end())
672 {
673 AssertPtr(it->second.pfnCallback);
674 rc = it->second.pfnCallback(u32Function, pvParms, cbParms, it->second.pvUser);
675 }
676 else
677 {
678 LogFlowFunc(("No callback for function %RU32 defined\n", u32Function));
679 rc = VERR_NOT_SUPPORTED; /* Tell the guest. */
680 }
681 }
682
683 LogFlowFunc(("Returning rc=%Rrc\n", rc));
684 return rc;
685}
686
687/**
688 * Helper function to query the internal progress object to an IProgress interface.
689 *
690 * @returns HRESULT
691 * @param ppProgress Where to query the progress object to.
692 */
693HRESULT GuestDnDState::queryProgressTo(IProgress **ppProgress)
694{
695 return m_pProgress.queryInterfaceTo(ppProgress);
696}
697
698/**
699 * Waits for a guest response to happen.
700 *
701 * @returns VBox status code.
702 * @param msTimeout Timeout (in ms) for waiting. Optional, waits 3000 ms if not specified.
703 */
704int GuestDnDState::waitForGuestResponse(RTMSINTERVAL msTimeout /*= 3000 */) const
705{
706 int rc = RTSemEventWait(m_EventSem, msTimeout);
707#ifdef DEBUG_andy
708 LogFlowFunc(("msTimeout=%RU32, rc=%Rrc\n", msTimeout, rc));
709#endif
710 return rc;
711}
712
713/*********************************************************************************************************************************
714 * GuestDnD implementation. *
715 ********************************************************************************************************************************/
716
717/** Static (Singleton) instance of the GuestDnD object. */
718GuestDnD* GuestDnD::s_pInstance = NULL;
719
720GuestDnD::GuestDnD(const ComObjPtr<Guest> &pGuest)
721 : m_pGuest(pGuest)
722 , m_cTransfersPending(0)
723{
724 LogFlowFuncEnter();
725
726 try
727 {
728 m_pState = new GuestDnDState(pGuest);
729 }
730 catch (std::bad_alloc &)
731 {
732 throw VERR_NO_MEMORY;
733 }
734
735 int rc = RTCritSectInit(&m_CritSect);
736 if (RT_FAILURE(rc))
737 throw rc;
738
739 /* List of supported default MIME types. */
740 LogRel2(("DnD: Supported default host formats:\n"));
741 const com::Utf8Str arrEntries[] = { VBOX_DND_FORMATS_DEFAULT };
742 for (size_t i = 0; i < RT_ELEMENTS(arrEntries); i++)
743 {
744 m_strDefaultFormats.push_back(arrEntries[i]);
745 LogRel2(("DnD: \t%s\n", arrEntries[i].c_str()));
746 }
747}
748
749GuestDnD::~GuestDnD(void)
750{
751 LogFlowFuncEnter();
752
753 Assert(m_cTransfersPending == 0); /* Sanity. */
754
755 RTCritSectDelete(&m_CritSect);
756
757 if (m_pState)
758 delete m_pState;
759}
760
761/**
762 * Adjusts coordinations to a given screen.
763 *
764 * @returns HRESULT
765 * @param uScreenId ID of screen to adjust coordinates to.
766 * @param puX Pointer to X coordinate to adjust. Will return the adjusted value on success.
767 * @param puY Pointer to Y coordinate to adjust. Will return the adjusted value on success.
768 */
769HRESULT GuestDnD::adjustScreenCoordinates(ULONG uScreenId, ULONG *puX, ULONG *puY) const
770{
771 /** @todo r=andy Save the current screen's shifting coordinates to speed things up.
772 * Only query for new offsets when the screen ID or the screen's resolution has changed. */
773
774 /* For multi-monitor support we need to add shift values to the coordinates
775 * (depending on the screen number). */
776 ComObjPtr<Console> pConsole = m_pGuest->i_getConsole();
777 ComPtr<IDisplay> pDisplay;
778 HRESULT hr = pConsole->COMGETTER(Display)(pDisplay.asOutParam());
779 if (FAILED(hr))
780 return hr;
781
782 ULONG dummy;
783 LONG xShift, yShift;
784 GuestMonitorStatus_T monitorStatus;
785 hr = pDisplay->GetScreenResolution(uScreenId, &dummy, &dummy, &dummy,
786 &xShift, &yShift, &monitorStatus);
787 if (FAILED(hr))
788 return hr;
789
790 if (puX)
791 *puX += xShift;
792 if (puY)
793 *puY += yShift;
794
795 LogFlowFunc(("uScreenId=%RU32, x=%RU32, y=%RU32\n", uScreenId, puX ? *puX : 0, puY ? *puY : 0));
796 return S_OK;
797}
798
799/**
800 * Returns a DnD guest state.
801 *
802 * @returns Pointer to DnD guest state, or NULL if not found / invalid.
803 * @param uID ID of DnD guest state to return.
804 */
805GuestDnDState *GuestDnD::getState(uint32_t uID /* = 0 */) const
806{
807 AssertMsgReturn(uID == 0, ("Only one state (0) is supported at the moment\n"), NULL);
808
809 return m_pState;
810}
811
812/**
813 * Sends a (blocking) message to the host side of the host service.
814 *
815 * @returns VBox status code.
816 * @param u32Function HGCM message ID to send.
817 * @param cParms Number of parameters to send.
818 * @param paParms Array of parameters to send. Must match \c cParms.
819 */
820int GuestDnD::hostCall(uint32_t u32Function, uint32_t cParms, PVBOXHGCMSVCPARM paParms) const
821{
822 Assert(!m_pGuest.isNull());
823 ComObjPtr<Console> pConsole = m_pGuest->i_getConsole();
824
825 /* Forward the information to the VMM device. */
826 Assert(!pConsole.isNull());
827 VMMDev *pVMMDev = pConsole->i_getVMMDev();
828 if (!pVMMDev)
829 return VERR_COM_OBJECT_NOT_FOUND;
830
831 return pVMMDev->hgcmHostCall("VBoxDragAndDropSvc", u32Function, cParms, paParms);
832}
833
834/**
835 * Registers a GuestDnDSource object with the GuestDnD manager.
836 *
837 * Currently only one source is supported at a time.
838 *
839 * @returns VBox status code.
840 * @param Source Source to register.
841 */
842int GuestDnD::registerSource(const ComObjPtr<GuestDnDSource> &Source)
843{
844 GUESTDND_LOCK();
845
846 Assert(m_lstSrc.size() == 0); /* We only support one source at a time at the moment. */
847 m_lstSrc.push_back(Source);
848
849 GUESTDND_UNLOCK();
850 return VINF_SUCCESS;
851}
852
853/**
854 * Unregisters a GuestDnDSource object from the GuestDnD manager.
855 *
856 * @returns VBox status code.
857 * @param Source Source to unregister.
858 */
859int GuestDnD::unregisterSource(const ComObjPtr<GuestDnDSource> &Source)
860{
861 GUESTDND_LOCK();
862
863 GuestDnDSrcList::iterator itSrc = std::find(m_lstSrc.begin(), m_lstSrc.end(), Source);
864 if (itSrc != m_lstSrc.end())
865 m_lstSrc.erase(itSrc);
866
867 GUESTDND_UNLOCK();
868 return VINF_SUCCESS;
869}
870
871/**
872 * Returns the current number of registered sources.
873 *
874 * @returns Current number of registered sources.
875 */
876size_t GuestDnD::getSourceCount(void)
877{
878 GUESTDND_LOCK_RET(0);
879
880 size_t cSources = m_lstSrc.size();
881
882 GUESTDND_UNLOCK();
883 return cSources;
884}
885
886/**
887 * Registers a GuestDnDTarget object with the GuestDnD manager.
888 *
889 * Currently only one target is supported at a time.
890 *
891 * @returns VBox status code.
892 * @param Target Target to register.
893 */
894int GuestDnD::registerTarget(const ComObjPtr<GuestDnDTarget> &Target)
895{
896 GUESTDND_LOCK();
897
898 Assert(m_lstTgt.size() == 0); /* We only support one target at a time at the moment. */
899 m_lstTgt.push_back(Target);
900
901 GUESTDND_UNLOCK();
902 return VINF_SUCCESS;
903}
904
905/**
906 * Unregisters a GuestDnDTarget object from the GuestDnD manager.
907 *
908 * @returns VBox status code.
909 * @param Target Target to unregister.
910 */
911int GuestDnD::unregisterTarget(const ComObjPtr<GuestDnDTarget> &Target)
912{
913 GUESTDND_LOCK();
914
915 GuestDnDTgtList::iterator itTgt = std::find(m_lstTgt.begin(), m_lstTgt.end(), Target);
916 if (itTgt != m_lstTgt.end())
917 m_lstTgt.erase(itTgt);
918
919 GUESTDND_UNLOCK();
920 return VINF_SUCCESS;
921}
922
923/**
924 * Returns the current number of registered targets.
925 *
926 * @returns Current number of registered targets.
927 */
928size_t GuestDnD::getTargetCount(void)
929{
930 GUESTDND_LOCK_RET(0);
931
932 size_t cTargets = m_lstTgt.size();
933
934 GUESTDND_UNLOCK();
935 return cTargets;
936}
937
938/**
939 * Static main dispatcher function to handle callbacks from the DnD host service.
940 *
941 * @returns VBox status code.
942 * @param pvExtension Pointer to service extension.
943 * @param u32Function Callback HGCM message ID.
944 * @param pvParms Pointer to optional data provided for a particular message. Optional.
945 * @param cbParms Size (in bytes) of \a pvParms.
946 */
947/* static */
948DECLCALLBACK(int) GuestDnD::notifyDnDDispatcher(void *pvExtension, uint32_t u32Function,
949 void *pvParms, uint32_t cbParms)
950{
951 LogFlowFunc(("pvExtension=%p, u32Function=%RU32, pvParms=%p, cbParms=%RU32\n",
952 pvExtension, u32Function, pvParms, cbParms));
953
954 GuestDnD *pGuestDnD = reinterpret_cast<GuestDnD*>(pvExtension);
955 AssertPtrReturn(pGuestDnD, VERR_INVALID_POINTER);
956
957 /** @todo In case we need to handle multiple guest DnD responses at a time this
958 * would be the place to lookup and dispatch to those. For the moment we
959 * only have one response -- simple. */
960 if (pGuestDnD->m_pState)
961 return pGuestDnD->m_pState->onDispatch(u32Function, pvParms, cbParms);
962
963 return VERR_NOT_SUPPORTED;
964}
965
966/**
967 * Static helper function to determine whether a format is part of a given MIME list.
968 *
969 * @returns \c true if found, \c false if not.
970 * @param strFormat Format to search for.
971 * @param lstFormats MIME list to search in.
972 */
973/* static */
974bool GuestDnD::isFormatInFormatList(const com::Utf8Str &strFormat, const GuestDnDMIMEList &lstFormats)
975{
976 return std::find(lstFormats.begin(), lstFormats.end(), strFormat) != lstFormats.end();
977}
978
979/**
980 * Static helper function to create a GuestDnDMIMEList out of a format list string.
981 *
982 * @returns MIME list object.
983 * @param strFormats List of formats to convert.
984 * @param strSep Separator to use. If not specified, DND_FORMATS_SEPARATOR_STR will be used.
985 */
986/* static */
987GuestDnDMIMEList GuestDnD::toFormatList(const com::Utf8Str &strFormats, const com::Utf8Str &strSep /* = DND_FORMATS_SEPARATOR_STR */)
988{
989 GuestDnDMIMEList lstFormats;
990 RTCList<RTCString> lstFormatsTmp = strFormats.split(strSep);
991
992 for (size_t i = 0; i < lstFormatsTmp.size(); i++)
993 lstFormats.push_back(com::Utf8Str(lstFormatsTmp.at(i)));
994
995 return lstFormats;
996}
997
998/**
999 * Static helper function to create a format list string from a given GuestDnDMIMEList object.
1000 *
1001 * @returns Format list string.
1002 * @param lstFormats GuestDnDMIMEList to convert.
1003 */
1004/* static */
1005com::Utf8Str GuestDnD::toFormatString(const GuestDnDMIMEList &lstFormats)
1006{
1007 com::Utf8Str strFormat;
1008 for (size_t i = 0; i < lstFormats.size(); i++)
1009 {
1010 const com::Utf8Str &f = lstFormats.at(i);
1011 strFormat += f + DND_FORMATS_SEPARATOR_STR;
1012 }
1013
1014 return strFormat;
1015}
1016
1017/**
1018 * Static helper function to create a filtered GuestDnDMIMEList object from supported and wanted formats.
1019 *
1020 * @returns Filtered MIME list object.
1021 * @param lstFormatsSupported MIME list of supported formats.
1022 * @param lstFormatsWanted MIME list of wanted formats in returned object.
1023 */
1024/* static */
1025GuestDnDMIMEList GuestDnD::toFilteredFormatList(const GuestDnDMIMEList &lstFormatsSupported, const GuestDnDMIMEList &lstFormatsWanted)
1026{
1027 GuestDnDMIMEList lstFormats;
1028
1029 for (size_t i = 0; i < lstFormatsWanted.size(); i++)
1030 {
1031 /* Only keep supported format types. */
1032 if (std::find(lstFormatsSupported.begin(),
1033 lstFormatsSupported.end(), lstFormatsWanted.at(i)) != lstFormatsSupported.end())
1034 {
1035 lstFormats.push_back(lstFormatsWanted[i]);
1036 }
1037 }
1038
1039 return lstFormats;
1040}
1041
1042/**
1043 * Static helper function to create a filtered GuestDnDMIMEList object from supported and wanted formats.
1044 *
1045 * @returns Filtered MIME list object.
1046 * @param lstFormatsSupported MIME list of supported formats.
1047 * @param strFormatsWanted Format list string of wanted formats in returned object.
1048 */
1049/* static */
1050GuestDnDMIMEList GuestDnD::toFilteredFormatList(const GuestDnDMIMEList &lstFormatsSupported, const com::Utf8Str &strFormatsWanted)
1051{
1052 GuestDnDMIMEList lstFmt;
1053
1054 RTCList<RTCString> lstFormats = strFormatsWanted.split(DND_FORMATS_SEPARATOR_STR);
1055 size_t i = 0;
1056 while (i < lstFormats.size())
1057 {
1058 /* Only keep allowed format types. */
1059 if (std::find(lstFormatsSupported.begin(),
1060 lstFormatsSupported.end(), lstFormats.at(i)) != lstFormatsSupported.end())
1061 {
1062 lstFmt.push_back(lstFormats[i]);
1063 }
1064 i++;
1065 }
1066
1067 return lstFmt;
1068}
1069
1070/**
1071 * Static helper function to convert a Main DnD action an internal DnD action.
1072 *
1073 * @returns Internal DnD action, or VBOX_DND_ACTION_IGNORE if not found / supported.
1074 * @param enmAction Main DnD action to convert.
1075 */
1076/* static */
1077VBOXDNDACTION GuestDnD::toHGCMAction(DnDAction_T enmAction)
1078{
1079 VBOXDNDACTION dndAction = VBOX_DND_ACTION_IGNORE;
1080 switch (enmAction)
1081 {
1082 case DnDAction_Copy:
1083 dndAction = VBOX_DND_ACTION_COPY;
1084 break;
1085 case DnDAction_Move:
1086 dndAction = VBOX_DND_ACTION_MOVE;
1087 break;
1088 case DnDAction_Link:
1089 /* For now it doesn't seems useful to allow a link
1090 action between host & guest. Later? */
1091 case DnDAction_Ignore:
1092 /* Ignored. */
1093 break;
1094 default:
1095 AssertMsgFailed(("Action %RU32 not recognized!\n", enmAction));
1096 break;
1097 }
1098
1099 return dndAction;
1100}
1101
1102/**
1103 * Static helper function to convert a Main DnD default action and allowed Main actions to their
1104 * corresponding internal representations.
1105 *
1106 * @param enmDnDActionDefault Default Main action to convert.
1107 * @param pDnDActionDefault Where to store the converted default action.
1108 * @param vecDnDActionsAllowed Allowed Main actions to convert.
1109 * @param pDnDLstActionsAllowed Where to store the converted allowed actions.
1110 */
1111/* static */
1112void GuestDnD::toHGCMActions(DnDAction_T enmDnDActionDefault,
1113 VBOXDNDACTION *pDnDActionDefault,
1114 const std::vector<DnDAction_T> vecDnDActionsAllowed,
1115 VBOXDNDACTIONLIST *pDnDLstActionsAllowed)
1116{
1117 VBOXDNDACTIONLIST dndLstActionsAllowed = VBOX_DND_ACTION_IGNORE;
1118 VBOXDNDACTION dndActionDefault = toHGCMAction(enmDnDActionDefault);
1119
1120 if (!vecDnDActionsAllowed.empty())
1121 {
1122 /* First convert the allowed actions to a bit array. */
1123 for (size_t i = 0; i < vecDnDActionsAllowed.size(); i++)
1124 dndLstActionsAllowed |= toHGCMAction(vecDnDActionsAllowed[i]);
1125
1126 /*
1127 * If no default action is set (ignoring), try one of the
1128 * set allowed actions, preferring copy, move (in that order).
1129 */
1130 if (isDnDIgnoreAction(dndActionDefault))
1131 {
1132 if (hasDnDCopyAction(dndLstActionsAllowed))
1133 dndActionDefault = VBOX_DND_ACTION_COPY;
1134 else if (hasDnDMoveAction(dndLstActionsAllowed))
1135 dndActionDefault = VBOX_DND_ACTION_MOVE;
1136 }
1137 }
1138
1139 if (pDnDActionDefault)
1140 *pDnDActionDefault = dndActionDefault;
1141 if (pDnDLstActionsAllowed)
1142 *pDnDLstActionsAllowed = dndLstActionsAllowed;
1143}
1144
1145/**
1146 * Static helper function to convert an internal DnD action to its Main representation.
1147 *
1148 * @returns Converted Main DnD action.
1149 * @param dndAction DnD action to convert.
1150 */
1151/* static */
1152DnDAction_T GuestDnD::toMainAction(VBOXDNDACTION dndAction)
1153{
1154 /* For now it doesn't seems useful to allow a
1155 * link action between host & guest. Maybe later! */
1156 return isDnDCopyAction(dndAction) ? DnDAction_Copy
1157 : isDnDMoveAction(dndAction) ? DnDAction_Move
1158 : DnDAction_Ignore;
1159}
1160
1161/**
1162 * Static helper function to convert an internal DnD action list to its Main representation.
1163 *
1164 * @returns Converted Main DnD action list.
1165 * @param dndActionList DnD action list to convert.
1166 */
1167/* static */
1168std::vector<DnDAction_T> GuestDnD::toMainActions(VBOXDNDACTIONLIST dndActionList)
1169{
1170 std::vector<DnDAction_T> vecActions;
1171
1172 /* For now it doesn't seems useful to allow a
1173 * link action between host & guest. Maybe later! */
1174 RTCList<DnDAction_T> lstActions;
1175 if (hasDnDCopyAction(dndActionList))
1176 lstActions.append(DnDAction_Copy);
1177 if (hasDnDMoveAction(dndActionList))
1178 lstActions.append(DnDAction_Move);
1179
1180 for (size_t i = 0; i < lstActions.size(); ++i)
1181 vecActions.push_back(lstActions.at(i));
1182
1183 return vecActions;
1184}
1185
1186/*********************************************************************************************************************************
1187 * GuestDnDBase implementation. *
1188 ********************************************************************************************************************************/
1189
1190GuestDnDBase::GuestDnDBase(void)
1191 : m_fIsPending(false)
1192{
1193 /* Initialize public attributes. */
1194 m_lstFmtSupported = GuestDnDInst()->defaultFormats();
1195}
1196
1197/**
1198 * Checks whether a given DnD format is supported or not.
1199 *
1200 * @returns \c true if supported, \c false if not.
1201 * @param aFormat DnD format to check.
1202 */
1203bool GuestDnDBase::i_isFormatSupported(const com::Utf8Str &aFormat) const
1204{
1205 return std::find(m_lstFmtSupported.begin(), m_lstFmtSupported.end(), aFormat) != m_lstFmtSupported.end();
1206}
1207
1208/**
1209 * Returns the currently supported DnD formats.
1210 *
1211 * @returns List of the supported DnD formats.
1212 */
1213const GuestDnDMIMEList &GuestDnDBase::i_getFormats(void) const
1214{
1215 return m_lstFmtSupported;
1216}
1217
1218/**
1219 * Adds DnD formats to the supported formats list.
1220 *
1221 * @returns HRESULT
1222 * @param aFormats List of DnD formats to add.
1223 */
1224HRESULT GuestDnDBase::i_addFormats(const GuestDnDMIMEList &aFormats)
1225{
1226 for (size_t i = 0; i < aFormats.size(); ++i)
1227 {
1228 Utf8Str strFormat = aFormats.at(i);
1229 if (std::find(m_lstFmtSupported.begin(),
1230 m_lstFmtSupported.end(), strFormat) == m_lstFmtSupported.end())
1231 {
1232 m_lstFmtSupported.push_back(strFormat);
1233 }
1234 }
1235
1236 return S_OK;
1237}
1238
1239/**
1240 * Removes DnD formats from tehh supported formats list.
1241 *
1242 * @returns HRESULT
1243 * @param aFormats List of DnD formats to remove.
1244 */
1245HRESULT GuestDnDBase::i_removeFormats(const GuestDnDMIMEList &aFormats)
1246{
1247 for (size_t i = 0; i < aFormats.size(); ++i)
1248 {
1249 Utf8Str strFormat = aFormats.at(i);
1250 GuestDnDMIMEList::iterator itFormat = std::find(m_lstFmtSupported.begin(),
1251 m_lstFmtSupported.end(), strFormat);
1252 if (itFormat != m_lstFmtSupported.end())
1253 m_lstFmtSupported.erase(itFormat);
1254 }
1255
1256 return S_OK;
1257}
1258
1259/**
1260 * Adds a new guest DnD message to the internal message queue.
1261 *
1262 * @returns VBox status code.
1263 * @param pMsg Pointer to message to add.
1264 */
1265int GuestDnDBase::msgQueueAdd(GuestDnDMsg *pMsg)
1266{
1267 m_DataBase.lstMsgOut.push_back(pMsg);
1268 return VINF_SUCCESS;
1269}
1270
1271/**
1272 * Returns the next guest DnD message in the internal message queue (FIFO).
1273 *
1274 * @returns Pointer to guest DnD message, or NULL if none found.
1275 */
1276GuestDnDMsg *GuestDnDBase::msgQueueGetNext(void)
1277{
1278 if (m_DataBase.lstMsgOut.empty())
1279 return NULL;
1280 return m_DataBase.lstMsgOut.front();
1281}
1282
1283/**
1284 * Removes the next guest DnD message from the internal message queue.
1285 */
1286void GuestDnDBase::msgQueueRemoveNext(void)
1287{
1288 if (!m_DataBase.lstMsgOut.empty())
1289 {
1290 GuestDnDMsg *pMsg = m_DataBase.lstMsgOut.front();
1291 if (pMsg)
1292 delete pMsg;
1293 m_DataBase.lstMsgOut.pop_front();
1294 }
1295}
1296
1297/**
1298 * Clears the internal message queue.
1299 */
1300void GuestDnDBase::msgQueueClear(void)
1301{
1302 LogFlowFunc(("cMsg=%zu\n", m_DataBase.lstMsgOut.size()));
1303
1304 GuestDnDMsgList::iterator itMsg = m_DataBase.lstMsgOut.begin();
1305 while (itMsg != m_DataBase.lstMsgOut.end())
1306 {
1307 GuestDnDMsg *pMsg = *itMsg;
1308 if (pMsg)
1309 delete pMsg;
1310
1311 itMsg++;
1312 }
1313
1314 m_DataBase.lstMsgOut.clear();
1315}
1316
1317/**
1318 * Sends a request to the guest side to cancel the current DnD operation.
1319 *
1320 * @returns VBox status code.
1321 */
1322int GuestDnDBase::sendCancel(void)
1323{
1324 GuestDnDMsg Msg;
1325 Msg.setType(HOST_DND_FN_CANCEL);
1326 if (m_pState->m_uProtocolVersion >= 3)
1327 Msg.appendUInt32(0); /** @todo ContextID not used yet. */
1328
1329 LogRel2(("DnD: Cancelling operation on guest ...\n"));
1330
1331 int rc = GuestDnDInst()->hostCall(Msg.getType(), Msg.getCount(), Msg.getParms());
1332 if (RT_FAILURE(rc))
1333 LogRel(("DnD: Cancelling operation on guest failed with %Rrc\n", rc));
1334
1335 return rc;
1336}
1337
1338/**
1339 * Helper function to update the progress based on given a GuestDnDData object.
1340 *
1341 * @returns VBox status code.
1342 * @param pData GuestDnDData object to use for accounting.
1343 * @param pState Guest state to update its progress object for.
1344 * @param cbDataAdd By how much data (in bytes) to update the progress.
1345 */
1346int GuestDnDBase::updateProgress(GuestDnDData *pData, GuestDnDState *pState,
1347 size_t cbDataAdd /* = 0 */)
1348{
1349 AssertPtrReturn(pData, VERR_INVALID_POINTER);
1350 AssertPtrReturn(pState, VERR_INVALID_POINTER);
1351 /* cbDataAdd is optional. */
1352
1353 LogFlowFunc(("cbExtra=%zu, cbProcessed=%zu, cbRemaining=%zu, cbDataAdd=%zu\n",
1354 pData->cbExtra, pData->cbProcessed, pData->getRemaining(), cbDataAdd));
1355
1356 if ( !pState
1357 || !cbDataAdd) /* Only update if something really changes. */
1358 return VINF_SUCCESS;
1359
1360 if (cbDataAdd)
1361 pData->addProcessed(cbDataAdd);
1362
1363 const uint8_t uPercent = pData->getPercentComplete();
1364
1365 LogRel2(("DnD: Transfer %RU8%% complete\n", uPercent));
1366
1367 int rc = pState->setProgress(uPercent,
1368 pData->isComplete()
1369 ? DND_PROGRESS_COMPLETE
1370 : DND_PROGRESS_RUNNING);
1371 LogFlowFuncLeaveRC(rc);
1372 return rc;
1373}
1374
1375/**
1376 * Waits for a specific guest callback event to get signalled.
1377 *
1378 * @returns VBox status code. Will return VERR_CANCELLED if the user has cancelled the progress object.
1379 * @param pEvent Callback event to wait for.
1380 * @param pState Guest state to update.
1381 * @param msTimeout Timeout (in ms) to wait.
1382 */
1383int GuestDnDBase::waitForEvent(GuestDnDCallbackEvent *pEvent, GuestDnDState *pState, RTMSINTERVAL msTimeout)
1384{
1385 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
1386 AssertPtrReturn(pState, VERR_INVALID_POINTER);
1387
1388 int rc;
1389
1390 LogFlowFunc(("msTimeout=%RU32\n", msTimeout));
1391
1392 uint64_t tsStart = RTTimeMilliTS();
1393 do
1394 {
1395 /*
1396 * Wait until our desired callback triggered the
1397 * wait event. As we don't want to block if the guest does not
1398 * respond, do busy waiting here.
1399 */
1400 rc = pEvent->Wait(500 /* ms */);
1401 if (RT_SUCCESS(rc))
1402 {
1403 rc = pEvent->Result();
1404 LogFlowFunc(("Callback done, result is %Rrc\n", rc));
1405 break;
1406 }
1407 else if (rc == VERR_TIMEOUT) /* Continue waiting. */
1408 rc = VINF_SUCCESS;
1409
1410 if ( msTimeout != RT_INDEFINITE_WAIT
1411 && RTTimeMilliTS() - tsStart > msTimeout)
1412 {
1413 rc = VERR_TIMEOUT;
1414 LogRel2(("DnD: Error: Guest did not respond within time\n"));
1415 }
1416 else if (pState->isProgressCanceled())
1417 {
1418 LogRel2(("DnD: Operation was canceled by user\n"));
1419 rc = VERR_CANCELLED;
1420 }
1421
1422 } while (RT_SUCCESS(rc));
1423
1424 LogFlowFuncLeaveRC(rc);
1425 return rc;
1426}
1427#endif /* VBOX_WITH_DRAG_AND_DROP */
1428
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