VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/GuestDnDTargetImpl.cpp@ 103415

Last change on this file since 103415 was 103415, checked in by vboxsync, 8 months ago

Additions,Main,VMM,Runtime: Fix some unused expression warnings, bugref:3409

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 63.4 KB
RevLine 
[51476]1/* $Id: GuestDnDTargetImpl.cpp 103415 2024-02-19 07:52:27Z vboxsync $ */
2/** @file
3 * VBox Console COM Class implementation - Guest drag'n drop target.
4 */
5
6/*
[98103]7 * Copyright (C) 2014-2023 Oracle and/or its affiliates.
[51476]8 *
[96407]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
[51476]26 */
27
28
[57358]29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
[67914]32#define LOG_GROUP LOG_GROUP_GUEST_DND //LOG_GROUP_MAIN_GUESTDNDTARGET
33#include "LoggingNew.h"
34
[51476]35#include "GuestImpl.h"
36#include "GuestDnDTargetImpl.h"
[55644]37#include "ConsoleImpl.h"
[51476]38
39#include "Global.h"
40#include "AutoCaller.h"
[58519]41#include "ThreadTask.h"
[51476]42
[55423]43#include <algorithm> /* For std::find(). */
[55422]44
[55644]45#include <iprt/asm.h>
[55422]46#include <iprt/file.h>
47#include <iprt/dir.h>
48#include <iprt/path.h>
49#include <iprt/uri.h>
[51476]50#include <iprt/cpp/utils.h> /* For unconst(). */
51
52#include <VBox/com/array.h>
53
[55422]54#include <VBox/GuestHost/DragAndDrop.h>
55#include <VBox/HostServices/Service.h>
56
[51476]57
[55422]58/**
59 * Base class for a target task.
60 */
[58519]61class GuestDnDTargetTask : public ThreadTask
[55422]62{
63public:
64
65 GuestDnDTargetTask(GuestDnDTarget *pTarget)
[58519]66 : ThreadTask("GenericGuestDnDTargetTask")
67 , mTarget(pTarget)
68 , mRC(VINF_SUCCESS) { }
[55422]69
70 virtual ~GuestDnDTargetTask(void) { }
71
[85681]72 /** Returns the overall result of the task. */
[55422]73 int getRC(void) const { return mRC; }
[85681]74 /** Returns if the overall result of the task is ok (succeeded) or not. */
[55422]75 bool isOk(void) const { return RT_SUCCESS(mRC); }
76
77protected:
78
[85681]79 /** COM object pointer to the parent (source). */
[55422]80 const ComObjPtr<GuestDnDTarget> mTarget;
[85681]81 /** Overall result of the task. */
[55422]82 int mRC;
83};
84
85/**
86 * Task structure for sending data to a target using
87 * a worker thread.
88 */
[85020]89class GuestDnDSendDataTask : public GuestDnDTargetTask
[55422]90{
91public:
92
[85020]93 GuestDnDSendDataTask(GuestDnDTarget *pTarget, GuestDnDSendCtx *pCtx)
[55422]94 : GuestDnDTargetTask(pTarget),
[58519]95 mpCtx(pCtx)
96 {
97 m_strTaskName = "dndTgtSndData";
98 }
[55422]99
[58519]100 void handler()
101 {
[85537]102 const ComObjPtr<GuestDnDTarget> pThis(mTarget);
103 Assert(!pThis.isNull());
[58519]104
[85537]105 AutoCaller autoCaller(pThis);
[94914]106 if (autoCaller.isNotOk())
[85537]107 return;
108
[98278]109 pThis->i_sendData(mpCtx, RT_INDEFINITE_WAIT /* msTimeout */); /* ignore return code */
[55422]110 }
111
[85537]112 virtual ~GuestDnDSendDataTask(void) { }
[55422]113
114protected:
115
116 /** Pointer to send data context. */
[85018]117 GuestDnDSendCtx *mpCtx;
[55422]118};
119
[51476]120// constructor / destructor
121/////////////////////////////////////////////////////////////////////////////
122
[97780]123GuestDnDTarget::GuestDnDTarget(void)
124 : GuestDnDBase(this) { }
[51476]125
[97780]126GuestDnDTarget::~GuestDnDTarget(void) { }
127
[51476]128HRESULT GuestDnDTarget::FinalConstruct(void)
129{
[55422]130 /* Set the maximum block size our guests can handle to 64K. This always has
131 * been hardcoded until now. */
132 /* Note: Never ever rely on information from the guest; the host dictates what and
133 * how to do something, so try to negogiate a sensible value here later. */
[85557]134 mData.mcbBlockSize = DND_DEFAULT_CHUNK_SIZE; /** @todo Make this configurable. */
[55422]135
[51476]136 LogFlowThisFunc(("\n"));
137 return BaseFinalConstruct();
138}
139
140void GuestDnDTarget::FinalRelease(void)
141{
142 LogFlowThisFuncEnter();
143 uninit();
144 BaseFinalRelease();
145 LogFlowThisFuncLeave();
146}
147
148// public initializer/uninitializer for internal purposes only
149/////////////////////////////////////////////////////////////////////////////
150
[85743]151HRESULT GuestDnDTarget::init(const ComObjPtr<Guest>& pGuest)
[51476]152{
153 LogFlowThisFuncEnter();
154
155 /* Enclose the state transition NotReady->InInit->Ready. */
156 AutoInitSpan autoInitSpan(this);
157 AssertReturn(autoInitSpan.isOk(), E_FAIL);
158
159 unconst(m_pGuest) = pGuest;
160
[85739]161 /* Set the response we're going to use for this object.
162 *
163 * At the moment we only have one response total, as we
164 * don't allow
165 * 1) parallel transfers (multiple G->H at the same time)
166 * nor 2) mixed transfers (G->H + H->G at the same time).
167 */
[85744]168 m_pState = GuestDnDInst()->getState();
169 AssertPtrReturn(m_pState, E_POINTER);
[85739]170
[51476]171 /* Confirm a successful initialization when it's the case. */
172 autoInitSpan.setSucceeded();
173
[85743]174 return S_OK;
[51476]175}
176
177/**
178 * Uninitializes the instance.
179 * Called from FinalRelease().
180 */
181void GuestDnDTarget::uninit(void)
182{
183 LogFlowThisFunc(("\n"));
184
185 /* Enclose the state transition Ready->InUninit->NotReady. */
186 AutoUninitSpan autoUninitSpan(this);
187 if (autoUninitSpan.uninitDone())
188 return;
189}
190
[51556]191// implementation of wrapped IDnDBase methods.
[51476]192/////////////////////////////////////////////////////////////////////////////
193
[55422]194HRESULT GuestDnDTarget::isFormatSupported(const com::Utf8Str &aFormat, BOOL *aSupported)
[51556]195{
[55422]196#if !defined(VBOX_WITH_DRAG_AND_DROP)
[51556]197 ReturnComNotImplemented();
198#else /* VBOX_WITH_DRAG_AND_DROP */
199
[51620]200 AutoCaller autoCaller(this);
[98262]201 if (autoCaller.isNotOk()) return autoCaller.hrc();
[51620]202
203 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
204
[85559]205 *aSupported = GuestDnDBase::i_isFormatSupported(aFormat) ? TRUE : FALSE;
206
207 return S_OK;
[51556]208#endif /* VBOX_WITH_DRAG_AND_DROP */
209}
210
[57221]211HRESULT GuestDnDTarget::getFormats(GuestDnDMIMEList &aFormats)
[51556]212{
[55422]213#if !defined(VBOX_WITH_DRAG_AND_DROP)
[51556]214 ReturnComNotImplemented();
215#else /* VBOX_WITH_DRAG_AND_DROP */
216
[51620]217 AutoCaller autoCaller(this);
[98262]218 if (autoCaller.isNotOk()) return autoCaller.hrc();
[51620]219
220 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
221
[85558]222 aFormats = GuestDnDBase::i_getFormats();
223
224 return S_OK;
[51556]225#endif /* VBOX_WITH_DRAG_AND_DROP */
226}
227
[57221]228HRESULT GuestDnDTarget::addFormats(const GuestDnDMIMEList &aFormats)
[51556]229{
[55422]230#if !defined(VBOX_WITH_DRAG_AND_DROP)
[51556]231 ReturnComNotImplemented();
232#else /* VBOX_WITH_DRAG_AND_DROP */
233
[51620]234 AutoCaller autoCaller(this);
[98262]235 if (autoCaller.isNotOk()) return autoCaller.hrc();
[51620]236
237 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
238
[55422]239 return GuestDnDBase::i_addFormats(aFormats);
[51556]240#endif /* VBOX_WITH_DRAG_AND_DROP */
241}
242
[57221]243HRESULT GuestDnDTarget::removeFormats(const GuestDnDMIMEList &aFormats)
[51556]244{
[55422]245#if !defined(VBOX_WITH_DRAG_AND_DROP)
[51556]246 ReturnComNotImplemented();
247#else /* VBOX_WITH_DRAG_AND_DROP */
248
[51620]249 AutoCaller autoCaller(this);
[98262]250 if (autoCaller.isNotOk()) return autoCaller.hrc();
[51620]251
252 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
253
[55422]254 return GuestDnDBase::i_removeFormats(aFormats);
[51556]255#endif /* VBOX_WITH_DRAG_AND_DROP */
256}
257
258// implementation of wrapped IDnDTarget methods.
259/////////////////////////////////////////////////////////////////////////////
260
[51476]261HRESULT GuestDnDTarget::enter(ULONG aScreenId, ULONG aX, ULONG aY,
[56656]262 DnDAction_T aDefaultAction,
263 const std::vector<DnDAction_T> &aAllowedActions,
[58370]264 const GuestDnDMIMEList &aFormats,
[56656]265 DnDAction_T *aResultAction)
[51476]266{
[55422]267#if !defined(VBOX_WITH_DRAG_AND_DROP)
[51476]268 ReturnComNotImplemented();
269#else /* VBOX_WITH_DRAG_AND_DROP */
270
271 /* Input validation. */
272 if (aDefaultAction == DnDAction_Ignore)
273 return setError(E_INVALIDARG, tr("No default action specified"));
274 if (!aAllowedActions.size())
275 return setError(E_INVALIDARG, tr("Number of allowed actions is empty"));
276 if (!aFormats.size())
277 return setError(E_INVALIDARG, tr("Number of supported formats is empty"));
278
279 AutoCaller autoCaller(this);
[98262]280 if (autoCaller.isNotOk()) return autoCaller.hrc();
[51476]281
282 /* Default action is ignoring. */
283 DnDAction_T resAction = DnDAction_Ignore;
284
[74439]285 /* Check & convert the drag & drop actions. */
286 VBOXDNDACTION dndActionDefault = 0;
287 VBOXDNDACTIONLIST dndActionListAllowed = 0;
288 GuestDnD::toHGCMActions(aDefaultAction, &dndActionDefault,
289 aAllowedActions, &dndActionListAllowed);
290
[51476]291 /* If there is no usable action, ignore this request. */
[74439]292 if (isDnDIgnoreAction(dndActionDefault))
[51476]293 return S_OK;
294
[97783]295 GuestDnDState *pState = GuestDnDInst()->getState();
296 AssertPtrReturn(pState, E_POINTER);
297
[57221]298 /*
299 * Make a flat data string out of the supported format list.
300 * In the GuestDnDTarget case the source formats are from the host,
301 * as GuestDnDTarget acts as a source for the guest.
302 */
[58370]303 Utf8Str strFormats = GuestDnD::toFormatString(GuestDnD::toFilteredFormatList(m_lstFmtSupported, aFormats));
[51476]304 if (strFormats.isEmpty())
[57221]305 return setError(E_INVALIDARG, tr("No or not supported format(s) specified"));
[58370]306 const uint32_t cbFormats = (uint32_t)strFormats.length() + 1; /* Include terminating zero. */
[51476]307
[56656]308 LogRel2(("DnD: Offered formats to guest:\n"));
[85746]309 RTCList<RTCString> lstFormats = strFormats.split(DND_PATH_SEPARATOR_STR);
[56656]310 for (size_t i = 0; i < lstFormats.size(); i++)
311 LogRel2(("DnD: \t%s\n", lstFormats[i].c_str()));
312
313 /* Save the formats offered to the guest. This is needed to later
314 * decide what to do with the data when sending stuff to the guest. */
[57221]315 m_lstFmtOffered = aFormats;
316 Assert(m_lstFmtOffered.size());
[56656]317
[51476]318 /* Adjust the coordinates in a multi-monitor setup. */
[97724]319 HRESULT hrc = GuestDnDInst()->adjustScreenCoordinates(aScreenId, &aX, &aY);
320 if (SUCCEEDED(hrc))
[51476]321 {
[55520]322 GuestDnDMsg Msg;
[85745]323 Msg.setType(HOST_DND_FN_HG_EVT_ENTER);
[85744]324 if (m_pState->m_uProtocolVersion >= 3)
[85554]325 Msg.appendUInt32(0); /** @todo ContextID not used yet. */
326 Msg.appendUInt32(aScreenId);
327 Msg.appendUInt32(aX);
328 Msg.appendUInt32(aY);
329 Msg.appendUInt32(dndActionDefault);
330 Msg.appendUInt32(dndActionListAllowed);
331 Msg.appendPointer((void *)strFormats.c_str(), cbFormats);
332 Msg.appendUInt32(cbFormats);
[51476]333
[97724]334 int vrc = GuestDnDInst()->hostCall(Msg.getType(), Msg.getCount(), Msg.getParms());
[94914]335 if (RT_SUCCESS(vrc))
[51476]336 {
[97783]337 int vrcGuest;
338 if (RT_SUCCESS(vrc = pState->waitForGuestResponse(&vrcGuest)))
[97724]339 {
[85744]340 resAction = GuestDnD::toMainAction(m_pState->getActionDefault());
[97724]341
342 LogRel2(("DnD: Host enters the VM window at %RU32,%RU32 (screen %u, default action is '%s') -> guest reported back action '%s'\n",
343 aX, aY, aScreenId, DnDActionToStr(dndActionDefault), DnDActionToStr(resAction)));
[97784]344
345 pState->set(VBOXDNDSTATE_ENTERED);
[97724]346 }
347 else
[97783]348 hrc = i_setErrorAndReset(vrc == VERR_DND_GUEST_ERROR ? vrcGuest : vrc, tr("Entering VM window failed"));
[51476]349 }
[97724]350 else
[97783]351 {
352 switch (vrc)
353 {
354 case VERR_ACCESS_DENIED:
355 {
356 hrc = i_setErrorAndReset(tr("Drag and drop to guest not allowed. Select the right mode first"));
357 break;
358 }
359
360 case VERR_NOT_SUPPORTED:
361 {
362 hrc = i_setErrorAndReset(tr("Drag and drop to guest not possible -- either the guest OS does not support this, "
363 "or the Guest Additions are not installed"));
364 break;
365 }
366
367 default:
368 hrc = i_setErrorAndReset(vrc, tr("Entering VM window failed"));
369 break;
370 }
371 }
[51476]372 }
373
[94914]374 if (SUCCEEDED(hrc))
[56656]375 {
376 if (aResultAction)
377 *aResultAction = resAction;
378 }
379
[94914]380 LogFlowFunc(("hrc=%Rhrc, resAction=%ld\n", hrc, resAction));
381 return hrc;
[51476]382#endif /* VBOX_WITH_DRAG_AND_DROP */
383}
384
385HRESULT GuestDnDTarget::move(ULONG aScreenId, ULONG aX, ULONG aY,
[56656]386 DnDAction_T aDefaultAction,
387 const std::vector<DnDAction_T> &aAllowedActions,
[57221]388 const GuestDnDMIMEList &aFormats,
[56656]389 DnDAction_T *aResultAction)
[51476]390{
[55422]391#if !defined(VBOX_WITH_DRAG_AND_DROP)
[51476]392 ReturnComNotImplemented();
393#else /* VBOX_WITH_DRAG_AND_DROP */
394
395 /* Input validation. */
396
397 AutoCaller autoCaller(this);
[98262]398 if (autoCaller.isNotOk()) return autoCaller.hrc();
[51476]399
400 /* Default action is ignoring. */
401 DnDAction_T resAction = DnDAction_Ignore;
402
403 /* Check & convert the drag & drop actions. */
[74439]404 VBOXDNDACTION dndActionDefault = 0;
405 VBOXDNDACTIONLIST dndActionListAllowed = 0;
406 GuestDnD::toHGCMActions(aDefaultAction, &dndActionDefault,
407 aAllowedActions, &dndActionListAllowed);
408
[51476]409 /* If there is no usable action, ignore this request. */
[74439]410 if (isDnDIgnoreAction(dndActionDefault))
[51476]411 return S_OK;
412
[97783]413 GuestDnDState *pState = GuestDnDInst()->getState();
414 AssertPtrReturn(pState, E_POINTER);
415
[57221]416 /*
417 * Make a flat data string out of the supported format list.
418 * In the GuestDnDTarget case the source formats are from the host,
419 * as GuestDnDTarget acts as a source for the guest.
420 */
[58370]421 Utf8Str strFormats = GuestDnD::toFormatString(GuestDnD::toFilteredFormatList(m_lstFmtSupported, aFormats));
[51476]422 if (strFormats.isEmpty())
[57221]423 return setError(E_INVALIDARG, tr("No or not supported format(s) specified"));
[58370]424 const uint32_t cbFormats = (uint32_t)strFormats.length() + 1; /* Include terminating zero. */
[51476]425
[97724]426 HRESULT hrc = GuestDnDInst()->adjustScreenCoordinates(aScreenId, &aX, &aY);
427 if (SUCCEEDED(hrc))
[51476]428 {
[55520]429 GuestDnDMsg Msg;
[85745]430 Msg.setType(HOST_DND_FN_HG_EVT_MOVE);
[85744]431 if (m_pState->m_uProtocolVersion >= 3)
[85554]432 Msg.appendUInt32(0); /** @todo ContextID not used yet. */
433 Msg.appendUInt32(aScreenId);
434 Msg.appendUInt32(aX);
435 Msg.appendUInt32(aY);
436 Msg.appendUInt32(dndActionDefault);
437 Msg.appendUInt32(dndActionListAllowed);
438 Msg.appendPointer((void *)strFormats.c_str(), cbFormats);
439 Msg.appendUInt32(cbFormats);
[51476]440
[97724]441 int vrc = GuestDnDInst()->hostCall(Msg.getType(), Msg.getCount(), Msg.getParms());
[94914]442 if (RT_SUCCESS(vrc))
[51476]443 {
[97783]444 int vrcGuest;
445 if (RT_SUCCESS(vrc = pState->waitForGuestResponse(&vrcGuest)))
[97724]446 {
[85744]447 resAction = GuestDnD::toMainAction(pState->getActionDefault());
[97720]448
[97724]449 LogRel2(("DnD: Host moved to %RU32,%RU32 in VM window (screen %u, default action is '%s') -> guest reported back action '%s'\n",
450 aX, aY, aScreenId, DnDActionToStr(dndActionDefault), DnDActionToStr(resAction)));
[97784]451
452 pState->set(VBOXDNDSTATE_DRAGGING);
[97724]453 }
454 else
[97783]455 hrc = i_setErrorAndReset(vrc == VERR_DND_GUEST_ERROR ? vrcGuest : vrc,
456 tr("Moving to %RU32,%RU32 (screen %u) failed"), aX, aY, aScreenId);
[51476]457 }
[97724]458 else
[97783]459 {
460 switch (vrc)
461 {
462 case VERR_ACCESS_DENIED:
463 {
464 hrc = i_setErrorAndReset(tr("Moving in guest not allowed. Select the right mode first"));
465 break;
466 }
467
468 case VERR_NOT_SUPPORTED:
469 {
470 hrc = i_setErrorAndReset(tr("Moving in guest not possible -- either the guest OS does not support this, "
471 "or the Guest Additions are not installed"));
472 break;
473 }
474
475 default:
476 hrc = i_setErrorAndReset(vrc, tr("Moving in VM window failed"));
477 break;
478 }
479 }
[51476]480 }
[97724]481 else
[97783]482 hrc = i_setErrorAndReset(tr("Retrieving move coordinates failed"));
[51476]483
[94914]484 if (SUCCEEDED(hrc))
[56656]485 {
486 if (aResultAction)
487 *aResultAction = resAction;
488 }
489
[94914]490 LogFlowFunc(("hrc=%Rhrc, *pResultAction=%ld\n", hrc, resAction));
491 return hrc;
[51476]492#endif /* VBOX_WITH_DRAG_AND_DROP */
493}
494
495HRESULT GuestDnDTarget::leave(ULONG uScreenId)
496{
[63252]497 RT_NOREF(uScreenId);
[55422]498#if !defined(VBOX_WITH_DRAG_AND_DROP)
[51476]499 ReturnComNotImplemented();
500#else /* VBOX_WITH_DRAG_AND_DROP */
501
502 AutoCaller autoCaller(this);
[98262]503 if (autoCaller.isNotOk()) return autoCaller.hrc();
[51476]504
[97783]505 GuestDnDState *pState = GuestDnDInst()->getState();
506 AssertPtrReturn(pState, E_POINTER);
507
[97784]508 if (pState->get() == VBOXDNDSTATE_DROP_STARTED)
509 return S_OK;
510
[94914]511 HRESULT hrc = S_OK;
[73941]512
[97717]513 LogRel2(("DnD: Host left the VM window (screen %u)\n", uScreenId));
514
[73941]515 GuestDnDMsg Msg;
[85745]516 Msg.setType(HOST_DND_FN_HG_EVT_LEAVE);
[85744]517 if (m_pState->m_uProtocolVersion >= 3)
[85554]518 Msg.appendUInt32(0); /** @todo ContextID not used yet. */
[73941]519
[94914]520 int vrc = GuestDnDInst()->hostCall(Msg.getType(), Msg.getCount(), Msg.getParms());
521 if (RT_SUCCESS(vrc))
[51476]522 {
[97783]523 int vrcGuest;
524 if (RT_SUCCESS(vrc = pState->waitForGuestResponse(&vrcGuest)))
[97724]525 {
[97784]526 pState->set(VBOXDNDSTATE_LEFT);
[97724]527 }
528 else
[97783]529 hrc = i_setErrorAndReset(vrc == VERR_DND_GUEST_ERROR ? vrcGuest : vrc, tr("Leaving VM window failed"));
[51476]530 }
[97724]531 else
[97783]532 {
533 switch (vrc)
534 {
535 case VERR_ACCESS_DENIED:
536 {
537 hrc = i_setErrorAndReset(tr("Leaving guest not allowed. Select the right mode first"));
538 break;
539 }
[51476]540
[97783]541 case VERR_NOT_SUPPORTED:
542 {
543 hrc = i_setErrorAndReset(tr("Leaving guest not possible -- either the guest OS does not support this, "
544 "or the Guest Additions are not installed"));
545 break;
546 }
547
548 default:
549 hrc = i_setErrorAndReset(vrc, tr("Leaving VM window failed"));
550 break;
551 }
552 }
553
[94914]554 LogFlowFunc(("hrc=%Rhrc\n", hrc));
555 return hrc;
[51476]556#endif /* VBOX_WITH_DRAG_AND_DROP */
557}
558
559HRESULT GuestDnDTarget::drop(ULONG aScreenId, ULONG aX, ULONG aY,
[56656]560 DnDAction_T aDefaultAction,
561 const std::vector<DnDAction_T> &aAllowedActions,
[57221]562 const GuestDnDMIMEList &aFormats,
[56656]563 com::Utf8Str &aFormat,
564 DnDAction_T *aResultAction)
[51476]565{
[55422]566#if !defined(VBOX_WITH_DRAG_AND_DROP)
[51476]567 ReturnComNotImplemented();
568#else /* VBOX_WITH_DRAG_AND_DROP */
569
[57221]570 if (aDefaultAction == DnDAction_Ignore)
571 return setError(E_INVALIDARG, tr("Invalid default action specified"));
572 if (!aAllowedActions.size())
573 return setError(E_INVALIDARG, tr("Invalid allowed actions specified"));
574 if (!aFormats.size())
575 return setError(E_INVALIDARG, tr("No drop format(s) specified"));
576 /* aResultAction is optional. */
[51476]577
578 AutoCaller autoCaller(this);
[98262]579 if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
[51476]580
581 /* Default action is ignoring. */
[85537]582 DnDAction_T resAct = DnDAction_Ignore;
583 Utf8Str resFmt;
[51476]584
[57221]585 /* Check & convert the drag & drop actions to HGCM codes. */
[74439]586 VBOXDNDACTION dndActionDefault = VBOX_DND_ACTION_IGNORE;
587 VBOXDNDACTIONLIST dndActionListAllowed = 0;
588 GuestDnD::toHGCMActions(aDefaultAction, &dndActionDefault,
589 aAllowedActions, &dndActionListAllowed);
590
[51476]591 /* If there is no usable action, ignore this request. */
[74439]592 if (isDnDIgnoreAction(dndActionDefault))
[56656]593 {
594 aFormat = "";
595 if (aResultAction)
596 *aResultAction = DnDAction_Ignore;
[51476]597 return S_OK;
[56656]598 }
[51476]599
[97783]600 GuestDnDState *pState = GuestDnDInst()->getState();
601 AssertPtrReturn(pState, E_POINTER);
602
[57221]603 /*
604 * Make a flat data string out of the supported format list.
605 * In the GuestDnDTarget case the source formats are from the host,
606 * as GuestDnDTarget acts as a source for the guest.
607 */
[58370]608 Utf8Str strFormats = GuestDnD::toFormatString(GuestDnD::toFilteredFormatList(m_lstFmtSupported, aFormats));
[51476]609 if (strFormats.isEmpty())
[57221]610 return setError(E_INVALIDARG, tr("No or not supported format(s) specified"));
[58370]611 const uint32_t cbFormats = (uint32_t)strFormats.length() + 1; /* Include terminating zero. */
[51476]612
613 /* Adjust the coordinates in a multi-monitor setup. */
[97724]614 HRESULT hrc = GuestDnDInst()->adjustScreenCoordinates(aScreenId, &aX, &aY);
615 if (SUCCEEDED(hrc))
[51476]616 {
[55520]617 GuestDnDMsg Msg;
[85745]618 Msg.setType(HOST_DND_FN_HG_EVT_DROPPED);
[85744]619 if (m_pState->m_uProtocolVersion >= 3)
[85554]620 Msg.appendUInt32(0); /** @todo ContextID not used yet. */
621 Msg.appendUInt32(aScreenId);
622 Msg.appendUInt32(aX);
623 Msg.appendUInt32(aY);
624 Msg.appendUInt32(dndActionDefault);
625 Msg.appendUInt32(dndActionListAllowed);
626 Msg.appendPointer((void*)strFormats.c_str(), cbFormats);
627 Msg.appendUInt32(cbFormats);
[51476]628
[97717]629 LogRel2(("DnD: Host drops at %RU32,%RU32 in VM window (screen %u, default action is '%s')\n",
630 aX, aY, aScreenId, DnDActionToStr(dndActionDefault)));
631
[85537]632 int vrc = GuestDnDInst()->hostCall(Msg.getType(), Msg.getCount(), Msg.getParms());
[73003]633 if (RT_SUCCESS(vrc))
[51476]634 {
[97783]635 int vrcGuest;
636 if (RT_SUCCESS(vrc = pState->waitForGuestResponse(&vrcGuest)))
[51476]637 {
[85744]638 resAct = GuestDnD::toMainAction(pState->getActionDefault());
[97717]639 if (resAct != DnDAction_Ignore) /* Does the guest accept a drop at the current position? */
640 {
641 GuestDnDMIMEList lstFormats = pState->formats();
642 if (lstFormats.size() == 1) /* Exactly one format to use specified? */
643 {
644 resFmt = lstFormats.at(0);
[97784]645
646 LogRel2(("DnD: Guest accepted drop in format '%s' (action %#x, %zu format(s))\n",
647 resFmt.c_str(), resAct, lstFormats.size()));
648
649 pState->set(VBOXDNDSTATE_DROP_STARTED);
[97717]650 }
651 else
652 {
653 if (lstFormats.size() == 0)
[97783]654 hrc = i_setErrorAndReset(VERR_DND_GUEST_ERROR, tr("Guest accepted drop, but did not specify the format"));
[97717]655 else
[97783]656 hrc = i_setErrorAndReset(VERR_DND_GUEST_ERROR, tr("Guest accepted drop, but returned more than one drop format (%zu formats)"),
657 lstFormats.size());
[97717]658 }
[57221]659 }
[51476]660 }
[57221]661 else
[97783]662 hrc = i_setErrorAndReset(vrc == VERR_DND_GUEST_ERROR ? vrcGuest : vrc, tr("Dropping into VM failed"));
[51476]663 }
[57221]664 else
[97783]665 hrc = i_setErrorAndReset(vrc, tr("Sending dropped event to guest failed"));
[51476]666 }
[57221]667 else
[97783]668 hrc = i_setErrorAndReset(hrc, tr("Retrieving drop coordinates failed"));
[51476]669
[97724]670 if (SUCCEEDED(hrc))
[56656]671 {
[85537]672 aFormat = resFmt;
[56656]673 if (aResultAction)
[85537]674 *aResultAction = resAct;
[56656]675 }
676
[97724]677 return hrc;
[51476]678#endif /* VBOX_WITH_DRAG_AND_DROP */
679}
680
[85371]681/**
682 * Initiates a data transfer from the host to the guest.
[55422]683 *
[85371]684 * The source is the host, whereas the target is the guest.
685 *
[55422]686 * @return HRESULT
[85371]687 * @param aScreenId Screen ID where this data transfer was initiated from.
688 * @param aFormat Format of data to send. MIME-style.
689 * @param aData Actual data to send.
690 * @param aProgress Where to return the progress object on success.
[55422]691 */
692HRESULT GuestDnDTarget::sendData(ULONG aScreenId, const com::Utf8Str &aFormat, const std::vector<BYTE> &aData,
[51477]693 ComPtr<IProgress> &aProgress)
[51476]694{
[55422]695#if !defined(VBOX_WITH_DRAG_AND_DROP)
[51476]696 ReturnComNotImplemented();
697#else /* VBOX_WITH_DRAG_AND_DROP */
698
699 AutoCaller autoCaller(this);
[98262]700 if (FAILED(autoCaller.hrc())) return autoCaller.hrc();
[51476]701
[55549]702 /* Input validation. */
703 if (RT_UNLIKELY((aFormat.c_str()) == NULL || *(aFormat.c_str()) == '\0'))
704 return setError(E_INVALIDARG, tr("No data format specified"));
705 if (RT_UNLIKELY(!aData.size()))
706 return setError(E_INVALIDARG, tr("No data to send specified"));
707
[58212]708 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
[55549]709
[85537]710 /* Check if this object still is in a pending state and bail out if so. */
711 if (m_fIsPending)
712 return setError(E_FAIL, tr("Current drop operation to guest still in progress"));
713
[58212]714 /* At the moment we only support one transfer at a time. */
[85537]715 if (GuestDnDInst()->getTargetCount())
716 return setError(E_INVALIDARG, tr("Another drag and drop operation to the guest already is in progress"));
[55549]717
[85537]718 /* Reset progress object. */
[85744]719 GuestDnDState *pState = GuestDnDInst()->getState();
720 AssertPtr(pState);
[97802]721 HRESULT hr = pState->resetProgress(m_pGuest, tr("Dropping data to guest"));
[55539]722 if (FAILED(hr))
723 return hr;
[55422]724
[85537]725 GuestDnDSendDataTask *pTask = NULL;
[58519]726
[55539]727 try
728 {
[85537]729 mData.mSendCtx.reset();
[58212]730
[85537]731 mData.mSendCtx.pTarget = this;
[85744]732 mData.mSendCtx.pState = pState;
[85537]733 mData.mSendCtx.uScreenID = aScreenId;
[85371]734
[85537]735 mData.mSendCtx.Meta.strFmt = aFormat;
736 mData.mSendCtx.Meta.add(aData);
737
[97717]738 LogRel2(("DnD: Host sends data in format '%s'\n", aFormat.c_str()));
739
[85537]740 pTask = new GuestDnDSendDataTask(this, &mData.mSendCtx);
[58519]741 if (!pTask->isOk())
742 {
743 delete pTask;
[78084]744 LogRel(("DnD: Could not create SendDataTask object\n"));
[58519]745 throw hr = E_FAIL;
746 }
[55422]747
[63471]748 /* This function delete pTask in case of exceptions,
749 * so there is no need in the call of delete operator. */
750 hr = pTask->createThreadWithType(RTTHREADTYPE_MAIN_WORKER);
[78745]751 pTask = NULL; /* Note: pTask is now owned by the worker thread. */
[58519]752 }
[63159]753 catch (std::bad_alloc &)
[58519]754 {
[97783]755 hr = E_OUTOFMEMORY;
[58519]756 }
[63159]757 catch (...)
[58519]758 {
[78084]759 LogRel(("DnD: Could not create thread for data sending task\n"));
[58519]760 hr = E_FAIL;
761 }
762
763 if (SUCCEEDED(hr))
764 {
[85537]765 /* Register ourselves at the DnD manager. */
766 GuestDnDInst()->registerTarget(this);
[85451]767
[85537]768 /* Return progress to caller. */
[85744]769 hr = pState->queryProgressTo(aProgress.asOutParam());
[63471]770 ComAssertComRC(hr);
[51476]771 }
[58519]772 else
[97783]773 hr = i_setErrorAndReset(tr("Starting thread for GuestDnDTarget failed (%Rhrc)"), hr);
[51476]774
[55539]775 LogFlowFunc(("Returning hr=%Rhrc\n", hr));
[51476]776 return hr;
777#endif /* VBOX_WITH_DRAG_AND_DROP */
778}
779
[85681]780/**
781 * Returns an error string from a guest DnD error.
782 *
783 * @returns Error string.
784 * @param guestRc Guest error to return error string for.
785 */
[55963]786/* static */
787Utf8Str GuestDnDTarget::i_guestErrorToString(int guestRc)
788{
789 Utf8Str strError;
790
791 switch (guestRc)
792 {
793 case VERR_ACCESS_DENIED:
794 strError += Utf8StrFmt(tr("For one or more guest files or directories selected for transferring to the host your guest "
795 "user does not have the appropriate access rights for. Please make sure that all selected "
[56553]796 "elements can be accessed and that your guest user has the appropriate rights"));
[55963]797 break;
798
799 case VERR_NOT_FOUND:
800 /* Should not happen due to file locking on the guest, but anyway ... */
801 strError += Utf8StrFmt(tr("One or more guest files or directories selected for transferring to the host were not"
802 "found on the guest anymore. This can be the case if the guest files were moved and/or"
[56553]803 "altered while the drag and drop operation was in progress"));
[55963]804 break;
805
806 case VERR_SHARING_VIOLATION:
807 strError += Utf8StrFmt(tr("One or more guest files or directories selected for transferring to the host were locked. "
808 "Please make sure that all selected elements can be accessed and that your guest user has "
[56553]809 "the appropriate rights"));
[55963]810 break;
811
[56553]812 case VERR_TIMEOUT:
813 strError += Utf8StrFmt(tr("The guest was not able to process the drag and drop data within time"));
814 break;
815
[55963]816 default:
817 strError += Utf8StrFmt(tr("Drag and drop error from guest (%Rrc)"), guestRc);
818 break;
819 }
820
821 return strError;
822}
823
[85681]824/**
825 * Returns an error string from a host DnD error.
826 *
827 * @returns Error string.
828 * @param hostRc Host error to return error string for.
829 */
[55963]830/* static */
831Utf8Str GuestDnDTarget::i_hostErrorToString(int hostRc)
832{
833 Utf8Str strError;
834
835 switch (hostRc)
836 {
837 case VERR_ACCESS_DENIED:
838 strError += Utf8StrFmt(tr("For one or more host files or directories selected for transferring to the guest your host "
839 "user does not have the appropriate access rights for. Please make sure that all selected "
840 "elements can be accessed and that your host user has the appropriate rights."));
841 break;
842
843 case VERR_NOT_FOUND:
844 /* Should not happen due to file locking on the host, but anyway ... */
845 strError += Utf8StrFmt(tr("One or more host files or directories selected for transferring to the host were not"
846 "found on the host anymore. This can be the case if the host files were moved and/or"
847 "altered while the drag and drop operation was in progress."));
848 break;
849
850 case VERR_SHARING_VIOLATION:
851 strError += Utf8StrFmt(tr("One or more host files or directories selected for transferring to the guest were locked. "
852 "Please make sure that all selected elements can be accessed and that your host user has "
853 "the appropriate rights."));
854 break;
855
856 default:
857 strError += Utf8StrFmt(tr("Drag and drop error from host (%Rrc)"), hostRc);
858 break;
859 }
860
861 return strError;
862}
863
[85681]864/**
865 * Resets all internal data and state.
866 */
[85537]867void GuestDnDTarget::i_reset(void)
868{
[97788]869 LogRel2(("DnD: Target reset\n"));
[85537]870
871 mData.mSendCtx.reset();
872
873 m_fIsPending = false;
874
875 /* Unregister ourselves from the DnD manager. */
876 GuestDnDInst()->unregisterTarget(this);
877}
878
[63183]879/**
[85371]880 * Main function for sending DnD host data to the guest.
881 *
882 * @returns VBox status code.
883 * @param pCtx Send context to use.
884 * @param msTimeout Timeout (in ms) to wait for getting the data sent.
[63183]885 */
[85018]886int GuestDnDTarget::i_sendData(GuestDnDSendCtx *pCtx, RTMSINTERVAL msTimeout)
[55422]887{
[55963]888 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
[55422]889
[85537]890 /* Don't allow receiving the actual data until our current transfer is complete. */
891 if (m_fIsPending)
892 return setError(E_FAIL, tr("Current drop operation to guest still in progress"));
[55422]893
[55549]894 /* Clear all remaining outgoing messages. */
[85402]895 m_DataBase.lstMsgOut.clear();
[55549]896
[56657]897 /**
898 * Do we need to build up a file tree?
899 * Note: The decision whether we need to build up a file tree and sending
900 * actual file data only depends on the actual formats offered by this target.
[85371]901 * If the guest does not want a transfer list ("text/uri-list") but text ("TEXT" and
[56657]902 * friends) instead, still send the data over to the guest -- the file as such still
903 * is needed on the guest in this case, as the guest then just wants a simple path
[85371]904 * instead of a transfer list (pointing to a file on the guest itself).
[56657]905 *
906 ** @todo Support more than one format; add a format<->function handler concept. Later. */
[94914]907 int vrc;
[85402]908 const bool fHasURIList = std::find(m_lstFmtOffered.begin(),
909 m_lstFmtOffered.end(), "text/uri-list") != m_lstFmtOffered.end();
[55571]910 if (fHasURIList)
[55422]911 {
[94914]912 vrc = i_sendTransferData(pCtx, msTimeout);
[55571]913 }
914 else
915 {
[94914]916 vrc = i_sendRawData(pCtx, msTimeout);
[55571]917 }
[55422]918
[97784]919 GuestDnDState *pState = GuestDnDInst()->getState();
920 AssertPtrReturn(pState, E_POINTER);
921
922 if (RT_SUCCESS(vrc))
[85564]923 {
[97784]924 pState->set(VBOXDNDSTATE_DROP_ENDED);
925 }
926 else
927 {
928 if (vrc == VERR_CANCELLED)
929 {
930 LogRel(("DnD: Sending data to guest cancelled by the user\n"));
931 pState->set(VBOXDNDSTATE_CANCELLED);
932 }
933 else
934 {
935 LogRel(("DnD: Sending data to guest failed with %Rrc\n", vrc));
936 pState->set(VBOXDNDSTATE_ERROR);
937 }
938
939 /* Make sure to fire a cancel request to the guest side in any case to prevent any guest side hangs. */
[85564]940 sendCancel();
941 }
[55422]942
[85537]943 /* Reset state. */
944 i_reset();
945
[94914]946 LogFlowFuncLeaveRC(vrc);
947 return vrc;
[55422]948}
949
[85371]950/**
951 * Sends the common meta data body to the guest.
952 *
953 * @returns VBox status code.
954 * @param pCtx Send context to use.
955 */
956int GuestDnDTarget::i_sendMetaDataBody(GuestDnDSendCtx *pCtx)
[58212]957{
[85371]958 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
[58212]959
[85423]960 uint8_t *pvData = (uint8_t *)pCtx->Meta.pvData;
961 size_t cbData = pCtx->Meta.cbData;
[58212]962
[94914]963 int vrc = VINF_SUCCESS;
[58212]964
[85423]965 const size_t cbFmt = pCtx->Meta.strFmt.length() + 1; /* Include terminator. */
966 const char *pcszFmt = pCtx->Meta.strFmt.c_str();
[58212]967
[85744]968 LogFlowFunc(("uProtoVer=%RU32, szFmt=%s, cbFmt=%RU32, cbData=%zu\n", m_pState->m_uProtocolVersion, pcszFmt, cbFmt, cbData));
[85423]969
970 LogRel2(("DnD: Sending meta data to guest as '%s' (%zu bytes)\n", pcszFmt, cbData));
971
972#ifdef DEBUG
[85746]973 RTCList<RTCString> lstFilesURI = RTCString((char *)pvData, cbData).split(DND_PATH_SEPARATOR_STR);
[85423]974 LogFlowFunc(("lstFilesURI=%zu\n", lstFilesURI.size()));
975 for (size_t i = 0; i < lstFilesURI.size(); i++)
976 LogFlowFunc(("\t%s\n", lstFilesURI.at(i).c_str()));
977#endif
978
979 uint8_t *pvChunk = pvData;
980 size_t cbChunk = RT_MIN(mData.mcbBlockSize, cbData);
981 while (cbData)
[58212]982 {
[85423]983 GuestDnDMsg Msg;
[85745]984 Msg.setType(HOST_DND_FN_HG_SND_DATA);
[85423]985
[85744]986 if (m_pState->m_uProtocolVersion < 3)
[85423]987 {
[85554]988 Msg.appendUInt32(pCtx->uScreenID); /* uScreenId */
989 Msg.appendPointer(unconst(pcszFmt), (uint32_t)cbFmt); /* pvFormat */
990 Msg.appendUInt32((uint32_t)cbFmt); /* cbFormat */
991 Msg.appendPointer(pvChunk, (uint32_t)cbChunk); /* pvData */
[85423]992 /* Fill in the current data block size to send.
993 * Note: Only supports uint32_t. */
[85554]994 Msg.appendUInt32((uint32_t)cbChunk); /* cbData */
[85423]995 }
996 else
997 {
[85554]998 Msg.appendUInt32(0); /** @todo ContextID not used yet. */
999 Msg.appendPointer(pvChunk, (uint32_t)cbChunk); /* pvData */
1000 Msg.appendUInt32((uint32_t)cbChunk); /* cbData */
1001 Msg.appendPointer(NULL, 0); /** @todo pvChecksum; not used yet. */
1002 Msg.appendUInt32(0); /** @todo cbChecksum; not used yet. */
[85423]1003 }
1004
[94914]1005 vrc = GuestDnDInst()->hostCall(Msg.getType(), Msg.getCount(), Msg.getParms());
1006 if (RT_FAILURE(vrc))
[85423]1007 break;
1008
1009 pvChunk += cbChunk;
[103415]1010 AssertBreakStmt(cbData >= cbChunk, vrc = VERR_BUFFER_UNDERFLOW);
[85423]1011 cbData -= cbChunk;
[58212]1012 }
1013
[94914]1014 if (RT_SUCCESS(vrc))
[85371]1015 {
[94914]1016 vrc = updateProgress(pCtx, pCtx->pState, (uint32_t)pCtx->Meta.cbData);
1017 AssertRC(vrc);
[85371]1018 }
[58212]1019
[94914]1020 LogFlowFuncLeaveRC(vrc);
1021 return vrc;
[58212]1022}
1023
[85371]1024/**
1025 * Sends the common meta data header to the guest.
1026 *
1027 * @returns VBox status code.
1028 * @param pCtx Send context to use.
1029 */
1030int GuestDnDTarget::i_sendMetaDataHeader(GuestDnDSendCtx *pCtx)
[58212]1031{
[85371]1032 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
[58212]1033
[85744]1034 if (m_pState->m_uProtocolVersion < 3) /* Protocol < v3 did not support this, skip. */
[85371]1035 return VINF_SUCCESS;
1036
[58212]1037 GuestDnDMsg Msg;
[85745]1038 Msg.setType(HOST_DND_FN_HG_SND_DATA_HDR);
[58212]1039
[85423]1040 LogRel2(("DnD: Sending meta data header to guest (%RU64 bytes total data, %RU32 bytes meta data, %RU64 objects)\n",
[85537]1041 pCtx->getTotalAnnounced(), pCtx->Meta.cbData, pCtx->Transfer.cObjToProcess));
[85423]1042
[85554]1043 Msg.appendUInt32(0); /** @todo uContext; not used yet. */
1044 Msg.appendUInt32(0); /** @todo uFlags; not used yet. */
1045 Msg.appendUInt32(pCtx->uScreenID); /* uScreen */
1046 Msg.appendUInt64(pCtx->getTotalAnnounced()); /* cbTotal */
1047 Msg.appendUInt32((uint32_t)pCtx->Meta.cbData); /* cbMeta*/
1048 Msg.appendPointer(unconst(pCtx->Meta.strFmt.c_str()), (uint32_t)pCtx->Meta.strFmt.length() + 1); /* pvMetaFmt */
1049 Msg.appendUInt32((uint32_t)pCtx->Meta.strFmt.length() + 1); /* cbMetaFmt */
1050 Msg.appendUInt64(pCtx->Transfer.cObjToProcess); /* cObjects */
1051 Msg.appendUInt32(0); /** @todo enmCompression; not used yet. */
1052 Msg.appendUInt32(0); /** @todo enmChecksumType; not used yet. */
1053 Msg.appendPointer(NULL, 0); /** @todo pvChecksum; not used yet. */
1054 Msg.appendUInt32(0); /** @todo cbChecksum; not used yet. */
[58212]1055
[94914]1056 int vrc = GuestDnDInst()->hostCall(Msg.getType(), Msg.getCount(), Msg.getParms());
[58212]1057
[94914]1058 LogFlowFuncLeaveRC(vrc);
1059 return vrc;
[58212]1060}
1061
[85681]1062/**
1063 * Sends a directory entry to the guest.
1064 *
1065 * @returns VBox status code.
1066 * @param pCtx Send context to use.
1067 * @param pObj Transfer object to send. Must be a directory.
1068 * @param pMsg Where to store the message to send.
1069 */
[85371]1070int GuestDnDTarget::i_sendDirectory(GuestDnDSendCtx *pCtx, PDNDTRANSFEROBJECT pObj, GuestDnDMsg *pMsg)
[55422]1071{
[85371]1072 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
1073 AssertPtrReturn(pMsg, VERR_INVALID_POINTER);
[55422]1074
[85371]1075 const char *pcszDstPath = DnDTransferObjectGetDestPath(pObj);
1076 AssertPtrReturn(pcszDstPath, VERR_INVALID_POINTER);
1077 const size_t cchPath = RTStrNLen(pcszDstPath, RTPATH_MAX); /* Note: Maximum is RTPATH_MAX on guest side. */
1078 AssertReturn(cchPath, VERR_INVALID_PARAMETER);
[57500]1079
[85371]1080 LogRel2(("DnD: Transferring host directory '%s' to guest\n", DnDTransferObjectGetSourcePath(pObj)));
[55422]1081
[85745]1082 pMsg->setType(HOST_DND_FN_HG_SND_DIR);
[85744]1083 if (m_pState->m_uProtocolVersion >= 3)
[85554]1084 pMsg->appendUInt32(0); /** @todo ContextID not used yet. */
1085 pMsg->appendString(pcszDstPath); /* path */
1086 pMsg->appendUInt32((uint32_t)(cchPath + 1)); /* path length, including terminator. */
1087 pMsg->appendUInt32(DnDTransferObjectGetMode(pObj)); /* mode */
[55422]1088
1089 return VINF_SUCCESS;
1090}
1091
[85371]1092/**
[85681]1093 * Sends a file to the guest.
[85371]1094 *
1095 * @returns VBox status code.
[85681]1096 * @param pCtx Send context to use.
1097 * @param pObj Transfer object to send. Must be a file.
1098 * @param pMsg Where to store the message to send.
[85371]1099 */
1100int GuestDnDTarget::i_sendFile(GuestDnDSendCtx *pCtx,
1101 PDNDTRANSFEROBJECT pObj, GuestDnDMsg *pMsg)
[55422]1102{
[85371]1103 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
1104 AssertPtrReturn(pObj, VERR_INVALID_POINTER);
1105 AssertPtrReturn(pMsg, VERR_INVALID_POINTER);
[55422]1106
[85371]1107 const char *pcszSrcPath = DnDTransferObjectGetSourcePath(pObj);
1108 AssertPtrReturn(pcszSrcPath, VERR_INVALID_POINTER);
1109 const char *pcszDstPath = DnDTransferObjectGetDestPath(pObj);
1110 AssertPtrReturn(pcszDstPath, VERR_INVALID_POINTER);
[57500]1111
[94914]1112 int vrc = VINF_SUCCESS;
[55422]1113
[85371]1114 if (!DnDTransferObjectIsOpen(pObj))
[55459]1115 {
[85371]1116 LogRel2(("DnD: Opening host file '%s' for transferring to guest\n", pcszSrcPath));
[84998]1117
[94914]1118 vrc = DnDTransferObjectOpen(pObj, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_WRITE, 0 /* fMode */,
1119 DNDTRANSFEROBJECT_FLAGS_NONE);
1120 if (RT_FAILURE(vrc))
[98278]1121 LogRel(("DnD: Opening host file '%s' failed, vrc=%Rrc\n", pcszSrcPath, vrc));
[55459]1122 }
1123
[94914]1124 if (RT_FAILURE(vrc))
1125 return vrc;
[85371]1126
[57500]1127 bool fSendData = false;
[94914]1128 if (RT_SUCCESS(vrc)) /** @todo r=aeichner Could save an identation level here as there is a error check above already... */
[55422]1129 {
[85744]1130 if (m_pState->m_uProtocolVersion >= 2)
[55422]1131 {
[85402]1132 if (!(pCtx->Transfer.fObjState & DND_OBJ_STATE_HAS_HDR))
[55422]1133 {
[85371]1134 const size_t cchDstPath = RTStrNLen(pcszDstPath, RTPATH_MAX);
1135 const size_t cbSize = DnDTransferObjectGetSize(pObj);
1136 const RTFMODE fMode = DnDTransferObjectGetMode(pObj);
1137
[55422]1138 /*
1139 * Since protocol v2 the file header and the actual file contents are
1140 * separate messages, so send the file header first.
1141 * The just registered callback will be called by the guest afterwards.
1142 */
[85745]1143 pMsg->setType(HOST_DND_FN_HG_SND_FILE_HDR);
[85554]1144 pMsg->appendUInt32(0); /** @todo ContextID not used yet. */
1145 pMsg->appendString(pcszDstPath); /* pvName */
1146 pMsg->appendUInt32((uint32_t)(cchDstPath + 1)); /* cbName */
1147 pMsg->appendUInt32(0); /* uFlags */
1148 pMsg->appendUInt32(fMode); /* fMode */
1149 pMsg->appendUInt64(cbSize); /* uSize */
[55422]1150
[85423]1151 LogRel2(("DnD: Transferring host file '%s' to guest (as '%s', %zu bytes, mode %#x)\n",
1152 pcszSrcPath, pcszDstPath, cbSize, fMode));
[55556]1153
1154 /** @todo Set progress object title to current file being transferred? */
[57500]1155
[85371]1156 /* Update object state to reflect that we have sent the file header. */
[85402]1157 pCtx->Transfer.fObjState |= DND_OBJ_STATE_HAS_HDR;
[55422]1158 }
[55459]1159 else
1160 {
1161 /* File header was sent, so only send the actual file data. */
[57500]1162 fSendData = true;
[55459]1163 }
[55422]1164 }
[55459]1165 else /* Protocol v1. */
[55422]1166 {
[55459]1167 /* Always send the file data, every time. */
[57500]1168 fSendData = true;
[55422]1169 }
1170 }
1171
[94914]1172 if ( RT_SUCCESS(vrc)
[57500]1173 && fSendData)
[55422]1174 {
[94914]1175 vrc = i_sendFileData(pCtx, pObj, pMsg);
[55422]1176 }
1177
[94914]1178 if (RT_FAILURE(vrc))
[98278]1179 LogRel(("DnD: Sending host file '%s' to guest failed, vrc=%Rrc\n", pcszSrcPath, vrc));
[74714]1180
[94914]1181 LogFlowFuncLeaveRC(vrc);
1182 return vrc;
[55422]1183}
1184
[85681]1185/**
1186 * Helper function to send actual file data to the guest.
1187 *
1188 * @returns VBox status code.
1189 * @param pCtx Send context to use.
1190 * @param pObj Transfer object to send. Must be a file.
1191 * @param pMsg Where to store the message to send.
1192 */
[85371]1193int GuestDnDTarget::i_sendFileData(GuestDnDSendCtx *pCtx,
1194 PDNDTRANSFEROBJECT pObj, GuestDnDMsg *pMsg)
[55422]1195{
[85371]1196 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
1197 AssertPtrReturn(pObj, VERR_INVALID_POINTER);
1198 AssertPtrReturn(pMsg, VERR_INVALID_POINTER);
[55422]1199
[85744]1200 AssertPtrReturn(pCtx->pState, VERR_WRONG_ORDER);
[57500]1201
[55422]1202 /** @todo Don't allow concurrent reads per context! */
1203
1204 /* Set the message type. */
[85745]1205 pMsg->setType(HOST_DND_FN_HG_SND_FILE_DATA);
[55422]1206
[85371]1207 const char *pcszSrcPath = DnDTransferObjectGetSourcePath(pObj);
1208 const char *pcszDstPath = DnDTransferObjectGetDestPath(pObj);
1209
[55422]1210 /* Protocol version 1 sends the file path *every* time with a new file chunk.
[85745]1211 * In protocol version 2 we only do this once with HOST_DND_FN_HG_SND_FILE_HDR. */
[85744]1212 if (m_pState->m_uProtocolVersion <= 1)
[55422]1213 {
[85371]1214 const size_t cchDstPath = RTStrNLen(pcszDstPath, RTPATH_MAX);
1215
[85554]1216 pMsg->appendString(pcszDstPath); /* pvName */
1217 pMsg->appendUInt32((uint32_t)cchDstPath + 1); /* cbName */
[55422]1218 }
[85744]1219 else if (m_pState->m_uProtocolVersion >= 2)
[55459]1220 {
[85554]1221 pMsg->appendUInt32(0); /** @todo ContextID not used yet. */
[55459]1222 }
[55422]1223
[85402]1224 void *pvBuf = pCtx->Transfer.pvScratchBuf;
[85371]1225 AssertPtr(pvBuf);
[85402]1226 size_t cbBuf = pCtx->Transfer.cbScratchBuf;
[85371]1227 Assert(cbBuf);
[55422]1228
[85371]1229 uint32_t cbRead;
1230
[94914]1231 int vrc = DnDTransferObjectRead(pObj, pvBuf, cbBuf, &cbRead);
1232 if (RT_SUCCESS(vrc))
[55422]1233 {
[85371]1234 LogFlowFunc(("cbBufe=%zu, cbRead=%RU32\n", cbBuf, cbRead));
1235
[85744]1236 if (m_pState->m_uProtocolVersion <= 1)
[55422]1237 {
[85554]1238 pMsg->appendPointer(pvBuf, cbRead); /* pvData */
1239 pMsg->appendUInt32(cbRead); /* cbData */
1240 pMsg->appendUInt32(DnDTransferObjectGetMode(pObj)); /* fMode */
[55422]1241 }
[58212]1242 else /* Protocol v2 and up. */
[55422]1243 {
[85554]1244 pMsg->appendPointer(pvBuf, cbRead); /* pvData */
1245 pMsg->appendUInt32(cbRead); /* cbData */
[58212]1246
[85744]1247 if (m_pState->m_uProtocolVersion >= 3)
[58212]1248 {
1249 /** @todo Calculate checksum. */
[85554]1250 pMsg->appendPointer(NULL, 0); /* pvChecksum */
1251 pMsg->appendUInt32(0); /* cbChecksum */
[58212]1252 }
[55422]1253 }
1254
[94914]1255 int vrc2 = updateProgress(pCtx, pCtx->pState, (uint32_t)cbRead);
1256 AssertRC(vrc2);
[85423]1257
1258 /* DnDTransferObjectRead() will return VINF_EOF if reading is complete. */
[94914]1259 if (vrc == VINF_EOF)
1260 vrc = VINF_SUCCESS;
[85423]1261
[85371]1262 if (DnDTransferObjectIsComplete(pObj)) /* Done reading? */
1263 LogRel2(("DnD: Transferring host file '%s' to guest complete\n", pcszSrcPath));
[55422]1264 }
[85371]1265 else
[94914]1266 LogRel(("DnD: Reading from host file '%s' failed, vrc=%Rrc\n", pcszSrcPath, vrc));
[55422]1267
[94914]1268 LogFlowFuncLeaveRC(vrc);
1269 return vrc;
[55422]1270}
1271
[85681]1272/**
1273 * Static HGCM service callback which handles sending transfer data to the guest.
1274 *
1275 * @returns VBox status code. Will get sent back to the host service.
1276 * @param uMsg HGCM message ID (function number).
1277 * @param pvParms Pointer to additional message data. Optional and can be NULL.
1278 * @param cbParms Size (in bytes) additional message data. Optional and can be 0.
1279 * @param pvUser User-supplied pointer on callback registration.
1280 */
[55422]1281/* static */
[85402]1282DECLCALLBACK(int) GuestDnDTarget::i_sendTransferDataCallback(uint32_t uMsg, void *pvParms, size_t cbParms, void *pvUser)
[55422]1283{
[85018]1284 GuestDnDSendCtx *pCtx = (GuestDnDSendCtx *)pvUser;
[55422]1285 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
1286
[85402]1287 GuestDnDTarget *pThis = pCtx->pTarget;
[55422]1288 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1289
[85423]1290 /* At the moment we only have one transfer list per transfer. */
1291 PDNDTRANSFERLIST pList = &pCtx->Transfer.List;
[55422]1292
[85423]1293 LogFlowFunc(("pThis=%p, pList=%p, uMsg=%RU32\n", pThis, pList, uMsg));
1294
[94914]1295 int vrc = VINF_SUCCESS;
[97751]1296 int vrcGuest = VINF_SUCCESS; /* Contains error code from guest in case of VERR_DND_GUEST_ERROR. */
[94914]1297 bool fNotify = false;
[55963]1298
[55422]1299 switch (uMsg)
1300 {
[85745]1301 case GUEST_DND_FN_CONNECT:
[58329]1302 /* Nothing to do here (yet). */
1303 break;
1304
[85745]1305 case GUEST_DND_FN_DISCONNECT:
[94914]1306 vrc = VERR_CANCELLED;
[58329]1307 break;
1308
[85745]1309 case GUEST_DND_FN_GET_NEXT_HOST_MSG:
[55422]1310 {
[58212]1311 PVBOXDNDCBHGGETNEXTHOSTMSG pCBData = reinterpret_cast<PVBOXDNDCBHGGETNEXTHOSTMSG>(pvParms);
[55422]1312 AssertPtr(pCBData);
[58212]1313 AssertReturn(sizeof(VBOXDNDCBHGGETNEXTHOSTMSG) == cbParms, VERR_INVALID_PARAMETER);
[58257]1314 AssertReturn(CB_MAGIC_DND_HG_GET_NEXT_HOST_MSG == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
[55422]1315
1316 try
1317 {
[55640]1318 GuestDnDMsg *pMsg = new GuestDnDMsg();
[55571]1319
[94914]1320 vrc = pThis->i_sendTransferListObject(pCtx, pList, pMsg);
1321 if (vrc == VINF_EOF) /* Transfer complete? */
[55422]1322 {
[85402]1323 LogFlowFunc(("Last transfer item processed, bailing out\n"));
[57500]1324 }
[94914]1325 else if (RT_SUCCESS(vrc))
[57500]1326 {
[94914]1327 vrc = pThis->msgQueueAdd(pMsg);
1328 if (RT_SUCCESS(vrc)) /* Return message type & required parameter count to the guest. */
[55422]1329 {
[85745]1330 LogFlowFunc(("GUEST_DND_FN_GET_NEXT_HOST_MSG -> %RU32 (%RU32 params)\n", pMsg->getType(), pMsg->getCount()));
[55422]1331 pCBData->uMsg = pMsg->getType();
1332 pCBData->cParms = pMsg->getCount();
1333 }
1334 }
1335
[94914]1336 if ( RT_FAILURE(vrc)
1337 || vrc == VINF_EOF) /* Transfer complete? */
[57500]1338 {
[55422]1339 delete pMsg;
[57500]1340 pMsg = NULL;
1341 }
[55422]1342 }
1343 catch(std::bad_alloc & /*e*/)
1344 {
[94914]1345 vrc = VERR_NO_MEMORY;
[55422]1346 }
1347 break;
1348 }
[97749]1349 case GUEST_DND_FN_EVT_ERROR:
[55549]1350 {
[58212]1351 PVBOXDNDCBEVTERRORDATA pCBData = reinterpret_cast<PVBOXDNDCBEVTERRORDATA>(pvParms);
[55549]1352 AssertPtr(pCBData);
[58212]1353 AssertReturn(sizeof(VBOXDNDCBEVTERRORDATA) == cbParms, VERR_INVALID_PARAMETER);
[97749]1354 AssertReturn(CB_MAGIC_DND_EVT_ERROR == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
[55549]1355
[85744]1356 pCtx->pState->reset();
[55963]1357
1358 if (RT_SUCCESS(pCBData->rc))
[57500]1359 {
1360 AssertMsgFailed(("Guest has sent an error event but did not specify an actual error code\n"));
[55963]1361 pCBData->rc = VERR_GENERAL_FAILURE; /* Make sure some error is set. */
[57500]1362 }
[55963]1363
[94914]1364 vrc = pCtx->pState->setProgress(100, DND_PROGRESS_ERROR, pCBData->rc,
1365 GuestDnDTarget::i_guestErrorToString(pCBData->rc));
1366 if (RT_SUCCESS(vrc))
[57500]1367 {
[97751]1368 vrc = VERR_DND_GUEST_ERROR;
[94914]1369 vrcGuest = pCBData->rc;
[57500]1370 }
[55549]1371 break;
1372 }
[85745]1373 case HOST_DND_FN_HG_SND_DIR:
1374 case HOST_DND_FN_HG_SND_FILE_HDR:
1375 case HOST_DND_FN_HG_SND_FILE_DATA:
[55422]1376 {
[58212]1377 PVBOXDNDCBHGGETNEXTHOSTMSGDATA pCBData
1378 = reinterpret_cast<PVBOXDNDCBHGGETNEXTHOSTMSGDATA>(pvParms);
[55422]1379 AssertPtr(pCBData);
[58212]1380 AssertReturn(sizeof(VBOXDNDCBHGGETNEXTHOSTMSGDATA) == cbParms, VERR_INVALID_PARAMETER);
[55422]1381
[55459]1382 LogFlowFunc(("pCBData->uMsg=%RU32, paParms=%p, cParms=%RU32\n", pCBData->uMsg, pCBData->paParms, pCBData->cParms));
1383
[55571]1384 GuestDnDMsg *pMsg = pThis->msgQueueGetNext();
[55422]1385 if (pMsg)
1386 {
1387 /*
1388 * Sanity checks.
1389 */
1390 if ( pCBData->uMsg != uMsg
1391 || pCBData->paParms == NULL
1392 || pCBData->cParms != pMsg->getCount())
1393 {
[58212]1394 LogFlowFunc(("Current message does not match:\n"));
1395 LogFlowFunc(("\tCallback: uMsg=%RU32, cParms=%RU32, paParms=%p\n",
1396 pCBData->uMsg, pCBData->cParms, pCBData->paParms));
1397 LogFlowFunc(("\t Next: uMsg=%RU32, cParms=%RU32\n", pMsg->getType(), pMsg->getCount()));
1398
[55571]1399 /* Start over. */
1400 pThis->msgQueueClear();
1401
[94914]1402 vrc = VERR_INVALID_PARAMETER;
[55422]1403 }
1404
[94914]1405 if (RT_SUCCESS(vrc))
[55422]1406 {
[55459]1407 LogFlowFunc(("Returning uMsg=%RU32\n", uMsg));
[94914]1408 vrc = HGCM::Message::CopyParms(pCBData->paParms, pCBData->cParms, pMsg->getParms(), pMsg->getCount(),
1409 false /* fDeepCopy */);
1410 if (RT_SUCCESS(vrc))
[55422]1411 {
1412 pCBData->cParms = pMsg->getCount();
[55571]1413 pThis->msgQueueRemoveNext();
[55422]1414 }
[55459]1415 else
[94914]1416 LogFlowFunc(("Copying parameters failed with vrc=%Rrc\n", vrc));
[55422]1417 }
1418 }
1419 else
[94914]1420 vrc = VERR_NO_DATA;
[55422]1421
[94914]1422 LogFlowFunc(("Processing next message ended with vrc=%Rrc\n", vrc));
[55422]1423 break;
1424 }
1425 default:
[94914]1426 vrc = VERR_NOT_SUPPORTED;
[55422]1427 break;
1428 }
1429
[94914]1430 int vrcToGuest = VINF_SUCCESS; /* Status which will be sent back to the guest. */
[57500]1431
1432 /*
1433 * Resolve errors.
1434 */
[94914]1435 switch (vrc)
[55963]1436 {
[57500]1437 case VINF_SUCCESS:
1438 break;
[55963]1439
[57500]1440 case VINF_EOF:
[55549]1441 {
[57500]1442 LogRel2(("DnD: Transfer to guest complete\n"));
[55549]1443
[57500]1444 /* Complete operation on host side. */
1445 fNotify = true;
[55549]1446
[57500]1447 /* The guest expects VERR_NO_DATA if the transfer is complete. */
[94914]1448 vrcToGuest = VERR_NO_DATA;
[57500]1449 break;
[55549]1450 }
1451
[97751]1452 case VERR_DND_GUEST_ERROR:
[57500]1453 {
[94914]1454 LogRel(("DnD: Guest reported error %Rrc, aborting transfer to guest\n", vrcGuest));
[57500]1455 break;
1456 }
1457
1458 case VERR_CANCELLED:
1459 {
1460 LogRel2(("DnD: Transfer to guest canceled\n"));
[94914]1461 vrcToGuest = VERR_CANCELLED; /* Also cancel on guest side. */
[57500]1462 break;
1463 }
1464
1465 default:
1466 {
[94914]1467 LogRel(("DnD: Host error %Rrc occurred, aborting transfer to guest\n", vrc));
1468 vrcToGuest = VERR_CANCELLED; /* Also cancel on guest side. */
[57500]1469 break;
1470 }
1471 }
1472
[94914]1473 if (RT_FAILURE(vrc))
[57500]1474 {
[55549]1475 /* Unregister this callback. */
[85744]1476 AssertPtr(pCtx->pState);
[94914]1477 int vrc2 = pCtx->pState->setCallback(uMsg, NULL /* PFNGUESTDNDCALLBACK */);
1478 AssertRC(vrc2);
[57500]1479
1480 /* Let the waiter(s) know. */
1481 fNotify = true;
[55963]1482 }
[55549]1483
[94914]1484 LogFlowFunc(("fNotify=%RTbool, vrc=%Rrc, vrcToGuest=%Rrc\n", fNotify, vrc, vrcToGuest));
[55963]1485
1486 if (fNotify)
1487 {
[94914]1488 int vrc2 = pCtx->EventCallback.Notify(vrc); /** @todo Also pass guest error back? */
1489 AssertRC(vrc2);
[55422]1490 }
1491
[94914]1492 LogFlowFuncLeaveRC(vrc);
1493 return vrcToGuest; /* Tell the guest. */
[55422]1494}
1495
[85371]1496/**
1497 * Main function for sending the actual transfer data (i.e. files + directories) to the guest.
1498 *
1499 * @returns VBox status code.
1500 * @param pCtx Send context to use.
1501 * @param msTimeout Timeout (in ms) to use for getting the data sent.
1502 */
1503int GuestDnDTarget::i_sendTransferData(GuestDnDSendCtx *pCtx, RTMSINTERVAL msTimeout)
[55422]1504{
1505 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
[85744]1506 AssertPtr(pCtx->pState);
[55422]1507
[94914]1508#define REGISTER_CALLBACK(x) \
1509 do { \
1510 vrc = pCtx->pState->setCallback(x, i_sendTransferDataCallback, pCtx); \
1511 if (RT_FAILURE(vrc)) \
1512 return vrc; \
[60051]1513 } while (0)
[55422]1514
[55571]1515#define UNREGISTER_CALLBACK(x) \
[60051]1516 do { \
[94914]1517 int vrc2 = pCtx->pState->setCallback(x, NULL); \
1518 AssertRC(vrc2); \
[60051]1519 } while (0)
[55571]1520
[94914]1521 int vrc = pCtx->Transfer.init(mData.mcbBlockSize);
1522 if (RT_FAILURE(vrc))
1523 return vrc;
[57469]1524
[94914]1525 vrc = pCtx->EventCallback.Reset();
1526 if (RT_FAILURE(vrc))
1527 return vrc;
[55512]1528
[58329]1529 /*
1530 * Register callbacks.
1531 */
1532 /* Guest callbacks. */
[85745]1533 REGISTER_CALLBACK(GUEST_DND_FN_CONNECT);
1534 REGISTER_CALLBACK(GUEST_DND_FN_DISCONNECT);
1535 REGISTER_CALLBACK(GUEST_DND_FN_GET_NEXT_HOST_MSG);
[97749]1536 REGISTER_CALLBACK(GUEST_DND_FN_EVT_ERROR);
[58329]1537 /* Host callbacks. */
[85745]1538 REGISTER_CALLBACK(HOST_DND_FN_HG_SND_DIR);
[85744]1539 if (m_pState->m_uProtocolVersion >= 2)
[85745]1540 REGISTER_CALLBACK(HOST_DND_FN_HG_SND_FILE_HDR);
1541 REGISTER_CALLBACK(HOST_DND_FN_HG_SND_FILE_DATA);
[58329]1542
[55422]1543 do
1544 {
1545 /*
[85371]1546 * Extract transfer list from current meta data.
[55422]1547 */
[94914]1548 vrc = DnDTransferListAppendPathsFromBuffer(&pCtx->Transfer.List, DNDTRANSFERLISTFMT_URI,
1549 (const char *)pCtx->Meta.pvData, pCtx->Meta.cbData, DND_PATH_SEPARATOR_STR,
1550 DNDTRANSFERLIST_FLAGS_RECURSIVE);
1551 if (RT_FAILURE(vrc))
[58212]1552 break;
[55422]1553
[58212]1554 /*
[85402]1555 * Update internal state to reflect everything we need to work with it.
[58212]1556 */
[85537]1557 pCtx->cbExtra = DnDTransferListObjTotalBytes(&pCtx->Transfer.List);
[85423]1558 /* cbExtra can be 0, if all files are of 0 bytes size. */
1559 pCtx->Transfer.cObjToProcess = DnDTransferListObjCount(&pCtx->Transfer.List);
[94914]1560 AssertBreakStmt(pCtx->Transfer.cObjToProcess, vrc = VERR_INVALID_PARAMETER);
[55422]1561
[85423]1562 /* Update the meta data to have the current root transfer entries in the right shape. */
1563 if (DnDMIMEHasFileURLs(pCtx->Meta.strFmt.c_str(), RTSTR_MAX))
1564 {
1565 /* Save original format we're still going to use after updating the actual meta data. */
1566 Utf8Str strFmt = pCtx->Meta.strFmt;
1567
1568 /* Reset stale data. */
1569 pCtx->Meta.reset();
1570
1571 void *pvData;
1572 size_t cbData;
1573#ifdef DEBUG
[94914]1574 vrc = DnDTransferListGetRootsEx(&pCtx->Transfer.List, DNDTRANSFERLISTFMT_URI, "" /* pcszPathBase */,
1575 "\n" /* pcszSeparator */, (char **)&pvData, &cbData);
1576 AssertRCReturn(vrc, vrc);
[85423]1577 LogFlowFunc(("URI data:\n%s", (char *)pvData));
1578 RTMemFree(pvData);
1579 cbData = 0;
1580#endif
[94914]1581 vrc = DnDTransferListGetRoots(&pCtx->Transfer.List, DNDTRANSFERLISTFMT_URI,
1582 (char **)&pvData, &cbData);
1583 AssertRCReturn(vrc, vrc);
[85423]1584
1585 /* pCtx->Meta now owns the allocated data. */
1586 pCtx->Meta.strFmt = strFmt;
1587 pCtx->Meta.pvData = pvData;
1588 pCtx->Meta.cbData = cbData;
1589 pCtx->Meta.cbAllocated = cbData;
[85537]1590 pCtx->Meta.cbAnnounced = cbData;
[85423]1591 }
1592
[58212]1593 /*
1594 * The first message always is the data header. The meta data itself then follows
[85371]1595 * and *only* contains the root elements of a transfer list.
[55422]1596 *
[58212]1597 * After the meta data we generate the messages required to send the
1598 * file/directory data itself.
1599 *
1600 * Note: Protocol < v3 use the first data message to tell what's being sent.
[55422]1601 */
1602
[58212]1603 /*
1604 * Send the data header first.
1605 */
[85744]1606 if (m_pState->m_uProtocolVersion >= 3)
[94914]1607 vrc = i_sendMetaDataHeader(pCtx);
[55422]1608
[58212]1609 /*
1610 * Send the (meta) data body.
1611 */
[94914]1612 if (RT_SUCCESS(vrc))
1613 vrc = i_sendMetaDataBody(pCtx);
[58212]1614
[94914]1615 if (RT_SUCCESS(vrc))
[55963]1616 {
[94914]1617 vrc = waitForEvent(&pCtx->EventCallback, pCtx->pState, msTimeout);
1618 if (RT_SUCCESS(vrc))
[85744]1619 pCtx->pState->setProgress(100, DND_PROGRESS_COMPLETE, VINF_SUCCESS);
[55963]1620 }
[55422]1621
1622 } while (0);
1623
[58329]1624 /*
1625 * Unregister callbacks.
1626 */
1627 /* Guest callbacks. */
[85745]1628 UNREGISTER_CALLBACK(GUEST_DND_FN_CONNECT);
1629 UNREGISTER_CALLBACK(GUEST_DND_FN_DISCONNECT);
1630 UNREGISTER_CALLBACK(GUEST_DND_FN_GET_NEXT_HOST_MSG);
[97749]1631 UNREGISTER_CALLBACK(GUEST_DND_FN_EVT_ERROR);
[58329]1632 /* Host callbacks. */
[85745]1633 UNREGISTER_CALLBACK(HOST_DND_FN_HG_SND_DIR);
[85744]1634 if (m_pState->m_uProtocolVersion >= 2)
[85745]1635 UNREGISTER_CALLBACK(HOST_DND_FN_HG_SND_FILE_HDR);
1636 UNREGISTER_CALLBACK(HOST_DND_FN_HG_SND_FILE_DATA);
[58329]1637
[55422]1638#undef REGISTER_CALLBACK
1639#undef UNREGISTER_CALLBACK
1640
[94914]1641 if (RT_FAILURE(vrc))
[58329]1642 {
[94914]1643 if (vrc == VERR_CANCELLED) /* Transfer was cancelled by the host. */
[74495]1644 {
1645 /*
1646 * Now that we've cleaned up tell the guest side to cancel.
1647 * This does not imply we're waiting for the guest to react, as the
1648 * host side never must depend on anything from the guest.
1649 */
[94914]1650 int vrc2 = sendCancel();
1651 AssertRC(vrc2);
[74495]1652
[85371]1653 LogRel2(("DnD: Sending transfer data to guest cancelled by user\n"));
1654
[94914]1655 vrc2 = pCtx->pState->setProgress(100, DND_PROGRESS_CANCELLED, VINF_SUCCESS);
1656 AssertRC(vrc2);
[85564]1657
1658 /* Cancelling is not an error, just set success here. */
[94914]1659 vrc = VINF_SUCCESS;
[74495]1660 }
[97751]1661 else if (vrc != VERR_DND_GUEST_ERROR) /* Guest-side error are already handled in the callback. */
[74495]1662 {
[94914]1663 LogRel(("DnD: Sending transfer data to guest failed with vrc=%Rrc\n", vrc));
1664 int vrc2 = pCtx->pState->setProgress(100, DND_PROGRESS_ERROR, vrc,
1665 GuestDnDTarget::i_hostErrorToString(vrc));
1666 AssertRC(vrc2);
[74495]1667 }
[55571]1668 }
1669
[94914]1670 LogFlowFuncLeaveRC(vrc);
1671 return vrc;
[55422]1672}
1673
[85423]1674/**
1675 * Sends the next object of a transfer list to the guest.
1676 *
1677 * @returns VBox status code. VINF_EOF if the transfer list is complete.
1678 * @param pCtx Send context to use.
1679 * @param pList Transfer list to use.
1680 * @param pMsg Message to store send data into.
1681 */
1682int GuestDnDTarget::i_sendTransferListObject(GuestDnDSendCtx *pCtx, PDNDTRANSFERLIST pList, GuestDnDMsg *pMsg)
[55422]1683{
[55459]1684 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
[85423]1685 AssertPtrReturn(pList, VERR_INVALID_POINTER);
[58212]1686 AssertPtrReturn(pMsg, VERR_INVALID_POINTER);
[55422]1687
[94914]1688 int vrc = updateProgress(pCtx, pCtx->pState);
1689 AssertRCReturn(vrc, vrc);
[55422]1690
[85537]1691 PDNDTRANSFEROBJECT pObj = DnDTransferListObjGetFirst(pList);
1692 if (!pObj) /* Transfer complete? */
[58212]1693 return VINF_EOF;
[55422]1694
[85371]1695 switch (DnDTransferObjectGetType(pObj))
1696 {
1697 case DNDTRANSFEROBJTYPE_DIRECTORY:
[94914]1698 vrc = i_sendDirectory(pCtx, pObj, pMsg);
[85371]1699 break;
[55422]1700
[85371]1701 case DNDTRANSFEROBJTYPE_FILE:
[94914]1702 vrc = i_sendFile(pCtx, pObj, pMsg);
[85371]1703 break;
[84998]1704
[85371]1705 default:
[94914]1706 AssertFailedStmt(vrc = VERR_NOT_SUPPORTED);
[85371]1707 break;
[55422]1708 }
1709
[94914]1710 if ( RT_SUCCESS(vrc)
[85423]1711 && DnDTransferObjectIsComplete(pObj))
1712 {
1713 DnDTransferListObjRemove(pList, pObj);
1714 pObj = NULL;
[55422]1715
[85423]1716 AssertReturn(pCtx->Transfer.cObjProcessed + 1 <= pCtx->Transfer.cObjToProcess, VERR_WRONG_ORDER);
1717 pCtx->Transfer.cObjProcessed++;
1718
1719 pCtx->Transfer.fObjState = DND_OBJ_STATE_NONE;
1720 }
1721
[94914]1722 LogFlowFuncLeaveRC(vrc);
1723 return vrc;
[55571]1724}
1725
[85371]1726/**
1727 * Main function for sending raw data (e.g. text, RTF, ...) to the guest.
1728 *
1729 * @returns VBox status code.
1730 * @param pCtx Send context to use.
1731 * @param msTimeout Timeout (in ms) to use for getting the data sent.
1732 */
[85018]1733int GuestDnDTarget::i_sendRawData(GuestDnDSendCtx *pCtx, RTMSINTERVAL msTimeout)
[55571]1734{
1735 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
1736 NOREF(msTimeout);
1737
[58212]1738 /** @todo At the moment we only allow sending up to 64K raw data.
[85745]1739 * For protocol v1+v2: Fix this by using HOST_DND_FN_HG_SND_MORE_DATA.
1740 * For protocol v3 : Send another HOST_DND_FN_HG_SND_DATA message. */
[85371]1741 if (!pCtx->Meta.cbData)
[58212]1742 return VINF_SUCCESS;
[55571]1743
[94914]1744 int vrc = i_sendMetaDataHeader(pCtx);
1745 if (RT_SUCCESS(vrc))
1746 vrc = i_sendMetaDataBody(pCtx);
[58212]1747
[94914]1748 int vrc2;
1749 if (RT_FAILURE(vrc))
[85371]1750 {
[94914]1751 LogRel(("DnD: Sending raw data to guest failed with vrc=%Rrc\n", vrc));
1752 vrc2 = pCtx->pState->setProgress(100 /* Percent */, DND_PROGRESS_ERROR, vrc,
1753 GuestDnDTarget::i_hostErrorToString(vrc));
[85371]1754 }
[56657]1755 else
[94914]1756 vrc2 = pCtx->pState->setProgress(100 /* Percent */, DND_PROGRESS_COMPLETE, vrc);
1757 AssertRC(vrc2);
[56657]1758
[94914]1759 LogFlowFuncLeaveRC(vrc);
1760 return vrc;
[55422]1761}
1762
[85371]1763/**
1764 * Cancels sending DnD data.
1765 *
1766 * @returns VBox status code.
1767 * @param aVeto Whether cancelling was vetoed or not.
1768 * Not implemented yet.
1769 */
[55422]1770HRESULT GuestDnDTarget::cancel(BOOL *aVeto)
1771{
1772#if !defined(VBOX_WITH_DRAG_AND_DROP)
1773 ReturnComNotImplemented();
1774#else /* VBOX_WITH_DRAG_AND_DROP */
1775
[85371]1776 LogRel2(("DnD: Sending cancelling request to the guest ...\n"));
1777
[94914]1778 int vrc = GuestDnDBase::sendCancel();
[55422]1779
1780 if (aVeto)
[85402]1781 *aVeto = FALSE; /** @todo Implement vetoing. */
[55422]1782
[97719]1783 HRESULT hrc = RT_SUCCESS(vrc) ? S_OK : VBOX_E_DND_ERROR;
[56656]1784
[94914]1785 LogFlowFunc(("hrc=%Rhrc\n", hrc));
1786 return hrc;
[55422]1787#endif /* VBOX_WITH_DRAG_AND_DROP */
1788}
1789
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