VirtualBox

Ignore:
Timestamp:
Jun 4, 2019 4:51:48 PM (6 years ago)
Author:
vboxsync
Message:

Shared Clipboard/URI: Update.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/GuestHost/SharedClipboard/clipboard-common.cpp

    r78161 r78974  
    2121#include <iprt/assert.h>
    2222#include <iprt/errcore.h>
     23
    2324#include <VBox/log.h>
    2425#include <VBox/GuestHost/clipboard-helper.h>
     26#ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
     27# include <VBox/GuestHost/SharedClipboard-uri.h>
     28#endif
     29
    2530
    2631/** @todo use const where appropriate; delinuxify the code (*Lin* -> *Host*); use AssertLogRel*. */
     
    307312    pFileHeader = (PBMFILEHEADER)pvDest;
    308313    pFileHeader->u16Type        = BITMAPHEADERMAGIC;
    309     pFileHeader->u32Size        = RT_H2LE_U32(cb);
     314    pFileHeader->u32Size        = (uint32_t)RT_H2LE_U32(cb);
    310315    pFileHeader->u16Reserved1   = pFileHeader->u16Reserved2 = 0;
    311     pFileHeader->u32OffBits     = RT_H2LE_U32(offPixel);
     316    pFileHeader->u32OffBits     = (uint32_t)RT_H2LE_U32(offPixel);
    312317    memcpy((uint8_t *)pvDest + sizeof(BMFILEHEADER), pvSrc, cbSrc);
    313318    *ppvDest = pvDest;
     
    336341}
    337342
     343#ifdef VBOX_WITH_SHARED_CLIPBOARD_URI_LIST
     344/**
     345 * Initializes an URI clipboard transfer struct.
     346 *
     347 * @returns VBox status code.
     348 * @param   pCtx                Shared Clipboard provider creation context to use.
     349 * @param   ppTransfer          Where to return the created URI transfer struct.
     350 *                              Must be free'd by SharedClipboardURITransferDestroy().
     351 */
     352int SharedClipboardURITransferCreate(PSHAREDCLIPBOARDPROVIDERCREATIONCTX pCtx,
     353                                     PSHAREDCLIPBOARDURITRANSFER *ppTransfer)
     354{
     355    AssertPtrReturn(pCtx,       VERR_INVALID_POINTER);
     356    AssertPtrReturn(ppTransfer, VERR_INVALID_POINTER);
     357
     358    PSHAREDCLIPBOARDURITRANSFER pTransfer = (PSHAREDCLIPBOARDURITRANSFER)RTMemAlloc(sizeof(SHAREDCLIPBOARDURITRANSFER));
     359    if (!pTransfer)
     360        return VERR_NO_MEMORY;
     361
     362    LogFlowFuncEnter();
     363
     364    pTransfer->pProvider = SharedClipboardProvider::Create(pCtx);
     365    if (!pTransfer->pProvider)
     366        return VERR_NO_MEMORY;
     367
     368    pTransfer->Thread.fCancelled = false;
     369    pTransfer->Thread.fStarted   = false;
     370
     371    pTransfer->pvUser = NULL;
     372    pTransfer->cbUser = 0;
     373
     374    *ppTransfer = pTransfer;
     375
     376    return VINF_SUCCESS;
     377}
     378
     379/**
     380 * Destroys an URI clipboard transfer context struct.
     381 *
     382 * @param   pURI                URI clipboard transfer struct to destroy.
     383 */
     384void SharedClipboardURITransferDestroy(PSHAREDCLIPBOARDURITRANSFER pTransfer)
     385{
     386    if (!pTransfer)
     387        return;
     388
     389    LogFlowFuncEnter();
     390
     391    if (pTransfer->pProvider)
     392    {
     393        delete pTransfer->pProvider;
     394        pTransfer->pProvider = NULL;
     395    }
     396
     397    RTMemFree(pTransfer);
     398    pTransfer = NULL;
     399}
     400
     401/**
     402 * Resets an URI clipboard context struct.
     403 *
     404 * @param   pTransfer           URI clipboard transfer struct to reset.
     405 */
     406void SharedClipboardURITransferReset(PSHAREDCLIPBOARDURITRANSFER pTransfer)
     407{
     408    AssertPtrReturnVoid(pTransfer);
     409
     410    LogFlowFuncEnter();
     411
     412    if (pTransfer->pProvider)
     413        pTransfer->pProvider->Reset();
     414}
     415
     416/**
     417 * Writes all URI objects using the connected provider.
     418 *
     419 * @returns VBox status code.
     420 * @param   pTransfer           Transfer to write objects for.
     421 */
     422int SharedClipboardURITransferWrite(PSHAREDCLIPBOARDURITRANSFER pTransfer)
     423{
     424    AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
     425
     426    LogFlowFuncEnter();
     427
     428    int rc = VINF_SUCCESS;
     429
     430    SharedClipboardURIList &lstURI = pTransfer->pProvider->GetURIList();
     431
     432    while (!lstURI.IsEmpty())
     433    {
     434        SharedClipboardURIObject *pObj = lstURI.First();
     435        AssertPtrBreakStmt(pObj, rc = VERR_INVALID_POINTER);
     436
     437        switch (pObj->GetType())
     438        {
     439            case SharedClipboardURIObject::Type_Directory:
     440            {
     441                rc = pTransfer->pProvider->WriteDirectoryObj(*pObj);
     442                break;
     443            }
     444
     445            case SharedClipboardURIObject::Type_File:
     446            {
     447                rc = pTransfer->pProvider->WriteFileHdrObj(*pObj);
     448                if (RT_FAILURE(rc))
     449                    break;
     450
     451                while (!pObj->IsComplete())
     452                {
     453                    uint32_t cbWritten;
     454                    rc = pTransfer->pProvider->WriteFileDataObj(*pObj, &cbWritten);
     455                    if (RT_FAILURE(rc))
     456                        break;
     457                }
     458                break;
     459            }
     460
     461            default:
     462                AssertFailed();
     463                break;
     464        }
     465
     466        if (RT_FAILURE(rc))
     467            break;
     468
     469        /* Only remove current object on success. */
     470        lstURI.RemoveFirst();
     471    }
     472
     473    LogFlowFuncLeaveRC(rc);
     474    return rc;
     475}
     476
     477/**
     478 * Thread for transferring (writing) URI objects from source to the target.
     479 * For target to source transfers we utilize our own IDataObject / IStream implementations.
     480 *
     481 * @returns VBox status code.
     482 * @param   hThread             Thread handle.
     483 * @param   pvUser              User arguments; is PSHAREDCLIPBOARDURICTX.
     484 */
     485int SharedClipboardURITransferWriteThread(RTTHREAD hThread, void *pvUser)
     486{
     487    AssertPtrReturn(pvUser, VERR_INVALID_POINTER);
     488
     489    LogFlowFuncEnter();
     490
     491    PSHAREDCLIPBOARDURICTX pURICtx = (PSHAREDCLIPBOARDURICTX)pvUser;
     492    AssertPtr(pURICtx);
     493
     494    /* At the moment we only support one transfer at a time. */
     495    PSHAREDCLIPBOARDURITRANSFER pTransfer = SharedClipboardURICtxGetTransfer(pURICtx, 0 /* Index*/);
     496    AssertPtr(pTransfer->pProvider);
     497
     498    int rc = VINF_SUCCESS;
     499
     500    if (RT_SUCCESS(rc))
     501        pTransfer->Thread.fStarted = true;
     502
     503    int rc2 = RTThreadUserSignal(hThread);
     504    const bool fSignalled = RT_SUCCESS(rc2);
     505
     506    if (RT_SUCCESS(rc))
     507        rc = SharedClipboardURITransferWrite(pTransfer);
     508
     509    if (!fSignalled)
     510    {
     511        rc2 = RTThreadUserSignal(hThread);
     512        AssertRC(rc2);
     513    }
     514
     515    LogFlowFuncLeaveRC(rc);
     516    return rc;
     517}
     518
     519/**
     520 * Initializes an URI clipboard context struct.
     521 *
     522 * @returns VBox status code.
     523 * @param   pURI                URI clipboard context to initialize.
     524 * @param   pTransfer           Pointer to URI clipboard transfer struct to use.
     525 */
     526int SharedClipboardURICtxInit(PSHAREDCLIPBOARDURICTX pURI, PSHAREDCLIPBOARDURITRANSFER pTransfer)
     527{
     528    AssertPtrReturn(pURI,      VERR_INVALID_POINTER);
     529    AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
     530
     531    LogFlowFuncEnter();
     532
     533    int rc = RTCritSectInit(&pURI->CritSect);
     534    if (RT_SUCCESS(rc))
     535    {
     536        RTListInit(&pURI->List);
     537
     538        SharedClipboardURICtxReset(pURI);
     539    }
     540
     541    return VINF_SUCCESS;
     542}
     543
     544/**
     545 * Destroys an URI clipboard information context struct.
     546 *
     547 * @param   pURI                URI clipboard context to destroy.
     548 */
     549void SharedClipboardURICtxDestroy(PSHAREDCLIPBOARDURICTX pURI)
     550{
     551    AssertPtrReturnVoid(pURI);
     552
     553    RTCritSectDelete(&pURI->CritSect);
     554
     555    PSHAREDCLIPBOARDURITRANSFER pTransfer, pTransferNext;
     556    RTListForEachSafe(&pURI->List, pTransfer, pTransferNext, SHAREDCLIPBOARDURITRANSFER, Node)
     557    {
     558        SharedClipboardURITransferDestroy(pTransfer);
     559        RTListNodeRemove(&pTransfer->Node);
     560    }
     561
     562    LogFlowFuncEnter();
     563}
     564
     565/**
     566 * Resets an URI clipboard context struct.
     567 *
     568 * @param   pURI                URI clipboard context to reset.
     569 */
     570void SharedClipboardURICtxReset(PSHAREDCLIPBOARDURICTX pURI)
     571{
     572    AssertPtrReturnVoid(pURI);
     573
     574    LogFlowFuncEnter();
     575
     576    pURI->cTransfers = 0;
     577
     578    PSHAREDCLIPBOARDURITRANSFER pTransfer;
     579    RTListForEach(&pURI->List, pTransfer, SHAREDCLIPBOARDURITRANSFER, Node)
     580        SharedClipboardURITransferReset(pTransfer);
     581}
     582
     583/**
     584 * Adds a new transfer to an URI clipboard context struct.
     585 *
     586 * @returns VBox status code.
     587 * @param   pURI                URI clipboard context to add transfer to.
     588 * @param   pTransfer           Pointer to URI clipboard transfer struct to add.
     589 */
     590int SharedClipboardURICtxTransferAdd(PSHAREDCLIPBOARDURICTX pURI, PSHAREDCLIPBOARDURITRANSFER pTransfer)
     591{
     592    AssertPtrReturn(pURI,      VERR_INVALID_POINTER);
     593    AssertPtrReturn(pTransfer, VERR_INVALID_POINTER);
     594
     595    LogFlowFuncEnter();
     596
     597    RTListAppend(&pURI->List, &pTransfer->Node);
     598
     599    return VINF_SUCCESS;
     600}
     601
     602/**
     603 * Returns a specific URI transfer.
     604 *
     605 * @returns URI transfer, or NULL if not found.
     606 * @param   pURI                URI clipboard context to return transfer for.
     607 * @param   uIdx                Index of the transfer to return.
     608 */
     609PSHAREDCLIPBOARDURITRANSFER SharedClipboardURICtxGetTransfer(PSHAREDCLIPBOARDURICTX pURI, uint32_t uIdx)
     610{
     611    AssertReturn(uIdx == 0, NULL); /* Only one transfer allowed at the moment. */
     612    return RTListGetFirst(&pURI->List, SHAREDCLIPBOARDURITRANSFER, Node);
     613}
     614
     615/**
     616 * Returns the number of active URI transfers.
     617 *
     618 * @returns VBox status code.
     619 * @param   pURI                URI clipboard context to return number for.
     620 */
     621uint32_t SharedClipboardURICtxGetActiveTransfers(PSHAREDCLIPBOARDURICTX pURI)
     622{
     623    return pURI->cTransfers;
     624}
     625#endif /* VBOX_WITH_SHARED_CLIPBOARD_URI_LIST */
     626
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette