2016-05-16 06:37:02 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCLUDED_CLIENTSSESSION_HPP
|
|
|
|
#define INCLUDED_CLIENTSSESSION_HPP
|
|
|
|
|
2016-11-24 08:56:06 -06:00
|
|
|
#include "Session.hpp"
|
2016-12-13 03:13:58 -06:00
|
|
|
#include "Storage.hpp"
|
2016-05-16 06:37:02 -05:00
|
|
|
#include "MessageQueue.hpp"
|
2016-12-13 18:20:05 -06:00
|
|
|
#include "SenderQueue.hpp"
|
2016-05-16 06:37:02 -05:00
|
|
|
|
2016-10-16 11:40:52 -05:00
|
|
|
#include <Poco/URI.h>
|
|
|
|
|
2016-05-16 06:37:02 -05:00
|
|
|
class DocumentBroker;
|
|
|
|
|
2016-09-16 01:56:35 -05:00
|
|
|
/// Represents a session to a LOOL client, in the WSD process.
|
2016-12-12 18:53:58 -06:00
|
|
|
class ClientSession final : public Session, public std::enable_shared_from_this<ClientSession>
|
2016-05-16 06:37:02 -05:00
|
|
|
{
|
|
|
|
public:
|
2016-05-16 19:49:36 -05:00
|
|
|
ClientSession(const std::string& id,
|
2016-10-16 12:20:49 -05:00
|
|
|
const std::shared_ptr<DocumentBroker>& docBroker,
|
2016-10-16 11:40:52 -05:00
|
|
|
const Poco::URI& uriPublic,
|
|
|
|
const bool isReadOnly = false);
|
2016-05-16 06:37:02 -05:00
|
|
|
|
2016-05-16 18:05:22 -05:00
|
|
|
virtual ~ClientSession();
|
|
|
|
|
2016-10-19 09:52:53 -05:00
|
|
|
void setReadOnly();
|
2016-07-10 23:50:25 -05:00
|
|
|
bool isReadOnly() const { return _isReadOnly; }
|
2016-05-16 17:22:41 -05:00
|
|
|
|
2017-01-21 21:32:00 -06:00
|
|
|
/// Returns true if a document is loaded (i.e. we got status message).
|
|
|
|
bool isLoaded() const { return _isLoaded; }
|
2017-01-11 15:45:14 -06:00
|
|
|
|
2016-11-08 07:37:28 -06:00
|
|
|
const std::string getUserId() const { return _userId; }
|
2016-10-26 09:35:40 -05:00
|
|
|
void setUserId(const std::string& userId) { _userId = userId; }
|
2016-08-29 13:08:01 -05:00
|
|
|
void setUserName(const std::string& userName) { _userName = userName; }
|
2016-11-23 00:01:39 -06:00
|
|
|
void setDocumentOwner(const bool documentOwner) { _isDocumentOwner = documentOwner; }
|
2016-11-22 11:40:10 -06:00
|
|
|
bool isDocumentOwner() const { return _isDocumentOwner; }
|
2016-08-29 13:08:01 -05:00
|
|
|
|
2017-01-21 19:31:22 -06:00
|
|
|
/// Handle kit-to-client message.
|
|
|
|
bool handleKitToClientMessage(const char* data, const int size);
|
|
|
|
|
2016-12-13 18:20:05 -06:00
|
|
|
using Session::sendTextFrame;
|
|
|
|
|
|
|
|
bool sendBinaryFrame(const char* buffer, int length) override
|
|
|
|
{
|
2017-01-21 22:50:08 -06:00
|
|
|
auto payload = std::make_shared<Message>(buffer, length, Message::Dir::Out);
|
2016-12-13 18:20:05 -06:00
|
|
|
enqueueSendMessage(payload);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool sendTextFrame(const char* buffer, const int length) override
|
|
|
|
{
|
2017-01-21 22:50:08 -06:00
|
|
|
auto payload = std::make_shared<Message>(buffer, length, Message::Dir::Out);
|
2016-12-13 18:20:05 -06:00
|
|
|
enqueueSendMessage(payload);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-21 18:51:02 -06:00
|
|
|
void enqueueSendMessage(const std::shared_ptr<Message>& data)
|
2016-12-13 18:20:05 -06:00
|
|
|
{
|
2017-01-21 19:59:45 -06:00
|
|
|
if (isHeadless())
|
|
|
|
{
|
|
|
|
// Fail silently and return as there is no actual websocket
|
|
|
|
// connection in this case.
|
|
|
|
LOG_INF(getName() << ": Headless peer, not forwarding message [" << data->abbr() << "].");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-06 19:10:08 -06:00
|
|
|
LOG_TRC(getName() << " enqueueing client message " << data->id());
|
2017-01-21 19:59:45 -06:00
|
|
|
_senderQueue.enqueue(data);
|
|
|
|
}
|
2016-12-13 18:20:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
bool stopping() const { return _stop || _senderQueue.stopping(); }
|
|
|
|
void stop()
|
|
|
|
{
|
|
|
|
_stop = true;
|
|
|
|
_senderQueue.stop();
|
|
|
|
}
|
|
|
|
|
2016-05-16 18:05:22 -05:00
|
|
|
/**
|
|
|
|
* Return the URL of the saved-as document when it's ready. If called
|
|
|
|
* before it's ready, the call blocks till then.
|
|
|
|
*/
|
2016-10-09 10:59:00 -05:00
|
|
|
std::string getSaveAsUrl(const unsigned timeoutMs)
|
2016-05-16 18:05:22 -05:00
|
|
|
{
|
2016-10-09 10:59:00 -05:00
|
|
|
const auto payload = _saveAsQueue.get(timeoutMs);
|
2017-01-19 22:24:23 -06:00
|
|
|
if (payload.empty())
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Timed-out while getting save-as URL.");
|
|
|
|
}
|
|
|
|
|
2016-05-16 18:05:22 -05:00
|
|
|
return std::string(payload.data(), payload.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void setSaveAsUrl(const std::string& url)
|
|
|
|
{
|
|
|
|
_saveAsQueue.put(url);
|
|
|
|
}
|
|
|
|
|
2016-10-16 12:20:49 -05:00
|
|
|
std::shared_ptr<DocumentBroker> getDocumentBroker() const { return _docBroker.lock(); }
|
2016-05-16 20:03:45 -05:00
|
|
|
|
2016-10-16 11:40:52 -05:00
|
|
|
/// Exact URI (including query params - access tokens etc.) with which
|
|
|
|
/// client made the request to us
|
|
|
|
const Poco::URI& getPublicUri() const { return _uriPublic; }
|
|
|
|
|
2016-12-13 03:13:58 -06:00
|
|
|
/// Set WOPI fileinfo object
|
|
|
|
void setWopiFileInfo(std::unique_ptr<WopiStorage::WOPIFileInfo>& wopiFileInfo) { _wopiFileInfo = std::move(wopiFileInfo); }
|
|
|
|
|
2017-03-06 10:26:52 -06:00
|
|
|
bool hasQueuedWrites() const override;
|
|
|
|
void performWrites() override;
|
|
|
|
|
2016-05-16 18:05:22 -05:00
|
|
|
private:
|
2016-10-29 20:15:00 -05:00
|
|
|
virtual bool _handleInput(const char* buffer, int length) override;
|
2016-05-16 18:05:22 -05:00
|
|
|
|
2017-01-19 19:44:39 -06:00
|
|
|
bool loadDocument(const char* buffer, int length, const std::vector<std::string>& tokens,
|
2016-10-16 12:20:49 -05:00
|
|
|
const std::shared_ptr<DocumentBroker>& docBroker);
|
2016-10-29 20:15:00 -05:00
|
|
|
bool getStatus(const char* buffer, int length,
|
2016-10-16 12:20:49 -05:00
|
|
|
const std::shared_ptr<DocumentBroker>& docBroker);
|
2017-01-19 19:44:39 -06:00
|
|
|
bool getCommandValues(const char* buffer, int length, const std::vector<std::string>& tokens,
|
2016-10-16 12:20:49 -05:00
|
|
|
const std::shared_ptr<DocumentBroker>& docBroker);
|
2017-01-19 19:44:39 -06:00
|
|
|
bool sendTile(const char* buffer, int length, const std::vector<std::string>& tokens,
|
2016-10-16 12:20:49 -05:00
|
|
|
const std::shared_ptr<DocumentBroker>& docBroker);
|
2017-01-19 19:44:39 -06:00
|
|
|
bool sendCombinedTiles(const char* buffer, int length, const std::vector<std::string>& tokens,
|
2016-10-16 12:20:49 -05:00
|
|
|
const std::shared_ptr<DocumentBroker>& docBroker);
|
2016-05-16 18:20:35 -05:00
|
|
|
|
2017-01-19 19:44:39 -06:00
|
|
|
bool sendFontRendering(const char* buffer, int length, const std::vector<std::string>& tokens,
|
2016-10-16 12:20:49 -05:00
|
|
|
const std::shared_ptr<DocumentBroker>& docBroker);
|
|
|
|
|
2016-10-16 12:43:44 -05:00
|
|
|
bool forwardToChild(const std::string& message,
|
2016-10-16 12:20:49 -05:00
|
|
|
const std::shared_ptr<DocumentBroker>& docBroker);
|
2016-10-08 13:25:27 -05:00
|
|
|
|
2017-01-21 20:38:11 -06:00
|
|
|
bool forwardToClient(const std::shared_ptr<Message>& payload);
|
|
|
|
|
2016-12-05 06:18:33 -06:00
|
|
|
/// Returns true if given message from the client should be allowed or not
|
|
|
|
/// Eg. in readonly mode only few messages should be allowed
|
|
|
|
bool filterMessage(const std::string& msg) const;
|
|
|
|
|
2016-05-16 06:37:02 -05:00
|
|
|
private:
|
2016-10-16 12:20:49 -05:00
|
|
|
std::weak_ptr<DocumentBroker> _docBroker;
|
2016-05-16 20:03:45 -05:00
|
|
|
|
2016-10-16 11:40:52 -05:00
|
|
|
/// URI with which client made request to us
|
|
|
|
const Poco::URI _uriPublic;
|
|
|
|
|
|
|
|
/// Whether the session is opened as readonly
|
2016-07-10 23:50:25 -05:00
|
|
|
bool _isReadOnly;
|
|
|
|
|
2016-11-08 07:37:28 -06:00
|
|
|
/// Whether this session is the owner of currently opened document
|
|
|
|
bool _isDocumentOwner;
|
|
|
|
|
2016-05-16 18:05:22 -05:00
|
|
|
/// Store URLs of completed 'save as' documents.
|
|
|
|
MessageQueue _saveAsQueue;
|
2016-05-16 06:37:02 -05:00
|
|
|
|
2017-01-21 21:32:00 -06:00
|
|
|
bool _isLoaded;
|
2017-01-11 15:45:14 -06:00
|
|
|
|
2016-12-13 03:13:58 -06:00
|
|
|
/// Wopi FileInfo object
|
|
|
|
std::unique_ptr<WopiStorage::WOPIFileInfo> _wopiFileInfo;
|
2016-12-13 18:20:05 -06:00
|
|
|
|
2017-01-21 18:51:02 -06:00
|
|
|
SenderQueue<std::shared_ptr<Message>> _senderQueue;
|
2016-12-13 18:20:05 -06:00
|
|
|
std::atomic<bool> _stop;
|
2016-05-16 06:37:02 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|