VirtualBox

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

Last change on this file since 107402 was 107189, checked in by vboxsync, 7 weeks ago

src/VBox/Main/src-client/GuestDnDPrivate: Fixed missing constructor intializers, found by Parfait.

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