VirtualBox

source: vbox/trunk/src/VBox/HostServices/DragAndDrop/VBoxDragAndDropSvc.cpp@ 78359

Last change on this file since 78359 was 76964, checked in by vboxsync, 6 years ago

DnD/HostServices: Renamed service.cpp to VBoxDragAndDropSvc.cpp.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 44.8 KB
Line 
1/* $Id: VBoxDragAndDropSvc.cpp 76964 2019-01-24 08:41:38Z 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 default:
934 {
935 /* All other messages are handled by the DnD manager. */
936 rc = m_pManager->GetNextMsg(u32Function, cParms, paParms);
937 if (rc == VERR_NO_DATA) /* Manager has no new messsages? Try asking the host. */
938 {
939 if (m_SvcCtx.pfnHostCallback)
940 {
941 VBOXDNDCBHGGETNEXTHOSTMSGDATA data;
942 RT_ZERO(data);
943
944 data.hdr.uMagic = VBOX_DND_CB_MAGIC_MAKE(0 /* uFn */, 0 /* uVer */);
945
946 data.uMsg = u32Function;
947 data.cParms = cParms;
948 data.paParms = paParms;
949
950 rc = m_SvcCtx.pfnHostCallback(m_SvcCtx.pvHostData, u32Function,
951 &data, sizeof(data));
952 if (RT_SUCCESS(rc))
953 {
954 cParms = data.cParms;
955 paParms = data.paParms;
956 }
957 else
958 {
959 /*
960 * In case the guest is too fast asking for the next message
961 * and the host did not supply it yet, just defer the client's
962 * return until a response from the host available.
963 */
964 LogFlowFunc(("No new messages from the host (yet), deferring request: %Rrc\n", rc));
965 rc = VINF_HGCM_ASYNC_EXECUTE;
966 }
967 }
968 else /* No host callback in place, so drag and drop is not supported by the host. */
969 rc = VERR_NOT_SUPPORTED;
970 }
971 break;
972 }
973 }
974 }
975
976 /*
977 * If async execution is requested, we didn't notify the guest yet about
978 * completion. The client is queued into the waiters list and will be
979 * notified as soon as a new event is available.
980 */
981 if (rc == VINF_HGCM_ASYNC_EXECUTE)
982 {
983 try
984 {
985 AssertPtr(pClient);
986 pClient->SetDeferred(callHandle, u32Function, cParms, paParms);
987 m_clientQueue.push_back(u32ClientID);
988 }
989 catch (std::bad_alloc &)
990 {
991 rc = VERR_NO_MEMORY;
992 /* Don't report to guest. */
993 }
994 }
995 else if (pClient)
996 pClient->Complete(callHandle, rc);
997 else
998 {
999 AssertMsgFailed(("Guest call failed with %Rrc\n", rc));
1000 rc = VERR_NOT_IMPLEMENTED;
1001 }
1002
1003 LogFlowFunc(("Returning rc=%Rrc\n", rc));
1004}
1005
1006int DragAndDropService::hostCall(uint32_t u32Function,
1007 uint32_t cParms, VBOXHGCMSVCPARM paParms[])
1008{
1009 LogFlowFunc(("u32Function=%RU32, cParms=%RU32, cClients=%zu, cQueue=%zu\n",
1010 u32Function, cParms, m_clientMap.size(), m_clientQueue.size()));
1011
1012 int rc;
1013
1014 do
1015 {
1016 bool fSendToGuest = false; /* Whether to send the message down to the guest side or not. */
1017
1018 switch (u32Function)
1019 {
1020 case HOST_DND_SET_MODE:
1021 {
1022 if (cParms != 1)
1023 rc = VERR_INVALID_PARAMETER;
1024 else if (paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT)
1025 rc = VERR_INVALID_PARAMETER;
1026 else
1027 rc = modeSet(paParms[0].u.uint32);
1028 break;
1029 }
1030
1031 case HOST_DND_CANCEL:
1032 {
1033 LogFlowFunc(("Cancelling all waiting clients ...\n"));
1034
1035 /* Reset the message queue as the host cancelled the whole operation. */
1036 m_pManager->Reset();
1037
1038 rc = m_pManager->AddMsg(u32Function, cParms, paParms, true /* fAppend */);
1039 if (RT_FAILURE(rc))
1040 {
1041 AssertMsgFailed(("Adding new message of type=%RU32 failed with rc=%Rrc\n", u32Function, rc));
1042 break;
1043 }
1044
1045 /*
1046 * Wake up all deferred clients and tell them to process
1047 * the cancelling message next.
1048 */
1049 DnDClientQueue::iterator itQueue = m_clientQueue.begin();
1050 while (itQueue != m_clientQueue.end())
1051 {
1052 DnDClientMap::iterator itClient = m_clientMap.find(*itQueue);
1053 Assert(itClient != m_clientMap.end());
1054
1055 DragAndDropClient *pClient = itClient->second;
1056 AssertPtr(pClient);
1057
1058 int rc2 = pClient->SetDeferredMsgInfo(HOST_DND_CANCEL,
1059 /* Protocol v3+ also contains the context ID. */
1060 pClient->GetProtocolVer() >= 3 ? 1 : 0);
1061 pClient->CompleteDeferred(rc2);
1062
1063 m_clientQueue.erase(itQueue);
1064 itQueue = m_clientQueue.begin();
1065 }
1066
1067 Assert(m_clientQueue.empty());
1068
1069 /* Tell the host that everything went well. */
1070 rc = VINF_SUCCESS;
1071 break;
1072 }
1073
1074 case HOST_DND_HG_EVT_ENTER:
1075 {
1076 /* Reset the message queue as a new DnD operation just began. */
1077 m_pManager->Reset();
1078
1079 fSendToGuest = true;
1080 rc = VINF_SUCCESS;
1081 break;
1082 }
1083
1084 default:
1085 {
1086 fSendToGuest = true;
1087 rc = VINF_SUCCESS;
1088 break;
1089 }
1090 }
1091
1092 if (fSendToGuest)
1093 {
1094 if (modeGet() == VBOX_DRAG_AND_DROP_MODE_OFF)
1095 {
1096 /* Tell the host that a wrong drag'n drop mode is set. */
1097 rc = VERR_ACCESS_DENIED;
1098 break;
1099 }
1100
1101 if (m_clientMap.empty()) /* At least one client on the guest connected? */
1102 {
1103 /*
1104 * Tell the host that the guest does not support drag'n drop.
1105 * This might happen due to not installed Guest Additions or
1106 * not running VBoxTray/VBoxClient.
1107 */
1108 rc = VERR_NOT_SUPPORTED;
1109 break;
1110 }
1111
1112 rc = m_pManager->AddMsg(u32Function, cParms, paParms, true /* fAppend */);
1113 if (RT_FAILURE(rc))
1114 {
1115 AssertMsgFailed(("Adding new message of type=%RU32 failed with rc=%Rrc\n", u32Function, rc));
1116 break;
1117 }
1118
1119 /* Any clients in our queue ready for processing the next command? */
1120 if (m_clientQueue.empty())
1121 {
1122 LogFlowFunc(("All clients (%zu) busy -- delaying execution\n", m_clientMap.size()));
1123 break;
1124 }
1125
1126 uint32_t uClientNext = m_clientQueue.front();
1127 DnDClientMap::iterator itClientNext = m_clientMap.find(uClientNext);
1128 Assert(itClientNext != m_clientMap.end());
1129
1130 DragAndDropClient *pClient = itClientNext->second;
1131 AssertPtr(pClient);
1132
1133 /*
1134 * Check if this was a request for getting the next host
1135 * message. If so, return the message ID and the parameter
1136 * count. The message itself has to be queued.
1137 */
1138 uint32_t uMsgClient = pClient->GetMsgType();
1139
1140 uint32_t uMsgNext = 0;
1141 uint32_t cParmsNext = 0;
1142 int rcNext = m_pManager->GetNextMsgInfo(&uMsgNext, &cParmsNext);
1143
1144 LogFlowFunc(("uMsgClient=%RU32, uMsgNext=%RU32, cParmsNext=%RU32, rcNext=%Rrc\n",
1145 uMsgClient, uMsgNext, cParmsNext, rcNext));
1146
1147 if (RT_SUCCESS(rcNext))
1148 {
1149 if (uMsgClient == GUEST_DND_GET_NEXT_HOST_MSG)
1150 {
1151 rc = pClient->SetDeferredMsgInfo(uMsgNext, cParmsNext);
1152
1153 /* Note: Report the current rc back to the guest. */
1154 pClient->CompleteDeferred(rc);
1155 }
1156 /*
1157 * Does the message the client is waiting for match the message
1158 * next in the queue? Process it right away then.
1159 */
1160 else if (uMsgClient == uMsgNext)
1161 {
1162 rc = m_pManager->GetNextMsg(u32Function, cParms, paParms);
1163
1164 /* Note: Report the current rc back to the guest. */
1165 pClient->CompleteDeferred(rc);
1166 }
1167 else /* Should not happen; cancel the operation on the guest. */
1168 {
1169 LogFunc(("Client ID=%RU32 in wrong state with uMsg=%RU32 (next message in queue: %RU32), cancelling\n",
1170 pClient->GetClientID(), uMsgClient, uMsgNext));
1171
1172 pClient->CompleteDeferred(VERR_CANCELLED);
1173 }
1174
1175 m_clientQueue.pop_front();
1176 }
1177
1178 } /* fSendToGuest */
1179
1180 } while (0); /* To use breaks. */
1181
1182 LogFlowFuncLeaveRC(rc);
1183 return rc;
1184}
1185
1186DECLCALLBACK(int) DragAndDropService::progressCallback(uint32_t uStatus, uint32_t uPercentage, int rc, void *pvUser)
1187{
1188 AssertPtrReturn(pvUser, VERR_INVALID_POINTER);
1189
1190 DragAndDropService *pSelf = static_cast<DragAndDropService *>(pvUser);
1191 AssertPtr(pSelf);
1192
1193 if (pSelf->m_SvcCtx.pfnHostCallback)
1194 {
1195 LogFlowFunc(("GUEST_DND_HG_EVT_PROGRESS: uStatus=%RU32, uPercentage=%RU32, rc=%Rrc\n",
1196 uStatus, uPercentage, rc));
1197
1198 VBOXDNDCBHGEVTPROGRESSDATA data;
1199 data.hdr.uMagic = CB_MAGIC_DND_HG_EVT_PROGRESS;
1200 data.uPercentage = RT_MIN(uPercentage, 100);
1201 data.uStatus = uStatus;
1202 data.rc = rc; /** @todo uin32_t vs. int. */
1203
1204 return pSelf->m_SvcCtx.pfnHostCallback(pSelf->m_SvcCtx.pvHostData,
1205 GUEST_DND_HG_EVT_PROGRESS,
1206 &data, sizeof(data));
1207 }
1208
1209 return VINF_SUCCESS;
1210}
1211
1212/**
1213 * @copydoc VBOXHGCMSVCLOAD
1214 */
1215extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad(VBOXHGCMSVCFNTABLE *pTable)
1216{
1217 return DragAndDropService::svcLoad(pTable);
1218}
1219
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