comphelper/source/misc/lok.cxx | 64 +++++++++++++++------------- desktop/qa/desktop_lib/test_desktop_lib.cxx | 8 +-- desktop/source/lib/init.cxx | 40 +++++------------ include/LibreOfficeKit/LibreOfficeKit.h | 19 +++----- include/LibreOfficeKit/LibreOfficeKit.hxx | 18 +------ include/comphelper/lok.hxx | 8 +-- include/sfx2/lokhelper.hxx | 6 -- sfx2/source/control/unoctitm.cxx | 42 +++++++----------- sfx2/source/view/lokhelper.cxx | 16 +++---- 9 files changed, 92 insertions(+), 129 deletions(-)
New commits: commit 6c564ea5eb8b43e8c5ee2bcbc20acdabfb5b7f75 Author: Pranam Lashkari <[email protected]> AuthorDate: Wed Sep 15 23:39:32 2021 +0530 Commit: Tor Lillqvist <[email protected]> CommitDate: Mon Jan 10 14:27:49 2022 +0200 LOK: unify freemium APIs and uno command restriction APIs Signed-off-by: Pranam Lashkari <[email protected]> Change-Id: I3badb038822331ab5cb30df6a66ce9a0640cf340 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122162 Tested-by: Tor Lillqvist <[email protected]> Reviewed-by: Tor Lillqvist <[email protected]> diff --git a/comphelper/source/misc/lok.cxx b/comphelper/source/misc/lok.cxx index 957697302e89..28e66723f298 100644 --- a/comphelper/source/misc/lok.cxx +++ b/comphelper/source/misc/lok.cxx @@ -39,9 +39,9 @@ static bool g_bLocalRendering(false); static Compat g_eCompatFlags(Compat::none); -static std::vector<OUString> g_vFreemiumDenyList; +static std::unordered_set<OUString> g_vFreemiumDenyList; -static std::vector<OUString> g_vRestrictedCommandList; +static std::unordered_set<OUString> g_vRestrictedCommandList; namespace { @@ -293,54 +293,60 @@ void statusIndicatorFinish() pStatusIndicatorCallback(pStatusIndicatorCallbackData, statusIndicatorCallbackType::Finish, 0, nullptr); } -void setFreemiumDenyList(const char* freemiumDenyList) +void setBlockedCommandList(const char* bolckedCommandList) { - if(!g_vFreemiumDenyList.empty()) - return; - OUString DenyListString(freemiumDenyList, strlen(freemiumDenyList), RTL_TEXTENCODING_UTF8); + OUString BolckedListString(bolckedCommandList, strlen(bolckedCommandList), RTL_TEXTENCODING_UTF8); - OUString command = DenyListString.getToken(0, ' '); - for (size_t i = 1; !command.isEmpty(); i++) + OUString type = BolckedListString.getToken(0, '-'); + + if (type == "freemium") { - g_vFreemiumDenyList.emplace_back(command); - command = DenyListString.getToken(i, ' '); + if(!g_vFreemiumDenyList.empty()) + return; + OUString commands = BolckedListString.getToken(1, '-'); + + OUString command = commands.getToken(0, ' '); + for (size_t i = 1; !command.isEmpty(); i++) + { + g_vFreemiumDenyList.emplace(command); + command = commands.getToken(i, ' '); + } + } + else + { + if(!g_vRestrictedCommandList.empty()) + return; + + OUString commands = BolckedListString.getToken(1, '-'); + + OUString command = commands.getToken(0, ' '); + for (size_t i = 1; !command.isEmpty(); i++) + { + g_vRestrictedCommandList.emplace(command); + command = commands.getToken(i, ' '); + } } } -const std::vector<OUString>& getFreemiumDenyList() +const std::unordered_set<OUString>& getFreemiumDenyList() { return g_vFreemiumDenyList; } bool isCommandFreemiumDenied(const OUString& command) { - return std::find(g_vFreemiumDenyList.begin(), g_vFreemiumDenyList.end(), command) != g_vFreemiumDenyList.end(); -} - -void setRestrictedCommandList(const char* restrictedCommandList) -{ - if(!g_vRestrictedCommandList.empty()) - return; - - OUString RestrictedListString(restrictedCommandList, strlen(restrictedCommandList), RTL_TEXTENCODING_UTF8); - - OUString command = RestrictedListString.getToken(0, ' '); - for (size_t i = 1; !command.isEmpty(); i++) - { - g_vRestrictedCommandList.emplace_back(command); - command = RestrictedListString.getToken(i, ' '); - } + return g_vFreemiumDenyList.find(command) != g_vFreemiumDenyList.end(); } -const std::vector<OUString>& getRestrictedCommandList() +const std::unordered_set<OUString>& getRestrictedCommandList() { return g_vRestrictedCommandList; } bool isRestrictedCommand(const OUString& command) { - return std::find(g_vRestrictedCommandList.begin(), g_vRestrictedCommandList.end(), command) != g_vRestrictedCommandList.end(); + return g_vRestrictedCommandList.find(command) != g_vRestrictedCommandList.end(); } } // namespace LibreOfficeKit diff --git a/desktop/qa/desktop_lib/test_desktop_lib.cxx b/desktop/qa/desktop_lib/test_desktop_lib.cxx index 66ff39edd7b3..f5b0916197a8 100644 --- a/desktop/qa/desktop_lib/test_desktop_lib.cxx +++ b/desktop/qa/desktop_lib/test_desktop_lib.cxx @@ -3307,14 +3307,12 @@ void DesktopLOKTest::testABI() CPPUNIT_ASSERT_EQUAL(documentClassOffset(59), offsetof(struct _LibreOfficeKitDocumentClass, completeFunction)); CPPUNIT_ASSERT_EQUAL(documentClassOffset(60), offsetof(struct _LibreOfficeKitDocumentClass, setWindowTextSelection)); CPPUNIT_ASSERT_EQUAL(documentClassOffset(61), offsetof(struct _LibreOfficeKitDocumentClass, sendFormFieldEvent)); - CPPUNIT_ASSERT_EQUAL(documentClassOffset(62), offsetof(struct _LibreOfficeKitDocumentClass, setFreemiumDenyList)); - CPPUNIT_ASSERT_EQUAL(documentClassOffset(63), offsetof(struct _LibreOfficeKitDocumentClass, setFreemiumView)); - CPPUNIT_ASSERT_EQUAL(documentClassOffset(64), offsetof(struct _LibreOfficeKitDocumentClass, setRestrictedCommandList)); - CPPUNIT_ASSERT_EQUAL(documentClassOffset(65), offsetof(struct _LibreOfficeKitDocumentClass, setRestrictedView)); + CPPUNIT_ASSERT_EQUAL(documentClassOffset(62), offsetof(struct _LibreOfficeKitDocumentClass, setBlockedCommandList)); + CPPUNIT_ASSERT_EQUAL(documentClassOffset(63), offsetof(struct _LibreOfficeKitDocumentClass, setBlockedCommandView)); // Extending is fine, update this, and add new assert for the offsetof the // new method - CPPUNIT_ASSERT_EQUAL(documentClassOffset(66), sizeof(struct _LibreOfficeKitDocumentClass)); + CPPUNIT_ASSERT_EQUAL(documentClassOffset(64), sizeof(struct _LibreOfficeKitDocumentClass)); } CPPUNIT_TEST_SUITE_REGISTRATION(DesktopLOKTest); diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx index 30ee6bb0f607..2a5831ec0b79 100644 --- a/desktop/source/lib/init.cxx +++ b/desktop/source/lib/init.cxx @@ -1084,16 +1084,13 @@ static void doc_postKeyEvent(LibreOfficeKitDocument* pThis, int nType, int nCharCode, int nKeyCode); -static void doc_setFreemiumDenyList(const char* freemiumDenyList); +static void doc_setBlockedCommandList(LibreOfficeKitDocument* pThis, + const char* bolckedCommandList); -static void doc_setFreemiumView(int nViewId, bool isFreemium); - -static void doc_setRestrictedCommandList(LibreOfficeKitDocument* pThis, - const char* restrictedCommandList); - -static void doc_setRestrictedView(LibreOfficeKitDocument* pThis, +static void doc_setBlockedCommandView(LibreOfficeKitDocument* pThis, int nViewId, - bool isRestricted); + const char* type, + bool isBlocked); static void doc_postWindowExtTextInputEvent(LibreOfficeKitDocument* pThis, unsigned nWindowId, @@ -1378,11 +1375,8 @@ LibLODocument_Impl::LibLODocument_Impl(const uno::Reference <css::lang::XCompone m_pDocumentClass->sendFormFieldEvent = doc_sendFormFieldEvent; - m_pDocumentClass->setFreemiumDenyList = doc_setFreemiumDenyList; - m_pDocumentClass->setFreemiumView = doc_setFreemiumView; - - m_pDocumentClass->setRestrictedCommandList = doc_setRestrictedCommandList; - m_pDocumentClass->setRestrictedView = doc_setRestrictedView; + m_pDocumentClass->setBlockedCommandList = doc_setBlockedCommandList; + m_pDocumentClass->setBlockedCommandView = doc_setBlockedCommandView; gDocumentClass = m_pDocumentClass; } @@ -3683,26 +3677,16 @@ static void doc_postKeyEvent(LibreOfficeKitDocument* pThis, int nType, int nChar } } -static void doc_setFreemiumDenyList(const char* freemiumDenyList) -{ - comphelper::LibreOfficeKit::setFreemiumDenyList(freemiumDenyList); -} - -static void doc_setFreemiumView(int nViewId, bool isFreemium) -{ - SolarMutexGuard aGuard; - SfxLokHelper::setFreemiumView(nViewId, isFreemium); -} - -static void doc_setRestrictedCommandList(LibreOfficeKitDocument* /*pThis*/, const char* restrictedCommandList) +static void doc_setBlockedCommandList(LibreOfficeKitDocument* /*pThis*/, const char* bolckedCommandList) { - comphelper::LibreOfficeKit::setRestrictedCommandList(restrictedCommandList); + comphelper::LibreOfficeKit::setBlockedCommandList(bolckedCommandList); } -static void doc_setRestrictedView(LibreOfficeKitDocument* /*pThis*/, int nViewId, bool isRestricted) +static void doc_setBlockedCommandView(LibreOfficeKitDocument* /*pThis*/, int nViewId, const char* type, bool isBlocked) { SolarMutexGuard aGuard; - SfxLokHelper::setRestrictedView(nViewId, isRestricted); + OUString aType(type, strlen(type), RTL_TEXTENCODING_UTF8); + SfxLokHelper::setBlockedCommandView(nViewId, aType, isBlocked); } static void doc_postWindowExtTextInputEvent(LibreOfficeKitDocument* pThis, unsigned nWindowId, int nType, const char* pText) diff --git a/include/LibreOfficeKit/LibreOfficeKit.h b/include/LibreOfficeKit/LibreOfficeKit.h index a365ffe05876..f9b9fc8148ba 100644 --- a/include/LibreOfficeKit/LibreOfficeKit.h +++ b/include/LibreOfficeKit/LibreOfficeKit.h @@ -458,20 +458,15 @@ struct _LibreOfficeKitDocumentClass void (*sendFormFieldEvent) (LibreOfficeKitDocument* pThis, const char* pArguments); - /// @see lok::Document::setFreemiumDenyList - void (*setFreemiumDenyList) (const char* freemiumDenyList); + /// @see lok::Document::setBlockedCommandList + void (*setBlockedCommandList) (LibreOfficeKitDocument* pThis, + const char* bolckedCommandList); - /// @see lok::Document::setFreemiumView - void (*setFreemiumView) (int nViewId, bool isFreemium); - - /// @see lok::Document::setRestrictedCommandList - void (*setRestrictedCommandList) (LibreOfficeKitDocument* pThis, - const char* restrictedCommandList); - - /// @see lok::Document::setRestrictedView - void (*setRestrictedView) (LibreOfficeKitDocument* pThis, + /// @see lok::Document::setBlockedCommandView + void (*setBlockedCommandView) (LibreOfficeKitDocument* pThis, int nViewId, - bool isRestricted); + const char* type, + bool isBlocked); #endif // defined LOK_USE_UNSTABLE_API || defined LIBO_INTERNAL_ONLY }; diff --git a/include/LibreOfficeKit/LibreOfficeKit.hxx b/include/LibreOfficeKit/LibreOfficeKit.hxx index b352d5f78055..c7011dd2d06e 100644 --- a/include/LibreOfficeKit/LibreOfficeKit.hxx +++ b/include/LibreOfficeKit/LibreOfficeKit.hxx @@ -790,24 +790,14 @@ public: mpDoc->pClass->sendFormFieldEvent(mpDoc, pArguments); } - void setFreemiumDenyList(const char* freemiumDenyList) + void setBlockedCommandList(const char* bolckedCommandList) { - mpDoc->pClass->setFreemiumDenyList(freemiumDenyList); + mpDoc->pClass->setBlockedCommandList(mpDoc, bolckedCommandList); } - void setFreemiumView(int nViewId, bool isFreemium) + void setBlockedCommandView(int nViewId, const char* type, bool isBlocked) { - mpDoc->pClass->setFreemiumView(nViewId, isFreemium); - } - - void setRestrictedCommandList(const char* restrictedCommandList) - { - mpDoc->pClass->setRestrictedCommandList(mpDoc, restrictedCommandList); - } - - void setRestrictedView(int nViewId, bool isRestricted) - { - mpDoc->pClass->setRestrictedView(mpDoc, nViewId, isRestricted); + mpDoc->pClass->setBlockedCommandView(mpDoc, nViewId, type, isBlocked); } #endif // defined LOK_USE_UNSTABLE_API || defined LIBO_INTERNAL_ONLY diff --git a/include/comphelper/lok.hxx b/include/comphelper/lok.hxx index 7a7496b68475..e48126015ff5 100644 --- a/include/comphelper/lok.hxx +++ b/include/comphelper/lok.hxx @@ -13,6 +13,7 @@ #include <comphelper/comphelperdllapi.h> #include <rtl/ustring.hxx> #include <vector> +#include <unordered_set> class LanguageTag; @@ -110,12 +111,11 @@ COMPHELPER_DLLPUBLIC void statusIndicatorStart(const OUString& sText); COMPHELPER_DLLPUBLIC void statusIndicatorSetValue(int percent); COMPHELPER_DLLPUBLIC void statusIndicatorFinish(); -COMPHELPER_DLLPUBLIC void setFreemiumDenyList(const char* freemiumDenyList); -COMPHELPER_DLLPUBLIC const std::vector<OUString>& getFreemiumDenyList(); +COMPHELPER_DLLPUBLIC void setBlockedCommandList(const char* bolckedCommandList); +COMPHELPER_DLLPUBLIC const std::unordered_set<OUString>& getFreemiumDenyList(); COMPHELPER_DLLPUBLIC bool isCommandFreemiumDenied(const OUString& command); -COMPHELPER_DLLPUBLIC void setRestrictedCommandList(const char* restrictedCommandList); -COMPHELPER_DLLPUBLIC const std::vector<OUString>& getRestrictedCommandList(); +COMPHELPER_DLLPUBLIC const std::unordered_set<OUString>& getRestrictedCommandList(); COMPHELPER_DLLPUBLIC bool isRestrictedCommand(const OUString& command); } } diff --git a/include/sfx2/lokhelper.hxx b/include/sfx2/lokhelper.hxx index e42093840886..6281bfbdd8be 100644 --- a/include/sfx2/lokhelper.hxx +++ b/include/sfx2/lokhelper.hxx @@ -62,10 +62,8 @@ public: static std::size_t getViewsCount(int nDocId); /// Get viewIds of views of the current DocId. static bool getViewIds(int nDocId, int* pArray, size_t nSize); - /// Set View Freemium - static void setFreemiumView(int nViewId, bool isFreemium); - /// Set View Restricted - static void setRestrictedView(int nViewId, bool isRestricted); + /// Set View Blocked for some uno commands + static void setBlockedCommandView(int nViewId, const OUString& type, bool isBlocked); /// Get the document id for a view static int getDocumentIdOfView(int nViewId); /// Get the default language that should be used for views diff --git a/sfx2/source/control/unoctitm.cxx b/sfx2/source/control/unoctitm.cxx index cf497b27348a..49949db6f317 100644 --- a/sfx2/source/control/unoctitm.cxx +++ b/sfx2/source/control/unoctitm.cxx @@ -50,6 +50,7 @@ #include <vcl/svapp.hxx> #include <vcl/uitest/logger.hxx> #include <boost/property_tree/json_parser.hpp> +#include <tools/json_writer.hxx> #include <sfx2/app.hxx> #include <sfx2/unoctitm.hxx> @@ -608,6 +609,17 @@ void collectUIInformation(const util::URL& rURL, const css::uno::Sequence< css:: } } +void lcl_BlockCommand(const rtl::OUString& command, const rtl::OUString& kind) +{ + tools::JsonWriter aTree; + aTree.put("code", ""); + aTree.put("kind", kind); + aTree.put("cmd", command); + aTree.put("message", "Blocked feature"); + aTree.put("viewID", SfxViewShell::Current()->GetViewShellId().get()); + + SfxViewShell::Current()->libreOfficeKitViewCallback(LOK_CALLBACK_ERROR, aTree.extractData()); +} void SfxDispatchController_Impl::dispatch( const css::util::URL& aURL, const css::uno::Sequence< css::beans::PropertyValue >& aArgs, @@ -619,36 +631,18 @@ void SfxDispatchController_Impl::dispatch( const css::util::URL& aURL, SolarMutexGuard aGuard; if (comphelper::LibreOfficeKit::isActive() && - SfxViewShell::Current()->isFreemiumView() && - comphelper::LibreOfficeKit::isCommandFreemiumDenied(aURL.Complete)) + SfxViewShell::Current()->isRestrictedView() && + comphelper::LibreOfficeKit::isRestrictedCommand(aURL.Complete)) { - boost::property_tree::ptree aTree; - aTree.put("code", ""); - aTree.put("kind", "freemiumdeny"); - aTree.put("cmd", aURL.Complete); - aTree.put("message", "Blocked Freemium feature"); - aTree.put("viewID", SfxViewShell::Current()->GetViewShellId().get()); - - std::stringstream aStream; - boost::property_tree::write_json(aStream, aTree); - SfxViewShell::Current()->libreOfficeKitViewCallback(LOK_CALLBACK_ERROR, aStream.str().c_str()); + lcl_BlockCommand(aURL.Complete, "restricted"); return; } if (comphelper::LibreOfficeKit::isActive() && - SfxViewShell::Current()->isRestrictedView() && - comphelper::LibreOfficeKit::isRestrictedCommand(aURL.Complete)) + SfxViewShell::Current()->isFreemiumView() && + comphelper::LibreOfficeKit::isCommandFreemiumDenied(aURL.Complete)) { - boost::property_tree::ptree aTree; - aTree.put("code", ""); - aTree.put("kind", "restricted"); - aTree.put("cmd", aURL.Complete); - aTree.put("message", "Blocked restricted feature"); - aTree.put("viewID", SfxViewShell::Current()->GetViewShellId().get()); - - std::stringstream aStream; - boost::property_tree::write_json(aStream, aTree); - SfxViewShell::Current()->libreOfficeKitViewCallback(LOK_CALLBACK_ERROR, aStream.str().c_str()); + lcl_BlockCommand(aURL.Complete, "freemiumdeny"); return; } diff --git a/sfx2/source/view/lokhelper.cxx b/sfx2/source/view/lokhelper.cxx index e36cc0d85a28..b478a73c58ad 100644 --- a/sfx2/source/view/lokhelper.cxx +++ b/sfx2/source/view/lokhelper.cxx @@ -739,21 +739,19 @@ void SfxLokHelper::postKeyEventAsync(const VclPtr<vcl::Window> &xWindow, postEventAsync(pLOKEv); } -void SfxLokHelper::setFreemiumView(int nViewId, bool isFreemium) +void SfxLokHelper::setBlockedCommandView(int nViewId, const OUString& type, bool isBlocked) { SfxViewShell* pViewShell = SfxLokHelper::getViewOfId(nViewId); if(pViewShell) - pViewShell->setFreemiumView(isFreemium); + { + if(type == "freemium") + pViewShell->setFreemiumView(isBlocked); + else + pViewShell->setRestrictedView(isBlocked); + } } -void SfxLokHelper::setRestrictedView(int nViewId, bool isRestricted) -{ - SfxViewShell* pViewShell = SfxLokHelper::getViewOfId(nViewId); - - if(pViewShell) - pViewShell->setRestrictedView(isRestricted); -} void SfxLokHelper::postExtTextEventAsync(const VclPtr<vcl::Window> &xWindow, int nType, const OUString &rText) {
