summaryrefslogtreecommitdiffstats
path: root/desktop/inc
diff options
context:
space:
mode:
authorAshod Nakashian <ashod.nakashian@collabora.co.uk>2016-04-19 21:26:42 -0400
committerAshod Nakashian <ashnakash@gmail.com>2016-05-07 22:19:50 +0000
commitc327bb5c88573c96f22e9a8cfc4b8a733ae6b671 (patch)
tree6d552e8c25356a33cd398170b7685eb67b397081 /desktop/inc
parentTypo: SYSTEN_LIBGLTF->SYSTEM_LIBGLTF (diff)
downloadcore-c327bb5c88573c96f22e9a8cfc4b8a733ae6b671.tar.gz
core-c327bb5c88573c96f22e9a8cfc4b8a733ae6b671.zip
New LOKDocument callback queue to flush events lazily on idle
Since desktop now queues up callback notifications and flushes them to the client on idle, the unit-tests must yield and process all tasks before they validate post-conditions. (cherry picked from commit e6a429770bde5da75239961ae88c06c78cfa5686) (cherry picked from commit 1f278848117080cd6e819f04ba428be52416af7c) (cherry picked from commit 6ca6f22777eb3651109cbf403577d0022a735c9b) (cherry picked from commit 548faf728cf097d93c3f6478ceea5f8747e789c6) Change-Id: I78307db29a9ce647ffaed3539f953227c605968e Reviewed-on: https://gerrit.libreoffice.org/24377 Tested-by: Jenkins <ci@libreoffice.org> Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Diffstat (limited to 'desktop/inc')
-rw-r--r--desktop/inc/lib/init.hxx112
1 files changed, 107 insertions, 5 deletions
diff --git a/desktop/inc/lib/init.hxx b/desktop/inc/lib/init.hxx
index 5b386e4820d7..5c987bec57ab 100644
--- a/desktop/inc/lib/init.hxx
+++ b/desktop/inc/lib/init.hxx
@@ -10,24 +10,126 @@
#ifndef INCLUDED_DESKTOP_INC_LIB_INIT_HXX
#define INCLUDED_DESKTOP_INC_LIB_INIT_HXX
+#include <map>
+#include <memory>
+#include <mutex>
+
+#include <osl/thread.h>
+#include <vcl/idle.hxx>
#include <LibreOfficeKit/LibreOfficeKit.h>
#include <LibreOfficeKit/LibreOfficeKitEnums.h>
#include <com/sun/star/frame/XStorable.hpp>
#include <com/sun/star/lang/XComponent.hpp>
-#include <memory>
-#include <map>
+
#include <desktop/dllapi.h>
-#include <osl/thread.h>
class LOKInteractionHandler;
namespace desktop {
+
+ class CallbackFlushHandler : public Idle
+ {
+ public:
+ explicit CallbackFlushHandler(LibreOfficeKitCallback pCallback, void* pData)
+ : Idle( "lokit timer callback" ),
+ m_pCallback(pCallback),
+ m_pData(pData)
+ {
+ SetPriority(SchedulerPriority::POST_PAINT);
+
+ // Add the states that are safe to skip duplicates on,
+ // even when not consequent.
+ m_states.emplace(LOK_CALLBACK_TEXT_SELECTION, "NIL");
+ m_states.emplace(LOK_CALLBACK_INVALIDATE_VISIBLE_CURSOR, "NIL");
+ m_states.emplace(LOK_CALLBACK_STATE_CHANGED, "NIL");
+
+ Start();
+ }
+
+ virtual ~CallbackFlushHandler()
+ {
+ Stop();
+
+ // We might have important notification (.uno:save?).
+ flush();
+ }
+
+ virtual void Invoke() override
+ {
+ flush();
+ }
+
+ static
+ void callback(const int type, const char* payload, void* data)
+ {
+ CallbackFlushHandler* self = reinterpret_cast<CallbackFlushHandler*>(data);
+ if (self)
+ {
+ self->queue(type, payload);
+ }
+ }
+
+ void queue(const int type, const char* data)
+ {
+ const std::string payload(data ? data : "(nil)");
+ std::unique_lock<std::mutex> lock(m_mutex);
+
+ const auto stateIt = m_states.find(type);
+ if (stateIt != m_states.end())
+ {
+ // If the state didn't change, it's safe to ignore.
+ if (stateIt->second == payload)
+ {
+ return;
+ }
+
+ stateIt->second = payload;
+ }
+
+ if (type == LOK_CALLBACK_INVALIDATE_TILES &&
+ !m_queue.empty() && std::get<0>(m_queue.back()) == type && std::get<1>(m_queue.back()) == payload)
+ {
+ // Supress duplicate invalidation only when they are in sequence.
+ return;
+ }
+
+ m_queue.emplace_back(type, payload);
+
+ lock.unlock();
+ if (!IsActive())
+ {
+ Start();
+ }
+ }
+
+ private:
+ void flush()
+ {
+ if (m_pCallback)
+ {
+ std::unique_lock<std::mutex> lock(m_mutex);
+ for (auto& pair : m_queue)
+ {
+ m_pCallback(std::get<0>(pair), std::get<1>(pair).c_str(), m_pData);
+ }
+
+ m_queue.clear();
+ }
+ }
+
+ private:
+ std::vector<std::tuple<int, std::string>> m_queue;
+ std::map<int, std::string> m_states;
+ LibreOfficeKitCallback m_pCallback;
+ void *m_pData;
+ std::mutex m_mutex;
+ };
+
struct DESKTOP_DLLPUBLIC LibLODocument_Impl : public _LibreOfficeKitDocument
{
css::uno::Reference<css::lang::XComponent> mxComponent;
std::shared_ptr< LibreOfficeKitDocumentClass > m_pDocumentClass;
- LibreOfficeKitCallback mpCallback;
- void *mpCallbackData;
+ std::shared_ptr<CallbackFlushHandler> mpCallbackFlushHandler;
explicit LibLODocument_Impl(const css::uno::Reference <css::lang::XComponent> &xComponent);
~LibLODocument_Impl();