2015-04-13 04:09:02 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
2015-03-04 17:14:04 -06:00
|
|
|
/*
|
|
|
|
* 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/.
|
|
|
|
*/
|
|
|
|
|
2020-04-18 03:39:50 -05:00
|
|
|
#pragma once
|
2015-03-09 03:01:30 -05:00
|
|
|
|
2016-04-18 18:12:26 -05:00
|
|
|
#include <atomic>
|
2015-04-20 09:43:31 -05:00
|
|
|
#include <cassert>
|
2015-04-22 13:35:52 -05:00
|
|
|
#include <memory>
|
2015-05-07 08:29:36 -05:00
|
|
|
#include <mutex>
|
2020-01-02 15:11:54 -06:00
|
|
|
#include <map>
|
2015-04-20 09:43:31 -05:00
|
|
|
#include <ostream>
|
2020-01-02 15:11:54 -06:00
|
|
|
#include <type_traits>
|
2015-04-08 09:22:42 -05:00
|
|
|
|
2015-03-17 18:56:15 -05:00
|
|
|
#include <Poco/Buffer.h>
|
2015-04-16 11:15:40 -05:00
|
|
|
#include <Poco/Path.h>
|
2015-03-17 18:56:15 -05:00
|
|
|
#include <Poco/Types.h>
|
2015-03-04 17:14:04 -06:00
|
|
|
|
2016-11-24 08:56:06 -06:00
|
|
|
#include "Protocol.hpp"
|
2016-05-20 16:42:14 -05:00
|
|
|
#include "Log.hpp"
|
2016-10-29 20:15:00 -05:00
|
|
|
#include "MessageQueue.hpp"
|
2017-01-21 18:47:49 -06:00
|
|
|
#include "Message.hpp"
|
2016-10-29 20:15:00 -05:00
|
|
|
#include "TileCache.hpp"
|
2017-02-24 13:52:36 -06:00
|
|
|
#include "WebSocketHandler.hpp"
|
2015-03-12 18:34:42 -05:00
|
|
|
|
2020-01-02 15:11:54 -06:00
|
|
|
class Session;
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
class SessionMap : public std::map<std::string, std::shared_ptr<T> >
|
|
|
|
{
|
|
|
|
std::map<std::string, int> _canonicalIds;
|
|
|
|
public:
|
|
|
|
SessionMap() {
|
|
|
|
static_assert(std::is_base_of<Session, T>::value, "sessions must have base of Session");
|
|
|
|
}
|
2020-11-15 11:03:45 -06:00
|
|
|
|
2020-11-13 15:40:00 -06:00
|
|
|
/// Generate a unique key for this set of view properties, only used by WSD
|
|
|
|
int createCanonicalId(const std::string &viewProps)
|
2020-01-02 15:11:54 -06:00
|
|
|
{
|
|
|
|
if (viewProps.empty())
|
|
|
|
return 0;
|
2020-11-15 11:03:45 -06:00
|
|
|
for (const auto& it : _canonicalIds)
|
|
|
|
{
|
2020-01-02 15:11:54 -06:00
|
|
|
if (it.first == viewProps)
|
|
|
|
return it.second;
|
|
|
|
}
|
2020-11-15 11:03:45 -06:00
|
|
|
|
|
|
|
const std::size_t id = _canonicalIds.size() + 1;
|
2020-01-02 15:11:54 -06:00
|
|
|
_canonicalIds[viewProps] = id;
|
|
|
|
return id;
|
|
|
|
}
|
2020-11-15 11:03:45 -06:00
|
|
|
|
2020-11-13 15:40:00 -06:00
|
|
|
/// Lookup one session in the map that matches this canonical view id, only used by Kit
|
2020-01-02 15:11:54 -06:00
|
|
|
std::shared_ptr<T> findByCanonicalId(int id)
|
|
|
|
{
|
2020-07-14 09:20:05 -05:00
|
|
|
for (const auto &it : *this) {
|
2020-01-02 15:11:54 -06:00
|
|
|
if (it.second->getCanonicalViewId() == id)
|
|
|
|
return it.second;
|
|
|
|
}
|
|
|
|
return std::shared_ptr<T>();
|
|
|
|
}
|
2020-07-14 09:20:05 -05:00
|
|
|
void dumpState(std::ostream& oss)
|
|
|
|
{
|
|
|
|
for (const auto &it : *this) {
|
|
|
|
oss << "\tsession '" << it.first << "'\n";
|
|
|
|
it.second->dumpState(oss);
|
|
|
|
}
|
|
|
|
}
|
2020-01-02 15:11:54 -06:00
|
|
|
};
|
|
|
|
|
2017-03-16 13:32:12 -05:00
|
|
|
/// Base class of a WebSocket session.
|
2020-03-06 11:43:46 -06:00
|
|
|
class Session : public MessageHandlerInterface
|
2015-03-04 17:14:04 -06:00
|
|
|
{
|
2015-03-12 18:34:42 -05:00
|
|
|
public:
|
2015-12-27 21:47:39 -06:00
|
|
|
const std::string& getId() const { return _id; }
|
|
|
|
const std::string& getName() const { return _name; }
|
2016-01-21 08:26:34 -06:00
|
|
|
bool isDisconnected() const { return _disconnected; }
|
2015-12-27 21:47:39 -06:00
|
|
|
|
2020-07-27 04:27:00 -05:00
|
|
|
virtual void setReadOnly(bool bValue = true) { _isReadOnly = bValue; }
|
2017-03-30 09:59:59 -05:00
|
|
|
bool isReadOnly() const { return _isReadOnly; }
|
|
|
|
|
2020-07-27 04:27:00 -05:00
|
|
|
void setAllowChangeComments(bool bValue = true)
|
|
|
|
{
|
|
|
|
_isAllowChangeComments = bValue;
|
|
|
|
}
|
|
|
|
bool isAllowChangeComments() const { return _isAllowChangeComments; }
|
|
|
|
|
2020-03-06 11:43:46 -06:00
|
|
|
/// overridden to prepend client ids on messages by the Kit
|
2016-10-29 20:15:00 -05:00
|
|
|
virtual bool sendBinaryFrame(const char* buffer, int length);
|
|
|
|
virtual bool sendTextFrame(const char* buffer, const int length);
|
2020-03-06 11:43:46 -06:00
|
|
|
|
|
|
|
/// Get notified that the underlying transports disconnected
|
|
|
|
void onDisconnect() override { /* ignore */ }
|
|
|
|
|
|
|
|
bool hasQueuedMessages() const override
|
|
|
|
{
|
|
|
|
// queued in Socket output buffer
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// By default rely on the socket buffer.
|
|
|
|
void writeQueuedMessages() override
|
|
|
|
{
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sends a WebSocket Text message.
|
|
|
|
int sendMessage(const std::string& msg)
|
|
|
|
{
|
|
|
|
return sendTextFrame(msg.data(), msg.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: remove synonym - and clean from WebSocketHandler too ... (?)
|
2016-07-27 09:56:28 -05:00
|
|
|
bool sendTextFrame(const std::string& text)
|
|
|
|
{
|
|
|
|
return sendTextFrame(text.data(), text.size());
|
|
|
|
}
|
2015-04-20 09:43:31 -05:00
|
|
|
|
2017-03-29 19:38:41 -05:00
|
|
|
template <std::size_t N>
|
|
|
|
bool sendTextFrame(const char (&buffer)[N])
|
|
|
|
{
|
|
|
|
return (buffer != nullptr && N > 0 ? sendTextFrame(buffer, N) : false);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool sendTextFrame(const char* buffer)
|
|
|
|
{
|
|
|
|
return (buffer != nullptr ? sendTextFrame(buffer, std::strlen(buffer)) : false);
|
|
|
|
}
|
|
|
|
|
2020-03-30 04:59:20 -05:00
|
|
|
bool sendTextFrameAndLogError(const std::string& text)
|
|
|
|
{
|
|
|
|
LOG_ERR(text);
|
|
|
|
return sendTextFrame(text.data(), text.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool sendTextFrameAndLogError(const char* buffer)
|
|
|
|
{
|
|
|
|
LOG_ERR(buffer);
|
|
|
|
return (buffer != nullptr ? sendTextFrame(buffer, std::strlen(buffer)) : false);
|
|
|
|
}
|
|
|
|
|
2020-03-05 12:55:00 -06:00
|
|
|
virtual void handleMessage(const std::vector<char> &data) override;
|
2015-06-09 10:04:46 -05:00
|
|
|
|
2016-01-21 08:26:34 -06:00
|
|
|
/// Invoked when we want to disconnect a session.
|
2016-04-13 07:56:23 -05:00
|
|
|
virtual void disconnect();
|
2016-01-21 08:26:34 -06:00
|
|
|
|
2020-03-05 12:42:00 -06:00
|
|
|
/// clean & normal shutdown
|
2020-03-06 11:43:46 -06:00
|
|
|
void shutdownNormal(const std::string& statusMessage = "") { shutdown(false, statusMessage); }
|
2020-03-05 12:42:00 -06:00
|
|
|
|
|
|
|
/// abnormal / hash shutdown end-point going away
|
2020-03-06 11:43:46 -06:00
|
|
|
void shutdownGoingAway(const std::string& statusMessage = "") { shutdown(true, statusMessage); }
|
2016-05-16 21:48:33 -05:00
|
|
|
|
2016-04-21 00:14:27 -05:00
|
|
|
bool isActive() const { return _isActive; }
|
2016-04-21 04:36:47 -05:00
|
|
|
void setIsActive(bool active) { _isActive = active; }
|
2016-04-09 16:47:23 -05:00
|
|
|
|
|
|
|
/// Returns the inactivity time of the client in milliseconds.
|
|
|
|
double getInactivityMS() const
|
|
|
|
{
|
|
|
|
const auto duration = (std::chrono::steady_clock::now() - _lastActivityTime);
|
|
|
|
return std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
|
|
|
|
}
|
|
|
|
|
2016-04-18 18:12:26 -05:00
|
|
|
void closeFrame() { _isCloseFrame = true; };
|
|
|
|
bool isCloseFrame() const { return _isCloseFrame; }
|
|
|
|
|
2017-05-31 21:40:01 -05:00
|
|
|
void getIOStats(uint64_t &sent, uint64_t &recv);
|
|
|
|
|
2018-11-13 02:04:19 -06:00
|
|
|
void setUserId(const std::string& userId) { _userId = userId; }
|
|
|
|
|
|
|
|
const std::string& getUserId() const { return _userId; }
|
|
|
|
|
|
|
|
void setWatermarkText(const std::string& watermarkText) { _watermarkText = watermarkText; }
|
|
|
|
|
|
|
|
void setUserExtraInfo(const std::string& userExtraInfo) { _userExtraInfo = userExtraInfo; }
|
|
|
|
|
|
|
|
void setUserName(const std::string& userName) { _userName = userName; }
|
|
|
|
|
|
|
|
const std::string& getUserName() const {return _userName; }
|
|
|
|
|
|
|
|
const std::string& getUserNameAnonym() const { return _userNameAnonym; }
|
|
|
|
|
|
|
|
bool isDocPasswordProtected() const { return _isDocPasswordProtected; }
|
|
|
|
|
|
|
|
const std::string& getDocOptions() const { return _docOptions; }
|
|
|
|
|
2019-08-26 15:23:04 -05:00
|
|
|
bool hasWatermark() const { return !_watermarkText.empty() && _watermarkOpacity > 0.0; }
|
|
|
|
|
2018-11-13 02:04:19 -06:00
|
|
|
const std::string& getWatermarkText() const { return _watermarkText; }
|
|
|
|
|
2019-08-26 15:23:04 -05:00
|
|
|
double getWatermarkOpacity() const { return _watermarkOpacity; }
|
|
|
|
|
2018-11-13 02:04:19 -06:00
|
|
|
const std::string& getLang() const { return _lang; }
|
|
|
|
|
|
|
|
bool getHaveDocPassword() const { return _haveDocPassword; }
|
|
|
|
|
2020-09-10 09:04:36 -05:00
|
|
|
void setHaveDocPassword(const bool val) { _haveDocPassword = val; }
|
|
|
|
|
|
|
|
void setDocPassword(const std::string& password) { _docPassword = password; }
|
|
|
|
|
2018-11-13 02:04:19 -06:00
|
|
|
const std::string& getDocPassword() const { return _docPassword; }
|
|
|
|
|
|
|
|
const std::string& getUserExtraInfo() const { return _userExtraInfo; }
|
|
|
|
|
|
|
|
const std::string& getDocURL() const { return _docURL; }
|
|
|
|
|
|
|
|
const std::string& getJailedFilePath() const { return _jailedFilePath; }
|
|
|
|
|
|
|
|
const std::string& getJailedFilePathAnonym() const { return _jailedFilePathAnonym; }
|
|
|
|
|
2020-01-02 15:11:54 -06:00
|
|
|
int getCanonicalViewId() { return _canonicalViewId; }
|
2020-11-13 15:40:00 -06:00
|
|
|
// Only called by kit.
|
|
|
|
void setCanonicalViewId(int viewId) { _canonicalViewId = viewId; }
|
|
|
|
// Only called by wsd.
|
|
|
|
template<class T> void createCanonicalViewId(SessionMap<T> &map)
|
2020-01-02 15:11:54 -06:00
|
|
|
{
|
2020-11-13 15:40:00 -06:00
|
|
|
_canonicalViewId = map.createCanonicalId(_watermarkText);
|
2020-01-02 15:11:54 -06:00
|
|
|
}
|
2019-10-15 07:35:35 -05:00
|
|
|
|
2020-04-20 14:26:21 -05:00
|
|
|
const std::string& getDeviceFormFactor() const { return _deviceFormFactor; }
|
|
|
|
|
2020-10-14 04:16:47 -05:00
|
|
|
const std::string& getSpellOnline() const { return _spellOnline; }
|
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
protected:
|
2020-03-06 11:43:46 -06:00
|
|
|
Session(const std::shared_ptr<ProtocolHandlerInterface> &handler,
|
|
|
|
const std::string& name, const std::string& id, bool readonly);
|
2016-12-12 18:53:58 -06:00
|
|
|
virtual ~Session();
|
2015-03-12 18:34:42 -05:00
|
|
|
|
2018-06-10 12:53:39 -05:00
|
|
|
/// Parses the options of the "load" command,
|
|
|
|
/// shared between MasterProcessSession::loadDocument() and ChildProcessSession::loadDocument().
|
2020-02-28 03:50:58 -06:00
|
|
|
void parseDocOptions(const StringVector& tokens, int& part, std::string& timestamp, std::string& doctemplate);
|
2015-11-19 03:30:00 -06:00
|
|
|
|
2016-04-09 16:47:23 -05:00
|
|
|
void updateLastActivityTime()
|
|
|
|
{
|
|
|
|
_lastActivityTime = std::chrono::steady_clock::now();
|
|
|
|
}
|
|
|
|
|
2017-04-05 11:59:29 -05:00
|
|
|
void dumpState(std::ostream& os) override;
|
|
|
|
|
2016-05-29 11:03:26 -05:00
|
|
|
private:
|
2020-03-05 12:42:00 -06:00
|
|
|
|
2020-11-18 21:00:50 -06:00
|
|
|
void shutdown(bool goingAway = false, const std::string& statusMessage = std::string());
|
2020-03-05 12:42:00 -06:00
|
|
|
|
2016-10-29 20:15:00 -05:00
|
|
|
virtual bool _handleInput(const char* buffer, int length) = 0;
|
2016-08-21 11:56:35 -05:00
|
|
|
|
|
|
|
/// A session ID specific to an end-to-end connection (from user to lokit).
|
|
|
|
const std::string _id;
|
|
|
|
|
|
|
|
/// A readable name that identifies our peer and ID.
|
|
|
|
const std::string _name;
|
|
|
|
|
|
|
|
/// True if we have been disconnected.
|
2016-09-27 20:17:55 -05:00
|
|
|
std::atomic<bool> _disconnected;
|
2016-08-21 11:56:35 -05:00
|
|
|
/// True if the user is active, otherwise false (switched tabs).
|
2016-09-27 20:17:55 -05:00
|
|
|
std::atomic<bool> _isActive;
|
2016-08-21 11:56:35 -05:00
|
|
|
|
2017-09-11 12:59:38 -05:00
|
|
|
/// Time of the last interactive event being received
|
2016-08-21 11:56:35 -05:00
|
|
|
std::chrono::steady_clock::time_point _lastActivityTime;
|
|
|
|
|
|
|
|
// Whether websocket received close frame. Closing Handshake
|
|
|
|
std::atomic<bool> _isCloseFrame;
|
|
|
|
|
|
|
|
std::mutex _mutex;
|
|
|
|
|
2017-03-30 09:59:59 -05:00
|
|
|
/// Whether the session is opened as readonly
|
|
|
|
bool _isReadOnly;
|
|
|
|
|
2020-07-27 04:27:00 -05:00
|
|
|
/// If the session is read-only, are comments allowed
|
|
|
|
bool _isAllowChangeComments;
|
|
|
|
|
2017-03-24 06:34:32 -05:00
|
|
|
/// The actual URL, also in the child, even if the child never accesses that.
|
2015-04-20 09:43:31 -05:00
|
|
|
std::string _docURL;
|
2015-06-05 08:12:06 -05:00
|
|
|
|
2017-03-24 06:34:32 -05:00
|
|
|
/// The Jailed document path.
|
2016-01-06 23:33:54 -06:00
|
|
|
std::string _jailedFilePath;
|
|
|
|
|
2018-06-10 12:53:39 -05:00
|
|
|
/// The Jailed document path, anonymized for logging.
|
|
|
|
std::string _jailedFilePathAnonym;
|
|
|
|
|
2017-03-24 06:34:32 -05:00
|
|
|
/// Password provided, if any, to open the document
|
2016-02-04 11:35:26 -06:00
|
|
|
std::string _docPassword;
|
|
|
|
|
2017-03-24 06:34:32 -05:00
|
|
|
/// If password is provided or not
|
2016-04-27 19:51:54 -05:00
|
|
|
bool _haveDocPassword;
|
2016-02-04 11:35:26 -06:00
|
|
|
|
2017-03-24 06:34:32 -05:00
|
|
|
/// Whether document is password protected
|
2016-02-04 11:35:26 -06:00
|
|
|
bool _isDocPasswordProtected;
|
|
|
|
|
2015-11-18 11:09:13 -06:00
|
|
|
/// Document options: a JSON string, containing options (rendering, also possibly load in the future).
|
|
|
|
std::string _docOptions;
|
2016-08-29 13:08:01 -05:00
|
|
|
|
2018-06-10 12:53:39 -05:00
|
|
|
/// Id of the user to whom the session belongs to.
|
2016-10-26 09:35:40 -05:00
|
|
|
std::string _userId;
|
|
|
|
|
2018-06-10 12:53:39 -05:00
|
|
|
/// Id of the user to whom the session belongs to, anonymized for logging.
|
|
|
|
std::string _userIdAnonym;
|
|
|
|
|
|
|
|
/// Name of the user to whom the session belongs to.
|
2016-08-29 13:08:01 -05:00
|
|
|
std::string _userName;
|
2017-03-24 06:34:32 -05:00
|
|
|
|
2018-06-10 12:53:39 -05:00
|
|
|
/// Name of the user to whom the session belongs to, anonymized for logging.
|
|
|
|
std::string _userNameAnonym;
|
|
|
|
|
2017-05-28 11:20:49 -05:00
|
|
|
/// Extra info per user, mostly mail, avatar, links, etc.
|
|
|
|
std::string _userExtraInfo;
|
|
|
|
|
2017-09-04 08:40:04 -05:00
|
|
|
/// In case a watermark has to be rendered on each tile.
|
|
|
|
std::string _watermarkText;
|
|
|
|
|
2019-08-26 15:23:04 -05:00
|
|
|
/// Opacity in case a watermark has to be rendered on each tile.
|
|
|
|
double _watermarkOpacity;
|
|
|
|
|
2017-03-24 06:34:32 -05:00
|
|
|
/// Language for the document based on what the user has in the UI.
|
|
|
|
std::string _lang;
|
2019-10-15 07:35:35 -05:00
|
|
|
|
2020-01-02 15:11:54 -06:00
|
|
|
/// the canonical id unique to the set of rendering properties of this session
|
|
|
|
int _canonicalViewId;
|
2020-04-20 14:26:21 -05:00
|
|
|
|
|
|
|
/// The form factor of the device where the client is running: desktop, tablet, mobile.
|
|
|
|
std::string _deviceFormFactor;
|
2020-10-14 04:16:47 -05:00
|
|
|
|
|
|
|
/// The start value of Auto Spell Checking wheter it is enabled or disabled on start.
|
|
|
|
std::string _spellOnline;
|
2015-04-20 09:43:31 -05:00
|
|
|
};
|
2015-03-09 10:33:53 -05:00
|
|
|
|
2015-03-04 17:14:04 -06:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|