comphelper/source/misc/lok.cxx              |   27 +++++++++++++++++++++++++++
 desktop/qa/desktop_lib/test_desktop_lib.cxx |   10 ++++++----
 desktop/source/lib/init.cxx                 |   21 +++++++++++++++++++++
 include/LibreOfficeKit/LibreOfficeKit.h     |   20 +++++++++++++++-----
 include/LibreOfficeKit/LibreOfficeKit.hxx   |   27 ++++++++++++++++++---------
 include/comphelper/lok.hxx                  |    4 ++++
 include/sfx2/lokhelper.hxx                  |    2 ++
 include/sfx2/viewsh.hxx                     |    5 +++++
 sfx2/source/control/unoctitm.cxx            |   17 +++++++++++++++++
 sfx2/source/view/lokhelper.cxx              |    7 +++++++
 sfx2/source/view/viewsh.cxx                 |    1 +
 11 files changed, 123 insertions(+), 18 deletions(-)

New commits:
commit 8de04f459611df936c0b315b6d48cb3dbcc1d5fa
Author:     Pranam Lashkari <[email protected]>
AuthorDate: Mon Sep 13 23:05:40 2021 +0530
Commit:     Miklos Vajna <[email protected]>
CommitDate: Fri Oct 22 12:49:56 2021 +0200

    LOK: introduce way to restrict uno commands
    
    With this new API we can define which uno commands to restrict their 
functionality
    
    Conflicts:
            desktop/qa/desktop_lib/test_desktop_lib.cxx
            include/LibreOfficeKit/LibreOfficeKit.h
            include/LibreOfficeKit/LibreOfficeKit.hxx
            include/sfx2/viewsh.hxx
    
    Change-Id: I9f3fd659d373e56542c5323922a53564f1cfb27b
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/124046
    Tested-by: Jenkins
    Reviewed-by: Miklos Vajna <[email protected]>

diff --git a/comphelper/source/misc/lok.cxx b/comphelper/source/misc/lok.cxx
index c7c57cb61e27..c4ab97ed2f06 100644
--- a/comphelper/source/misc/lok.cxx
+++ b/comphelper/source/misc/lok.cxx
@@ -37,6 +37,8 @@ static Compat g_eCompatFlags(Compat::none);
 
 static std::vector<OUString> g_vFreemiumDenyList;
 
+static std::vector<OUString> g_vRestrictedCommandList;
+
 namespace
 {
 
@@ -310,6 +312,31 @@ 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, ' ');
+    }
+}
+
+const std::vector<OUString>& getRestrictedCommandList()
+{
+    return g_vRestrictedCommandList;
+}
+
+bool isRestrictedCommand(const OUString& command)
+{
+    return std::find(g_vRestrictedCommandList.begin(), 
g_vRestrictedCommandList.end(), command) != g_vRestrictedCommandList.end();
+}
+
 } // namespace
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/desktop/qa/desktop_lib/test_desktop_lib.cxx 
b/desktop/qa/desktop_lib/test_desktop_lib.cxx
index d8ae56644263..19aa24684cb8 100644
--- a/desktop/qa/desktop_lib/test_desktop_lib.cxx
+++ b/desktop/qa/desktop_lib/test_desktop_lib.cxx
@@ -3458,13 +3458,15 @@ 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, renderSearchResult));
-    CPPUNIT_ASSERT_EQUAL(documentClassOffset(63), offsetof(struct 
_LibreOfficeKitDocumentClass, setFreemiumDenyList));
-    CPPUNIT_ASSERT_EQUAL(documentClassOffset(64), offsetof(struct 
_LibreOfficeKitDocumentClass, setFreemiumView));
+    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(66), offsetof(struct 
_LibreOfficeKitDocumentClass, renderSearchResult));
 
     // Extending is fine, update this, and add new assert for the offsetof the
     // new method
-    CPPUNIT_ASSERT_EQUAL(documentClassOffset(65), sizeof(struct 
_LibreOfficeKitDocumentClass));
+    CPPUNIT_ASSERT_EQUAL(documentClassOffset(67), sizeof(struct 
_LibreOfficeKitDocumentClass));
 }
 
 CPPUNIT_TEST_SUITE_REGISTRATION(DesktopLOKTest);
diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index 535cf9808d4d..3720ed54fbbf 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -1077,6 +1077,13 @@ static void doc_setFreemiumView(LibreOfficeKitDocument* 
pThis,
                                 int nViewId,
                                 bool isFreemium);
 
+static void doc_setRestrictedCommandList(LibreOfficeKitDocument* pThis,
+                                    const char* restrictedCommandList);
+
+static void doc_setRestrictedView(LibreOfficeKitDocument* pThis,
+                                int nViewId,
+                                bool isRestricted);
+
 static void doc_postWindowExtTextInputEvent(LibreOfficeKitDocument* pThis,
                                             unsigned nWindowId,
                                             int nType,
@@ -1370,6 +1377,9 @@ LibLODocument_Impl::LibLODocument_Impl(const 
uno::Reference <css::lang::XCompone
         m_pDocumentClass->setFreemiumDenyList = doc_setFreemiumDenyList;
         m_pDocumentClass->setFreemiumView = doc_setFreemiumView;
 
+        m_pDocumentClass->setRestrictedCommandList = 
doc_setRestrictedCommandList;
+        m_pDocumentClass->setRestrictedView = doc_setRestrictedView;
+
         gDocumentClass = m_pDocumentClass;
     }
     pClass = m_pDocumentClass.get();
@@ -3658,6 +3668,17 @@ static void doc_setFreemiumView(LibreOfficeKitDocument* 
/*pThis*/, int nViewId,
     SfxLokHelper::setFreemiumView(nViewId, isFreemium);
 }
 
+static void doc_setRestrictedCommandList(LibreOfficeKitDocument* /*pThis*/, 
const char* restrictedCommandList)
+{
+    
comphelper::LibreOfficeKit::setRestrictedCommandList(restrictedCommandList);
+}
+
+static void doc_setRestrictedView(LibreOfficeKitDocument* /*pThis*/, int 
nViewId, bool isRestricted)
+{
+    SolarMutexGuard aGuard;
+    SfxLokHelper::setRestrictedView(nViewId, isRestricted);
+}
+
 static void doc_postWindowExtTextInputEvent(LibreOfficeKitDocument* pThis, 
unsigned nWindowId, int nType, const char* pText)
 {
     comphelper::ProfileZone aZone("doc_postWindowExtTextInputEvent");
diff --git a/include/LibreOfficeKit/LibreOfficeKit.h 
b/include/LibreOfficeKit/LibreOfficeKit.h
index 79a6b2a3faea..6a94583b8bf3 100644
--- a/include/LibreOfficeKit/LibreOfficeKit.h
+++ b/include/LibreOfficeKit/LibreOfficeKit.h
@@ -458,11 +458,6 @@ struct _LibreOfficeKitDocumentClass
     void (*sendFormFieldEvent) (LibreOfficeKitDocument* pThis,
                                 const char* pArguments);
 
-    /// @see lok::Document::renderSearchResult
-    bool (*renderSearchResult) (LibreOfficeKitDocument* pThis,
-                                const char* pSearchResult,
-                                unsigned char** pBitmapBuffer,
-                                int* pWidth, int* pHeight, size_t* pByteSize);
     /// @see lok::Document::setFreemiumDenyList
     void (*setFreemiumDenyList) (LibreOfficeKitDocument* pThis,
                                 const char* freemiumDenyList);
@@ -472,6 +467,21 @@ struct _LibreOfficeKitDocumentClass
                             int nViewId,
                             bool isFreemium);
 
+    /// @see lok::Document::setRestrictedCommandList
+    void (*setRestrictedCommandList) (LibreOfficeKitDocument* pThis,
+                                const char* restrictedCommandList);
+
+    /// @see lok::Document::setRestrictedView
+    void (*setRestrictedView) (LibreOfficeKitDocument* pThis,
+                            int nViewId,
+                            bool isRestricted);
+
+    /// @see lok::Document::renderSearchResult
+    bool (*renderSearchResult) (LibreOfficeKitDocument* pThis,
+                                const char* pSearchResult,
+                                unsigned char** pBitmapBuffer,
+                                int* pWidth, int* pHeight, size_t* pByteSize);
+
 #endif // defined LOK_USE_UNSTABLE_API || defined LIBO_INTERNAL_ONLY
 };
 
diff --git a/include/LibreOfficeKit/LibreOfficeKit.hxx 
b/include/LibreOfficeKit/LibreOfficeKit.hxx
index 778f3a80d3fe..9215ffae6048 100644
--- a/include/LibreOfficeKit/LibreOfficeKit.hxx
+++ b/include/LibreOfficeKit/LibreOfficeKit.hxx
@@ -787,6 +787,24 @@ public:
         mpDoc->pClass->sendFormFieldEvent(mpDoc, pArguments);
     }
 
+    void setFreemiumDenyList(const char* freemiumDenyList)
+    {
+        mpDoc->pClass->setFreemiumDenyList(mpDoc, freemiumDenyList);
+    }
+
+    void setFreemiumView(int nViewId, bool isFreemium)
+    {
+        mpDoc->pClass->setFreemiumView(mpDoc, nViewId, isFreemium);
+    }
+    void setRestrictedCommandList(const char* restrictedCommandList)
+    {
+        mpDoc->pClass->setRestrictedCommandList(mpDoc, restrictedCommandList);
+    }
+
+    void setRestrictedView(int nViewId, bool isRestricted)
+    {
+        mpDoc->pClass->setRestrictedView(mpDoc, nViewId, isRestricted);
+    }
     /**
      * Render input search result to a bitmap buffer.
      *
@@ -802,15 +820,6 @@ public:
     {
         return mpDoc->pClass->renderSearchResult(mpDoc, pSearchResult, 
pBitmapBuffer, pWidth, pHeight, pByteSize);
     }
-    void setFreemiumDenyList(const char* freemiumDenyList)
-    {
-        mpDoc->pClass->setFreemiumDenyList(mpDoc, freemiumDenyList);
-    }
-
-    void setFreemiumView(int nViewId, bool isFreemium)
-    {
-        mpDoc->pClass->setFreemiumView(mpDoc, nViewId, isFreemium);
-    }
 
 #endif // defined LOK_USE_UNSTABLE_API || defined LIBO_INTERNAL_ONLY
 };
diff --git a/include/comphelper/lok.hxx b/include/comphelper/lok.hxx
index d68fabb2e600..847c56012565 100644
--- a/include/comphelper/lok.hxx
+++ b/include/comphelper/lok.hxx
@@ -113,6 +113,10 @@ COMPHELPER_DLLPUBLIC void statusIndicatorFinish();
 COMPHELPER_DLLPUBLIC void setFreemiumDenyList(const char* freemiumDenyList);
 COMPHELPER_DLLPUBLIC const std::vector<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 bool isRestrictedCommand(const OUString& command);
 }
 
 #endif // INCLUDED_COMPHELPER_LOK_HXX
diff --git a/include/sfx2/lokhelper.hxx b/include/sfx2/lokhelper.hxx
index ce5c9f4a63e3..982296600d48 100644
--- a/include/sfx2/lokhelper.hxx
+++ b/include/sfx2/lokhelper.hxx
@@ -65,6 +65,8 @@ public:
     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);
     /// 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/include/sfx2/viewsh.hxx b/include/sfx2/viewsh.hxx
index c972d67c7f13..886ca34cdf44 100644
--- a/include/sfx2/viewsh.hxx
+++ b/include/sfx2/viewsh.hxx
@@ -167,6 +167,7 @@ friend class SfxPrinterController;
     LanguageTag                 maLOKLocale;
     LOKDeviceFormFactor         maLOKDeviceFormFactor;
     bool                        mbLOKIsFreemiumView;
+    bool                        mbLOKIsRestrictedView;
 
     /// Used to set the DocId at construction time. See SetCurrentDocId.
     static ViewShellDocId       mnCurrentDocId;
@@ -396,6 +397,10 @@ public:
     // Freemium view settings
     void setFreemiumView(bool isFreemium) { mbLOKIsFreemiumView = isFreemium; }
     bool isFreemiumView() const { return mbLOKIsFreemiumView; }
+
+    // Restricted view setting
+    void setRestrictedView(bool isRestricted) { mbLOKIsRestrictedView = 
isRestricted; }
+    bool isRestrictedView() { return mbLOKIsRestrictedView; }
 };
 
 
diff --git a/sfx2/source/control/unoctitm.cxx b/sfx2/source/control/unoctitm.cxx
index a20056c15fff..5b55a4d75b75 100644
--- a/sfx2/source/control/unoctitm.cxx
+++ b/sfx2/source/control/unoctitm.cxx
@@ -543,6 +543,23 @@ void SfxDispatchController_Impl::dispatch( const 
css::util::URL& aURL,
         return;
     }
 
+    if (comphelper::LibreOfficeKit::isActive() &&
+        SfxViewShell::Current()->isRestrictedView() &&
+        comphelper::LibreOfficeKit::isRestrictedCommand(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());
+        return;
+    }
+
     if (
         !(pDispatch &&
         (
diff --git a/sfx2/source/view/lokhelper.cxx b/sfx2/source/view/lokhelper.cxx
index f62497f5ae81..bfb55c10eff1 100644
--- a/sfx2/source/view/lokhelper.cxx
+++ b/sfx2/source/view/lokhelper.cxx
@@ -747,6 +747,13 @@ void SfxLokHelper::setFreemiumView(int nViewId, bool 
isFreemium)
         pViewShell->setFreemiumView(isFreemium);
 }
 
+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)
 {
diff --git a/sfx2/source/view/viewsh.cxx b/sfx2/source/view/viewsh.cxx
index 4b12dae1c0ee..2b78cde00386 100644
--- a/sfx2/source/view/viewsh.cxx
+++ b/sfx2/source/view/viewsh.cxx
@@ -1068,6 +1068,7 @@ SfxViewShell::SfxViewShell
 ,   maLOKLocale(LANGUAGE_NONE)
 ,   maLOKDeviceFormFactor(LOKDeviceFormFactor::UNKNOWN)
 ,   mbLOKIsFreemiumView(false)
+,   mbLOKIsRestrictedView(false)
 {
     SetMargin( pViewFrame->GetMargin_Impl() );
 

Reply via email to