1 | /* $Id: dndmanager.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Drag and Drop manager: Handling of DnD messages on the host side.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2020 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 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 |
|
---|
23 | #ifdef LOG_GROUP
|
---|
24 | #undef LOG_GROUP
|
---|
25 | #endif
|
---|
26 | #define LOG_GROUP LOG_GROUP_GUEST_DND
|
---|
27 |
|
---|
28 | #include "dndmanager.h"
|
---|
29 |
|
---|
30 | #include <VBox/log.h>
|
---|
31 | #include <iprt/file.h>
|
---|
32 | #include <iprt/dir.h>
|
---|
33 | #include <iprt/path.h>
|
---|
34 | #include <iprt/uri.h>
|
---|
35 |
|
---|
36 |
|
---|
37 | /*********************************************************************************************************************************
|
---|
38 | * DnDManager *
|
---|
39 | *********************************************************************************************************************************/
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Adds a DnD message to the manager's queue.
|
---|
43 | *
|
---|
44 | * @returns IPRT status code.
|
---|
45 | * @param pMsg Pointer to DnD message to add. The queue then owns the pointer.
|
---|
46 | * @param fAppend Whether to append or prepend the message to the queue.
|
---|
47 | */
|
---|
48 | int DnDManager::AddMsg(DnDMessage *pMsg, bool fAppend /* = true */)
|
---|
49 | {
|
---|
50 | AssertPtrReturn(pMsg, VERR_INVALID_POINTER);
|
---|
51 |
|
---|
52 | LogFlowFunc(("uMsg=%RU32, cParms=%RU32, fAppend=%RTbool\n", pMsg->GetType(), pMsg->GetParamCount(), fAppend));
|
---|
53 |
|
---|
54 | if (fAppend)
|
---|
55 | m_queueMsg.append(pMsg);
|
---|
56 | else
|
---|
57 | m_queueMsg.prepend(pMsg);
|
---|
58 |
|
---|
59 | /** @todo Catch / handle OOM? */
|
---|
60 |
|
---|
61 | return VINF_SUCCESS;
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Adds a DnD message to the manager's queue.
|
---|
66 | *
|
---|
67 | * @returns IPRT status code.
|
---|
68 | * @param uMsg Type (function number) of message to add.
|
---|
69 | * @param cParms Number of parameters of message to add.
|
---|
70 | * @param paParms Array of parameters of message to add.
|
---|
71 | * @param fAppend Whether to append or prepend the message to the queue.
|
---|
72 | */
|
---|
73 | int DnDManager::AddMsg(uint32_t uMsg, uint32_t cParms, VBOXHGCMSVCPARM paParms[], bool fAppend /* = true */)
|
---|
74 | {
|
---|
75 | int rc;
|
---|
76 |
|
---|
77 | try
|
---|
78 | {
|
---|
79 | DnDMessage *pMsg = new DnDGenericMessage(uMsg, cParms, paParms);
|
---|
80 | rc = AddMsg(pMsg, fAppend);
|
---|
81 | }
|
---|
82 | catch(std::bad_alloc &)
|
---|
83 | {
|
---|
84 | rc = VERR_NO_MEMORY;
|
---|
85 | }
|
---|
86 |
|
---|
87 | LogFlowFuncLeaveRC(rc);
|
---|
88 | return rc;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Retrieves information about the next message in the queue.
|
---|
93 | *
|
---|
94 | * @returns IPRT status code. VERR_NO_DATA if no next message is available.
|
---|
95 | * @param puType Where to store the message type.
|
---|
96 | * @param pcParms Where to store the message parameter count.
|
---|
97 | */
|
---|
98 | int DnDManager::GetNextMsgInfo(uint32_t *puType, uint32_t *pcParms)
|
---|
99 | {
|
---|
100 | AssertPtrReturn(puType, VERR_INVALID_POINTER);
|
---|
101 | AssertPtrReturn(pcParms, VERR_INVALID_POINTER);
|
---|
102 |
|
---|
103 | int rc;
|
---|
104 |
|
---|
105 | if (m_queueMsg.isEmpty())
|
---|
106 | {
|
---|
107 | rc = VERR_NO_DATA;
|
---|
108 | }
|
---|
109 | else
|
---|
110 | {
|
---|
111 | DnDMessage *pMsg = m_queueMsg.first();
|
---|
112 | AssertPtr(pMsg);
|
---|
113 |
|
---|
114 | *puType = pMsg->GetType();
|
---|
115 | *pcParms = pMsg->GetParamCount();
|
---|
116 |
|
---|
117 | rc = VINF_SUCCESS;
|
---|
118 | }
|
---|
119 |
|
---|
120 | LogFlowFunc(("Returning puMsg=%RU32, pcParms=%RU32, rc=%Rrc\n", *puType, *pcParms, rc));
|
---|
121 | return rc;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Retrieves the next queued up message and removes it from the queue on success.
|
---|
126 | * Will return VERR_NO_DATA if no next message is available.
|
---|
127 | *
|
---|
128 | * @returns IPRT status code.
|
---|
129 | * @param uMsg Message type to retrieve.
|
---|
130 | * @param cParms Number of parameters the \@a paParms array can store.
|
---|
131 | * @param paParms Where to store the message parameters.
|
---|
132 | */
|
---|
133 | int DnDManager::GetNextMsg(uint32_t uMsg, uint32_t cParms, VBOXHGCMSVCPARM paParms[])
|
---|
134 | {
|
---|
135 | LogFlowFunc(("uMsg=%RU32, cParms=%RU32\n", uMsg, cParms));
|
---|
136 |
|
---|
137 | /* Check for pending messages in our queue. */
|
---|
138 | if (m_queueMsg.isEmpty())
|
---|
139 | return VERR_NO_DATA;
|
---|
140 |
|
---|
141 | /* Get the current message. */
|
---|
142 | DnDMessage *pMsg = m_queueMsg.first();
|
---|
143 | AssertPtr(pMsg);
|
---|
144 |
|
---|
145 | m_queueMsg.removeFirst(); /* Remove the current message from the queue. */
|
---|
146 |
|
---|
147 | /* Fetch the current message info. */
|
---|
148 | int rc = pMsg->GetData(uMsg, cParms, paParms);
|
---|
149 |
|
---|
150 | /*
|
---|
151 | * If there was an error handling the current message or the user has canceled
|
---|
152 | * the operation, we need to cleanup all pending events and inform the progress
|
---|
153 | * callback about our exit.
|
---|
154 | */
|
---|
155 | if (RT_FAILURE(rc))
|
---|
156 | {
|
---|
157 | /* Clear any pending messages. */
|
---|
158 | Reset();
|
---|
159 |
|
---|
160 | /* Create a new cancel message to inform the guest + call
|
---|
161 | * the host whether the current transfer was canceled or aborted
|
---|
162 | * due to an error. */
|
---|
163 | try
|
---|
164 | {
|
---|
165 | if (rc == VERR_CANCELLED)
|
---|
166 | LogFlowFunc(("Operation was cancelled\n"));
|
---|
167 |
|
---|
168 | DnDHGCancelMessage *pMsgCancel = new DnDHGCancelMessage();
|
---|
169 |
|
---|
170 | int rc2 = AddMsg(pMsgCancel, false /* Prepend */);
|
---|
171 | AssertRC(rc2);
|
---|
172 |
|
---|
173 | if (m_pfnProgressCallback)
|
---|
174 | {
|
---|
175 | LogFlowFunc(("Notifying host about aborting operation (%Rrc) ...\n", rc));
|
---|
176 | m_pfnProgressCallback( rc == VERR_CANCELLED
|
---|
177 | ? DragAndDropSvc::DND_PROGRESS_CANCELLED
|
---|
178 | : DragAndDropSvc::DND_PROGRESS_ERROR,
|
---|
179 | 100 /* Percent */, rc,
|
---|
180 | m_pvProgressUser);
|
---|
181 | }
|
---|
182 | }
|
---|
183 | catch(std::bad_alloc &)
|
---|
184 | {
|
---|
185 | rc = VERR_NO_MEMORY;
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | LogFlowFunc(("Message processed with rc=%Rrc\n", rc));
|
---|
190 | return rc;
|
---|
191 | }
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Resets the manager by clearing the message queue and internal state.
|
---|
195 | */
|
---|
196 | void DnDManager::Reset(void)
|
---|
197 | {
|
---|
198 | LogFlowFuncEnter();
|
---|
199 |
|
---|
200 | while (!m_queueMsg.isEmpty())
|
---|
201 | {
|
---|
202 | delete m_queueMsg.last();
|
---|
203 | m_queueMsg.removeLast();
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|