VirtualBox

source: vbox/trunk/src/VBox/HostServices/DragAndDrop/service.cpp@ 76734

Last change on this file since 76734 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 45.9 KB
Line 
1/* $Id: service.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * Drag and Drop Service.
4 */
5
6/*
7 * Copyright (C) 2011-2019 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/** @page pg_svc_guest_control Drag and drop HGCM Service
19 *
20 * TODO
21 */
22
23
24/*********************************************************************************************************************************
25* Header Files *
26*********************************************************************************************************************************/
27#define LOG_GROUP LOG_GROUP_GUEST_DND
28#include <VBox/GuestHost/DragAndDrop.h>
29#include <VBox/GuestHost/DragAndDropDefs.h>
30#include <VBox/HostServices/Service.h>
31#include <VBox/HostServices/DragAndDropSvc.h>
32
33#include <VBox/err.h>
34
35#include <algorithm>
36#include <list>
37#include <map>
38
39#include "dndmanager.h"
40
41using namespace DragAndDropSvc;
42
43
44/*********************************************************************************************************************************
45* Service class declaration *
46*********************************************************************************************************************************/
47
48class DragAndDropClient : public HGCM::Client
49{
50public:
51
52 DragAndDropClient(uint32_t uClientID)
53 : HGCM::Client(uClientID)
54 {
55 RT_ZERO(m_SvcCtx);
56 }
57
58 virtual ~DragAndDropClient(void)
59 {
60 disconnect();
61 }
62
63public:
64
65 void disconnect(void);
66};
67
68/** Map holding pointers to drag and drop clients. Key is the (unique) HGCM client ID. */
69typedef std::map<uint32_t, DragAndDropClient*> DnDClientMap;
70
71/** Simple queue (list) which holds deferred (waiting) clients. */
72typedef std::list<uint32_t> DnDClientQueue;
73
74/**
75 * Specialized drag & drop service class.
76 */
77class DragAndDropService : public HGCM::AbstractService<DragAndDropService>
78{
79public:
80
81 explicit DragAndDropService(PVBOXHGCMSVCHELPERS pHelpers)
82 : HGCM::AbstractService<DragAndDropService>(pHelpers)
83 , m_pManager(NULL) {}
84
85protected:
86
87 int init(VBOXHGCMSVCFNTABLE *pTable);
88 int uninit(void);
89 int clientConnect(uint32_t u32ClientID, void *pvClient);
90 int clientDisconnect(uint32_t u32ClientID, void *pvClient);
91 void guestCall(VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID, void *pvClient, uint32_t u32Function, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
92 int hostCall(uint32_t u32Function, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
93
94 int modeSet(uint32_t u32Mode);
95 inline uint32_t modeGet(void) const { return m_u32Mode; };
96
97protected:
98
99 static DECLCALLBACK(int) progressCallback(uint32_t uStatus, uint32_t uPercentage, int rc, void *pvUser);
100
101protected:
102
103 /** Pointer to our DnD manager instance. */
104 DnDManager *m_pManager;
105 /** Map of all connected clients.
106 * The primary key is the (unique) client ID, the secondary value
107 * an allocated pointer to the DragAndDropClient class, managed
108 * by this service class. */
109 DnDClientMap m_clientMap;
110 /** List of all clients which are queued up (deferred return) and ready
111 * to process new commands. The key is the (unique) client ID. */
112 DnDClientQueue m_clientQueue;
113 /** Current drag and drop mode. */
114 uint32_t m_u32Mode;
115};
116
117
118/*********************************************************************************************************************************
119* Client implementation *
120*********************************************************************************************************************************/
121
122/**
123 * Called when the HGCM client disconnected on the guest side.
124 * This function takes care of the client's data cleanup and also lets the host
125 * know that the client has been disconnected.
126 *
127 */
128void DragAndDropClient::disconnect(void)
129{
130 LogFlowThisFunc(("uClient=%RU32\n", m_uClientID));
131
132 if (IsDeferred())
133 CompleteDeferred(VERR_INTERRUPTED);
134
135 /*
136 * Let the host know.
137 */
138 VBOXDNDCBDISCONNECTMSGDATA data;
139 RT_ZERO(data);
140 /** @todo Magic needed? */
141 /** @todo Add context ID. */
142
143 if (m_SvcCtx.pfnHostCallback)
144 {
145 int rc2 = m_SvcCtx.pfnHostCallback(m_SvcCtx.pvHostData, GUEST_DND_DISCONNECT, &data, sizeof(data));
146 if (RT_FAILURE(rc2))
147 LogFlowFunc(("Warning: Unable to notify host about client %RU32 disconnect, rc=%Rrc\n", m_uClientID, rc2));
148 /* Not fatal. */
149 }
150}
151
152
153/*********************************************************************************************************************************
154* Service class implementation *
155*********************************************************************************************************************************/
156
157int DragAndDropService::init(VBOXHGCMSVCFNTABLE *pTable)
158{
159 /* Register functions. */
160 pTable->pfnHostCall = svcHostCall;
161 pTable->pfnSaveState = NULL; /* The service is stateless, so the normal */
162 pTable->pfnLoadState = NULL; /* construction done before restoring suffices */
163 pTable->pfnRegisterExtension = svcRegisterExtension;
164 pTable->pfnNotify = NULL;
165
166 /* Drag'n drop mode is disabled by default. */
167 modeSet(VBOX_DRAG_AND_DROP_MODE_OFF);
168
169 int rc = VINF_SUCCESS;
170
171 try
172 {
173 m_pManager = new DnDManager(&DragAndDropService::progressCallback, this);
174 }
175 catch(std::bad_alloc &)
176 {
177 rc = VERR_NO_MEMORY;
178 }
179
180 LogFlowFuncLeaveRC(rc);
181 return rc;
182}
183
184int DragAndDropService::uninit(void)
185{
186 LogFlowFuncEnter();
187
188 if (m_pManager)
189 {
190 delete m_pManager;
191 m_pManager = NULL;
192 }
193
194 DnDClientMap::iterator itClient = m_clientMap.begin();
195 while (itClient != m_clientMap.end())
196 {
197 delete itClient->second;
198 m_clientMap.erase(itClient);
199 itClient = m_clientMap.begin();
200 }
201
202 LogFlowFuncLeave();
203 return VINF_SUCCESS;
204}
205
206int DragAndDropService::clientConnect(uint32_t u32ClientID, void *pvClient)
207{
208 RT_NOREF1(pvClient);
209 if (m_clientMap.size() >= UINT8_MAX) /* Don't allow too much clients at the same time. */
210 {
211 AssertMsgFailed(("Maximum number of clients reached\n"));
212 return VERR_MAX_PROCS_REACHED;
213 }
214
215 int rc = VINF_SUCCESS;
216
217 /*
218 * Add client to our client map.
219 */
220 if (m_clientMap.find(u32ClientID) != m_clientMap.end())
221 rc = VERR_ALREADY_EXISTS;
222
223 if (RT_SUCCESS(rc))
224 {
225 try
226 {
227 DragAndDropClient *pClient = new DragAndDropClient(u32ClientID);
228 pClient->SetSvcContext(m_SvcCtx);
229 m_clientMap[u32ClientID] = pClient;
230 }
231 catch(std::bad_alloc &)
232 {
233 rc = VERR_NO_MEMORY;
234 }
235
236 if (RT_SUCCESS(rc))
237 {
238 /*
239 * Reset the message queue as soon as a new clients connect
240 * to ensure that every client has the same state.
241 */
242 if (m_pManager)
243 m_pManager->Reset();
244 }
245 }
246
247 LogFlowFunc(("Client %RU32 connected, rc=%Rrc\n", u32ClientID, rc));
248 return rc;
249}
250
251int DragAndDropService::clientDisconnect(uint32_t u32ClientID, void *pvClient)
252{
253 RT_NOREF1(pvClient);
254
255 /* Client not found? Bail out early. */
256 DnDClientMap::iterator itClient = m_clientMap.find(u32ClientID);
257 if (itClient == m_clientMap.end())
258 return VERR_NOT_FOUND;
259
260 /*
261 * Remove from waiters queue.
262 */
263 m_clientQueue.remove(u32ClientID);
264
265 /*
266 * Remove from client map and deallocate.
267 */
268 AssertPtr(itClient->second);
269 delete itClient->second;
270
271 m_clientMap.erase(itClient);
272
273 LogFlowFunc(("Client %RU32 disconnected\n", u32ClientID));
274 return VINF_SUCCESS;
275}
276
277int DragAndDropService::modeSet(uint32_t u32Mode)
278{
279#ifndef VBOX_WITH_DRAG_AND_DROP_GH
280 if ( u32Mode == VBOX_DRAG_AND_DROP_MODE_GUEST_TO_HOST
281 || u32Mode == VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL)
282 {
283 m_u32Mode = VBOX_DRAG_AND_DROP_MODE_OFF;
284 return VERR_NOT_SUPPORTED;
285 }
286#endif
287
288 switch (u32Mode)
289 {
290 case VBOX_DRAG_AND_DROP_MODE_OFF:
291 case VBOX_DRAG_AND_DROP_MODE_HOST_TO_GUEST:
292 case VBOX_DRAG_AND_DROP_MODE_GUEST_TO_HOST:
293 case VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL:
294 m_u32Mode = u32Mode;
295 break;
296
297 default:
298 m_u32Mode = VBOX_DRAG_AND_DROP_MODE_OFF;
299 break;
300 }
301
302 return VINF_SUCCESS;
303}
304
305void DragAndDropService::guestCall(VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID,
306 void *pvClient, uint32_t u32Function,
307 uint32_t cParms, VBOXHGCMSVCPARM paParms[])
308{
309 RT_NOREF1(pvClient);
310 LogFlowFunc(("u32ClientID=%RU32, u32Function=%RU32, cParms=%RU32\n",
311 u32ClientID, u32Function, cParms));
312
313 /* Check if we've the right mode set. */
314 int rc = VERR_ACCESS_DENIED; /* Play safe. */
315 switch (u32Function)
316 {
317 case GUEST_DND_GET_NEXT_HOST_MSG:
318 {
319 if (modeGet() != VBOX_DRAG_AND_DROP_MODE_OFF)
320 {
321 rc = VINF_SUCCESS;
322 }
323 else
324 {
325 LogFlowFunc(("DnD disabled, deferring request\n"));
326 rc = VINF_HGCM_ASYNC_EXECUTE;
327 }
328 break;
329 }
330
331 /* New since protocol v2. */
332 case GUEST_DND_CONNECT:
333 {
334 /*
335 * Never block the initial connect call, as the clients do this when
336 * initializing and might get stuck if drag and drop is set to "disabled" at
337 * that time.
338 */
339 rc = VINF_SUCCESS;
340 break;
341 }
342 case GUEST_DND_HG_ACK_OP:
343 case GUEST_DND_HG_REQ_DATA:
344 case GUEST_DND_HG_EVT_PROGRESS:
345 {
346 if ( modeGet() == VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL
347 || modeGet() == VBOX_DRAG_AND_DROP_MODE_HOST_TO_GUEST)
348 {
349 rc = VINF_SUCCESS;
350 }
351 else
352 LogFlowFunc(("Host -> Guest DnD mode disabled, ignoring request\n"));
353 break;
354 }
355
356 case GUEST_DND_GH_ACK_PENDING:
357 case GUEST_DND_GH_SND_DATA_HDR:
358 case GUEST_DND_GH_SND_DATA:
359 case GUEST_DND_GH_SND_DIR:
360 case GUEST_DND_GH_SND_FILE_HDR:
361 case GUEST_DND_GH_SND_FILE_DATA:
362 case GUEST_DND_GH_EVT_ERROR:
363 {
364#ifdef VBOX_WITH_DRAG_AND_DROP_GH
365 if ( modeGet() == VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL
366 || modeGet() == VBOX_DRAG_AND_DROP_MODE_GUEST_TO_HOST)
367 {
368 rc = VINF_SUCCESS;
369 }
370 else
371#endif
372 LogFlowFunc(("Guest -> Host DnD mode disabled, ignoring request\n"));
373 break;
374 }
375
376 default:
377 /* Reach through to DnD manager. */
378 rc = VINF_SUCCESS;
379 break;
380 }
381
382#ifdef DEBUG_andy
383 LogFlowFunc(("Mode (%RU32) check rc=%Rrc\n", modeGet(), rc));
384#endif
385
386#define DO_HOST_CALLBACK(); \
387 if ( RT_SUCCESS(rc) \
388 && m_SvcCtx.pfnHostCallback) \
389 { \
390 rc = m_SvcCtx.pfnHostCallback(m_SvcCtx.pvHostData, u32Function, &data, sizeof(data)); \
391 }
392
393 /*
394 * Lookup client.
395 */
396 DragAndDropClient *pClient = NULL;
397
398 DnDClientMap::iterator itClient = m_clientMap.find(u32ClientID);
399 if (itClient != m_clientMap.end())
400 {
401 pClient = itClient->second;
402 AssertPtr(pClient);
403 }
404 else
405 {
406 LogFunc(("Client %RU32 was not found\n", u32ClientID));
407 rc = VERR_NOT_FOUND;
408 }
409
410 if (rc == VINF_SUCCESS) /* Note: rc might be VINF_HGCM_ASYNC_EXECUTE! */
411 {
412 LogFlowFunc(("Client %RU32: Protocol v%RU32\n", pClient->GetClientID(), pClient->GetProtocolVer()));
413
414 rc = VERR_INVALID_PARAMETER; /* Play safe. */
415
416 switch (u32Function)
417 {
418 /*
419 * Note: Older VBox versions with enabled DnD guest->host support (< 5.0)
420 * used the same message ID (300) for GUEST_DND_GET_NEXT_HOST_MSG and
421 * HOST_DND_GH_REQ_PENDING, which led this service returning
422 * VERR_INVALID_PARAMETER when the guest wanted to actually
423 * handle HOST_DND_GH_REQ_PENDING.
424 */
425 case GUEST_DND_GET_NEXT_HOST_MSG:
426 {
427 LogFlowFunc(("GUEST_DND_GET_NEXT_HOST_MSG\n"));
428 if (cParms == 3)
429 {
430 rc = m_pManager->GetNextMsgInfo(&paParms[0].u.uint32 /* uMsg */, &paParms[1].u.uint32 /* cParms */);
431 if (RT_FAILURE(rc)) /* No queued messages available? */
432 {
433 if (m_SvcCtx.pfnHostCallback) /* Try asking the host. */
434 {
435 VBOXDNDCBHGGETNEXTHOSTMSG data;
436 RT_ZERO(data);
437 data.hdr.uMagic = CB_MAGIC_DND_HG_GET_NEXT_HOST_MSG;
438 rc = m_SvcCtx.pfnHostCallback(m_SvcCtx.pvHostData, u32Function, &data, sizeof(data));
439 if (RT_SUCCESS(rc))
440 {
441 paParms[0].u.uint32 = data.uMsg; /* uMsg */
442 paParms[1].u.uint32 = data.cParms; /* cParms */
443 /* Note: paParms[2] was set by the guest as blocking flag. */
444 }
445 }
446 else /* No host callback in place, so drag and drop is not supported by the host. */
447 rc = VERR_NOT_SUPPORTED;
448
449 if (RT_FAILURE(rc))
450 rc = m_pManager->GetNextMsg(u32Function, cParms, paParms);
451
452 /* Some error occurred or no (new) messages available? */
453 if (RT_FAILURE(rc))
454 {
455 uint32_t fFlags = 0;
456 int rc2 = HGCMSvcGetU32(&paParms[2], &fFlags);
457 if ( RT_SUCCESS(rc2)
458 && fFlags) /* Blocking flag set? */
459 {
460 /* Defer client returning. */
461 rc = VINF_HGCM_ASYNC_EXECUTE;
462 }
463 else
464 rc = VERR_INVALID_PARAMETER;
465
466 LogFlowFunc(("Message queue is empty, returning %Rrc to guest\n", rc));
467 }
468 }
469 }
470 break;
471 }
472 case GUEST_DND_CONNECT:
473 {
474 LogFlowFunc(("GUEST_DND_CONNECT\n"));
475 if (cParms >= 2)
476 {
477 const uint8_t idxProto = cParms >= 3 ? 1 : 0;
478
479 VBOXDNDCBCONNECTMSGDATA data;
480 RT_ZERO(data);
481 data.hdr.uMagic = CB_MAGIC_DND_CONNECT;
482 if (cParms >= 3)
483 rc = HGCMSvcGetU32(&paParms[0], &data.hdr.uContextID);
484 else /* Older protocols don't have a context ID. */
485 rc = VINF_SUCCESS;
486 if (RT_SUCCESS(rc))
487 rc = HGCMSvcGetU32(&paParms[idxProto], &data.uProtocol);
488 if (RT_SUCCESS(rc))
489 rc = HGCMSvcGetU32(&paParms[idxProto + 1], &data.uFlags);
490 if (RT_SUCCESS(rc))
491 pClient->SetProtocolVer(data.uProtocol);
492 if (RT_SUCCESS(rc))
493 {
494 LogFlowFunc(("Client %RU32 is now using protocol v%RU32\n", pClient->GetClientID(), pClient->GetProtocolVer()));
495 DO_HOST_CALLBACK();
496 }
497 }
498 break;
499 }
500 case GUEST_DND_HG_ACK_OP:
501 {
502 LogFlowFunc(("GUEST_DND_HG_ACK_OP\n"));
503
504 VBOXDNDCBHGACKOPDATA data;
505 RT_ZERO(data);
506 data.hdr.uMagic = CB_MAGIC_DND_HG_ACK_OP;
507
508 switch (pClient->GetProtocolVer())
509 {
510 case 3:
511 {
512 if (cParms == 2)
513 {
514 rc = HGCMSvcGetU32(&paParms[0], &data.hdr.uContextID);
515 if (RT_SUCCESS(rc))
516 rc = HGCMSvcGetU32(&paParms[1], &data.uAction); /* Get drop action. */
517 }
518 break;
519 }
520
521 case 2:
522 default:
523 {
524 if (cParms == 1)
525 rc = HGCMSvcGetU32(&paParms[0], &data.uAction); /* Get drop action. */
526 break;
527 }
528 }
529
530 DO_HOST_CALLBACK();
531 break;
532 }
533 case GUEST_DND_HG_REQ_DATA:
534 {
535 LogFlowFunc(("GUEST_DND_HG_REQ_DATA\n"));
536
537 VBOXDNDCBHGREQDATADATA data;
538 RT_ZERO(data);
539 data.hdr.uMagic = CB_MAGIC_DND_HG_REQ_DATA;
540
541 switch (pClient->GetProtocolVer())
542 {
543 case 3:
544 {
545 if (cParms == 3)
546 {
547 rc = HGCMSvcGetU32(&paParms[0], &data.hdr.uContextID);
548 if (RT_SUCCESS(rc))
549 rc = HGCMSvcGetPv(&paParms[1], (void **)&data.pszFormat, &data.cbFormat);
550 if (RT_SUCCESS(rc))
551 rc = HGCMSvcGetU32(&paParms[2], &data.cbFormat);
552 }
553 break;
554 }
555
556 case 2:
557 default:
558 {
559 if (cParms == 1)
560 rc = HGCMSvcGetPv(&paParms[0], (void**)&data.pszFormat, &data.cbFormat);
561 break;
562 }
563 }
564
565 DO_HOST_CALLBACK();
566 break;
567 }
568 case GUEST_DND_HG_EVT_PROGRESS:
569 {
570 LogFlowFunc(("GUEST_DND_HG_EVT_PROGRESS\n"));
571
572 VBOXDNDCBHGEVTPROGRESSDATA data;
573 RT_ZERO(data);
574 data.hdr.uMagic = CB_MAGIC_DND_HG_EVT_PROGRESS;
575
576 switch (pClient->GetProtocolVer())
577 {
578 case 3:
579 {
580 if (cParms == 4)
581 {
582 rc = HGCMSvcGetU32(&paParms[0], &data.uStatus);
583 if (RT_SUCCESS(rc))
584 rc = HGCMSvcGetU32(&paParms[1], &data.uStatus);
585 if (RT_SUCCESS(rc))
586 rc = HGCMSvcGetU32(&paParms[2], &data.uPercentage);
587 if (RT_SUCCESS(rc))
588 rc = HGCMSvcGetU32(&paParms[3], &data.rc);
589 }
590 break;
591 }
592
593 case 2:
594 default:
595 {
596 if (cParms == 3)
597 {
598 rc = HGCMSvcGetU32(&paParms[0], &data.uStatus);
599 if (RT_SUCCESS(rc))
600 rc = HGCMSvcGetU32(&paParms[1], &data.uPercentage);
601 if (RT_SUCCESS(rc))
602 rc = HGCMSvcGetU32(&paParms[2], &data.rc);
603 }
604 break;
605 }
606 }
607
608 DO_HOST_CALLBACK();
609 break;
610 }
611#ifdef VBOX_WITH_DRAG_AND_DROP_GH
612 case GUEST_DND_GH_ACK_PENDING:
613 {
614 LogFlowFunc(("GUEST_DND_GH_ACK_PENDING\n"));
615
616 VBOXDNDCBGHACKPENDINGDATA data;
617 RT_ZERO(data);
618 data.hdr.uMagic = CB_MAGIC_DND_GH_ACK_PENDING;
619
620 switch (pClient->GetProtocolVer())
621 {
622 case 3:
623 {
624 if (cParms == 5)
625 {
626 rc = HGCMSvcGetU32(&paParms[0], &data.hdr.uContextID);
627 if (RT_SUCCESS(rc))
628 rc = HGCMSvcGetU32(&paParms[1], &data.uDefAction);
629 if (RT_SUCCESS(rc))
630 rc = HGCMSvcGetU32(&paParms[2], &data.uAllActions);
631 if (RT_SUCCESS(rc))
632 rc = HGCMSvcGetPv(&paParms[3], (void**)&data.pszFormat, &data.cbFormat);
633 if (RT_SUCCESS(rc))
634 rc = HGCMSvcGetU32(&paParms[4], &data.cbFormat);
635 }
636 break;
637 }
638
639 case 2:
640 default:
641 {
642 if (cParms == 3)
643 {
644 rc = HGCMSvcGetU32(&paParms[0], &data.uDefAction);
645 if (RT_SUCCESS(rc))
646 rc = HGCMSvcGetU32(&paParms[1], &data.uAllActions);
647 if (RT_SUCCESS(rc))
648 rc = HGCMSvcGetPv(&paParms[2], (void**)&data.pszFormat, &data.cbFormat);
649 }
650 break;
651 }
652 }
653
654 DO_HOST_CALLBACK();
655 break;
656 }
657 /* New since protocol v3. */
658 case GUEST_DND_GH_SND_DATA_HDR:
659 {
660 LogFlowFunc(("GUEST_DND_GH_SND_DATA_HDR\n"));
661 if (cParms == 12)
662 {
663 VBOXDNDCBSNDDATAHDRDATA data;
664 RT_ZERO(data);
665 data.hdr.uMagic = CB_MAGIC_DND_GH_SND_DATA_HDR;
666 rc = HGCMSvcGetU32(&paParms[0], &data.hdr.uContextID);
667 if (RT_SUCCESS(rc))
668 rc = HGCMSvcGetU32(&paParms[1], &data.data.uFlags);
669 if (RT_SUCCESS(rc))
670 rc = HGCMSvcGetU32(&paParms[2], &data.data.uScreenId);
671 if (RT_SUCCESS(rc))
672 rc = HGCMSvcGetU64(&paParms[3], &data.data.cbTotal);
673 if (RT_SUCCESS(rc))
674 rc = HGCMSvcGetU32(&paParms[4], &data.data.cbMeta);
675 if (RT_SUCCESS(rc))
676 rc = HGCMSvcGetPv(&paParms[5], &data.data.pvMetaFmt, &data.data.cbMetaFmt);
677 if (RT_SUCCESS(rc))
678 rc = HGCMSvcGetU32(&paParms[6], &data.data.cbMetaFmt);
679 if (RT_SUCCESS(rc))
680 rc = HGCMSvcGetU64(&paParms[7], &data.data.cObjects);
681 if (RT_SUCCESS(rc))
682 rc = HGCMSvcGetU32(&paParms[8], &data.data.enmCompression);
683 if (RT_SUCCESS(rc))
684 rc = HGCMSvcGetU32(&paParms[9], (uint32_t *)&data.data.enmChecksumType);
685 if (RT_SUCCESS(rc))
686 rc = HGCMSvcGetPv(&paParms[10], &data.data.pvChecksum, &data.data.cbChecksum);
687 if (RT_SUCCESS(rc))
688 rc = HGCMSvcGetU32(&paParms[11], &data.data.cbChecksum);
689
690 LogFlowFunc(("fFlags=0x%x, cbTotalSize=%RU64, cObj=%RU64\n",
691 data.data.uFlags, data.data.cbTotal, data.data.cObjects));
692 DO_HOST_CALLBACK();
693 }
694 break;
695 }
696 case GUEST_DND_GH_SND_DATA:
697 {
698 LogFlowFunc(("GUEST_DND_GH_SND_DATA\n"));
699 switch (pClient->GetProtocolVer())
700 {
701 case 3:
702 {
703 if (cParms == 5)
704 {
705 VBOXDNDCBSNDDATADATA data;
706 RT_ZERO(data);
707 data.hdr.uMagic = CB_MAGIC_DND_GH_SND_DATA;
708 rc = HGCMSvcGetU32(&paParms[0], &data.hdr.uContextID);
709 if (RT_SUCCESS(rc))
710 rc = HGCMSvcGetPv(&paParms[1], (void**)&data.data.u.v3.pvData, &data.data.u.v3.cbData);
711 if (RT_SUCCESS(rc))
712 rc = HGCMSvcGetU32(&paParms[2], &data.data.u.v3.cbData);
713 if (RT_SUCCESS(rc))
714 rc = HGCMSvcGetPv(&paParms[3], (void**)&data.data.u.v3.pvChecksum, &data.data.u.v3.cbChecksum);
715 if (RT_SUCCESS(rc))
716 rc = HGCMSvcGetU32(&paParms[4], &data.data.u.v3.cbChecksum);
717 DO_HOST_CALLBACK();
718 }
719 break;
720 }
721
722 case 2:
723 default:
724 {
725 if (cParms == 2)
726 {
727 VBOXDNDCBSNDDATADATA data;
728 RT_ZERO(data);
729 data.hdr.uMagic = CB_MAGIC_DND_GH_SND_DATA;
730 rc = HGCMSvcGetPv(&paParms[0], (void**)&data.data.u.v1.pvData, &data.data.u.v1.cbData);
731 if (RT_SUCCESS(rc))
732 rc = HGCMSvcGetU32(&paParms[1], &data.data.u.v1.cbTotalSize);
733 DO_HOST_CALLBACK();
734 }
735 break;
736 }
737 }
738 break;
739 }
740 case GUEST_DND_GH_SND_DIR:
741 {
742 LogFlowFunc(("GUEST_DND_GH_SND_DIR\n"));
743
744 VBOXDNDCBSNDDIRDATA data;
745 RT_ZERO(data);
746 data.hdr.uMagic = CB_MAGIC_DND_GH_SND_DIR;
747
748 switch (pClient->GetProtocolVer())
749 {
750 case 3:
751 {
752 if (cParms == 4)
753 {
754 rc = HGCMSvcGetU32(&paParms[0], &data.hdr.uContextID);
755 if (RT_SUCCESS(rc))
756 rc = HGCMSvcGetPv(&paParms[1], (void**)&data.pszPath, &data.cbPath);
757 if (RT_SUCCESS(rc))
758 rc = HGCMSvcGetU32(&paParms[2], &data.cbPath);
759 if (RT_SUCCESS(rc))
760 rc = HGCMSvcGetU32(&paParms[3], &data.fMode);
761 }
762 break;
763 }
764
765 case 2:
766 default:
767 {
768 if (cParms == 3)
769 {
770 rc = HGCMSvcGetPv(&paParms[0], (void**)&data.pszPath, &data.cbPath);
771 if (RT_SUCCESS(rc))
772 rc = HGCMSvcGetU32(&paParms[1], &data.cbPath);
773 if (RT_SUCCESS(rc))
774 rc = HGCMSvcGetU32(&paParms[2], &data.fMode);
775 }
776 break;
777 }
778 }
779
780 DO_HOST_CALLBACK();
781 break;
782 }
783 /* New since protocol v2 (>= VBox 5.0). */
784 case GUEST_DND_GH_SND_FILE_HDR:
785 {
786 LogFlowFunc(("GUEST_DND_GH_SND_FILE_HDR\n"));
787 if (cParms == 6)
788 {
789 VBOXDNDCBSNDFILEHDRDATA data;
790 RT_ZERO(data);
791 data.hdr.uMagic = CB_MAGIC_DND_GH_SND_FILE_HDR;
792
793 rc = HGCMSvcGetU32(&paParms[0], &data.hdr.uContextID);
794 if (RT_SUCCESS(rc))
795 rc = HGCMSvcGetPv(&paParms[1], (void**)&data.pszFilePath, &data.cbFilePath);
796 if (RT_SUCCESS(rc))
797 rc = HGCMSvcGetU32(&paParms[2], &data.cbFilePath);
798 if (RT_SUCCESS(rc))
799 rc = HGCMSvcGetU32(&paParms[3], &data.fFlags);
800 if (RT_SUCCESS(rc))
801 rc = HGCMSvcGetU32(&paParms[4], &data.fMode);
802 if (RT_SUCCESS(rc))
803 rc = HGCMSvcGetU64(&paParms[5], &data.cbSize);
804
805 LogFlowFunc(("pszPath=%s, cbPath=%RU32, fMode=0x%x, cbSize=%RU64\n",
806 data.pszFilePath, data.cbFilePath, data.fMode, data.cbSize));
807 DO_HOST_CALLBACK();
808 }
809 break;
810 }
811 case GUEST_DND_GH_SND_FILE_DATA:
812 {
813 LogFlowFunc(("GUEST_DND_GH_SND_FILE_DATA\n"));
814
815 switch (pClient->GetProtocolVer())
816 {
817 /* Protocol v3 adds (optional) checksums. */
818 case 3:
819 {
820 if (cParms == 5)
821 {
822 VBOXDNDCBSNDFILEDATADATA data;
823 RT_ZERO(data);
824 data.hdr.uMagic = CB_MAGIC_DND_GH_SND_FILE_DATA;
825
826 rc = HGCMSvcGetU32(&paParms[0], &data.hdr.uContextID);
827 if (RT_SUCCESS(rc))
828 rc = HGCMSvcGetPv(&paParms[1], (void**)&data.pvData, &data.cbData);
829 if (RT_SUCCESS(rc))
830 rc = HGCMSvcGetU32(&paParms[2], &data.cbData);
831 if (RT_SUCCESS(rc))
832 rc = HGCMSvcGetPv(&paParms[3], (void**)&data.u.v3.pvChecksum, &data.u.v3.cbChecksum);
833 if (RT_SUCCESS(rc))
834 rc = HGCMSvcGetU32(&paParms[4], &data.u.v3.cbChecksum);
835
836 LogFlowFunc(("pvData=0x%p, cbData=%RU32\n", data.pvData, data.cbData));
837 DO_HOST_CALLBACK();
838 }
839 break;
840 }
841 /* Protocol v2 only sends the next data chunks to reduce traffic. */
842 case 2:
843 {
844 if (cParms == 3)
845 {
846 VBOXDNDCBSNDFILEDATADATA data;
847 RT_ZERO(data);
848 data.hdr.uMagic = CB_MAGIC_DND_GH_SND_FILE_DATA;
849 rc = HGCMSvcGetU32(&paParms[0], &data.hdr.uContextID);
850 if (RT_SUCCESS(rc))
851 rc = HGCMSvcGetPv(&paParms[1], (void**)&data.pvData, &data.cbData);
852 if (RT_SUCCESS(rc))
853 rc = HGCMSvcGetU32(&paParms[2], &data.cbData);
854
855 LogFlowFunc(("cbData=%RU32, pvData=0x%p\n", data.cbData, data.pvData));
856 DO_HOST_CALLBACK();
857 }
858 break;
859 }
860 /* Protocol v1 sends the file path and attributes for every file chunk (!). */
861 default:
862 {
863 if (cParms == 5)
864 {
865 VBOXDNDCBSNDFILEDATADATA data;
866 RT_ZERO(data);
867 data.hdr.uMagic = CB_MAGIC_DND_GH_SND_FILE_DATA;
868 uint32_t cTmp;
869 rc = HGCMSvcGetPv(&paParms[0], (void**)&data.u.v1.pszFilePath, &cTmp);
870 if (RT_SUCCESS(rc))
871 rc = HGCMSvcGetU32(&paParms[1], &data.u.v1.cbFilePath);
872 if (RT_SUCCESS(rc))
873 rc = HGCMSvcGetPv(&paParms[2], (void**)&data.pvData, &cTmp);
874 if (RT_SUCCESS(rc))
875 rc = HGCMSvcGetU32(&paParms[3], &data.cbData);
876 if (RT_SUCCESS(rc))
877 rc = HGCMSvcGetU32(&paParms[4], &data.u.v1.fMode);
878
879 LogFlowFunc(("pszFilePath=%s, cbData=%RU32, pvData=0x%p, fMode=0x%x\n",
880 data.u.v1.pszFilePath, data.cbData, data.pvData, data.u.v1.fMode));
881 DO_HOST_CALLBACK();
882 }
883 break;
884 }
885 }
886 break;
887 }
888 case GUEST_DND_GH_EVT_ERROR:
889 {
890 LogFlowFunc(("GUEST_DND_GH_EVT_ERROR\n"));
891
892 VBOXDNDCBEVTERRORDATA data;
893 RT_ZERO(data);
894 data.hdr.uMagic = CB_MAGIC_DND_GH_EVT_ERROR;
895
896 switch (pClient->GetProtocolVer())
897 {
898 case 3:
899 {
900 if (cParms == 2)
901 {
902 rc = HGCMSvcGetU32(&paParms[0], &data.hdr.uContextID);
903 if (RT_SUCCESS(rc))
904 {
905 uint32_t rcOp;
906 rc = HGCMSvcGetU32(&paParms[1], &rcOp);
907 if (RT_SUCCESS(rc))
908 data.rc = rcOp;
909 }
910 }
911 break;
912 }
913
914 case 2:
915 default:
916 {
917 if (cParms == 1)
918 {
919 uint32_t rcOp;
920 rc = HGCMSvcGetU32(&paParms[0], &rcOp);
921 if (RT_SUCCESS(rc))
922 data.rc = (int32_t)rcOp;
923 }
924 break;
925 }
926 }
927
928 DO_HOST_CALLBACK();
929 break;
930 }
931#endif /* VBOX_WITH_DRAG_AND_DROP_GH */
932
933 /*
934 * Note: This is a fire-and-forget message, as the host should
935 * not rely on an answer from the guest side in order to
936 * properly cancel the operation.
937 */
938 case HOST_DND_HG_EVT_CANCEL:
939 {
940 LogFlowFunc(("HOST_DND_HG_EVT_CANCEL\n"));
941
942 VBOXDNDCBEVTERRORDATA data;
943 RT_ZERO(data);
944 data.hdr.uMagic = CB_MAGIC_DND_GH_EVT_ERROR;
945
946 switch (pClient->GetProtocolVer())
947 {
948 case 3:
949 {
950 /* Protocol v3+ at least requires the context ID. */
951 if (cParms == 1)
952 rc = HGCMSvcGetU32(&paParms[0], &data.hdr.uContextID);
953
954 break;
955 }
956
957 default:
958 break;
959 }
960
961 /* Tell the host that the guest has cancelled the operation. */
962 data.rc = VERR_CANCELLED;
963
964 DO_HOST_CALLBACK();
965
966 /* Note: If the host is not prepared for handling the cancelling reply
967 * from the guest, don't report this back to the guest. */
968 if (RT_FAILURE(rc))
969 rc = VINF_SUCCESS;
970 break;
971 }
972
973 default:
974 {
975 /* All other messages are handled by the DnD manager. */
976 rc = m_pManager->GetNextMsg(u32Function, cParms, paParms);
977 if (rc == VERR_NO_DATA) /* Manager has no new messsages? Try asking the host. */
978 {
979 if (m_SvcCtx.pfnHostCallback)
980 {
981 VBOXDNDCBHGGETNEXTHOSTMSGDATA data;
982 RT_ZERO(data);
983
984 data.hdr.uMagic = VBOX_DND_CB_MAGIC_MAKE(0 /* uFn */, 0 /* uVer */);
985
986 data.uMsg = u32Function;
987 data.cParms = cParms;
988 data.paParms = paParms;
989
990 rc = m_SvcCtx.pfnHostCallback(m_SvcCtx.pvHostData, u32Function,
991 &data, sizeof(data));
992 if (RT_SUCCESS(rc))
993 {
994 cParms = data.cParms;
995 paParms = data.paParms;
996 }
997 else
998 {
999 /*
1000 * In case the guest is too fast asking for the next message
1001 * and the host did not supply it yet, just defer the client's
1002 * return until a response from the host available.
1003 */
1004 LogFlowFunc(("No new messages from the host (yet), deferring request: %Rrc\n", rc));
1005 rc = VINF_HGCM_ASYNC_EXECUTE;
1006 }
1007 }
1008 else /* No host callback in place, so drag and drop is not supported by the host. */
1009 rc = VERR_NOT_SUPPORTED;
1010 }
1011 break;
1012 }
1013 }
1014 }
1015
1016 /*
1017 * If async execution is requested, we didn't notify the guest yet about
1018 * completion. The client is queued into the waiters list and will be
1019 * notified as soon as a new event is available.
1020 */
1021 if (rc == VINF_HGCM_ASYNC_EXECUTE)
1022 {
1023 try
1024 {
1025 AssertPtr(pClient);
1026 pClient->SetDeferred(callHandle, u32Function, cParms, paParms);
1027 m_clientQueue.push_back(u32ClientID);
1028 }
1029 catch (std::bad_alloc &)
1030 {
1031 rc = VERR_NO_MEMORY;
1032 /* Don't report to guest. */
1033 }
1034 }
1035 else if (pClient)
1036 pClient->Complete(callHandle, rc);
1037 else
1038 {
1039 AssertMsgFailed(("Guest call failed with %Rrc\n", rc));
1040 rc = VERR_NOT_IMPLEMENTED;
1041 }
1042
1043 LogFlowFunc(("Returning rc=%Rrc\n", rc));
1044}
1045
1046int DragAndDropService::hostCall(uint32_t u32Function,
1047 uint32_t cParms, VBOXHGCMSVCPARM paParms[])
1048{
1049 LogFlowFunc(("u32Function=%RU32, cParms=%RU32, cClients=%zu, cQueue=%zu\n",
1050 u32Function, cParms, m_clientMap.size(), m_clientQueue.size()));
1051
1052 int rc;
1053
1054 do
1055 {
1056 bool fSendToGuest = false; /* Whether to send the message down to the guest side or not. */
1057
1058 switch (u32Function)
1059 {
1060 case HOST_DND_SET_MODE:
1061 {
1062 if (cParms != 1)
1063 rc = VERR_INVALID_PARAMETER;
1064 else if (paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT)
1065 rc = VERR_INVALID_PARAMETER;
1066 else
1067 rc = modeSet(paParms[0].u.uint32);
1068 break;
1069 }
1070
1071 case HOST_DND_HG_EVT_ENTER:
1072 {
1073 /* Reset the message queue as a new DnD operation just began. */
1074 m_pManager->Reset();
1075
1076 fSendToGuest = true;
1077 rc = VINF_SUCCESS;
1078 break;
1079 }
1080
1081 case HOST_DND_HG_EVT_CANCEL:
1082 {
1083 LogFlowFunc(("Cancelling all waiting clients ...\n"));
1084
1085 /* Reset the message queue as the host cancelled the whole operation. */
1086 m_pManager->Reset();
1087
1088 /*
1089 * Wake up all deferred clients and tell them to process
1090 * the cancelling message next.
1091 */
1092 DnDClientQueue::iterator itQueue = m_clientQueue.begin();
1093 while (itQueue != m_clientQueue.end())
1094 {
1095 DnDClientMap::iterator itClient = m_clientMap.find(*itQueue);
1096 Assert(itClient != m_clientMap.end());
1097
1098 DragAndDropClient *pClient = itClient->second;
1099 AssertPtr(pClient);
1100
1101 int rc2 = pClient->SetDeferredMsgInfo(HOST_DND_HG_EVT_CANCEL,
1102 /* Protocol v3+ also contains the context ID. */
1103 pClient->GetProtocolVer() >= 3 ? 1 : 0);
1104 pClient->CompleteDeferred(rc2);
1105
1106 m_clientQueue.erase(itQueue);
1107 itQueue = m_clientQueue.begin();
1108 }
1109
1110 Assert(m_clientQueue.empty());
1111
1112 /* Tell the host that everything went well. */
1113 rc = VINF_SUCCESS;
1114 break;
1115 }
1116
1117 default:
1118 {
1119 fSendToGuest = true;
1120 rc = VINF_SUCCESS;
1121 break;
1122 }
1123 }
1124
1125 if (fSendToGuest)
1126 {
1127 if (modeGet() == VBOX_DRAG_AND_DROP_MODE_OFF)
1128 {
1129 /* Tell the host that a wrong drag'n drop mode is set. */
1130 rc = VERR_ACCESS_DENIED;
1131 break;
1132 }
1133
1134 if (m_clientMap.empty()) /* At least one client on the guest connected? */
1135 {
1136 /*
1137 * Tell the host that the guest does not support drag'n drop.
1138 * This might happen due to not installed Guest Additions or
1139 * not running VBoxTray/VBoxClient.
1140 */
1141 rc = VERR_NOT_SUPPORTED;
1142 break;
1143 }
1144
1145 rc = m_pManager->AddMsg(u32Function, cParms, paParms, true /* fAppend */);
1146 if (RT_FAILURE(rc))
1147 {
1148 AssertMsgFailed(("Adding new message of type=%RU32 failed with rc=%Rrc\n", u32Function, rc));
1149 break;
1150 }
1151
1152 /* Any clients in our queue ready for processing the next command? */
1153 if (m_clientQueue.empty())
1154 {
1155 LogFlowFunc(("All clients (%zu) busy -- delaying execution\n", m_clientMap.size()));
1156 break;
1157 }
1158
1159 uint32_t uClientNext = m_clientQueue.front();
1160 DnDClientMap::iterator itClientNext = m_clientMap.find(uClientNext);
1161 Assert(itClientNext != m_clientMap.end());
1162
1163 DragAndDropClient *pClient = itClientNext->second;
1164 AssertPtr(pClient);
1165
1166 /*
1167 * Check if this was a request for getting the next host
1168 * message. If so, return the message ID and the parameter
1169 * count. The message itself has to be queued.
1170 */
1171 uint32_t uMsgClient = pClient->GetMsgType();
1172
1173 uint32_t uMsgNext = 0;
1174 uint32_t cParmsNext = 0;
1175 int rcNext = m_pManager->GetNextMsgInfo(&uMsgNext, &cParmsNext);
1176
1177 LogFlowFunc(("uMsgClient=%RU32, uMsgNext=%RU32, cParmsNext=%RU32, rcNext=%Rrc\n",
1178 uMsgClient, uMsgNext, cParmsNext, rcNext));
1179
1180 if (RT_SUCCESS(rcNext))
1181 {
1182 if (uMsgClient == GUEST_DND_GET_NEXT_HOST_MSG)
1183 {
1184 rc = pClient->SetDeferredMsgInfo(uMsgNext, cParmsNext);
1185
1186 /* Note: Report the current rc back to the guest. */
1187 pClient->CompleteDeferred(rc);
1188 }
1189 /*
1190 * Does the message the client is waiting for match the message
1191 * next in the queue? Process it right away then.
1192 */
1193 else if (uMsgClient == uMsgNext)
1194 {
1195 rc = m_pManager->GetNextMsg(u32Function, cParms, paParms);
1196
1197 /* Note: Report the current rc back to the guest. */
1198 pClient->CompleteDeferred(rc);
1199 }
1200 else /* Should not happen; cancel the operation on the guest. */
1201 {
1202 LogFunc(("Client ID=%RU32 in wrong state with uMsg=%RU32 (next message in queue: %RU32), cancelling\n",
1203 pClient->GetClientID(), uMsgClient, uMsgNext));
1204
1205 pClient->CompleteDeferred(VERR_CANCELLED);
1206 }
1207
1208 m_clientQueue.pop_front();
1209 }
1210
1211 } /* fSendToGuest */
1212
1213 } while (0); /* To use breaks. */
1214
1215 LogFlowFuncLeaveRC(rc);
1216 return rc;
1217}
1218
1219DECLCALLBACK(int) DragAndDropService::progressCallback(uint32_t uStatus, uint32_t uPercentage, int rc, void *pvUser)
1220{
1221 AssertPtrReturn(pvUser, VERR_INVALID_POINTER);
1222
1223 DragAndDropService *pSelf = static_cast<DragAndDropService *>(pvUser);
1224 AssertPtr(pSelf);
1225
1226 if (pSelf->m_SvcCtx.pfnHostCallback)
1227 {
1228 LogFlowFunc(("GUEST_DND_HG_EVT_PROGRESS: uStatus=%RU32, uPercentage=%RU32, rc=%Rrc\n",
1229 uStatus, uPercentage, rc));
1230
1231 VBOXDNDCBHGEVTPROGRESSDATA data;
1232 data.hdr.uMagic = CB_MAGIC_DND_HG_EVT_PROGRESS;
1233 data.uPercentage = RT_MIN(uPercentage, 100);
1234 data.uStatus = uStatus;
1235 data.rc = rc; /** @todo uin32_t vs. int. */
1236
1237 return pSelf->m_SvcCtx.pfnHostCallback(pSelf->m_SvcCtx.pvHostData,
1238 GUEST_DND_HG_EVT_PROGRESS,
1239 &data, sizeof(data));
1240 }
1241
1242 return VINF_SUCCESS;
1243}
1244
1245/**
1246 * @copydoc VBOXHGCMSVCLOAD
1247 */
1248extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad(VBOXHGCMSVCFNTABLE *pTable)
1249{
1250 return DragAndDropService::svcLoad(pTable);
1251}
1252
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