[Libreoffice-commits] online.git: loolwsd/ChildSession.cpp loolwsd/ChildSession.hpp loolwsd/LOOLKit.cpp

Ashod Nakashian ashod.nakashian at collabora.co.uk
Wed May 18 11:29:23 UTC 2016


 loolwsd/ChildSession.cpp |   11 +++----
 loolwsd/ChildSession.hpp |   14 +++------
 loolwsd/LOOLKit.cpp      |   67 ++++++++++++++++++-----------------------------
 3 files changed, 37 insertions(+), 55 deletions(-)

New commits:
commit a21bcc23412700ad39c2e6ad73928b23f188de6f
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Tue May 17 18:28:59 2016 -0400

    loolwsd: LibreOfficeKitDocument* -> shared_ptr<lok::Document>
    
    Change-Id: Iafc48317c225f93b7d58708dd26f04d6bb13988e
    Reviewed-on: https://gerrit.libreoffice.org/25092
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
    Tested-by: Ashod Nakashian <ashnakash at gmail.com>

diff --git a/loolwsd/ChildSession.cpp b/loolwsd/ChildSession.cpp
index 0c1d001..06a94c5 100644
--- a/loolwsd/ChildSession.cpp
+++ b/loolwsd/ChildSession.cpp
@@ -287,13 +287,12 @@ private:
 std::recursive_mutex ChildSession::Mutex;
 
 ChildSession::ChildSession(const std::string& id,
-                                         std::shared_ptr<WebSocket> ws,
-                                         LibreOfficeKitDocument * loKitDocument,
-                                         const std::string& jailId,
-                                         OnLoadCallback onLoad,
-                                         OnUnloadCallback onUnload) :
+                           std::shared_ptr<WebSocket> ws,
+                           const std::string& jailId,
+                           OnLoadCallback onLoad,
+                           OnUnloadCallback onUnload) :
     LOOLSession(id, Kind::ToMaster, ws),
-    _loKitDocument(loKitDocument),
+    _loKitDocument(nullptr),
     _multiView(std::getenv("LOK_VIEW_CALLBACK")),
     _jailId(jailId),
     _viewId(0),
diff --git a/loolwsd/ChildSession.hpp b/loolwsd/ChildSession.hpp
index e55a490..9795366 100644
--- a/loolwsd/ChildSession.hpp
+++ b/loolwsd/ChildSession.hpp
@@ -12,15 +12,12 @@
 
 #include <mutex>
 
-#define LOK_USE_UNSTABLE_API
-#include <LibreOfficeKit/LibreOfficeKit.h>
-#include <LibreOfficeKit/LibreOfficeKitEnums.h>
-
 #include <Poco/Thread.h>
 #include <Poco/NotificationQueue.h>
 
 #include "Common.hpp"
 #include "LOOLSession.hpp"
+#include "LibreOfficeKit.hpp"
 
 class CallbackWorker;
 typedef std::function<LibreOfficeKitDocument*(const std::string&, const std::string&, const std::string&, const std::string&, bool)> OnLoadCallback;
@@ -37,11 +34,10 @@ public:
     /// jailId The JailID of the jail root directory,
     //         used by downloadas to construct jailed path.
     ChildSession(const std::string& id,
-                        std::shared_ptr<Poco::Net::WebSocket> ws,
-                        LibreOfficeKitDocument * loKitDocument,
-                        const std::string& jailId,
-                        OnLoadCallback onLoad,
-                        OnUnloadCallback onUnload);
+                 std::shared_ptr<Poco::Net::WebSocket> ws,
+                 const std::string& jailId,
+                 OnLoadCallback onLoad,
+                 OnUnloadCallback onUnload);
     virtual ~ChildSession();
 
     bool getStatus(const char *buffer, int length);
diff --git a/loolwsd/LOOLKit.cpp b/loolwsd/LOOLKit.cpp
index 9015518..5c57bd6 100644
--- a/loolwsd/LOOLKit.cpp
+++ b/loolwsd/LOOLKit.cpp
@@ -29,7 +29,6 @@
 
 #define LOK_USE_UNSTABLE_API
 #include <LibreOfficeKit/LibreOfficeKitInit.h>
-#include <LibreOfficeKit/LibreOfficeKitEnums.h>
 
 #include <Poco/Exception.h>
 #include <Poco/Net/HTTPClientSession.h>
@@ -41,14 +40,15 @@
 #include <Poco/Runnable.h>
 #include <Poco/StringTokenizer.h>
 #include <Poco/Thread.h>
-#include <Poco/Util/Application.h>
 #include <Poco/URI.h>
+#include <Poco/Util/Application.h>
 
 #include "ChildSession.hpp"
 #include "Common.hpp"
 #include "IoUtil.hpp"
 #include "LOKitHelper.hpp"
 #include "LOOLProtocol.hpp"
+#include "LibreOfficeKit.hpp"
 #include "QueueHandler.hpp"
 #include "Unit.hpp"
 #include "UserMessages.hpp"
@@ -358,7 +358,6 @@ public:
         _jailId(jailId),
         _docKey(docKey),
         _url(url),
-        _loKitDocument(nullptr),
         _docPassword(""),
         _haveDocPassword(false),
         _isDocPasswordProtected(false),
@@ -407,22 +406,6 @@ public:
 
         // Destroy all connections and views.
         _connections.clear();
-
-        // TODO. check what is happening when destroying lokit document,
-        // often it blows up.
-        // Destroy the document.
-        if (_loKitDocument != nullptr)
-        {
-            try
-            {
-                _loKitDocument->pClass->destroy(_loKitDocument);
-            }
-            catch (const std::exception& exc)
-            {
-                Log::error() << "Document::~Document: " << exc.what()
-                             << Log::end;
-            }
-        }
     }
 
     const std::string& getUrl() const { return _url; }
@@ -463,7 +446,7 @@ public:
             auto ws = std::make_shared<WebSocket>(cs, request, response);
             ws->setReceiveTimeout(0);
 
-            auto session = std::make_shared<ChildSession>(sessionId, ws, _loKitDocument, _jailId,
+            auto session = std::make_shared<ChildSession>(sessionId, ws, _jailId,
                            [this](const std::string& id, const std::string& uri, const std::string& docPassword,
                                   const std::string& renderOpts, bool haveDocPassword) { return onLoad(id, uri, docPassword, renderOpts, haveDocPassword); },
                            [this](const std::string& id) { onUnload(id); });
@@ -618,7 +601,7 @@ public:
 
         std::unique_lock<std::recursive_mutex> lock(ChildSession::getLock());
 
-        if (_loKitDocument == nullptr)
+        if (!_loKitDocument)
         {
             Log::error("Tile rendering requested before loading document.");
             return;
@@ -626,7 +609,7 @@ public:
 
         //TODO: Support multiviews.
         //if (_multiView)
-            //_loKitDocument->pClass->setView(_loKitDocument, _viewId);
+            //_loKitDocument->setView(_viewId);
 
         // Send back the request with all optional parameters given in the request.
 #if ENABLE_DEBUG
@@ -644,14 +627,13 @@ public:
         pixmap.resize(4 * width * height);
 
         Timestamp timestamp;
-        _loKitDocument->pClass->paintPartTile(_loKitDocument, pixmap.data(), part,
-                                              width, height, tilePosX, tilePosY,
-                                              tileWidth, tileHeight);
+        _loKitDocument->paintPartTile(pixmap.data(), part,
+                                      width, height, tilePosX, tilePosY,
+                                      tileWidth, tileHeight);
         Log::trace() << "paintTile at [" << tilePosX << ", " << tilePosY
                      << "] rendered in " << (timestamp.elapsed()/1000.) << " ms" << Log::end;
 
-        const LibreOfficeKitTileMode mode =
-                static_cast<LibreOfficeKitTileMode>(_loKitDocument->pClass->getTileMode(_loKitDocument));
+        const auto mode = static_cast<LibreOfficeKitTileMode>(_loKitDocument->getTileMode());
         if (!Util::encodeBufferToPNG(pixmap.data(), width, height, output, mode))
         {
             //FIXME: Return error.
@@ -881,6 +863,10 @@ private:
         try
         {
             load(sessionId, uri, docPassword, renderOpts, haveDocPassword);
+            if (!_loKitDocument)
+            {
+                return nullptr;
+            }
         }
         catch (const std::exception& exc)
         {
@@ -889,12 +875,13 @@ private:
         }
 
         // Done loading, let the next one in (if any).
+        assert(_loKitDocument && "Uninitialized lok::Document instance");
         lock.lock();
         ++_clientViews;
         --_isLoading;
         _cvLoading.notify_one();
 
-        return _loKitDocument;
+        return _loKitDocument->get();
     }
 
     void onUnload(const std::string& sessionId)
@@ -922,9 +909,9 @@ private:
                         << sessionId << "] unloaded, leaving "
                         << _clientViews << " views." << Log::end;
 
-            const auto viewId = _loKitDocument->pClass->getView(_loKitDocument);
-            _loKitDocument->pClass->registerCallback(_loKitDocument, nullptr, nullptr);
-            _loKitDocument->pClass->destroyView(_loKitDocument, viewId);
+            const auto viewId = _loKitDocument->getView();
+            _loKitDocument->registerCallback(nullptr, nullptr);
+            _loKitDocument->destroyView(viewId);
         }
     }
 
@@ -946,7 +933,7 @@ private:
 
         auto session = it->second->getSession();
 
-        if (_loKitDocument == nullptr)
+        if (!_loKitDocument)
         {
             // This is the first time we are loading the document
             Log::info("Loading new document from URI: [" + uri + "] for session [" + sessionId + "].");
@@ -966,10 +953,10 @@ private:
             _isDocPasswordProtected = false;
 
             Log::debug("Calling lokit::documentLoad.");
-            _loKitDocument = _loKit->pClass->documentLoad(_loKit, uri.c_str());
+            _loKitDocument = std::make_shared<lok::Document>(_loKit->pClass->documentLoad(_loKit, uri.c_str()));
             Log::debug("Returned lokit::documentLoad.");
 
-            if (_loKitDocument == nullptr)
+            if (!_loKitDocument)
             {
                 Log::error("Failed to load: " + uri + ", error: " + _loKit->pClass->getError(_loKit));
 
@@ -1000,9 +987,9 @@ private:
             if (_multiView)
             {
                 Log::info("Loading view to document from URI: [" + uri + "] for session [" + sessionId + "].");
-                const auto viewId = _loKitDocument->pClass->createView(_loKitDocument);
+                const auto viewId = _loKitDocument->createView();
 
-                _loKitDocument->pClass->registerCallback(_loKitDocument, ViewCallback, reinterpret_cast<void*>(intSessionId));
+                _loKitDocument->registerCallback(ViewCallback, reinterpret_cast<void*>(intSessionId));
 
                 Log::info() << "Document [" << _url << "] view ["
                             << viewId << "] loaded, leaving "
@@ -1010,10 +997,10 @@ private:
             }
             else
             {
-                _loKitDocument->pClass->registerCallback(_loKitDocument, DocumentCallback, this);
+                _loKitDocument->registerCallback(DocumentCallback, this);
             }
 
-            _loKitDocument->pClass->initializeForRendering(_loKitDocument, (renderOpts.empty() ? nullptr : renderOpts.c_str()));
+            _loKitDocument->initializeForRendering((renderOpts.empty() ? nullptr : renderOpts.c_str()));
         }
         else
         {
@@ -1038,7 +1025,7 @@ private:
             }
         }
 
-        return _loKitDocument;
+        return _loKitDocument->get();
     }
 
 private:
@@ -1050,7 +1037,7 @@ private:
     const std::string _url;
     std::string _jailedUrl;
 
-    LibreOfficeKitDocument *_loKitDocument;
+    std::shared_ptr<lok::Document> _loKitDocument;
 
     // Document password provided
     std::string _docPassword;


More information about the Libreoffice-commits mailing list