VirtualBox

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

Last change on this file since 43105 was 42342, checked in by vboxsync, 12 years ago

dnd: debug leftover

  • 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 42342 2012-07-24 09:36:51Z 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 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 int rc = VINF_SUCCESS;
174 LogFlowFunc(("u32ClientID = %d, fn = %d, cParms = %d, pparms = %d\n",
175 u32ClientID, u32Function, cParms, paParms));
176// RTPrintf("u32ClientID = %d, fn = %d, cParms = %d, pparms = %d\n",
177// u32ClientID, u32Function, cParms, paParms);
178
179 switch (u32Function)
180 {
181 case DragAndDropSvc::GUEST_DND_GET_NEXT_HOST_MSG:
182 {
183 DO(("GUEST_DND_GET_NEXT_HOST_MSG\n"));
184 if ( cParms != 3
185 || paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* message */
186 || paParms[1].type != VBOX_HGCM_SVC_PARM_32BIT /* parameter count */
187 || paParms[2].type != VBOX_HGCM_SVC_PARM_32BIT /* blocking */)
188 rc = VERR_INVALID_PARAMETER;
189 else
190 {
191 rc = m_pManager->nextMessageInfo(&paParms[0].u.uint32, &paParms[1].u.uint32);
192 if ( RT_FAILURE(rc)
193 && paParms[2].u.uint32) /* Blocking? */
194 {
195 m_clientQueue.append(new HGCM::Client(u32ClientID, callHandle, u32Function, cParms, paParms));
196 rc = VINF_HGCM_ASYNC_EXECUTE;
197 }
198 }
199 break;
200 }
201 case DragAndDropSvc::GUEST_DND_HG_ACK_OP:
202 {
203 DO(("GUEST_DND_HG_ACK_OP\n"));
204 if ( modeGet() != VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL
205 && modeGet() != VBOX_DRAG_AND_DROP_MODE_HOST_TO_GUEST)
206 {
207 DO(("=> ignoring!\n"));
208 break;
209 }
210
211 if ( cParms != 1
212 || paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* action */)
213 rc = VERR_INVALID_PARAMETER;
214 else
215 {
216 DragAndDropSvc::VBOXDNDCBHGACKOPDATA data;
217 data.hdr.u32Magic = DragAndDropSvc::CB_MAGIC_DND_HG_ACK_OP;
218 paParms[0].getUInt32(&data.uAction);
219 if (m_pfnHostCallback)
220 rc = m_pfnHostCallback(m_pvHostData, u32Function, &data, sizeof(data));
221// m_pHelpers->pfnCallComplete(callHandle, rc);
222 }
223 break;
224 }
225 case DragAndDropSvc::GUEST_DND_HG_REQ_DATA:
226 {
227 DO(("GUEST_DND_HG_REQ_DATA\n"));
228 if ( modeGet() != VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL
229 && modeGet() != VBOX_DRAG_AND_DROP_MODE_HOST_TO_GUEST)
230 {
231 DO(("=> ignoring!\n"));
232 break;
233 }
234
235 if ( cParms != 1
236 || paParms[0].type != VBOX_HGCM_SVC_PARM_PTR /* format */)
237 rc = VERR_INVALID_PARAMETER;
238 else
239 {
240 DragAndDropSvc::VBOXDNDCBHGREQDATADATA data;
241 data.hdr.u32Magic = DragAndDropSvc::CB_MAGIC_DND_HG_REQ_DATA;
242 uint32_t cTmp;
243 paParms[0].getPointer((void**)&data.pszFormat, &cTmp);
244 if (m_pfnHostCallback)
245 rc = m_pfnHostCallback(m_pvHostData, u32Function, &data, sizeof(data));
246// m_pHelpers->pfnCallComplete(callHandle, rc);
247// if (data.pszFormat)
248// RTMemFree(data.pszFormat);
249// if (data.pszTmpPath)
250// RTMemFree(data.pszTmpPath);
251 }
252 break;
253 }
254#ifdef VBOX_WITH_DRAG_AND_DROP_GH
255 case DragAndDropSvc::GUEST_DND_GH_ACK_PENDING:
256 {
257 DO(("GUEST_DND_GH_ACK_PENDING\n"));
258 if ( modeGet() != VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL
259 && modeGet() != VBOX_DRAG_AND_DROP_MODE_GUEST_TO_HOST)
260 {
261 DO(("=> ignoring!\n"));
262 break;
263 }
264
265 if ( cParms != 3
266 || paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* defaction */
267 || paParms[1].type != VBOX_HGCM_SVC_PARM_32BIT /* allactions */
268 || paParms[2].type != VBOX_HGCM_SVC_PARM_PTR /* format */)
269 rc = VERR_INVALID_PARAMETER;
270 else
271 {
272 DragAndDropSvc::VBOXDNDCBGHACKPENDINGDATA data;
273 data.hdr.u32Magic = DragAndDropSvc::CB_MAGIC_DND_GH_ACK_PENDING;
274 paParms[0].getUInt32(&data.uDefAction);
275 paParms[1].getUInt32(&data.uAllActions);
276 uint32_t cTmp;
277 paParms[2].getPointer((void**)&data.pszFormat, &cTmp);
278 if (m_pfnHostCallback)
279 rc = m_pfnHostCallback(m_pvHostData, u32Function, &data, sizeof(data));
280 }
281 break;
282 }
283 case DragAndDropSvc::GUEST_DND_GH_SND_DATA:
284 {
285 DO(("GUEST_DND_GH_SND_DATA\n"));
286 if ( modeGet() != VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL
287 && modeGet() != VBOX_DRAG_AND_DROP_MODE_GUEST_TO_HOST)
288 {
289 DO(("=> ignoring\n"));
290 break;
291 }
292
293 if ( cParms != 2
294 || paParms[0].type != VBOX_HGCM_SVC_PARM_PTR /* data */
295 || paParms[1].type != VBOX_HGCM_SVC_PARM_32BIT /* size */)
296 rc = VERR_INVALID_PARAMETER;
297 else
298 {
299 DragAndDropSvc::VBOXDNDCBSNDDATADATA data;
300 data.hdr.u32Magic = DragAndDropSvc::CB_MAGIC_DND_GH_SND_DATA;
301 paParms[0].getPointer((void**)&data.pvData, &data.cbData);
302 paParms[1].getUInt32(&data.cbAllSize);
303 if (m_pfnHostCallback)
304 rc = m_pfnHostCallback(m_pvHostData, u32Function, &data, sizeof(data));
305 }
306 break;
307 }
308 case DragAndDropSvc::GUEST_DND_GH_EVT_ERROR:
309 {
310 DO(("GUEST_DND_GH_EVT_ERROR\n"));
311 if ( modeGet() != VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL
312 && modeGet() != VBOX_DRAG_AND_DROP_MODE_GUEST_TO_HOST)
313 {
314 DO(("=> ignoring!\n"));
315 break;
316 }
317
318 if ( cParms != 1
319 || paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT /* rc */)
320 rc = VERR_INVALID_PARAMETER;
321 else
322 {
323 DragAndDropSvc::VBOXDNDCBEVTERRORDATA data;
324 data.hdr.u32Magic = DragAndDropSvc::CB_MAGIC_DND_GH_EVT_ERROR;
325 uint32_t rcOp;
326 paParms[0].getUInt32(&rcOp);
327 data.rc = rcOp;
328 if (m_pfnHostCallback)
329 rc = m_pfnHostCallback(m_pvHostData, u32Function, &data, sizeof(data));
330 }
331 break;
332 }
333#endif
334 default:
335 {
336 /* All other messages are handled by the DnD manager. */
337 rc = m_pManager->nextMessage(u32Function, cParms, paParms);
338 /* Check for error. Buffer overflow is allowed. It signals the
339 * guest to ask for more data in the next event. */
340 if ( RT_FAILURE(rc)
341 && rc != VERR_CANCELLED
342 && rc != VERR_BUFFER_OVERFLOW) /* Buffer overflow is allowed. */
343 {
344 m_clientQueue.append(new HGCM::Client(u32ClientID, callHandle, u32Function, cParms, paParms));
345 rc = VINF_HGCM_ASYNC_EXECUTE;
346 }
347 break;
348 }
349 }
350 /* If async execute is requested, we didn't notify the guest about
351 * completion. The client is queued into the waiters list and will be
352 * notified as soon as a new event is available. */
353 if (rc != VINF_HGCM_ASYNC_EXECUTE)
354 m_pHelpers->pfnCallComplete(callHandle, rc);
355 DO(("guest call: %Rrc\n", rc));
356}
357
358int DragAndDropService::hostMessage(uint32_t u32Function, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
359{
360 int rc = VINF_SUCCESS;
361#if 0
362 HGCM::Message *pMessage = new HGCM::Message(u32Function, cParms, paParms);
363 m_hostQueue.push(pMessage);
364// bool fPush = true;
365 RTPrintf("client queue %u\n", m_clientQueue.size());
366 RTPrintf("host queue %u\n", m_hostQueue.size());
367 if (!m_clientQueue.empty())
368 {
369 pMessage = m_hostQueue.front();
370 HGCM::Client *pClient = m_clientQueue.front();
371 /* Check if this was a request for getting the next host
372 * message. If so, return the message id and the parameter
373 * count. The message itself has to be queued. */
374 if (pClient->message() == DragAndDropSvc::GUEST_GET_NEXT_HOST_MSG)
375 {
376 RTPrintf("client is waiting for next host msg\n");
377// rc = VERR_TOO_MUCH_DATA;
378 pClient->addMessageInfo(pMessage);
379 /* temp */
380// m_pHelpers->pfnCallComplete(pClient->handle(), rc);
381// m_clientQueue.pop();
382// delete pClient;
383 }
384 else
385 {
386 RTPrintf("client is waiting for host msg (%d)\n", u32Function);
387 /* There is a request for a host message pending. Check
388 * if this is the correct message and if so deliver. If
389 * not the message will be queued. */
390 rc = pClient->addMessage(pMessage);
391 m_hostQueue.pop();
392 delete pMessage;
393// if (RT_SUCCESS(rc))
394// fPush = false;
395 }
396 /* In any case mark this client request as done. */
397 m_pHelpers->pfnCallComplete(pClient->handle(), rc);
398 m_clientQueue.pop_front();
399 delete pClient;
400 }
401// if (fPush)
402// {
403// RTPrintf("push message\n");
404// m_hostQueue.push(pMessage);
405// }
406// else
407// delete pMessage;
408#endif
409
410 return rc;
411}
412
413int DragAndDropService::hostCall(uint32_t u32Function, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
414{
415 int rc = VINF_SUCCESS;
416 if (u32Function == DragAndDropSvc::HOST_DND_SET_MODE)
417 {
418 if (cParms != 1)
419 rc = VERR_INVALID_PARAMETER;
420 else if (paParms[0].type != VBOX_HGCM_SVC_PARM_32BIT)
421 rc = VERR_INVALID_PARAMETER;
422 else
423 modeSet(paParms[0].u.uint32);
424 }
425 else if (modeGet() != VBOX_DRAG_AND_DROP_MODE_OFF)
426 {
427 rc = m_pManager->addMessage(u32Function, cParms, paParms);
428 if ( RT_SUCCESS(rc)
429 && !m_clientQueue.isEmpty())
430 {
431 HGCM::Client *pClient = m_clientQueue.first();
432 /* Check if this was a request for getting the next host
433 * message. If so, return the message id and the parameter
434 * count. The message itself has to be queued. */
435 if (pClient->message() == DragAndDropSvc::GUEST_DND_GET_NEXT_HOST_MSG)
436 {
437 DO(("client is waiting for next host msg\n"));
438// rc = m_pManager->nextMessageInfo(&paParms[0].u.uint32, &paParms[1].u.uint32);
439 uint32_t uMsg1;
440 uint32_t cParms1;
441 rc = m_pManager->nextMessageInfo(&uMsg1, &cParms1);
442 if (RT_SUCCESS(rc))
443 {
444 pClient->addMessageInfo(uMsg1, cParms1);
445 m_pHelpers->pfnCallComplete(pClient->handle(), rc);
446 m_clientQueue.removeFirst();
447 delete pClient;
448 }
449 else
450 AssertMsgFailed(("Should not happen!"));
451 }
452 else
453 AssertMsgFailed(("Should not happen!"));
454 }
455// else
456// AssertMsgFailed(("Should not happen %Rrc!", rc));
457 }
458
459 LogFlowFunc(("rc=%Rrc\n", rc));
460 return rc;
461}
462
463DECLCALLBACK(int) DragAndDropService::progressCallback(unsigned uPercentage, uint32_t uState, void *pvUser)
464{
465 AssertPtrReturn(pvUser, VERR_INVALID_POINTER);
466
467 DragAndDropService *pSelf = static_cast<DragAndDropService *>(pvUser);
468
469 if (pSelf->m_pfnHostCallback)
470 {
471 DO(("GUEST_DND_HG_EVT_PROGRESS %u\n", uPercentage));
472 DragAndDropSvc::VBOXDNDCBHGEVTPROGRESSDATA data;
473 data.hdr.u32Magic = DragAndDropSvc::CB_MAGIC_DND_HG_EVT_PROGRESS;
474 data.uPercentage = uPercentage;
475 data.uState = uState;
476
477 return pSelf->m_pfnHostCallback(pSelf->m_pvHostData, DragAndDropSvc::GUEST_DND_HG_EVT_PROGRESS, &data, sizeof(data));
478 }
479
480 return VINF_SUCCESS;
481}
482
483/**
484 * @copydoc VBOXHGCMSVCLOAD
485 */
486extern "C" DECLCALLBACK(DECLEXPORT(int)) VBoxHGCMSvcLoad(VBOXHGCMSVCFNTABLE *pTable)
487{
488 return DragAndDropService::svcLoad(pTable);
489}
490
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