VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuest/lib/VBoxGuestR3LibClipboard.cpp@ 78683

Last change on this file since 78683 was 78683, checked in by vboxsync, 6 years ago

Shared Clipboard/URI: Update.

  • Property svn:eol-style set to native
  • Property svn:keyword set to Id
  • Property svn:keywords set to Author Date Id Revision
File size: 29.7 KB
Line 
1/* $Id: VBoxGuestR3LibClipboard.cpp 78683 2019-05-23 10:07:21Z vboxsync $ */
2/** @file
3 * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, Clipboard.
4 */
5
6/*
7 * Copyright (C) 2007-2019 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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <VBox/GuestHost/SharedClipboard.h>
32#ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
33# include <VBox/GuestHost/SharedClipboard-uri.h>
34#endif
35#include <VBox/HostServices/VBoxClipboardSvc.h>
36#include <VBox/err.h>
37#include <iprt/assert.h>
38#include <iprt/string.h>
39#include <iprt/cpp/ministring.h>
40#include "VBoxGuestR3LibInternal.h"
41
42
43/*********************************************************************************************************************************
44* Prototypes *
45*********************************************************************************************************************************/
46#ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
47static int vbglR3ClipboardSendErrorInternal(HGCMCLIENTID idClient, int rcErr);
48static int vbglR3ClipboardSendURIData(HGCMCLIENTID idClient, const void *pvData, size_t cbData);
49#endif
50
51
52/**
53 * Connects to the clipboard service.
54 *
55 * @returns VBox status code
56 * @param pidClient Where to put the client id on success. The client id
57 * must be passed to all the other clipboard calls.
58 */
59VBGLR3DECL(int) VbglR3ClipboardConnect(HGCMCLIENTID *pidClient)
60{
61 int rc = VbglR3HGCMConnect("VBoxSharedClipboard", pidClient);
62 if (rc == VERR_HGCM_SERVICE_NOT_FOUND)
63 rc = VINF_PERMISSION_DENIED;
64 return rc;
65}
66
67
68/**
69 * Disconnect from the clipboard service.
70 *
71 * @returns VBox status code.
72 * @param idClient The client id returned by VbglR3ClipboardConnect().
73 */
74VBGLR3DECL(int) VbglR3ClipboardDisconnect(HGCMCLIENTID idClient)
75{
76 return VbglR3HGCMDisconnect(idClient);
77}
78
79
80/**
81 * Get a host message.
82 *
83 * This will block until a message becomes available.
84 *
85 * @returns VBox status code.
86 * @param idClient The client id returned by VbglR3ClipboardConnect().
87 * @param pidMsg Where to store the message id.
88 * @param pfFormats Where to store the format(s) the message applies to.
89 */
90VBGLR3DECL(int) VbglR3ClipboardGetHostMsg(HGCMCLIENTID idClient, uint32_t *pidMsg, uint32_t *pfFormats)
91{
92 VBoxClipboardGetHostMsg Msg;
93
94 VBGL_HGCM_HDR_INIT(&Msg.hdr, idClient, VBOX_SHARED_CLIPBOARD_FN_GET_HOST_MSG, 2);
95 VbglHGCMParmUInt32Set(&Msg.msg, 0);
96 VbglHGCMParmUInt32Set(&Msg.formats, 0);
97
98 int rc = VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
99 if (RT_SUCCESS(rc))
100 {
101 int rc2 = VbglHGCMParmUInt32Get(&Msg.msg, pidMsg);
102 if (RT_SUCCESS(rc))
103 {
104 rc2 = VbglHGCMParmUInt32Get(&Msg.formats, pfFormats);
105 if (RT_SUCCESS(rc2))
106 return rc;
107 }
108 rc = rc2;
109 }
110 *pidMsg = UINT32_MAX - 1;
111 *pfFormats = UINT32_MAX;
112 return rc;
113}
114
115
116/**
117 * Reads data from the host clipboard.
118 *
119 * @returns VBox status code.
120 * @retval VINF_BUFFER_OVERFLOW If there is more data available than the caller provided buffer space for.
121 *
122 * @param idClient The client id returned by VbglR3ClipboardConnect().
123 * @param fFormat The format we're requesting the data in.
124 * @param pv Where to store the data.
125 * @param cb The size of the buffer pointed to by pv.
126 * @param pcb The actual size of the host clipboard data. May be larger than cb.
127 */
128VBGLR3DECL(int) VbglR3ClipboardReadData(HGCMCLIENTID idClient, uint32_t fFormat, void *pv, uint32_t cb, uint32_t *pcb)
129{
130 VBoxClipboardReadDataMsg Msg;
131
132 VBGL_HGCM_HDR_INIT(&Msg.hdr, idClient, VBOX_SHARED_CLIPBOARD_FN_READ_DATA, 3);
133 VbglHGCMParmUInt32Set(&Msg.format, fFormat);
134 VbglHGCMParmPtrSet(&Msg.ptr, pv, cb);
135 VbglHGCMParmUInt32Set(&Msg.size, 0);
136
137 int rc = VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
138 if (RT_SUCCESS(rc))
139 {
140 uint32_t cbActual;
141 int rc2 = VbglHGCMParmUInt32Get(&Msg.size, &cbActual);
142 if (RT_SUCCESS(rc2))
143 {
144 *pcb = cbActual;
145 if (cbActual > cb)
146 return VINF_BUFFER_OVERFLOW;
147 return rc;
148 }
149 rc = rc2;
150 }
151 return rc;
152}
153
154/**
155 * Reads the meta data header from the host.
156 *
157 * @returns IPRT status code.
158 * @param idClient The client id returned by VbglR3ClipboardConnect().
159 * @param pDataHdr Where to store the read meta data header.
160 */
161static int vbglR3ClipboardReadMetaDataHdr(HGCMCLIENTID idClient, PVBOXDNDSNDDATAHDR pDataHdr)
162{
163 AssertPtrReturn(pDataHdr, VERR_INVALID_POINTER);
164
165 VBOXDNDHGSENDDATAHDRMSG Msg;
166 RT_ZERO(Msg);
167 VBGL_HGCM_HDR_INIT(&Msg.hdr, idClient, HOST_DND_HG_SND_DATA_HDR, 12);
168 Msg.uContext.SetUInt32(0);
169 Msg.uFlags.SetUInt32(0);
170 Msg.uScreenId.SetUInt32(0);
171 Msg.cbTotal.SetUInt64(0);
172 Msg.cbMeta.SetUInt32(0);
173 Msg.pvMetaFmt.SetPtr(pDataHdr->pvMetaFmt, pDataHdr->cbMetaFmt);
174 Msg.cbMetaFmt.SetUInt32(0);
175 Msg.cObjects.SetUInt64(0);
176 Msg.enmCompression.SetUInt32(0);
177 Msg.enmChecksumType.SetUInt32(0);
178 Msg.pvChecksum.SetPtr(pDataHdr->pvChecksum, pDataHdr->cbChecksum);
179 Msg.cbChecksum.SetUInt32(0);
180
181 int rc = VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
182 if (RT_SUCCESS(rc))
183 {
184 /* Msg.uContext not needed here. */
185 Msg.uFlags.GetUInt32(&pDataHdr->uFlags);
186 Msg.uScreenId.GetUInt32(&pDataHdr->uScreenId);
187 Msg.cbTotal.GetUInt64(&pDataHdr->cbTotal);
188 Msg.cbMeta.GetUInt32(&pDataHdr->cbMeta);
189 Msg.cbMetaFmt.GetUInt32(&pDataHdr->cbMetaFmt);
190 Msg.cObjects.GetUInt64(&pDataHdr->cObjects);
191 Msg.enmCompression.GetUInt32(&pDataHdr->enmCompression);
192 Msg.enmChecksumType.GetUInt32((uint32_t *)&pDataHdr->enmChecksumType);
193 Msg.cbChecksum.GetUInt32(&pDataHdr->cbChecksum);
194 }
195
196 LogFlowFuncLeaveRC(rc);
197 return rc;
198}
199
200/**
201 * Helper function for reading the actual clipboard meta data from the host. Do not call directly.
202 *
203 * @returns IPRT status code.
204 * @param idClient The client id returned by VbglR3ClipboardConnect().
205 * @param pDataHdr Where to store the data header data.
206 * @param ppvData Returns the received meta data. Needs to be free'd by the caller.
207 * @param pcbData Where to store the size (in bytes) of the received meta data.
208 */
209static int vbglR3ClipboardReadMetaDataLoop(HGCMCLIENTID idClient, PVBOXDNDSNDDATAHDR pDataHdr,
210 void **ppvData, uint64_t *pcbData)
211{
212 AssertPtrReturn(pDataHdr, VERR_INVALID_POINTER);
213 AssertPtrReturn(ppvData, VERR_INVALID_POINTER);
214 AssertPtrReturn(pcbData, VERR_INVALID_POINTER);
215
216 int rc;
217 uint32_t cbDataRecv;
218
219 LogFlowFuncEnter();
220
221 rc = vbglR3DnDHGRecvDataHdr(pCtx, pDataHdr);
222 if (RT_FAILURE(rc))
223 return rc;
224
225 LogFlowFunc(("cbTotal=%RU64, cbMeta=%RU32, cObjects=%RU32\n", pDataHdr->cbTotal, pDataHdr->cbMeta, pDataHdr->cObjects));
226 if (pDataHdr->cbMeta)
227 {
228 uint64_t cbDataTmp = 0;
229 void *pvDataTmp = RTMemAlloc(pDataHdr->cbMeta);
230 if (!pvDataTmp)
231 rc = VERR_NO_MEMORY;
232
233 if (RT_SUCCESS(rc))
234 {
235 uint8_t *pvDataOff = (uint8_t *)pvDataTmp;
236 while (cbDataTmp < pDataHdr->cbMeta)
237 {
238 rc = vbglR3DnDHGRecvDataRaw(pCtx, pDataHdr,
239 pvDataOff, RT_MIN(pDataHdr->cbMeta - cbDataTmp, pCtx->cbMaxChunkSize),
240 &cbDataRecv);
241 if (RT_SUCCESS(rc))
242 {
243 LogFlowFunc(("cbDataRecv=%RU32, cbDataTmp=%RU64\n", cbDataRecv, cbDataTmp));
244 Assert(cbDataTmp + cbDataRecv <= pDataHdr->cbMeta);
245 cbDataTmp += cbDataRecv;
246 pvDataOff += cbDataRecv;
247 }
248 else
249 break;
250 }
251
252 if (RT_SUCCESS(rc))
253 {
254 Assert(cbDataTmp == pDataHdr->cbMeta);
255
256 LogFlowFunc(("Received %RU64 bytes of data\n", cbDataTmp));
257
258 *ppvData = pvDataTmp;
259 *pcbData = cbDataTmp;
260 }
261 else
262 RTMemFree(pvDataTmp);
263 }
264 }
265 else
266 {
267 *ppvData = NULL;
268 *pcbData = 0;
269 }
270
271 LogFlowFuncLeaveRC(rc);
272 return rc;
273}
274
275/**
276 * Main function for reading the actual meta data from the host, extended version.
277 *
278 * @returns IPRT status code.
279 * @param idClient The client id returned by VbglR3ClipboardConnect().
280 * @param pEnmType Where to store the meta data type. Optional.
281 * @param ppvData Returns the received meta data. Needs to be free'd by the caller. Optional.
282 * @param pcbData Where to store the size (in bytes) of the received meta data. Optional.
283 */
284static int vbglR3ClipboardReadMetaDataMainEx(HGCMCLIENTID idClient,
285 VBGLR3GUESTDNDMETADATATYPE *pEnmType,
286 void **ppvData,
287 uint32_t *pcbData)
288{
289 /* The rest is optional. */
290
291 VBOXDNDDATAHDR dataHdr;
292 RT_ZERO(dataHdr);
293
294 AssertMsg(pCtx->cbMaxChunkSize, ("Maximum chunk size must not be 0\n"));
295
296 dataHdr.cbMetaFmt = pCtx->cbMaxChunkSize;
297 dataHdr.pvMetaFmt = RTMemAlloc(dataHdr.cbMetaFmt);
298 if (!dataHdr.pvMetaFmt)
299 return VERR_NO_MEMORY;
300
301 DnDURIList lstURI;
302 DnDDroppedFiles droppedFiles;
303
304 void *pvData = NULL;
305 uint64_t cbData = 0;
306
307 int rc = vbglR3DnDHGRecvDataLoop(pCtx, &dataHdr, &pvData, &cbData);
308 if (RT_SUCCESS(rc))
309 {
310 /**
311 * Check if this is an URI event. If so, let VbglR3 do all the actual
312 * data transfer + file/directory creation internally without letting
313 * the caller know.
314 *
315 * This keeps the actual (guest OS-)dependent client (like VBoxClient /
316 * VBoxTray) small by not having too much redundant code.
317 */
318 Assert(dataHdr.cbMetaFmt);
319 AssertPtr(dataHdr.pvMetaFmt);
320 if (DnDMIMEHasFileURLs((char *)dataHdr.pvMetaFmt, dataHdr.cbMetaFmt)) /* URI data. */
321 {
322 AssertPtr(pvData);
323 Assert(cbData);
324
325 rc = lstURI.SetFromURIData(pvData, cbData, 0 /* fFlags */);
326 if (RT_SUCCESS(rc))
327 rc = vbglR3DnDHGRecvURIData(pCtx, &dataHdr, &droppedFiles);
328
329 if (RT_SUCCESS(rc)) /** @todo Remove this block as soon as we hand in DnDURIList. */
330 {
331 if (pvData)
332 {
333 /* Reuse data buffer to fill in the transformed URI file list. */
334 RTMemFree(pvData);
335 pvData = NULL;
336 }
337
338 RTCString strData = lstURI.GetRootEntries(droppedFiles.GetDirAbs());
339 Assert(!strData.isEmpty());
340
341 cbData = strData.length() + 1;
342 LogFlowFunc(("URI list has %zu bytes\n", cbData));
343
344 pvData = RTMemAlloc(cbData);
345 if (pvData)
346 {
347 memcpy(pvData, strData.c_str(), cbData);
348
349 if (pEnmType)
350 *pEnmType = VBGLR3GUESTDNDMETADATATYPE_URI_LIST;
351 }
352 else
353 rc = VERR_NO_MEMORY;
354 }
355 }
356 else /* Raw data. */
357 {
358 if (pEnmType)
359 *pEnmType = VBGLR3GUESTDNDMETADATATYPE_RAW;
360 }
361 }
362
363 if (dataHdr.pvMetaFmt)
364 RTMemFree(dataHdr.pvMetaFmt);
365
366 if (RT_SUCCESS(rc))
367 {
368 if ( pvData
369 && cbData)
370 {
371 if (pcbData)
372 *pcbData = cbData;
373 if (ppvData)
374 *ppvData = pvData;
375 else
376 RTMemFree(pvData);
377 }
378 }
379 else if ( RT_FAILURE(rc)
380 && rc != VERR_CANCELLED)
381 {
382 if (pvData)
383 RTMemFree(pvData);
384
385 int rc2 = VbglR3DnDHGSendProgress(pCtx, DND_PROGRESS_ERROR, 100 /* Percent */, rc);
386 if (RT_FAILURE(rc2))
387 LogFlowFunc(("Unable to send progress error %Rrc to host: %Rrc\n", rc, rc2));
388 }
389
390 LogFlowFuncLeaveRC(rc);
391 return rc;
392}
393
394/**
395 * Main function for reading the actual meta data from the host.
396 *
397 * @returns IPRT status code.
398 * @param idClient The client id returned by VbglR3ClipboardConnect().
399 * @param pMeta Where to store the actual meta data received from the host.
400 */
401static int vbglR3ClipboardReadMetaDataMain(HGCMCLIENTID idClient, PVBGLR3GUESTDNDMETADATA pMeta)
402{
403 AssertPtrReturn(pMeta, VERR_INVALID_POINTER);
404
405 int rc = vbglR3ClipboardReadMetaDataMainEx(idClient,
406 &pMeta->enmType,
407 &pMeta->pvMeta,
408 &pMeta->cbMeta);
409 return rc;
410}
411
412/**
413 * Reports (advertises) guest clipboard formats to the host.
414 *
415 * @returns VBox status code.
416 * @param idClient The client id returned by VbglR3ClipboardConnect().
417 * @param fFormats The formats to advertise.
418 */
419VBGLR3DECL(int) VbglR3ClipboardReportFormats(HGCMCLIENTID idClient, uint32_t fFormats)
420{
421 VBoxClipboardWriteFormatsMsg Msg;
422
423 VBGL_HGCM_HDR_INIT(&Msg.hdr, idClient, VBOX_SHARED_CLIPBOARD_FN_REPORT_FORMATS, 1);
424 VbglHGCMParmUInt32Set(&Msg.formats, fFormats);
425
426 return VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
427}
428
429/**
430 * Sends guest clipboard data to the host.
431 *
432 * This is usually called in reply to a VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA message
433 * from the host.
434 *
435 * @returns VBox status code.
436 * @param idClient The client id returned by VbglR3ClipboardConnect().
437 * @param fFormat The format of the data.
438 * @param pv The data.
439 * @param cb The size of the data.
440 */
441static int vbglR3ClipboardWriteDataRaw(HGCMCLIENTID idClient, uint32_t fFormat, void *pv, uint32_t cb)
442{
443 VBoxClipboardWriteDataMsg Msg;
444 VBGL_HGCM_HDR_INIT(&Msg.hdr, idClient, VBOX_SHARED_CLIPBOARD_FN_WRITE_DATA, 2);
445 VbglHGCMParmUInt32Set(&Msg.format, fFormat);
446 VbglHGCMParmPtrSet(&Msg.ptr, pv, cb);
447
448 return VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
449}
450
451/**
452 * Send guest clipboard data to the host.
453 *
454 * This is usually called in reply to a VBOX_SHARED_CLIPBOARD_HOST_MSG_READ_DATA message
455 * from the host.
456 *
457 * @returns VBox status code.
458 * @param idClient The client id returned by VbglR3ClipboardConnect().
459 * @param fFormat The format of the data.
460 * @param pv The data.
461 * @param cb The size of the data.
462 */
463VBGLR3DECL(int) VbglR3ClipboardWriteData(HGCMCLIENTID idClient, uint32_t fFormat, void *pv, uint32_t cb)
464{
465 int rc;
466#ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
467 if (fFormat == VBOX_SHARED_CLIPBOARD_FMT_URI_LIST)
468 {
469 rc = vbglR3ClipboardSendURIData(idClient, pv, cb);
470 }
471 else
472#else
473 RT_NOREF(fFormat);
474#endif
475 rc = vbglR3ClipboardWriteDataRaw(idClient, fFormat, pv, cb);
476
477#ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
478 if (RT_FAILURE(rc))
479 {
480 int rc2 = vbglR3ClipboardSendErrorInternal(idClient, rc);
481 if (RT_FAILURE(rc2))
482 LogFlowFunc(("Unable to send error (%Rrc) to host, rc=%Rrc\n", rc, rc2));
483 }
484#endif
485
486 return rc;
487}
488
489#ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
490/**
491 * Guest -> Host
492 * Utility function to send clipboard data from guest to the host.
493 *
494 * @returns IPRT status code.
495 * @param idClient The client id returned by VbglR3ClipboardConnect().
496 * @param pvData Data block to send.
497 * @param cbData Size (in bytes) of data block to send.
498 * @param pDataHdr Data header to use -- needed for accounting.
499 */
500static int vbglR3ClipboardSendDataInternal(HGCMCLIENTID idClient,
501 void *pvData, uint64_t cbData, PVBOXCLIPBOARDDATAHDR pDataHdr)
502{
503 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
504 AssertReturn(cbData, VERR_INVALID_PARAMETER);
505 AssertPtrReturn(pDataHdr, VERR_INVALID_POINTER);
506
507 VBoxClipboardWriteDataHdrMsg MsgHdr;
508 RT_ZERO(MsgHdr);
509
510 VBGL_HGCM_HDR_INIT(&MsgHdr.hdr, idClient, VBOX_SHARED_CLIPBOARD_FN_WRITE_DATA_HDR, 12);
511 MsgHdr.uContext.SetUInt32(0); /** @todo Not used yet. */
512 MsgHdr.uFlags.SetUInt32(0); /** @todo Not used yet. */
513 MsgHdr.uScreenId.SetUInt32(0); /** @todo Not used for guest->host (yet). */
514 MsgHdr.cbTotal.SetUInt64(pDataHdr->cbTotal);
515 MsgHdr.cbMeta.SetUInt32(pDataHdr->cbMeta);
516 MsgHdr.pvMetaFmt.SetPtr(pDataHdr->pvMetaFmt, pDataHdr->cbMetaFmt);
517 MsgHdr.cbMetaFmt.SetUInt32(pDataHdr->cbMetaFmt);
518 MsgHdr.cObjects.SetUInt64(pDataHdr->cObjects);
519 MsgHdr.enmCompression.SetUInt32(0); /** @todo Not used yet. */
520 MsgHdr.enmChecksumType.SetUInt32(RTDIGESTTYPE_INVALID); /** @todo Not used yet. */
521 MsgHdr.pvChecksum.SetPtr(NULL, 0); /** @todo Not used yet. */
522 MsgHdr.cbChecksum.SetUInt32(0); /** @todo Not used yet. */
523
524 int rc = VbglR3HGCMCall(&MsgHdr.hdr, sizeof(MsgHdr));
525
526 LogFlowFunc(("cbTotal=%RU64, cbMeta=%RU32, cObjects=%RU64, rc=%Rrc\n",
527 pDataHdr->cbTotal, pDataHdr->cbMeta, pDataHdr->cObjects, rc));
528
529 if (RT_SUCCESS(rc))
530 {
531 VBoxClipboardWriteDataChunkMsg MsgData;
532 RT_ZERO(MsgData);
533
534 VBGL_HGCM_HDR_INIT(&MsgData.hdr, idClient, VBOX_SHARED_CLIPBOARD_FN_WRITE_DATA_CHUNK, 5);
535 MsgData.uContext.SetUInt32(0); /** @todo Not used yet. */
536 MsgData.pvChecksum.SetPtr(NULL, 0); /** @todo Not used yet. */
537 MsgData.cbChecksum.SetUInt32(0); /** @todo Not used yet. */
538
539 uint32_t cbCurChunk;
540 const uint32_t cbMaxChunk = VBOX_SHARED_CLIPBOARD_MAX_CHUNK_SIZE;
541 uint32_t cbSent = 0;
542
543 HGCMFunctionParameter *pParm = &MsgData.pvData;
544
545 while (cbSent < cbData)
546 {
547 cbCurChunk = RT_MIN(cbData - cbSent, cbMaxChunk);
548 pParm->SetPtr(static_cast<uint8_t *>(pvData) + cbSent, cbCurChunk);
549
550 MsgData.cbData.SetUInt32(cbCurChunk);
551
552 rc = VbglR3HGCMCall(&MsgData.hdr, sizeof(MsgData));
553 if (RT_FAILURE(rc))
554 break;
555
556 cbSent += cbCurChunk;
557 }
558
559 LogFlowFunc(("cbMaxChunk=%RU32, cbData=%RU64, cbSent=%RU32, rc=%Rrc\n",
560 cbMaxChunk, cbData, cbSent, rc));
561
562 if (RT_SUCCESS(rc))
563 Assert(cbSent == cbData);
564 }
565
566 LogFlowFuncLeaveRC(rc);
567 return rc;
568}
569
570/**
571 * Guest -> Host
572 * Utility function to send a guest directory to the host.
573 *
574 * @returns IPRT status code.
575 * @param idClient The client id returned by VbglR3ClipboardConnect().
576 * @param pObj URI object containing the directory to send.
577 */
578static int vbglR3ClipboardSendDir(HGCMCLIENTID idClient, SharedClipboardURIObject *pObj)
579{
580 AssertPtrReturn(pObj, VERR_INVALID_POINTER);
581 AssertReturn(pObj->GetType() == SharedClipboardURIObject::Type_Directory, VERR_INVALID_PARAMETER);
582
583 RTCString strPath = pObj->GetDestPathAbs();
584 LogFlowFunc(("strDir=%s (%zu), fMode=0x%x\n",
585 strPath.c_str(), strPath.length(), pObj->GetMode()));
586
587 if (strPath.length() > RTPATH_MAX)
588 return VERR_INVALID_PARAMETER;
589
590 const uint32_t cbPath = (uint32_t)strPath.length() + 1; /* Include termination. */
591
592 VBoxClipboardWriteDirMsg Msg;
593 RT_ZERO(Msg);
594
595 VBGL_HGCM_HDR_INIT(&Msg.hdr, idClient, VBOX_SHARED_CLIPBOARD_FN_WRITE_DIR, 4);
596 /** @todo Context ID not used yet. */
597 Msg.pvName.SetPtr((void *)strPath.c_str(), (uint32_t)cbPath);
598 Msg.cbName.SetUInt32((uint32_t)cbPath);
599 Msg.fMode.SetUInt32(pObj->GetMode());
600
601 return VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
602}
603
604/**
605 * Guest -> Host
606 * Utility function to send a file from the guest to the host.
607 *
608 * @returns IPRT status code.
609 * @param idClient The client id returned by VbglR3ClipboardConnect().
610 * @param pObj URI object containing the file to send.
611 */
612static int vbglR3ClipboardSendFile(HGCMCLIENTID idClient, SharedClipboardURIObject *pObj)
613{
614 AssertPtrReturn(pObj, VERR_INVALID_POINTER);
615 AssertReturn(pObj->GetType() == SharedClipboardURIObject::Type_File, VERR_INVALID_PARAMETER);
616 AssertReturn(pObj->IsOpen(), VERR_INVALID_STATE);
617
618 uint32_t cbBuf = _64K; /** @todo Make this configurable? */
619 void *pvBuf = RTMemAlloc(cbBuf);
620 if (!pvBuf)
621 return VERR_NO_MEMORY;
622
623 RTCString strPath = pObj->GetDestPathAbs();
624
625 LogFlowFunc(("strFile=%s (%zu), cbSize=%RU64, fMode=0x%x\n", strPath.c_str(), strPath.length(),
626 pObj->GetSize(), pObj->GetMode()));
627
628 VBoxClipboardWriteFileHdrMsg MsgHdr;
629 RT_ZERO(MsgHdr);
630
631 VBGL_HGCM_HDR_INIT(&MsgHdr.hdr, idClient, VBOX_SHARED_CLIPBOARD_FN_WRITE_FILE_HDR, 6);
632 MsgHdr.uContext.SetUInt32(0); /* Context ID; unused at the moment. */
633 MsgHdr.pvName.SetPtr((void *)strPath.c_str(), (uint32_t)(strPath.length() + 1));
634 MsgHdr.cbName.SetUInt32((uint32_t)(strPath.length() + 1));
635 MsgHdr.uFlags.SetUInt32(0); /* Flags; unused at the moment. */
636 MsgHdr.fMode.SetUInt32(pObj->GetMode()); /* File mode */
637 MsgHdr.cbTotal.SetUInt64(pObj->GetSize()); /* File size (in bytes). */
638
639 int rc = VbglR3HGCMCall(&MsgHdr.hdr, sizeof(MsgHdr));
640
641 LogFlowFunc(("Sending file header resulted in %Rrc\n", rc));
642
643 if (RT_SUCCESS(rc))
644 {
645 /*
646 * Send the actual file data, chunk by chunk.
647 */
648 VBoxClipboardWriteFileDataMsg Msg;
649 RT_ZERO(Msg);
650
651 VBGL_HGCM_HDR_INIT(&Msg.hdr, idClient, VBOX_SHARED_CLIPBOARD_FN_WRITE_FILE_DATA, 5);
652 Msg.uContext.SetUInt32(0);
653 Msg.pvChecksum.SetPtr(NULL, 0);
654 Msg.cbChecksum.SetUInt32(0);
655
656 uint64_t cbToReadTotal = pObj->GetSize();
657 uint64_t cbWrittenTotal = 0;
658 while (cbToReadTotal)
659 {
660 uint32_t cbToRead = RT_MIN(cbToReadTotal, cbBuf);
661 uint32_t cbRead = 0;
662 if (cbToRead)
663 rc = pObj->Read(pvBuf, cbToRead, &cbRead);
664
665 LogFlowFunc(("cbToReadTotal=%RU64, cbToRead=%RU32, cbRead=%RU32, rc=%Rrc\n",
666 cbToReadTotal, cbToRead, cbRead, rc));
667
668 if ( RT_SUCCESS(rc)
669 && cbRead)
670 {
671 Msg.pvData.SetPtr(pvBuf, cbRead);
672 Msg.cbData.SetUInt32(cbRead);
673 /** @todo Calculate + set checksums. */
674
675 rc = VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
676 }
677
678 if (RT_FAILURE(rc))
679 {
680 LogFlowFunc(("Reading failed with rc=%Rrc\n", rc));
681 break;
682 }
683
684 Assert(cbRead <= cbToReadTotal);
685 cbToReadTotal -= cbRead;
686 cbWrittenTotal += cbRead;
687
688 LogFlowFunc(("%RU64/%RU64 -- %RU8%%\n", cbWrittenTotal, pObj->GetSize(), cbWrittenTotal * 100 / pObj->GetSize()));
689 };
690 }
691
692 RTMemFree(pvBuf);
693
694 LogFlowFuncLeaveRC(rc);
695 return rc;
696}
697
698/**
699 * Guest -> Host
700 * Utility function to send an URI object from guest to the host.
701 *
702 * @returns IPRT status code.
703 * @param idClient The client id returned by VbglR3ClipboardConnect().
704 * @param pObj URI object to send from guest to the host.
705 */
706static int vbglR3ClipboardSendURIObject(HGCMCLIENTID idClient, SharedClipboardURIObject *pObj)
707{
708 AssertPtrReturn(pObj, VERR_INVALID_POINTER);
709
710 int rc;
711
712 switch (pObj->GetType())
713 {
714 case SharedClipboardURIObject::Type_Directory:
715 rc = vbglR3ClipboardSendDir(idClient, pObj);
716 break;
717
718 case SharedClipboardURIObject::Type_File:
719 rc = vbglR3ClipboardSendFile(idClient, pObj);
720 break;
721
722 default:
723 AssertMsgFailed(("Object type %ld not implemented\n", pObj->GetType()));
724 rc = VERR_NOT_IMPLEMENTED;
725 break;
726 }
727
728 return rc;
729}
730
731/**
732 * Guest -> Host
733 * Utility function to send URI data from guest to the host.
734 *
735 * @returns IPRT status code.
736 * @param idClient The client id returned by VbglR3ClipboardConnect().
737 * @param pvData Block to URI data to send.
738 * @param cbData Size (in bytes) of URI data to send.
739 */
740static int vbglR3ClipboardSendURIData(HGCMCLIENTID idClient, const void *pvData, size_t cbData)
741{
742 AssertPtrReturn(pvData, VERR_INVALID_POINTER);
743 AssertReturn(cbData, VERR_INVALID_PARAMETER);
744
745 RTCList<RTCString> lstPaths =
746 RTCString((const char *)pvData, cbData).split("\r\n");
747
748 /** @todo Add symlink support (SHAREDCLIPBOARDURILIST_FLAGS_RESOLVE_SYMLINKS) here. */
749 /** @todo Add lazy loading (SHAREDCLIPBOARDURILIST_FLAGS_LAZY) here. */
750 uint32_t fFlags = SHAREDCLIPBOARDURILIST_FLAGS_KEEP_OPEN;
751
752 SharedClipboardURIList lstURI;
753 int rc = lstURI.AppendURIPathsFromList(lstPaths, fFlags);
754 if (RT_SUCCESS(rc))
755 {
756 /*
757 * Send the (meta) data; in case of URIs it's the (non-recursive) file/directory
758 * URI list the host needs to know upfront to set up the Shared Clipboard operation.
759 */
760 RTCString strRootDest = lstURI.GetRootEntries();
761 if (strRootDest.isNotEmpty())
762 {
763 void *pvURIList = (void *)strRootDest.c_str(); /* URI root list. */
764 uint32_t cbURLIist = (uint32_t)strRootDest.length() + 1; /* Include string termination. */
765
766 /* The total size also contains the size of the meta data. */
767 uint64_t cbTotal = cbURLIist;
768 cbTotal += lstURI.GetTotalBytes();
769
770 /* We're going to send an URI list in text format. */
771 const char szMetaFmt[] = "text/uri-list";
772 const uint32_t cbMetaFmt = (uint32_t)strlen(szMetaFmt) + 1; /* Include termination. */
773
774 VBOXCLIPBOARDDATAHDR dataHdr;
775 dataHdr.uFlags = 0; /* Flags not used yet. */
776 dataHdr.cbTotal = cbTotal;
777 dataHdr.cbMeta = cbURLIist;
778 dataHdr.pvMetaFmt = (void *)szMetaFmt;
779 dataHdr.cbMetaFmt = cbMetaFmt;
780 dataHdr.cObjects = lstURI.GetTotalCount();
781
782 rc = vbglR3ClipboardSendDataInternal(idClient,
783 pvURIList, cbURLIist, &dataHdr);
784 }
785 else
786 rc = VERR_INVALID_PARAMETER;
787 }
788
789 if (RT_SUCCESS(rc))
790 {
791 while (!lstURI.IsEmpty())
792 {
793 SharedClipboardURIObject *pNextObj = lstURI.First();
794
795 rc = vbglR3ClipboardSendURIObject(idClient, pNextObj);
796 if (RT_FAILURE(rc))
797 break;
798
799 lstURI.RemoveFirst();
800 }
801 }
802
803 return rc;
804}
805
806/**
807 * Guest -> Host
808 * Sends an error back to the host.
809 *
810 * @returns IPRT status code.
811 * @param idClient The client id returned by VbglR3ClipboardConnect().
812 * @param rcErr Error (IPRT-style) to send.
813 */
814static int vbglR3ClipboardSendErrorInternal(HGCMCLIENTID idClient, int rcErr)
815{
816 VBoxClipboardWriteErrorMsg Msg;
817 RT_ZERO(Msg);
818
819 VBGL_HGCM_HDR_INIT(&Msg.hdr, idClient, VBOX_SHARED_CLIPBOARD_FN_WRITE_ERROR, 2);
820 /** @todo Context ID not used yet. */
821 Msg.uContext.SetUInt32(0);
822 Msg.rc.SetUInt32((uint32_t)rcErr); /* uint32_t vs. int. */
823
824 int rc = VbglR3HGCMCall(&Msg.hdr, sizeof(Msg));
825
826 /*
827 * Never return an error if the host did not accept the error at the current
828 * time. This can be due to the host not having any appropriate callbacks
829 * set which would handle that error.
830 *
831 * bird: Looks like VERR_NOT_SUPPORTED is what the host will return if it
832 * doesn't an appropriate callback. The code used to ignore ALL errors
833 * the host would return, also relevant ones.
834 */
835 if (RT_FAILURE(rc))
836 LogFlowFunc(("Sending error %Rrc failed with rc=%Rrc\n", rcErr, rc));
837 if (rc == VERR_NOT_SUPPORTED)
838 rc = VINF_SUCCESS;
839
840 return rc;
841}
842
843/**
844 * Guest -> Host
845 * Send an error back to the host.
846 *
847 * @returns IPRT status code.
848 * @param idClient The client id returned by VbglR3ClipboardConnect().
849 * @param rcErr Error (IPRT-style) to send.
850 */
851VBGLR3DECL(int) VbglR3ClipboardSendError(HGCMCLIENTID idClient, int rcErr)
852{
853 return vbglR3ClipboardSendErrorInternal(idClient, rcErr);
854}
855#endif /* VBOX_WITH_SHARED_CLIPBOARD_URI_LIST */
856
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