VirtualBox

Changeset 103615 in vbox


Ignore:
Timestamp:
Feb 29, 2024 6:36:37 PM (9 months ago)
Author:
vboxsync
Message:

Shared Clipboard/Transfers: Made code a bit easier to follow / read, removed some unnecessary locking (cleanup). bugref:9437

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/VBox/GuestHost/SharedClipboard-win.h

    r103450 r103615  
    499499void SharedClipboardWinTransferDestroy(PSHCLWINCTX pWinCtx, PSHCLTRANSFER pTransfer);
    500500
    501 int SharedClipboardWinTransferHandOffToDataObject(PSHCLWINCTX pWinCtx, PSHCLTRANSFER pTransfer);
    502501int SharedClipboardWinTransferCreateAndSetDataObject(PSHCLWINCTX pWinCtx, PSHCLCONTEXT pCtx, SharedClipboardWinDataObject::PCALLBACKS pCallbacks);
     502int SharedClipboardWinTransferInitialize(PSHCLWINCTX pWinCtx, PSHCLTRANSFER pTransfer);
     503int SharedClipboardWinTransferStart(PSHCLWINCTX pWinCtx, PSHCLTRANSFER pTransfer);
    503504# endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
    504505#endif /* !VBOX_INCLUDED_GuestHost_SharedClipboard_win_h */
  • trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxClipboard.cpp

    r103450 r103615  
    165165 *
    166166 * Called by ShClTransferInit via VbglR3.
    167  * For G->H: Called on transfer intialization to notify the "in-flight" IDataObject about a data transfer.
    168  * For H->G: Called on transfer intialization to populate the transfer's root list.
     167 * For H->G: Called on transfer intialization to initialize the "in-flight" IDataObject for a data transfer.
     168 * For G->H: Called on transfer intialization to populate the transfer's root list.
    169169 *
    170170 * @thread  Clipboard main thread.
     
    184184    switch(ShClTransferGetDir(pTransfer))
    185185    {
    186         case SHCLTRANSFERDIR_FROM_REMOTE: /* G->H */
    187         {
    188             rc = SharedClipboardWinTransferHandOffToDataObject(&pCtx->Win, pTransfer);
    189             break;
    190         }
    191 
    192         case SHCLTRANSFERDIR_TO_REMOTE: /* H->G */
     186        case SHCLTRANSFERDIR_FROM_REMOTE: /* H->G */
     187        {
     188            rc = SharedClipboardWinTransferInitialize(&pCtx->Win, pTransfer);
     189            break;
     190        }
     191
     192        case SHCLTRANSFERDIR_TO_REMOTE: /* G->H */
    193193        {
    194194            rc = SharedClipboardWinTransferGetRootsFromClipboard(&pCtx->Win, pTransfer);
     
    202202    LogFlowFuncLeaveRC(rc);
    203203    return rc;
     204}
     205
     206/**
     207 * @copydoc SHCLTRANSFERCALLBACKS::pfnOnInitialized
     208 *
     209 * Called by ShClTransferInit via VbglR3.
     210 * For H->G: Called on transfer intialization to start the data transfer for the "in-flight" IDataObject.
     211 * For G->H: Nothing to do here.
     212 *
     213 * @thread  Clipboard main thread.
     214 */
     215static DECLCALLBACK(void) vbtrShClTransferInitializedCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx)
     216{
     217    LogFlowFuncEnter();
     218
     219    int rc = VINF_SUCCESS;
     220
     221    PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pCbCtx->pvUser;
     222    AssertPtr(pCtx);
     223
     224    PSHCLTRANSFER pTransfer = pCbCtx->pTransfer;
     225    AssertPtr(pTransfer);
     226
     227    switch(ShClTransferGetDir(pTransfer))
     228    {
     229        case SHCLTRANSFERDIR_FROM_REMOTE: /* H->G */
     230        {
     231            rc = SharedClipboardWinTransferStart(&pCtx->Win, pTransfer);
     232            break;
     233        }
     234
     235        case SHCLTRANSFERDIR_TO_REMOTE: /* G->H */
     236            break;
     237
     238        default:
     239            break;
     240    }
     241
     242    LogFlowFuncLeaveRC(rc);
    204243}
    205244
     
    9731012    pCtx->CmdCtx.Transfers.Callbacks.pfnOnDestroy     = vbtrShClTransferDestroyCallback;
    9741013    pCtx->CmdCtx.Transfers.Callbacks.pfnOnInitialize  = vbtrShClTransferInitializeCallback;
     1014    pCtx->CmdCtx.Transfers.Callbacks.pfnOnInitialized = vbtrShClTransferInitializedCallback;
    9751015    pCtx->CmdCtx.Transfers.Callbacks.pfnOnStarted     = vbtrShClTransferStartedCallback;
    9761016    pCtx->CmdCtx.Transfers.Callbacks.pfnOnCompleted   = vbtrShClTransferCompletedCallback;
  • trunk/src/VBox/GuestHost/SharedClipboard/ClipboardDataObjectImpl-win.cpp

    r103484 r103615  
    219219
    220220        lock();
    221 
    222         /* Make sure to release the transfer. */
    223         setTransferLocked(NULL);
    224     }
     221    }
     222
     223    /* Make sure to release the transfer in any state. */
     224    setTransferLocked(NULL);
    225225
    226226    unlock();
     
    280280        m_pStgMedium = NULL;
    281281    }
    282 
    283     if (m_pTransfer)
    284         ShClTransferRelease(m_pTransfer);
    285282
    286283    FsObjEntryList::const_iterator itRoot = m_lstEntries.cbegin();
     
    566563            if (RT_SUCCESS(rc))
    567564            {
    568                 pThis->lock();
    569 
    570565                AssertReleaseMsg(pThis->m_lstEntries.size(),
    571566                                 ("Shared Clipboard: No transfer root entries found -- should not happen, please file a bug report\n"));
     
    578573                        break;
    579574
    580                     pThis->unlock();
    581 
    582575                    /* Transferring stuff can take a while, so don't use any timeout here. */
    583576                    rc = RTSemEventWait(pThis->m_EventStatusChanged, RT_INDEFINITE_WAIT);
    584 
    585                     pThis->lock();
    586 
    587577                    if (RT_FAILURE(rc))
    588578                        break;
     
    620610                            break;
    621611                    }
    622 
    623                     pThis->unlock();
    624612
    625613                    if (pThis->m_Callbacks.pfnTransferEnd)
     
    630618                    }
    631619
    632                     pThis->lock();
    633 
    634620                    break;
    635                 }
    636 
    637                 pThis->unlock();
     621                } /* for */
    638622            }
    639623        }
     
    806790    int rc = VINF_SUCCESS;
    807791
    808     /* Pre-check -- see if the data object still is alive. */
    809     if (m_enmStatus == Uninitialized)
    810         rc = VERR_OBJECT_DESTROYED;
    811 
    812792    if (    RT_SUCCESS(rc)
    813793        && (   pFormatEtc->cfFormat == m_cfFileDescriptorA
     
    841821                lock();
    842822
     823                LogFunc(("Wait resulted in %Rrc and status %#x\n", rc, m_enmStatus));
     824
    843825                if (RT_FAILURE(rc))
    844826                {
     
    864846                const bool fUnicode = pFormatEtc->cfFormat == m_cfFileDescriptorW;
    865847
     848                /* Leave lock while waiting. */
     849                unlock();
     850
    866851                SHCLTRANSFERSTATUS const enmTransferStatus = ShClTransferGetStatus(m_pTransfer);
    867                 RT_NOREF(enmTransferStatus);
    868852
    869853                LogFlowFunc(("FormatIndex_FileDescriptor%s, enmTransferStatus=%s\n",
     
    871855
    872856                /* The caller can call GetData() several times, so make sure we don't do the same transfer multiple times. */
    873                 if (ShClTransferGetStatus(m_pTransfer) != SHCLTRANSFERSTATUS_STARTED)
     857                if (enmTransferStatus != SHCLTRANSFERSTATUS_STARTED)
    874858                {
    875859                    /* Start the transfer + run it asynchronously in a separate thread. */
     
    880864                        if (RT_SUCCESS(rc))
    881865                        {
    882                             /* Leave lock while waiting. */
    883                             unlock();
    884 
    885866                            /* Don't block for too long here, as this also will screw other apps running on the OS. */
    886867                            LogRel2(("Shared Clipboard: Waiting for IDataObject listing to arrive ...\n"));
     
    893874                                || m_enmStatus != Running) /* Still in running state? */
    894875                            {
    895                                 rc = VERR_OBJECT_DESTROYED;
     876                                rc = VERR_SHCLPB_NO_DATA;
    896877                                break;
    897878                            }
     879
     880                            unlock();
    898881                        }
    899882                    }
    900883                }
     884
     885                lock();
    901886
    902887                if (RT_SUCCESS(rc))
     
    11471132 * @returns VBox status code.
    11481133 * @param   pTransfer           Transfer to assign.
    1149  *                              Must be in INITIALIZED state.
    11501134 *                              When set to NULL, the transfer will be released from the object.
    11511135 */
     
    11541138    AssertReturn(RTCritSectIsOwned(&m_CritSect), VERR_WRONG_ORDER);
    11551139
    1156     LogFlowFunc(("pTransfer=%p\n", pTransfer));
     1140    LogFunc(("pTransfer=%p\n", pTransfer));
    11571141
    11581142    int rc = VINF_SUCCESS;
     
    11641148        if (m_enmStatus == Initialized)
    11651149        {
    1166             SHCLTRANSFERSTATUS const enmSts = ShClTransferGetStatus(pTransfer);
    1167             AssertMsgStmt(enmSts == SHCLTRANSFERSTATUS_INITIALIZED, /* Transfer must not be started yet. */
    1168                           ("Transfer has wrong status (%#x)\n", enmSts), rc = VERR_WRONG_ORDER);
    1169             if (RT_SUCCESS(rc))
    1170             {
    1171                 m_pTransfer = pTransfer;
    1172 
    1173                 SharedClipboardWinTransferCtx *pWinURITransferCtx = (SharedClipboardWinTransferCtx *)pTransfer->pvUser;
    1174                 AssertPtr(pWinURITransferCtx);
    1175 
    1176                 pWinURITransferCtx->pDataObj = this; /* Save a backref to this object. */
    1177 
    1178                 ShClTransferAcquire(pTransfer);
    1179             }
     1150            m_pTransfer = pTransfer;
     1151
     1152            SharedClipboardWinTransferCtx *pWinURITransferCtx = (SharedClipboardWinTransferCtx *)pTransfer->pvUser;
     1153            AssertPtr(pWinURITransferCtx);
     1154
     1155            pWinURITransferCtx->pDataObj = this; /* Save a backref to this object. */
     1156
     1157            ShClTransferAcquire(pTransfer);
    11801158        }
    11811159        else
     
    12081186 * @returns VBox status code.
    12091187 * @param   pTransfer           Transfer to assign.
    1210  *                              Must be in INITIALIZED state.
    12111188 *                              When set to NULL, the transfer will be released from the object.
    12121189 */
  • trunk/src/VBox/GuestHost/SharedClipboard/clipboard-transfers.cpp

    r103480 r103615  
    12341234
    12351235    int rc = VINF_SUCCESS;
     1236
    12361237    if (pTransfer->Callbacks.pfnOnInitialize)
    12371238        rc = pTransfer->Callbacks.pfnOnInitialize(&pTransfer->CallbackCtx);
    1238 
    12391239    if (RT_SUCCESS(rc))
    12401240    {
     
    12441244            AssertMsgStmt(ShClTransferRootsCount(pTransfer), ("Transfer has no root entries set\n"), rc = VERR_WRONG_ORDER);
    12451245
    1246         rc = shClTransferSetStatus(pTransfer, SHCLTRANSFERSTATUS_INITIALIZED);
    1247 
    1248         if (   RT_SUCCESS(rc)
    1249             && pTransfer->Callbacks.pfnOnInitialized)
    1250             pTransfer->Callbacks.pfnOnInitialized(&pTransfer->CallbackCtx);
     1246        if (RT_SUCCESS(rc))
     1247            rc = shClTransferSetStatus(pTransfer, SHCLTRANSFERSTATUS_INITIALIZED);
    12511248    }
    12521249
    12531250    shClTransferUnlock(pTransfer);
    12541251
     1252    /* Note: Callback will be called after we unlocked the transfer, as the caller might access the transfer right away. */
     1253    if (   RT_SUCCESS(rc)
     1254        && pTransfer->Callbacks.pfnOnInitialized)
     1255        pTransfer->Callbacks.pfnOnInitialized(&pTransfer->CallbackCtx);
     1256
    12551257    if (RT_FAILURE(rc))
    1256         LogRel2(("Shared Clipboard: Initialziation of transfer failed with %Rrc\n", rc));
     1258        LogRel(("Shared Clipboard: Initialziation of transfer failed with %Rrc\n", rc));
    12571259
    12581260    LogFlowFuncLeaveRC(rc);
  • trunk/src/VBox/GuestHost/SharedClipboard/clipboard-win.cpp

    r103452 r103615  
    11921192
    11931193/**
    1194  * Hands off a transfer to the current data object in-flight.
     1194 * Initializes a Windows transfer for a given data object.
    11951195 *
    11961196 * @returns VBox status code.
    11971197 * @param   pWinCtx             Windows context to use.
    1198  * @param   pTransfer           Transfer to hand off to the data object.
    1199  *
    1200  * @note    The data object will be put into running state on success and handed over to Windows.
    1201  *          Our data object pointer will be invalid afterwards.
    1202  */
    1203 int SharedClipboardWinTransferHandOffToDataObject(PSHCLWINCTX pWinCtx, PSHCLTRANSFER pTransfer)
     1198 * @param   pTransfer           Transfer to initialize for the data object.
     1199 * @param   pObj                Data object to initialize transfer for.
     1200 */
     1201static int sharedClipboardWinTransferInitializeInternal(PSHCLWINCTX pWinCtx, PSHCLTRANSFER pTransfer,
     1202                                                        SharedClipboardWinDataObject *pObj)
     1203{
     1204    RT_NOREF(pWinCtx);
     1205
     1206    return pObj->SetTransfer(pTransfer);
     1207}
     1208
     1209/**
     1210 * Initializes a Windows transfer for the current data object in-flight.
     1211 *
     1212 * @returns VBox status code.
     1213 * @retval  VERR_WRONG_ORDER if no current in-flight data object is available.
     1214 * @param   pWinCtx             Windows context to use.
     1215 * @param   pTransfer           Transfer to initialize for the data object.
     1216 */
     1217int SharedClipboardWinTransferInitialize(PSHCLWINCTX pWinCtx, PSHCLTRANSFER pTransfer)
    12041218{
    12051219    int rc = RTCritSectEnter(&pWinCtx->CritSect);
     
    12091223        if (pObj)
    12101224        {
    1211             rc = pObj->SetTransfer(pTransfer);
     1225            rc = sharedClipboardWinTransferInitializeInternal(pWinCtx, pTransfer, pObj);
     1226        }
     1227        else /* No current in-flight data object. */
     1228            rc = VERR_WRONG_ORDER;
     1229
     1230        int rc2 = RTCritSectLeave(&pWinCtx->CritSect);
     1231        AssertRC(rc2);
     1232    }
     1233
     1234    return rc;
     1235}
     1236
     1237/**
     1238 * Starts a Windows transfer for a given data object.
     1239 *
     1240 * @returns VBox status code.
     1241 * @param   pWinCtx             Windows context to use.
     1242 * @param   pObj                Data object to start transfer for.
     1243 */
     1244static int sharedClipboardWinTransferStartInternal(PSHCLWINCTX pWinCtx, SharedClipboardWinDataObject *pObj)
     1245{
     1246    RT_NOREF(pWinCtx);
     1247
     1248    return pObj->SetStatus(SharedClipboardWinDataObject::Running);
     1249}
     1250
     1251/**
     1252 * Starts a Windows transfer for the current data object in-flight.
     1253 *
     1254 * This hands off the data object in-flight to Windows on success.
     1255 *
     1256 * @returns VBox status code.
     1257 * @retval  VERR_WRONG_ORDER if no current in-flight data object is available.
     1258 * @param   pWinCtx             Windows context to use.
     1259 * @param   pTransfer           Transfer to initialize for the data object.
     1260 */
     1261int SharedClipboardWinTransferStart(PSHCLWINCTX pWinCtx, PSHCLTRANSFER pTransfer)
     1262{
     1263    RT_NOREF(pTransfer);
     1264
     1265    int rc = RTCritSectEnter(&pWinCtx->CritSect);
     1266    if (RT_SUCCESS(rc))
     1267    {
     1268        SharedClipboardWinDataObject *pObj = pWinCtx->pDataObjInFlight;
     1269        if (pObj)
     1270        {
     1271            rc = sharedClipboardWinTransferStartInternal(pWinCtx, pObj);
    12121272            if (RT_SUCCESS(rc))
    1213                 rc = pObj->SetStatus(SharedClipboardWinDataObject::Running);
    1214 
    1215             pWinCtx->pDataObjInFlight = NULL; /* Hand off to Windows. */
    1216         }
    1217         else
    1218         {
    1219             AssertMsgFailed(("No data object in flight (yet)!\n"));
     1273                pWinCtx->pDataObjInFlight = NULL; /* Hand off to Windows on success. */
     1274        }
     1275        else /* No current in-flight data object. */
    12201276            rc = VERR_WRONG_ORDER;
    1221         }
    12221277
    12231278        int rc2 = RTCritSectLeave(&pWinCtx->CritSect);
  • trunk/src/VBox/HostServices/SharedClipboard/VBoxSharedClipboardSvc-win.cpp

    r103453 r103615  
    312312        case SHCLTRANSFERDIR_FROM_REMOTE: /* G->H */
    313313        {
    314             rc = SharedClipboardWinTransferHandOffToDataObject(&pCtx->Win, pTransfer);
     314            rc = SharedClipboardWinTransferInitialize(&pCtx->Win, pTransfer);
    315315            break;
    316316        }
     
    329329    LogFlowFuncLeaveRC(rc);
    330330    return rc;
     331}
     332
     333/**
     334 * @copydoc SHCLTRANSFERCALLBACKS::pfnOnInitialized
     335 *
     336 * Called by ShClTransferInit via VbglR3.
     337 * For H->G: Called on transfer intialization to start the data transfer for the "in-flight" IDataObject.
     338 * For G->H: Nothing to do here.
     339 *
     340 * @thread  Clipboard main thread.
     341 */
     342static DECLCALLBACK(void) shClSvcWinTransferOnInitializedCallback(PSHCLTRANSFERCALLBACKCTX pCbCtx)
     343{
     344    LogFlowFuncEnter();
     345
     346    int rc = VINF_SUCCESS;
     347
     348    PSHCLCONTEXT pCtx = (PSHCLCONTEXT)pCbCtx->pvUser;
     349    AssertPtr(pCtx);
     350
     351    PSHCLTRANSFER pTransfer = pCbCtx->pTransfer;
     352    AssertPtr(pTransfer);
     353
     354    switch(ShClTransferGetDir(pTransfer))
     355    {
     356        case SHCLTRANSFERDIR_FROM_REMOTE: /* H->G */
     357        {
     358            rc = SharedClipboardWinTransferStart(&pCtx->Win, pTransfer);
     359            break;
     360        }
     361
     362        case SHCLTRANSFERDIR_TO_REMOTE: /* G->H */
     363            break;
     364
     365        default:
     366            break;
     367    }
     368
     369    LogFlowFuncLeaveRC(rc);
    331370}
    332371
     
    840879        pClient->Transfers.Callbacks.cbUser = sizeof(SHCLCONTEXT);
    841880
    842         pClient->Transfers.Callbacks.pfnOnCreated    = shClSvcWinTransferOnCreatedCallback;
    843         pClient->Transfers.Callbacks.pfnOnInitialize = shClSvcWinTransferOnInitializeCallback;
    844         pClient->Transfers.Callbacks.pfnOnDestroy    = shClSvcWinTransferOnDestroyCallback;
     881        pClient->Transfers.Callbacks.pfnOnCreated     = shClSvcWinTransferOnCreatedCallback;
     882        pClient->Transfers.Callbacks.pfnOnInitialize  = shClSvcWinTransferOnInitializeCallback;
     883        pClient->Transfers.Callbacks.pfnOnInitialized = shClSvcWinTransferOnInitializedCallback;
     884        pClient->Transfers.Callbacks.pfnOnDestroy     = shClSvcWinTransferOnDestroyCallback;
    845885#endif /* VBOX_WITH_SHARED_CLIPBOARD_TRANSFERS */
    846886    }
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