vcl/win/dtrans/WinClipboard.cxx |  122 ++++++++++++++++++++--------------------
 vcl/win/dtrans/WinClipboard.hxx |   11 ++-
 2 files changed, 69 insertions(+), 64 deletions(-)

New commits:
commit b9d32aa128ca1c8096a43c8d564a38330f1fc85c
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Sun Jul 14 22:01:59 2024 +0500
Commit:     Xisco Fauli <xiscofa...@libreoffice.org>
CommitDate: Tue Jul 16 21:03:49 2024 +0200

    tdf#148647: make sure to update own content on Win clipboard change
    
    Before, only a new call to setContents, or destruction of the data
    object referenced from CWinClipboard, would force CWinClipboard to
    clean its m_pCurrentClipContent. A data object could be referenced
    elsewhere, and so not get destroyed on clipboard change; this kept
    the value of m_pCurrentClipContent, and all the following calls to
    getContents would still return that stuck own data object.
    
    This change makes CWinClipboard  store new own data object pointer
    into temporary m_pNewOwnClipContent.  In onClipboardContentChanged
    notification, the value is moved to m_pCurrentOwnClipContent. This
    ensures that following onClipboardContentChanged events will clear
    m_pCurrentOwnClipContent, preventing stuck clipboard content.
    
    Change-Id: Idd0a64a820d79bede9deceba8d5800b7fc61e66b
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/170459
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>
    Tested-by: Jenkins
    Signed-off-by: Xisco Fauli <xiscofa...@libreoffice.org>
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/170462

diff --git a/vcl/win/dtrans/WinClipboard.cxx b/vcl/win/dtrans/WinClipboard.cxx
index ccd3c6907078..3c9f57a1e250 100644
--- a/vcl/win/dtrans/WinClipboard.cxx
+++ b/vcl/win/dtrans/WinClipboard.cxx
@@ -60,7 +60,6 @@ CWinClipboard::CWinClipboard(const 
uno::Reference<uno::XComponentContext>& rxCon
                              const OUString& aClipboardName)
     : m_xContext(rxContext)
     , m_itsName(aClipboardName)
-    , m_pCurrentClipContent(nullptr)
 {
     // necessary to reassociate from
     // the static callback function
@@ -92,6 +91,12 @@ void CWinClipboard::disposing(std::unique_lock<std::mutex>& 
mutex)
 
 // XClipboard
 
+CXNotifyingDataObject* CWinClipboard::getOwnClipContent() const
+{
+    assert(!m_pCurrentOwnClipContent || !m_pNewOwnClipContent); // Both can be 
null, or only one set
+    return m_pCurrentOwnClipContent ? m_pCurrentOwnClipContent : 
m_pNewOwnClipContent;
+}
+
 // to avoid unnecessary traffic we check first if there is a clipboard
 // content which was set via setContent, in this case we don't need
 // to query the content from the clipboard, create a new wrapper object
@@ -110,12 +115,12 @@ css::uno::Reference<css::datatransfer::XTransferable> 
CWinClipboard::getContents
         throw lang::DisposedException("object is already disposed",
                                       static_cast<XClipboardEx*>(this));
 
-    assert(!m_pCurrentClipContent || !m_foreignContent); // Both can be null, 
or only one set
+    assert(!getOwnClipContent() || !m_foreignContent); // Both can be null, or 
only one set
 
     // use the shortcut or create a transferable from
     // system clipboard
-    if (nullptr != m_pCurrentClipContent)
-        return m_pCurrentClipContent->m_XTransferable;
+    if (auto pOwnClipContent = getOwnClipContent())
+        return pOwnClipContent->m_XTransferable;
 
     // Content cached?
     if (m_foreignContent.is())
@@ -175,18 +180,21 @@ void SAL_CALL CWinClipboard::setContents(
     IDataObjectPtr pIDataObj;
 
     m_foreignContent.clear();
+    m_pCurrentOwnClipContent = nullptr;
 
     if (xTransferable.is())
     {
-        m_pCurrentClipContent = new CXNotifyingDataObject(
+        // Store the new object's pointer to temporary m_pNewOwnClipContent, 
to be moved to
+        // m_pCurrentOwnClipContent in handleClipboardContentChanged.
+        m_pNewOwnClipContent = new CXNotifyingDataObject(
             CDTransObjFactory::createDataObjFromTransferable(m_xContext, 
xTransferable),
             xTransferable, xClipboardOwner, this);
 
-        pIDataObj = IDataObjectPtr(m_pCurrentClipContent);
+        pIDataObj = IDataObjectPtr(m_pNewOwnClipContent);
     }
     else
     {
-        m_pCurrentClipContent = nullptr;
+        m_pNewOwnClipContent = nullptr;
     }
 
     m_MtaOleClipboard.setClipboard(pIDataObj.get());
@@ -219,7 +227,7 @@ void SAL_CALL CWinClipboard::flushClipboard()
     // It may be possible to move the request to the clipboard STA thread by 
saving the
     // DataObject and call OleIsCurrentClipboard before flushing.
 
-    if (nullptr != m_pCurrentClipContent)
+    if (getOwnClipContent())
     {
         aGuard.unlock();
         m_MtaOleClipboard.flushClipboard();
@@ -272,13 +280,16 @@ void SAL_CALL CWinClipboard::removeClipboardListener(
     maClipboardListeners.removeInterface(aGuard, listener);
 }
 
-void CWinClipboard::clearCacheAndAllClipboardListener()
+void CWinClipboard::handleClipboardContentChanged()
 {
     std::unique_lock aGuard(m_aMutex);
     if (m_bDisposed)
         return;
 
     m_foreignContent.clear();
+    // If new own content assignment is pending, do it; otherwise, clear it.
+    // This makes sure that there will be no stuck clipboard content.
+    m_pCurrentOwnClipContent = std::exchange(m_pNewOwnClipContent, nullptr);
 
     if (!maClipboardListeners.getLength(aGuard))
         return;
@@ -351,8 +362,8 @@ void 
CWinClipboard::onReleaseDataObject(CXNotifyingDataObject* theCaller)
     // because an external source must be the clipboardowner now
     std::unique_lock aGuard(m_aMutex);
 
-    if (m_pCurrentClipContent == theCaller)
-        m_pCurrentClipContent = nullptr;
+    if (getOwnClipContent() == theCaller)
+        m_pCurrentOwnClipContent = m_pNewOwnClipContent = nullptr;
 }
 
 void CWinClipboard::registerClipboardViewer()
@@ -372,7 +383,7 @@ void WINAPI CWinClipboard::onClipboardContentChanged()
     }
 
     if (pWinClipboard)
-        pWinClipboard->clearCacheAndAllClipboardListener();
+        pWinClipboard->handleClipboardContentChanged();
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/vcl/win/dtrans/WinClipboard.hxx b/vcl/win/dtrans/WinClipboard.hxx
index fcf7bf089cb6..83d01da65e05 100644
--- a/vcl/win/dtrans/WinClipboard.hxx
+++ b/vcl/win/dtrans/WinClipboard.hxx
@@ -57,12 +57,15 @@ class CWinClipboard final
     css::uno::Reference<css::uno::XComponentContext> m_xContext;
     const OUString m_itsName;
     CMtaOleClipboard m_MtaOleClipboard;
-    CXNotifyingDataObject* m_pCurrentClipContent;
+    CXNotifyingDataObject* m_pNewOwnClipContent = nullptr; // until 
onClipboardContentChanged
+    CXNotifyingDataObject* m_pCurrentOwnClipContent = nullptr;
     
com::sun::star::uno::Reference<com::sun::star::datatransfer::XTransferable> 
m_foreignContent;
     
comphelper::OInterfaceContainerHelper4<css::datatransfer::clipboard::XClipboardListener>
         maClipboardListeners;
 
-    void clearCacheAndAllClipboardListener();
+    CXNotifyingDataObject* getOwnClipContent() const;
+
+    void handleClipboardContentChanged();
     void onReleaseDataObject(CXNotifyingDataObject* theCaller);
 
     void registerClipboardViewer();
commit bb1aac87d66f90ef4ecf6dfb3b109928b5c33924
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Sun Jul 14 10:39:29 2024 +0500
Commit:     Xisco Fauli <xiscofa...@libreoffice.org>
CommitDate: Tue Jul 16 21:03:36 2024 +0200

    Try to simplify Windows clipboard locking
    
    Having three mutexes for one object is a bit too much. It is impossible
    to handle it properly. Issues like tdf#148647, that affect many users,
    but have no reliable reproducer, hint that possibly there is a deadlock
    somewhere in the code that should clear m_pCurrentClipContent, or maybe
    something similar.
    
    This drops both local mutexes, and uses inherited m_aMutex everywhere
    to guard accessto all the local data. I hope that this this will make
    debugging of the problems around the clipboard possible. But it looks
    invasive, of course.
    
    Change-Id: I2311b1b4702f766e3e5e0d104f9b2b2999aa53c9
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/170450
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>
    Signed-off-by: Xisco Fauli <xiscofa...@libreoffice.org>
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/170461

diff --git a/vcl/win/dtrans/WinClipboard.cxx b/vcl/win/dtrans/WinClipboard.cxx
index 5f46bebecfd2..ccd3c6907078 100644
--- a/vcl/win/dtrans/WinClipboard.cxx
+++ b/vcl/win/dtrans/WinClipboard.cxx
@@ -52,7 +52,7 @@ using namespace com::sun::star;
 namespace
 {
 CWinClipboard* s_pCWinClipbImpl = nullptr;
-osl::Mutex s_aClipboardSingletonMutex;
+std::mutex s_aClipboardSingletonMutex;
 }
 
 /*XEventListener,*/
@@ -65,7 +65,7 @@ CWinClipboard::CWinClipboard(const 
uno::Reference<uno::XComponentContext>& rxCon
     // necessary to reassociate from
     // the static callback function
     {
-        osl::MutexGuard aGuard(s_aClipboardSingletonMutex);
+        std::unique_lock aGuard(s_aClipboardSingletonMutex);
         s_pCWinClipbImpl = this;
     }
 
@@ -74,18 +74,14 @@ CWinClipboard::CWinClipboard(const 
uno::Reference<uno::XComponentContext>& rxCon
 
 CWinClipboard::~CWinClipboard()
 {
-    {
-        osl::MutexGuard aGuard(s_aClipboardSingletonMutex);
-        s_pCWinClipbImpl = nullptr;
-    }
-
-    unregisterClipboardViewer();
+    assert(m_bDisposed);
+    assert(!s_pCWinClipbImpl);
 }
 
 void CWinClipboard::disposing(std::unique_lock<std::mutex>& mutex)
 {
     {
-        osl::MutexGuard aGuard(s_aClipboardSingletonMutex);
+        std::unique_lock aGuard(s_aClipboardSingletonMutex);
         s_pCWinClipbImpl = nullptr;
     }
 
@@ -104,27 +100,26 @@ void 
CWinClipboard::disposing(std::unique_lock<std::mutex>& mutex)
 
 uno::Reference<datatransfer::XTransferable> SAL_CALL 
CWinClipboard::getContents()
 {
-    osl::MutexGuard aGuard(m_aContentMutex);
+    std::unique_lock aGuard(m_aMutex);
+    return getContents_noLock();
+}
 
+css::uno::Reference<css::datatransfer::XTransferable> 
CWinClipboard::getContents_noLock()
+{
     if (m_bDisposed)
         throw lang::DisposedException("object is already disposed",
                                       static_cast<XClipboardEx*>(this));
 
+    assert(!m_pCurrentClipContent || !m_foreignContent); // Both can be null, 
or only one set
+
     // use the shortcut or create a transferable from
     // system clipboard
-    {
-        osl::MutexGuard aGuard2(m_aContentCacheMutex);
-
-        if (nullptr != m_pCurrentClipContent)
-            return m_pCurrentClipContent->m_XTransferable;
-
-        // Content cached?
-        if (m_foreignContent.is())
-            return m_foreignContent;
+    if (nullptr != m_pCurrentClipContent)
+        return m_pCurrentClipContent->m_XTransferable;
 
-        // release the mutex, so that the variable may be
-        // changed by other threads
-    }
+    // Content cached?
+    if (m_foreignContent.is())
+        return m_foreignContent;
 
     uno::Reference<datatransfer::XTransferable> rClipContent;
 
@@ -138,7 +133,6 @@ uno::Reference<datatransfer::XTransferable> SAL_CALL 
CWinClipboard::getContents(
             std::vector<sal_uInt32> aFormats(aUINTFormats.begin(), 
aUINTFormats.end());
             rClipContent = new CDOTransferable(m_xContext, this, aFormats);
 
-            osl::MutexGuard aGuard2(m_aContentCacheMutex);
             m_foreignContent = rClipContent;
         }
     }
@@ -148,7 +142,7 @@ uno::Reference<datatransfer::XTransferable> SAL_CALL 
CWinClipboard::getContents(
 
 IDataObjectPtr CWinClipboard::getIDataObject()
 {
-    osl::MutexGuard aGuard(m_aContentMutex);
+    std::unique_lock aGuard(m_aMutex);
 
     if (m_bDisposed)
         throw lang::DisposedException("object is already disposed",
@@ -172,7 +166,7 @@ void SAL_CALL CWinClipboard::setContents(
     const uno::Reference<datatransfer::XTransferable>& xTransferable,
     const uno::Reference<datatransfer::clipboard::XClipboardOwner>& 
xClipboardOwner)
 {
-    osl::MutexGuard aGuard(m_aContentMutex);
+    std::unique_lock aGuard(m_aMutex);
 
     if (m_bDisposed)
         throw lang::DisposedException("object is already disposed",
@@ -180,26 +174,27 @@ void SAL_CALL CWinClipboard::setContents(
 
     IDataObjectPtr pIDataObj;
 
+    m_foreignContent.clear();
+
     if (xTransferable.is())
     {
-        {
-            osl::MutexGuard aGuard2(m_aContentCacheMutex);
-
-            m_foreignContent.clear();
-
-            m_pCurrentClipContent = new CXNotifyingDataObject(
-                CDTransObjFactory::createDataObjFromTransferable(m_xContext, 
xTransferable),
-                xTransferable, xClipboardOwner, this);
-        }
+        m_pCurrentClipContent = new CXNotifyingDataObject(
+            CDTransObjFactory::createDataObjFromTransferable(m_xContext, 
xTransferable),
+            xTransferable, xClipboardOwner, this);
 
         pIDataObj = IDataObjectPtr(m_pCurrentClipContent);
     }
+    else
+    {
+        m_pCurrentClipContent = nullptr;
+    }
 
     m_MtaOleClipboard.setClipboard(pIDataObj.get());
 }
 
 OUString SAL_CALL CWinClipboard::getName()
 {
+    std::unique_lock aGuard(m_aMutex);
     if (m_bDisposed)
         throw lang::DisposedException("object is already disposed",
                                       static_cast<XClipboardEx*>(this));
@@ -211,24 +206,24 @@ OUString SAL_CALL CWinClipboard::getName()
 
 void SAL_CALL CWinClipboard::flushClipboard()
 {
-    osl::MutexGuard aGuard(m_aContentMutex);
+    std::unique_lock aGuard(m_aMutex);
 
     if (m_bDisposed)
         throw lang::DisposedException("object is already disposed",
                                       static_cast<XClipboardEx*>(this));
 
-    // actually it should be ClearableMutexGuard aGuard( m_aContentCacheMutex 
);
-    // but it does not work since FlushClipboard does a callback and frees 
DataObject
-    // which results in a deadlock in onReleaseDataObject.
-    // FlushClipboard had to be synchron in order to prevent shutdown until all
-    // clipboard-formats are rendered.
-    // The request is needed to prevent flushing if we are not clipboard owner 
(it is
-    // not known what happens if we flush but aren't clipboard owner).
+    // FlushClipboard does a callback and frees DataObject, which calls 
onReleaseDataObject and
+    // locks mutex. FlushClipboard has to be synchron in order to prevent 
shutdown until all
+    // clipboard-formats are rendered. The request is needed to prevent 
flushing if we are not
+    // clipboard owner (it is not known what happens if we flush but aren't 
clipboard owner).
     // It may be possible to move the request to the clipboard STA thread by 
saving the
     // DataObject and call OleIsCurrentClipboard before flushing.
 
     if (nullptr != m_pCurrentClipContent)
+    {
+        aGuard.unlock();
         m_MtaOleClipboard.flushClipboard();
+    }
 }
 
 // XClipboardEx
@@ -248,6 +243,7 @@ sal_Int8 SAL_CALL CWinClipboard::getRenderingCapabilities()
 void SAL_CALL CWinClipboard::addClipboardListener(
     const uno::Reference<datatransfer::clipboard::XClipboardListener>& 
listener)
 {
+    std::unique_lock aGuard(m_aMutex);
     if (m_bDisposed)
         throw lang::DisposedException("object is already disposed",
                                       static_cast<XClipboardEx*>(this));
@@ -257,13 +253,13 @@ void SAL_CALL CWinClipboard::addClipboardListener(
         throw lang::IllegalArgumentException("empty reference", 
static_cast<XClipboardEx*>(this),
                                              1);
 
-    std::unique_lock aGuard(m_aMutex);
     maClipboardListeners.addInterface(aGuard, listener);
 }
 
 void SAL_CALL CWinClipboard::removeClipboardListener(
     const uno::Reference<datatransfer::clipboard::XClipboardListener>& 
listener)
 {
+    std::unique_lock aGuard(m_aMutex);
     if (m_bDisposed)
         throw lang::DisposedException("object is already disposed",
                                       static_cast<XClipboardEx*>(this));
@@ -273,25 +269,23 @@ void SAL_CALL CWinClipboard::removeClipboardListener(
         throw lang::IllegalArgumentException("empty reference", 
static_cast<XClipboardEx*>(this),
                                              1);
 
-    std::unique_lock aGuard(m_aMutex);
     maClipboardListeners.removeInterface(aGuard, listener);
 }
 
-void CWinClipboard::notifyAllClipboardListener()
+void CWinClipboard::clearCacheAndAllClipboardListener()
 {
-    if (m_bDisposed)
-        return;
-
     std::unique_lock aGuard(m_aMutex);
     if (m_bDisposed)
         return;
 
+    m_foreignContent.clear();
+
     if (!maClipboardListeners.getLength(aGuard))
         return;
 
     try
     {
-        uno::Reference<datatransfer::XTransferable> rXTransf(getContents());
+        uno::Reference<datatransfer::XTransferable> 
rXTransf(getContents_noLock());
         datatransfer::clipboard::ClipboardEvent 
aClipbEvent(static_cast<XClipboard*>(this),
                                                             rXTransf);
         maClipboardListeners.notifyEach(
@@ -355,7 +349,7 @@ void 
CWinClipboard::onReleaseDataObject(CXNotifyingDataObject* theCaller)
 
     // if the current caller is the one we currently hold, then set it to NULL
     // because an external source must be the clipboardowner now
-    osl::MutexGuard aGuard(m_aContentCacheMutex);
+    std::unique_lock aGuard(m_aMutex);
 
     if (m_pCurrentClipContent == theCaller)
         m_pCurrentClipContent = nullptr;
@@ -373,15 +367,12 @@ void WINAPI CWinClipboard::onClipboardContentChanged()
     rtl::Reference<CWinClipboard> pWinClipboard;
     {
         // Only hold the mutex to obtain a safe reference to the impl, to 
avoid deadlock
-        osl::MutexGuard aGuard(s_aClipboardSingletonMutex);
+        std::unique_lock aGuard(s_aClipboardSingletonMutex);
         pWinClipboard.set(s_pCWinClipbImpl);
     }
 
     if (pWinClipboard)
-    {
-        pWinClipboard->m_foreignContent.clear();
-        pWinClipboard->notifyAllClipboardListener();
-    }
+        pWinClipboard->clearCacheAndAllClipboardListener();
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/vcl/win/dtrans/WinClipboard.hxx b/vcl/win/dtrans/WinClipboard.hxx
index fbaa1b206288..fcf7bf089cb6 100644
--- a/vcl/win/dtrans/WinClipboard.hxx
+++ b/vcl/win/dtrans/WinClipboard.hxx
@@ -59,12 +59,10 @@ class CWinClipboard final
     CMtaOleClipboard m_MtaOleClipboard;
     CXNotifyingDataObject* m_pCurrentClipContent;
     
com::sun::star::uno::Reference<com::sun::star::datatransfer::XTransferable> 
m_foreignContent;
-    osl::Mutex m_aContentMutex;
-    osl::Mutex m_aContentCacheMutex;
     
comphelper::OInterfaceContainerHelper4<css::datatransfer::clipboard::XClipboardListener>
         maClipboardListeners;
 
-    void notifyAllClipboardListener();
+    void clearCacheAndAllClipboardListener();
     void onReleaseDataObject(CXNotifyingDataObject* theCaller);
 
     void registerClipboardViewer();
@@ -72,6 +70,8 @@ class CWinClipboard final
 
     static void WINAPI onClipboardContentChanged();
 
+    css::uno::Reference<css::datatransfer::XTransferable> getContents_noLock();
+
 public:
     CWinClipboard(const css::uno::Reference<css::uno::XComponentContext>& 
rxContext,
                   const OUString& aClipboardName);

Reply via email to