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 06:27:47 -05:00
|
|
|
#include <list>
|
|
|
|
#include <utility>
|
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,
|
2019-05-29 10:26:16 -05:00
|
|
|
const bool isReadOnly,
|
|
|
|
const std::string& hostNoTrust);
|
|
|
|
void construct();
|
2016-05-16 18:05:22 -05:00
|
|
|
virtual ~ClientSession();
|
|
|
|
|
2019-05-29 10:26:16 -05:00
|
|
|
/// Lookup any session by id.
|
|
|
|
static std::shared_ptr<ClientSession> getById(const std::string &id);
|
|
|
|
|
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-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);
|
|
|
|
|
2019-05-29 10:26:16 -05:00
|
|
|
/// Integer id of the view in the kit process, or -1 if unknown
|
|
|
|
int getKitViewId() const { return _kitViewId; }
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-02-14 13:01:43 -06:00
|
|
|
bool sendTile(const std::string &header, const TileCache::Tile &tile)
|
|
|
|
{
|
|
|
|
// FIXME: performance - optimize away this copy ...
|
|
|
|
std::vector<char> output;
|
|
|
|
|
|
|
|
output.resize(header.size() + tile->size());
|
|
|
|
std::memcpy(output.data(), header.data(), header.size());
|
|
|
|
std::memcpy(output.data() + header.size(), tile->data(), tile->size());
|
|
|
|
|
|
|
|
return sendBinaryFrame(output.data(), output.size());
|
|
|
|
}
|
|
|
|
|
2016-12-13 18:20:05 -06:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-02-14 13:01:43 -06:00
|
|
|
|
2018-09-26 15:15:37 -05:00
|
|
|
void enqueueSendMessage(const std::shared_ptr<Message>& data);
|
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-09-28 12:30:57 -05:00
|
|
|
size_t countIdenticalTilesOnFly(const TileDesc& tile) const;
|
2018-06-07 06:13:36 -05:00
|
|
|
|
2018-07-05 07:40:28 -05:00
|
|
|
Util::Rectangle getVisibleArea() const { return _clientVisibleArea; }
|
2018-08-30 10:40:42 -05:00
|
|
|
/// Visible area can have negative value as position, but we have tiles only in the positive range
|
|
|
|
Util::Rectangle getNormalizedVisibleArea() const;
|
|
|
|
|
2018-07-05 07:40:28 -05:00
|
|
|
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-08-22 09:19:04 -05:00
|
|
|
/// Clear wireId map anytime when client visible area changes (visible area, zoom, part number)
|
|
|
|
void resetWireIdMap();
|
2018-09-21 08:49:30 -05:00
|
|
|
|
|
|
|
bool isTextDocument() const { return _isTextDocument; }
|
2019-05-29 10:26:16 -05:00
|
|
|
|
2019-06-21 06:35:17 -05:00
|
|
|
/// Do we recognize this clipboard ?
|
|
|
|
bool matchesClipboardKeys(const std::string &viewId, const std::string &tag);
|
2019-05-29 10:26:16 -05:00
|
|
|
|
2019-06-21 06:35:17 -05:00
|
|
|
/// Handle a clipboard fetch / put request.
|
2019-06-22 11:45:36 -05:00
|
|
|
void handleClipboardRequest(DocumentBroker::ClipboardRequest type,
|
|
|
|
const std::shared_ptr<StreamSocket> &socket,
|
|
|
|
const std::shared_ptr<std::string> &data);
|
2017-03-14 21:19:51 -05:00
|
|
|
|
2019-05-29 10:26:16 -05:00
|
|
|
/// Create URI for transient clipboard content.
|
2019-06-21 11:48:57 -05:00
|
|
|
std::string getClipboardURI(bool encode = true);
|
2019-06-21 06:35:17 -05:00
|
|
|
|
2019-07-04 10:47:03 -05:00
|
|
|
/// Adds and/or modified the copied payload before sending on to the client.
|
|
|
|
void postProcessCopyPayload(std::shared_ptr<Message> payload);
|
|
|
|
|
2019-06-21 06:35:17 -05:00
|
|
|
/// Generate and rotate a new clipboard hash, sending it if appropriate
|
2019-06-22 15:23:12 -05:00
|
|
|
void rotateClipboardKey(bool notifyClient);
|
2019-05-29 10:26:16 -05:00
|
|
|
|
2019-06-21 06:35:17 -05:00
|
|
|
private:
|
2017-03-14 21:19:51 -05:00
|
|
|
/// SocketHandler: disconnection event.
|
|
|
|
void onDisconnect() override;
|
2019-06-21 06:35:17 -05:00
|
|
|
|
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);
|
|
|
|
|
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
|
|
|
|
2019-05-29 10:26:16 -05:00
|
|
|
/// The integer id of the view in the Kit process
|
|
|
|
int _kitViewId;
|
|
|
|
|
|
|
|
/// Un-trusted hostname of our service from the client
|
|
|
|
const std::string _hostNoTrust;
|
|
|
|
|
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
|
|
|
|
2019-06-21 06:35:17 -05:00
|
|
|
/// Rotating clipboard remote access identifiers - protected by SessionMapMutex
|
|
|
|
std::string _clipboardKeys[2];
|
2019-05-29 10:26:16 -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 06:27:47 -05:00
|
|
|
std::list<std::pair<std::string, std::chrono::steady_clock::time_point>> _tilesOnFly;
|
2018-06-07 06:13:36 -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;
|
2019-05-29 10:26:16 -05:00
|
|
|
|
|
|
|
/// Sockets to send binary selection content to
|
|
|
|
std::vector<std::weak_ptr<StreamSocket>> _clipSockets;
|
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: */
|