VirtualBox

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

Last change on this file since 50265 was 50265, checked in by vboxsync, 11 years ago

DnD: First working implementation for Windows guest->host support; still work in progress. As for now only pure text data can be dragged over.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.9 KB
Line 
1/* $Id: service.cpp 50265 2014-01-29 11:12:44Z vboxsync $ */
2/** @file
3 * Drag and Drop Service.
4 */
5
6/*
7 * Copyright (C) 2011-2014 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 Guest Control HGCM Service
19 *
20 * This service acts as a proxy for handling and buffering host command requests
21 * and clients on the guest. It tries to be as transparent as possible to let
22 * the guest (client) and host side do their protocol handling as desired.
23 *
24 * The following terms are used:
25 * - Host: A host process (e.g. VBoxManage or another tool utilizing the Main API)
26 * which wants to control something on the guest.
27 * - Client: A client (e.g. VBoxService) running inside the guest OS waiting for
28 * new host commands to perform. There can be multiple clients connected
29 * to a service. A client is represented by its HGCM client ID.
30 * - Context ID: An (almost) unique ID automatically generated on the host (Main API)
31 * to not only distinguish clients but individual requests. Because
32 * the host does not know anything about connected clients it needs
33 * an indicator which it can refer to later. This context ID gets
34 * internally bound by the service to a client which actually processes
35 * the command in order to have a relationship between client<->context ID(s).
36 *
37 * The host can trigger commands which get buffered by the service (with full HGCM
38 * parameter info). As soon as a client connects (or is ready to do some new work)
39 * it gets a buffered host command to process it. This command then will be immediately
40 * removed from the command list. If there are ready clients but no new commands to be
41 * processed, these clients will be set into a deferred state (that is being blocked
42 * to return until a new command is available).
43 *
44 * If a client needs to inform the host that something happened, it can send a
45 * message to a low level HGCM callback registered in Main. This callback contains
46 * the actual data as well as the context ID to let the host do the next necessary
47 * steps for this context. This context ID makes it possible to wait for an event
48 * inside the host's Main API function (like starting a process on the guest and
49 * wait for getting its PID returned by the client) as well as cancelling blocking
50 * host calls in order the client terminated/crashed (HGCM detects disconnected
51 * clients and reports it to this service's callback).
52 */
53
54/******************************************************************************
55 * Header Files *
56 ******************************************************************************/
57#ifdef LOG_GROUP
58 #undef LOG_GROUP
59#endif
60#define LOG_GROUP LOG_GROUP_GUEST_DND
61
62#include "dndmanager.h"
63
64/******************************************************************************
65 * Service class declaration *
66 ******************************************************************************/
67
68/**
69 * Specialized drag & drop service class.
70 */
71class DragAndDropService: public HGCM::AbstractService<DragAndDropService>
72{
73public:
74 explicit DragAndDropService(PVBOXHGCMSVCHELPERS pHelpers)
75 : HGCM::AbstractService<DragAndDropService>(pHelpers)
76 , m_pManager(0)
77 , m_cClients(0)
78 {}
79
80protected:
81 /* HGCM service implementation */
82 int init(VBOXHGCMSVCFNTABLE *pTable);
83 int uninit();
84 int clientConnect(uint32_t u32ClientID, void *pvClient);
85 int clientDisconnect(uint32_t u32ClientID, void *pvClient);
86 void guestCall(VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID, void *pvClient, uint32_t u32Function, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
87 int hostCall(uint32_t u32Function, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
88
89 static DECLCALLBACK(int) progressCallback(uint32_t uPercentage, uint32_t uState, int rc, void *pvUser);
90 int hostMessage(uint32_t u32Function, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
91 void modeSet(uint32_t u32Mode);
92 inline uint32_t modeGet() { return m_u32Mode; };
93
94 DnDManager *m_pManager;
95
96 uint32_t m_cClients;
97 RTCList<HGCM::Client*> m_clientQueue;
98 uint32_t m_u32Mode;
99};
100
101/******************************************************************************
102 * Service class implementation *
103 ******************************************************************************/
104
105int DragAndDropService::init(VBOXHGCMSVCFNTABLE *pTable)
106{
107 /* Register functions. */
108 pTable->pfnHostCall = svcHostCall;
109 pTable->pfnSaveState = NULL; /* The service is stateless, so the normal */
110 pTable->pfnLoadState = NULL; /* construction done before restoring suffices */
111 pTable->pfnRegisterExtension = svcRegisterExtension;
112 modeSet(VBOX_DRAG_AND_DROP_MODE_OFF);
113
114 m_pManager = new DnDManager(&DragAndDropService::progressCallback, this);
115
116 return VINF_SUCCESS;
117}
118
119int DragAndDropService::uninit()
120{
121 delete m_pManager;
122
123 return VINF_SUCCESS;
124}
125
126int DragAndDropService::clientConnect(uint32_t u32ClientID, void *pvClient)
127{
128 LogFlowFunc(("New client (%ld) connected\n", u32ClientID));
129 if (m_cClients < UINT32_MAX)
130 m_cClients++;
131 else
132 AssertMsgFailed(("Maximum number of clients reached\n"));
133 return VINF_SUCCESS;
134}
135
136int DragAndDropService::clientDisconnect(uint32_t u32ClientID, void *pvClient)
137{
138 /* Remove all waiters with this clientId. */
139 for (size_t i = 0; i < m_clientQueue.size(); )
140 {
141 HGCM::Client *pClient = m_clientQueue.at(i);
142 if (pClient->clientId() == u32ClientID)
143 {
144 m_pHelpers->pfnCallComplete(pClient->handle(), VERR_INTERRUPTED);
145 m_clientQueue.removeAt(i);
146 delete pClient;
147 }
148 else
149 i++;
150 }
151
152 return VINF_SUCCESS;
153}
154
155void DragAndDropService::modeSet(uint32_t u32Mode)
156{
157 switch (u32Mode)
158 {
159 case VBOX_DRAG_AND_DROP_MODE_OFF:
160 case VBOX_DRAG_AND_DROP_MODE_HOST_TO_GUEST:
161 case VBOX_DRAG_AND_DROP_MODE_GUEST_TO_HOST:
162 case VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL:
163 m_u32Mode = u32Mode;
164 break;
165
166 default:
167 m_u32Mode = VBOX_DRAG_AND_DROP_MODE_OFF;
168 }
169}
170
171void DragAndDropService::guestCall(VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID, void *pvClient, uint32_t u32Function, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
172{
173 LogFlowFunc(("u32ClientID=%RU32, u32Function=%RU32, cParms=%RU32\n",
174 u32ClientID, u32Function, cParms));
175
176 int rc = VINF_SUCCESS;
177 switch (u32Function)
178 {
179 /* Note: Older VBox versions with enabled DnD guest->host support (< 4.4)
180 * used the same ID (300) for GUEST_DND_GET_NEXT_HOST_MSG and
181 * HOST_DND_GH_REQ_PENDING, which led this service returning
182 * VERR_INVALID_PARAMETER when the guest wanted to actually
183 * handle HOST_DND_GH_REQ_PENDING. */
184 case DragAndDropSvc::GUEST_DND_GET_NEXT_HOST_MSG:
185 {
186 LogFlowFunc(("GUEST_DND_GET_NEXT_HOST_MSG\n"));
187 if ( cParms != 3
188 || paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* message */
189 || paParms[1].type != VBOX_HGCM_SVC_PARM_32BIT /* parameter count */
190 || paParms[2].type != VBOX_HGCM_SVC_PARM_32BIT /* blocking */)
191 rc = VERR_INVALID_PARAMETER;
192 else
193 {
194 rc = m_pManager->nextMessageInfo(&paParms[0].u.uint32, &paParms[1].u.uint32);
195 if ( RT_FAILURE(rc)
196 && paParms[2].u.uint32) /* Blocking? */
197 {
198 m_clientQueue.append(new HGCM::Client(u32ClientID, callHandle, u32Function, cParms, paParms));
199 rc = VINF_HGCM_ASYNC_EXECUTE;
200 }
201 }
202 break;
203 }
204 case DragAndDropSvc::GUEST_DND_HG_ACK_OP:
205 {
206 LogFlowFunc(("GUEST_DND_HG_ACK_OP\n"));
207 if ( modeGet() != VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL
208 && modeGet() != VBOX_DRAG_AND_DROP_MODE_HOST_TO_GUEST)
209 {
210 LogFlowFunc(("=> ignoring!\n"));
211 break;
212 }
213
214 if ( cParms != 1
215 || paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* action */)
216 rc = VERR_INVALID_PARAMETER;
217 else
218 {
219 DragAndDropSvc::VBOXDNDCBHGACKOPDATA data;
220 data.hdr.u32Magic = DragAndDropSvc::CB_MAGIC_DND_HG_ACK_OP;
221 paParms[0].getUInt32(&data.uAction);
222 if (m_pfnHostCallback)
223 rc = m_pfnHostCallback(m_pvHostData, u32Function, &data, sizeof(data));
224// m_pHelpers->pfnCallComplete(callHandle, rc);
225 }
226 break;
227 }
228 case DragAndDropSvc::GUEST_DND_HG_REQ_DATA:
229 {
230 LogFlowFunc(("GUEST_DND_HG_REQ_DATA\n"));
231 if ( modeGet() != VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL
232 && modeGet() != VBOX_DRAG_AND_DROP_MODE_HOST_TO_GUEST)
233 {
234 LogFlowFunc(("=> ignoring!\n"));
235 break;
236 }
237
238 if ( cParms != 1
239 || paParms[0].type != VBOX_HGCM_SVC_PARM_PTR /* format */)
240 rc = VERR_INVALID_PARAMETER;
241 else
242 {
243 DragAndDropSvc::VBOXDNDCBHGREQDATADATA data;
244 data.hdr.u32Magic = DragAndDropSvc::CB_MAGIC_DND_HG_REQ_DATA;
245 uint32_t cTmp;
246 paParms[0].getPointer((void**)&data.pszFormat, &cTmp);
247 if (m_pfnHostCallback)
248 rc = m_pfnHostCallback(m_pvHostData, u32Function, &data, sizeof(data));
249// m_pHelpers->pfnCallComplete(callHandle, rc);
250// if (data.pszFormat)
251// RTMemFree(data.pszFormat);
252// if (data.pszTmpPath)
253// RTMemFree(data.pszTmpPath);
254 }
255 break;
256 }
257#ifdef VBOX_WITH_DRAG_AND_DROP_GH
258 case DragAndDropSvc::GUEST_DND_GH_ACK_PENDING:
259 {
260 LogFlowFunc(("GUEST_DND_GH_ACK_PENDING\n"));
261 if ( modeGet() != VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL
262 && modeGet() != VBOX_DRAG_AND_DROP_MODE_GUEST_TO_HOST)
263 {
264 LogFlowFunc(("=> ignoring!\n"));
265 break;
266 }
267
268 if ( cParms != 3
269 || paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* defaction */
270 || paParms[1].type != VBOX_HGCM_SVC_PARM_32BIT /* allactions */
271 || paParms[2].type != VBOX_HGCM_SVC_PARM_PTR /* format */)
272 rc = VERR_INVALID_PARAMETER;
273 else
274 {
275 DragAndDropSvc::VBOXDNDCBGHACKPENDINGDATA data;
276 data.hdr.u32Magic = DragAndDropSvc::CB_MAGIC_DND_GH_ACK_PENDING;
277 paParms[0].getUInt32(&data.uDefAction);
278 paParms[1].getUInt32(&data.uAllActions);
279 uint32_t cTmp;
280 paParms[2].getPointer((void**)&data.pszFormat, &cTmp);
281 if (m_pfnHostCallback)
282 rc = m_pfnHostCallback(m_pvHostData, u32Function, &data, sizeof(data));
283 }
284 break;
285 }
286 case DragAndDropSvc::GUEST_DND_GH_SND_DATA:
287 {
288 LogFlowFunc(("GUEST_DND_GH_SND_DATA\n"));
289 if ( modeGet() != VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL
290 && modeGet() != VBOX_DRAG_AND_DROP_MODE_GUEST_TO_HOST)
291 {
292 LogFlowFunc(("=> ignoring\n"));
293 break;
294 }
295
296 if ( cParms != 2
297 || paParms[0].type != VBOX_HGCM_SVC_PARM_PTR /* data */
298 || paParms[1].type != VBOX_HGCM_SVC_PARM_32BIT /* size */)
299 rc = VERR_INVALID_PARAMETER;
300 else
301 {
302 DragAndDropSvc::VBOXDNDCBSNDDATADATA data;
303 data.hdr.u32Magic = DragAndDropSvc::CB_MAGIC_DND_GH_SND_DATA;
304 paParms[0].getPointer((void**)&data.pvData, &data.cbData);
305 paParms[1].getUInt32(&data.cbAllSize);
306 if (m_pfnHostCallback)
307 rc = m_pfnHostCallback(m_pvHostData, u32Function, &data, sizeof(data));
308 }
309 break;
310 }
311 case DragAndDropSvc::GUEST_DND_GH_EVT_ERROR:
312 {
313 LogFlowFunc(("GUEST_DND_GH_EVT_ERROR\n"));
314 if ( modeGet() != VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL
315 && modeGet() != VBOX_DRAG_AND_DROP_MODE_GUEST_TO_HOST)
316 {
317 LogFlowFunc(("=> ignoring!\n"));
318 break;
319 }
320
321 if ( cParms != 1
322 || paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* rc */)
323 rc = VERR_INVALID_PARAMETER;
324 else
325 {
326 DragAndDropSvc::VBOXDNDCBEVTERRORDATA data;
327 data.hdr.u32Magic = DragAndDropSvc::CB_MAGIC_DND_GH_EVT_ERROR;
328 uint32_t rcOp;
329 paParms[0].getUInt32(&rcOp);
330 data.rc = rcOp;
331 if (m_pfnHostCallback)
332 rc = m_pfnHostCallback(m_pvHostData, u32Function, &data, sizeof(data));
333 }
334 break;
335 }
336#endif
337 default:
338 {
339 /* All other messages are handled by the DnD manager. */
340 rc = m_pManager->nextMessage(u32Function, cParms, paParms);
341 break;
342 }
343 }
344 /* If async execute is requested, we didn't notify the guest about
345 * completion. The client is queued into the waiters list and will be
346 * notified as soon as a new event is available. */
347 if (rc != VINF_HGCM_ASYNC_EXECUTE)
348 m_pHelpers->pfnCallComplete(callHandle, rc);
349 LogFlowFunc(("Returning rc=%Rrc\n", rc));
350}
351
352int DragAndDropService::hostMessage(uint32_t u32Function, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
353{
354 int rc = VINF_SUCCESS;
355#if 0
356 HGCM::Message *pMessage = new HGCM::Message(u32Function, cParms, paParms);
357 m_hostQueue.push(pMessage);
358// bool fPush = true;
359 RTPrintf("client queue %u\n", m_clientQueue.size());
360 RTPrintf("host queue %u\n", m_hostQueue.size());
361 if (!m_clientQueue.empty())
362 {
363 pMessage = m_hostQueue.front();
364 HGCM::Client *pClient = m_clientQueue.front();
365 /* Check if this was a request for getting the next host
366 * message. If so, return the message id and the parameter
367 * count. The message itself has to be queued. */
368 if (pClient->message() == DragAndDropSvc::GUEST_GET_NEXT_HOST_MSG)
369 {
370 RTPrintf("client is waiting for next host msg\n");
371// rc = VERR_TOO_MUCH_DATA;
372 pClient->addMessageInfo(pMessage);
373 /* temp */
374// m_pHelpers->pfnCallComplete(pClient->handle(), rc);
375// m_clientQueue.pop();
376// delete pClient;
377 }
378 else
379 {
380 RTPrintf("client is waiting for host msg (%d)\n", u32Function);
381 /* There is a request for a host message pending. Check
382 * if this is the correct message and if so deliver. If
383 * not the message will be queued. */
384 rc = pClient->addMessage(pMessage);
385 m_hostQueue.pop();
386 delete pMessage;
387// if (RT_SUCCESS(rc))
388// fPush = false;
389 }
390 /* In any case mark this client request as done. */
391 m_pHelpers->pfnCallComplete(pClient->handle(), rc);
392 m_clientQueue.pop_front();
393 delete pClient;
394 }
395// if (fPush)
396// {
397// RTPrintf("push message\n");
398// m_hostQueue.push(pMessage);
399// }
400// else
401// delete pMessage;
402#endif
403
404 return rc;
405}
406
407int DragAndDropService::hostCall(uint32_t u32Function, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
408{
409 LogFlowFunc(("u32Function=%RU32, cParms=%RU32\n", u32Function, cParms));
410
411 int rc = VINF_SUCCESS;
412 if (u32Function == DragAndDropSvc::HOST_DND_SET_MODE)
413 {
414 if (cParms != 1)
415 rc = VERR_INVALID_PARAMETER;
416 else if (paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT)
417 rc = VERR_INVALID_PARAMETER;
418 else
419 modeSet(paParms[0].u.uint32);
420 }
421 else if (modeGet() != VBOX_DRAG_AND_DROP_MODE_OFF)
422 {
423 rc = m_pManager->addMessage(u32Function, cParms, paParms);
424 if ( RT_SUCCESS(rc)
425 && !m_clientQueue.isEmpty())
426 {
427 HGCM::Client *pClient = m_clientQueue.first();
428 AssertPtr(pClient);
429 /* Check if this was a request for getting the next host
430 * message. If so, return the message id and the parameter
431 * count. The message itself has to be queued. */
432 if (pClient->message() == DragAndDropSvc::GUEST_DND_GET_NEXT_HOST_MSG)
433 {
434 LogFlowFunc(("Client %RU32 is waiting for next host msg\n", pClient->clientId()));
435
436 uint32_t uMsg1;
437 uint32_t cParms1;
438 rc = m_pManager->nextMessageInfo(&uMsg1, &cParms1);
439 if (RT_SUCCESS(rc))
440 {
441 pClient->addMessageInfo(uMsg1, cParms1);
442 m_pHelpers->pfnCallComplete(pClient->handle(), rc);
443 m_clientQueue.removeFirst();
444 delete pClient;
445 }
446 else
447 AssertMsgFailed(("Should not happen!"));
448 }
449 else
450 AssertMsgFailed(("Should not happen!"));
451 }
452// else
453// AssertMsgFailed(("Should not happen %Rrc!", rc));
454 }
455
456 LogFlowFunc(("rc=%Rrc\n", rc));
457 return rc;
458}
459
460DECLCALLBACK(int) DragAndDropService::progressCallback(uint32_t uPercentage, uint32_t uState, int rc, void *pvUser)
461{
462 AssertPtrReturn(pvUser, VERR_INVALID_POINTER);
463
464 DragAndDropService *pSelf = static_cast<DragAndDropService *>(pvUser);
465
466 if (pSelf->m_pfnHostCallback)
467 {
468 LogFlowFunc(("GUEST_DND_HG_EVT_PROGRESS: uPercentage=%RU32, uState=%RU32, rc=%Rrc\n",
469 uPercentage, uState, rc));
470 DragAndDropSvc::VBOXDNDCBHGEVTPROGRESSDATA data;
471 data.hdr.u32Magic = DragAndDropSvc::CB_MAGIC_DND_HG_EVT_PROGRESS;
472 data.uPercentage = uPercentage;
473 data.uState = uState;
474 data.rc = rc;
475
476 return pSelf->m_pfnHostCallback(pSelf->m_pvHostData, DragAndDropSvc::GUEST_DND_HG_EVT_PROGRESS, &data, sizeof(data));
477 }
478
479 return VINF_SUCCESS;
480}
481
482/**
483 * @copydoc VBOXHGCMSVCLOAD
484 */
485extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad(VBOXHGCMSVCFNTABLE *pTable)
486{
487 return DragAndDropService::svcLoad(pTable);
488}
489
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette