kit/ChildSession.cpp   |    2 
 kit/ChildSession.hpp   |    2 
 kit/Kit.cpp            |  214 +++++++++++++++++++++++++------------------------
 test/WhiteBoxTests.cpp |    4 
 4 files changed, 117 insertions(+), 105 deletions(-)

New commits:
commit 1525f774d9317e0f40d254b1f2af98344ba79daa
Author:     Tor Lillqvist <t...@collabora.com>
AuthorDate: Mon Apr 20 17:01:00 2020 +0300
Commit:     Tor Lillqvist <t...@collabora.com>
CommitDate: Tue Apr 21 12:15:07 2020 +0200

    tdf#128502: Get rid of the static file-level variable 'document' in Kit.cpp
    
    It is not a problem in the multi-process web-based Online, where the
    variable exists separately in each KIT process (which handles exactly
    one document). But in a mobile app, when we want to be able to handle
    multiple document in the single process, we can't have such variables.
    
    Change-Id: I1d3da48316eb3a8c72ff4957cc3fcba8f6870f16
    Reviewed-on: https://gerrit.libreoffice.org/c/online/+/92582
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Reviewed-by: Tor Lillqvist <t...@collabora.com>

diff --git a/kit/ChildSession.cpp b/kit/ChildSession.cpp
index cfc7c8baa..aa9f2545c 100644
--- a/kit/ChildSession.cpp
+++ b/kit/ChildSession.cpp
@@ -1464,7 +1464,7 @@ bool ChildSession::unoCommand(const char* /*buffer*/, int 
/*length*/, const Stri
     {
         if (tokens[1] == ".uno:fakeDiskFull")
         {
-            Util::alertAllUsers("internal", "diskfull");
+            _docManager->alertAllUsers("internal", "diskfull");
         }
         else
         {
diff --git a/kit/ChildSession.hpp b/kit/ChildSession.hpp
index 5cb75d702..10f3dbaf1 100644
--- a/kit/ChildSession.hpp
+++ b/kit/ChildSession.hpp
@@ -69,6 +69,8 @@ public:
     virtual std::shared_ptr<TileQueue>& getTileQueue() = 0;
 
     virtual bool sendFrame(const char* buffer, int length, WSOpCode opCode = 
WSOpCode::Text) = 0;
+
+    virtual void alertAllUsers(const std::string& cmd, const std::string& 
kind) = 0;
 };
 
 struct RecordedEvent
diff --git a/kit/Kit.cpp b/kit/Kit.cpp
index dff8f08d3..142392f6e 100644
--- a/kit/Kit.cpp
+++ b/kit/Kit.cpp
@@ -105,7 +105,6 @@ using std::size_t;
 
 // We only host a single document in our lifetime.
 class Document;
-static std::shared_ptr<Document> document;
 #ifndef BUILDING_TESTS
 static bool AnonymizeUserData = false;
 static uint64_t AnonymizationSalt = 82589933;
@@ -1197,6 +1196,11 @@ public:
         return false;
     }
 
+    void alertAllUsers(const std::string& cmd, const std::string& kind)
+    {
+        alertAllUsers("errortoall: cmd=" + cmd + " kind=" + kind);
+    }
+
     static void GlobalCallback(const int type, const char* p, void* data)
     {
         if (SigUtil::getTerminationFlag())
@@ -2035,6 +2039,11 @@ private:
         return _obfuscatedFileId;
     }
 
+    void alertAllUsers(const std::string& msg)
+    {
+        sendTextFrame(msg);
+    }
+
 private:
     std::shared_ptr<lok::Office> _loKit;
     const std::string _jailId;
@@ -2091,20 +2100,106 @@ std::shared_ptr<lok::Document> getLOKDocument()
     return Document::_loKitDocument;
 }
 
+class KitSocketPoll : public SocketPoll
+{
+    std::chrono::steady_clock::time_point _pollEnd;
+    std::shared_ptr<Document> _document;
+
+public:
+    KitSocketPoll() :
+        SocketPoll("kit")
+    {
+    }
+
+    // process pending message-queue events.
+    void drainQueue(const std::chrono::steady_clock::time_point &now)
+    {
+        if (_document)
+            _document->drainQueue(now);
+    }
+
+    // called from inside poll, inside a wakeup
+    void wakeupHook()
+    {
+        _pollEnd = std::chrono::steady_clock::now();
+    }
+
+    // a LOK compatible poll function merging the functions.
+    // returns the number of events signalled
+    int kitPoll(int timeoutMicroS)
+    {
+        if (SigUtil::getTerminationFlag())
+        {
+            LOG_TRC("Termination of unipoll mainloop flagged");
+            return -1;
+        }
+
+        // The maximum number of extra events to process beyond the first.
+        int maxExtraEvents = 15;
+        int eventsSignalled = 0;
+
+
+        if (timeoutMicroS < 0)
+        {
+            // Flush at most 1 + maxExtraEvents, or return when nothing left.
+            while (poll(0) > 0 && maxExtraEvents-- > 0)
+                ++eventsSignalled;
+        }
+        else
+        {
+            // Flush at most maxEvents+1, or return when nothing left.
+            _pollEnd = std::chrono::steady_clock::now() + 
std::chrono::microseconds(timeoutMicroS);
+            do
+            {
+                if (poll(timeoutMicroS) <= 0)
+                    break;
+
+                const auto now = std::chrono::steady_clock::now();
+                drainQueue(now);
+
+                timeoutMicroS = 
std::chrono::duration_cast<std::chrono::microseconds>(_pollEnd - now).count();
+                ++eventsSignalled;
+            }
+            while (timeoutMicroS > 0 && !SigUtil::getTerminationFlag() && 
maxExtraEvents-- > 0);
+        }
+
+        drainQueue(std::chrono::steady_clock::now());
+
+#if !MOBILEAPP
+        if (_document && _document->purgeSessions() == 0)
+        {
+            LOG_INF("Last session discarded. Setting TerminationFlag");
+            SigUtil::setTerminationFlag();
+            return -1;
+        }
+#endif
+        // Report the number of events we processed.
+        return eventsSignalled;
+    }
+
+    void setDocument(std::shared_ptr<Document> document)
+    {
+        _document = document;
+    }
+};
+
 class KitWebSocketHandler final : public WebSocketHandler
 {
     std::shared_ptr<TileQueue> _queue;
     std::string _socketName;
     std::shared_ptr<lok::Office> _loKit;
     std::string _jailId;
+    std::shared_ptr<Document> _document;
+    KitSocketPoll &_ksPoll;
 
 public:
-    KitWebSocketHandler(const std::string& socketName, const 
std::shared_ptr<lok::Office>& loKit, const std::string& jailId) :
+    KitWebSocketHandler(const std::string& socketName, const 
std::shared_ptr<lok::Office>& loKit, const std::string& jailId, KitSocketPoll& 
ksPoll) :
         WebSocketHandler(/* isClient = */ true, /* isMasking */ false),
         _queue(std::make_shared<TileQueue>()),
         _socketName(socketName),
         _loKit(loKit),
-        _jailId(jailId)
+        _jailId(jailId),
+        _ksPoll(ksPoll)
     {
     }
 
@@ -2155,13 +2250,16 @@ protected:
             LOG_INF("New session [" << sessionId << "] request on url [" << 
url << "].");
             Util::setThreadName("kit_" + docId);
 
-            if (!document)
-                document = std::make_shared<Document>(
+            if (!_document)
+            {
+                _document = std::make_shared<Document>(
                     _loKit, _jailId, docKey, docId, url, _queue,
                     
std::static_pointer_cast<WebSocketHandler>(shared_from_this()));
+                _ksPoll.setDocument(_document);
+            }
 
             // Validate and create session.
-            if (!(url == document->getUrl() && 
document->createSession(sessionId)))
+            if (!(url == _document->getUrl() && 
_document->createSession(sessionId)))
             {
                 LOG_DBG("CreateSession failed.");
             }
@@ -2176,14 +2274,14 @@ protected:
 #else
             LOG_INF("Setting TerminationFlag due to 'exit' command.");
             SigUtil::setTerminationFlag();
-            document.reset();
+            _document.reset();
 #endif
         }
         else if (tokens.equals(0, "tile") || tokens.equals(0, "tilecombine") 
|| tokens.equals(0, "canceltiles") ||
                 tokens.equals(0, "paintwindow") || tokens.equals(0, 
"resizewindow") ||
                 LOOLProtocol::getFirstToken(tokens[0], '-') == "child")
         {
-            if (document)
+            if (_document)
             {
                 _queue->put(message);
             }
@@ -2222,82 +2320,6 @@ void documentViewCallback(const int type, const char* 
payload, void* data)
     Document::ViewCallback(type, payload, data);
 }
 
-class KitSocketPoll : public SocketPoll
-{
-    std::chrono::steady_clock::time_point _pollEnd;
-public:
-    KitSocketPoll() :
-        SocketPoll("kit")
-    {
-    }
-
-    // process pending message-queue events.
-    void drainQueue(const std::chrono::steady_clock::time_point &now)
-    {
-        if (document)
-            document->drainQueue(now);
-    }
-
-    // called from inside poll, inside a wakeup
-    void wakeupHook()
-    {
-        _pollEnd = std::chrono::steady_clock::now();
-    }
-
-    // a LOK compatible poll function merging the functions.
-    // returns the number of events signalled
-    int kitPoll(int timeoutMicroS)
-    {
-        if (SigUtil::getTerminationFlag())
-        {
-            LOG_TRC("Termination of unipoll mainloop flagged");
-            return -1;
-        }
-
-        // The maximum number of extra events to process beyond the first.
-        int maxExtraEvents = 15;
-        int eventsSignalled = 0;
-
-
-        if (timeoutMicroS < 0)
-        {
-            // Flush at most 1 + maxExtraEvents, or return when nothing left.
-            while (poll(0) > 0 && maxExtraEvents-- > 0)
-                ++eventsSignalled;
-        }
-        else
-        {
-            // Flush at most maxEvents+1, or return when nothing left.
-            _pollEnd = std::chrono::steady_clock::now() + 
std::chrono::microseconds(timeoutMicroS);
-            do
-            {
-                if (poll(timeoutMicroS) <= 0)
-                    break;
-
-                const auto now = std::chrono::steady_clock::now();
-                drainQueue(now);
-
-                timeoutMicroS = 
std::chrono::duration_cast<std::chrono::microseconds>(_pollEnd - now).count();
-                ++eventsSignalled;
-            }
-            while (timeoutMicroS > 0 && !SigUtil::getTerminationFlag() && 
maxExtraEvents-- > 0);
-        }
-
-        drainQueue(std::chrono::steady_clock::now());
-
-#if !MOBILEAPP
-        if (document && document->purgeSessions() == 0)
-        {
-            LOG_INF("Last session discarded. Setting TerminationFlag");
-            SigUtil::setTerminationFlag();
-            return -1;
-        }
-#endif
-        // Report the number of events we processed.
-        return eventsSignalled;
-    }
-};
-
 /// Called by LOK main-loop the central location for data processing.
 int pollCallback(void* pData, int timeoutUs)
 {
@@ -2660,8 +2682,9 @@ void lokit_main(
         KitSocketPoll mainKit;
         mainKit.runOnClientThread(); // We will do the polling on this thread.
 
-        std::shared_ptr<ProtocolHandlerInterface> websocketHandler =
-            std::make_shared<KitWebSocketHandler>("child_ws", loKit, jailId);
+        std::shared_ptr<KitWebSocketHandler> websocketHandler =
+            std::make_shared<KitWebSocketHandler>("child_ws", loKit, jailId, 
mainKit);
+
 #if !MOBILEAPP
         mainKit.insertNewUnixSocket(MasterLocation, pathAndQuery, 
websocketHandler);
 #else
@@ -2827,23 +2850,6 @@ std::string anonymizeUsername(const std::string& 
username)
 #endif
 }
 
-#if !defined(BUILDING_TESTS) && !defined(KIT_IN_PROCESS)
-namespace Util
-{
-
-void alertAllUsers(const std::string& msg)
-{
-    document->sendTextFrame(msg);
-}
-
-void alertAllUsers(const std::string& cmd, const std::string& kind)
-{
-    alertAllUsers("errortoall: cmd=" + cmd + " kind=" + kind);
-}
-
-}
-#endif
-
-#endif // MOBILEAPP
+#endif // !MOBILEAPP
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/test/WhiteBoxTests.cpp b/test/WhiteBoxTests.cpp
index 12112d120..66f3e895f 100644
--- a/test/WhiteBoxTests.cpp
+++ b/test/WhiteBoxTests.cpp
@@ -561,6 +561,10 @@ public:
     {
         return true;
     }
+
+    void alertAllUsers(const std::string& /*cmd*/, const std::string& 
/*kind*/) override
+    {
+    }
 };
 
 void WhiteBoxTests::testEmptyCellCursor()
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to