VirtualBox

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

Last change on this file since 61441 was 59856, checked in by vboxsync, 9 years ago

DnD: HostServices/DragAndDrop/service.cpp: Cancel deferred client returns if they're in a wrong state when issuing a new host command.

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