VirtualBox

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

Last change on this file since 85416 was 85407, checked in by vboxsync, 4 years ago

DnD/HostService: Condensed guestCall() parameter validation; makes use of the ASSERT_GUEST macros now.

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