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/.
|
|
|
|
*/
|
|
|
|
|
2015-03-09 03:01:30 -05:00
|
|
|
#ifndef INCLUDED_LOOLSESSION_HPP
|
|
|
|
#define INCLUDED_LOOLSESSION_HPP
|
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
#include <cassert>
|
2015-04-08 09:22:42 -05:00
|
|
|
#include <map>
|
2015-04-20 09:43:31 -05:00
|
|
|
#include <ostream>
|
2015-04-08 09:22:42 -05:00
|
|
|
#include <set>
|
|
|
|
|
2015-03-04 17:14:04 -06:00
|
|
|
#define LOK_USE_UNSTABLE_API
|
|
|
|
#include <LibreOfficeKit/LibreOfficeKit.h>
|
|
|
|
|
|
|
|
#include <Poco/Net/WebSocket.h>
|
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
|
|
|
|
2015-03-12 18:34:42 -05:00
|
|
|
#include "TileCache.hpp"
|
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
// We have three kinds of Websocket sessions
|
|
|
|
// 1) Between the master loolwsd server to the end-user LOOL client
|
|
|
|
// 2) Between the master loolwsd server and a jailed loolwsd child process, in the master process
|
|
|
|
// 3) Ditto, in the jailed loolwsd process
|
2015-04-10 07:20:04 -05:00
|
|
|
|
2015-03-12 18:34:42 -05:00
|
|
|
class LOOLSession
|
2015-03-04 17:14:04 -06:00
|
|
|
{
|
2015-03-12 18:34:42 -05:00
|
|
|
public:
|
2015-04-20 09:43:31 -05:00
|
|
|
enum class Kind { ToClient, ToPrisoner, ToMaster };
|
|
|
|
|
|
|
|
void sendTextFrame(const std::string& text);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
LOOLSession(Poco::Net::WebSocket& ws, Kind kind);
|
|
|
|
virtual ~LOOLSession();
|
2015-03-12 18:34:42 -05:00
|
|
|
|
2015-04-13 07:13:38 -05:00
|
|
|
static const std::string jailDocumentURL;
|
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
const Kind _kind;
|
|
|
|
|
|
|
|
virtual bool handleInput(char *buffer, int length) = 0;
|
2015-03-12 18:34:42 -05:00
|
|
|
|
2015-03-04 17:14:04 -06:00
|
|
|
void sendBinaryFrame(const char *buffer, int length);
|
2015-03-12 18:34:42 -05:00
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
virtual bool loadDocument(const char *buffer, int length, Poco::StringTokenizer& tokens) = 0;
|
|
|
|
virtual bool getStatus(const char *buffer, int length) = 0;
|
2015-04-16 11:15:40 -05:00
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
virtual void sendTile(const char *buffer, int length, Poco::StringTokenizer& tokens) = 0;
|
2015-04-08 09:22:42 -05:00
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
// Fields common to sessions in master and jailed processes:
|
2015-04-16 11:15:40 -05:00
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
// In the master process, the websocket to the LOOL client or the jailed child process. In a
|
|
|
|
// jailed process, the websocket to the parent.
|
|
|
|
Poco::Net::WebSocket *_ws;
|
2015-03-04 17:14:04 -06:00
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
// In the master, the actual URL. In the child, the copy inside the chroot jail.
|
|
|
|
std::string _docURL;
|
2015-03-12 18:34:42 -05:00
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
// The id of the child process
|
|
|
|
Poco::UInt64 _childId;
|
2015-03-19 07:38:04 -05:00
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
std::unique_ptr<TileCache> _tileCache;
|
|
|
|
};
|
2015-03-09 10:33:53 -05:00
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
template<typename charT, typename traits>
|
|
|
|
inline std::basic_ostream<charT, traits> & operator <<(std::basic_ostream<charT, traits> & stream, LOOLSession::Kind kind)
|
|
|
|
{
|
|
|
|
switch (kind)
|
|
|
|
{
|
|
|
|
case LOOLSession::Kind::ToClient:
|
|
|
|
return stream << "TO_CLIENT";
|
|
|
|
case LOOLSession::Kind::ToPrisoner:
|
|
|
|
return stream << "TO_PRISONER";
|
|
|
|
case LOOLSession::Kind::ToMaster:
|
|
|
|
return stream << "TO_MASTER";
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
return stream << "UNK_" + std::to_string(static_cast<int>(kind));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MasterProcessSession final : public LOOLSession
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MasterProcessSession(Poco::Net::WebSocket& ws, Kind kind);
|
|
|
|
virtual ~MasterProcessSession();
|
2015-03-17 18:56:15 -05:00
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
virtual bool handleInput(char *buffer, int length) override;
|
2015-03-17 18:56:15 -05:00
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
bool haveSeparateProcess();
|
2015-03-12 18:34:42 -05:00
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
static Poco::Path getJailPath(Poco::UInt64 childId);
|
2015-03-17 18:56:15 -05:00
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
// Set up the chroot environment for one child process and start
|
|
|
|
// it, in advance of it being actually needed
|
|
|
|
static void preSpawn();
|
2015-04-08 09:22:42 -05:00
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
static std::map<Poco::Process::PID, Poco::UInt64> _childProcesses;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual bool loadDocument(const char *buffer, int length, Poco::StringTokenizer& tokens) override;
|
|
|
|
virtual bool getStatus(const char *buffer, int length);
|
2015-03-17 18:56:15 -05:00
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
virtual void sendTile(const char *buffer, int length, Poco::StringTokenizer& tokens);
|
|
|
|
|
|
|
|
void dispatchChild();
|
|
|
|
void forwardToPeer(const char *buffer, int length);
|
|
|
|
|
|
|
|
// If _kind==ToPrisoner and the child process has started and completed its handshake with the
|
|
|
|
// parent process: Points to the WebSocketSession for the child process handling the document in
|
|
|
|
// question, if any.
|
2015-03-17 18:56:15 -05:00
|
|
|
|
2015-04-14 09:50:38 -05:00
|
|
|
// In the session to the child process, points to the LOOLSession for the LOOL client. This will
|
2015-04-13 07:13:38 -05:00
|
|
|
// obvious have to be rethought when we add collaboration and there can be several LOOL clients
|
|
|
|
// per document being edited (i.e., per child process).
|
2015-04-20 09:43:31 -05:00
|
|
|
MasterProcessSession *_peer;
|
2015-04-14 09:50:38 -05:00
|
|
|
|
2015-04-08 09:22:42 -05:00
|
|
|
// Map from child ids to the corresponding session to the child
|
|
|
|
// process.
|
2015-04-20 09:43:31 -05:00
|
|
|
static std::map<Poco::UInt64, MasterProcessSession*> _childIdToChildSession;
|
2015-04-08 09:22:42 -05:00
|
|
|
|
2015-04-15 07:17:15 -05:00
|
|
|
// Pre-spawned child processes that haven't yet connected.
|
|
|
|
static std::set<Poco::UInt64> _pendingPreSpawnedChildren;
|
2015-03-17 18:56:15 -05:00
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
// Sessions to pre-spawned child processes that have connected but are not yet assigned a
|
2015-04-08 09:22:42 -05:00
|
|
|
// document to work on.
|
2015-04-20 09:43:31 -05:00
|
|
|
static std::set<MasterProcessSession*> _availableChildSessions;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ChildProcessSession final : public LOOLSession
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ChildProcessSession(Poco::Net::WebSocket& ws, LibreOfficeKit *loKit, Poco::UInt64 childId);
|
|
|
|
virtual ~ChildProcessSession();
|
|
|
|
|
|
|
|
virtual bool handleInput(char *buffer, int length) override;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual bool loadDocument(const char *buffer, int length, Poco::StringTokenizer& tokens) override;
|
|
|
|
virtual bool getStatus(const char *buffer, int length);
|
|
|
|
|
|
|
|
virtual void sendTile(const char *buffer, int length, Poco::StringTokenizer& tokens);
|
|
|
|
|
|
|
|
bool keyEvent(const char *buffer, int length, Poco::StringTokenizer& tokens);
|
|
|
|
bool mouseEvent(const char *buffer, int length, Poco::StringTokenizer& tokens);
|
|
|
|
bool unoCommand(const char *buffer, int length, Poco::StringTokenizer& tokens);
|
|
|
|
bool selectText(const char *buffer, int length, Poco::StringTokenizer& tokens);
|
|
|
|
bool selectGraphic(const char *buffer, int length, Poco::StringTokenizer& tokens);
|
|
|
|
bool resetSelection(const char *buffer, int length, Poco::StringTokenizer& tokens);
|
|
|
|
bool saveAs(const char *buffer, int length, Poco::StringTokenizer& tokens);
|
2015-03-17 18:56:15 -05:00
|
|
|
|
2015-04-08 09:22:42 -05:00
|
|
|
std::string _jail;
|
|
|
|
std::string _loSubPath;
|
2015-03-17 18:56:15 -05:00
|
|
|
LibreOfficeKit *_loKit;
|
|
|
|
LibreOfficeKitDocument *_loKitDocument;
|
2015-03-04 17:14:04 -06: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: */
|