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 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/.
|
|
|
|
*/
|
|
|
|
|
2016-12-12 18:53:58 -06:00
|
|
|
#ifndef INCLUDED_SESSION_HPP
|
|
|
|
#define INCLUDED_SESSION_HPP
|
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>
|
2015-04-20 09:43:31 -05:00
|
|
|
#include <ostream>
|
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>
|
|
|
|
#include <Poco/Process.h>
|
2015-03-04 17:14:04 -06:00
|
|
|
#include <Poco/StringTokenizer.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
|
|
|
|
2017-03-16 13:32:12 -05:00
|
|
|
/// Base class of a WebSocket session.
|
2017-02-24 13:52:36 -06:00
|
|
|
class Session : public WebSocketHandler
|
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
|
|
|
|
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);
|
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);
|
|
|
|
}
|
|
|
|
|
2017-02-24 13:52:36 -06:00
|
|
|
virtual void handleMessage(bool fin, WSOpCode code, 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
|
|
|
|
|
|
|
/// Called to handle disconnection command from socket.
|
2016-04-13 07:56:23 -05:00
|
|
|
virtual bool handleDisconnect();
|
2016-01-21 08:26:34 -06:00
|
|
|
|
2017-02-28 21:33:39 -06:00
|
|
|
void shutdown(const WebSocketHandler::StatusCodes statusCode = WebSocketHandler::StatusCodes::NORMAL_CLOSE,
|
2017-01-15 11:28:52 -06:00
|
|
|
const std::string& 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; }
|
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
protected:
|
2017-02-24 13:52:36 -06:00
|
|
|
Session(const std::string& name, const std::string& id);
|
2016-12-12 18:53:58 -06:00
|
|
|
virtual ~Session();
|
2015-03-12 18:34:42 -05:00
|
|
|
|
2015-11-19 03:30:00 -06:00
|
|
|
/// Parses the options of the "load" command, shared between MasterProcessSession::loadDocument() and ChildProcessSession::loadDocument().
|
2017-01-19 19:44:39 -06:00
|
|
|
void parseDocOptions(const std::vector<std::string>& tokens, int& part, std::string& timestamp);
|
2015-11-19 03:30:00 -06:00
|
|
|
|
2016-04-09 16:47:23 -05:00
|
|
|
void updateLastActivityTime()
|
|
|
|
{
|
|
|
|
_lastActivityTime = std::chrono::steady_clock::now();
|
|
|
|
}
|
|
|
|
|
2016-09-27 21:05:02 -05:00
|
|
|
/// Internal lock shared with derived classes.
|
|
|
|
std::unique_lock<std::mutex> getLock()
|
|
|
|
{
|
|
|
|
return std::unique_lock<std::mutex>(_mutex);
|
|
|
|
}
|
|
|
|
|
2016-05-29 11:03:26 -05:00
|
|
|
private:
|
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
|
|
|
|
|
|
|
private:
|
|
|
|
/// 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
|
|
|
|
|
|
|
std::chrono::steady_clock::time_point _lastActivityTime;
|
|
|
|
|
|
|
|
// Whether websocket received close frame. Closing Handshake
|
|
|
|
std::atomic<bool> _isCloseFrame;
|
|
|
|
|
|
|
|
std::mutex _mutex;
|
|
|
|
|
2016-05-29 11:03:26 -05:00
|
|
|
protected:
|
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;
|
|
|
|
|
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
|
|
|
|
2016-10-26 09:35:40 -05:00
|
|
|
/// Id of the user to whom the session belongs to
|
|
|
|
std::string _userId;
|
|
|
|
|
2016-08-29 13:08:01 -05:00
|
|
|
/// Name of the user to whom the session belongs to
|
|
|
|
std::string _userName;
|
2017-03-24 06:34:32 -05:00
|
|
|
|
|
|
|
/// Language for the document based on what the user has in the UI.
|
|
|
|
std::string _lang;
|
2015-04-20 09:43:31 -05:00
|
|
|
};
|
2015-03-09 10:33:53 -05:00
|
|
|
|
2015-03-09 03:01:30 -05:00
|
|
|
#endif
|
|
|
|
|
2015-03-04 17:14:04 -06:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|