From 419ebb636dc46040fe508f71af3f6baad3c930ff Mon Sep 17 00:00:00 2001 From: Michael Stahl Date: Fri, 22 Jan 2016 13:39:32 +0100 Subject: [PATCH 389/398] libreofficekit: password interaction optional and off by default Add setOptionalFeatures() function that clients must call during initialization, and enum LibreOfficeKitOptionalFeatures. Reviewed-on: https://gerrit.libreoffice.org/21809 Reviewed-by: Jan Holesovsky Tested-by: Jan Holesovsky (cherry picked from commit 23a0ee3c01c3588472e1c19605909d6b9401253c) libreofficekit: ask for password when loading encrypted documents (cherry picked from commit 2b63e576a5cf06f4af877d63403ad7955ac71b72) desktop: use x prefix for uno::Reference (cherry picked from commit 0101cd3da6262169fa273309a86ba5e7cfe573bf) loplugin:defaultparams (cherry picked from commit 95c8b8e85d3328bfbe906ef3f69145842aae01db) Reviewed-on: https://gerrit.libreoffice.org/21838 Tested-by: Jenkins Reviewed-by: Miklos Vajna (cherry picked from commit 2241a7fd97b8b70d2d3106ac531cc72192ad708f) Change-Id: I73035193c87033052921c3aad94fdc057fe81111 --- desktop/inc/lib/init.hxx | 13 +++- desktop/source/lib/init.cxx | 38 +++++++++- desktop/source/lib/lokinteractionhandler.cxx | 107 +++++++++++++++++++++++++-- desktop/source/lib/lokinteractionhandler.hxx | 19 ++++- include/LibreOfficeKit/LibreOfficeKit.h | 9 +++ include/LibreOfficeKit/LibreOfficeKit.hxx | 34 +++++++++ include/LibreOfficeKit/LibreOfficeKitEnums.h | 45 ++++++++++- libreofficekit/source/gtk/lokdocview.cxx | 10 +++ 8 files changed, 264 insertions(+), 11 deletions(-) diff --git a/desktop/inc/lib/init.hxx b/desktop/inc/lib/init.hxx index 4f878d26c007..560bd06c6cd9 100644 --- a/desktop/inc/lib/init.hxx +++ b/desktop/inc/lib/init.hxx @@ -7,13 +7,16 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include +#include #include #include #include +#include +#include #include "../../source/inc/desktopdllapi.h" #include -using namespace css; +class LOKInteractionHandler; namespace desktop { struct DESKTOP_DLLPUBLIC LibLODocument_Impl : public _LibreOfficeKitDocument @@ -34,7 +37,15 @@ namespace desktop { oslThread maThread; LibreOfficeKitCallback mpCallback; void *mpCallbackData; + int64_t mOptionalFeatures; + std::map> mInteractionMap; LibLibreOffice_Impl(); + ~LibLibreOffice_Impl(); + + bool hasOptionalFeature(LibreOfficeKitOptionalFeatures const feature) + { + return (mOptionalFeatures & feature) != 0; + } }; } diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx index 6bed25514137..35ba91306036 100644 --- a/desktop/source/lib/init.cxx +++ b/desktop/source/lib/init.cxx @@ -433,11 +433,16 @@ static void lo_registerCallback (LibreOfficeKit* pThis, LibreOfficeKitCallback pCallback, void* pData); static char* lo_getFilterTypes(LibreOfficeKit* pThis); +static void lo_setOptionalFeatures(LibreOfficeKit* pThis, uint64_t features); +static void lo_setDocumentPassword(LibreOfficeKit* pThis, + const char* pURL, + const char* pPassword); LibLibreOffice_Impl::LibLibreOffice_Impl() : maThread(0) , mpCallback(nullptr) , mpCallbackData(nullptr) + , mOptionalFeatures(0) { if(!(m_pOfficeClass = gOfficeClass.lock())) { m_pOfficeClass.reset(new LibreOfficeKitClass); @@ -449,6 +454,8 @@ LibLibreOffice_Impl::LibLibreOffice_Impl() m_pOfficeClass->documentLoadWithOptions = lo_documentLoadWithOptions; m_pOfficeClass->registerCallback = lo_registerCallback; m_pOfficeClass->getFilterTypes = lo_getFilterTypes; + m_pOfficeClass->setOptionalFeatures = lo_setOptionalFeatures; + m_pOfficeClass->setDocumentPassword = lo_setDocumentPassword; gOfficeClass = m_pOfficeClass; } @@ -456,6 +463,10 @@ LibLibreOffice_Impl::LibLibreOffice_Impl() pClass = m_pOfficeClass.get(); } +LibLibreOffice_Impl::~LibLibreOffice_Impl() +{ +} + namespace { @@ -517,7 +528,10 @@ static LibreOfficeKitDocument* lo_documentLoadWithOptions(LibreOfficeKit* pThis, uno::makeAny(OUString::createFromAscii(pOptions)), beans::PropertyState_DIRECT_VALUE); - uno::Reference xInteraction(new LOKInteractionHandler(::comphelper::getProcessComponentContext())); + rtl::Reference const pInteraction( + new LOKInteractionHandler(::comphelper::getProcessComponentContext(), pLib)); + auto const pair(pLib->mInteractionMap.insert(std::make_pair(aURL.toUtf8(), pInteraction))); + uno::Reference const xInteraction(pInteraction.get()); aFilterOptions[1].Name = "InteractionHandler"; aFilterOptions[1].Value <<= xInteraction; @@ -536,6 +550,12 @@ static LibreOfficeKitDocument* lo_documentLoadWithOptions(LibreOfficeKit* pThis, aURL, OUString("_blank"), 0, aFilterOptions); + assert(!xComponent.is() || pair.second); // concurrent loading of same URL ought to fail + if (!pair.second) + { + pLib->mInteractionMap.erase(aURL.toUtf8()); + } + if (!xComponent.is()) { pLib->maLastExceptionMsg = "loadComponentFromURL returned an empty reference"; @@ -1579,6 +1599,22 @@ static char* lo_getFilterTypes(LibreOfficeKit* pThis) return strdup(aStream.str().c_str()); } +static void lo_setOptionalFeatures(LibreOfficeKit* pThis, uint64_t const features) +{ + LibLibreOffice_Impl *const pLib = static_cast(pThis); + pLib->mOptionalFeatures = features; +} + +static void lo_setDocumentPassword(LibreOfficeKit* pThis, + const char* pURL, const char* pPassword) +{ + assert(pThis); + assert(pURL); + LibLibreOffice_Impl *const pLib = static_cast(pThis); + assert(pLib->mInteractionMap.find(OString(pURL)) != pLib->mInteractionMap.end()); + pLib->mInteractionMap.find(OString(pURL))->second->SetPassword(pPassword); +} + static void force_c_locale() { // force locale (and resource files loaded) to en-US diff --git a/desktop/source/lib/lokinteractionhandler.cxx b/desktop/source/lib/lokinteractionhandler.cxx index 1d20b0219e28..50a79721ff69 100644 --- a/desktop/source/lib/lokinteractionhandler.cxx +++ b/desktop/source/lib/lokinteractionhandler.cxx @@ -19,14 +19,28 @@ #include "lokinteractionhandler.hxx" +#include #include +#include #include +#include +#include + +#define LOK_USE_UNSTABLE_API +#include <../../inc/lib/init.hxx> + +#include using namespace com::sun::star; -LOKInteractionHandler::LOKInteractionHandler(uno::Reference const & /*rxContext*/) +LOKInteractionHandler::LOKInteractionHandler( + uno::Reference const & /*rxContext*/, + desktop::LibLibreOffice_Impl *const pLOKit) + : m_pLOKit(pLOKit) + , m_usePassword(false) { + assert(m_pLOKit); } LOKInteractionHandler::~LOKInteractionHandler() @@ -58,15 +72,84 @@ void SAL_CALL LOKInteractionHandler::initialize(uno::Sequence const & { } -void SAL_CALL LOKInteractionHandler::handle(uno::Reference const & rRequest) throw (uno::RuntimeException, std::exception) +void SAL_CALL LOKInteractionHandler::handle( + uno::Reference const & xRequest) +throw (uno::RuntimeException, std::exception) { // just do the same thing in both cases - handleInteractionRequest(rRequest); + handleInteractionRequest(xRequest); } -sal_Bool SAL_CALL LOKInteractionHandler::handleInteractionRequest(const uno::Reference& rRequest) throw ( uno::RuntimeException, std::exception ) +sal_Bool SAL_CALL LOKInteractionHandler::handleInteractionRequest( + const uno::Reference& xRequest) +throw (uno::RuntimeException, std::exception) { - uno::Sequence> const &rContinuations = rRequest->getContinuations(); + uno::Sequence> const &rContinuations = xRequest->getContinuations(); + + uno::Any const request(xRequest->getRequest()); + task::DocumentPasswordRequest2 passwordRequest; + if (request >>= passwordRequest) + { + if (m_pLOKit->hasOptionalFeature((passwordRequest.IsRequestPasswordToModify) + ? LOK_FEATURE_DOCUMENT_PASSWORD_TO_MODIFY + : LOK_FEATURE_DOCUMENT_PASSWORD)) + { + OString const url(passwordRequest.Name.toUtf8()); + m_pLOKit->mpCallback(passwordRequest.IsRequestPasswordToModify + ? LOK_CALLBACK_DOCUMENT_PASSWORD_TO_MODIFY + : LOK_CALLBACK_DOCUMENT_PASSWORD, + url.getStr(), + m_pLOKit->mpCallbackData); + + // block until SetPassword is called + m_havePassword.wait(); + m_havePassword.reset(); + } + + for (sal_Int32 i = 0; i < rContinuations.getLength(); ++i) + { + if (m_usePassword) + { + if (passwordRequest.IsRequestPasswordToModify) + { + uno::Reference const xIPW2( + rContinuations[i], uno::UNO_QUERY); + xIPW2->setPasswordToModify(m_Password); + xIPW2->select(); + } + else + { + uno::Reference const xIPW( + rContinuations[i], uno::UNO_QUERY); + if (xIPW.is()) + { + xIPW->setPassword(m_Password); + xIPW->select(); + } + } + } + else + { + if (passwordRequest.IsRequestPasswordToModify) + { + uno::Reference const xIPW2( + rContinuations[i], uno::UNO_QUERY); + xIPW2->setRecommendReadOnly(true); + xIPW2->select(); + } + else + { + uno::Reference const xAbort( + rContinuations[i], uno::UNO_QUERY); + if (xAbort.is()) + { + xAbort->select(); + } + } + } + } + return sal_True; + } // TODO: add LOK api that allows handling this for real, for the moment we // just set the interaction as 'Approved' @@ -80,4 +163,18 @@ sal_Bool SAL_CALL LOKInteractionHandler::handleInteractionRequest(const uno::Ref return sal_True; } +void LOKInteractionHandler::SetPassword(char const*const pPassword) +{ + if (pPassword) + { + m_Password = OUString(pPassword, strlen(pPassword), RTL_TEXTENCODING_UTF8); + m_usePassword = true; + } + else + { + m_usePassword = false; + } + m_havePassword.set(); +} + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/desktop/source/lib/lokinteractionhandler.hxx b/desktop/source/lib/lokinteractionhandler.hxx index 6d4aa8231daf..584658337fc4 100644 --- a/desktop/source/lib/lokinteractionhandler.hxx +++ b/desktop/source/lib/lokinteractionhandler.hxx @@ -20,12 +20,15 @@ #ifndef INCLUDED_DESKTOP_SOURCE_LIB_LOKINTERACTIONHANDLER_HXX #define INCLUDED_DESKTOP_SOURCE_LIB_LOKINTERACTIONHANDLER_HXX +#include #include #include #include #include +namespace desktop { struct LibLibreOffice_Impl; } + /** InteractionHandler is an interface that provides the user with various dialogs / error messages. We need an own implementation for the LibreOfficeKit so that we can route the @@ -38,11 +41,21 @@ class LOKInteractionHandler: public cppu::WeakImplHelper { - LOKInteractionHandler(const LOKInteractionHandler&) SAL_DELETED_FUNCTION; - LOKInteractionHandler& operator=(const LOKInteractionHandler&) SAL_DELETED_FUNCTION; +private: + desktop::LibLibreOffice_Impl * m_pLOKit; + OUString m_Password; + bool m_usePassword; + osl::Condition m_havePassword; + + LOKInteractionHandler(const LOKInteractionHandler&) = delete; + LOKInteractionHandler& operator=(const LOKInteractionHandler&) = delete; public: - explicit LOKInteractionHandler(com::sun::star::uno::Reference const & rxContext); + void SetPassword(char const* pPassword); + + explicit LOKInteractionHandler( + com::sun::star::uno::Reference const & rxContext, + desktop::LibLibreOffice_Impl *); virtual ~LOKInteractionHandler(); diff --git a/include/LibreOfficeKit/LibreOfficeKit.h b/include/LibreOfficeKit/LibreOfficeKit.h index 5189cca5eb5e..b31e5eef1c01 100644 --- a/include/LibreOfficeKit/LibreOfficeKit.h +++ b/include/LibreOfficeKit/LibreOfficeKit.h @@ -15,6 +15,7 @@ #ifdef LOK_USE_UNSTABLE_API // the unstable API needs C99's bool #include +#include #endif #include @@ -62,6 +63,14 @@ struct _LibreOfficeKitClass /// @see lok::Office::getFilterTypes(). char* (*getFilterTypes) (LibreOfficeKit* pThis); + + /// @see lok::Office::setOptionalFeatures(). + void (*setOptionalFeatures)(LibreOfficeKit* pThis, uint64_t features); + + /// @see lok::Office::setDocumentPassword(). + void (*setDocumentPassword) (LibreOfficeKit* pThis, + char const* pURL, + char const* pPassword); #endif }; diff --git a/include/LibreOfficeKit/LibreOfficeKit.hxx b/include/LibreOfficeKit/LibreOfficeKit.hxx index e592bbe549ab..53ad4359357a 100644 --- a/include/LibreOfficeKit/LibreOfficeKit.hxx +++ b/include/LibreOfficeKit/LibreOfficeKit.hxx @@ -442,6 +442,40 @@ public: { return mpThis->pClass->getFilterTypes(mpThis); } + + /** + * Set bitmask of optional features supported by the client. + * + * @see LibreOfficeKitOptionalFeatures + */ + void setOptionalFeatures(uint64_t features) + { + return mpThis->pClass->setOptionalFeatures(mpThis, features); + } + + /** + * Set password required for loading or editing a document. + * + * Loading the document is blocked until the password is provided. + * + * @param pURL the URL of the document, as sent to the callback + * @param pPassword the password, nullptr indicates no password + * + * In response to LOK_CALLBACK_DOCUMENT_PASSWORD, a vaild password + * will continue loading the document, an invalid password will + * result in another LOK_CALLBACK_DOCUMENT_PASSWORD request, + * and a NULL password will abort loading the document. + * + * In response to LOK_CALLBACK_DOCUMENT_PASSWORD_TO_MODIFY, a vaild + * password will continue loading the document, an invalid password will + * result in another LOK_CALLBACK_DOCUMENT_PASSWORD_TO_MODIFY request, + * and a NULL password will continue loading the document in read-only + * mode. + */ + inline void setDocumentPassword(char const* pURL, char const* pPassword) + { + mpThis->pClass->setDocumentPassword(mpThis, pURL, pPassword); + } #endif // LOK_USE_UNSTABLE_API }; diff --git a/include/LibreOfficeKit/LibreOfficeKitEnums.h b/include/LibreOfficeKit/LibreOfficeKitEnums.h index a0f5e886dcac..5931f7871e50 100644 --- a/include/LibreOfficeKit/LibreOfficeKitEnums.h +++ b/include/LibreOfficeKit/LibreOfficeKitEnums.h @@ -40,6 +40,32 @@ typedef enum } LibreOfficeKitTileMode; +/** Optional features of LibreOfficeKit, in particular callbacks that block + * LibreOfficeKit until the corresponding reply is received, which would + * deadlock if the client does not support the feature. + * + * @see lok::Office::setOptionalFeatures(). + */ +typedef enum +{ + /** + * Handle LOK_CALLBACK_DOCUMENT_PASSWORD by prompting the user + * for a password. + * + * @see lok::Office::setDocumentPassword(). + */ + LOK_FEATURE_DOCUMENT_PASSWORD = (1ULL << 0), + + /** + * Handle LOK_CALLBACK_DOCUMENT_PASSWORD_TO_MODIFY by prompting the user + * for a password. + * + * @see lok::Office::setDocumentPassword(). + */ + LOK_FEATURE_DOCUMENT_PASSWORD_TO_MODIFY = (1ULL << 1), +} +LibreOfficeKitOptionalFeatures; + typedef enum { /** @@ -221,7 +247,24 @@ typedef enum /** * The text content of the formula bar in Calc. */ - LOK_CALLBACK_CELL_FORMULA + LOK_CALLBACK_CELL_FORMULA, + + /** + * Loading a document requires a password. + * + * Loading the document is blocked until the password is provided via + * lok::Office::setDocumentPassword(). The document cannot be loaded + * without the password. + */ + LOK_CALLBACK_DOCUMENT_PASSWORD, + + /** + * Editing a document requires a password. + * + * Loading the document is blocked until the password is provided via + * lok::Office::setDocumentPassword(). + */ + LOK_CALLBACK_DOCUMENT_PASSWORD_TO_MODIFY, } LibreOfficeKitCallbackType; diff --git a/libreofficekit/source/gtk/lokdocview.cxx b/libreofficekit/source/gtk/lokdocview.cxx index 1af678e1acc2..960c8c1f2624 100644 --- a/libreofficekit/source/gtk/lokdocview.cxx +++ b/libreofficekit/source/gtk/lokdocview.cxx @@ -804,6 +804,14 @@ globalCallback (gpointer pData) g_signal_emit (pCallback->m_pDocView, doc_view_signals[LOAD_CHANGED], 0, 1.0); } break; + case LOK_CALLBACK_DOCUMENT_PASSWORD: + case LOK_CALLBACK_DOCUMENT_PASSWORD_TO_MODIFY: + { + char const*const pURL(pCallback->m_aPayload.c_str()); + // TODO maybe allow more passwords + priv->m_pOffice->pClass->setDocumentPassword(priv->m_pOffice, pURL, "1"); + } + break; default: g_assert(false); break; @@ -2026,6 +2034,8 @@ static gboolean lok_doc_view_initable_init (GInitable *initable, GCancellable* / return FALSE; } +// priv->m_pOffice->pClass->setOptionalFeatures(priv->m_pOffice, LOK_FEATURE_DOCUMENT_PASSWORD|LOK_FEATURE_DOCUMENT_PASSWORD_TO_MODIFY); + return TRUE; } -- 2.12.0