service.cpp revision d1a3b68e2ab222e4135826076ab26972869ee23d
/* $Id$ */
/** @file
* Drag and Drop Service.
*/
/*
* Copyright (C) 2011-2012 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/** @page pg_svc_guest_control Guest Control HGCM Service
*
* This service acts as a proxy for handling and buffering host command requests
* and clients on the guest. It tries to be as transparent as possible to let
* the guest (client) and host side do their protocol handling as desired.
*
* The following terms are used:
* - Host: A host process (e.g. VBoxManage or another tool utilizing the Main API)
* which wants to control something on the guest.
* - Client: A client (e.g. VBoxService) running inside the guest OS waiting for
* new host commands to perform. There can be multiple clients connected
* to a service. A client is represented by its HGCM client ID.
* - Context ID: An (almost) unique ID automatically generated on the host (Main API)
* to not only distinguish clients but individual requests. Because
* the host does not know anything about connected clients it needs
* an indicator which it can refer to later. This context ID gets
* internally bound by the service to a client which actually processes
* the command in order to have a relationship between client<->context ID(s).
*
* The host can trigger commands which get buffered by the service (with full HGCM
* parameter info). As soon as a client connects (or is ready to do some new work)
* it gets a buffered host command to process it. This command then will be immediately
* removed from the command list. If there are ready clients but no new commands to be
* processed, these clients will be set into a deferred state (that is being blocked
* to return until a new command is available).
*
* If a client needs to inform the host that something happened, it can send a
* message to a low level HGCM callback registered in Main. This callback contains
* the actual data as well as the context ID to let the host do the next necessary
* steps for this context. This context ID makes it possible to wait for an event
* inside the host's Main API function (like starting a process on the guest and
* wait for getting its PID returned by the client) as well as cancelling blocking
* host calls in order the client terminated/crashed (HGCM detects disconnected
* clients and reports it to this service's callback).
*/
/******************************************************************************
* Header Files *
******************************************************************************/
#define LOG_GROUP LOG_GROUP_HGCM
#include "dndmanager.h"
//# define DO(s) RTPrintf s
#define DO(s) do {} while(0)
//#define DO(s) Log s
/******************************************************************************
* Service class declaration *
******************************************************************************/
/**
* Specialized drag & drop service class.
*/
{
public:
, m_pManager(0)
, m_cClients(0)
{}
protected:
/* HGCM service implementation */
int uninit();
void guestCall(VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID, void *pvClient, uint32_t u32Function, uint32_t cParms, VBOXHGCMSVCPARM paParms[]);
};
/******************************************************************************
* Service class implementation *
******************************************************************************/
{
/* Register functions. */
return VINF_SUCCESS;
}
int DragAndDropService::uninit()
{
delete m_pManager;
return VINF_SUCCESS;
}
{
if (m_cClients < UINT32_MAX)
m_cClients++;
else
AssertMsgFailed(("Maximum number of clients reached\n"));
return VINF_SUCCESS;
}
{
/* Remove all waiters with this clientId. */
while (!m_clientQueue.isEmpty())
{
{
delete pClient;
}
}
return VINF_SUCCESS;
}
{
switch (u32Mode)
{
break;
default:
}
}
void DragAndDropService::guestCall(VBOXHGCMCALLHANDLE callHandle, uint32_t u32ClientID, void *pvClient, uint32_t u32Function, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
{
int rc = VINF_SUCCESS;
LogFlowFunc(("u32ClientID = %d, fn = %d, cParms = %d, pparms = %d\n",
// RTPrintf("u32ClientID = %d, fn = %d, cParms = %d, pparms = %d\n",
// u32ClientID, u32Function, cParms, paParms);
switch (u32Function)
{
{
DO(("GUEST_DND_GET_NEXT_HOST_MSG\n"));
if ( cParms != 3
else
{
if ( RT_FAILURE(rc)
{
}
}
break;
}
{
DO(("GUEST_DND_HG_ACK_OP\n"));
if ( modeGet() != VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL
{
DO(("=> ignoring!\n"));
break;
}
if ( cParms != 1
else
{
if (m_pfnHostCallback)
// m_pHelpers->pfnCallComplete(callHandle, rc);
}
break;
}
{
DO(("GUEST_DND_HG_REQ_DATA\n"));
if ( modeGet() != VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL
{
DO(("=> ignoring!\n"));
break;
}
if ( cParms != 1
else
{
if (m_pfnHostCallback)
// m_pHelpers->pfnCallComplete(callHandle, rc);
// if (data.pszFormat)
// RTMemFree(data.pszFormat);
// if (data.pszTmpPath)
// RTMemFree(data.pszTmpPath);
}
break;
}
#ifdef VBOX_WITH_DRAG_AND_DROP_GH
{
DO(("GUEST_DND_GH_ACK_PENDING\n"));
if ( modeGet() != VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL
{
DO(("=> ignoring!\n"));
break;
}
if ( cParms != 3
else
{
if (m_pfnHostCallback)
}
break;
}
{
DO(("GUEST_DND_GH_SND_DATA\n"));
if ( modeGet() != VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL
{
DO(("=> ignoring\n"));
break;
}
if ( cParms != 2
else
{
if (m_pfnHostCallback)
}
break;
}
{
DO(("GUEST_DND_GH_EVT_ERROR\n"));
if ( modeGet() != VBOX_DRAG_AND_DROP_MODE_BIDIRECTIONAL
{
DO(("=> ignoring!\n"));
break;
}
if ( cParms != 1
else
{
if (m_pfnHostCallback)
}
break;
}
#endif
default:
{
/* All other messages are handled by the DnD manager. */
/* Check for error. Buffer overflow is allowed. It signals the
* guest to ask for more data in the next event. */
if ( RT_FAILURE(rc)
&& rc != VERR_CANCELLED
{
}
break;
}
}
/* If async execute is requested, we didn't notify the guest about
* completion. The client is queued into the waiters list and will be
* notified as soon as a new event is available. */
if (rc != VINF_HGCM_ASYNC_EXECUTE)
}
int DragAndDropService::hostMessage(uint32_t u32Function, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
{
int rc = VINF_SUCCESS;
#if 0
// bool fPush = true;
if (!m_clientQueue.empty())
{
/* Check if this was a request for getting the next host
* message. If so, return the message id and the parameter
* count. The message itself has to be queued. */
{
RTPrintf("client is waiting for next host msg\n");
// rc = VERR_TOO_MUCH_DATA;
/* temp */
// m_pHelpers->pfnCallComplete(pClient->handle(), rc);
// m_clientQueue.pop();
// delete pClient;
}
else
{
/* There is a request for a host message pending. Check
* if this is the correct message and if so deliver. If
* not the message will be queued. */
m_hostQueue.pop();
delete pMessage;
// if (RT_SUCCESS(rc))
// fPush = false;
}
/* In any case mark this client request as done. */
delete pClient;
}
// if (fPush)
// {
// RTPrintf("push message\n");
// m_hostQueue.push(pMessage);
// }
// else
// delete pMessage;
#endif
return rc;
}
{
int rc = VINF_SUCCESS;
{
if (cParms != 1)
else
}
else if (modeGet() != VBOX_DRAG_AND_DROP_MODE_OFF)
{
if ( RT_SUCCESS(rc)
&& !m_clientQueue.isEmpty())
{
/* Check if this was a request for getting the next host
* message. If so, return the message id and the parameter
* count. The message itself has to be queued. */
{
DO(("client is waiting for next host msg\n"));
// rc = m_pManager->nextMessageInfo(&paParms[0].u.uint32, &paParms[1].u.uint32);
if (RT_SUCCESS(rc))
{
delete pClient;
}
else
AssertMsgFailed(("Should not happen!"));
}
else
AssertMsgFailed(("Should not happen!"));
}
// else
// AssertMsgFailed(("Should not happen %Rrc!", rc));
}
return rc;
}
DECLCALLBACK(int) DragAndDropService::progressCallback(unsigned uPercentage, uint32_t uState, void *pvUser)
{
if (pSelf->m_pfnHostCallback)
{
return pSelf->m_pfnHostCallback(pSelf->m_pvHostData, DragAndDropSvc::GUEST_DND_HG_EVT_PROGRESS, &data, sizeof(data));
}
return VINF_SUCCESS;
}
/**
* @copydoc VBOXHGCMSVCLOAD
*/
{
}