VirtualBox

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

Last change on this file since 55437 was 55422, checked in by vboxsync, 10 years ago

DnD: Protocol overhaul with versioning added which now can communicate with Main.

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