VirtualBox

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

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

Merged private draganddrop branch into trunk.

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