comphelper/source/misc/lok.cxx                              |    9 +-
 desktop/source/lib/init.cxx                                 |    5 +
 include/LibreOfficeKit/LibreOfficeKitTypes.h                |    2 
 include/comphelper/lok.hxx                                  |    5 -
 include/vcl/scheduler.hxx                                   |    6 -
 sfx2/qa/cppunit/view.cxx                                    |   19 +----
 sfx2/source/view/lokhelper.cxx                              |   41 ++----------
 sw/qa/extras/tiledrendering/tiledrenderingmodeltestbase.cxx |   14 +++-
 vcl/source/app/scheduler.cxx                                |   13 +--
 9 files changed, 45 insertions(+), 69 deletions(-)

New commits:
commit 56d4c1a5108656217e0c0fbd799d05b01201dfd6
Author:     Miklos Vajna <vmik...@collabora.com>
AuthorDate: Tue Feb 18 14:56:00 2025 +0100
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Thu Feb 20 09:40:54 2025 +0100

    cool#11064 vcl lok: fold the scheduler info into the anyinput callback
    
    This changes the LOK API added in commit
    e9e39e45f0daed52276377321088aa4d49b112b6 (cool#11064 vcl lok: expose
    info about the scheduler, 2025-02-14) so that a LOK client doesn't have
    to query the priority of the most urgent task: instead the anyInput
    callback gets it as a parameter.
    
    The scheduler is a hot path and it's not expected this priority
    information would be needed in other places, so folding the new LOK API
    into the anyInput callback simplifies things.
    
    Change-Id: I1385055bfa27f47a243d40683b54cb2ddc9d33bd

diff --git a/comphelper/source/misc/lok.cxx b/comphelper/source/misc/lok.cxx
index 2fe5d6c181a2..22895ba65661 100644
--- a/comphelper/source/misc/lok.cxx
+++ b/comphelper/source/misc/lok.cxx
@@ -42,8 +42,9 @@ static bool g_bLocalRendering(false);
 
 static Compat g_eCompatFlags(Compat::none);
 
-static std::function<bool(void*)> g_pAnyInputCallback;
+static std::function<bool(void*, int)> g_pAnyInputCallback;
 static void* g_pAnyInputCallbackData;
+static std::function<int()> g_pMostUrgentPriorityGetter;
 
 static std::function<void(int)> g_pViewSetter;
 static std::function<int()> g_pViewGetter;
@@ -329,10 +330,11 @@ void statusIndicatorFinish()
         pStatusIndicatorCallback(pStatusIndicatorCallbackData, 
statusIndicatorCallbackType::Finish, 0, nullptr);
 }
 
-void setAnyInputCallback(const std::function<bool(void*)>& pAnyInputCallback, 
void* pData)
+void setAnyInputCallback(const std::function<bool(void*, int)>& 
pAnyInputCallback, void* pData, std::function<int()> pMostUrgentPriorityGetter)
 {
     g_pAnyInputCallback = pAnyInputCallback;
     g_pAnyInputCallbackData = pData;
+    g_pMostUrgentPriorityGetter = pMostUrgentPriorityGetter;
 }
 
 bool anyInput()
@@ -342,7 +344,8 @@ bool anyInput()
     // Ignore input events during background save.
     if (!g_bForkedChild && g_pAnyInputCallback && g_pAnyInputCallbackData)
     {
-        bRet = g_pAnyInputCallback(g_pAnyInputCallbackData);
+        int nMostUrgentPriority = g_pMostUrgentPriorityGetter();
+        bRet = g_pAnyInputCallback(g_pAnyInputCallbackData, 
nMostUrgentPriority);
     }
 
     return bRet;
diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index 107946b4201b..84fe5f3e32f2 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -198,6 +198,7 @@
 #include <comphelper/diagnose_ex.hxx>
 #include <vcl/uitest/uiobject.hxx>
 #include <vcl/jsdialog/executor.hxx>
+#include <vcl/scheduler.hxx>
 
 // Needed for getUndoManager()
 #include <com/sun/star/document/XUndoManager.hpp>
@@ -7667,7 +7668,9 @@ static void lo_registerAnyInputCallback(LibreOfficeKit* 
/*pThis*/,
                        void* pData)
 {
     SolarMutexGuard aGuard;
-    comphelper::LibreOfficeKit::setAnyInputCallback(pAnyInputCallback, pData);
+    comphelper::LibreOfficeKit::setAnyInputCallback(pAnyInputCallback, pData, 
[]() -> int {
+        return Scheduler::GetMostUrgentTaskPriority();
+    });
 }
 
 static bool bInitialized = false;
diff --git a/include/LibreOfficeKit/LibreOfficeKitTypes.h 
b/include/LibreOfficeKit/LibreOfficeKitTypes.h
index a702a229834b..939766567607 100644
--- a/include/LibreOfficeKit/LibreOfficeKitTypes.h
+++ b/include/LibreOfficeKit/LibreOfficeKitTypes.h
@@ -26,7 +26,7 @@ typedef int (*LibreOfficeKitPollCallback)(void* pData, int 
timeoutUs);
 typedef void (*LibreOfficeKitWakeCallback)(void* pData);
 
 /// @see lok::Office::registerAnyInputCallback()
-typedef bool (*LibreOfficeKitAnyInputCallback)(void* pData);
+typedef bool (*LibreOfficeKitAnyInputCallback)(void* pData, int 
nMostUrgentPriority);
 
 #ifdef __cplusplus
 }
diff --git a/include/comphelper/lok.hxx b/include/comphelper/lok.hxx
index 3cee8180ccb1..2502a7c354c0 100644
--- a/include/comphelper/lok.hxx
+++ b/include/comphelper/lok.hxx
@@ -138,8 +138,9 @@ COMPHELPER_DLLPUBLIC void statusIndicatorFinish();
 
 COMPHELPER_DLLPUBLIC void setBlockedCommandList(const char* 
blockedCommandList);
 
-COMPHELPER_DLLPUBLIC void setAnyInputCallback(const 
std::function<bool(void*)>& pAnyInputCallback,
-                                              void* pData);
+COMPHELPER_DLLPUBLIC void
+setAnyInputCallback(const std::function<bool(void*, int)>& pAnyInputCallback, 
void* pData,
+                    std::function<int()> pMostUrgentPriorityGetter);
 COMPHELPER_DLLPUBLIC bool anyInput();
 
 // These allow setting callbacks, so that set/get of a LOK view is possible 
even in code that is
diff --git a/include/vcl/scheduler.hxx b/include/vcl/scheduler.hxx
index 36af96ded55e..a30cac61c6eb 100644
--- a/include/vcl/scheduler.hxx
+++ b/include/vcl/scheduler.hxx
@@ -23,10 +23,6 @@
 #include <vcl/dllapi.h>
 
 struct ImplSchedulerContext;
-namespace tools
-{
-class JsonWriter;
-}
 
 class VCL_DLLPUBLIC Scheduler final
 {
@@ -93,7 +89,7 @@ public:
         ~IdlesLockGuard();
     };
 
-    static void dumpAsJSON(tools::JsonWriter& rJsonWriter);
+    static int GetMostUrgentTaskPriority();
 };
 
 #endif // INCLUDED_VCL_SCHEDULER_HXX
diff --git a/sfx2/qa/cppunit/view.cxx b/sfx2/qa/cppunit/view.cxx
index dad9e4a51f45..22dbdfaa84ad 100644
--- a/sfx2/qa/cppunit/view.cxx
+++ b/sfx2/qa/cppunit/view.cxx
@@ -27,6 +27,7 @@
 #include <rtl/ustrbuf.hxx>
 #include <comphelper/base64.hxx>
 #include <comphelper/propertyvalue.hxx>
+#include <vcl/scheduler.hxx>
 
 using namespace com::sun::star;
 
@@ -228,21 +229,11 @@ CPPUNIT_TEST_FIXTURE(Sfx2ViewTest, testScheduler)
     mxComponent = loadFromDesktop("private:factory/swriter", 
"com.sun.star.text.TextDocument");
 
     // When asking for the state of the scheduler:
-    tools::JsonWriter aWriter;
-    SfxLokHelper::getCommandValues(aWriter, ".uno:Scheduler");
-    OString aJson = aWriter.finishAndGetAsOString();
+    int nRet = Scheduler::GetMostUrgentTaskPriority();
 
-    // Then make sure we get an int priority:
-    CPPUNIT_ASSERT(SfxLokHelper::supportsCommand(u"Scheduler"));
-    std::stringstream aStream{ std::string(aJson) };
-    boost::property_tree::ptree aTree;
-    boost::property_tree::read_json(aStream, aTree);
-    auto it = aTree.find("mostUrgentPriority");
-    // Without the accompanying fix in place, this test would have failed, 
this JSON key was
-    // missing.
-    CPPUNIT_ASSERT(it != aTree.not_found());
-    // This returns TaskPriority::HIGH_IDLE, but just make sure we get an int.
-    it->second.get_value<int>();
+    // Then make sure we get a priority:
+    // This returns TaskPriority::HIGH_IDLE, but just make sure we get a 
positive priority.
+    CPPUNIT_ASSERT(nRet != -1);
 }
 #endif
 
diff --git a/sfx2/source/view/lokhelper.cxx b/sfx2/source/view/lokhelper.cxx
index 5d034da1da3e..0ea04c16238f 100644
--- a/sfx2/source/view/lokhelper.cxx
+++ b/sfx2/source/view/lokhelper.cxx
@@ -44,7 +44,6 @@
 #include <tools/json_writer.hxx>
 #include <svl/cryptosign.hxx>
 #include <tools/urlobj.hxx>
-#include <vcl/scheduler.hxx>
 
 #include <boost/property_tree/json_parser.hpp>
 
@@ -997,8 +996,7 @@ void SfxLokHelper::addCertificates(const 
std::vector<std::string>& rCerts)
 
 bool SfxLokHelper::supportsCommand(std::u16string_view rCommand)
 {
-    static const std::initializer_list<std::u16string_view> vSupport
-        = { u"Signature", u"Scheduler" };
+    static const std::initializer_list<std::u16string_view> vSupport = { 
u"Signature" };
 
     return std::find(vSupport.begin(), vSupport.end(), rCommand) != 
vSupport.end();
 }
@@ -1030,11 +1028,14 @@ std::map<OUString, OUString> 
SfxLokHelper::parseCommandParameters(std::u16string
     return aMap;
 }
 
-namespace
-{
-/// Implements getCommandValues(".uno:Signature").
-void GetSignature(tools::JsonWriter& rJsonWriter, std::string_view rCommand)
+void SfxLokHelper::getCommandValues(tools::JsonWriter& rJsonWriter, 
std::string_view rCommand)
 {
+    static constexpr OString aSignature(".uno:Signature"_ostr);
+    if (!o3tl::starts_with(rCommand, aSignature))
+    {
+        return;
+    }
+
     SfxObjectShell* pObjectShell = SfxObjectShell::Current();
     if (!pObjectShell)
     {
@@ -1052,7 +1053,7 @@ void GetSignature(tools::JsonWriter& rJsonWriter, 
std::string_view rCommand)
     }
     pObjectShell->SignDocumentContentUsingCertificate(aSigningContext);
     // Set commandName, this is a reply to a request.
-    rJsonWriter.put("commandName", ".uno:Signature");
+    rJsonWriter.put("commandName", aSignature);
     auto aCommandValues = rJsonWriter.startNode("commandValues");
     rJsonWriter.put("signatureTime", aSigningContext.m_nSignatureTime);
 
@@ -1063,30 +1064,6 @@ void GetSignature(tools::JsonWriter& rJsonWriter, 
std::string_view rCommand)
     rJsonWriter.put("digest", aBuffer.makeStringAndClear());
 }
 
-/// Implements getCommandValues(".uno:Scheduler").
-void GetScheduler(tools::JsonWriter& rJsonWriter)
-{
-    Scheduler::dumpAsJSON(rJsonWriter);
-}
-}
-
-void SfxLokHelper::getCommandValues(tools::JsonWriter& rJsonWriter, 
std::string_view rCommand)
-{
-    static constexpr OString aSignature(".uno:Signature"_ostr);
-    static constexpr OString aScheduler(".uno:Scheduler"_ostr);
-    if (o3tl::starts_with(rCommand, aSignature))
-    {
-        GetSignature(rJsonWriter, rCommand);
-        return;
-    }
-
-    if (o3tl::starts_with(rCommand, aScheduler))
-    {
-        GetScheduler(rJsonWriter);
-        return;
-    }
-}
-
 void SfxLokHelper::notifyUpdate(SfxViewShell const* pThisView, int nType)
 {
     if (DisableCallbacks::disabled() || !pThisView)
diff --git a/sw/qa/extras/tiledrendering/tiledrenderingmodeltestbase.cxx 
b/sw/qa/extras/tiledrendering/tiledrenderingmodeltestbase.cxx
index 96bfd851ce0c..0c7f1437b2d9 100644
--- a/sw/qa/extras/tiledrendering/tiledrenderingmodeltestbase.cxx
+++ b/sw/qa/extras/tiledrendering/tiledrenderingmodeltestbase.cxx
@@ -506,11 +506,19 @@ public:
 class AnyInputCallback final
 {
 public:
-    static bool callback(void* /*pData*/) { return true; }
+    static bool callback(void* /*pData*/, int /*nPriority*/) { return true; }
 
-    AnyInputCallback() { 
comphelper::LibreOfficeKit::setAnyInputCallback(&callback, this); }
+    AnyInputCallback()
+    {
+        comphelper::LibreOfficeKit::setAnyInputCallback(&callback, this,
+                                                        []() -> int { return 
-1; });
+    }
 
-    ~AnyInputCallback() { 
comphelper::LibreOfficeKit::setAnyInputCallback(nullptr, nullptr); }
+    ~AnyInputCallback()
+    {
+        comphelper::LibreOfficeKit::setAnyInputCallback(nullptr, nullptr,
+                                                        []() -> int { return 
-1; });
+    }
 };
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/vcl/source/app/scheduler.cxx b/vcl/source/app/scheduler.cxx
index 61814364d22e..527698d10a24 100644
--- a/vcl/source/app/scheduler.cxx
+++ b/vcl/source/app/scheduler.cxx
@@ -299,7 +299,7 @@ Scheduler::IdlesLockGuard::~IdlesLockGuard()
     osl_atomic_decrement(&rSchedCtx.mnIdlesLockCount);
 }
 
-void Scheduler::dumpAsJSON(tools::JsonWriter& rJsonWriter)
+int Scheduler::GetMostUrgentTaskPriority()
 {
     // Similar to Scheduler::CallbackTaskScheduling(), figure out the most 
urgent priority, but
     // don't actually invoke any task.
@@ -308,15 +308,13 @@ void Scheduler::dumpAsJSON(tools::JsonWriter& rJsonWriter)
     ImplSchedulerContext& rSchedCtx = pSVData->maSchedCtx;
     if (!rSchedCtx.mbActive || rSchedCtx.mnTimerPeriod == InfiniteTimeoutMs)
     {
-        rJsonWriter.put("mostUrgentPriority", nMostUrgentPriority);
-        return;
+        return nMostUrgentPriority;
     }
 
     sal_uInt64 nTime = tools::Time::GetSystemTicks();
     if (nTime < rSchedCtx.mnTimerStart + rSchedCtx.mnTimerPeriod - 1)
     {
-        rJsonWriter.put("mostUrgentPriority", nMostUrgentPriority);
-        return;
+        return nMostUrgentPriority;
     }
 
     for (int nTaskPriority = 0; nTaskPriority < PRIO_COUNT; ++nTaskPriority)
@@ -332,14 +330,13 @@ void Scheduler::dumpAsJSON(tools::JsonWriter& rJsonWriter)
                 if (nReadyPeriod == ImmediateTimeoutMs)
                 {
                     nMostUrgentPriority = nTaskPriority;
-                    rJsonWriter.put("mostUrgentPriority", nMostUrgentPriority);
-                    return;
+                    return nMostUrgentPriority;
                 }
             }
             pSchedulerData = pSchedulerData->mpNext;
         }
     }
-    rJsonWriter.put("mostUrgentPriority", nMostUrgentPriority);
+    return nMostUrgentPriority;
 }
 
 inline void Scheduler::UpdateSystemTimer( ImplSchedulerContext &rSchedCtx,

Reply via email to