[Libreoffice-commits] online.git: 2 commits - loolwsd/ChildProcessSession.cpp loolwsd/ChildProcessSession.hpp loolwsd/LOOLKit.cpp loolwsd/LOOLSession.cpp loolwsd/LOOLSession.hpp loolwsd/MasterProcessSession.cpp loolwsd/MasterProcessSession.hpp loolwsd/TileCache.cpp loolwsd/TileCache.hpp

Ashod Nakashian ashod.nakashian at collabora.co.uk
Fri Dec 25 19:59:08 PST 2015


 loolwsd/ChildProcessSession.cpp  |   98 +++++++++++++++++++++++++++++++--------
 loolwsd/ChildProcessSession.hpp  |    8 +--
 loolwsd/LOOLKit.cpp              |   34 +++++++++++--
 loolwsd/LOOLSession.cpp          |   29 +++++++++++
 loolwsd/LOOLSession.hpp          |    6 +-
 loolwsd/MasterProcessSession.cpp |    8 +--
 loolwsd/MasterProcessSession.hpp |    6 +-
 loolwsd/TileCache.cpp            |   12 ++--
 loolwsd/TileCache.hpp            |    4 -
 9 files changed, 161 insertions(+), 44 deletions(-)

New commits:
commit 8fc1e3c65b1227ccd5e506f7ccc46d4edfed70af
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Fri Dec 25 19:37:44 2015 -0500

    loolwsd: better error handling and resilience
    
    Change-Id: Ic0208ccb72cb35cb8f8b3c1f36b636dd3a467dc1
    Reviewed-on: https://gerrit.libreoffice.org/20948
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
    Tested-by: Ashod Nakashian <ashnakash at gmail.com>

diff --git a/loolwsd/ChildProcessSession.cpp b/loolwsd/ChildProcessSession.cpp
index 4a7b3ef..5eeda67 100644
--- a/loolwsd/ChildProcessSession.cpp
+++ b/loolwsd/ChildProcessSession.cpp
@@ -58,7 +58,7 @@ ChildProcessSession::ChildProcessSession(std::shared_ptr<WebSocket> ws,
 
 ChildProcessSession::~ChildProcessSession()
 {
-    Log::info() << "ChildProcessSession ctor " << Kind::ToMaster
+    Log::info() << "ChildProcessSession dtor " << Kind::ToMaster
                 << " this:" << this << " ws:" << _ws.get() << Log::end;
     if (LIBREOFFICEKIT_HAS(_loKit, registerCallback))
         _loKit->pClass->registerCallback(_loKit, 0, 0);
@@ -216,9 +216,10 @@ extern "C"
     }
 }
 
-
 bool ChildProcessSession::loadDocument(const char *buffer, int length, StringTokenizer& tokens)
 {
+    Poco::Mutex::ScopedLock lock(_mutex);
+
     int part = -1;
     if (tokens.count() < 2)
     {
@@ -253,24 +254,29 @@ bool ChildProcessSession::loadDocument(const char *buffer, int length, StringTok
 
     // The URL in the request is the original one, not visible in the chroot jail.
     // The child process uses the fixed name jailDocumentURL.
-
-    if (LIBREOFFICEKIT_HAS(_loKit, registerCallback))
-        _loKit->pClass->registerCallback(_loKit, myCallback, this);
-
     if (aUri.isRelative() || aUri.getScheme() == "file")
+    {
         aUri = URI( URI("file://"), Path(jailDocumentURL + Path::separator() + std::to_string(Process::id()),
                     Path(aUri.getPath()).getFileName()).toString() );
+        Log::info("Local URI: [" + aUri.toString() + "].");
+    }
+
 
     if (_loKitDocument != nullptr)
     {
         _viewId = _loKitDocument->pClass->createView(_loKitDocument);
     }
     else
-    if ((_loKitDocument = _loKit->pClass->documentLoad(_loKit, aUri.toString().c_str())) == nullptr)
     {
-        sendTextFrame("error: cmd=load kind=failed");
-        Log::error("Failed to load: " + aUri.toString() + ", error is: " + _loKit->pClass->getError(_loKit));
-        return false;
+        if ( LIBREOFFICEKIT_HAS(_loKit, registerCallback))
+            _loKit->pClass->registerCallback(_loKit, myCallback, this);
+
+        if ((_loKitDocument = _loKit->pClass->documentLoad(_loKit, aUri.toString().c_str())) == nullptr)
+        {
+            Log::error("Failed to load: " + aUri.toString() + ", error: " + _loKit->pClass->getError(_loKit));
+            sendTextFrame("error: cmd=load kind=failed");
+            return false;
+        }
     }
 
     _loKitDocument->pClass->setView(_loKitDocument, _viewId);
@@ -286,17 +292,17 @@ bool ChildProcessSession::loadDocument(const char *buffer, int length, StringTok
 
     _loKitDocument->pClass->initializeForRendering(_loKitDocument, (renderingOptions.empty() ? nullptr : renderingOptions.c_str()));
 
-    if ( _docType != "text" && part != -1)
+    if (_docType != "text" && part != -1)
     {
         _clientPart = part;
         _loKitDocument->pClass->setPart(_loKitDocument, part);
     }
 
+    _loKitDocument->pClass->registerCallback(_loKitDocument, myCallback, this);
+
     if (!getStatus(buffer, length))
         return false;
 
-    _loKitDocument->pClass->registerCallback(_loKitDocument, myCallback, this);
-
     return true;
 }
 
@@ -313,6 +319,10 @@ void ChildProcessSession::sendFontRendering(const char* /*buffer*/, int /*length
         return;
     }
 
+    Poco::Mutex::ScopedLock lock(_mutex);
+
+   _loKitDocument->pClass->setView(_loKitDocument, _viewId);
+
     URI::decode(font, decodedFont);
     std::string response = "renderfont: " + Poco::cat(std::string(" "), tokens.begin() + 1, tokens.end()) + "\n";
 
@@ -340,6 +350,8 @@ void ChildProcessSession::sendFontRendering(const char* /*buffer*/, int /*length
 
 bool ChildProcessSession::getStatus(const char* /*buffer*/, int /*length*/)
 {
+    Poco::Mutex::ScopedLock lock(_mutex);
+
     std::string status = "status: " + LOKitHelper::documentStatus(_loKitDocument);
     StringTokenizer tokens(status, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
     if (!getTokenString(tokens[1], "type", _docType))
@@ -353,6 +365,8 @@ bool ChildProcessSession::getStatus(const char* /*buffer*/, int /*length*/)
 
 bool ChildProcessSession::getCommandValues(const char* /*buffer*/, int /*length*/, StringTokenizer& tokens)
 {
+    Poco::Mutex::ScopedLock lock(_mutex);
+
     std::string command;
     if (tokens.count() != 2 || !getTokenString(tokens[1], "command", command))
     {
@@ -365,12 +379,17 @@ bool ChildProcessSession::getCommandValues(const char* /*buffer*/, int /*length*
 
 bool ChildProcessSession::getPartPageRectangles(const char* /*buffer*/, int /*length*/)
 {
-    sendTextFrame("partpagerectangles: " + std::string(_loKitDocument->pClass->getPartPageRectangles(_loKitDocument)));
+    Poco::Mutex::ScopedLock lock(_mutex);
+
+   _loKitDocument->pClass->setView(_loKitDocument, _viewId);
+   sendTextFrame("partpagerectangles: " + std::string(_loKitDocument->pClass->getPartPageRectangles(_loKitDocument)));
     return true;
 }
 
 void ChildProcessSession::sendTile(const char* /*buffer*/, int /*length*/, StringTokenizer& tokens)
 {
+    Poco::Mutex::ScopedLock lock(_mutex);
+
     int part, width, height, tilePosX, tilePosY, tileWidth, tileHeight;
 
     if (tokens.count() < 8 ||
@@ -398,6 +417,8 @@ void ChildProcessSession::sendTile(const char* /*buffer*/, int /*length*/, Strin
         return;
     }
 
+    _loKitDocument->pClass->setView(_loKitDocument, _viewId);
+
     std::string response = "tile: " + Poco::cat(std::string(" "), tokens.begin() + 1, tokens.end()) + "\n";
 
     std::vector<char> output;
@@ -432,7 +453,9 @@ void ChildProcessSession::sendTile(const char* /*buffer*/, int /*length*/, Strin
 
 bool ChildProcessSession::clientZoom(const char* /*buffer*/, int /*length*/, StringTokenizer& tokens)
 {
-    int tilePixelWidth, tilePixelHeight, tileTwipWidth, tileTwipHeight;
+    Poco::Mutex::ScopedLock lock(_mutex);
+
+   int tilePixelWidth, tilePixelHeight, tileTwipWidth, tileTwipHeight;
 
     if (tokens.count() != 5 ||
         !getTokenInteger(tokens[1], "tilepixelwidth", tilePixelWidth) ||
@@ -444,12 +467,15 @@ bool ChildProcessSession::clientZoom(const char* /*buffer*/, int /*length*/, Str
         return false;
     }
 
+    _loKitDocument->pClass->setView(_loKitDocument, _viewId);
     _loKitDocument->pClass->setClientZoom(_loKitDocument, tilePixelWidth, tilePixelHeight, tileTwipWidth, tileTwipHeight);
     return true;
 }
 
 bool ChildProcessSession::downloadAs(const char* /*buffer*/, int /*length*/, StringTokenizer& tokens)
 {
+    Poco::Mutex::ScopedLock lock(_mutex);
+
     std::string name, id, format, filterOptions;
 
     if (tokens.count() < 5 ||
@@ -502,6 +528,8 @@ bool ChildProcessSession::getChildId()
 
 bool ChildProcessSession::getTextSelection(const char* /*buffer*/, int /*length*/, StringTokenizer& tokens)
 {
+    Poco::Mutex::ScopedLock lock(_mutex);
+
     std::string mimeType;
 
     if (tokens.count() != 2 ||
@@ -511,6 +539,7 @@ bool ChildProcessSession::getTextSelection(const char* /*buffer*/, int /*length*
         return false;
     }
 
+    _loKitDocument->pClass->setView(_loKitDocument, _viewId);
     char *textSelection = _loKitDocument->pClass->getTextSelection(_loKitDocument, mimeType.c_str(), nullptr);
 
     sendTextFrame("textselectioncontent: " + std::string(textSelection));
@@ -530,6 +559,9 @@ bool ChildProcessSession::paste(const char* /*buffer*/, int /*length*/, StringTo
 
     data = Poco::cat(std::string(" "), tokens.begin() + 2, tokens.end()).substr(strlen("data="));
 
+    Poco::Mutex::ScopedLock lock(_mutex);
+
+    _loKitDocument->pClass->setView(_loKitDocument, _viewId);
     _loKitDocument->pClass->paste(_loKitDocument, mimeType.c_str(), data.c_str(), std::strlen(data.c_str()));
 
     return true;
@@ -547,6 +579,8 @@ bool ChildProcessSession::insertFile(const char* /*buffer*/, int /*length*/, Str
         return false;
     }
 
+    Poco::Mutex::ScopedLock lock(_mutex);
+
     if (type == "graphic")
     {
         std::string fileName = "file://" + jailDocumentURL + "/insertfile/" + name;
@@ -556,6 +590,7 @@ bool ChildProcessSession::insertFile(const char* /*buffer*/, int /*length*/, Str
                 "\"type\":\"string\","
                 "\"value\":\"" + fileName + "\""
             "}}";
+        _loKitDocument->pClass->setView(_loKitDocument, _viewId);
         _loKitDocument->pClass->postUnoCommand(_loKitDocument, command.c_str(), arguments.c_str(), false);
     }
 
@@ -564,6 +599,8 @@ bool ChildProcessSession::insertFile(const char* /*buffer*/, int /*length*/, Str
 
 bool ChildProcessSession::keyEvent(const char* /*buffer*/, int /*length*/, StringTokenizer& tokens)
 {
+    Poco::Mutex::ScopedLock lock(_mutex);
+
     int type, charcode, keycode;
 
     if (tokens.count() != 4 ||
@@ -577,6 +614,7 @@ bool ChildProcessSession::keyEvent(const char* /*buffer*/, int /*length*/, Strin
         return false;
     }
 
+    _loKitDocument->pClass->setView(_loKitDocument, _viewId);
     _loKitDocument->pClass->postKeyEvent(_loKitDocument, type, charcode, keycode);
 
     return true;
@@ -584,6 +622,8 @@ bool ChildProcessSession::keyEvent(const char* /*buffer*/, int /*length*/, Strin
 
 bool ChildProcessSession::mouseEvent(const char* /*buffer*/, int /*length*/, StringTokenizer& tokens)
 {
+    Poco::Mutex::ScopedLock lock(_mutex);
+
     int type, x, y, count, buttons, modifier;
 
     if (tokens.count() != 7 ||
@@ -602,6 +642,7 @@ bool ChildProcessSession::mouseEvent(const char* /*buffer*/, int /*length*/, Str
         return false;
     }
 
+    _loKitDocument->pClass->setView(_loKitDocument, _viewId);
     _loKitDocument->pClass->postMouseEvent(_loKitDocument, type, x, y, count, buttons, modifier);
 
     return true;
@@ -609,12 +650,16 @@ bool ChildProcessSession::mouseEvent(const char* /*buffer*/, int /*length*/, Str
 
 bool ChildProcessSession::unoCommand(const char* /*buffer*/, int /*length*/, StringTokenizer& tokens)
 {
+    Poco::Mutex::ScopedLock lock(_mutex);
+
     if (tokens.count() == 1)
     {
         sendTextFrame("error: cmd=uno kind=syntax");
         return false;
     }
 
+    _loKitDocument->pClass->setView(_loKitDocument, _viewId);
+
     // we need to get LOK_CALLBACK_UNO_COMMAND_RESULT callback when saving
     bool bNotify = (tokens[1] == ".uno:Save");
 
@@ -632,6 +677,8 @@ bool ChildProcessSession::unoCommand(const char* /*buffer*/, int /*length*/, Str
 
 bool ChildProcessSession::selectText(const char* /*buffer*/, int /*length*/, StringTokenizer& tokens)
 {
+    Poco::Mutex::ScopedLock lock(_mutex);
+
     int type, x, y;
 
     if (tokens.count() != 4 ||
@@ -647,6 +694,7 @@ bool ChildProcessSession::selectText(const char* /*buffer*/, int /*length*/, Str
         return false;
     }
 
+    _loKitDocument->pClass->setView(_loKitDocument, _viewId);
     _loKitDocument->pClass->setTextSelection(_loKitDocument, type, x, y);
 
     return true;
@@ -654,6 +702,8 @@ bool ChildProcessSession::selectText(const char* /*buffer*/, int /*length*/, Str
 
 bool ChildProcessSession::selectGraphic(const char* /*buffer*/, int /*length*/, StringTokenizer& tokens)
 {
+    Poco::Mutex::ScopedLock lock(_mutex);
+
     int type, x, y;
 
     if (tokens.count() != 4 ||
@@ -668,6 +718,7 @@ bool ChildProcessSession::selectGraphic(const char* /*buffer*/, int /*length*/,
         return false;
     }
 
+    _loKitDocument->pClass->setView(_loKitDocument, _viewId);
     _loKitDocument->pClass->setGraphicSelection(_loKitDocument, type, x, y);
 
     return true;
@@ -675,12 +726,15 @@ bool ChildProcessSession::selectGraphic(const char* /*buffer*/, int /*length*/,
 
 bool ChildProcessSession::resetSelection(const char* /*buffer*/, int /*length*/, StringTokenizer& tokens)
 {
+    Poco::Mutex::ScopedLock lock(_mutex);
+
     if (tokens.count() != 1)
     {
         sendTextFrame("error: cmd=resetselection kind=syntax");
         return false;
     }
 
+    _loKitDocument->pClass->setView(_loKitDocument, _viewId);
     _loKitDocument->pClass->resetSelection(_loKitDocument);
 
     return true;
@@ -688,6 +742,8 @@ bool ChildProcessSession::resetSelection(const char* /*buffer*/, int /*length*/,
 
 bool ChildProcessSession::saveAs(const char* /*buffer*/, int /*length*/, StringTokenizer& tokens)
 {
+    Poco::Mutex::ScopedLock lock(_mutex);
+
     std::string url, format, filterOptions;
 
     if (tokens.count() < 4 ||
@@ -707,6 +763,8 @@ bool ChildProcessSession::saveAs(const char* /*buffer*/, int /*length*/, StringT
         }
     }
 
+    _loKitDocument->pClass->setView(_loKitDocument, _viewId);
+
     bool success = _loKitDocument->pClass->saveAs(_loKitDocument, url.c_str(),
             format.size() == 0 ? nullptr :format.c_str(),
             filterOptions.size() == 0 ? nullptr : filterOptions.c_str());
@@ -732,6 +790,8 @@ bool ChildProcessSession::setClientPart(const char* /*buffer*/, int /*length*/,
 
 bool ChildProcessSession::setPage(const char* /*buffer*/, int /*length*/, StringTokenizer& tokens)
 {
+    Poco::Mutex::ScopedLock lock(_mutex);
+
     int page;
     if (tokens.count() < 2 ||
         !getTokenInteger(tokens[1], "page", page))
@@ -739,6 +799,8 @@ bool ChildProcessSession::setPage(const char* /*buffer*/, int /*length*/, String
         sendTextFrame("error: cmd=setpage kind=invalid");
         return false;
     }
+
+    _loKitDocument->pClass->setView(_loKitDocument, _viewId);
     _loKitDocument->pClass->setPart(_loKitDocument, page);
     return true;
 }
diff --git a/loolwsd/LOOLKit.cpp b/loolwsd/LOOLKit.cpp
index c819331..6f14936 100644
--- a/loolwsd/LOOLKit.cpp
+++ b/loolwsd/LOOLKit.cpp
@@ -121,9 +121,9 @@ public:
     void callback(const int nType, const std::string& rPayload, void* pData)
     {
         ChildProcessSession *srv = reinterpret_cast<ChildProcessSession *>(pData);
-        Log::debug() << "Callback [" << srv->_viewId << "] "
-                     << std::string(callbackTypeToString(nType))
-                     << " " << rPayload << Log::end;
+        Log::trace() << "Callback [" << srv->_viewId << "] "
+                     << callbackTypeToString(nType)
+                     << " [" << rPayload << "]." << Log::end;
 
         switch ((LibreOfficeKitCallbackType) nType)
         {
@@ -242,9 +242,27 @@ public:
                 CallBackNotification::Ptr aCallBackNotification = aNotification.cast<CallBackNotification>();
                 if (aCallBackNotification)
                 {
+                    const auto nType = aCallBackNotification->m_nType;
+                    try
                     {
                         FastMutex::ScopedLock lock(_mutex);
-                        callback(aCallBackNotification->m_nType, aCallBackNotification->m_aPayload, aCallBackNotification->m_pSession);
+                        callback(nType, aCallBackNotification->m_aPayload, aCallBackNotification->m_pSession);
+                    }
+                    catch (const Exception& exc)
+                    {
+                        Log::error() << "Error while handling callback [" << callbackTypeToString(nType) << "]. "
+                                     << exc.displayText()
+                                     << (exc.nested() ? " (" + exc.nested()->displayText() + ")" : "")
+                                     << Log::end;
+                    }
+                    catch (const std::exception& exc)
+                    {
+                        Log::error("Error while handling callback [" + callbackTypeToString(nType) + "]. " +
+                                   std::string("Exception: ") + exc.what());
+                    }
+                    catch (...)
+                    {
+                        Log::error("Unexpected Exception while handling callback [" + callbackTypeToString(nType) + "].");
                     }
                 }
             }
@@ -388,7 +406,9 @@ public:
         }
         catch (const Exception& exc)
         {
-            Log::error(std::string("Exception: ") + exc.what());
+            Log::error() << exc.displayText()
+                         << (exc.nested() ? " (" + exc.nested()->displayText() + ")" : "")
+                         << Log::end;
         }
         catch (const std::exception& exc)
         {
@@ -607,7 +627,9 @@ void run_lok_main(const std::string &loSubPath, Poco::UInt64 _childId, const std
     }
     catch (const Exception& exc)
     {
-        Log::error(std::string("Exception: ") + exc.what());
+        Log::error() << exc.name() << ": " << exc.displayText()
+                     << (exc.nested() ? " (" + exc.nested()->displayText() + ")" : "")
+                     << Log::end;
     }
     catch (const std::exception& exc)
     {
diff --git a/loolwsd/LOOLSession.cpp b/loolwsd/LOOLSession.cpp
index 53f46cf..f78abfe 100644
--- a/loolwsd/LOOLSession.cpp
+++ b/loolwsd/LOOLSession.cpp
@@ -190,11 +190,15 @@ bool LOOLSession::handleInput(const char *buffer, int length)
     }
     catch (const Exception& exc)
     {
-        Log::error(std::string("Exception: ") + exc.what());
+        Log::error() << "Error while handling [" + getFirstLine(buffer, length) + "]. "
+                     << exc.displayText()
+                     << (exc.nested() ? " (" + exc.nested()->displayText() + ")" : "")
+                     << Log::end;
     }
     catch (const std::exception& exc)
     {
-        Log::error(std::string("Exception: ") + exc.what());
+        Log::error("Error while handling [" + getFirstLine(buffer, length) + "]. " +
+                   std::string("Exception: ") + exc.what());
     }
     catch (...)
     {
diff --git a/loolwsd/MasterProcessSession.cpp b/loolwsd/MasterProcessSession.cpp
index a423eaa..f7dfa9b 100644
--- a/loolwsd/MasterProcessSession.cpp
+++ b/loolwsd/MasterProcessSession.cpp
@@ -61,7 +61,7 @@ MasterProcessSession::MasterProcessSession(std::shared_ptr<WebSocket> ws, const
 
 MasterProcessSession::~MasterProcessSession()
 {
-    Log::info() << "MasterProcessSession ctor " << _kindString
+    Log::info() << "MasterProcessSession dtor " << _kindString
                 << " this:" << this << " ws:" << _ws.get() << Log::end;
 
     auto peer = _peer.lock();
diff --git a/loolwsd/TileCache.cpp b/loolwsd/TileCache.cpp
index 56641c1..938552c 100644
--- a/loolwsd/TileCache.cpp
+++ b/loolwsd/TileCache.cpp
@@ -218,14 +218,14 @@ std::unique_ptr<std::fstream> TileCache::lookupRendering(const std::string& name
 void TileCache::invalidateTiles(int part, int x, int y, int width, int height)
 {
     // in the Editing cache, remove immediately
-    std::string editingDirName = cacheDirName(true);
+    const std::string editingDirName = cacheDirName(true);
     File editingDir(editingDirName);
     if (editingDir.exists() && editingDir.isDirectory())
     {
         _cacheMutex.lock();
         for (auto tileIterator = DirectoryIterator(editingDir); tileIterator != DirectoryIterator(); ++tileIterator)
         {
-            std::string fileName = tileIterator.path().getFileName();
+            const std::string fileName = tileIterator.path().getFileName();
             if (intersectsTile(fileName, part, x, y, width, height))
             {
                 File(tileIterator.path()).remove();
@@ -235,13 +235,13 @@ void TileCache::invalidateTiles(int part, int x, int y, int width, int height)
     }
 
     // in the Persistent cache, add to _toBeRemoved for removal on save
-    std::string persistentDirName = cacheDirName(false);
+    const std::string persistentDirName = cacheDirName(false);
     File persistentDir(persistentDirName);
     if (persistentDir.exists() && persistentDir.isDirectory())
     {
         for (auto tileIterator = DirectoryIterator(persistentDir); tileIterator != DirectoryIterator(); ++tileIterator)
         {
-            std::string fileName = tileIterator.path().getFileName();
+            const std::string fileName = tileIterator.path().getFileName();
             if (_toBeRemoved.find(fileName) == _toBeRemoved.end() && intersectsTile(fileName, part, x, y, width, height))
             {
                 _toBeRemoved.insert(fileName);
@@ -304,12 +304,12 @@ std::string TileCache::cacheFileName(int part, int width, int height, int tilePo
             std::to_string(tileWidth) + "x" + std::to_string(tileHeight) + ".png");
 }
 
-bool TileCache::parseCacheFileName(std::string& fileName, int& part, int& width, int& height, int& tilePosX, int& tilePosY, int& tileWidth, int& tileHeight)
+bool TileCache::parseCacheFileName(const std::string& fileName, int& part, int& width, int& height, int& tilePosX, int& tilePosY, int& tileWidth, int& tileHeight)
 {
     return (std::sscanf(fileName.c_str(), "%d_%dx%d.%d,%d.%dx%d.png", &part, &width, &height, &tilePosX, &tilePosY, &tileWidth, &tileHeight) == 7);
 }
 
-bool TileCache::intersectsTile(std::string& fileName, int part, int x, int y, int width, int height)
+bool TileCache::intersectsTile(const std::string& fileName, int part, int x, int y, int width, int height)
 {
     int tilePart, tilePixelWidth, tilePixelHeight, tilePosX, tilePosY, tileWidth, tileHeight;
 
diff --git a/loolwsd/TileCache.hpp b/loolwsd/TileCache.hpp
index 316f85f..9babf20 100644
--- a/loolwsd/TileCache.hpp
+++ b/loolwsd/TileCache.hpp
@@ -70,10 +70,10 @@ private:
     std::string cacheDirName(bool useEditingCache);
 
     std::string cacheFileName(int part, int width, int height, int tilePosX, int tilePosY, int tileWidth, int tileHeight);
-    bool parseCacheFileName(std::string& fileName, int& part, int& width, int& height, int& tilePosX, int& tilePosY, int& tileWidth, int& tileHeight);
+    bool parseCacheFileName(const std::string& fileName, int& part, int& width, int& height, int& tilePosX, int& tilePosY, int& tileWidth, int& tileHeight);
 
     /// Extract location from fileName, and check if it intersects with [x, y, width, height].
-    bool intersectsTile(std::string& fileName, int part, int x, int y, int width, int height);
+    bool intersectsTile(const std::string& fileName, int part, int x, int y, int width, int height);
 
     /// Load the timestamp from modtime.txt.
     Poco::Timestamp getLastModified();
commit 42d07b2aea57b60fb13ba20ed534f27b5838c762
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Fri Dec 25 13:51:32 2015 -0500

    loolwsd: refactored LOOLSession::handleInput to handle errors
    
    Change-Id: I3be929242317f4fafcb62c55d3532b2fbfd6591b
    Reviewed-on: https://gerrit.libreoffice.org/20947
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
    Tested-by: Ashod Nakashian <ashnakash at gmail.com>

diff --git a/loolwsd/ChildProcessSession.cpp b/loolwsd/ChildProcessSession.cpp
index 2f49ece..4a7b3ef 100644
--- a/loolwsd/ChildProcessSession.cpp
+++ b/loolwsd/ChildProcessSession.cpp
@@ -65,11 +65,9 @@ ChildProcessSession::~ChildProcessSession()
     Util::shutdownWebSocket(*_ws);
 }
 
-bool ChildProcessSession::handleInput(const char *buffer, int length)
+bool ChildProcessSession::_handleInput(const char *buffer, int length)
 {
-    Log::info(_kindString + ",Input," + getAbbreviatedMessage(buffer, length));
-
-    std::string firstLine = getFirstLine(buffer, length);
+    const std::string firstLine = getFirstLine(buffer, length);
     StringTokenizer tokens(firstLine, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
 
     if (tokens[0] == "canceltiles")
diff --git a/loolwsd/ChildProcessSession.hpp b/loolwsd/ChildProcessSession.hpp
index 0c62a39..f40fd7e 100644
--- a/loolwsd/ChildProcessSession.hpp
+++ b/loolwsd/ChildProcessSession.hpp
@@ -31,8 +31,6 @@ public:
                         const std::string& childId);
     virtual ~ChildProcessSession();
 
-    virtual bool handleInput(const char *buffer, int length) override;
-
     virtual bool getStatus(const char *buffer, int length);
 
     virtual bool getCommandValues(const char *buffer, int length, Poco::StringTokenizer& tokens);
@@ -73,7 +71,11 @@ public:
     LibreOfficeKit *_loKit;
     const std::string _childId;
 
- private:
+private:
+
+    virtual bool _handleInput(const char *buffer, int length) override;
+
+private:
     int _clientPart;
 };
 
diff --git a/loolwsd/LOOLSession.cpp b/loolwsd/LOOLSession.cpp
index da80bca..53f46cf 100644
--- a/loolwsd/LOOLSession.cpp
+++ b/loolwsd/LOOLSession.cpp
@@ -178,4 +178,29 @@ void LOOLSession::parseDocOptions(const StringTokenizer& tokens, int& part, std:
     }
 }
 
+bool LOOLSession::handleInput(const char *buffer, int length)
+{
+    assert(buffer != nullptr);
+
+    Log::trace(_kindString + ",Recv," + getAbbreviatedMessage(buffer, length));
+
+    try
+    {
+        return _handleInput(buffer, length);
+    }
+    catch (const Exception& exc)
+    {
+        Log::error(std::string("Exception: ") + exc.what());
+    }
+    catch (const std::exception& exc)
+    {
+        Log::error(std::string("Exception: ") + exc.what());
+    }
+    catch (...)
+    {
+        Log::error("Unexpected Exception.");
+    }
+
+    return false;
+}
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/loolwsd/LOOLSession.hpp b/loolwsd/LOOLSession.hpp
index 22a372f..b719ee8 100644
--- a/loolwsd/LOOLSession.hpp
+++ b/loolwsd/LOOLSession.hpp
@@ -50,7 +50,7 @@ public:
 
     virtual bool getPartPageRectangles(const char *buffer, int length) = 0;
 
-    virtual bool handleInput(const char *buffer, int length) = 0;
+    bool handleInput(const char *buffer, int length);
 
     static const std::string jailDocumentURL;
 
@@ -86,6 +86,10 @@ protected:
     std::string _docOptions;
 
 private:
+
+    virtual bool _handleInput(const char *buffer, int length) = 0;
+
+private:
     std::mutex _mutex;
 };
 
diff --git a/loolwsd/MasterProcessSession.cpp b/loolwsd/MasterProcessSession.cpp
index 96f2a73..a423eaa 100644
--- a/loolwsd/MasterProcessSession.cpp
+++ b/loolwsd/MasterProcessSession.cpp
@@ -71,11 +71,9 @@ MasterProcessSession::~MasterProcessSession()
     }
 }
 
-bool MasterProcessSession::handleInput(const char *buffer, int length)
+bool MasterProcessSession::_handleInput(const char *buffer, int length)
 {
-    Log::trace(_kindString + ",Recv," + getAbbreviatedMessage(buffer, length));
-
-    std::string firstLine = getFirstLine(buffer, length);
+    const std::string firstLine = getFirstLine(buffer, length);
     StringTokenizer tokens(firstLine, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
 
     if (haveSeparateProcess())
diff --git a/loolwsd/MasterProcessSession.hpp b/loolwsd/MasterProcessSession.hpp
index 4b9329a..85cc047 100644
--- a/loolwsd/MasterProcessSession.hpp
+++ b/loolwsd/MasterProcessSession.hpp
@@ -22,8 +22,6 @@ public:
     MasterProcessSession(std::shared_ptr<Poco::Net::WebSocket> ws, Kind kind);
     virtual ~MasterProcessSession();
 
-    virtual bool handleInput(const char *buffer, int length) override;
-
     bool haveSeparateProcess();
 
     static Poco::Path getJailPath(Poco::UInt64 childId);
@@ -72,6 +70,10 @@ public:
     std::unique_ptr<TileCache> _tileCache;
 
 private:
+
+    virtual bool _handleInput(const char *buffer, int length) override;
+
+private:
     // The id of the child process
     Poco::UInt64 _childId;
     // The pid of the child process


More information about the Libreoffice-commits mailing list