VirtualBox

Changeset 103365 in vbox for trunk


Ignore:
Timestamp:
Feb 14, 2024 6:13:38 PM (12 months ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
161681
Message:

Shared Clipboard/Additions: Removed lots of code duplication for reading clipboard data from the host (partly introduced by r159772).

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/VBox/VBoxGuestLib.h

    r103170 r103365  
    704704VBGLR3DECL(int)     VbglR3ClipboardDisconnect(HGCMCLIENTID idClient);
    705705VBGLR3DECL(int)     VbglR3ClipboardGetHostMsgOld(HGCMCLIENTID idClient, uint32_t *pMsg, uint32_t *pfFormats);
    706 VBGLR3DECL(int)     VbglR3ClipboardReadData(HGCMCLIENTID idClient, uint32_t fFormat, void *pv, uint32_t cb, uint32_t *pcb);
    707 VBGLR3DECL(int)     VbglR3ClipboardReadDataEx(PVBGLR3SHCLCMDCTX pCtx, SHCLFORMAT uFormat, void *pvData, uint32_t cbData, uint32_t *pcbRead);
     706VBGLR3DECL(int)     VbglR3ClipboardReadData(HGCMCLIENTID idClient, SHCLFORMAT uFormat, void *pvData, uint32_t cbData, uint32_t *pcbRead);
     707VBGLR3DECL(int)     VbglR3ClipboardReadDataEx(PVBGLR3SHCLCMDCTX pCtx, SHCLFORMAT uFormat, void **ppvData, uint32_t *pcbData);
    708708VBGLR3DECL(int)     VbglR3ClipboardWriteData(HGCMCLIENTID idClient, uint32_t fFormat, void *pv, uint32_t cb);
    709709VBGLR3DECL(int)     VbglR3ClipboardWriteDataEx(PVBGLR3SHCLCMDCTX pCtx, SHCLFORMAT fFormat, void *pvData, uint32_t cbData);
  • trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxClipboard.cpp

    r103363 r103365  
    101101
    102102
    103 /**
    104  * Worker for a reading clipboard from the host.
    105  *
    106  * @returns VBox status code.
    107  * @retval  VERR_SHCLPB_NO_DATA if no clipboard data is available.
    108  * @param   pCtx                Shared Clipbaord context to use.
    109  * @param   uFmt                The format to read clipboard data in.
    110  * @param   ppv                 Where to return the allocated data read.
    111  *                              Must be free'd by the caller.
    112  * @param   pcb                 Where to return number of bytes read.
    113  * @param   pvUser              User-supplied context.
    114  */
    115 static DECLCALLBACK(int) vbtrReadDataWorker(PSHCLCONTEXT pCtx, SHCLFORMAT uFmt, void **ppv, uint32_t *pcb, void *pvUser)
    116 {
    117     RT_NOREF(pvUser);
    118 
    119     LogFlowFuncEnter();
    120 
    121     int rc;
    122 
    123     uint32_t cbRead = 0;
    124 
    125     uint32_t cbData = _4K; /** @todo Make this dynamic. */
    126     void    *pvData = RTMemAlloc(cbData);
    127     if (pvData)
    128     {
    129         rc = VbglR3ClipboardReadDataEx(&pCtx->CmdCtx, uFmt, pvData, cbData, &cbRead);
    130     }
    131     else
    132         rc = VERR_NO_MEMORY;
    133 
    134     /*
    135      * A return value of VINF_BUFFER_OVERFLOW tells us to try again with a
    136      * larger buffer.  The size of the buffer needed is placed in *pcb.
    137      * So we start all over again.
    138      */
    139     if (rc == VINF_BUFFER_OVERFLOW)
    140     {
    141         /* cbRead contains the size required. */
    142 
    143         cbData = cbRead;
    144         pvData = RTMemRealloc(pvData, cbRead);
    145         if (pvData)
    146         {
    147             rc = VbglR3ClipboardReadDataEx(&pCtx->CmdCtx, uFmt, pvData, cbData, &cbRead);
    148             if (rc == VINF_BUFFER_OVERFLOW)
    149                 rc = VERR_BUFFER_OVERFLOW;
    150         }
    151         else
    152             rc = VERR_NO_MEMORY;
    153     }
    154 
    155     if (!cbRead)
    156         rc = VERR_SHCLPB_NO_DATA;
    157 
    158     if (RT_SUCCESS(rc))
    159     {
    160         if (ppv)
    161             *ppv = pvData;
    162         if (pcb)
    163             *pcb = cbRead; /* Actual bytes read. */
    164     }
    165     else
    166     {
    167         /*
    168          * Catch other errors. This also catches the case in which the buffer was
    169          * too small a second time, possibly because the clipboard contents
    170          * changed half-way through the operation.  Since we can't say whether or
    171          * not this is actually an error, we just return size 0.
    172          */
    173         RTMemFree(pvData);
    174     }
    175 
    176     LogFlowFuncLeaveRC(rc);
    177     return rc;
    178 }
    179 
    180103#ifdef VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS
    181104/**
     
    291214 * Worker for a reading clipboard from the host.
    292215 *
     216 * @returns VBox status code.
     217 * @retval  VERR_SHCLPB_NO_DATA if no clipboard data is available.
     218 * @param   pCtx                Shared Clipbaord context to use.
     219 * @param   uFmt                The format to read clipboard data in.
     220 * @param   ppvData             Where to return the allocated data read.
     221 *                              Must be free'd by the caller.
     222 * @param   pcbData             Where to return number of bytes read.
     223 * @param   pvUser              User-supplied context.
     224 *
    293225 * @thread  Clipboard main thread.
     226 *
    294227 */
    295228static DECLCALLBACK(int) vbtrShClRequestDataFromSourceCallbackWorker(PSHCLCONTEXT pCtx,
    296                                                                      SHCLFORMAT uFmt, void **ppv, uint32_t *pcb, void *pvUser)
     229                                                                     SHCLFORMAT uFmt, void **ppvData, uint32_t *pcbData, void *pvUser)
    297230{
    298231    RT_NOREF(pvUser);
    299232
    300     LogFlowFunc(("pCtx=%p, uFmt=%#x\n", pCtx, uFmt));
    301 
    302     int rc = vbtrReadDataWorker(pCtx, uFmt, ppv, pcb, pvUser);
    303 
    304     if (RT_FAILURE(rc))
    305         LogRel(("Shared Clipboard: Requesting data in format %#x from host failed with %Rrc\n", uFmt, rc));
    306 
    307     LogFlowFuncLeaveRC(rc);
    308     return rc;
     233    return VbglR3ClipboardReadDataEx(&pCtx->CmdCtx, uFmt, ppvData, pcbData);
    309234}
    310235
  • trunk/src/VBox/Additions/common/VBoxGuest/lib/VBoxGuestR3LibClipboard.cpp

    r101720 r103365  
    438438 *
    439439 * @returns VBox status code.
    440  * @retval  VINF_BUFFER_OVERFLOW    If there is more data available than the caller provided buffer space for.
    441  *
     440 * @retval  VINF_BUFFER_OVERFLOW if there is more data available than the caller provided buffer space for.
    442441 * @param   idClient        The client id returned by VbglR3ClipboardConnect().
    443442 * @param   fFormat         The format we're requesting the data in.
     
    494493 * @param   pCtx                The command context returned by VbglR3ClipboardConnectEx().
    495494 * @param   uFormat             Clipboard format of clipboard data to be read.
    496  * @param   pvData              Buffer where to store the read data.
    497  * @param   cbData              Size (in bytes) of data buffer where to store the read data.
    498  * @param   pcbRead             The actual size of the host clipboard data.
     495 * @param   ppvData             Buffer where return the newly allocated read clipboard data on success.
     496 *                              Needs to be free'd by the caller.
     497 * @param   pcbData             Size (in bytes) of clipboard data read on success.
    499498 */
    500499VBGLR3DECL(int) VbglR3ClipboardReadDataEx(PVBGLR3SHCLCMDCTX pCtx,
    501                                           SHCLFORMAT uFormat, void *pvData, uint32_t cbData, uint32_t *pcbRead)
     500                                          SHCLFORMAT uFormat, void **ppvData, uint32_t *pcbData)
    502501{
    503502    AssertPtrReturn(pCtx,   VERR_INVALID_POINTER);
    504     AssertPtrReturn(pvData, VERR_INVALID_POINTER);
    505     return VbglR3ClipboardReadData(pCtx->idClient, uFormat, pvData, cbData, pcbRead);
     503    AssertPtrReturn(ppvData, VERR_INVALID_POINTER);
     504    AssertReturn(pcbData, VERR_INVALID_PARAMETER);
     505
     506    int rc;
     507
     508    uint32_t cbRead = 0;
     509    uint32_t cbData = _4K;
     510
     511    void *pvData = RTMemAlloc(cbData);
     512    if (pvData)
     513    {
     514        rc = VbglR3ClipboardReadData(pCtx->idClient, uFormat, pvData, cbData, &cbRead);
     515
     516        /*
     517         * A return value of VINF_BUFFER_OVERFLOW tells us to try again with a
     518         * larger buffer.  The size of the buffer needed is placed in *pcb.
     519         * So we start all over again.
     520         */
     521        if (   rc == VINF_BUFFER_OVERFLOW
     522            && cbRead)
     523        {
     524            /* cbRead contains the size required. */
     525            pvData = RTMemReallocZ(pvData, cbData, cbRead);
     526            cbData = cbRead;
     527            if (pvData)
     528            {
     529                rc = VbglR3ClipboardReadData(pCtx->idClient, uFormat, pvData, cbData, &cbRead);
     530                if (RT_SUCCESS(rc))
     531                {
     532                    if (cbRead != cbData) /* The data size must match now. */
     533                        rc = VERR_MISMATCH;
     534                }
     535                else
     536                {
     537                    if (rc == VINF_BUFFER_OVERFLOW)
     538                        rc = VERR_BUFFER_OVERFLOW;
     539                }
     540            }
     541            else
     542                rc = VERR_NO_MEMORY;
     543        }
     544
     545        if (RT_SUCCESS(rc))
     546        {
     547            *pcbData = cbRead; /* Actual bytes read. */
     548            *ppvData = pvData;
     549        }
     550    }
     551    else
     552        rc = VERR_NO_MEMORY;
     553
     554    if (!cbRead)
     555        rc = VERR_SHCLPB_NO_DATA;
     556
     557    if (RT_FAILURE(rc))
     558    {
     559        RTMemFree(pvData);
     560        LogRel(("Shared Clipboard: Reading clipboard data in format %#x from host failed with %Rrc\n", uFormat, rc));
     561    }
     562
     563    return rc;
    506564}
    507565
  • trunk/src/VBox/Additions/x11/VBoxClient/clipboard-common.cpp

    r103364 r103365  
    110110}
    111111
    112 /**
    113  * Reads clipboard from the host.
    114  *
    115  * @returns VBox status code.
    116  * @retval  VERR_SHCLPB_NO_DATA if no clipboard data is available.
    117  * @param   pCtx                Shared Clipbaord context to use.
    118  * @param   uFmt                The format to read clipboard data in.
    119  * @param   ppv                 Where to return the allocated data read.
    120  *                              Must be free'd by the caller.
    121  * @param   pcb                 Where to return number of bytes read.
    122  */
    123 RTDECL(int) VBClClipboardReadHostClipboard(PVBGLR3SHCLCMDCTX pCtx, SHCLFORMAT uFmt, void **ppv, uint32_t *pcb)
     112RTDECL(int) VBClClipboardReadHostClipboard(PVBGLR3SHCLCMDCTX pCtx,
     113                                           SHCLFORMAT uFmt, void **ppvData, uint32_t *pcbData)
    124114{
    125     int rc;
     115    return VbglR3ClipboardReadDataEx(pCtx, uFmt, ppvData, pcbData);
     116}
    126117
    127     uint32_t cbRead = 0;
    128     uint32_t cbData = _4K;
    129 
    130     void *pvData;
    131 
    132     pvData = RTMemAllocZ(cbData);
    133     if (pvData)
    134     {
    135         rc = VbglR3ClipboardReadDataEx(pCtx, uFmt, pvData, cbData, &cbRead);
    136 
    137         /*
    138          * A return value of VINF_BUFFER_OVERFLOW tells us to try again with a
    139          * larger buffer.  The size of the buffer needed is placed in *pcb.
    140          * So we start all over again.
    141          */
    142         if (rc == VINF_BUFFER_OVERFLOW)
    143         {
    144             /* cbRead contains the size required. */
    145             pvData = RTMemReallocZ(pvData, cbData, cbRead);
    146             cbData = cbRead;
    147             if (pvData)
    148             {
    149                 rc = VbglR3ClipboardReadDataEx(pCtx, uFmt, pvData, cbData, &cbRead);
    150                 if (rc == VINF_BUFFER_OVERFLOW)
    151                     rc = VERR_BUFFER_OVERFLOW;
    152             }
    153             else
    154                 rc = VERR_NO_MEMORY;
    155         }
    156 
    157         if (RT_SUCCESS(rc))
    158         {
    159             *pcb = cbRead; /* Actual bytes read. */
    160             *ppv = pvData;
    161         }
    162     }
    163     else
    164         rc = VERR_NO_MEMORY;
    165 
    166     if (!cbRead)
    167         rc = VERR_SHCLPB_NO_DATA;
    168 
    169     if (RT_FAILURE(rc))
    170         RTMemFree(pvData);
    171 
    172     return rc;
    173 }
  • trunk/src/VBox/Additions/x11/VBoxClient/clipboard-x11.cpp

    r103363 r103365  
    206206 * Worker for a reading clipboard from the host.
    207207 */
    208 static DECLCALLBACK(int) vbclX11ReadDataWorker(PSHCLCONTEXT pCtx, SHCLFORMAT uFmt, void **ppv, uint32_t *pcb, void *pvUser)
     208static DECLCALLBACK(int) vbclX11ReadDataWorker(PSHCLCONTEXT pCtx,
     209                                               SHCLFORMAT uFmt, void **ppvData, uint32_t *pcbData, void *pvUser)
    209210{
    210211    RT_NOREF(pvUser);
    211212
    212     LogFlowFuncEnter();
    213 
    214     int rc;
    215 
    216     uint32_t cbRead = 0;
    217 
    218     uint32_t cbData = _4K; /** @todo Make this dynamic. */
    219     void    *pvData = RTMemAlloc(cbData);
    220     if (pvData)
    221     {
    222         rc = VbglR3ClipboardReadDataEx(&pCtx->CmdCtx, uFmt, pvData, cbData, &cbRead);
    223     }
    224     else
    225         rc = VERR_NO_MEMORY;
    226 
    227     /*
    228      * A return value of VINF_BUFFER_OVERFLOW tells us to try again with a
    229      * larger buffer.  The size of the buffer needed is placed in *pcb.
    230      * So we start all over again.
    231      */
    232     if (rc == VINF_BUFFER_OVERFLOW)
    233     {
    234         /* cbRead contains the size required. */
    235 
    236         cbData = cbRead;
    237         pvData = RTMemRealloc(pvData, cbRead);
    238         if (pvData)
    239         {
    240             rc = VbglR3ClipboardReadDataEx(&pCtx->CmdCtx, uFmt, pvData, cbData, &cbRead);
    241             if (rc == VINF_BUFFER_OVERFLOW)
    242                 rc = VERR_BUFFER_OVERFLOW;
    243         }
    244         else
    245             rc = VERR_NO_MEMORY;
    246     }
    247 
    248     if (!cbRead)
    249         rc = VERR_SHCLPB_NO_DATA;
    250 
    251     if (RT_SUCCESS(rc))
    252     {
    253         if (ppv)
    254             *ppv = pvData;
    255         if (pcb)
    256             *pcb = cbRead; /* Actual bytes read. */
    257     }
    258     else
    259     {
    260         /*
    261          * Catch other errors. This also catches the case in which the buffer was
    262          * too small a second time, possibly because the clipboard contents
    263          * changed half-way through the operation.  Since we can't say whether or
    264          * not this is actually an error, we just return size 0.
    265          */
    266         RTMemFree(pvData);
    267     }
    268 
    269     LogFlowFuncLeaveRC(rc);
    270     return rc;
     213    return VbglR3ClipboardReadDataEx(&pCtx->CmdCtx, uFmt, ppvData, pcbData);
    271214}
    272215
  • trunk/src/VBox/Additions/x11/VBoxClient/clipboard.h

    r102027 r103365  
    114114 * @param   pCtx            Host Shared Clipboard service connection context.
    115115 * @param   uFmt            Format in which data should be read.
    116  * @param   ppv             Newly allocated output buffer (should be freed by caller).
    117  * @param   pcb             Output buffer size.
     116 * @param   ppvData         Newly allocated output buffer (should be freed by caller).
     117 * @param   pcbData         Output buffer size.
    118118 */
    119 RTDECL(int) VBClClipboardReadHostClipboard(PVBGLR3SHCLCMDCTX pCtx, SHCLFORMAT uFmt, void **ppv, uint32_t *pcb);
     119RTDECL(int) VBClClipboardReadHostClipboard(PVBGLR3SHCLCMDCTX pCtx, SHCLFORMAT uFmt, void **ppvData, uint32_t *pcbData);
    120120
    121121#endif /* !GA_INCLUDED_SRC_x11_VBoxClient_clipboard_h */
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