summaryrefslogtreecommitdiffstats
path: root/wsd
diff options
context:
space:
mode:
authorAshod Nakashian <ashod.nakashian@collabora.co.uk>2022-06-05 18:37:03 -0400
committerMichael Meeks <michael.meeks@collabora.com>2022-07-15 11:14:14 +0100
commit45b7eb778d39cf7ee82369caa6de2438356d2431 (patch)
tree2922a6f787c56767ec3d1ba71bfc37f0d857805c /wsd
parentAdd home mode option - enabling more configurability for CODE. (diff)
downloadonline-45b7eb778d39cf7ee82369caa6de2438356d2431.tar.gz
online-45b7eb778d39cf7ee82369caa6de2438356d2431.zip
wsd: reuse parsed message
Change-Id: I9d67f92ba99037954400e07a7d1101e5de3716fd Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
Diffstat (limited to 'wsd')
-rw-r--r--wsd/COOLWSD.cpp8
-rw-r--r--wsd/ClientSession.cpp39
-rw-r--r--wsd/ClientSession.hpp2
-rw-r--r--wsd/DocumentBroker.cpp49
-rw-r--r--wsd/DocumentBroker.hpp8
-rw-r--r--wsd/TileCache.cpp9
-rw-r--r--wsd/TileCache.hpp2
7 files changed, 56 insertions, 61 deletions
diff --git a/wsd/COOLWSD.cpp b/wsd/COOLWSD.cpp
index 9f8bc94533..847423daa1 100644
--- a/wsd/COOLWSD.cpp
+++ b/wsd/COOLWSD.cpp
@@ -3247,20 +3247,20 @@ private:
if (UnitWSD::get().filterChildMessage(data))
return;
- const std::string abbr = getAbbreviatedMessage(data);
+ auto message = std::make_shared<Message>(data.data(), data.size(), Message::Dir::Out);
std::shared_ptr<StreamSocket> socket = getSocket().lock();
if (socket)
- LOG_TRC('#' << socket->getFD() << " Prisoner message [" << abbr << "].");
+ LOG_TRC('#' << socket->getFD() << " Prisoner message [" << message->abbr() << ']');
else
LOG_WRN("Message handler called but without valid socket.");
std::shared_ptr<ChildProcess> child = _childProcess.lock();
std::shared_ptr<DocumentBroker> docBroker = child ? child->getDocumentBroker() : nullptr;
if (docBroker)
- docBroker->handleInput(data);
+ docBroker->handleInput(message);
else
LOG_WRN("Child " << child->getPid() <<
- " has no DocumentBroker to handle message: [" << abbr << "].");
+ " has no DocumentBroker to handle message: [" << message->abbr() << ']');
}
int getPollEvents(std::chrono::steady_clock::time_point /* now */,
diff --git a/wsd/ClientSession.cpp b/wsd/ClientSession.cpp
index 04c1bfd457..c9d33c6e5c 100644
--- a/wsd/ClientSession.cpp
+++ b/wsd/ClientSession.cpp
@@ -1414,10 +1414,8 @@ void ClientSession::postProcessCopyPayload(const std::shared_ptr<Message>& paylo
});
}
-bool ClientSession::handleKitToClientMessage(const char* buffer, const int length)
+bool ClientSession::handleKitToClientMessage(const std::shared_ptr<Message>& payload)
{
- const auto payload = std::make_shared<Message>(buffer, length, Message::Dir::Out);
-
LOG_TRC("handling kit-to-client [" << payload->abbr() << ']');
const std::string& firstLine = payload->firstLine();
@@ -1437,14 +1435,12 @@ bool ClientSession::handleKitToClientMessage(const char* buffer, const int lengt
const auto& tokens = payload->tokens();
if (tokens.equals(0, "unocommandresult:"))
{
- const std::string stringMsg(buffer, length);
- LOG_INF("Command: " << stringMsg);
- const std::size_t index = stringMsg.find_first_of('{');
- if (index != std::string::npos)
+ LOG_INF("Command: " << firstLine);
+ const std::string stringJSON = payload->jsonString();
+ if (!stringJSON.empty())
{
try
{
- const std::string stringJSON = stringMsg.substr(index);
Poco::JSON::Parser parser;
const Poco::Dynamic::Var parsedJSON = parser.parse(stringJSON);
const auto& object = parsedJSON.extract<Poco::JSON::Object::Ptr>();
@@ -1476,7 +1472,7 @@ bool ClientSession::handleKitToClientMessage(const char* buffer, const int lengt
}
else
{
- LOG_WRN("Expected json unocommandresult. Ignoring: " << stringMsg);
+ LOG_WRN("Expected json unocommandresult. Ignoring: " << firstLine);
}
}
else if (tokens.equals(0, "error:"))
@@ -1831,13 +1827,11 @@ bool ClientSession::handleKitToClientMessage(const char* buffer, const int lengt
}
else if (tokens.equals(0, "commandvalues:"))
{
- const std::string stringMsg(buffer, length);
- const std::size_t index = stringMsg.find_first_of('{');
- if (index != std::string::npos)
+ const std::string stringJSON = payload->jsonString();
+ if (!stringJSON.empty())
{
try
{
- const std::string stringJSON = stringMsg.substr(index);
Poco::JSON::Parser parser;
const Poco::Dynamic::Var result = parser.parse(stringJSON);
const auto& object = result.extract<Poco::JSON::Object::Ptr>();
@@ -1846,7 +1840,8 @@ bool ClientSession::handleKitToClientMessage(const char* buffer, const int lengt
commandName == ".uno:StyleApply")
{
// other commands should not be cached
- docBroker->tileCache().saveTextStream(TileCache::StreamType::CmdValues, stringMsg, commandName);
+ docBroker->tileCache().saveTextStream(TileCache::StreamType::CmdValues,
+ commandName, payload->data());
}
}
catch (const std::exception& exception)
@@ -1857,7 +1852,8 @@ bool ClientSession::handleKitToClientMessage(const char* buffer, const int lengt
}
else if (tokens.equals(0, "invalidatetiles:"))
{
- assert(firstLine.size() == static_cast<std::string::size_type>(length));
+ assert(firstLine.size() == payload->size() &&
+ "Unexpected multiline data in invalidatetiles");
// First forward invalidation
bool ret = forwardToClient(payload);
@@ -1867,10 +1863,10 @@ bool ClientSession::handleKitToClientMessage(const char* buffer, const int lengt
}
else if (tokens.equals(0, "invalidatecursor:"))
{
- assert(firstLine.size() == static_cast<std::string::size_type>(length));
+ assert(firstLine.size() == payload->size() &&
+ "Unexpected multiline data in invalidatecursor");
- const std::size_t index = firstLine.find_first_of('{');
- const std::string stringJSON = firstLine.substr(index);
+ const std::string stringJSON = payload->jsonString();
Poco::JSON::Parser parser;
try
{
@@ -1912,9 +1908,10 @@ bool ClientSession::handleKitToClientMessage(const char* buffer, const int lengt
}
getTokenString(tokens[2], "char", text);
- assert(firstLine.size() < static_cast<std::string::size_type>(length));
- docBroker->tileCache().saveStream(TileCache::StreamType::Font, font+text,
- buffer + firstLine.size() + 1, length - firstLine.size() - 1);
+ assert(firstLine.size() < payload->size() && "Missing multiline data in renderfont");
+ docBroker->tileCache().saveStream(TileCache::StreamType::Font, font + text,
+ payload->data().data() + firstLine.size() + 1,
+ payload->data().size() - firstLine.size() - 1);
return forwardToClient(payload);
}
}
diff --git a/wsd/ClientSession.hpp b/wsd/ClientSession.hpp
index 4e9cad94f3..fdb88caa83 100644
--- a/wsd/ClientSession.hpp
+++ b/wsd/ClientSession.hpp
@@ -69,7 +69,7 @@ public:
bool isWritable() const { return !isReadOnly() || isAllowChangeComments(); }
/// Handle kit-to-client message.
- bool handleKitToClientMessage(const char* data, const int size);
+ bool handleKitToClientMessage(const std::shared_ptr<Message>& payload);
/// Integer id of the view in the kit process, or -1 if unknown
int getKitViewId() const { return _kitViewId; }
diff --git a/wsd/DocumentBroker.cpp b/wsd/DocumentBroker.cpp
index 3345e257af..7a50379631 100644
--- a/wsd/DocumentBroker.cpp
+++ b/wsd/DocumentBroker.cpp
@@ -2607,9 +2607,8 @@ void DocumentBroker::unregisterDownloadId(const std::string& downloadId)
}
/// Handles input from the prisoner / child kit process
-bool DocumentBroker::handleInput(const std::vector<char>& payload)
+bool DocumentBroker::handleInput(const std::shared_ptr<Message>& message)
{
- auto message = std::make_shared<Message>(payload.data(), payload.size(), Message::Dir::Out);
LOG_TRC("DocumentBroker handling child message: [" << message->abbr() << "].");
#if !MOBILEAPP
@@ -2628,11 +2627,11 @@ bool DocumentBroker::handleInput(const std::vector<char>& payload)
{
if (message->firstTokenMatches("tile:"))
{
- handleTileResponse(payload);
+ handleTileResponse(message);
}
else if (message->firstTokenMatches("tilecombine:"))
{
- handleTileCombinedResponse(payload);
+ handleTileCombinedResponse(message);
}
else if (message->firstTokenMatches("errortoall:"))
{
@@ -2660,9 +2659,10 @@ bool DocumentBroker::handleInput(const std::vector<char>& payload)
LOG_CHECK_RET(message->tokens().size() == 1, false);
if (COOLWSD::TraceEventFile != NULL && TraceEvent::isRecordingOn())
{
- const auto newLine = static_cast<const char*>(memchr(payload.data(), '\n', payload.size()));
- if (newLine)
- COOLWSD::writeTraceEventRecording(newLine + 1, payload.size() - (newLine + 1 - payload.data()));
+ const auto firstLine = message->firstLine();
+ if (firstLine.size() < message->size())
+ COOLWSD::writeTraceEventRecording(message->data().data() + firstLine.size() + 1,
+ message->size() - firstLine.size() - 1);
}
}
else if (message->firstTokenMatches("forcedtraceevent:"))
@@ -2670,9 +2670,10 @@ bool DocumentBroker::handleInput(const std::vector<char>& payload)
LOG_CHECK_RET(message->tokens().size() == 1, false);
if (COOLWSD::TraceEventFile != NULL)
{
- const auto newLine = static_cast<const char*>(memchr(payload.data(), '\n', payload.size()));
- if (newLine)
- COOLWSD::writeTraceEventRecording(newLine + 1, payload.size() - (newLine + 1 - payload.data()));
+ const auto firstLine = message->firstLine();
+ if (firstLine.size() < message->size())
+ COOLWSD::writeTraceEventRecording(message->data().data() + firstLine.size() + 1,
+ message->size() - firstLine.size() - 1);
}
}
else
@@ -3032,18 +3033,18 @@ void DocumentBroker::cancelTileRequests(const std::shared_ptr<ClientSession>& se
}
}
-void DocumentBroker::handleTileResponse(const std::vector<char>& payload)
+void DocumentBroker::handleTileResponse(const std::shared_ptr<Message>& message)
{
- const std::string firstLine = getFirstLine(payload);
+ const std::string firstLine = message->firstLine();
LOG_DBG("Handling tile: " << firstLine);
try
{
- const std::size_t length = payload.size();
+ const std::size_t length = message->size();
if (firstLine.size() < static_cast<std::string::size_type>(length) - 1)
{
const TileDesc tile = TileDesc::parse(firstLine);
- const char* buffer = payload.data();
+ const char* buffer = message->data().data();
const std::size_t offset = firstLine.size() + 1;
std::unique_lock<std::mutex> lock(_mutex);
@@ -3062,18 +3063,18 @@ void DocumentBroker::handleTileResponse(const std::vector<char>& payload)
}
}
-void DocumentBroker::handleTileCombinedResponse(const std::vector<char>& payload)
+void DocumentBroker::handleTileCombinedResponse(const std::shared_ptr<Message>& message)
{
- const std::string firstLine = getFirstLine(payload);
+ const std::string firstLine = message->firstLine();
LOG_DBG("Handling tile combined: " << firstLine);
try
{
- const std::size_t length = payload.size();
+ const std::size_t length = message->size();
if (firstLine.size() <= static_cast<std::string::size_type>(length) - 1)
{
const TileCombined tileCombined = TileCombined::parse(firstLine);
- const char* buffer = payload.data();
+ const char* buffer = message->data().data();
std::size_t offset = firstLine.size() + 1;
std::unique_lock<std::mutex> lock(_mutex);
@@ -3213,9 +3214,6 @@ bool DocumentBroker::forwardToClient(const std::shared_ptr<Message>& payload)
std::string sid;
if (COOLProtocol::parseNameValuePair(payload->forwardToken(), name, sid, '-') && name == "client")
{
- const auto& data = payload->data().data();
- const auto& size = payload->size();
-
if (sid == "all")
{
// Broadcast to all.
@@ -3224,7 +3222,7 @@ bool DocumentBroker::forwardToClient(const std::shared_ptr<Message>& payload)
for (const auto& it : _sessions)
{
if (!it.second->inWaitDisconnected())
- it.second->handleKitToClientMessage(data, size);
+ it.second->handleKitToClientMessage(payload);
}
}
else
@@ -3235,7 +3233,7 @@ bool DocumentBroker::forwardToClient(const std::shared_ptr<Message>& payload)
// Take a ref as the session could be removed from _sessions
// if it's the save confirmation keeping a stopped session alive.
std::shared_ptr<ClientSession> session = it->second;
- return session->handleKitToClientMessage(data, size);
+ return session->handleKitToClientMessage(payload);
}
else
{
@@ -3583,13 +3581,12 @@ void RenderSearchResultBroker::dispose()
}
}
-bool RenderSearchResultBroker::handleInput(const std::vector<char>& payload)
+bool RenderSearchResultBroker::handleInput(const std::shared_ptr<Message>& message)
{
- bool bResult = DocumentBroker::handleInput(payload);
+ bool bResult = DocumentBroker::handleInput(message);
if (bResult)
{
- auto message = std::make_shared<Message>(payload.data(), payload.size(), Message::Dir::Out);
auto const& messageData = message->data();
static std::string commandString = "rendersearchresult:\n";
diff --git a/wsd/DocumentBroker.hpp b/wsd/DocumentBroker.hpp
index 7f46348907..2817beb6a0 100644
--- a/wsd/DocumentBroker.hpp
+++ b/wsd/DocumentBroker.hpp
@@ -407,7 +407,7 @@ public:
bool isMarkedToDestroy() const { return _docState.isMarkedToDestroy() || _stop; }
- virtual bool handleInput(const std::vector<char>& payload);
+ virtual bool handleInput(const std::shared_ptr<Message>& message);
/// Forward a message from client session to its respective child session.
bool forwardToChild(const std::string& viewId, const std::string& message);
@@ -496,9 +496,9 @@ private:
std::unique_lock<std::mutex> getDeferredLock() { return std::unique_lock<std::mutex>(_mutex, std::defer_lock); }
- void handleTileResponse(const std::vector<char>& payload);
+ void handleTileResponse(const std::shared_ptr<Message>& message);
void handleDialogPaintResponse(const std::vector<char>& payload, bool child);
- void handleTileCombinedResponse(const std::vector<char>& payload);
+ void handleTileCombinedResponse(const std::shared_ptr<Message>& message);
void handleDialogRequest(const std::string& dialogCmd);
/// Invoked to issue a save before renaming the document filename.
@@ -1388,7 +1388,7 @@ public:
void dispose() override;
/// Override to filter out the data that is returned by a command
- bool handleInput(const std::vector<char>& payload) override;
+ bool handleInput(const std::shared_ptr<Message>& message) override;
/// How many instances are running.
static std::size_t getInstanceCount();
diff --git a/wsd/TileCache.cpp b/wsd/TileCache.cpp
index b585203b23..ab6d753127 100644
--- a/wsd/TileCache.cpp
+++ b/wsd/TileCache.cpp
@@ -256,12 +256,13 @@ bool TileCache::getTextStream(StreamType type, const std::string& fileName, std:
return true;
}
-void TileCache::saveTextStream(StreamType type, const std::string& text, const std::string& fileName)
+void TileCache::saveTextStream(StreamType type, const std::string& fileName,
+ const std::vector<char>& data)
{
- LOG_INF("Saving '" << COOLProtocol::getAbbreviatedMessage(text.c_str(), text.size()) <<
- "' to " << fileName << " of size " << text.size() << " bytes");
+ LOG_INF("Saving '" << COOLProtocol::getAbbreviatedMessage(data.data(), data.size()) << "' to " << fileName
+ << " of size " << data.size() << " bytes");
- saveDataToStreamCache(type, fileName, text.c_str(), text.size());
+ saveDataToStreamCache(type, fileName, data.data(), data.size());
}
void TileCache::saveStream(StreamType type, const std::string& name, const char *data, std::size_t size)
diff --git a/wsd/TileCache.hpp b/wsd/TileCache.hpp
index 8d88abd2c5..48c769aaed 100644
--- a/wsd/TileCache.hpp
+++ b/wsd/TileCache.hpp
@@ -223,7 +223,7 @@ public:
bool getTextStream(StreamType type, const std::string& fileName, std::string& content);
// Save some text into a file in the cache directory
- void saveTextStream(StreamType type, const std::string& text, const std::string& fileName);
+ void saveTextStream(StreamType type, const std::string& fileName, const std::vector<char>& data);
// Saves a font / style / etc rendering
void saveStream(StreamType type, const std::string& name, const char* data, size_t size);