desktop/inc/lib/init.hxx    |   20 ++++++++------
 desktop/source/lib/init.cxx |   62 ++++++++++++++++++++++++--------------------
 2 files changed, 46 insertions(+), 36 deletions(-)

New commits:
commit 041516806bdaa351ec22846754cf030905be53da
Author:     Caolán McNamara <caolan.mcnam...@collabora.com>
AuthorDate: Mon May 6 11:19:17 2024 +0100
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Mon May 6 13:42:25 2024 +0200

    Revert "flush CallbackFlushHandler queue via PostUserEvent..."
    
    This reverts commit d78938768c6f8dbc0917184c8daa0c381400bff7.
    
    on the theory that this is triggering:
    
    Error: Timed out waiting for: http://localhost:9900
        at 
/home/collabora/jenkins/workspace/github_online_master_debug_vs_co-24.04_cypress_desktop/cypress_test/node_modules/wait-on/lib/wait-on.js:132:31
    Change-Id: I39f0f36b08510805223da1105fa2a46977102bf2
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/167195
    Reviewed-by: Miklos Vajna <vmik...@collabora.com>
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>

diff --git a/desktop/inc/lib/init.hxx b/desktop/inc/lib/init.hxx
index 536faf00aff9..323a508098f5 100644
--- a/desktop/inc/lib/init.hxx
+++ b/desktop/inc/lib/init.hxx
@@ -93,11 +93,12 @@ namespace desktop {
     };
 
     /// One instance of this per view, handles flushing callbacks
-    class DESKTOP_DLLPUBLIC CallbackFlushHandler final : public 
SfxLokCallbackInterface
+    class DESKTOP_DLLPUBLIC CallbackFlushHandler final : public Idle, public 
SfxLokCallbackInterface
     {
     public:
         explicit CallbackFlushHandler(LibreOfficeKitDocument* pDocument, 
LibreOfficeKitCallback pCallback, void* pData);
         virtual ~CallbackFlushHandler() override;
+        virtual void Invoke() override;
         // TODO This should be dropped and the binary 
libreOfficeKitViewCallback() variants should be called?
         void queue(const int type, const OString& data);
 
@@ -188,8 +189,7 @@ namespace desktop {
         typedef std::vector<int> queue_type1;
         typedef std::vector<CallbackData> queue_type2;
 
-        void scheduleFlush();
-        void invoke();
+        void startTimer();
         bool removeAll(int type);
         bool removeAll(int type, const std::function<bool (const 
CallbackData&)>& rTestFunc);
         bool processInvalidateTilesEvent(int type, CallbackData& 
aCallbackData);
@@ -200,8 +200,6 @@ namespace desktop {
         void enqueueUpdatedTypes();
         void enqueueUpdatedType( int type, const SfxViewShell* 
sourceViewShell, int viewId );
 
-        void stop();
-
         /** we frequently want to scan the queue, and mostly when we do so, we 
only care about the element type
             so we split the queue in 2 to make the scanning cache friendly. */
         queue_type1 m_queue1;
@@ -232,12 +230,18 @@ namespace desktop {
         LibreOfficeKitDocument* m_pDocument;
         int m_viewId = -1; // view id of the associated SfxViewShell
         LibreOfficeKitCallback m_pCallback;
-        ImplSVEvent* m_pFlushEvent;
         void *m_pData;
         int m_nDisableCallbacks;
         std::recursive_mutex m_mutex;
-
-        DECL_LINK(FlushQueue, void*, void);
+        class TimeoutIdle : public Timer
+        {
+        public:
+            TimeoutIdle( CallbackFlushHandler* handler );
+            virtual void Invoke() override;
+        private:
+            CallbackFlushHandler* mHandler;
+        };
+        TimeoutIdle m_TimeoutIdle;
     };
 
     struct DESKTOP_DLLPUBLIC LibLODocument_Impl : public 
_LibreOfficeKitDocument
diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index d5476cfe7666..081166baeb0b 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -1524,14 +1524,32 @@ static OUString getGenerator()
 
 extern "C" {
 
+CallbackFlushHandler::TimeoutIdle::TimeoutIdle( CallbackFlushHandler* handler )
+    : Timer( "lokit timer callback" )
+    , mHandler( handler )
+{
+    // A second timer with higher priority, it'll ensure we flush in 
reasonable time if we get too busy
+    // to get POST_PAINT priority processing. Otherwise it could take a long 
time to flush.
+    SetPriority(TaskPriority::DEFAULT);
+    SetTimeout( 100 ); // 100 ms
+}
+
+void CallbackFlushHandler::TimeoutIdle::Invoke()
+{
+    mHandler->Invoke();
+}
+
 // One of these is created per view to handle events cf. doc_registerCallback
 CallbackFlushHandler::CallbackFlushHandler(LibreOfficeKitDocument* pDocument, 
LibreOfficeKitCallback pCallback, void* pData)
-    : m_pDocument(pDocument),
+    : Idle( "lokit idle callback" ),
+      m_pDocument(pDocument),
       m_pCallback(pCallback),
-      m_pFlushEvent(nullptr),
       m_pData(pData),
-      m_nDisableCallbacks(0)
+      m_nDisableCallbacks(0),
+      m_TimeoutIdle( this )
 {
+    SetPriority(TaskPriority::POST_PAINT);
+
     // Add the states that are safe to skip duplicates on, even when
     // not consequent (i.e. do no emit them if unchanged from last).
     m_states.emplace(LOK_CALLBACK_TEXT_SELECTION, "NIL"_ostr);
@@ -1550,18 +1568,9 @@ 
CallbackFlushHandler::CallbackFlushHandler(LibreOfficeKitDocument* pDocument, Li
     m_states.emplace(LOK_CALLBACK_STATUS_INDICATOR_SET_VALUE, "NIL"_ostr);
 }
 
-void CallbackFlushHandler::stop()
-{
-    if (m_pFlushEvent)
-    {
-        Application::RemoveUserEvent(m_pFlushEvent);
-        m_pFlushEvent = nullptr;
-    }
-}
-
 CallbackFlushHandler::~CallbackFlushHandler()
 {
-    stop();
+    Stop();
 }
 
 CallbackFlushHandler::queue_type2::iterator 
CallbackFlushHandler::toQueue2(CallbackFlushHandler::queue_type1::iterator pos)
@@ -1583,7 +1592,7 @@ void CallbackFlushHandler::setUpdatedType( int nType, 
bool value )
         m_updatedTypes.resize( nType + 1 ); // new are default-constructed, 
i.e. false
     m_updatedTypes[ nType ] = value;
     if(value)
-        scheduleFlush();
+        startTimer();
 }
 
 void CallbackFlushHandler::resetUpdatedType( int nType )
@@ -1599,7 +1608,7 @@ void CallbackFlushHandler::setUpdatedTypePerViewId( int 
nType, int nViewId, int
         types.resize( nType + 1 ); // new are default-constructed, i.e. 'set' 
is false
     types[ nType ] = PerViewIdData{ value, nSourceViewId };
     if(value)
-        scheduleFlush();
+        startTimer();
 }
 
 void CallbackFlushHandler::resetUpdatedTypePerViewId( int nType, int nViewId )
@@ -1676,7 +1685,7 @@ void CallbackFlushHandler::dumpState(rtl::OStringBuffer 
&rState)
 void CallbackFlushHandler::libreOfficeKitViewAddPendingInvalidateTiles()
 {
     // Invoke() will call flushPendingLOKInvalidateTiles(), so just make sure 
the timer is active.
-    scheduleFlush();
+    startTimer();
 }
 
 void CallbackFlushHandler::queue(const int type, const OString& data)
@@ -1989,7 +1998,7 @@ void CallbackFlushHandler::queue(const int type, 
CallbackData& aCallbackData)
 #endif
 
     lock.unlock();
-    scheduleFlush();
+    startTimer();
 }
 
 bool CallbackFlushHandler::processInvalidateTilesEvent(int type, CallbackData& 
aCallbackData)
@@ -2367,7 +2376,7 @@ void CallbackFlushHandler::enqueueUpdatedType( int type, 
const SfxViewShell* vie
         << "] to have " << m_queue1.size() << " entries.");
 }
 
-void CallbackFlushHandler::invoke()
+void CallbackFlushHandler::Invoke()
 {
     comphelper::ProfileZone aZone("CallbackFlushHandler::Invoke");
 
@@ -2475,19 +2484,16 @@ void CallbackFlushHandler::invoke()
 
     m_queue1.clear();
     m_queue2.clear();
-    stop();
+    Stop();
+    m_TimeoutIdle.Stop();
 }
 
-void CallbackFlushHandler::scheduleFlush()
+void CallbackFlushHandler::startTimer()
 {
-    if (!m_pFlushEvent)
-        m_pFlushEvent = Application::PostUserEvent(LINK(this, 
CallbackFlushHandler, FlushQueue));
-}
-
-IMPL_LINK_NOARG(CallbackFlushHandler, FlushQueue, void*, void)
-{
-    m_pFlushEvent = nullptr;
-    invoke();
+    if (!IsActive())
+        Start();
+    if (!m_TimeoutIdle.IsActive())
+        m_TimeoutIdle.Start();
 }
 
 bool CallbackFlushHandler::removeAll(int type)

Reply via email to