46e938c
From 4796ce2b93089c363639c64a8fde734df2c594a1 Mon Sep 17 00:00:00 2001
46e938c
From: =?UTF-8?q?Caol=C3=A1n=20McNamara?= <caolanm@redhat.com>
46e938c
Date: Mon, 8 May 2017 15:26:22 +0100
46e938c
Subject: [PATCH 5/5] Resolves: rhbz#144437 make gnome-documents not crash the
46e938c
 whole time
46e938c
46e938c
accept that once initted that LibreOffice cannot be deinitted and reinited
46e938c
(without lots of work), but allow the main loop to quit and restart so LOKs
46e938c
thread can run and exit successfully, new LOK connections will restart the main
46e938c
loop.
46e938c
46e938c
The buckets of global state continues to be valid the whole time this way
46e938c
46e938c
Change-Id: Ide54c0df2ce4065f7c192ae8c2cedfaaa2b58d72
46e938c
---
46e938c
 desktop/source/app/app.cxx               |  2 +-
46e938c
 desktop/source/app/officeipcthread.cxx   |  9 +++++++--
46e938c
 desktop/source/app/officeipcthread.hxx   |  2 +-
46e938c
 desktop/source/lib/init.cxx              |  5 ++++-
46e938c
 framework/source/services/desktop.cxx    |  8 +++++---
46e938c
 libreofficekit/source/gtk/lokdocview.cxx | 34 +++++++++++++++++++++-----------
46e938c
 vcl/source/app/svmain.cxx                |  8 ++++++++
46e938c
 7 files changed, 49 insertions(+), 19 deletions(-)
46e938c
46e938c
diff --git a/desktop/source/app/app.cxx b/desktop/source/app/app.cxx
46e938c
index 8793697..9786afe 100644
46e938c
--- a/desktop/source/app/app.cxx
46e938c
+++ b/desktop/source/app/app.cxx
46e938c
@@ -1994,7 +1994,7 @@ IMPL_LINK_NOARG(Desktop, OpenClients_Impl, void*, void)
46e938c
         // When this server closes down it attempts to recreate the pipe (in RequestHandler::Disable()).
46e938c
         // It's possible that the client has a pending connection request.
46e938c
         // When the IPC thread is not running, this connection locks (because maPipe.accept()) is never called
46e938c
-        RequestHandler::SetReady();
46e938c
+        RequestHandler::SetReady(true);
46e938c
         OpenClients();
46e938c
 
46e938c
         CloseSplashScreen();
46e938c
diff --git a/desktop/source/app/officeipcthread.cxx b/desktop/source/app/officeipcthread.cxx
46e938c
index 7a2fd46..5ea5f41 100644
46e938c
--- a/desktop/source/app/officeipcthread.cxx
46e938c
+++ b/desktop/source/app/officeipcthread.cxx
46e938c
@@ -939,6 +939,8 @@ void RequestHandler::Disable()
46e938c
             handler->mIpcThread->join();
46e938c
             handler->mIpcThread.clear();
46e938c
         }
46e938c
+
46e938c
+        handler->cReady.reset();
46e938c
     }
46e938c
 }
46e938c
 
46e938c
@@ -953,12 +955,15 @@ RequestHandler::~RequestHandler()
46e938c
     assert(!mIpcThread.is());
46e938c
 }
46e938c
 
46e938c
-void RequestHandler::SetReady()
46e938c
+void RequestHandler::SetReady(bool bIsReady)
46e938c
 {
46e938c
     osl::MutexGuard g(GetMutex());
46e938c
     if (pGlobal.is())
46e938c
     {
46e938c
-        pGlobal->cReady.set();
46e938c
+        if (bIsReady)
46e938c
+            pGlobal->cReady.set();
46e938c
+        else
46e938c
+            pGlobal->cReady.reset();
46e938c
     }
46e938c
 }
46e938c
 
46e938c
diff --git a/desktop/source/app/officeipcthread.hxx b/desktop/source/app/officeipcthread.hxx
46e938c
index 2826aba..2b58ce4 100644
46e938c
--- a/desktop/source/app/officeipcthread.hxx
46e938c
+++ b/desktop/source/app/officeipcthread.hxx
46e938c
@@ -122,7 +122,7 @@ class RequestHandler: public salhelper::SimpleReferenceObject
46e938c
     static Status               Enable(bool ipc);
46e938c
     static void                 Disable();
46e938c
     // start dispatching events...
46e938c
-    static void                 SetReady();
46e938c
+    static void                 SetReady(bool bIsReady);
46e938c
     static void                 WaitForReady();
46e938c
 
46e938c
     bool                        AreRequestsEnabled() const { return mState == State::RequestsEnabled; }
46e938c
diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
46e938c
index 9df6471..14c7bfd 100644
46e938c
--- a/desktop/source/lib/init.cxx
46e938c
+++ b/desktop/source/lib/init.cxx
46e938c
@@ -2742,10 +2742,12 @@ static void lo_startmain(void*)
46e938c
 {
46e938c
     osl_setThreadName("lo_startmain");
46e938c
 
46e938c
-    if (GetpApp())
46e938c
+    if (comphelper::SolarMutex::get())
46e938c
         Application::GetSolarMutex().tryToAcquire();
46e938c
 
46e938c
     soffice_main();
46e938c
+
46e938c
+    Application::ReleaseSolarMutex();
46e938c
 }
46e938c
 
46e938c
 static bool bInitialized = false;
46e938c
@@ -2912,6 +2914,7 @@ static int lo_initialize(LibreOfficeKit* pThis, const char* pAppPath, const char
46e938c
             SAL_INFO("lok", "Enabling RequestHandler");
46e938c
             RequestHandler::Enable(false);
46e938c
             SAL_INFO("lok", "Starting soffice_main");
46e938c
+            RequestHandler::SetReady(false);
46e938c
             pLib->maThread = osl_createThread(lo_startmain, nullptr);
46e938c
             SAL_INFO("lok", "Waiting for RequestHandler");
46e938c
             RequestHandler::WaitForReady();
46e938c
diff --git a/framework/source/services/desktop.cxx b/framework/source/services/desktop.cxx
46e938c
index 20afab1..6f6412a 100644
46e938c
--- a/framework/source/services/desktop.cxx
46e938c
+++ b/framework/source/services/desktop.cxx
46e938c
@@ -59,6 +59,7 @@
46e938c
 #include <com/sun/star/frame/XTerminateListener2.hpp>
46e938c
 
46e938c
 #include <comphelper/sequence.hxx>
46e938c
+#include <comphelper/lok.hxx>
46e938c
 #include <cppuhelper/supportsservice.hxx>
46e938c
 #include <rtl/instance.hxx>
46e938c
 #include <vcl/svapp.hxx>
46e938c
@@ -228,8 +229,9 @@ sal_Bool SAL_CALL Desktop::terminate()
46e938c
 
46e938c
     // try to close all open frames.
46e938c
     // Allow using of any UI ... because Desktop.terminate() was designed as UI functionality in the past.
46e938c
-    bool bIsEventTestingMode = Application::IsEventTestingModeEnabled();
46e938c
-    bool bFramesClosed = impl_closeFrames(!bIsEventTestingMode);
46e938c
+    bool bRestartableMainLoop = Application::IsEventTestingModeEnabled() ||
46e938c
+                                comphelper::LibreOfficeKit::isActive();
46e938c
+    bool bFramesClosed = impl_closeFrames(!bRestartableMainLoop);
46e938c
 
46e938c
     // Ask normal terminate listener. They could stop terminating the process.
46e938c
     Desktop::TTerminateListenerList lCalledTerminationListener;
46e938c
@@ -241,7 +243,7 @@ sal_Bool SAL_CALL Desktop::terminate()
46e938c
         return false;
46e938c
     }
46e938c
 
46e938c
-    if (bIsEventTestingMode)
46e938c
+    if (bRestartableMainLoop)
46e938c
     {
46e938c
         Application::Quit();
46e938c
         return true;
46e938c
diff --git a/libreofficekit/source/gtk/lokdocview.cxx b/libreofficekit/source/gtk/lokdocview.cxx
46e938c
index 18985d5..04bd143 100644
46e938c
--- a/libreofficekit/source/gtk/lokdocview.cxx
46e938c
+++ b/libreofficekit/source/gtk/lokdocview.cxx
46e938c
@@ -2587,20 +2587,32 @@ static void lok_doc_view_destroy (GtkWidget* widget)
46e938c
     std::stringstream ss;
46e938c
     ss << "lok::Document::setView(" << priv->m_nViewId << ")";
46e938c
     g_info("%s", ss.str().c_str());
46e938c
-    priv->m_pDocument->pClass->setView(priv->m_pDocument, priv->m_nViewId);
46e938c
-    priv->m_pDocument->pClass->registerCallback(priv->m_pDocument, nullptr, nullptr);
46e938c
-    aGuard.unlock();
46e938c
-
46e938c
-    if (priv->m_pDocument && priv->m_pDocument->pClass->getViewsCount(priv->m_pDocument) > 1)
46e938c
+    if (priv->m_pDocument)
46e938c
     {
46e938c
-        priv->m_pDocument->pClass->destroyView(priv->m_pDocument, priv->m_nViewId);
46e938c
+        priv->m_pDocument->pClass->setView(priv->m_pDocument, priv->m_nViewId);
46e938c
+        priv->m_pDocument->pClass->registerCallback(priv->m_pDocument, nullptr, nullptr);
46e938c
     }
46e938c
-    else
46e938c
+    aGuard.unlock();
46e938c
+
46e938c
+    if (priv->m_pDocument)
46e938c
     {
46e938c
-        if (priv->m_pDocument)
46e938c
-            priv->m_pDocument->pClass->destroy (priv->m_pDocument);
46e938c
-        if (priv->m_pOffice)
46e938c
-            priv->m_pOffice->pClass->destroy (priv->m_pOffice);
46e938c
+        if (priv->m_pDocument->pClass->getViewsCount(priv->m_pDocument) > 1)
46e938c
+        {
46e938c
+            priv->m_pDocument->pClass->destroyView(priv->m_pDocument, priv->m_nViewId);
46e938c
+        }
46e938c
+        else
46e938c
+        {
46e938c
+            if (priv->m_pDocument)
46e938c
+            {
46e938c
+                priv->m_pDocument->pClass->destroy (priv->m_pDocument);
46e938c
+                priv->m_pDocument = nullptr;
46e938c
+            }
46e938c
+            if (priv->m_pOffice)
46e938c
+            {
46e938c
+                priv->m_pOffice->pClass->destroy (priv->m_pOffice);
46e938c
+                priv->m_pOffice = nullptr;
46e938c
+            }
46e938c
+        }
46e938c
     }
46e938c
 
46e938c
     GTK_WIDGET_CLASS (lok_doc_view_parent_class)->destroy (widget);
46e938c
diff --git a/vcl/source/app/svmain.cxx b/vcl/source/app/svmain.cxx
46e938c
index 2cd6137..a5f7807 100644
46e938c
--- a/vcl/source/app/svmain.cxx
46e938c
+++ b/vcl/source/app/svmain.cxx
46e938c
@@ -84,6 +84,7 @@
46e938c
 #include <com/sun/star/lang/XComponent.hpp>
46e938c
 #include <com/sun/star/frame/Desktop.hpp>
46e938c
 
46e938c
+#include <comphelper/lok.hxx>
46e938c
 #include <cppuhelper/implbase.hxx>
46e938c
 #include <uno/current_context.hxx>
46e938c
 
46e938c
@@ -361,6 +362,13 @@ VCLUnoWrapperDeleter::disposing(lang::EventObject const& /* rSource */)
46e938c
 
46e938c
 void DeInitVCL()
46e938c
 {
46e938c
+    //rhbz#1444437, when using LibreOffice like a library you can't realistically
46e938c
+    //tear everything down and recreate them on the next call, there's too many
46e938c
+    //(c++) singletons that point to stuff that gets deleted during shutdown
46e938c
+    //which won't be recreated on restart.
46e938c
+    if (comphelper::LibreOfficeKit::isActive())
46e938c
+        return;
46e938c
+
46e938c
     {
46e938c
         SolarMutexReleaser r; // unblock threads blocked on that so we can join
46e938c
         ::comphelper::JoinAsyncEventNotifiers();
46e938c
-- 
46e938c
2.9.3
46e938c