VirtualBox

source: vbox/trunk/src/VBox/Main/include/GuestDnDPrivate.h@ 55594

Last change on this file since 55594 was 55571, checked in by vboxsync, 10 years ago

DnD: Simplified cancellation logic.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.7 KB
Line 
1/* $Id: GuestDnDPrivate.h 55571 2015-04-30 17:04:37Z vboxsync $ */
2/** @file
3 * Private guest drag and drop code, used by GuestDnDTarget +
4 * GuestDnDSource.
5 */
6
7/*
8 * Copyright (C) 2011-2015 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19#ifndef ____H_GUESTDNDPRIVATE
20#define ____H_GUESTDNDPRIVATE
21
22#include <iprt/dir.h>
23#include <iprt/file.h>
24
25#include "VBox/hgcmsvc.h" /* For PVBOXHGCMSVCPARM. */
26#include "VBox/GuestHost/DragAndDrop.h"
27
28/**
29 * Forward prototype declarations.
30 */
31class Guest;
32class GuestDnDBase;
33class GuestDnDResponse;
34class GuestDnDSource;
35class GuestDnDTarget;
36class Progress;
37
38class GuestDnDCallbackEvent
39{
40public:
41
42 GuestDnDCallbackEvent(void)
43 : mSemEvent(NIL_RTSEMEVENT)
44 , mRc(VINF_SUCCESS) { }
45
46 virtual ~GuestDnDCallbackEvent(void);
47
48public:
49
50 int Reset(void);
51
52 int Notify(int rc);
53
54 int Result(void) const { return mRc; }
55
56 int Wait(RTMSINTERVAL msTimeout);
57
58protected:
59
60 /** Event semaphore to notify on error/completion. */
61 RTSEMEVENT mSemEvent;
62 /** Callback result. */
63 int mRc;
64};
65
66/**
67 * Structure for keeping the (URI) data to be sent/received.
68 */
69typedef struct GuestDnDData
70{
71 GuestDnDData(void)
72 : cbToProcess(0)
73 , cbProcessed(0) { }
74
75 void Reset(void)
76 {
77 vecData.clear();
78 cbToProcess = 0;
79 cbProcessed = 0;
80 }
81
82 /** Array (vector) of guest DnD data. This might be an URI list, according
83 * to the format being set. */
84 std::vector<BYTE> vecData;
85 /** Overall size (in bytes) of data to send. */
86 uint64_t cbToProcess;
87 /** Overall size (in bytes) of processed file data. */
88 uint64_t cbProcessed;
89
90} GuestDnDData;
91
92/**
93 * Structure for keeping around URI (list) data.
94 */
95typedef struct GuestDnDURIData
96{
97 GuestDnDURIData(void)
98 : pvScratchBuf(NULL)
99 , cbScratchBuf(0) { }
100
101 virtual ~GuestDnDURIData(void)
102 {
103 Reset();
104 }
105
106 void Reset(void)
107 {
108 strDropDir = "";
109 lstURI.Clear();
110 lstDirs.clear();
111 lstFiles.clear();
112#if 0 /* Currently the scratch buffer will be maintained elswewhere. */
113 if (pvScratchBuf)
114 {
115 RTMemFree(pvScratchBuf);
116 pvScratchBuf = NULL;
117 }
118 cbScratchBuf = 0;
119#else
120 pvScratchBuf = NULL;
121 cbScratchBuf = 0;
122#endif
123 }
124
125 int Rollback(void)
126 {
127 if (strDropDir.isEmpty())
128 return VINF_SUCCESS;
129
130 int rc = VINF_SUCCESS;
131 int rc2;
132
133 /* Rollback by removing any stuff created.
134 * Note: Only remove empty directories, never ever delete
135 * anything recursive here! Steam (tm) knows best ... :-) */
136 for (size_t i = 0; i < lstFiles.size(); i++)
137 {
138 rc2 = RTFileDelete(lstFiles.at(i).c_str());
139 if (RT_SUCCESS(rc))
140 rc = rc2;
141 }
142
143 for (size_t i = 0; i < lstDirs.size(); i++)
144 {
145 rc2 = RTDirRemove(lstDirs.at(i).c_str());
146 if (RT_SUCCESS(rc))
147 rc = rc2;
148 }
149
150 rc2 = RTDirRemove(strDropDir.c_str());
151 if (RT_SUCCESS(rc))
152 rc = rc2;
153
154 return rc;
155 }
156
157 /** Temporary drop directory on the host where to
158 * put the files sent from the guest. */
159 com::Utf8Str strDropDir;
160 /** (Non-recursive) List of root URI objects to receive. */
161 DnDURIList lstURI;
162 /** Current object to receive. */
163 DnDURIObject objURI;
164 /** List for holding created directories in the case of a rollback. */
165 RTCList<RTCString> lstDirs;
166 /** List for holding created files in the case of a rollback. */
167 RTCList<RTCString> lstFiles;
168 /** Pointer to an optional scratch buffer to use for
169 * doing the actual chunk transfers. */
170 void *pvScratchBuf;
171 /** Size (in bytes) of scratch buffer. */
172 size_t cbScratchBuf;
173
174} GuestDnDURIData;
175
176/**
177 * Context structure for sending data to the guest.
178 */
179typedef struct SENDDATACTX
180{
181 /** Pointer to guest target class this context belongs to. */
182 GuestDnDTarget *mpTarget;
183 /** Pointer to guest response class this context belongs to. */
184 GuestDnDResponse *mpResp;
185 /** Flag indicating whether a file transfer is active and
186 * initiated by the host. */
187 bool mIsActive;
188 /** Target (VM) screen ID. */
189 uint32_t mScreenID;
190 /** Drag'n drop format to send. */
191 com::Utf8Str mFormat;
192 /** Drag'n drop data to send.
193 * This can be arbitrary data or an URI list. */
194 GuestDnDData mData;
195 /** URI data structure. */
196 GuestDnDURIData mURI;
197 /** Callback event to use. */
198 GuestDnDCallbackEvent mCallback;
199
200} SENDDATACTX, *PSENDDATACTX;
201
202/**
203 * Context structure for receiving data from the guest.
204 */
205typedef struct RECVDATACTX
206{
207 /** Pointer to guest source class this context belongs to. */
208 GuestDnDSource *mpSource;
209 /** Pointer to guest response class this context belongs to. */
210 GuestDnDResponse *mpResp;
211 /** Flag indicating whether a file transfer is active and
212 * initiated by the host. */
213 bool mIsActive;
214 /** Drag'n drop format to send. */
215 com::Utf8Str mFormat;
216 /** Desired drop action to perform on the host.
217 * Needed to tell the guest if data has to be
218 * deleted e.g. when moving instead of copying. */
219 uint32_t mAction;
220 /** Drag'n drop received from the guest.
221 * This can be arbitrary data or an URI list. */
222 GuestDnDData mData;
223 /** URI data structure. */
224 GuestDnDURIData mURI;
225 /** Callback event to use. */
226 GuestDnDCallbackEvent mCallback;
227
228} RECVDATACTX, *PRECVDATACTX;
229
230/**
231 * Simple structure for a buffered guest DnD message.
232 */
233class GuestDnDMsg
234{
235public:
236
237 GuestDnDMsg(void)
238 : uMsg(0)
239 , cParms(0)
240 , cParmsAlloc(0)
241 , paParms(NULL) { }
242
243 virtual ~GuestDnDMsg(void)
244 {
245 if (paParms)
246 {
247 /* Remove deep copies. */
248 for (uint32_t i = 0; i < cParms; i++)
249 {
250 if ( paParms[i].type == VBOX_HGCM_SVC_PARM_PTR
251 && paParms[i].u.pointer.addr)
252 {
253 RTMemFree(paParms[i].u.pointer.addr);
254 }
255 }
256
257 delete paParms;
258 }
259 }
260
261public:
262
263 PVBOXHGCMSVCPARM getNextParam(void)
264 {
265 if (cParms >= cParmsAlloc)
266 {
267 paParms = (PVBOXHGCMSVCPARM)RTMemRealloc(paParms, (cParmsAlloc + 4) * sizeof(VBOXHGCMSVCPARM));
268 if (!paParms)
269 throw VERR_NO_MEMORY;
270 RT_BZERO(&paParms[cParmsAlloc], 4 * sizeof(VBOXHGCMSVCPARM));
271 cParmsAlloc += 4;
272 }
273
274 return &paParms[cParms++];
275 }
276
277 uint32_t getCount(void) const { return cParms; }
278 PVBOXHGCMSVCPARM getParms(void) const { return paParms; }
279 uint32_t getType(void) const { return uMsg; }
280
281 int setNextPointer(void *pvBuf, uint32_t cbBuf)
282 {
283 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
284 AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
285
286 PVBOXHGCMSVCPARM pParm = getNextParam();
287 if (!pParm)
288 return VERR_NO_MEMORY;
289
290 void *pvTmp = RTMemDup(pvBuf, cbBuf);
291 if (!pvTmp)
292 {
293 RTMemFree(pParm);
294 return VERR_NO_MEMORY;
295 }
296
297 pParm->setPointer(pvTmp, cbBuf);
298 return VINF_SUCCESS;
299 }
300
301 int setNextString(const char *pszString)
302 {
303 PVBOXHGCMSVCPARM pParm = getNextParam();
304 if (!pParm)
305 return VERR_NO_MEMORY;
306
307 char *pszTemp = RTStrDup(pszString);
308 if (!pszTemp)
309 {
310 RTMemFree(pParm);
311 return VERR_NO_MEMORY;
312 }
313
314 pParm->setString(pszTemp);
315 return VINF_SUCCESS;
316 }
317
318 int setNextUInt32(uint32_t u32Val)
319 {
320 PVBOXHGCMSVCPARM pParm = getNextParam();
321 if (!pParm)
322 return VERR_NO_MEMORY;
323
324 pParm->setUInt32(u32Val);
325 return VINF_SUCCESS;
326 }
327
328 int setNextUInt64(uint64_t u64Val)
329 {
330 PVBOXHGCMSVCPARM pParm = getNextParam();
331 if (!pParm)
332 return VERR_NO_MEMORY;
333
334 pParm->setUInt64(u64Val);
335 return VINF_SUCCESS;
336 }
337
338 void setType(uint32_t uMsgType) { uMsg = uMsgType; }
339
340protected:
341
342 /** Message type. */
343 uint32_t uMsg;
344 /** Message parameters. */
345 uint32_t cParms;
346 /** Size of array. */
347 uint32_t cParmsAlloc;
348 /** Array of HGCM parameters */
349 PVBOXHGCMSVCPARM paParms;
350};
351
352/** Guest DnD callback function definition. */
353typedef DECLCALLBACKPTR(int, PFNGUESTDNDCALLBACK) (uint32_t uMsg, void *pvParms, size_t cbParms, void *pvUser);
354
355/**
356 * Structure for keeping a guest DnD callback.
357 * Each callback can handle one HGCM message, however, multiple HGCM messages can be registered
358 * to the same callback (function).
359 */
360typedef struct GuestDnDCallback
361{
362 GuestDnDCallback(void)
363 : uMessgage(0)
364 , pfnCallback(NULL)
365 , pvUser(NULL) { }
366
367 GuestDnDCallback(PFNGUESTDNDCALLBACK pvCB, uint32_t uMsg, void *pvUsr = NULL)
368 : uMessgage(uMsg)
369 , pfnCallback(pvCB)
370 , pvUser(pvUsr) { }
371
372 /** The HGCM message ID to handle. */
373 uint32_t uMessgage;
374 /** Pointer to callback function. */
375 PFNGUESTDNDCALLBACK pfnCallback;
376 /** Pointer to user-supplied data. */
377 void *pvUser;
378
379} GuestDnDCallback;
380
381/** Contains registered callback pointers for specific HGCM message types. */
382typedef std::map<uint32_t, GuestDnDCallback> GuestDnDCallbackMap;
383
384class GuestDnDResponse
385{
386
387public:
388
389 GuestDnDResponse(const ComObjPtr<Guest>& pGuest);
390 virtual ~GuestDnDResponse(void);
391
392public:
393
394 int notifyAboutGuestResponse(void) const;
395 int waitForGuestResponse(RTMSINTERVAL msTimeout = 500) const;
396
397 void setAllActions(uint32_t a) { m_allActions = a; }
398 uint32_t allActions(void) const { return m_allActions; }
399
400 void setDefAction(uint32_t a) { m_defAction = a; }
401 uint32_t defAction(void) const { return m_defAction; }
402
403 void setFormat(const Utf8Str &strFormat) { m_strFormat = strFormat; }
404 Utf8Str format(void) const { return m_strFormat; }
405
406 void reset(void);
407
408 bool isProgressCanceled(void) const;
409 int setCallback(uint32_t uMsg, PFNGUESTDNDCALLBACK pfnCallback, void *pvUser = NULL);
410 int setProgress(unsigned uPercentage, uint32_t uState, int rcOp = VINF_SUCCESS);
411 HRESULT resetProgress(const ComObjPtr<Guest>& pParent);
412 HRESULT queryProgressTo(IProgress **ppProgress);
413
414public:
415
416 /** @name HGCM callback handling.
417 @{ */
418 int onDispatch(uint32_t u32Function, void *pvParms, uint32_t cbParms);
419 /** @} */
420
421public:
422
423 Utf8Str errorToString(const ComObjPtr<Guest>& pGuest, int guestRc);
424
425protected:
426
427 /** Pointer to context this class is tied to. */
428 void *m_pvCtx;
429 RTSEMEVENT m_EventSem;
430 uint32_t m_defAction;
431 uint32_t m_allActions;
432 Utf8Str m_strFormat;
433
434 /** Pointer to IGuest parent object. */
435 ComObjPtr<Guest> m_parent;
436 /** Pointer to associated progress object. Optional. */
437 ComObjPtr<Progress> m_progress;
438 /** Callback map. */
439 GuestDnDCallbackMap m_mapCallbacks;
440};
441
442/**
443 * Private singleton class for the guest's DnD
444 * implementation. Can't be instanciated directly, only via
445 * the factory pattern.
446 */
447class GuestDnD
448{
449public:
450
451 static GuestDnD *createInstance(const ComObjPtr<Guest>& pGuest)
452 {
453 Assert(NULL == GuestDnD::s_pInstance);
454 GuestDnD::s_pInstance = new GuestDnD(pGuest);
455 return GuestDnD::s_pInstance;
456 }
457
458 static void destroyInstance(void)
459 {
460 if (GuestDnD::s_pInstance)
461 {
462 delete GuestDnD::s_pInstance;
463 GuestDnD::s_pInstance = NULL;
464 }
465 }
466
467 static inline GuestDnD *getInstance(void)
468 {
469 AssertPtr(GuestDnD::s_pInstance);
470 return GuestDnD::s_pInstance;
471 }
472
473protected:
474
475 GuestDnD(const ComObjPtr<Guest>& pGuest);
476 virtual ~GuestDnD(void);
477
478public:
479
480 /** @name Public helper functions.
481 * @{ */
482 int adjustScreenCoordinates(ULONG uScreenId, ULONG *puX, ULONG *puY) const;
483 int hostCall(uint32_t u32Function, uint32_t cParms, PVBOXHGCMSVCPARM paParms) const;
484 GuestDnDResponse *response(void) { return m_pResponse; }
485 std::vector<com::Utf8Str> defaultFormats(void) const { return m_strDefaultFormats; }
486 /** @} */
487
488public:
489
490 /** @name Static low-level HGCM callback handler.
491 * @{ */
492 static DECLCALLBACK(int) notifyDnDDispatcher(void *pvExtension, uint32_t u32Function, void *pvParms, uint32_t cbParms);
493 /** @} */
494
495 /** @name Static helper methods.
496 * @{ */
497 static com::Utf8Str toFormatString(const std::vector<com::Utf8Str> &lstSupportedFormats, const std::vector<com::Utf8Str> &lstFormats);
498 static void toFormatVector(const std::vector<com::Utf8Str> &lstSupportedFormats, const com::Utf8Str &strFormats, std::vector<com::Utf8Str> &vecformats);
499 static DnDAction_T toMainAction(uint32_t uAction);
500 static void toMainActions(uint32_t uActions, std::vector<DnDAction_T> &vecActions);
501 static uint32_t toHGCMAction(DnDAction_T enmAction);
502 static void toHGCMActions(DnDAction_T enmDefAction, uint32_t *puDefAction, const std::vector<DnDAction_T> vecAllowedActions, uint32_t *puAllowedActions);
503 /** @} */
504
505protected:
506
507 /** @name Singleton properties.
508 * @{ */
509 /** List of supported default MIME/Content-type formats. */
510 std::vector<com::Utf8Str> m_strDefaultFormats;
511 /** Pointer to guest implementation. */
512 const ComObjPtr<Guest> m_pGuest;
513 /** The current (last) response from the guest. At the
514 * moment we only support only response a time (ARQ-style). */
515 GuestDnDResponse *m_pResponse;
516 /** @} */
517
518private:
519
520 /** Staic pointer to singleton instance. */
521 static GuestDnD *s_pInstance;
522};
523
524/** Access to the GuestDnD's singleton instance. */
525#define GuestDnDInst() GuestDnD::getInstance()
526
527/** List of pointers to guest DnD Messages. */
528typedef std::list<GuestDnDMsg *> GuestDnDMsgList;
529
530/**
531 * IDnDBase class implementation for sharing code between
532 * IGuestDnDSource and IGuestDnDTarget implementation.
533 */
534class GuestDnDBase
535{
536protected:
537
538 GuestDnDBase(void);
539
540protected:
541
542 /** Shared (internal) IDnDBase method implementations.
543 * @{ */
544 HRESULT i_isFormatSupported(const com::Utf8Str &aFormat, BOOL *aSupported);
545 HRESULT i_getFormats(std::vector<com::Utf8Str> &aFormats);
546 HRESULT i_addFormats(const std::vector<com::Utf8Str> &aFormats);
547 HRESULT i_removeFormats(const std::vector<com::Utf8Str> &aFormats);
548
549 HRESULT i_getProtocolVersion(ULONG *puVersion);
550 /** @} */
551
552protected:
553
554 int getProtocolVersion(uint32_t *puVersion);
555
556 /** @name Functions for handling a simple host HGCM message queue.
557 * @{ */
558 int msgQueueAdd(GuestDnDMsg *pMsg);
559 GuestDnDMsg *msgQueueGetNext(void);
560 void msgQueueRemoveNext(void);
561 void msgQueueClear(void);
562 /** @} */
563
564 int sendCancel(void);
565 int waitForEvent(RTMSINTERVAL msTimeout, GuestDnDCallbackEvent &Event, GuestDnDResponse *pResp);
566
567protected:
568
569 /** @name Public attributes (through getters/setters).
570 * @{ */
571 /** Pointer to guest implementation. */
572 const ComObjPtr<Guest> m_pGuest;
573 /** List of supported MIME/Content-type formats. */
574 std::vector<com::Utf8Str> m_strFormats;
575 /** @} */
576
577 struct
578 {
579 /** Flag indicating whether a drop operation currently
580 * is in progress or not. */
581 bool mfTransferIsPending;
582 /** The DnD protocol version to use, depending on the
583 * installed Guest Additions. */
584 uint32_t mProtocolVersion;
585 /** Outgoing message queue. */
586 GuestDnDMsgList mListOutgoing;
587 } mDataBase;
588};
589#endif /* ____H_GUESTDNDPRIVATE */
590
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