From 04b9b4635f258a5252c7a44f06ecf9acee17f354 Mon Sep 17 00:00:00 2001 From: Pranam Lashkari Date: Fri, 17 Sep 2021 19:19:06 +0530 Subject: LOK: unifying the Freemium and uno command hiding APIs part 2 * replace vector with unordered_set * avoid sending multiple blocking messages Signed-off-by: Pranam Lashkari Change-Id: I2f714e4f2797ade4ac9d5b4f824855bba31031c4 --- common/CommandControl.cpp | 14 +++---- common/CommandControl.hpp | 10 ++--- kit/ChildSession.cpp | 63 +++++++++++++++++++--------- kit/ChildSession.hpp | 3 +- loleaflet/src/control/Control.AlertDialog.js | 4 -- loleaflet/src/control/Control.Command.js | 2 +- loleaflet/src/core/Socket.js | 6 +++ loleaflet/src/map/Map.js | 3 +- wsd/ClientSession.cpp | 6 +-- wsd/DocumentBroker.cpp | 12 +++++- 10 files changed, 76 insertions(+), 47 deletions(-) diff --git a/common/CommandControl.cpp b/common/CommandControl.cpp index 1517c12660..7f1b1e91e2 100644 --- a/common/CommandControl.cpp +++ b/common/CommandControl.cpp @@ -7,7 +7,7 @@ #include #include -#include +#include #include "ConfigUtil.hpp" #include "Util.hpp" #include "CommandControl.hpp" @@ -16,7 +16,7 @@ namespace CommandControl { bool FreemiumManager::_isFreemiumUser = false; -std::vector FreemiumManager::FreemiumDenyList; +std::unordered_set FreemiumManager::FreemiumDenyList; std::string FreemiumManager::FreemiumDenyListString; FreemiumManager::FreemiumManager() {} @@ -37,13 +37,13 @@ void FreemiumManager::generateDenyList() command = Util::trim_whitespace(commandList[i]); if(!command.empty()) { - FreemiumDenyList.emplace_back(command); + FreemiumDenyList.emplace(command); } } #endif } -const std::vector& FreemiumManager::getFreemiumDenyList() +const std::unordered_set& FreemiumManager::getFreemiumDenyList() { if (FreemiumDenyList.empty()) generateDenyList(); @@ -61,7 +61,7 @@ const std::string FreemiumManager::getFreemiumDenyListString() bool RestrictionManager::_isRestrictedUser = false; -std::vector RestrictionManager::RestrictedCommandList; +std::unordered_set RestrictionManager::RestrictedCommandList; std::string RestrictionManager::RestrictedCommandListString; RestrictionManager::RestrictionManager() {} @@ -81,13 +81,13 @@ void RestrictionManager::generateRestrictedCommandList() command = Util::trim_whitespace(commandList[i]); if(!command.empty()) { - RestrictedCommandList.emplace_back(command); + RestrictedCommandList.emplace(command); } } #endif } -const std::vector& RestrictionManager::getRestrictedCommandList() +const std::unordered_set& RestrictionManager::getRestrictedCommandList() { if (RestrictedCommandList.empty()) generateRestrictedCommandList(); diff --git a/common/CommandControl.hpp b/common/CommandControl.hpp index dade8daba5..187701fd4d 100644 --- a/common/CommandControl.hpp +++ b/common/CommandControl.hpp @@ -8,14 +8,14 @@ #pragma once #include -#include +#include #include "ConfigUtil.hpp" namespace CommandControl { class FreemiumManager { - static std::vector FreemiumDenyList; + static std::unordered_set FreemiumDenyList; static bool _isFreemiumUser; static std::string FreemiumDenyListString; @@ -23,7 +23,7 @@ class FreemiumManager public: FreemiumManager(); - static const std::vector& getFreemiumDenyList(); + static const std::unordered_set& getFreemiumDenyList(); static const std::string getFreemiumDenyListString(); static bool isFreemiumUser() { return _isFreemiumUser; } @@ -41,7 +41,7 @@ public: class RestrictionManager { - static std::vector RestrictedCommandList; + static std::unordered_set RestrictedCommandList; static bool _isRestrictedUser; static std::string RestrictedCommandListString; @@ -49,7 +49,7 @@ class RestrictionManager public: RestrictionManager(); - static const std::vector& getRestrictedCommandList(); + static const std::unordered_set& getRestrictedCommandList(); static const std::string getRestrictedCommandListString(); static bool isRestrictedUser() { return _isRestrictedUser; } diff --git a/kit/ChildSession.cpp b/kit/ChildSession.cpp index 4ceb931962..6abe8ef5f7 100644 --- a/kit/ChildSession.cpp +++ b/kit/ChildSession.cpp @@ -270,16 +270,10 @@ bool ChildSession::_handleInput(const char *buffer, int length) // Just ignore these. // FIXME: We probably should do something for "canceltiles" at least? } - else if (tokens.equals(0, "freemiumstatus")) + else if (tokens.equals(0, "blockingcommandstatus")) { -#ifdef ENABLE_FREEMIUM - return updateBlockingCommandStatus(buffer, length, tokens, "freemium"); -#endif - } - else if (tokens.equals(0, "restrictionstatus")) - { -#ifdef ENABLE_FEATURE_RESTRICTION - return updateBlockingCommandStatus(buffer, length, tokens, "restricted"); +#if defined(ENABLE_FREEMIUM) || defined(ENABLE_FEATURE_RESTRICTION) + return updateBlockingCommandStatus(buffer, length, tokens); #endif } else @@ -2567,27 +2561,42 @@ int ChildSession::getSpeed() } #if defined(ENABLE_FEATURE_RESTRICTION) || defined(ENABLE_FREEMIUM) -bool ChildSession::updateBlockingCommandStatus(const char* /*buffer*/, int /*length*/, const StringVector& tokens, std::string type) +bool ChildSession::updateBlockingCommandStatus(const char* /*buffer*/, int /*length*/, const StringVector& tokens) { - std::string status; - if (tokens.size() < 2 || (type == "freemium" && !getTokenString(tokens[1], "isFreemiumUser", status))) + std::string freemiumStatus, restrictedStatus; + if (tokens.size() < 2 || !getTokenString(tokens[1], "isRestrictedUser", restrictedStatus)) { - sendTextFrameAndLogError("error: cmd=freemiumstatus kind=failure"); + sendTextFrameAndLogError("error: cmd=restrictionstatus kind=failure"); return false; } - else if (tokens.size() < 2 || (type == "restricted" && !getTokenString(tokens[1], "isRestrictedUser", status))) + else if (tokens.size() < 2 || !getTokenString(tokens[2], "isFreemiumUser", freemiumStatus)) { - sendTextFrameAndLogError("error: cmd=restrictionstatus kind=failure"); + sendTextFrameAndLogError("error: cmd=freemiumstatus kind=failure"); return false; } + std::string blockedCommands = ""; + if (restrictedStatus == "true") + blockedCommands += CommandControl::RestrictionManager::getRestrictedCommandListString(); + if (freemiumStatus == "true") + blockedCommands += blockedCommands.empty() ? CommandControl::FreemiumManager::getFreemiumDenyListString() : + " " + CommandControl::FreemiumManager::getFreemiumDenyListString(); - if(type == "freemium") - getLOKitDocument()->setBlockedCommandList((type + '-' + CommandControl::FreemiumManager::getFreemiumDenyListString()).c_str()); - else if(type == "restricted") - getLOKitDocument()->setBlockedCommandList((type + '-' + CommandControl::RestrictionManager::getRestrictedCommandListString()).c_str()); - getLOKitDocument()->setBlockedCommandView(_viewId, type.c_str(), status == "true"); + getLOKitDocument()->setBlockedCommandList(_viewId, blockedCommands.c_str()); return true; } + +std::string ChildSession::getBlockedCommandType(std::string command) +{ + if(CommandControl::RestrictionManager::getRestrictedCommandList().find(command) + != CommandControl::RestrictionManager::getRestrictedCommandList().end()) + return "restricted"; + + if(CommandControl::FreemiumManager::getFreemiumDenyList().find(command) + != CommandControl::FreemiumManager::getFreemiumDenyList().end()) + return "freemiumdeny"; + + return ""; +} #endif void ChildSession::loKitCallback(const int type, const std::string& payload) @@ -2894,6 +2903,20 @@ void ChildSession::loKitCallback(const int type, const std::string& payload) case LOK_CALLBACK_DOCUMENT_BACKGROUND_COLOR: sendTextFrame("documentbackgroundcolor: " + payload); break; + case LOK_COMMAND_BLOCKED: + { +#if defined(ENABLE_FREEMIUM) || defined(ENABLE_FEATURE_RESTRICTION) + LOG_INF("COMMAND_BLOCKED: " << payload); + Parser parser; + Poco::Dynamic::Var var = parser.parse(payload); + Object::Ptr object = var.extract(); + + std::string cmd = object->get("cmd").toString(); + sendTextFrame("blockedcommand: cmd=" + cmd + + " kind=" + getBlockedCommandType(cmd) + " code=" + object->get("code").toString()); +#endif + } + break; default: LOG_ERR("Unknown callback event (" << lokCallbackTypeToString(type) << "): " << payload); } diff --git a/kit/ChildSession.hpp b/kit/ChildSession.hpp index 30ba7da635..fcb3bed78e 100644 --- a/kit/ChildSession.hpp +++ b/kit/ChildSession.hpp @@ -303,7 +303,8 @@ private: bool renderShapeSelection(const char* buffer, int length, const StringVector& tokens); bool removeTextContext(const char* /*buffer*/, int /*length*/, const StringVector& tokens); #if defined(ENABLE_FREEMIUM) || defined(ENABLE_FEATURE_RESTRICTION) - bool updateBlockingCommandStatus(const char* buffer, int length, const StringVector& tokens, std::string type); + bool updateBlockingCommandStatus(const char* buffer, int length, const StringVector& tokens); + std::string getBlockedCommandType(std::string command); #endif void rememberEventsForInactiveUser(const int type, const std::string& payload); diff --git a/loleaflet/src/control/Control.AlertDialog.js b/loleaflet/src/control/Control.AlertDialog.js index 3aa59b3e4e..630e5a63ce 100644 --- a/loleaflet/src/control/Control.AlertDialog.js +++ b/loleaflet/src/control/Control.AlertDialog.js @@ -84,10 +84,6 @@ L.Control.AlertDialog = L.Control.extend({ e.map.focus(); } }); - } else if (e.kind == 'freemiumdeny') { - this._map.openSubscriptionPopup(e.cmd); - } else if (e.kind == 'restricted') { - //do nothing } else if (e.cmd && e.kind) { this._map.fire('hidebusy'); diff --git a/loleaflet/src/control/Control.Command.js b/loleaflet/src/control/Control.Command.js index ba061e5420..2287754a47 100644 --- a/loleaflet/src/control/Control.Command.js +++ b/loleaflet/src/control/Control.Command.js @@ -49,7 +49,7 @@ L.Map.include({ $(DOMParentElement).click(function(event) { event.stopPropagation(); - that.openSubscriptionPopup(); + that.openSubscriptionPopup(''); }); } }, diff --git a/loleaflet/src/core/Socket.js b/loleaflet/src/core/Socket.js index 8c94021689..ed0eb6e7d0 100644 --- a/loleaflet/src/core/Socket.js +++ b/loleaflet/src/core/Socket.js @@ -1121,6 +1121,12 @@ app.definitions.Socket = L.Class.extend({ this._map._setRestrictions(restrictionInfo); return; } + else if (textMsg.startsWith('blockedcommand: ')) { + var blockedInfo = app.socket.parseServerCmd(textMsg.substring(16)); + if (blockedInfo.errorKind === 'freemiumdeny') + this._map.openSubscriptionPopup(blockedInfo.errorCmd); + return; + } else if (!textMsg.startsWith('tile:') && !textMsg.startsWith('renderfont:') && !textMsg.startsWith('windowpaint:')) { if (imgBytes !== undefined) { diff --git a/loleaflet/src/map/Map.js b/loleaflet/src/map/Map.js index 7e0fa76fc1..644b6255cf 100644 --- a/loleaflet/src/map/Map.js +++ b/loleaflet/src/map/Map.js @@ -273,8 +273,7 @@ L.Map = L.Evented.extend({ this.on('docloaded', function(e) { this._docLoaded = e.status; if (this._docLoaded) { - app.socket.sendMessage('restrictionstatus isRestrictedUser=' + this.Restriction.isRestrictedUser); - app.socket.sendMessage('freemiumstatus isFreemiumUser=' + this.Freemium.isFreemiumUser); + app.socket.sendMessage('blockingcommandstatus isRestrictedUser=' + this.Restriction.isRestrictedUser + ' isFreemiumUser=' + this.Freemium.isFreemiumUser); this.notifyActive(); if (!document.hasFocus()) { this.fire('editorgotfocus'); diff --git a/wsd/ClientSession.cpp b/wsd/ClientSession.cpp index 670bbf5f6e..fe1a70f4af 100644 --- a/wsd/ClientSession.cpp +++ b/wsd/ClientSession.cpp @@ -1028,11 +1028,7 @@ bool ClientSession::_handleInput(const char *buffer, int length) { return attemptLock(docBroker); } - else if (tokens.equals(0, "freemiumstatus")) - { - return forwardToChild(std::string(buffer, length), docBroker); - } - else if (tokens.equals(0, "restrictionstatus")) + else if (tokens.equals(0, "blockingcommandstatus")) { return forwardToChild(std::string(buffer, length), docBroker); } diff --git a/wsd/DocumentBroker.cpp b/wsd/DocumentBroker.cpp index 63f0907045..2ba824718b 100644 --- a/wsd/DocumentBroker.cpp +++ b/wsd/DocumentBroker.cpp @@ -792,7 +792,11 @@ bool DocumentBroker::download(const std::shared_ptr& session, con #ifdef ENABLE_FREEMIUM Object::Ptr freemiumInfo = new Object(); freemiumInfo->set("IsFreemiumUser", CommandControl::FreemiumManager::isFreemiumUser()); - freemiumInfo->set("FreemiumDenyList", CommandControl::FreemiumManager::getFreemiumDenyList()); + + // Poco:Dynamic:Var does not support std::unordred_set so converted to std::vector + std::vector freemiumDenyList(CommandControl::FreemiumManager::getFreemiumDenyList().begin(), + CommandControl::FreemiumManager::getFreemiumDenyList().end()); + freemiumInfo->set("FreemiumDenyList", freemiumDenyList); freemiumInfo->set("FreemiumPurchaseTitle", CommandControl::FreemiumManager::getFreemiumPurchaseTitle()); freemiumInfo->set("FreemiumPurchaseLink", CommandControl::FreemiumManager::getFreemiumPurchaseLink()); freemiumInfo->set("FreemiumPurchaseDescription", CommandControl::FreemiumManager::getFreemiumPurchaseDescription()); @@ -811,7 +815,11 @@ bool DocumentBroker::download(const std::shared_ptr& session, con #ifdef ENABLE_FEATURE_RESTRICTION Object::Ptr restrictionInfo = new Object(); restrictionInfo->set("IsRestrictedUser", CommandControl::RestrictionManager::isRestrictedUser()); - restrictionInfo->set("RestrictedCommandList", CommandControl::RestrictionManager::getRestrictedCommandList()); + + // Poco:Dynamic:Var does not support std::unordred_set so converted to std::vector + std::vector restrictedCommandList(CommandControl::RestrictionManager::getRestrictedCommandList().begin(), + CommandControl::RestrictionManager::getRestrictedCommandList().end()); + restrictionInfo->set("RestrictedCommandList", restrictedCommandList); std::ostringstream ossRestrictionInfo; restrictionInfo->stringify(ossRestrictionInfo); -- cgit