VirtualBox

Ignore:
Timestamp:
Feb 29, 2024 6:36:37 PM (11 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/src/VBox/GuestHost/SharedClipboard
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • 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);
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