VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/GuestDnDSourceImpl.cpp@ 59851

Last change on this file since 59851 was 59851, checked in by vboxsync, 9 years ago

DnD/GuestDnDSourceImpl.cpp: Rollback fixes + logging.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 52.5 KB
Line 
1/* $Id: GuestDnDSourceImpl.cpp 59851 2016-02-26 15:35:35Z vboxsync $ */
2/** @file
3 * VBox Console COM Class implementation - Guest drag and drop source.
4 */
5
6/*
7 * Copyright (C) 2014-2016 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include "GuestImpl.h"
23#include "GuestDnDSourceImpl.h"
24#include "GuestDnDPrivate.h"
25#include "ConsoleImpl.h"
26
27#include "Global.h"
28#include "AutoCaller.h"
29#include "ThreadTask.h"
30
31#include <iprt/asm.h>
32#include <iprt/dir.h>
33#include <iprt/file.h>
34#include <iprt/path.h>
35#include <iprt/uri.h>
36
37#include <iprt/cpp/utils.h> /* For unconst(). */
38
39#include <VBox/com/array.h>
40
41#ifdef LOG_GROUP
42 #undef LOG_GROUP
43#endif
44#define LOG_GROUP LOG_GROUP_GUEST_DND
45#include <VBox/log.h>
46
47/**
48 * Base class for a source task.
49 */
50class GuestDnDSourceTask : public ThreadTask
51{
52public:
53
54 GuestDnDSourceTask(GuestDnDSource *pSource)
55 : ThreadTask("GenericGuestDnDSourceTask")
56 , mSource(pSource)
57 , mRC(VINF_SUCCESS) { }
58
59 virtual ~GuestDnDSourceTask(void) { }
60
61 int getRC(void) const { return mRC; }
62 bool isOk(void) const { return RT_SUCCESS(mRC); }
63 const ComObjPtr<GuestDnDSource> &getSource(void) const { return mSource; }
64
65protected:
66
67 const ComObjPtr<GuestDnDSource> mSource;
68 int mRC;
69};
70
71/**
72 * Task structure for receiving data from a source using
73 * a worker thread.
74 */
75class RecvDataTask : public GuestDnDSourceTask
76{
77public:
78
79 RecvDataTask(GuestDnDSource *pSource, PRECVDATACTX pCtx)
80 : GuestDnDSourceTask(pSource)
81 , mpCtx(pCtx)
82 {
83 m_strTaskName = "dndSrcRcvData";
84 }
85
86 void handler()
87 {
88 int vrc = GuestDnDSource::i_receiveDataThread(*m_pThread, this);
89 }
90
91 virtual ~RecvDataTask(void) { }
92
93 PRECVDATACTX getCtx(void) { return mpCtx; }
94
95protected:
96
97 /** Pointer to receive data context. */
98 PRECVDATACTX mpCtx;
99};
100
101// constructor / destructor
102/////////////////////////////////////////////////////////////////////////////
103
104DEFINE_EMPTY_CTOR_DTOR(GuestDnDSource)
105
106HRESULT GuestDnDSource::FinalConstruct(void)
107{
108 /*
109 * Set the maximum block size this source can handle to 64K. This always has
110 * been hardcoded until now.
111 *
112 * Note: Never ever rely on information from the guest; the host dictates what and
113 * how to do something, so try to negogiate a sensible value here later.
114 */
115 mData.mcbBlockSize = _64K; /** @todo Make this configurable. */
116
117 LogFlowThisFunc(("\n"));
118 return BaseFinalConstruct();
119}
120
121void GuestDnDSource::FinalRelease(void)
122{
123 LogFlowThisFuncEnter();
124 uninit();
125 BaseFinalRelease();
126 LogFlowThisFuncLeave();
127}
128
129// public initializer/uninitializer for internal purposes only
130/////////////////////////////////////////////////////////////////////////////
131
132int GuestDnDSource::init(const ComObjPtr<Guest>& pGuest)
133{
134 LogFlowThisFuncEnter();
135
136 /* Enclose the state transition NotReady->InInit->Ready. */
137 AutoInitSpan autoInitSpan(this);
138 AssertReturn(autoInitSpan.isOk(), E_FAIL);
139
140 unconst(m_pGuest) = pGuest;
141
142 /* Confirm a successful initialization when it's the case. */
143 autoInitSpan.setSucceeded();
144
145 return VINF_SUCCESS;
146}
147
148/**
149 * Uninitializes the instance.
150 * Called from FinalRelease().
151 */
152void GuestDnDSource::uninit(void)
153{
154 LogFlowThisFunc(("\n"));
155
156 /* Enclose the state transition Ready->InUninit->NotReady. */
157 AutoUninitSpan autoUninitSpan(this);
158 if (autoUninitSpan.uninitDone())
159 return;
160}
161
162// implementation of wrapped IDnDBase methods.
163/////////////////////////////////////////////////////////////////////////////
164
165HRESULT GuestDnDSource::isFormatSupported(const com::Utf8Str &aFormat, BOOL *aSupported)
166{
167#if !defined(VBOX_WITH_DRAG_AND_DROP) || !defined(VBOX_WITH_DRAG_AND_DROP_GH)
168 ReturnComNotImplemented();
169#else /* VBOX_WITH_DRAG_AND_DROP */
170
171 AutoCaller autoCaller(this);
172 if (FAILED(autoCaller.rc())) return autoCaller.rc();
173
174 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
175
176 return GuestDnDBase::i_isFormatSupported(aFormat, aSupported);
177#endif /* VBOX_WITH_DRAG_AND_DROP */
178}
179
180HRESULT GuestDnDSource::getFormats(GuestDnDMIMEList &aFormats)
181{
182#if !defined(VBOX_WITH_DRAG_AND_DROP) || !defined(VBOX_WITH_DRAG_AND_DROP_GH)
183 ReturnComNotImplemented();
184#else /* VBOX_WITH_DRAG_AND_DROP */
185
186 AutoCaller autoCaller(this);
187 if (FAILED(autoCaller.rc())) return autoCaller.rc();
188
189 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
190
191 return GuestDnDBase::i_getFormats(aFormats);
192#endif /* VBOX_WITH_DRAG_AND_DROP */
193}
194
195HRESULT GuestDnDSource::addFormats(const GuestDnDMIMEList &aFormats)
196{
197#if !defined(VBOX_WITH_DRAG_AND_DROP) || !defined(VBOX_WITH_DRAG_AND_DROP_GH)
198 ReturnComNotImplemented();
199#else /* VBOX_WITH_DRAG_AND_DROP */
200
201 AutoCaller autoCaller(this);
202 if (FAILED(autoCaller.rc())) return autoCaller.rc();
203
204 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
205
206 return GuestDnDBase::i_addFormats(aFormats);
207#endif /* VBOX_WITH_DRAG_AND_DROP */
208}
209
210HRESULT GuestDnDSource::removeFormats(const GuestDnDMIMEList &aFormats)
211{
212#if !defined(VBOX_WITH_DRAG_AND_DROP) || !defined(VBOX_WITH_DRAG_AND_DROP_GH)
213 ReturnComNotImplemented();
214#else /* VBOX_WITH_DRAG_AND_DROP */
215
216 AutoCaller autoCaller(this);
217 if (FAILED(autoCaller.rc())) return autoCaller.rc();
218
219 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
220
221 return GuestDnDBase::i_removeFormats(aFormats);
222#endif /* VBOX_WITH_DRAG_AND_DROP */
223}
224
225HRESULT GuestDnDSource::getProtocolVersion(ULONG *aProtocolVersion)
226{
227#if !defined(VBOX_WITH_DRAG_AND_DROP)
228 ReturnComNotImplemented();
229#else /* VBOX_WITH_DRAG_AND_DROP */
230
231 AutoCaller autoCaller(this);
232 if (FAILED(autoCaller.rc())) return autoCaller.rc();
233
234 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
235
236 return GuestDnDBase::i_getProtocolVersion(aProtocolVersion);
237#endif /* VBOX_WITH_DRAG_AND_DROP */
238}
239
240// implementation of wrapped IDnDSource methods.
241/////////////////////////////////////////////////////////////////////////////
242
243HRESULT GuestDnDSource::dragIsPending(ULONG uScreenId, GuestDnDMIMEList &aFormats,
244 std::vector<DnDAction_T> &aAllowedActions, DnDAction_T *aDefaultAction)
245{
246#if !defined(VBOX_WITH_DRAG_AND_DROP) || !defined(VBOX_WITH_DRAG_AND_DROP_GH)
247 ReturnComNotImplemented();
248#else /* VBOX_WITH_DRAG_AND_DROP */
249
250 /* aDefaultAction is optional. */
251
252 AutoCaller autoCaller(this);
253 if (FAILED(autoCaller.rc())) return autoCaller.rc();
254
255 /* Determine guest DnD protocol to use. */
256 GuestDnDBase::getProtocolVersion(&mDataBase.m_uProtocolVersion);
257
258 /* Default is ignoring the action. */
259 if (aDefaultAction)
260 *aDefaultAction = DnDAction_Ignore;
261
262 HRESULT hr = S_OK;
263
264 GuestDnDMsg Msg;
265 Msg.setType(HOST_DND_GH_REQ_PENDING);
266 if (mDataBase.m_uProtocolVersion >= 3)
267 Msg.setNextUInt32(0); /** @todo ContextID not used yet. */
268 Msg.setNextUInt32(uScreenId);
269
270 int rc = GuestDnDInst()->hostCall(Msg.getType(), Msg.getCount(), Msg.getParms());
271 if (RT_SUCCESS(rc))
272 {
273 GuestDnDResponse *pResp = GuestDnDInst()->response();
274 AssertPtr(pResp);
275
276 bool fFetchResult = true;
277
278 rc = pResp->waitForGuestResponse(100 /* Timeout in ms */);
279 if (RT_FAILURE(rc))
280 fFetchResult = false;
281
282 if ( fFetchResult
283 && isDnDIgnoreAction(pResp->defAction()))
284 fFetchResult = false;
285
286 /* Fetch the default action to use. */
287 if (fFetchResult)
288 {
289 /*
290 * In the GuestDnDSource case the source formats are from the guest,
291 * as GuestDnDSource acts as a target for the guest. The host always
292 * dictates what's supported and what's not, so filter out all formats
293 * which are not supported by the host.
294 */
295 GuestDnDMIMEList lstFiltered = GuestDnD::toFilteredFormatList(m_lstFmtSupported, pResp->formats());
296 if (lstFiltered.size())
297 {
298 LogRel3(("DnD: Host offered the following formats:\n"));
299 for (size_t i = 0; i < lstFiltered.size(); i++)
300 LogRel3(("DnD:\tFormat #%zu: %s\n", i, lstFiltered.at(i).c_str()));
301
302 aFormats = lstFiltered;
303 aAllowedActions = GuestDnD::toMainActions(pResp->allActions());
304 if (aDefaultAction)
305 *aDefaultAction = GuestDnD::toMainAction(pResp->defAction());
306
307 /* Apply the (filtered) formats list. */
308 m_lstFmtOffered = lstFiltered;
309 }
310 else
311 LogRel2(("DnD: Negotiation of formats between guest and host failed, drag and drop to host not possible\n"));
312 }
313
314 LogFlowFunc(("fFetchResult=%RTbool, allActions=0x%x\n", fFetchResult, pResp->allActions()));
315 }
316
317 LogFlowFunc(("hr=%Rhrc\n", hr));
318 return hr;
319#endif /* VBOX_WITH_DRAG_AND_DROP */
320}
321
322HRESULT GuestDnDSource::drop(const com::Utf8Str &aFormat, DnDAction_T aAction, ComPtr<IProgress> &aProgress)
323{
324#if !defined(VBOX_WITH_DRAG_AND_DROP) || !defined(VBOX_WITH_DRAG_AND_DROP_GH)
325 ReturnComNotImplemented();
326#else /* VBOX_WITH_DRAG_AND_DROP */
327
328 AutoCaller autoCaller(this);
329 if (FAILED(autoCaller.rc())) return autoCaller.rc();
330
331 LogFunc(("aFormat=%s, aAction=%RU32\n", aFormat.c_str(), aAction));
332
333 /* Input validation. */
334 if (RT_UNLIKELY((aFormat.c_str()) == NULL || *(aFormat.c_str()) == '\0'))
335 return setError(E_INVALIDARG, tr("No drop format specified"));
336
337 /* Is the specified format in our list of (left over) offered formats? */
338 if (!GuestDnD::isFormatInFormatList(aFormat, m_lstFmtOffered))
339 return setError(E_INVALIDARG, tr("Specified format '%s' is not supported"), aFormat.c_str());
340
341 uint32_t uAction = GuestDnD::toHGCMAction(aAction);
342 if (isDnDIgnoreAction(uAction)) /* If there is no usable action, ignore this request. */
343 return S_OK;
344
345 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
346
347 /* At the moment we only support one transfer at a time. */
348 if (mDataBase.m_cTransfersPending)
349 return setError(E_INVALIDARG, tr("Another drop operation already is in progress"));
350
351 /* Dito. */
352 GuestDnDResponse *pResp = GuestDnDInst()->response();
353 AssertPtr(pResp);
354
355 HRESULT hr = pResp->resetProgress(m_pGuest);
356 if (FAILED(hr))
357 return hr;
358
359 RecvDataTask *pTask = NULL;
360 RTTHREAD threadRcv;
361 int rc = S_OK;
362
363 try
364 {
365 mData.mRecvCtx.mIsActive = false;
366 mData.mRecvCtx.mpSource = this;
367 mData.mRecvCtx.mpResp = pResp;
368 mData.mRecvCtx.mFmtReq = aFormat;
369 mData.mRecvCtx.mFmtOffered = m_lstFmtOffered;
370
371 LogRel2(("DnD: Requesting data from guest in format: %s\n", aFormat.c_str()));
372
373 pTask = new RecvDataTask(this, &mData.mRecvCtx);
374 if (!pTask->isOk())
375 {
376 delete pTask;
377 LogRel2(("DnD: Could not create RecvDataTask object \n"));
378 throw hr = E_FAIL;
379 }
380
381 /* This function delete pTask in case of exceptions,
382 * so there is no need in the call of delete operator. */
383 hr = pTask->createThread(&threadRcv);
384
385 }
386 catch(std::bad_alloc &)
387 {
388 hr = setError(E_OUTOFMEMORY);
389 }
390 catch(...)
391 {
392 LogRel2(("DnD: Could not create thread for RecvDataTask \n"));
393 hr = E_FAIL;
394 }
395
396 if (SUCCEEDED(hr))
397 {
398 rc = RTThreadUserWait(threadRcv, 30 * 1000 /* 30s timeout */);
399 if (RT_SUCCESS(rc))
400 {
401 mDataBase.m_cTransfersPending++;
402
403 hr = pResp->queryProgressTo(aProgress.asOutParam());
404 ComAssertComRC(hr);
405
406 /* Note: pTask is now owned by the worker thread. */
407 }
408 else
409 hr = setError(VBOX_E_IPRT_ERROR, tr("Waiting for receiving thread failed (%Rrc)"), rc);
410 }
411 else
412 hr = setError(VBOX_E_IPRT_ERROR, tr("Starting thread for GuestDnDSource::i_receiveDataThread failed (%Rrc)"), rc);
413 /* Note: mDataBase.mfTransferIsPending will be set to false again by i_receiveDataThread. */
414
415 LogFlowFunc(("Returning hr=%Rhrc\n", hr));
416 return hr;
417#endif /* VBOX_WITH_DRAG_AND_DROP */
418}
419
420HRESULT GuestDnDSource::receiveData(std::vector<BYTE> &aData)
421{
422#if !defined(VBOX_WITH_DRAG_AND_DROP) || !defined(VBOX_WITH_DRAG_AND_DROP_GH)
423 ReturnComNotImplemented();
424#else /* VBOX_WITH_DRAG_AND_DROP */
425
426 AutoCaller autoCaller(this);
427 if (FAILED(autoCaller.rc())) return autoCaller.rc();
428
429 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
430
431 LogFlowThisFunc(("cTransfersPending=%RU32\n", mDataBase.m_cTransfersPending));
432
433 /* Don't allow receiving the actual data until our transfer actually is complete. */
434 if (mDataBase.m_cTransfersPending)
435 return setError(E_FAIL, tr("Current drop operation still in progress"));
436
437 PRECVDATACTX pCtx = &mData.mRecvCtx;
438 HRESULT hr = S_OK;
439
440 try
441 {
442 bool fHasURIList = DnDMIMENeedsDropDir(pCtx->mFmtRecv.c_str(), pCtx->mFmtRecv.length());
443 if (fHasURIList)
444 {
445 LogRel2(("DnD: Drop directory is: %s\n", pCtx->mURI.getDroppedFiles().GetDirAbs()));
446 int rc2 = pCtx->mURI.toMetaData(aData);
447 if (RT_FAILURE(rc2))
448 hr = E_OUTOFMEMORY;
449 }
450 else
451 {
452 const size_t cbData = pCtx->mData.getMeta().getSize();
453 LogFlowFunc(("cbData=%zu\n", cbData));
454 if (cbData)
455 {
456 /* Copy the data into a safe array of bytes. */
457 aData.resize(cbData);
458 memcpy(&aData.front(), pCtx->mData.getMeta().getData(), cbData);
459 }
460 else
461 aData.resize(0);
462 }
463 }
464 catch (std::bad_alloc &)
465 {
466 hr = E_OUTOFMEMORY;
467 }
468
469 LogFlowFunc(("Returning hr=%Rhrc\n", hr));
470 return hr;
471#endif /* VBOX_WITH_DRAG_AND_DROP */
472}
473
474// implementation of internal methods.
475/////////////////////////////////////////////////////////////////////////////
476
477/* static */
478Utf8Str GuestDnDSource::i_guestErrorToString(int guestRc)
479{
480 Utf8Str strError;
481
482 switch (guestRc)
483 {
484 case VERR_ACCESS_DENIED:
485 strError += Utf8StrFmt(tr("For one or more guest files or directories selected for transferring to the host your guest "
486 "user does not have the appropriate access rights for. Please make sure that all selected "
487 "elements can be accessed and that your guest user has the appropriate rights"));
488 break;
489
490 case VERR_NOT_FOUND:
491 /* Should not happen due to file locking on the guest, but anyway ... */
492 strError += Utf8StrFmt(tr("One or more guest files or directories selected for transferring to the host were not"
493 "found on the guest anymore. This can be the case if the guest files were moved and/or"
494 "altered while the drag and drop operation was in progress"));
495 break;
496
497 case VERR_SHARING_VIOLATION:
498 strError += Utf8StrFmt(tr("One or more guest files or directories selected for transferring to the host were locked. "
499 "Please make sure that all selected elements can be accessed and that your guest user has "
500 "the appropriate rights"));
501 break;
502
503 case VERR_TIMEOUT:
504 strError += Utf8StrFmt(tr("The guest was not able to retrieve the drag and drop data within time"));
505 break;
506
507 default:
508 strError += Utf8StrFmt(tr("Drag and drop error from guest (%Rrc)"), guestRc);
509 break;
510 }
511
512 return strError;
513}
514
515/* static */
516Utf8Str GuestDnDSource::i_hostErrorToString(int hostRc)
517{
518 Utf8Str strError;
519
520 switch (hostRc)
521 {
522 case VERR_ACCESS_DENIED:
523 strError += Utf8StrFmt(tr("For one or more host files or directories selected for transferring to the guest your host "
524 "user does not have the appropriate access rights for. Please make sure that all selected "
525 "elements can be accessed and that your host user has the appropriate rights."));
526 break;
527
528 case VERR_DISK_FULL:
529 strError += Utf8StrFmt(tr("Host disk ran out of space (disk is full)."));
530 break;
531
532 case VERR_NOT_FOUND:
533 /* Should not happen due to file locking on the host, but anyway ... */
534 strError += Utf8StrFmt(tr("One or more host files or directories selected for transferring to the host were not"
535 "found on the host anymore. This can be the case if the host files were moved and/or"
536 "altered while the drag and drop operation was in progress."));
537 break;
538
539 case VERR_SHARING_VIOLATION:
540 strError += Utf8StrFmt(tr("One or more host files or directories selected for transferring to the guest were locked. "
541 "Please make sure that all selected elements can be accessed and that your host user has "
542 "the appropriate rights."));
543 break;
544
545 default:
546 strError += Utf8StrFmt(tr("Drag and drop error from host (%Rrc)"), hostRc);
547 break;
548 }
549
550 return strError;
551}
552
553#ifdef VBOX_WITH_DRAG_AND_DROP_GH
554int GuestDnDSource::i_onReceiveDataHdr(PRECVDATACTX pCtx, PVBOXDNDSNDDATAHDR pDataHdr)
555{
556 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
557 AssertReturn(pDataHdr, VERR_INVALID_POINTER);
558
559 pCtx->mData.setEstimatedSize(pDataHdr->cbTotal, pDataHdr->cbMeta);
560
561 Assert(pCtx->mURI.getObjToProcess() == 0);
562 pCtx->mURI.reset();
563 pCtx->mURI.setEstimatedObjects(pDataHdr->cObjects);
564
565 /** @todo Handle compression type. */
566 /** @todo Handle checksum type. */
567
568 LogFlowFuncLeave();
569 return VINF_SUCCESS;
570}
571
572int GuestDnDSource::i_onReceiveData(PRECVDATACTX pCtx, PVBOXDNDSNDDATA pSndData)
573{
574 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
575 AssertPtrReturn(pSndData, VERR_INVALID_POINTER);
576
577 int rc = VINF_SUCCESS;
578
579 try
580 {
581 GuestDnDData *pData = &pCtx->mData;
582 GuestDnDURIData *pURI = &pCtx->mURI;
583
584 uint32_t cbData;
585 void *pvData;
586 uint64_t cbTotal;
587 uint32_t cbMeta;
588
589 if (mDataBase.m_uProtocolVersion < 3)
590 {
591 cbData = pSndData->u.v1.cbData;
592 pvData = pSndData->u.v1.pvData;
593
594 /* Sends the total data size to receive for every data chunk. */
595 cbTotal = pSndData->u.v1.cbTotalSize;
596
597 /* Meta data size always is cbData, meaning there cannot be an
598 * extended data chunk transfer by sending further data. */
599 cbMeta = cbData;
600 }
601 else
602 {
603 cbData = pSndData->u.v3.cbData;
604 pvData = pSndData->u.v3.pvData;
605
606 /* Note: Data sizes get updated in i_onReceiveDataHdr(). */
607 cbTotal = pData->getTotal();
608 cbMeta = pData->getMeta().getSize();
609 }
610 Assert(cbTotal);
611
612 if ( cbData == 0
613 || cbData > cbTotal /* Paranoia */)
614 {
615 LogFlowFunc(("Incoming data size invalid: cbData=%RU32, cbToProcess=%RU64\n", cbData, pData->getTotal()));
616 rc = VERR_INVALID_PARAMETER;
617 }
618 else if (cbTotal < cbMeta)
619 {
620 AssertMsgFailed(("cbTotal (%RU64) is smaller than cbMeta (%RU32)\n", cbTotal, cbMeta));
621 rc = VERR_INVALID_PARAMETER;
622 }
623
624 if (RT_SUCCESS(rc))
625 {
626 cbMeta = pData->getMeta().add(pvData, cbData);
627 LogFlowThisFunc(("cbMetaSize=%zu, cbData=%RU32, cbMeta=%RU32, cbTotal=%RU64\n",
628 pData->getMeta().getSize(), cbData, cbMeta, cbTotal));
629 }
630
631 if (RT_SUCCESS(rc))
632 {
633 /*
634 * (Meta) Data transfer complete?
635 */
636 Assert(cbMeta <= pData->getMeta().getSize());
637 if (cbMeta == pData->getMeta().getSize())
638 {
639 bool fHasURIList = DnDMIMENeedsDropDir(pCtx->mFmtRecv.c_str(), pCtx->mFmtRecv.length());
640 LogFlowThisFunc(("fHasURIList=%RTbool\n", fHasURIList));
641 if (fHasURIList)
642 {
643 /* Try parsing the data as URI list. */
644 rc = pURI->fromRemoteMetaData(pData->getMeta());
645 if (RT_SUCCESS(rc))
646 {
647 if (mDataBase.m_uProtocolVersion < 3)
648 pData->setEstimatedSize(cbTotal, cbMeta);
649
650 /*
651 * Update our process with the data we already received.
652 * Note: The total size will consist of the meta data (in pVecData) and
653 * the actual accumulated file/directory data from the guest.
654 */
655 rc = updateProgress(pData, pCtx->mpResp, (uint32_t)pData->getMeta().getSize());
656 }
657 }
658 else /* Raw data. */
659 rc = updateProgress(pData, pCtx->mpResp, cbData);
660 }
661 }
662 }
663 catch (std::bad_alloc &)
664 {
665 rc = VERR_NO_MEMORY;
666 }
667
668 LogFlowFuncLeaveRC(rc);
669 return rc;
670}
671
672int GuestDnDSource::i_onReceiveDir(PRECVDATACTX pCtx, const char *pszPath, uint32_t cbPath, uint32_t fMode)
673{
674 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
675 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
676 AssertReturn(cbPath, VERR_INVALID_PARAMETER);
677
678 LogFlowFunc(("pszPath=%s, cbPath=%RU32, fMode=0x%x\n", pszPath, cbPath, fMode));
679
680 /*
681 * Sanity checking.
682 */
683 if ( !cbPath
684 || cbPath > RTPATH_MAX)
685 {
686 LogFlowFunc(("Path length invalid, bailing out\n"));
687 return VERR_INVALID_PARAMETER;
688 }
689
690 int rc = RTStrValidateEncodingEx(pszPath, RTSTR_MAX, 0);
691 if (RT_FAILURE(rc))
692 {
693 LogFlowFunc(("Path validation failed with %Rrc, bailing out\n", rc));
694 return VERR_INVALID_PARAMETER;
695 }
696
697 if (pCtx->mURI.isComplete())
698 {
699 LogFlowFunc(("Data transfer already complete, bailing out\n"));
700 return VERR_INVALID_PARAMETER;
701 }
702
703 GuestDnDURIObjCtx &objCtx = pCtx->mURI.getObj(0); /** @todo Fill in context ID. */
704
705 rc = objCtx.createIntermediate(DnDURIObject::Directory);
706 if (RT_FAILURE(rc))
707 return rc;
708
709 DnDURIObject *pObj = objCtx.getObj();
710 AssertPtr(pObj);
711
712 const char *pszDroppedFilesDir = pCtx->mURI.getDroppedFiles().GetDirAbs();
713 char *pszDir = RTPathJoinA(pszDroppedFilesDir, pszPath);
714 if (pszDir)
715 {
716#ifdef RT_OS_WINDOWS
717 RTPathChangeToDosSlashes(pszDir, true /* fForce */);
718#else
719 RTPathChangeToDosSlashes(pszDir, true /* fForce */);
720#endif
721 rc = RTDirCreateFullPath(pszDir, fMode);
722 if (RT_SUCCESS(rc))
723 {
724 pCtx->mURI.processObject(*pObj);
725
726 /* Add for having a proper rollback. */
727 int rc2 = pCtx->mURI.getDroppedFiles().AddDir(pszDir);
728 AssertRC(rc2);
729
730 objCtx.reset();
731 LogRel2(("DnD: Created guest directory on host: %s\n", pszDir));
732 }
733 else
734 LogRel(("DnD: Error creating guest directory '%s' on host, rc=%Rrc\n", pszDir, rc));
735
736 RTStrFree(pszDir);
737 }
738 else
739 rc = VERR_NO_MEMORY;
740
741 LogFlowFuncLeaveRC(rc);
742 return rc;
743}
744
745int GuestDnDSource::i_onReceiveFileHdr(PRECVDATACTX pCtx, const char *pszPath, uint32_t cbPath,
746 uint64_t cbSize, uint32_t fMode, uint32_t fFlags)
747{
748 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
749 AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
750 AssertReturn(cbPath, VERR_INVALID_PARAMETER);
751 AssertReturn(fMode, VERR_INVALID_PARAMETER);
752 /* fFlags are optional. */
753
754 LogFlowFunc(("pszPath=%s, cbPath=%RU32, cbSize=%RU64, fMode=0x%x, fFlags=0x%x\n", pszPath, cbPath, cbSize, fMode, fFlags));
755
756 /*
757 * Sanity checking.
758 */
759 if ( !cbPath
760 || cbPath > RTPATH_MAX)
761 {
762 return VERR_INVALID_PARAMETER;
763 }
764
765 if (!RTStrIsValidEncoding(pszPath))
766 return VERR_INVALID_PARAMETER;
767
768 if (cbSize > pCtx->mData.getTotal())
769 {
770 AssertMsgFailed(("File size (%RU64) exceeds total size to transfer (%RU64)\n", cbSize, pCtx->mData.getTotal()));
771 return VERR_INVALID_PARAMETER;
772 }
773
774 if (pCtx->mURI.getObjToProcess() && pCtx->mURI.isComplete())
775 return VERR_INVALID_PARAMETER;
776
777 int rc = VINF_SUCCESS;
778
779 do
780 {
781 GuestDnDURIObjCtx &objCtx = pCtx->mURI.getObj(0); /** @todo Fill in context ID. */
782 DnDURIObject *pObj = objCtx.getObj();
783
784 /*
785 * Sanity checking.
786 */
787 if (pObj)
788 {
789 if ( pObj->IsOpen()
790 && !pObj->IsComplete())
791 {
792 AssertMsgFailed(("Object '%s' not complete yet\n", pObj->GetDestPath().c_str()));
793 rc = VERR_WRONG_ORDER;
794 break;
795 }
796
797 if (pObj->IsOpen()) /* File already opened? */
798 {
799 AssertMsgFailed(("Current opened object is '%s', close this first\n", pObj->GetDestPath().c_str()));
800 rc = VERR_WRONG_ORDER;
801 break;
802 }
803 }
804 else
805 {
806 /*
807 * Create new intermediate object to work with.
808 */
809 rc = objCtx.createIntermediate();
810 }
811
812 if (RT_SUCCESS(rc))
813 {
814 pObj = objCtx.getObj();
815 AssertPtr(pObj);
816
817 const char *pszDroppedFilesDir = pCtx->mURI.getDroppedFiles().GetDirAbs();
818 AssertPtr(pszDroppedFilesDir);
819
820 char pszPathAbs[RTPATH_MAX];
821 rc = RTPathJoin(pszPathAbs, sizeof(pszPathAbs), pszDroppedFilesDir, pszPath);
822 if (RT_FAILURE(rc))
823 {
824 LogFlowFunc(("Warning: Rebasing current file failed with rc=%Rrc\n", rc));
825 break;
826 }
827
828 rc = DnDPathSanitize(pszPathAbs, sizeof(pszPathAbs));
829 if (RT_FAILURE(rc))
830 {
831 LogFlowFunc(("Warning: Rebasing current file failed with rc=%Rrc\n", rc));
832 break;
833 }
834
835 LogFunc(("Rebased to: %s\n", pszPathAbs));
836
837 /** @todo Add sparse file support based on fFlags? (Use Open(..., fFlags | SPARSE). */
838 rc = pObj->OpenEx(pszPathAbs, DnDURIObject::File, DnDURIObject::Target,
839 RTFILE_O_CREATE_REPLACE | RTFILE_O_WRITE | RTFILE_O_DENY_WRITE,
840 (fMode & RTFS_UNIX_MASK) | RTFS_UNIX_IRUSR | RTFS_UNIX_IWUSR);
841 if (RT_SUCCESS(rc))
842 {
843 /* Add for having a proper rollback. */
844 int rc2 = pCtx->mURI.getDroppedFiles().AddFile(pszPathAbs);
845 AssertRC(rc2);
846 }
847 }
848
849 if (RT_SUCCESS(rc))
850 {
851 /* Note: Protocol v1 does not send any file sizes, so always 0. */
852 if (mDataBase.m_uProtocolVersion >= 2)
853 rc = pObj->SetSize(cbSize);
854
855 /** @todo Unescpae path before printing. */
856 LogRel2(("DnD: Transferring guest file to host: %s (%RU64 bytes, mode 0x%x)\n",
857 pObj->GetDestPath().c_str(), pObj->GetSize(), pObj->GetMode()));
858
859 /** @todo Set progress object title to current file being transferred? */
860
861 if (!cbSize) /* 0-byte file? Close again. */
862 pObj->Close();
863 }
864
865 if (RT_FAILURE(rc))
866 {
867 LogRel2(("DnD: Error opening/creating guest file '%s' on host, rc=%Rrc\n",
868 pObj->GetDestPath().c_str(), rc));
869 break;
870 }
871
872 } while (0);
873
874 LogFlowFuncLeaveRC(rc);
875 return rc;
876}
877
878int GuestDnDSource::i_onReceiveFileData(PRECVDATACTX pCtx, const void *pvData, uint32_t cbData)
879{
880 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
881 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
882 AssertReturn(cbData, VERR_INVALID_PARAMETER);
883
884 int rc = VINF_SUCCESS;
885
886 LogFlowFunc(("pvData=%p, cbData=%RU32, cbBlockSize=%RU32\n", pvData, cbData, mData.mcbBlockSize));
887
888 /*
889 * Sanity checking.
890 */
891 if (cbData > mData.mcbBlockSize)
892 return VERR_INVALID_PARAMETER;
893
894 do
895 {
896 GuestDnDURIObjCtx &objCtx = pCtx->mURI.getObj(0); /** @todo Fill in context ID. */
897 DnDURIObject *pObj = objCtx.getObj();
898
899 if (!pObj)
900 {
901 LogFlowFunc(("Warning: No current object set\n"));
902 rc = VERR_WRONG_ORDER;
903 break;
904 }
905
906 if (pObj->IsComplete())
907 {
908 LogFlowFunc(("Warning: Object '%s' already completed\n", pObj->GetDestPath().c_str()));
909 rc = VERR_WRONG_ORDER;
910 break;
911 }
912
913 if (!pObj->IsOpen()) /* File opened on host? */
914 {
915 LogFlowFunc(("Warning: Object '%s' not opened\n", pObj->GetDestPath().c_str()));
916 rc = VERR_WRONG_ORDER;
917 break;
918 }
919
920 uint32_t cbWritten;
921 rc = pObj->Write(pvData, cbData, &cbWritten);
922 if (RT_SUCCESS(rc))
923 {
924 Assert(cbWritten <= cbData);
925 if (cbWritten < cbData)
926 {
927 /** @todo What to do when the host's disk is full? */
928 rc = VERR_DISK_FULL;
929 }
930
931 if (RT_SUCCESS(rc))
932 rc = updateProgress(&pCtx->mData, pCtx->mpResp, cbWritten);
933 }
934 else /* Something went wrong; close the object. */
935 pObj->Close();
936
937 if (RT_SUCCESS(rc))
938 {
939 if (pObj->IsComplete())
940 {
941 /** @todo Sanitize path. */
942 LogRel2(("DnD: File transfer to host complete: %s\n", pObj->GetDestPath().c_str()));
943 pCtx->mURI.processObject(*pObj);
944 objCtx.reset();
945 }
946 }
947 else
948 {
949 /** @todo What to do when the host's disk is full? */
950 LogRel(("DnD: Error writing guest file to host to '%s': %Rrc\n", pObj->GetDestPath().c_str(), rc));
951 }
952
953 } while (0);
954
955 LogFlowFuncLeaveRC(rc);
956 return rc;
957}
958#endif /* VBOX_WITH_DRAG_AND_DROP_GH */
959
960int GuestDnDSource::i_receiveData(PRECVDATACTX pCtx, RTMSINTERVAL msTimeout)
961{
962 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
963
964 /* Is this context already in receiving state? */
965 if (ASMAtomicReadBool(&pCtx->mIsActive))
966 return VERR_WRONG_ORDER;
967 ASMAtomicWriteBool(&pCtx->mIsActive, true);
968
969 GuestDnD *pInst = GuestDnDInst();
970 if (!pInst)
971 return VERR_INVALID_POINTER;
972
973 GuestDnDResponse *pResp = pCtx->mpResp;
974 AssertPtr(pCtx->mpResp);
975
976 int rc = pCtx->mCBEvent.Reset();
977 if (RT_FAILURE(rc))
978 return rc;
979
980 /*
981 * Reset any old data.
982 */
983 pCtx->mData.reset();
984 pCtx->mURI.reset();
985 pResp->reset();
986
987 /*
988 * Do we need to receive a different format than initially requested?
989 *
990 * For example, receiving a file link as "text/plain" requires still to receive
991 * the file from the guest as "text/uri-list" first, then pointing to
992 * the file path on the host in the "text/plain" data returned.
993 */
994
995 bool fFoundFormat = true; /* Whether we've found a common format between host + guest. */
996
997 LogFlowFunc(("mFmtReq=%s, mFmtRecv=%s, mAction=0x%x\n",
998 pCtx->mFmtReq.c_str(), pCtx->mFmtRecv.c_str(), pCtx->mAction));
999
1000 /* Plain text wanted? */
1001 if ( pCtx->mFmtReq.equalsIgnoreCase("text/plain")
1002 || pCtx->mFmtReq.equalsIgnoreCase("text/plain;charset=utf-8"))
1003 {
1004 /* Did the guest offer a file? Receive a file instead. */
1005 if (GuestDnD::isFormatInFormatList("text/uri-list", pCtx->mFmtOffered))
1006 pCtx->mFmtRecv = "text/uri-list";
1007 /* Guest only offers (plain) text. */
1008 else
1009 pCtx->mFmtRecv = "text/plain;charset=utf-8";
1010
1011 /** @todo Add more conversions here. */
1012 }
1013 /* File(s) wanted? */
1014 else if (pCtx->mFmtReq.equalsIgnoreCase("text/uri-list"))
1015 {
1016 /* Does the guest support sending files? */
1017 if (GuestDnD::isFormatInFormatList("text/uri-list", pCtx->mFmtOffered))
1018 pCtx->mFmtRecv = "text/uri-list";
1019 else /* Bail out. */
1020 fFoundFormat = false;
1021 }
1022
1023 if (fFoundFormat)
1024 {
1025 Assert(!pCtx->mFmtReq.isEmpty());
1026 Assert(!pCtx->mFmtRecv.isEmpty());
1027
1028 if (!pCtx->mFmtRecv.equals(pCtx->mFmtReq))
1029 LogRel3(("DnD: Requested data in format '%s', receiving in intermediate format '%s' now\n",
1030 pCtx->mFmtReq.c_str(), pCtx->mFmtRecv.c_str()));
1031
1032 /*
1033 * Call the appropriate receive handler based on the data format to handle.
1034 */
1035 bool fURIData = DnDMIMENeedsDropDir(pCtx->mFmtRecv.c_str(), pCtx->mFmtRecv.length());
1036 if (fURIData)
1037 {
1038 rc = i_receiveURIData(pCtx, msTimeout);
1039 }
1040 else
1041 {
1042 rc = i_receiveRawData(pCtx, msTimeout);
1043 }
1044 }
1045 else /* Just inform the user (if verbose release logging is enabled). */
1046 {
1047 LogRel2(("DnD: The guest does not support format '%s':\n", pCtx->mFmtReq.c_str()));
1048 LogRel2(("DnD: Guest offered the following formats:\n"));
1049 for (size_t i = 0; i < pCtx->mFmtOffered.size(); i++)
1050 LogRel2(("DnD:\tFormat #%zu: %s\n", i, pCtx->mFmtOffered.at(i).c_str()));
1051 }
1052
1053 ASMAtomicWriteBool(&pCtx->mIsActive, false);
1054
1055 LogFlowFuncLeaveRC(rc);
1056 return rc;
1057}
1058
1059/* static */
1060DECLCALLBACK(int) GuestDnDSource::i_receiveDataThread(RTTHREAD Thread, void *pvUser)
1061{
1062 LogFlowFunc(("pvUser=%p\n", pvUser));
1063
1064 RecvDataTask *pTask = (RecvDataTask *)pvUser;
1065 AssertPtrReturn(pTask, VERR_INVALID_POINTER);
1066
1067 const ComObjPtr<GuestDnDSource> pThis(pTask->getSource());
1068 Assert(!pThis.isNull());
1069
1070 AutoCaller autoCaller(pThis);
1071 if (FAILED(autoCaller.rc())) return VERR_COM_INVALID_OBJECT_STATE;
1072
1073 int rc = RTThreadUserSignal(Thread);
1074 AssertRC(rc);
1075
1076 rc = pThis->i_receiveData(pTask->getCtx(), RT_INDEFINITE_WAIT /* msTimeout */);
1077
1078 AutoWriteLock alock(pThis COMMA_LOCKVAL_SRC_POS);
1079
1080 Assert(pThis->mDataBase.m_cTransfersPending);
1081 pThis->mDataBase.m_cTransfersPending--;
1082
1083 LogFlowFunc(("pSource=%p returning rc=%Rrc\n", (GuestDnDSource *)pThis, rc));
1084 return rc;
1085}
1086
1087int GuestDnDSource::i_receiveRawData(PRECVDATACTX pCtx, RTMSINTERVAL msTimeout)
1088{
1089 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
1090
1091 int rc;
1092
1093 LogFlowFuncEnter();
1094
1095 GuestDnDResponse *pResp = pCtx->mpResp;
1096 AssertPtr(pCtx->mpResp);
1097
1098 GuestDnD *pInst = GuestDnDInst();
1099 if (!pInst)
1100 return VERR_INVALID_POINTER;
1101
1102#define REGISTER_CALLBACK(x) \
1103 rc = pResp->setCallback(x, i_receiveRawDataCallback, pCtx); \
1104 if (RT_FAILURE(rc)) \
1105 return rc;
1106
1107#define UNREGISTER_CALLBACK(x) \
1108 { \
1109 int rc2 = pResp->setCallback(x, NULL); \
1110 AssertRC(rc2); \
1111 }
1112
1113 /*
1114 * Register callbacks.
1115 */
1116 REGISTER_CALLBACK(GUEST_DND_CONNECT);
1117 REGISTER_CALLBACK(GUEST_DND_DISCONNECT);
1118 REGISTER_CALLBACK(GUEST_DND_GH_EVT_ERROR);
1119 if (mDataBase.m_uProtocolVersion >= 3)
1120 REGISTER_CALLBACK(GUEST_DND_GH_SND_DATA_HDR);
1121 REGISTER_CALLBACK(GUEST_DND_GH_SND_DATA);
1122
1123 do
1124 {
1125 /*
1126 * Receive the raw data.
1127 */
1128 GuestDnDMsg Msg;
1129 Msg.setType(HOST_DND_GH_EVT_DROPPED);
1130 if (mDataBase.m_uProtocolVersion >= 3)
1131 Msg.setNextUInt32(0); /** @todo ContextID not used yet. */
1132 Msg.setNextPointer((void*)pCtx->mFmtRecv.c_str(), (uint32_t)pCtx->mFmtRecv.length() + 1);
1133 Msg.setNextUInt32((uint32_t)pCtx->mFmtRecv.length() + 1);
1134 Msg.setNextUInt32(pCtx->mAction);
1135
1136 /* Make the initial call to the guest by telling that we initiated the "dropped" event on
1137 * the host and therefore now waiting for the actual raw data. */
1138 rc = pInst->hostCall(Msg.getType(), Msg.getCount(), Msg.getParms());
1139 if (RT_SUCCESS(rc))
1140 {
1141 rc = waitForEvent(&pCtx->mCBEvent, pCtx->mpResp, msTimeout);
1142 if (RT_SUCCESS(rc))
1143 rc = pCtx->mpResp->setProgress(100, DND_PROGRESS_COMPLETE, VINF_SUCCESS);
1144 }
1145
1146 } while (0);
1147
1148 /*
1149 * Unregister callbacks.
1150 */
1151 UNREGISTER_CALLBACK(GUEST_DND_CONNECT);
1152 UNREGISTER_CALLBACK(GUEST_DND_DISCONNECT);
1153 UNREGISTER_CALLBACK(GUEST_DND_GH_EVT_ERROR);
1154 if (mDataBase.m_uProtocolVersion >= 3)
1155 UNREGISTER_CALLBACK(GUEST_DND_GH_SND_DATA_HDR);
1156 UNREGISTER_CALLBACK(GUEST_DND_GH_SND_DATA);
1157
1158#undef REGISTER_CALLBACK
1159#undef UNREGISTER_CALLBACK
1160
1161 if (RT_FAILURE(rc))
1162 {
1163 if (rc == VERR_CANCELLED)
1164 {
1165 int rc2 = pCtx->mpResp->setProgress(100, DND_PROGRESS_CANCELLED);
1166 AssertRC(rc2);
1167
1168 rc2 = sendCancel();
1169 AssertRC(rc2);
1170 }
1171 else if (rc != VERR_GSTDND_GUEST_ERROR) /* Guest-side error are already handled in the callback. */
1172 {
1173 int rc2 = pCtx->mpResp->setProgress(100, DND_PROGRESS_ERROR,
1174 rc, GuestDnDSource::i_hostErrorToString(rc));
1175 AssertRC(rc2);
1176 }
1177 }
1178
1179 LogFlowFuncLeaveRC(rc);
1180 return rc;
1181}
1182
1183int GuestDnDSource::i_receiveURIData(PRECVDATACTX pCtx, RTMSINTERVAL msTimeout)
1184{
1185 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
1186
1187 int rc;
1188
1189 LogFlowFuncEnter();
1190
1191 GuestDnDResponse *pResp = pCtx->mpResp;
1192 AssertPtr(pCtx->mpResp);
1193
1194 GuestDnD *pInst = GuestDnDInst();
1195 if (!pInst)
1196 return VERR_INVALID_POINTER;
1197
1198#define REGISTER_CALLBACK(x) \
1199 rc = pResp->setCallback(x, i_receiveURIDataCallback, pCtx); \
1200 if (RT_FAILURE(rc)) \
1201 return rc;
1202
1203#define UNREGISTER_CALLBACK(x) \
1204 { \
1205 int rc2 = pResp->setCallback(x, NULL); \
1206 AssertRC(rc2); \
1207 }
1208
1209 /*
1210 * Register callbacks.
1211 */
1212 /* Guest callbacks. */
1213 REGISTER_CALLBACK(GUEST_DND_CONNECT);
1214 REGISTER_CALLBACK(GUEST_DND_DISCONNECT);
1215 REGISTER_CALLBACK(GUEST_DND_GH_EVT_ERROR);
1216 if (mDataBase.m_uProtocolVersion >= 3)
1217 REGISTER_CALLBACK(GUEST_DND_GH_SND_DATA_HDR);
1218 REGISTER_CALLBACK(GUEST_DND_GH_SND_DATA);
1219 REGISTER_CALLBACK(GUEST_DND_GH_SND_DIR);
1220 if (mDataBase.m_uProtocolVersion >= 2)
1221 REGISTER_CALLBACK(GUEST_DND_GH_SND_FILE_HDR);
1222 REGISTER_CALLBACK(GUEST_DND_GH_SND_FILE_DATA);
1223
1224 DnDDroppedFiles &droppedFiles = pCtx->mURI.getDroppedFiles();
1225
1226 do
1227 {
1228 rc = droppedFiles.OpenTemp(0 /* fFlags */);
1229 if (RT_FAILURE(rc))
1230 break;
1231 LogFlowFunc(("rc=%Rrc, strDropDir=%s\n", rc, droppedFiles.GetDirAbs()));
1232 if (RT_FAILURE(rc))
1233 break;
1234
1235 /*
1236 * Receive the URI list.
1237 */
1238 GuestDnDMsg Msg;
1239 Msg.setType(HOST_DND_GH_EVT_DROPPED);
1240 if (mDataBase.m_uProtocolVersion >= 3)
1241 Msg.setNextUInt32(0); /** @todo ContextID not used yet. */
1242 Msg.setNextPointer((void*)pCtx->mFmtRecv.c_str(), (uint32_t)pCtx->mFmtRecv.length() + 1);
1243 Msg.setNextUInt32((uint32_t)pCtx->mFmtRecv.length() + 1);
1244 Msg.setNextUInt32(pCtx->mAction);
1245
1246 /* Make the initial call to the guest by telling that we initiated the "dropped" event on
1247 * the host and therefore now waiting for the actual URI data. */
1248 rc = pInst->hostCall(Msg.getType(), Msg.getCount(), Msg.getParms());
1249 if (RT_SUCCESS(rc))
1250 {
1251 LogFlowFunc(("Waiting ...\n"));
1252
1253 rc = waitForEvent(&pCtx->mCBEvent, pCtx->mpResp, msTimeout);
1254 if (RT_SUCCESS(rc))
1255 rc = pCtx->mpResp->setProgress(100, DND_PROGRESS_COMPLETE, VINF_SUCCESS);
1256
1257 LogFlowFunc(("Waiting ended with rc=%Rrc\n", rc));
1258 }
1259
1260 } while (0);
1261
1262 /*
1263 * Unregister callbacks.
1264 */
1265 UNREGISTER_CALLBACK(GUEST_DND_CONNECT);
1266 UNREGISTER_CALLBACK(GUEST_DND_DISCONNECT);
1267 UNREGISTER_CALLBACK(GUEST_DND_GH_EVT_ERROR);
1268 UNREGISTER_CALLBACK(GUEST_DND_GH_SND_DATA_HDR);
1269 UNREGISTER_CALLBACK(GUEST_DND_GH_SND_DATA);
1270 UNREGISTER_CALLBACK(GUEST_DND_GH_SND_DIR);
1271 UNREGISTER_CALLBACK(GUEST_DND_GH_SND_FILE_HDR);
1272 UNREGISTER_CALLBACK(GUEST_DND_GH_SND_FILE_DATA);
1273
1274#undef REGISTER_CALLBACK
1275#undef UNREGISTER_CALLBACK
1276
1277 if (RT_FAILURE(rc))
1278 {
1279 if (rc == VERR_CANCELLED)
1280 {
1281 int rc2 = pCtx->mpResp->setProgress(100, DND_PROGRESS_CANCELLED);
1282 AssertRC(rc2);
1283
1284 rc2 = sendCancel();
1285 AssertRC(rc2);
1286 }
1287 else if (rc != VERR_GSTDND_GUEST_ERROR) /* Guest-side error are already handled in the callback. */
1288 {
1289 int rc2 = pCtx->mpResp->setProgress(100, DND_PROGRESS_ERROR,
1290 rc, GuestDnDSource::i_hostErrorToString(rc));
1291 AssertRC(rc2);
1292 }
1293 }
1294
1295 if (RT_FAILURE(rc))
1296 {
1297 int rc2 = droppedFiles.Rollback();
1298 if (RT_FAILURE(rc2))
1299 LogRel(("DnD: Deleting left over temporary files failed (%Rrc). Please remove directory manually: %s\n",
1300 rc2, droppedFiles.GetDirAbs()));
1301 }
1302
1303 droppedFiles.Close();
1304
1305 LogFlowFuncLeaveRC(rc);
1306 return rc;
1307}
1308
1309/* static */
1310DECLCALLBACK(int) GuestDnDSource::i_receiveRawDataCallback(uint32_t uMsg, void *pvParms, size_t cbParms, void *pvUser)
1311{
1312 PRECVDATACTX pCtx = (PRECVDATACTX)pvUser;
1313 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
1314
1315 GuestDnDSource *pThis = pCtx->mpSource;
1316 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1317
1318 LogFlowFunc(("pThis=%p, uMsg=%RU32\n", pThis, uMsg));
1319
1320 int rc = VINF_SUCCESS;
1321
1322 int rcCallback = VINF_SUCCESS; /* rc for the callback. */
1323 bool fNotify = false;
1324
1325 switch (uMsg)
1326 {
1327 case GUEST_DND_CONNECT:
1328 /* Nothing to do here (yet). */
1329 break;
1330
1331 case GUEST_DND_DISCONNECT:
1332 rc = VERR_CANCELLED;
1333 break;
1334
1335#ifdef VBOX_WITH_DRAG_AND_DROP_GH
1336 case GUEST_DND_GH_SND_DATA_HDR:
1337 {
1338 PVBOXDNDCBSNDDATAHDRDATA pCBData = reinterpret_cast<PVBOXDNDCBSNDDATAHDRDATA>(pvParms);
1339 AssertPtr(pCBData);
1340 AssertReturn(sizeof(VBOXDNDCBSNDDATAHDRDATA) == cbParms, VERR_INVALID_PARAMETER);
1341 AssertReturn(CB_MAGIC_DND_GH_SND_DATA_HDR == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
1342
1343 rc = pThis->i_onReceiveDataHdr(pCtx, &pCBData->data);
1344 break;
1345 }
1346 case GUEST_DND_GH_SND_DATA:
1347 {
1348 PVBOXDNDCBSNDDATADATA pCBData = reinterpret_cast<PVBOXDNDCBSNDDATADATA>(pvParms);
1349 AssertPtr(pCBData);
1350 AssertReturn(sizeof(VBOXDNDCBSNDDATADATA) == cbParms, VERR_INVALID_PARAMETER);
1351 AssertReturn(CB_MAGIC_DND_GH_SND_DATA == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
1352
1353 rc = pThis->i_onReceiveData(pCtx, &pCBData->data);
1354 break;
1355 }
1356 case GUEST_DND_GH_EVT_ERROR:
1357 {
1358 PVBOXDNDCBEVTERRORDATA pCBData = reinterpret_cast<PVBOXDNDCBEVTERRORDATA>(pvParms);
1359 AssertPtr(pCBData);
1360 AssertReturn(sizeof(VBOXDNDCBEVTERRORDATA) == cbParms, VERR_INVALID_PARAMETER);
1361 AssertReturn(CB_MAGIC_DND_GH_EVT_ERROR == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
1362
1363 pCtx->mpResp->reset();
1364
1365 if (RT_SUCCESS(pCBData->rc))
1366 {
1367 AssertMsgFailed(("Received guest error with no error code set\n"));
1368 pCBData->rc = VERR_GENERAL_FAILURE; /* Make sure some error is set. */
1369 }
1370 else if (pCBData->rc == VERR_WRONG_ORDER)
1371 {
1372 rc = pCtx->mpResp->setProgress(100, DND_PROGRESS_CANCELLED);
1373 }
1374 else
1375 rc = pCtx->mpResp->setProgress(100, DND_PROGRESS_ERROR, pCBData->rc,
1376 GuestDnDSource::i_guestErrorToString(pCBData->rc));
1377
1378 LogRel3(("DnD: Guest reported data transfer error: %Rrc\n", pCBData->rc));
1379
1380 if (RT_SUCCESS(rc))
1381 rcCallback = VERR_GSTDND_GUEST_ERROR;
1382 break;
1383 }
1384#endif /* VBOX_WITH_DRAG_AND_DROP_GH */
1385 default:
1386 rc = VERR_NOT_SUPPORTED;
1387 break;
1388 }
1389
1390 if ( RT_FAILURE(rc)
1391 || RT_FAILURE(rcCallback))
1392 {
1393 fNotify = true;
1394 if (RT_SUCCESS(rcCallback))
1395 rcCallback = rc;
1396 }
1397
1398 if (RT_FAILURE(rc))
1399 {
1400 switch (rc)
1401 {
1402 case VERR_NO_DATA:
1403 LogRel2(("DnD: Data transfer to host complete\n"));
1404 break;
1405
1406 case VERR_CANCELLED:
1407 LogRel2(("DnD: Data transfer to host canceled\n"));
1408 break;
1409
1410 default:
1411 LogRel(("DnD: Error %Rrc occurred, aborting data transfer to host\n", rc));
1412 break;
1413 }
1414
1415 /* Unregister this callback. */
1416 AssertPtr(pCtx->mpResp);
1417 int rc2 = pCtx->mpResp->setCallback(uMsg, NULL /* PFNGUESTDNDCALLBACK */);
1418 AssertRC(rc2);
1419 }
1420
1421 /* All data processed? */
1422 if (pCtx->mData.isComplete())
1423 fNotify = true;
1424
1425 LogFlowFunc(("cbProcessed=%RU64, cbToProcess=%RU64, fNotify=%RTbool, rcCallback=%Rrc, rc=%Rrc\n",
1426 pCtx->mData.getProcessed(), pCtx->mData.getTotal(), fNotify, rcCallback, rc));
1427
1428 if (fNotify)
1429 {
1430 int rc2 = pCtx->mCBEvent.Notify(rcCallback);
1431 AssertRC(rc2);
1432 }
1433
1434 LogFlowFuncLeaveRC(rc);
1435 return rc; /* Tell the guest. */
1436}
1437
1438/* static */
1439DECLCALLBACK(int) GuestDnDSource::i_receiveURIDataCallback(uint32_t uMsg, void *pvParms, size_t cbParms, void *pvUser)
1440{
1441 PRECVDATACTX pCtx = (PRECVDATACTX)pvUser;
1442 AssertPtrReturn(pCtx, VERR_INVALID_POINTER);
1443
1444 GuestDnDSource *pThis = pCtx->mpSource;
1445 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1446
1447 LogFlowFunc(("pThis=%p, uMsg=%RU32\n", pThis, uMsg));
1448
1449 int rc = VINF_SUCCESS;
1450
1451 int rcCallback = VINF_SUCCESS; /* rc for the callback. */
1452 bool fNotify = false;
1453
1454 switch (uMsg)
1455 {
1456 case GUEST_DND_CONNECT:
1457 /* Nothing to do here (yet). */
1458 break;
1459
1460 case GUEST_DND_DISCONNECT:
1461 rc = VERR_CANCELLED;
1462 break;
1463
1464#ifdef VBOX_WITH_DRAG_AND_DROP_GH
1465 case GUEST_DND_GH_SND_DATA_HDR:
1466 {
1467 PVBOXDNDCBSNDDATAHDRDATA pCBData = reinterpret_cast<PVBOXDNDCBSNDDATAHDRDATA>(pvParms);
1468 AssertPtr(pCBData);
1469 AssertReturn(sizeof(VBOXDNDCBSNDDATAHDRDATA) == cbParms, VERR_INVALID_PARAMETER);
1470 AssertReturn(CB_MAGIC_DND_GH_SND_DATA_HDR == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
1471
1472 rc = pThis->i_onReceiveDataHdr(pCtx, &pCBData->data);
1473 break;
1474 }
1475 case GUEST_DND_GH_SND_DATA:
1476 {
1477 PVBOXDNDCBSNDDATADATA pCBData = reinterpret_cast<PVBOXDNDCBSNDDATADATA>(pvParms);
1478 AssertPtr(pCBData);
1479 AssertReturn(sizeof(VBOXDNDCBSNDDATADATA) == cbParms, VERR_INVALID_PARAMETER);
1480 AssertReturn(CB_MAGIC_DND_GH_SND_DATA == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
1481
1482 rc = pThis->i_onReceiveData(pCtx, &pCBData->data);
1483 break;
1484 }
1485 case GUEST_DND_GH_SND_DIR:
1486 {
1487 PVBOXDNDCBSNDDIRDATA pCBData = reinterpret_cast<PVBOXDNDCBSNDDIRDATA>(pvParms);
1488 AssertPtr(pCBData);
1489 AssertReturn(sizeof(VBOXDNDCBSNDDIRDATA) == cbParms, VERR_INVALID_PARAMETER);
1490 AssertReturn(CB_MAGIC_DND_GH_SND_DIR == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
1491
1492 rc = pThis->i_onReceiveDir(pCtx, pCBData->pszPath, pCBData->cbPath, pCBData->fMode);
1493 break;
1494 }
1495 case GUEST_DND_GH_SND_FILE_HDR:
1496 {
1497 PVBOXDNDCBSNDFILEHDRDATA pCBData = reinterpret_cast<PVBOXDNDCBSNDFILEHDRDATA>(pvParms);
1498 AssertPtr(pCBData);
1499 AssertReturn(sizeof(VBOXDNDCBSNDFILEHDRDATA) == cbParms, VERR_INVALID_PARAMETER);
1500 AssertReturn(CB_MAGIC_DND_GH_SND_FILE_HDR == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
1501
1502 rc = pThis->i_onReceiveFileHdr(pCtx, pCBData->pszFilePath, pCBData->cbFilePath,
1503 pCBData->cbSize, pCBData->fMode, pCBData->fFlags);
1504 break;
1505 }
1506 case GUEST_DND_GH_SND_FILE_DATA:
1507 {
1508 PVBOXDNDCBSNDFILEDATADATA pCBData = reinterpret_cast<PVBOXDNDCBSNDFILEDATADATA>(pvParms);
1509 AssertPtr(pCBData);
1510 AssertReturn(sizeof(VBOXDNDCBSNDFILEDATADATA) == cbParms, VERR_INVALID_PARAMETER);
1511 AssertReturn(CB_MAGIC_DND_GH_SND_FILE_DATA == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
1512
1513 if (pThis->mDataBase.m_uProtocolVersion <= 1)
1514 {
1515 /**
1516 * Notes for protocol v1 (< VBox 5.0):
1517 * - Every time this command is being sent it includes the file header,
1518 * so just process both calls here.
1519 * - There was no information whatsoever about the total file size; the old code only
1520 * appended data to the desired file. So just pass 0 as cbSize.
1521 */
1522 rc = pThis->i_onReceiveFileHdr(pCtx, pCBData->u.v1.pszFilePath, pCBData->u.v1.cbFilePath,
1523 0 /* cbSize */, pCBData->u.v1.fMode, 0 /* fFlags */);
1524 if (RT_SUCCESS(rc))
1525 rc = pThis->i_onReceiveFileData(pCtx, pCBData->pvData, pCBData->cbData);
1526 }
1527 else /* Protocol v2 and up. */
1528 rc = pThis->i_onReceiveFileData(pCtx, pCBData->pvData, pCBData->cbData);
1529 break;
1530 }
1531 case GUEST_DND_GH_EVT_ERROR:
1532 {
1533 PVBOXDNDCBEVTERRORDATA pCBData = reinterpret_cast<PVBOXDNDCBEVTERRORDATA>(pvParms);
1534 AssertPtr(pCBData);
1535 AssertReturn(sizeof(VBOXDNDCBEVTERRORDATA) == cbParms, VERR_INVALID_PARAMETER);
1536 AssertReturn(CB_MAGIC_DND_GH_EVT_ERROR == pCBData->hdr.uMagic, VERR_INVALID_PARAMETER);
1537
1538 pCtx->mpResp->reset();
1539
1540 if (RT_SUCCESS(pCBData->rc))
1541 {
1542 AssertMsgFailed(("Received guest error with no error code set\n"));
1543 pCBData->rc = VERR_GENERAL_FAILURE; /* Make sure some error is set. */
1544 }
1545 else if (pCBData->rc == VERR_WRONG_ORDER)
1546 {
1547 rc = pCtx->mpResp->setProgress(100, DND_PROGRESS_CANCELLED);
1548 }
1549 else
1550 rc = pCtx->mpResp->setProgress(100, DND_PROGRESS_ERROR, pCBData->rc,
1551 GuestDnDSource::i_guestErrorToString(pCBData->rc));
1552
1553 LogRel3(("DnD: Guest reported file transfer error: %Rrc\n", pCBData->rc));
1554
1555 if (RT_SUCCESS(rc))
1556 rcCallback = VERR_GSTDND_GUEST_ERROR;
1557 break;
1558 }
1559#endif /* VBOX_WITH_DRAG_AND_DROP_GH */
1560 default:
1561 rc = VERR_NOT_SUPPORTED;
1562 break;
1563 }
1564
1565 if ( RT_FAILURE(rc)
1566 || RT_FAILURE(rcCallback))
1567 {
1568 fNotify = true;
1569 if (RT_SUCCESS(rcCallback))
1570 rcCallback = rc;
1571 }
1572
1573 if (RT_FAILURE(rc))
1574 {
1575 switch (rc)
1576 {
1577 case VERR_NO_DATA:
1578 LogRel2(("DnD: File transfer to host complete\n"));
1579 break;
1580
1581 case VERR_CANCELLED:
1582 LogRel2(("DnD: File transfer to host canceled\n"));
1583 break;
1584
1585 default:
1586 LogRel(("DnD: Error %Rrc occurred, aborting file transfer to host\n", rc));
1587 break;
1588 }
1589
1590 /* Unregister this callback. */
1591 AssertPtr(pCtx->mpResp);
1592 int rc2 = pCtx->mpResp->setCallback(uMsg, NULL /* PFNGUESTDNDCALLBACK */);
1593 AssertRC(rc2);
1594 }
1595
1596 /* All data processed? */
1597 if ( pCtx->mURI.isComplete()
1598 && pCtx->mData.isComplete())
1599 {
1600 fNotify = true;
1601 }
1602
1603 LogFlowFunc(("cbProcessed=%RU64, cbToProcess=%RU64, fNotify=%RTbool, rcCallback=%Rrc, rc=%Rrc\n",
1604 pCtx->mData.getProcessed(), pCtx->mData.getTotal(), fNotify, rcCallback, rc));
1605
1606 if (fNotify)
1607 {
1608 int rc2 = pCtx->mCBEvent.Notify(rcCallback);
1609 AssertRC(rc2);
1610 }
1611
1612 LogFlowFuncLeaveRC(rc);
1613 return rc; /* Tell the guest. */
1614}
1615
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette