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"
|
2017-03-15 07:07:17 -05:00
|
|
|
#include "DocumentBroker.hpp"
|
2016-10-16 11:40:52 -05:00
|
|
|
#include <Poco/URI.h>
|
2018-05-30 12:35:13 -05:00
|
|
|
#include <Rectangle.hpp>
|
2018-08-03 07:25:21 -05:00
|
|
|
#include <deque>
|
2018-07-06 05:38:31 -05:00
|
|
|
#include <map>
|
2018-08-23 05:46:49 -05:00
|
|
|
#include <unordered_map>
|
2018-07-31 07:47:27 -05:00
|
|
|
#include <unordered_set>
|
2016-10-16 11:40:52 -05:00
|
|
|
|
2016-05-16 06:37:02 -05:00
|
|
|
class DocumentBroker;
|
|
|
|
|
2018-06-07 06:13:36 -05:00
|
|
|
|
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,
|
2018-07-12 07:50:04 -05:00
|
|
|
const bool isReadOnly = false);
|
2016-05-16 06:37:02 -05:00
|
|
|
|
2016-05-16 18:05:22 -05:00
|
|
|
virtual ~ClientSession();
|
|
|
|
|
2017-05-05 05:51:43 -05:00
|
|
|
void handleIncomingMessage(SocketDisposition &) override;
|
2017-03-16 13:32:12 -05:00
|
|
|
|
2017-03-30 09:59:59 -05:00
|
|
|
void setReadOnly() override;
|
2016-05-16 17:22:41 -05:00
|
|
|
|
2017-04-19 23:08:13 -05:00
|
|
|
/// Returns true if this session is added to a DocBroker.
|
2017-03-29 19:39:44 -05:00
|
|
|
bool isAttached() const { return _isAttached; }
|
|
|
|
void setAttached() { _isAttached = true; }
|
2017-01-11 15:45:14 -06:00
|
|
|
|
2017-04-19 23:08:13 -05:00
|
|
|
/// Returns true if this session has loaded a view (i.e. we got status message).
|
2017-04-20 21:17:08 -05:00
|
|
|
bool isViewLoaded() const { return _isViewLoaded; }
|
|
|
|
void setViewLoaded() { _isViewLoaded = true; }
|
2017-04-19 23:08:13 -05:00
|
|
|
|
2016-11-08 07:37:28 -06:00
|
|
|
const std::string getUserId() const { return _userId; }
|
2017-04-07 09:19:04 -05:00
|
|
|
const std::string getUserName() const {return _userName; }
|
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; }
|
2017-05-28 11:20:49 -05:00
|
|
|
void setUserExtraInfo(const std::string& userExtraInfo) { _userExtraInfo = userExtraInfo; }
|
2017-09-04 08:40:04 -05:00
|
|
|
void setWatermarkText(const std::string& watermarkText) { _watermarkText = watermarkText; }
|
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);
|
|
|
|
|
2017-03-29 19:38:41 -05:00
|
|
|
// sendTextFrame that takes std::string and string literal.
|
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
|
|
|
{
|
2018-02-07 03:17:59 -06:00
|
|
|
const std::shared_ptr<DocumentBroker> docBroker = _docBroker.lock();
|
2017-03-15 07:07:17 -05:00
|
|
|
// If in the correct thread - no need for wakeups.
|
2017-04-05 07:48:49 -05:00
|
|
|
if (docBroker)
|
|
|
|
docBroker->assertCorrectThread();
|
2017-03-15 07:07:17 -05:00
|
|
|
|
2017-03-26 22:04:27 -05:00
|
|
|
LOG_TRC(getName() << " enqueueing client message " << data->id());
|
2018-07-23 09:11:47 -05:00
|
|
|
size_t sizeBefore = _senderQueue.size();
|
|
|
|
size_t newSize = _senderQueue.enqueue(data);
|
2018-08-22 16:20:27 -05:00
|
|
|
|
|
|
|
// Track sent tile
|
|
|
|
const std::string command = data->firstToken();
|
|
|
|
if (command == "tile:")
|
2018-07-23 09:11:47 -05:00
|
|
|
{
|
2018-08-22 16:20:27 -05:00
|
|
|
const TileDesc tile = TileDesc::parse(data->firstLine());
|
|
|
|
traceTileBySend(tile, sizeBefore == newSize);
|
2018-07-23 09:11:47 -05:00
|
|
|
}
|
2016-12-13 18:20:05 -06:00
|
|
|
}
|
|
|
|
|
2017-03-18 15:36:32 -05:00
|
|
|
/// Set the save-as socket which is used to send convert-to results.
|
2017-03-16 21:42:02 -05:00
|
|
|
void setSaveAsSocket(const std::shared_ptr<StreamSocket>& socket)
|
2016-05-16 18:05:22 -05:00
|
|
|
{
|
2017-03-16 21:42:02 -05:00
|
|
|
_saveAsSocket = socket;
|
2016-05-16 18:05:22 -05:00
|
|
|
}
|
|
|
|
|
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
|
2017-05-12 10:42:03 -05:00
|
|
|
///
|
|
|
|
/// Note: This URI is unsafe - when connecting to existing sessions, we must
|
|
|
|
/// ignore everything but the access_token, and use the access_token with
|
|
|
|
/// the URI of the initial request.
|
2016-10-16 11:40:52 -05:00
|
|
|
const Poco::URI& getPublicUri() const { return _uriPublic; }
|
|
|
|
|
2017-05-12 10:42:03 -05:00
|
|
|
/// The access token of this session.
|
2017-08-16 09:38:00 -05:00
|
|
|
Authorization getAuthorization() const;
|
2017-05-12 10:42:03 -05:00
|
|
|
|
2016-12-13 03:13:58 -06:00
|
|
|
/// Set WOPI fileinfo object
|
|
|
|
void setWopiFileInfo(std::unique_ptr<WopiStorage::WOPIFileInfo>& wopiFileInfo) { _wopiFileInfo = std::move(wopiFileInfo); }
|
|
|
|
|
2018-07-10 07:23:20 -05:00
|
|
|
/// Get requested tiles waiting for sending to the client
|
2018-08-03 07:25:21 -05:00
|
|
|
std::deque<TileDesc>& getRequestedTiles() { return _requestedTiles; }
|
2018-06-07 06:13:36 -05:00
|
|
|
|
2018-07-10 07:23:20 -05:00
|
|
|
/// Mark a new tile as sent
|
2018-07-10 07:05:36 -05:00
|
|
|
void addTileOnFly(const TileDesc& tile);
|
|
|
|
void clearTilesOnFly();
|
|
|
|
size_t getTilesOnFlyCount() const { return _tilesOnFly.size(); }
|
2018-08-23 05:46:49 -05:00
|
|
|
void removeOutdatedTilesOnFly();
|
2018-06-07 06:13:36 -05:00
|
|
|
|
2018-07-05 07:40:28 -05:00
|
|
|
Util::Rectangle getVisibleArea() const { return _clientVisibleArea; }
|
|
|
|
int getTileWidthInTwips() const { return _tileWidthTwips; }
|
|
|
|
int getTileHeightInTwips() const { return _tileHeightTwips; }
|
|
|
|
|
2018-07-06 05:38:31 -05:00
|
|
|
/// This method updates internal data related to sent tiles (wireID and tiles-on-fly)
|
|
|
|
/// Call this method anytime when a new tile is sent to the client
|
2018-08-22 16:20:27 -05:00
|
|
|
void traceTileBySend(const TileDesc& tile, bool deduplicated = false);
|
2018-07-23 09:09:55 -05:00
|
|
|
|
2018-07-31 07:47:27 -05:00
|
|
|
/// Trask tiles what we a subscription to
|
|
|
|
void traceSubscribeToTile(const std::string& tileCacheName);
|
|
|
|
void traceUnSubscribeToTile(const std::string& tileCacheName);
|
|
|
|
void removeOutdatedTileSubscriptions();
|
|
|
|
void clearTileSubscription();
|
2018-07-23 09:09:55 -05:00
|
|
|
|
2018-07-31 07:47:27 -05:00
|
|
|
size_t getTilesBeingRenderedCount() const {return _tilesBeingRendered.size();}
|
2018-08-22 09:19:04 -05:00
|
|
|
|
|
|
|
/// Clear wireId map anytime when client visible area changes (visible area, zoom, part number)
|
|
|
|
void resetWireIdMap();
|
2017-03-14 21:19:51 -05:00
|
|
|
private:
|
|
|
|
|
|
|
|
/// SocketHandler: disconnection event.
|
|
|
|
void onDisconnect() override;
|
2017-03-17 16:59:09 -05:00
|
|
|
/// Does SocketHandler: have data or timeouts to setup.
|
|
|
|
int getPollEvents(std::chrono::steady_clock::time_point /* now */,
|
|
|
|
int & /* timeoutMaxMs */) override;
|
2017-03-14 21:19:51 -05:00
|
|
|
/// SocketHandler: write to socket.
|
2017-03-06 10:26:52 -06:00
|
|
|
void performWrites() override;
|
|
|
|
|
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;
|
|
|
|
|
2017-03-18 09:59:09 -05:00
|
|
|
void dumpState(std::ostream& os) override;
|
|
|
|
|
2018-07-10 07:23:20 -05:00
|
|
|
/// Handle invalidation message comming from a kit and transfer it to a tile request.
|
2018-05-31 13:20:09 -05:00
|
|
|
void handleTileInvalidation(const std::string& message,
|
|
|
|
const std::shared_ptr<DocumentBroker>& docBroker);
|
|
|
|
|
2018-07-10 07:23:20 -05:00
|
|
|
/// Generate a unique id for a tile
|
2018-07-06 05:38:31 -05:00
|
|
|
std::string generateTileID(const TileDesc& tile);
|
|
|
|
|
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;
|
|
|
|
|
2016-11-08 07:37:28 -06:00
|
|
|
/// Whether this session is the owner of currently opened document
|
|
|
|
bool _isDocumentOwner;
|
|
|
|
|
2017-03-16 21:42:02 -05:00
|
|
|
/// The socket to which the converted (saveas) doc is sent.
|
|
|
|
std::shared_ptr<StreamSocket> _saveAsSocket;
|
2016-05-16 06:37:02 -05:00
|
|
|
|
2017-04-19 23:08:13 -05:00
|
|
|
/// If we are added to a DocBroker.
|
2017-03-29 19:39:44 -05:00
|
|
|
bool _isAttached;
|
2017-01-11 15:45:14 -06:00
|
|
|
|
2017-04-19 23:08:13 -05:00
|
|
|
/// If we have loaded a view.
|
2017-04-20 21:17:08 -05:00
|
|
|
bool _isViewLoaded;
|
2017-04-19 23:08:13 -05: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-09-11 12:59:38 -05:00
|
|
|
/// Count of key-strokes
|
|
|
|
uint64_t _keyEvents;
|
|
|
|
|
2017-01-21 18:51:02 -06:00
|
|
|
SenderQueue<std::shared_ptr<Message>> _senderQueue;
|
2018-05-30 12:35:13 -05:00
|
|
|
|
|
|
|
/// Visible area of the client
|
|
|
|
Util::Rectangle _clientVisibleArea;
|
|
|
|
|
|
|
|
/// Selected part of the document viewed by the client (no parts in Writer)
|
|
|
|
int _clientSelectedPart;
|
|
|
|
|
|
|
|
/// Zoom properties of the client
|
|
|
|
int _tileWidthPixel;
|
|
|
|
int _tileHeightPixel;
|
|
|
|
int _tileWidthTwips;
|
|
|
|
int _tileHeightTwips;
|
2018-05-31 13:20:09 -05:00
|
|
|
|
2018-07-06 05:38:31 -05:00
|
|
|
/// Client is using a text document?
|
2018-06-19 09:15:37 -05:00
|
|
|
bool _isTextDocument;
|
2018-06-07 06:13:36 -05:00
|
|
|
|
2018-07-10 07:23:20 -05:00
|
|
|
/// TileID's of the sent tiles. Push by sending and pop by tileprocessed message from the client.
|
2018-08-23 05:46:49 -05:00
|
|
|
std::unordered_map<std::string, std::chrono::steady_clock::time_point> _tilesOnFly;
|
2018-06-07 06:13:36 -05:00
|
|
|
|
2018-07-31 07:47:27 -05:00
|
|
|
/// Names of tiles requested from kit, which this session is subsrcibed to
|
2018-07-23 09:09:55 -05:00
|
|
|
/// Track only non-thumbnail tiles (getId() == -1)
|
2018-07-31 07:47:27 -05:00
|
|
|
std::unordered_set<std::string> _tilesBeingRendered;
|
2018-07-23 09:09:55 -05:00
|
|
|
|
2018-07-10 07:23:20 -05:00
|
|
|
/// Requested tiles are stored in this list, before we can send them to the client
|
2018-08-03 07:25:21 -05:00
|
|
|
std::deque<TileDesc> _requestedTiles;
|
2018-07-06 05:38:31 -05:00
|
|
|
|
2018-07-10 07:23:20 -05:00
|
|
|
/// Store wireID's of the sent tiles inside the actual visible area
|
2018-07-06 05:38:31 -05:00
|
|
|
std::map<std::string, TileWireId> _oldWireIds;
|
2016-05-16 06:37:02 -05:00
|
|
|
};
|
|
|
|
|
2018-06-07 06:13:36 -05:00
|
|
|
|
2016-05-16 06:37:02 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|