VirtualBox

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

Last change on this file since 42338 was 42261, checked in by vboxsync, 13 years ago

enabled shared clipboard support for Linux hosts (guest=>host only)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.0 KB
Line 
1/* $Id: service.cpp 42261 2012-07-20 13:27:47Z vboxsync $ */
2/** @file
3 * Drag and Drop Service.
4 */
5
6/*
7 * Copyright (C) 2011-2012 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#define LOG_GROUP LOG_GROUP_HGCM
58
59#include "dndmanager.h"
60
61//# define DO(s) RTPrintf s
62#define DO(s) do {} while(0)
63//#define DO(s) Log s
64
65/******************************************************************************
66 * Service class declaration *
67 ******************************************************************************/
68
69/**
70 * Specialized drag & drop service class.
71 */
72class DragAndDropService: public HGCM::AbstractService<DragAndDropService>
73{
74public:
75 explicit DragAndDropService(PVBOXHGCMSVCHELPERS pHelpers)
76 : HGCM::AbstractService<DragAndDropService>(pHelpers)
77 , m_pManager(0)
78 , m_cClients(0)
79 {}
80
81protected:
82 /* HGCM service implementation */
83 int init(VBOXHGCMSVCFNTABLE *pTable);
84 int uninit();
85 int clientConnect(uint32_t u32ClientID, void *pvClient);
86 int clientDisconnect(uint32_t u32ClientID, void *pvClient);
87 void guestCall(VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID, void *pvClient, uint32_t u32Function, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
88 int hostCall(uint32_t u32Function, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
89
90 static DECLCALLBACK(int) progressCallback(unsigned uPercentage, uint32_t uState, void *pvUser);
91 int hostMessage(uint32_t u32Function, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
92 void modeSet(uint32_t u32Mode);
93 inline uint32_t modeGet() { return m_u32Mode; };
94
95 DnDManager *m_pManager;
96
97 uint32_t m_cClients;
98 RTCList<HGCM::Client*> m_clientQueue;
99 uint32_t m_u32Mode;
100};
101
102/******************************************************************************
103 * Service class implementation *
104 ******************************************************************************/
105
106int DragAndDropService::init(VBOXHGCMSVCFNTABLE *pTable)
107{
108 /* Register functions. */
109 pTable->pfnHostCall = svcHostCall;
110 pTable->pfnSaveState = NULL; /* The service is stateless, so the normal */
111 pTable->pfnLoadState = NULL; /* construction done before restoring suffices */
112 pTable->pfnRegisterExtension = svcRegisterExtension;
113 modeSet(VBOX_DRAG_AND_DROP_MODE_OFF);
114
115 m_pManager = new DnDManager(&DragAndDropService::progressCallback, this);
116
117 return VINF_SUCCESS;
118}
119
120int DragAndDropService::uninit()
121{
122 delete m_pManager;
123
124 return VINF_SUCCESS;
125}
126
127int DragAndDropService::clientConnect(uint32_t u32ClientID, void *pvClient)
128{
129 LogFlowFunc(("New client (%ld) connected\n", u32ClientID));
130 DO(("New client (%ld) connected\n", u32ClientID));
131 if (m_cClients < UINT32_MAX)
132 m_cClients++;
133 else
134 AssertMsgFailed(("Maximum number of clients reached\n"));
135 return VINF_SUCCESS;
136}
137
138int DragAndDropService::clientDisconnect(uint32_t u32ClientID, void *pvClient)
139{
140 /* Remove all waiters with this clientId. */
141 while (!m_clientQueue.isEmpty())
142 {
143 HGCM::Client *pClient = m_clientQueue.first();
144 if (pClient->clientId() == u32ClientID)
145 {
146 m_pHelpers->pfnCallComplete(pClient->handle(), VERR_INTERRUPTED);
147 m_clientQueue.removeFirst();
148 delete pClient;
149 }
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 LogRel(("drag'n'drop mode = %d\n", u32Mode));
164 m_u32Mode = u32Mode;
165 break;
166
167 default:
168 m_u32Mode = VBOX_DRAG_AND_DROP_MODE_OFF;
169 }
170}
171
172void DragAndDropService::guestCall(VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID, void *pvClient, uint32_t u32Function, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
173{
174 int rc = VINF_SUCCESS;
175 LogFlowFunc(("u32ClientID = %d, fn = %d, cParms = %d, pparms = %d\n",
176 u32ClientID, u32Function, cParms, paParms));
177// RTPrintf("u32ClientID = %d, fn = %d, cParms = %d, pparms = %d\n",
178// u32ClientID, u32Function, cParms, paParms);
179
180 switch (u32Function)
181 {
182 case DragAndDropSvc::GUEST_DND_GET_NEXT_HOST_MSG:
183 {
184 DO(("GUEST_DND_GET_NEXT_HOST_MSG\n"));
185 if ( cParms != 3
186 || paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* message */
187 || paParms[1].type != VBOX_HGCM_SVC_PARM_32BIT /* parameter count */
188 || paParms[2].type != VBOX_HGCM_SVC_PARM_32BIT /* blocking */)
189 rc = VERR_INVALID_PARAMETER;
190 else
191 {
192 rc = m_pManager->nextMessageInfo(&paParms[0].u.uint32, &paParms[1].u.uint32);
193 if ( RT_FAILURE(rc)
194 && paParms[2].u.uint32) /* Blocking? */
195 {
196 m_clientQueue.append(new HGCM::Client(u32ClientID, callHandle, u32Function, cParms, paParms));
197 rc = VINF_HGCM_ASYNC_EXECUTE;
198 }
199 }
200 break;
201 }
202 case DragAndDropSvc::GUEST_DND_HG_ACK_OP:
203 {
204 DO(("GUEST_DND_HG_ACK_OP\n"));
205 if ( modeGet() != VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL
206 && modeGet() != VBOX_DRAG_AND_DROP_MODE_HOST_TO_GUEST)
207 {
208 DO(("=> ignoring!\n"));
209 break;
210 }
211
212 if ( cParms != 1
213 || paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* action */)
214 rc = VERR_INVALID_PARAMETER;
215 else
216 {
217 DragAndDropSvc::VBOXDNDCBHGACKOPDATA data;
218 data.hdr.u32Magic = DragAndDropSvc::CB_MAGIC_DND_HG_ACK_OP;
219 paParms[0].getUInt32(&data.uAction);
220 if (m_pfnHostCallback)
221 rc = m_pfnHostCallback(m_pvHostData, u32Function, &data, sizeof(data));
222// m_pHelpers->pfnCallComplete(callHandle, rc);
223 }
224 break;
225 }
226 case DragAndDropSvc::GUEST_DND_HG_REQ_DATA:
227 {
228 DO(("GUEST_DND_HG_REQ_DATA\n"));
229 if ( modeGet() != VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL
230 && modeGet() != VBOX_DRAG_AND_DROP_MODE_HOST_TO_GUEST)
231 {
232 DO(("=> ignoring!\n"));
233 break;
234 }
235
236 if ( cParms != 1
237 || paParms[0].type != VBOX_HGCM_SVC_PARM_PTR /* format */)
238 rc = VERR_INVALID_PARAMETER;
239 else
240 {
241 DragAndDropSvc::VBOXDNDCBHGREQDATADATA data;
242 data.hdr.u32Magic = DragAndDropSvc::CB_MAGIC_DND_HG_REQ_DATA;
243 uint32_t cTmp;
244 paParms[0].getPointer((void**)&data.pszFormat, &cTmp);
245 if (m_pfnHostCallback)
246 rc = m_pfnHostCallback(m_pvHostData, u32Function, &data, sizeof(data));
247// m_pHelpers->pfnCallComplete(callHandle, rc);
248// if (data.pszFormat)
249// RTMemFree(data.pszFormat);
250// if (data.pszTmpPath)
251// RTMemFree(data.pszTmpPath);
252 }
253 break;
254 }
255#ifdef VBOX_WITH_DRAG_AND_DROP_GH
256 case DragAndDropSvc::GUEST_DND_GH_ACK_PENDING:
257 {
258 DO(("GUEST_DND_GH_ACK_PENDING\n"));
259 if ( modeGet() != VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL
260 && modeGet() != VBOX_DRAG_AND_DROP_MODE_GUEST_TO_HOST)
261 {
262 DO(("=> ignoring!\n"));
263 break;
264 }
265
266 if ( cParms != 3
267 || paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* defaction */
268 || paParms[1].type != VBOX_HGCM_SVC_PARM_32BIT /* allactions */
269 || paParms[2].type != VBOX_HGCM_SVC_PARM_PTR /* format */)
270 rc = VERR_INVALID_PARAMETER;
271 else
272 {
273 DragAndDropSvc::VBOXDNDCBGHACKPENDINGDATA data;
274 data.hdr.u32Magic = DragAndDropSvc::CB_MAGIC_DND_GH_ACK_PENDING;
275 paParms[0].getUInt32(&data.uDefAction);
276 paParms[1].getUInt32(&data.uAllActions);
277 uint32_t cTmp;
278 paParms[2].getPointer((void**)&data.pszFormat, &cTmp);
279 if (m_pfnHostCallback)
280 rc = m_pfnHostCallback(m_pvHostData, u32Function, &data, sizeof(data));
281 }
282 break;
283 }
284 case DragAndDropSvc::GUEST_DND_GH_SND_DATA:
285 {
286 DO(("GUEST_DND_GH_SND_DATA\n"));
287 if ( modeGet() != VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL
288 && modeGet() != VBOX_DRAG_AND_DROP_MODE_GUEST_TO_HOST)
289 {
290 DO(("=> ignoring\n"));
291 break;
292 }
293
294 if ( cParms != 2
295 || paParms[0].type != VBOX_HGCM_SVC_PARM_PTR /* data */
296 || paParms[1].type != VBOX_HGCM_SVC_PARM_32BIT /* size */)
297 rc = VERR_INVALID_PARAMETER;
298 else
299 {
300 DragAndDropSvc::VBOXDNDCBSNDDATADATA data;
301 data.hdr.u32Magic = DragAndDropSvc::CB_MAGIC_DND_GH_SND_DATA;
302 paParms[0].getPointer((void**)&data.pvData, &data.cbData);
303 paParms[1].getUInt32(&data.cbAllSize);
304 if (m_pfnHostCallback)
305 rc = m_pfnHostCallback(m_pvHostData, u32Function, &data, sizeof(data));
306 }
307 break;
308 }
309 case DragAndDropSvc::GUEST_DND_GH_EVT_ERROR:
310 {
311 DO(("GUEST_DND_GH_EVT_ERROR\n"));
312 if ( modeGet() != VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL
313 && modeGet() != VBOX_DRAG_AND_DROP_MODE_GUEST_TO_HOST)
314 {
315 DO(("=> ignoring!\n"));
316 break;
317 }
318
319 if ( cParms != 1
320 || paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* rc */)
321 rc = VERR_INVALID_PARAMETER;
322 else
323 {
324 DragAndDropSvc::VBOXDNDCBEVTERRORDATA data;
325 data.hdr.u32Magic = DragAndDropSvc::CB_MAGIC_DND_GH_EVT_ERROR;
326 uint32_t rcOp;
327 paParms[0].getUInt32(&rcOp);
328 data.rc = rcOp;
329 if (m_pfnHostCallback)
330 rc = m_pfnHostCallback(m_pvHostData, u32Function, &data, sizeof(data));
331 }
332 break;
333 }
334#endif
335 default:
336 {
337 /* All other messages are handled by the DnD manager. */
338 rc = m_pManager->nextMessage(u32Function, cParms, paParms);
339 /* Check for error. Buffer overflow is allowed. It signals the
340 * guest to ask for more data in the next event. */
341 if ( RT_FAILURE(rc)
342 && rc != VERR_CANCELLED
343 && rc != VERR_BUFFER_OVERFLOW) /* Buffer overflow is allowed. */
344 {
345 m_clientQueue.append(new HGCM::Client(u32ClientID, callHandle, u32Function, cParms, paParms));
346 rc = VINF_HGCM_ASYNC_EXECUTE;
347 }
348 break;
349 }
350 }
351 /* If async execute is requested, we didn't notify the guest about
352 * completion. The client is queued into the waiters list and will be
353 * notified as soon as a new event is available. */
354 if (rc != VINF_HGCM_ASYNC_EXECUTE)
355 m_pHelpers->pfnCallComplete(callHandle, rc);
356 DO(("guest call: %Rrc\n", rc));
357}
358
359int DragAndDropService::hostMessage(uint32_t u32Function, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
360{
361 int rc = VINF_SUCCESS;
362#if 0
363 HGCM::Message *pMessage = new HGCM::Message(u32Function, cParms, paParms);
364 m_hostQueue.push(pMessage);
365// bool fPush = true;
366 RTPrintf("client queue %u\n", m_clientQueue.size());
367 RTPrintf("host queue %u\n", m_hostQueue.size());
368 if (!m_clientQueue.empty())
369 {
370 pMessage = m_hostQueue.front();
371 HGCM::Client *pClient = m_clientQueue.front();
372 /* Check if this was a request for getting the next host
373 * message. If so, return the message id and the parameter
374 * count. The message itself has to be queued. */
375 if (pClient->message() == DragAndDropSvc::GUEST_GET_NEXT_HOST_MSG)
376 {
377 RTPrintf("client is waiting for next host msg\n");
378// rc = VERR_TOO_MUCH_DATA;
379 pClient->addMessageInfo(pMessage);
380 /* temp */
381// m_pHelpers->pfnCallComplete(pClient->handle(), rc);
382// m_clientQueue.pop();
383// delete pClient;
384 }
385 else
386 {
387 RTPrintf("client is waiting for host msg (%d)\n", u32Function);
388 /* There is a request for a host message pending. Check
389 * if this is the correct message and if so deliver. If
390 * not the message will be queued. */
391 rc = pClient->addMessage(pMessage);
392 m_hostQueue.pop();
393 delete pMessage;
394// if (RT_SUCCESS(rc))
395// fPush = false;
396 }
397 /* In any case mark this client request as done. */
398 m_pHelpers->pfnCallComplete(pClient->handle(), rc);
399 m_clientQueue.pop_front();
400 delete pClient;
401 }
402// if (fPush)
403// {
404// RTPrintf("push message\n");
405// m_hostQueue.push(pMessage);
406// }
407// else
408// delete pMessage;
409#endif
410
411 return rc;
412}
413
414int DragAndDropService::hostCall(uint32_t u32Function, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
415{
416 int rc = VINF_SUCCESS;
417 if (u32Function == DragAndDropSvc::HOST_DND_SET_MODE)
418 {
419 if (cParms != 1)
420 rc = VERR_INVALID_PARAMETER;
421 else if (paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT)
422 rc = VERR_INVALID_PARAMETER;
423 else
424 modeSet(paParms[0].u.uint32);
425 }
426 else if (modeGet() != VBOX_DRAG_AND_DROP_MODE_OFF)
427 {
428 rc = m_pManager->addMessage(u32Function, cParms, paParms);
429 if ( RT_SUCCESS(rc)
430 && !m_clientQueue.isEmpty())
431 {
432 HGCM::Client *pClient = m_clientQueue.first();
433 /* Check if this was a request for getting the next host
434 * message. If so, return the message id and the parameter
435 * count. The message itself has to be queued. */
436 if (pClient->message() == DragAndDropSvc::GUEST_DND_GET_NEXT_HOST_MSG)
437 {
438 DO(("client is waiting for next host msg\n"));
439// rc = m_pManager->nextMessageInfo(&paParms[0].u.uint32, &paParms[1].u.uint32);
440 uint32_t uMsg1;
441 uint32_t cParms1;
442 rc = m_pManager->nextMessageInfo(&uMsg1, &cParms1);
443 if (RT_SUCCESS(rc))
444 {
445 pClient->addMessageInfo(uMsg1, cParms1);
446 m_pHelpers->pfnCallComplete(pClient->handle(), rc);
447 m_clientQueue.removeFirst();
448 delete pClient;
449 }
450 else
451 AssertMsgFailed(("Should not happen!"));
452 }
453 else
454 AssertMsgFailed(("Should not happen!"));
455 }
456// else
457// AssertMsgFailed(("Should not happen %Rrc!", rc));
458 }
459
460 LogFlowFunc(("rc=%Rrc\n", rc));
461 return rc;
462}
463
464DECLCALLBACK(int) DragAndDropService::progressCallback(unsigned uPercentage, uint32_t uState, void *pvUser)
465{
466 AssertPtrReturn(pvUser, VERR_INVALID_POINTER);
467
468 DragAndDropService *pSelf = static_cast<DragAndDropService *>(pvUser);
469
470 if (pSelf->m_pfnHostCallback)
471 {
472 DO(("GUEST_DND_HG_EVT_PROGRESS %u\n", uPercentage));
473 DragAndDropSvc::VBOXDNDCBHGEVTPROGRESSDATA data;
474 data.hdr.u32Magic = DragAndDropSvc::CB_MAGIC_DND_HG_EVT_PROGRESS;
475 data.uPercentage = uPercentage;
476 data.uState = uState;
477
478 return pSelf->m_pfnHostCallback(pSelf->m_pvHostData, DragAndDropSvc::GUEST_DND_HG_EVT_PROGRESS, &data, sizeof(data));
479 }
480
481 return VINF_SUCCESS;
482}
483
484/**
485 * @copydoc VBOXHGCMSVCLOAD
486 */
487extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad(VBOXHGCMSVCFNTABLE *pTable)
488{
489 return DragAndDropService::svcLoad(pTable);
490}
491
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