VirtualBox

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

Last change on this file since 98103 was 98103, checked in by vboxsync, 17 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 54.0 KB
Line 
1/* $Id: GuestDnDPrivate.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * Private guest drag and drop code, used by GuestDnDTarget + GuestDnDSource.
4 */
5
6/*
7 * Copyright (C) 2011-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#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 uScreenID = 0;
207
208 Transfer.reset();
209
210 int rc2 = EventCallback.Reset();
211 AssertRC(rc2);
212
213 GuestDnDData::reset();
214}
215
216/*********************************************************************************************************************************
217 * GuestDnDRecvCtx implementation. *
218 ********************************************************************************************************************************/
219
220GuestDnDRecvCtx::GuestDnDRecvCtx(void)
221 : pSource(NULL)
222 , pState(NULL)
223{
224 reset();
225}
226
227/**
228 * Resets a GuestDnDRecvCtx object.
229 */
230void GuestDnDRecvCtx::reset(void)
231{
232 lstFmtOffered.clear();
233 strFmtReq = "";
234 strFmtRecv = "";
235 enmAction = 0;
236
237 Transfer.reset();
238
239 int rc2 = EventCallback.Reset();
240 AssertRC(rc2);
241
242 GuestDnDData::reset();
243}
244
245/*********************************************************************************************************************************
246 * GuestDnDCallbackEvent implementation. *
247 ********************************************************************************************************************************/
248
249GuestDnDCallbackEvent::~GuestDnDCallbackEvent(void)
250{
251 if (NIL_RTSEMEVENT != m_SemEvent)
252 RTSemEventDestroy(m_SemEvent);
253}
254
255/**
256 * Resets a GuestDnDCallbackEvent object.
257 */
258int GuestDnDCallbackEvent::Reset(void)
259{
260 int rc = VINF_SUCCESS;
261
262 if (NIL_RTSEMEVENT == m_SemEvent)
263 rc = RTSemEventCreate(&m_SemEvent);
264
265 m_Rc = VINF_SUCCESS;
266 return rc;
267}
268
269/**
270 * Completes a callback event by notifying the waiting side.
271 *
272 * @returns VBox status code.
273 * @param rc Result code to use for the event completion.
274 */
275int GuestDnDCallbackEvent::Notify(int rc /* = VINF_SUCCESS */)
276{
277 m_Rc = rc;
278 return RTSemEventSignal(m_SemEvent);
279}
280
281/**
282 * Waits on a callback event for being notified.
283 *
284 * @returns VBox status code.
285 * @param msTimeout Timeout (in ms) to wait for callback event.
286 */
287int GuestDnDCallbackEvent::Wait(RTMSINTERVAL msTimeout)
288{
289 return RTSemEventWait(m_SemEvent, msTimeout);
290}
291
292/********************************************************************************************************************************
293 *
294 ********************************************************************************************************************************/
295
296GuestDnDState::GuestDnDState(const ComObjPtr<Guest>& pGuest)
297 : m_uProtocolVersion(0)
298 , m_fGuestFeatures0(VBOX_DND_GF_NONE)
299 , m_EventSem(NIL_RTSEMEVENT)
300 , m_pParent(pGuest)
301{
302 reset();
303
304 int rc = RTCritSectInit(&m_CritSect);
305 if (RT_FAILURE(rc))
306 throw rc;
307 rc = RTSemEventCreate(&m_EventSem);
308 if (RT_FAILURE(rc))
309 throw rc;
310}
311
312GuestDnDState::~GuestDnDState(void)
313{
314 int rc = RTSemEventDestroy(m_EventSem);
315 AssertRC(rc);
316 rc = RTCritSectDelete(&m_CritSect);
317 AssertRC(rc);
318}
319
320/**
321 * Notifies the waiting side about a guest notification response.
322 *
323 * @returns VBox status code.
324 * @param rcGuest Guest rc to set for the response.
325 * Defaults to VINF_SUCCESS (for success).
326 */
327int GuestDnDState::notifyAboutGuestResponse(int rcGuest /* = VINF_SUCCESS */)
328{
329 m_rcGuest = rcGuest;
330 return RTSemEventSignal(m_EventSem);
331}
332
333/**
334 * Resets a guest drag'n drop state.
335 */
336void GuestDnDState::reset(void)
337{
338 LogRel2(("DnD: Reset\n"));
339
340 m_enmState = VBOXDNDSTATE_UNKNOWN;
341
342 m_dndActionDefault = VBOX_DND_ACTION_IGNORE;
343 m_dndLstActionsAllowed = VBOX_DND_ACTION_IGNORE;
344
345 m_lstFormats.clear();
346 m_mapCallbacks.clear();
347
348 m_rcGuest = VERR_IPE_UNINITIALIZED_STATUS;
349}
350
351/**
352 * Default callback handler for guest callbacks.
353 *
354 * This handler acts as a fallback in case important callback messages are not being handled
355 * by the specific callers.
356 *
357 * @returns VBox status code. Will get sent back to the host service.
358 * @retval VERR_NO_DATA if no new messages from the host side are available at the moment.
359 * @retval VERR_CANCELLED for indicating that the current operation was cancelled.
360 * @param uMsg HGCM message ID (function number).
361 * @param pvParms Pointer to additional message data. Optional and can be NULL.
362 * @param cbParms Size (in bytes) additional message data. Optional and can be 0.
363 * @param pvUser User-supplied pointer on callback registration.
364 */
365/* static */
366DECLCALLBACK(int) GuestDnDState::i_defaultCallback(uint32_t uMsg, void *pvParms, size_t cbParms, void *pvUser)
367{
368 GuestDnDState *pThis = (GuestDnDState *)pvUser;
369 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
370
371 LogFlowFunc(("uMsg=%RU32 (%#x)\n", uMsg, uMsg));
372
373 int vrc = VERR_IPE_UNINITIALIZED_STATUS;
374
375 switch (uMsg)
376 {
377 case GUEST_DND_FN_EVT_ERROR:
378 {
379 PVBOXDNDCBEVTERRORDATA pCBData = reinterpret_cast<PVBOXDNDCBEVTERRORDATA>(pvParms);
380 AssertPtr(pCBData);
381 AssertReturn(sizeof(VBOXDNDCBEVTERRORDATA) == cbParms, VERR_INVALID_PARAMETER);
382 AssertReturn(CB_MAGIC_DND_EVT_ERROR == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
383
384 if (RT_SUCCESS(pCBData->rc))
385 {
386 AssertMsgFailed(("Guest has sent an error event but did not specify an actual error code\n"));
387 pCBData->rc = VERR_GENERAL_FAILURE; /* Make sure some error is set. */
388 }
389
390 vrc = pThis->setProgress(100, DND_PROGRESS_ERROR, pCBData->rc,
391 Utf8StrFmt("Received error from guest: %Rrc", pCBData->rc));
392 AssertRCBreak(vrc);
393 vrc = pThis->notifyAboutGuestResponse(pCBData->rc);
394 AssertRCBreak(vrc);
395 break;
396 }
397
398 case GUEST_DND_FN_GET_NEXT_HOST_MSG:
399 vrc = VERR_NO_DATA; /* Indicate back to the host service that there are no new messages. */
400 break;
401
402 default:
403 AssertMsgBreakStmt(pThis->isProgressRunning() == false,
404 ("Progress object not completed / canceld yet! State is '%s' (%#x)\n",
405 DnDStateToStr(pThis->m_enmState), pThis->m_enmState),
406 vrc = VERR_INVALID_STATE); /* Please report this! */
407 vrc = VERR_CANCELLED;
408 break;
409 }
410
411 LogFlowFunc(("Returning rc=%Rrc\n", vrc));
412 return vrc;
413}
414
415/**
416 * Resets the progress object.
417 *
418 * @returns HRESULT
419 * @param pParent Parent to set for the progress object.
420 * @param strDesc Description of the progress.
421 */
422HRESULT GuestDnDState::resetProgress(const ComObjPtr<Guest>& pParent, const Utf8Str &strDesc)
423{
424 AssertReturn(strDesc.isNotEmpty(), E_INVALIDARG);
425
426 m_pProgress.setNull();
427
428 HRESULT hr = m_pProgress.createObject();
429 if (SUCCEEDED(hr))
430 {
431 hr = m_pProgress->init(static_cast<IGuest *>(pParent),
432 Bstr(strDesc).raw(),
433 TRUE /* aCancelable */);
434 }
435
436 return hr;
437}
438
439/**
440 * Returns whether the progress object has been canceled or not.
441 *
442 * @returns \c true if canceled or progress does not exist, \c false if not.
443 */
444bool GuestDnDState::isProgressCanceled(void) const
445{
446 if (m_pProgress.isNull())
447 return true;
448
449 BOOL fCanceled;
450 HRESULT hr = m_pProgress->COMGETTER(Canceled)(&fCanceled);
451 AssertComRCReturn(hr, false);
452 return RT_BOOL(fCanceled);
453}
454
455/**
456 * Returns whether the progress object still is in a running state or not.
457 *
458 * @returns \c true if running, \c false if not.
459 */
460bool GuestDnDState::isProgressRunning(void) const
461{
462 if (m_pProgress.isNull())
463 return false;
464
465 BOOL fCompleted;
466 HRESULT const hr = m_pProgress->COMGETTER(Completed)(&fCompleted);
467 AssertComRCReturn(hr, false);
468 return !RT_BOOL(fCompleted);
469}
470
471/**
472 * Sets (registers or unregisters) a callback for a specific HGCM message.
473 *
474 * @returns VBox status code.
475 * @param uMsg HGCM message ID to set callback for.
476 * @param pfnCallback Callback function pointer to use. Pass NULL to unregister.
477 * @param pvUser User-provided arguments for the callback function. Optional and can be NULL.
478 */
479int GuestDnDState::setCallback(uint32_t uMsg, PFNGUESTDNDCALLBACK pfnCallback, void *pvUser /* = NULL */)
480{
481 GuestDnDCallbackMap::iterator it = m_mapCallbacks.find(uMsg);
482
483 /* Register. */
484 if (pfnCallback)
485 {
486 try
487 {
488 m_mapCallbacks[uMsg] = GuestDnDCallback(pfnCallback, uMsg, pvUser);
489 }
490 catch (std::bad_alloc &)
491 {
492 return VERR_NO_MEMORY;
493 }
494 return VINF_SUCCESS;
495 }
496
497 /* Unregister. */
498 if (it != m_mapCallbacks.end())
499 m_mapCallbacks.erase(it);
500
501 return VINF_SUCCESS;
502}
503
504/**
505 * Sets the progress object to a new state.
506 *
507 * @returns VBox status code.
508 * @param uPercentage Percentage (0-100) to set.
509 * @param uStatus Status (of type DND_PROGRESS_XXX) to set.
510 * @param rcOp IPRT-style result code to set. Optional.
511 * @param strMsg Message to set. Optional.
512 */
513int GuestDnDState::setProgress(unsigned uPercentage, uint32_t uStatus,
514 int rcOp /* = VINF_SUCCESS */, const Utf8Str &strMsg /* = "" */)
515{
516 LogFlowFunc(("uPercentage=%u, uStatus=%RU32, , rcOp=%Rrc, strMsg=%s\n",
517 uPercentage, uStatus, rcOp, strMsg.c_str()));
518
519 HRESULT hr = S_OK;
520
521 if (m_pProgress.isNull())
522 return VINF_SUCCESS;
523
524 BOOL fCompleted = FALSE;
525 hr = m_pProgress->COMGETTER(Completed)(&fCompleted);
526 AssertComRCReturn(hr, VERR_COM_UNEXPECTED);
527
528 BOOL fCanceled = FALSE;
529 hr = m_pProgress->COMGETTER(Canceled)(&fCanceled);
530 AssertComRCReturn(hr, VERR_COM_UNEXPECTED);
531
532 LogFlowFunc(("Progress fCompleted=%RTbool, fCanceled=%RTbool\n", fCompleted, fCanceled));
533
534 int rc = VINF_SUCCESS;
535
536 switch (uStatus)
537 {
538 case DragAndDropSvc::DND_PROGRESS_ERROR:
539 {
540 LogRel(("DnD: Guest reported error %Rrc\n", rcOp));
541
542 if (!fCompleted)
543 hr = m_pProgress->i_notifyComplete(VBOX_E_DND_ERROR,
544 COM_IIDOF(IGuest),
545 m_pParent->getComponentName(), strMsg.c_str());
546 break;
547 }
548
549 case DragAndDropSvc::DND_PROGRESS_CANCELLED:
550 {
551 LogRel2(("DnD: Guest cancelled operation\n"));
552
553 if (!fCanceled)
554 {
555 hr = m_pProgress->Cancel();
556 AssertComRC(hr);
557 }
558
559 if (!fCompleted)
560 {
561 hr = m_pProgress->i_notifyComplete(S_OK);
562 AssertComRC(hr);
563 }
564 break;
565 }
566
567 case DragAndDropSvc::DND_PROGRESS_RUNNING:
568 RT_FALL_THROUGH();
569 case DragAndDropSvc::DND_PROGRESS_COMPLETE:
570 {
571 LogRel2(("DnD: Guest reporting running/completion status with %u%%\n", uPercentage));
572
573 if ( !fCompleted
574 && !fCanceled)
575 {
576 hr = m_pProgress->SetCurrentOperationProgress(uPercentage);
577 AssertComRCReturn(hr, VERR_COM_UNEXPECTED);
578 if ( uStatus == DragAndDropSvc::DND_PROGRESS_COMPLETE
579 || uPercentage >= 100)
580 {
581 hr = m_pProgress->i_notifyComplete(S_OK);
582 AssertComRCReturn(hr, VERR_COM_UNEXPECTED);
583 }
584 }
585 break;
586 }
587
588 default:
589 break;
590 }
591
592 LogFlowFuncLeaveRC(rc);
593 return rc;
594}
595
596/**
597 * Dispatching function for handling the host service service callback.
598 *
599 * @returns VBox status code.
600 * @param u32Function HGCM message ID to handle.
601 * @param pvParms Pointer to optional data provided for a particular message. Optional.
602 * @param cbParms Size (in bytes) of \a pvParms.
603 */
604int GuestDnDState::onDispatch(uint32_t u32Function, void *pvParms, uint32_t cbParms)
605{
606 LogFlowFunc(("u32Function=%RU32, pvParms=%p, cbParms=%RU32\n", u32Function, pvParms, cbParms));
607
608 int rc = VERR_WRONG_ORDER; /* Play safe. */
609
610 /* Whether or not to try calling host-installed callbacks after successfully processing the message. */
611 bool fTryCallbacks = false;
612
613 switch (u32Function)
614 {
615 case DragAndDropSvc::GUEST_DND_FN_CONNECT:
616 {
617 DragAndDropSvc::PVBOXDNDCBCONNECTDATA pCBData = reinterpret_cast<DragAndDropSvc::PVBOXDNDCBCONNECTDATA>(pvParms);
618 AssertPtr(pCBData);
619 AssertReturn(sizeof(DragAndDropSvc::VBOXDNDCBCONNECTDATA) == cbParms, VERR_INVALID_PARAMETER);
620 AssertReturn(DragAndDropSvc::CB_MAGIC_DND_CONNECT == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
621
622 m_uProtocolVersion = pCBData->uProtocolVersion;
623 /** @todo Handle flags. */
624
625 LogThisFunc(("Client connected, using protocol v%RU32\n", m_uProtocolVersion));
626
627 rc = VINF_SUCCESS;
628 break;
629 }
630
631 case DragAndDropSvc::GUEST_DND_FN_REPORT_FEATURES:
632 {
633 DragAndDropSvc::PVBOXDNDCBREPORTFEATURESDATA pCBData = reinterpret_cast<DragAndDropSvc::PVBOXDNDCBREPORTFEATURESDATA>(pvParms);
634 AssertPtr(pCBData);
635 AssertReturn(sizeof(DragAndDropSvc::VBOXDNDCBREPORTFEATURESDATA) == cbParms, VERR_INVALID_PARAMETER);
636 AssertReturn(DragAndDropSvc::CB_MAGIC_DND_REPORT_FEATURES == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
637
638 m_fGuestFeatures0 = pCBData->fGuestFeatures0;
639
640 LogThisFunc(("Client reported features: %#RX64\n", m_fGuestFeatures0));
641
642 rc = VINF_SUCCESS;
643 break;
644 }
645
646 /* Note: GUEST_DND_FN_EVT_ERROR is handled in either the state's default callback or in specific
647 * (overriden) callbacks (e.g. GuestDnDSendCtx / GuestDnDRecvCtx). */
648
649 case DragAndDropSvc::GUEST_DND_FN_DISCONNECT:
650 {
651 LogThisFunc(("Client disconnected\n"));
652 rc = setProgress(100, DND_PROGRESS_CANCELLED, VINF_SUCCESS);
653 break;
654 }
655
656 case DragAndDropSvc::GUEST_DND_FN_HG_ACK_OP:
657 {
658 DragAndDropSvc::PVBOXDNDCBHGACKOPDATA pCBData = reinterpret_cast<DragAndDropSvc::PVBOXDNDCBHGACKOPDATA>(pvParms);
659 AssertPtr(pCBData);
660 AssertReturn(sizeof(DragAndDropSvc::VBOXDNDCBHGACKOPDATA) == cbParms, VERR_INVALID_PARAMETER);
661 AssertReturn(DragAndDropSvc::CB_MAGIC_DND_HG_ACK_OP == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
662
663 LogRel2(("DnD: Guest responded with action '%s' for host->guest drag event\n", DnDActionToStr(pCBData->uAction)));
664
665 setActionDefault(pCBData->uAction);
666 rc = notifyAboutGuestResponse();
667 break;
668 }
669
670 case DragAndDropSvc::GUEST_DND_FN_HG_REQ_DATA:
671 {
672 DragAndDropSvc::PVBOXDNDCBHGREQDATADATA pCBData = reinterpret_cast<DragAndDropSvc::PVBOXDNDCBHGREQDATADATA>(pvParms);
673 AssertPtr(pCBData);
674 AssertReturn(sizeof(DragAndDropSvc::VBOXDNDCBHGREQDATADATA) == cbParms, VERR_INVALID_PARAMETER);
675 AssertReturn(DragAndDropSvc::CB_MAGIC_DND_HG_REQ_DATA == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
676
677 if ( pCBData->cbFormat == 0
678 || pCBData->cbFormat > _64K /** @todo Make this configurable? */
679 || pCBData->pszFormat == NULL)
680 {
681 rc = VERR_INVALID_PARAMETER;
682 }
683 else if (!RTStrIsValidEncoding(pCBData->pszFormat))
684 {
685 rc = VERR_INVALID_PARAMETER;
686 }
687 else
688 {
689 setFormats(GuestDnD::toFormatList(pCBData->pszFormat));
690 rc = VINF_SUCCESS;
691 }
692
693 int rc2 = notifyAboutGuestResponse();
694 if (RT_SUCCESS(rc))
695 rc = rc2;
696 break;
697 }
698
699 case DragAndDropSvc::GUEST_DND_FN_HG_EVT_PROGRESS:
700 {
701 DragAndDropSvc::PVBOXDNDCBHGEVTPROGRESSDATA pCBData =
702 reinterpret_cast<DragAndDropSvc::PVBOXDNDCBHGEVTPROGRESSDATA>(pvParms);
703 AssertPtr(pCBData);
704 AssertReturn(sizeof(DragAndDropSvc::VBOXDNDCBHGEVTPROGRESSDATA) == cbParms, VERR_INVALID_PARAMETER);
705 AssertReturn(DragAndDropSvc::CB_MAGIC_DND_HG_EVT_PROGRESS == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
706
707 rc = setProgress(pCBData->uPercentage, pCBData->uStatus, pCBData->rc);
708 if (RT_SUCCESS(rc))
709 rc = notifyAboutGuestResponse(pCBData->rc);
710 break;
711 }
712#ifdef VBOX_WITH_DRAG_AND_DROP_GH
713 case DragAndDropSvc::GUEST_DND_FN_GH_ACK_PENDING:
714 {
715 DragAndDropSvc::PVBOXDNDCBGHACKPENDINGDATA pCBData =
716 reinterpret_cast<DragAndDropSvc::PVBOXDNDCBGHACKPENDINGDATA>(pvParms);
717 AssertPtr(pCBData);
718 AssertReturn(sizeof(DragAndDropSvc::VBOXDNDCBGHACKPENDINGDATA) == cbParms, VERR_INVALID_PARAMETER);
719 AssertReturn(DragAndDropSvc::CB_MAGIC_DND_GH_ACK_PENDING == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
720
721 LogRel2(("DnD: Guest responded with pending action '%s' (%RU32 bytes format data) to guest->host drag event\n",
722 DnDActionToStr(pCBData->uDefAction), pCBData->cbFormat));
723
724 if ( pCBData->cbFormat == 0
725 || pCBData->cbFormat > _64K /** @todo Make the maximum size configurable? */
726 || pCBData->pszFormat == NULL)
727 {
728 rc = VERR_INVALID_PARAMETER;
729 }
730 else if (!RTStrIsValidEncoding(pCBData->pszFormat))
731 {
732 rc = VERR_INVALID_PARAMETER;
733 }
734 else
735 {
736 setFormats (GuestDnD::toFormatList(pCBData->pszFormat));
737 setActionDefault (pCBData->uDefAction);
738 setActionsAllowed(pCBData->uAllActions);
739
740 rc = VINF_SUCCESS;
741 }
742
743 int rc2 = notifyAboutGuestResponse();
744 if (RT_SUCCESS(rc))
745 rc = rc2;
746 break;
747 }
748#endif /* VBOX_WITH_DRAG_AND_DROP_GH */
749 default:
750 /* * Try if the event is covered by a registered callback. */
751 fTryCallbacks = true;
752 break;
753 }
754
755 /*
756 * Try the host's installed callbacks (if any).
757 */
758 if (fTryCallbacks)
759 {
760 GuestDnDCallbackMap::const_iterator it = m_mapCallbacks.find(u32Function);
761 if (it != m_mapCallbacks.end())
762 {
763 AssertPtr(it->second.pfnCallback);
764 rc = it->second.pfnCallback(u32Function, pvParms, cbParms, it->second.pvUser);
765 }
766 else
767 {
768 /* Invoke the default callback handler in case we don't have any registered callback above. */
769 rc = i_defaultCallback(u32Function, pvParms, cbParms, this /* pvUser */);
770 }
771 }
772
773 LogFlowFunc(("Returning rc=%Rrc\n", rc));
774 return rc;
775}
776
777/**
778 * Helper function to query the internal progress object to an IProgress interface.
779 *
780 * @returns HRESULT
781 * @param ppProgress Where to query the progress object to.
782 */
783HRESULT GuestDnDState::queryProgressTo(IProgress **ppProgress)
784{
785 return m_pProgress.queryInterfaceTo(ppProgress);
786}
787
788/**
789 * Waits for a guest response to happen, extended version.
790 *
791 * @returns VBox status code.
792 * @retval VERR_TIMEOUT when waiting has timed out.
793 * @retval VERR_DND_GUEST_ERROR on an error reported back from the guest.
794 * @param msTimeout Timeout (in ms) for waiting. Optional, waits 3000 ms if not specified.
795 * @param prcGuest Where to return the guest error when VERR_DND_GUEST_ERROR is returned. Optional.
796 */
797int GuestDnDState::waitForGuestResponseEx(RTMSINTERVAL msTimeout /* = 3000 */, int *prcGuest /* = NULL */)
798{
799 int vrc = RTSemEventWait(m_EventSem, msTimeout);
800 if (RT_SUCCESS(vrc))
801 {
802 if (RT_FAILURE(m_rcGuest))
803 vrc = VERR_DND_GUEST_ERROR;
804 if (prcGuest)
805 *prcGuest = m_rcGuest;
806 }
807 return vrc;
808}
809
810/**
811 * Waits for a guest response to happen.
812 *
813 * @returns VBox status code.
814 * @retval VERR_TIMEOUT when waiting has timed out.
815 * @retval VERR_DND_GUEST_ERROR on an error reported back from the guest.
816 * @param prcGuest Where to return the guest error when VERR_DND_GUEST_ERROR is returned. Optional.
817 *
818 * @note Uses the default timeout of 3000 ms.
819 */
820int GuestDnDState::waitForGuestResponse(int *prcGuest /* = NULL */)
821{
822 return waitForGuestResponseEx(3000 /* ms */, prcGuest);
823}
824
825/*********************************************************************************************************************************
826 * GuestDnD implementation. *
827 ********************************************************************************************************************************/
828
829/** Static (Singleton) instance of the GuestDnD object. */
830GuestDnD* GuestDnD::s_pInstance = NULL;
831
832GuestDnD::GuestDnD(const ComObjPtr<Guest> &pGuest)
833 : m_pGuest(pGuest)
834 , m_cTransfersPending(0)
835{
836 LogFlowFuncEnter();
837
838 try
839 {
840 m_pState = new GuestDnDState(pGuest);
841 }
842 catch (std::bad_alloc &)
843 {
844 throw VERR_NO_MEMORY;
845 }
846
847 int rc = RTCritSectInit(&m_CritSect);
848 if (RT_FAILURE(rc))
849 throw rc;
850
851 /* List of supported default MIME types. */
852 LogRel2(("DnD: Supported default host formats:\n"));
853 const com::Utf8Str arrEntries[] = { VBOX_DND_FORMATS_DEFAULT };
854 for (size_t i = 0; i < RT_ELEMENTS(arrEntries); i++)
855 {
856 m_strDefaultFormats.push_back(arrEntries[i]);
857 LogRel2(("DnD: \t%s\n", arrEntries[i].c_str()));
858 }
859}
860
861GuestDnD::~GuestDnD(void)
862{
863 LogFlowFuncEnter();
864
865 Assert(m_cTransfersPending == 0); /* Sanity. */
866
867 RTCritSectDelete(&m_CritSect);
868
869 if (m_pState)
870 delete m_pState;
871}
872
873/**
874 * Adjusts coordinations to a given screen.
875 *
876 * @returns HRESULT
877 * @param uScreenId ID of screen to adjust coordinates to.
878 * @param puX Pointer to X coordinate to adjust. Will return the adjusted value on success.
879 * @param puY Pointer to Y coordinate to adjust. Will return the adjusted value on success.
880 */
881HRESULT GuestDnD::adjustScreenCoordinates(ULONG uScreenId, ULONG *puX, ULONG *puY) const
882{
883 /** @todo r=andy Save the current screen's shifting coordinates to speed things up.
884 * Only query for new offsets when the screen ID or the screen's resolution has changed. */
885
886 /* For multi-monitor support we need to add shift values to the coordinates
887 * (depending on the screen number). */
888 ComObjPtr<Console> pConsole = m_pGuest->i_getConsole();
889 ComPtr<IDisplay> pDisplay;
890 HRESULT hr = pConsole->COMGETTER(Display)(pDisplay.asOutParam());
891 if (FAILED(hr))
892 return hr;
893
894 ULONG dummy;
895 LONG xShift, yShift;
896 GuestMonitorStatus_T monitorStatus;
897 hr = pDisplay->GetScreenResolution(uScreenId, &dummy, &dummy, &dummy,
898 &xShift, &yShift, &monitorStatus);
899 if (FAILED(hr))
900 return hr;
901
902 if (puX)
903 *puX += xShift;
904 if (puY)
905 *puY += yShift;
906
907 LogFlowFunc(("uScreenId=%RU32, x=%RU32, y=%RU32\n", uScreenId, puX ? *puX : 0, puY ? *puY : 0));
908 return S_OK;
909}
910
911/**
912 * Returns a DnD guest state.
913 *
914 * @returns Pointer to DnD guest state, or NULL if not found / invalid.
915 * @param uID ID of DnD guest state to return.
916 */
917GuestDnDState *GuestDnD::getState(uint32_t uID /* = 0 */) const
918{
919 AssertMsgReturn(uID == 0, ("Only one state (0) is supported at the moment\n"), NULL);
920
921 return m_pState;
922}
923
924/**
925 * Sends a (blocking) message to the host side of the host service.
926 *
927 * @returns VBox status code.
928 * @param u32Function HGCM message ID to send.
929 * @param cParms Number of parameters to send.
930 * @param paParms Array of parameters to send. Must match \c cParms.
931 */
932int GuestDnD::hostCall(uint32_t u32Function, uint32_t cParms, PVBOXHGCMSVCPARM paParms) const
933{
934 Assert(!m_pGuest.isNull());
935 ComObjPtr<Console> pConsole = m_pGuest->i_getConsole();
936
937 /* Forward the information to the VMM device. */
938 Assert(!pConsole.isNull());
939 VMMDev *pVMMDev = pConsole->i_getVMMDev();
940 if (!pVMMDev)
941 return VERR_COM_OBJECT_NOT_FOUND;
942
943 return pVMMDev->hgcmHostCall("VBoxDragAndDropSvc", u32Function, cParms, paParms);
944}
945
946/**
947 * Registers a GuestDnDSource object with the GuestDnD manager.
948 *
949 * Currently only one source is supported at a time.
950 *
951 * @returns VBox status code.
952 * @param Source Source to register.
953 */
954int GuestDnD::registerSource(const ComObjPtr<GuestDnDSource> &Source)
955{
956 GUESTDND_LOCK();
957
958 Assert(m_lstSrc.size() == 0); /* We only support one source at a time at the moment. */
959 m_lstSrc.push_back(Source);
960
961 GUESTDND_UNLOCK();
962 return VINF_SUCCESS;
963}
964
965/**
966 * Unregisters a GuestDnDSource object from the GuestDnD manager.
967 *
968 * @returns VBox status code.
969 * @param Source Source to unregister.
970 */
971int GuestDnD::unregisterSource(const ComObjPtr<GuestDnDSource> &Source)
972{
973 GUESTDND_LOCK();
974
975 GuestDnDSrcList::iterator itSrc = std::find(m_lstSrc.begin(), m_lstSrc.end(), Source);
976 if (itSrc != m_lstSrc.end())
977 m_lstSrc.erase(itSrc);
978
979 GUESTDND_UNLOCK();
980 return VINF_SUCCESS;
981}
982
983/**
984 * Returns the current number of registered sources.
985 *
986 * @returns Current number of registered sources.
987 */
988size_t GuestDnD::getSourceCount(void)
989{
990 GUESTDND_LOCK_RET(0);
991
992 size_t cSources = m_lstSrc.size();
993
994 GUESTDND_UNLOCK();
995 return cSources;
996}
997
998/**
999 * Registers a GuestDnDTarget object with the GuestDnD manager.
1000 *
1001 * Currently only one target is supported at a time.
1002 *
1003 * @returns VBox status code.
1004 * @param Target Target to register.
1005 */
1006int GuestDnD::registerTarget(const ComObjPtr<GuestDnDTarget> &Target)
1007{
1008 GUESTDND_LOCK();
1009
1010 Assert(m_lstTgt.size() == 0); /* We only support one target at a time at the moment. */
1011 m_lstTgt.push_back(Target);
1012
1013 GUESTDND_UNLOCK();
1014 return VINF_SUCCESS;
1015}
1016
1017/**
1018 * Unregisters a GuestDnDTarget object from the GuestDnD manager.
1019 *
1020 * @returns VBox status code.
1021 * @param Target Target to unregister.
1022 */
1023int GuestDnD::unregisterTarget(const ComObjPtr<GuestDnDTarget> &Target)
1024{
1025 GUESTDND_LOCK();
1026
1027 GuestDnDTgtList::iterator itTgt = std::find(m_lstTgt.begin(), m_lstTgt.end(), Target);
1028 if (itTgt != m_lstTgt.end())
1029 m_lstTgt.erase(itTgt);
1030
1031 GUESTDND_UNLOCK();
1032 return VINF_SUCCESS;
1033}
1034
1035/**
1036 * Returns the current number of registered targets.
1037 *
1038 * @returns Current number of registered targets.
1039 */
1040size_t GuestDnD::getTargetCount(void)
1041{
1042 GUESTDND_LOCK_RET(0);
1043
1044 size_t cTargets = m_lstTgt.size();
1045
1046 GUESTDND_UNLOCK();
1047 return cTargets;
1048}
1049
1050/**
1051 * Static main dispatcher function to handle callbacks from the DnD host service.
1052 *
1053 * @returns VBox status code.
1054 * @param pvExtension Pointer to service extension.
1055 * @param u32Function Callback HGCM message ID.
1056 * @param pvParms Pointer to optional data provided for a particular message. Optional.
1057 * @param cbParms Size (in bytes) of \a pvParms.
1058 */
1059/* static */
1060DECLCALLBACK(int) GuestDnD::notifyDnDDispatcher(void *pvExtension, uint32_t u32Function,
1061 void *pvParms, uint32_t cbParms)
1062{
1063 LogFlowFunc(("pvExtension=%p, u32Function=%RU32, pvParms=%p, cbParms=%RU32\n",
1064 pvExtension, u32Function, pvParms, cbParms));
1065
1066 GuestDnD *pGuestDnD = reinterpret_cast<GuestDnD*>(pvExtension);
1067 AssertPtrReturn(pGuestDnD, VERR_INVALID_POINTER);
1068
1069 /** @todo In case we need to handle multiple guest DnD responses at a time this
1070 * would be the place to lookup and dispatch to those. For the moment we
1071 * only have one response -- simple. */
1072 if (pGuestDnD->m_pState)
1073 return pGuestDnD->m_pState->onDispatch(u32Function, pvParms, cbParms);
1074
1075 return VERR_NOT_SUPPORTED;
1076}
1077
1078/**
1079 * Static helper function to determine whether a format is part of a given MIME list.
1080 *
1081 * @returns \c true if found, \c false if not.
1082 * @param strFormat Format to search for.
1083 * @param lstFormats MIME list to search in.
1084 */
1085/* static */
1086bool GuestDnD::isFormatInFormatList(const com::Utf8Str &strFormat, const GuestDnDMIMEList &lstFormats)
1087{
1088 return std::find(lstFormats.begin(), lstFormats.end(), strFormat) != lstFormats.end();
1089}
1090
1091/**
1092 * Static helper function to create a GuestDnDMIMEList out of a format list string.
1093 *
1094 * @returns MIME list object.
1095 * @param strFormats List of formats to convert.
1096 * @param strSep Separator to use. If not specified, DND_FORMATS_SEPARATOR_STR will be used.
1097 */
1098/* static */
1099GuestDnDMIMEList GuestDnD::toFormatList(const com::Utf8Str &strFormats, const com::Utf8Str &strSep /* = DND_FORMATS_SEPARATOR_STR */)
1100{
1101 GuestDnDMIMEList lstFormats;
1102 RTCList<RTCString> lstFormatsTmp = strFormats.split(strSep);
1103
1104 for (size_t i = 0; i < lstFormatsTmp.size(); i++)
1105 lstFormats.push_back(com::Utf8Str(lstFormatsTmp.at(i)));
1106
1107 return lstFormats;
1108}
1109
1110/**
1111 * Static helper function to create a format list string from a given GuestDnDMIMEList object.
1112 *
1113 * @returns Format list string.
1114 * @param lstFormats GuestDnDMIMEList to convert.
1115 * @param strSep Separator to use between formats.
1116 * Uses DND_FORMATS_SEPARATOR_STR as default.
1117 */
1118/* static */
1119com::Utf8Str GuestDnD::toFormatString(const GuestDnDMIMEList &lstFormats, const com::Utf8Str &strSep /* = DND_FORMATS_SEPARATOR_STR */)
1120{
1121 com::Utf8Str strFormat;
1122 for (size_t i = 0; i < lstFormats.size(); i++)
1123 {
1124 const com::Utf8Str &f = lstFormats.at(i);
1125 strFormat += f + strSep;
1126 }
1127
1128 return strFormat;
1129}
1130
1131/**
1132 * Static helper function to create a filtered GuestDnDMIMEList object from supported and wanted formats.
1133 *
1134 * @returns Filtered MIME list object.
1135 * @param lstFormatsSupported MIME list of supported formats.
1136 * @param lstFormatsWanted MIME list of wanted formats in returned object.
1137 */
1138/* static */
1139GuestDnDMIMEList GuestDnD::toFilteredFormatList(const GuestDnDMIMEList &lstFormatsSupported, const GuestDnDMIMEList &lstFormatsWanted)
1140{
1141 GuestDnDMIMEList lstFormats;
1142
1143 for (size_t i = 0; i < lstFormatsWanted.size(); i++)
1144 {
1145 /* Only keep supported format types. */
1146 if (std::find(lstFormatsSupported.begin(),
1147 lstFormatsSupported.end(), lstFormatsWanted.at(i)) != lstFormatsSupported.end())
1148 {
1149 lstFormats.push_back(lstFormatsWanted[i]);
1150 }
1151 }
1152
1153 return lstFormats;
1154}
1155
1156/**
1157 * Static helper function to create a filtered GuestDnDMIMEList object from supported and wanted formats.
1158 *
1159 * @returns Filtered MIME list object.
1160 * @param lstFormatsSupported MIME list of supported formats.
1161 * @param strFormatsWanted Format list string of wanted formats in returned object.
1162 */
1163/* static */
1164GuestDnDMIMEList GuestDnD::toFilteredFormatList(const GuestDnDMIMEList &lstFormatsSupported, const com::Utf8Str &strFormatsWanted)
1165{
1166 GuestDnDMIMEList lstFmt;
1167
1168 RTCList<RTCString> lstFormats = strFormatsWanted.split(DND_FORMATS_SEPARATOR_STR);
1169 size_t i = 0;
1170 while (i < lstFormats.size())
1171 {
1172 /* Only keep allowed format types. */
1173 if (std::find(lstFormatsSupported.begin(),
1174 lstFormatsSupported.end(), lstFormats.at(i)) != lstFormatsSupported.end())
1175 {
1176 lstFmt.push_back(lstFormats[i]);
1177 }
1178 i++;
1179 }
1180
1181 return lstFmt;
1182}
1183
1184/**
1185 * Static helper function to convert a Main DnD action an internal DnD action.
1186 *
1187 * @returns Internal DnD action, or VBOX_DND_ACTION_IGNORE if not found / supported.
1188 * @param enmAction Main DnD action to convert.
1189 */
1190/* static */
1191VBOXDNDACTION GuestDnD::toHGCMAction(DnDAction_T enmAction)
1192{
1193 VBOXDNDACTION dndAction = VBOX_DND_ACTION_IGNORE;
1194 switch (enmAction)
1195 {
1196 case DnDAction_Copy:
1197 dndAction = VBOX_DND_ACTION_COPY;
1198 break;
1199 case DnDAction_Move:
1200 dndAction = VBOX_DND_ACTION_MOVE;
1201 break;
1202 case DnDAction_Link:
1203 /* For now it doesn't seems useful to allow a link
1204 action between host & guest. Later? */
1205 case DnDAction_Ignore:
1206 /* Ignored. */
1207 break;
1208 default:
1209 AssertMsgFailed(("Action %RU32 not recognized!\n", enmAction));
1210 break;
1211 }
1212
1213 return dndAction;
1214}
1215
1216/**
1217 * Static helper function to convert a Main DnD default action and allowed Main actions to their
1218 * corresponding internal representations.
1219 *
1220 * @param enmDnDActionDefault Default Main action to convert.
1221 * @param pDnDActionDefault Where to store the converted default action.
1222 * @param vecDnDActionsAllowed Allowed Main actions to convert.
1223 * @param pDnDLstActionsAllowed Where to store the converted allowed actions.
1224 */
1225/* static */
1226void GuestDnD::toHGCMActions(DnDAction_T enmDnDActionDefault,
1227 VBOXDNDACTION *pDnDActionDefault,
1228 const std::vector<DnDAction_T> vecDnDActionsAllowed,
1229 VBOXDNDACTIONLIST *pDnDLstActionsAllowed)
1230{
1231 VBOXDNDACTIONLIST dndLstActionsAllowed = VBOX_DND_ACTION_IGNORE;
1232 VBOXDNDACTION dndActionDefault = toHGCMAction(enmDnDActionDefault);
1233
1234 if (!vecDnDActionsAllowed.empty())
1235 {
1236 /* First convert the allowed actions to a bit array. */
1237 for (size_t i = 0; i < vecDnDActionsAllowed.size(); i++)
1238 dndLstActionsAllowed |= toHGCMAction(vecDnDActionsAllowed[i]);
1239
1240 /*
1241 * If no default action is set (ignoring), try one of the
1242 * set allowed actions, preferring copy, move (in that order).
1243 */
1244 if (isDnDIgnoreAction(dndActionDefault))
1245 {
1246 if (hasDnDCopyAction(dndLstActionsAllowed))
1247 dndActionDefault = VBOX_DND_ACTION_COPY;
1248 else if (hasDnDMoveAction(dndLstActionsAllowed))
1249 dndActionDefault = VBOX_DND_ACTION_MOVE;
1250 }
1251 }
1252
1253 if (pDnDActionDefault)
1254 *pDnDActionDefault = dndActionDefault;
1255 if (pDnDLstActionsAllowed)
1256 *pDnDLstActionsAllowed = dndLstActionsAllowed;
1257}
1258
1259/**
1260 * Static helper function to convert an internal DnD action to its Main representation.
1261 *
1262 * @returns Converted Main DnD action.
1263 * @param dndAction DnD action to convert.
1264 */
1265/* static */
1266DnDAction_T GuestDnD::toMainAction(VBOXDNDACTION dndAction)
1267{
1268 /* For now it doesn't seems useful to allow a
1269 * link action between host & guest. Maybe later! */
1270 return isDnDCopyAction(dndAction) ? DnDAction_Copy
1271 : isDnDMoveAction(dndAction) ? DnDAction_Move
1272 : DnDAction_Ignore;
1273}
1274
1275/**
1276 * Static helper function to convert an internal DnD action list to its Main representation.
1277 *
1278 * @returns Converted Main DnD action list.
1279 * @param dndActionList DnD action list to convert.
1280 */
1281/* static */
1282std::vector<DnDAction_T> GuestDnD::toMainActions(VBOXDNDACTIONLIST dndActionList)
1283{
1284 std::vector<DnDAction_T> vecActions;
1285
1286 /* For now it doesn't seems useful to allow a
1287 * link action between host & guest. Maybe later! */
1288 RTCList<DnDAction_T> lstActions;
1289 if (hasDnDCopyAction(dndActionList))
1290 lstActions.append(DnDAction_Copy);
1291 if (hasDnDMoveAction(dndActionList))
1292 lstActions.append(DnDAction_Move);
1293
1294 for (size_t i = 0; i < lstActions.size(); ++i)
1295 vecActions.push_back(lstActions.at(i));
1296
1297 return vecActions;
1298}
1299
1300/*********************************************************************************************************************************
1301 * GuestDnDBase implementation. *
1302 ********************************************************************************************************************************/
1303
1304GuestDnDBase::GuestDnDBase(VirtualBoxBase *pBase)
1305 : m_pBase(pBase)
1306 , m_fIsPending(false)
1307{
1308 /* Initialize public attributes. */
1309 m_lstFmtSupported = GuestDnDInst()->defaultFormats();
1310}
1311
1312GuestDnDBase::~GuestDnDBase(void)
1313{
1314}
1315
1316/**
1317 * Checks whether a given DnD format is supported or not.
1318 *
1319 * @returns \c true if supported, \c false if not.
1320 * @param aFormat DnD format to check.
1321 */
1322bool GuestDnDBase::i_isFormatSupported(const com::Utf8Str &aFormat) const
1323{
1324 return std::find(m_lstFmtSupported.begin(), m_lstFmtSupported.end(), aFormat) != m_lstFmtSupported.end();
1325}
1326
1327/**
1328 * Returns the currently supported DnD formats.
1329 *
1330 * @returns List of the supported DnD formats.
1331 */
1332const GuestDnDMIMEList &GuestDnDBase::i_getFormats(void) const
1333{
1334 return m_lstFmtSupported;
1335}
1336
1337/**
1338 * Adds DnD formats to the supported formats list.
1339 *
1340 * @returns HRESULT
1341 * @param aFormats List of DnD formats to add.
1342 */
1343HRESULT GuestDnDBase::i_addFormats(const GuestDnDMIMEList &aFormats)
1344{
1345 for (size_t i = 0; i < aFormats.size(); ++i)
1346 {
1347 Utf8Str strFormat = aFormats.at(i);
1348 if (std::find(m_lstFmtSupported.begin(),
1349 m_lstFmtSupported.end(), strFormat) == m_lstFmtSupported.end())
1350 {
1351 m_lstFmtSupported.push_back(strFormat);
1352 }
1353 }
1354
1355 return S_OK;
1356}
1357
1358/**
1359 * Removes DnD formats from tehh supported formats list.
1360 *
1361 * @returns HRESULT
1362 * @param aFormats List of DnD formats to remove.
1363 */
1364HRESULT GuestDnDBase::i_removeFormats(const GuestDnDMIMEList &aFormats)
1365{
1366 for (size_t i = 0; i < aFormats.size(); ++i)
1367 {
1368 Utf8Str strFormat = aFormats.at(i);
1369 GuestDnDMIMEList::iterator itFormat = std::find(m_lstFmtSupported.begin(),
1370 m_lstFmtSupported.end(), strFormat);
1371 if (itFormat != m_lstFmtSupported.end())
1372 m_lstFmtSupported.erase(itFormat);
1373 }
1374
1375 return S_OK;
1376}
1377
1378/**
1379 * Prints an error in the release log and sets the COM error info.
1380 *
1381 * @returns HRESULT
1382 * @param vrc IPRT-style error code to print in addition.
1383 * Will not be printed if set to a non-error (e.g. VINF _ / VWRN_) code.
1384 * @param pcszMsgFmt Format string.
1385 * @param va Format arguments.
1386 * @note
1387 */
1388HRESULT GuestDnDBase::i_setErrorV(int vrc, const char *pcszMsgFmt, va_list va)
1389{
1390 char *psz = NULL;
1391 if (RTStrAPrintfV(&psz, pcszMsgFmt, va) < 0)
1392 return E_OUTOFMEMORY;
1393 AssertPtrReturn(psz, E_OUTOFMEMORY);
1394
1395 HRESULT hrc;
1396 if (RT_FAILURE(vrc))
1397 {
1398 LogRel(("DnD: Error: %s (%Rrc)\n", psz, vrc));
1399 hrc = m_pBase->setErrorBoth(VBOX_E_DND_ERROR, vrc, "DnD: Error: %s (%Rrc)", psz, vrc);
1400 }
1401 else
1402 {
1403 LogRel(("DnD: Error: %s\n", psz));
1404 hrc = m_pBase->setErrorBoth(VBOX_E_DND_ERROR, vrc, "DnD: Error: %s", psz);
1405 }
1406
1407 RTStrFree(psz);
1408 return hrc;
1409}
1410
1411/**
1412 * Prints an error in the release log and sets the COM error info.
1413 *
1414 * @returns HRESULT
1415 * @param vrc IPRT-style error code to print in addition.
1416 * Will not be printed if set to a non-error (e.g. VINF _ / VWRN_) code.
1417 * @param pcszMsgFmt Format string.
1418 * @param ... Format arguments.
1419 * @note
1420 */
1421HRESULT GuestDnDBase::i_setError(int vrc, const char *pcszMsgFmt, ...)
1422{
1423 va_list va;
1424 va_start(va, pcszMsgFmt);
1425 HRESULT const hrc = i_setErrorV(vrc, pcszMsgFmt, va);
1426 va_end(va);
1427
1428 return hrc;
1429}
1430
1431/**
1432 * Prints an error in the release log, sets the COM error info and calls the object's reset function.
1433 *
1434 * @returns HRESULT
1435 * @param pcszMsgFmt Format string.
1436 * @param va Format arguments.
1437 * @note
1438 */
1439HRESULT GuestDnDBase::i_setErrorAndReset(const char *pcszMsgFmt, ...)
1440{
1441 va_list va;
1442 va_start(va, pcszMsgFmt);
1443 HRESULT const hrc = i_setErrorV(VINF_SUCCESS, pcszMsgFmt, va);
1444 va_end(va);
1445
1446 i_reset();
1447
1448 return hrc;
1449}
1450
1451/**
1452 * Prints an error in the release log, sets the COM error info and calls the object's reset function.
1453 *
1454 * @returns HRESULT
1455 * @param vrc IPRT-style error code to print in addition.
1456 * Will not be printed if set to a non-error (e.g. VINF _ / VWRN_) code.
1457 * @param pcszMsgFmt Format string.
1458 * @param ... Format arguments.
1459 * @note
1460 */
1461HRESULT GuestDnDBase::i_setErrorAndReset(int vrc, const char *pcszMsgFmt, ...)
1462{
1463 va_list va;
1464 va_start(va, pcszMsgFmt);
1465 HRESULT const hrc = i_setErrorV(vrc, pcszMsgFmt, va);
1466 va_end(va);
1467
1468 i_reset();
1469
1470 return hrc;
1471}
1472
1473/**
1474 * Adds a new guest DnD message to the internal message queue.
1475 *
1476 * @returns VBox status code.
1477 * @param pMsg Pointer to message to add.
1478 */
1479int GuestDnDBase::msgQueueAdd(GuestDnDMsg *pMsg)
1480{
1481 m_DataBase.lstMsgOut.push_back(pMsg);
1482 return VINF_SUCCESS;
1483}
1484
1485/**
1486 * Returns the next guest DnD message in the internal message queue (FIFO).
1487 *
1488 * @returns Pointer to guest DnD message, or NULL if none found.
1489 */
1490GuestDnDMsg *GuestDnDBase::msgQueueGetNext(void)
1491{
1492 if (m_DataBase.lstMsgOut.empty())
1493 return NULL;
1494 return m_DataBase.lstMsgOut.front();
1495}
1496
1497/**
1498 * Removes the next guest DnD message from the internal message queue.
1499 */
1500void GuestDnDBase::msgQueueRemoveNext(void)
1501{
1502 if (!m_DataBase.lstMsgOut.empty())
1503 {
1504 GuestDnDMsg *pMsg = m_DataBase.lstMsgOut.front();
1505 if (pMsg)
1506 delete pMsg;
1507 m_DataBase.lstMsgOut.pop_front();
1508 }
1509}
1510
1511/**
1512 * Clears the internal message queue.
1513 */
1514void GuestDnDBase::msgQueueClear(void)
1515{
1516 LogFlowFunc(("cMsg=%zu\n", m_DataBase.lstMsgOut.size()));
1517
1518 GuestDnDMsgList::iterator itMsg = m_DataBase.lstMsgOut.begin();
1519 while (itMsg != m_DataBase.lstMsgOut.end())
1520 {
1521 GuestDnDMsg *pMsg = *itMsg;
1522 if (pMsg)
1523 delete pMsg;
1524
1525 itMsg++;
1526 }
1527
1528 m_DataBase.lstMsgOut.clear();
1529}
1530
1531/**
1532 * Sends a request to the guest side to cancel the current DnD operation.
1533 *
1534 * @returns VBox status code.
1535 */
1536int GuestDnDBase::sendCancel(void)
1537{
1538 GuestDnDMsg Msg;
1539 Msg.setType(HOST_DND_FN_CANCEL);
1540 if (m_pState->m_uProtocolVersion >= 3)
1541 Msg.appendUInt32(0); /** @todo ContextID not used yet. */
1542
1543 LogRel2(("DnD: Cancelling operation on guest ...\n"));
1544
1545 int rc = GuestDnDInst()->hostCall(Msg.getType(), Msg.getCount(), Msg.getParms());
1546 if (RT_FAILURE(rc))
1547 LogRel(("DnD: Cancelling operation on guest failed with %Rrc\n", rc));
1548
1549 return rc;
1550}
1551
1552/**
1553 * Helper function to update the progress based on given a GuestDnDData object.
1554 *
1555 * @returns VBox status code.
1556 * @param pData GuestDnDData object to use for accounting.
1557 * @param pState Guest state to update its progress object for.
1558 * @param cbDataAdd By how much data (in bytes) to update the progress.
1559 */
1560int GuestDnDBase::updateProgress(GuestDnDData *pData, GuestDnDState *pState,
1561 size_t cbDataAdd /* = 0 */)
1562{
1563 AssertPtrReturn(pData, VERR_INVALID_POINTER);
1564 AssertPtrReturn(pState, VERR_INVALID_POINTER);
1565 /* cbDataAdd is optional. */
1566
1567 LogFlowFunc(("cbExtra=%zu, cbProcessed=%zu, cbRemaining=%zu, cbDataAdd=%zu\n",
1568 pData->cbExtra, pData->cbProcessed, pData->getRemaining(), cbDataAdd));
1569
1570 if ( !pState
1571 || !cbDataAdd) /* Only update if something really changes. */
1572 return VINF_SUCCESS;
1573
1574 if (cbDataAdd)
1575 pData->addProcessed(cbDataAdd);
1576
1577 const uint8_t uPercent = pData->getPercentComplete();
1578
1579 LogRel2(("DnD: Transfer %RU8%% complete\n", uPercent));
1580
1581 int rc = pState->setProgress(uPercent,
1582 pData->isComplete()
1583 ? DND_PROGRESS_COMPLETE
1584 : DND_PROGRESS_RUNNING);
1585 LogFlowFuncLeaveRC(rc);
1586 return rc;
1587}
1588
1589/**
1590 * Waits for a specific guest callback event to get signalled.
1591 *
1592 * @returns VBox status code. Will return VERR_CANCELLED if the user has cancelled the progress object.
1593 * @param pEvent Callback event to wait for.
1594 * @param pState Guest state to update.
1595 * @param msTimeout Timeout (in ms) to wait.
1596 */
1597int GuestDnDBase::waitForEvent(GuestDnDCallbackEvent *pEvent, GuestDnDState *pState, RTMSINTERVAL msTimeout)
1598{
1599 AssertPtrReturn(pEvent, VERR_INVALID_POINTER);
1600 AssertPtrReturn(pState, VERR_INVALID_POINTER);
1601
1602 int rc;
1603
1604 LogFlowFunc(("msTimeout=%RU32\n", msTimeout));
1605
1606 uint64_t tsStart = RTTimeMilliTS();
1607 do
1608 {
1609 /*
1610 * Wait until our desired callback triggered the
1611 * wait event. As we don't want to block if the guest does not
1612 * respond, do busy waiting here.
1613 */
1614 rc = pEvent->Wait(500 /* ms */);
1615 if (RT_SUCCESS(rc))
1616 {
1617 rc = pEvent->Result();
1618 LogFlowFunc(("Callback done, result is %Rrc\n", rc));
1619 break;
1620 }
1621 else if (rc == VERR_TIMEOUT) /* Continue waiting. */
1622 rc = VINF_SUCCESS;
1623
1624 if ( msTimeout != RT_INDEFINITE_WAIT
1625 && RTTimeMilliTS() - tsStart > msTimeout)
1626 {
1627 rc = VERR_TIMEOUT;
1628 LogRel2(("DnD: Error: Guest did not respond within time\n"));
1629 }
1630 else if (pState->isProgressCanceled())
1631 {
1632 LogRel2(("DnD: Operation was canceled by user\n"));
1633 rc = VERR_CANCELLED;
1634 }
1635
1636 } while (RT_SUCCESS(rc));
1637
1638 LogFlowFuncLeaveRC(rc);
1639 return rc;
1640}
1641#endif /* VBOX_WITH_DRAG_AND_DROP */
1642
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use