2016-03-10 20:42:33 -06: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/.
|
|
|
|
*/
|
|
|
|
|
2016-03-12 18:29:17 -06:00
|
|
|
#ifndef INCLUDED_DOCUMENTBROKER_HPP
|
|
|
|
#define INCLUDED_DOCUMENTBROKER_HPP
|
2016-03-10 20:42:33 -06:00
|
|
|
|
2016-08-30 02:06:39 -05:00
|
|
|
#include <csignal>
|
2016-04-03 20:40:14 -05:00
|
|
|
|
2016-03-10 20:42:33 -06:00
|
|
|
#include <atomic>
|
2016-04-10 21:07:09 -05:00
|
|
|
#include <chrono>
|
2016-05-16 22:02:05 -05:00
|
|
|
#include <condition_variable>
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
2016-03-10 20:42:33 -06:00
|
|
|
#include <mutex>
|
|
|
|
#include <string>
|
2016-05-02 06:21:30 -05:00
|
|
|
#include <thread>
|
2016-03-10 20:42:33 -06:00
|
|
|
|
2016-03-25 21:56:18 -05:00
|
|
|
#include <Poco/URI.h>
|
2016-04-24 10:56:02 -05:00
|
|
|
#include <Poco/Net/WebSocket.h>
|
2016-03-25 21:56:18 -05:00
|
|
|
|
2016-04-05 21:37:29 -05:00
|
|
|
#include "IoUtil.hpp"
|
loolwsd: include cleanup and organization
A source file (.cpp) must include its own header first.
This insures that the header is self-contained and
doesn't depend on arbitrary (and accidental) includes
before it to compile.
Furthermore, system headers should go next, followed by
C then C++ headers, then libraries (Poco, etc) and, finally,
project headers come last.
This makes sure that headers and included in the same dependency
order to avoid side-effects. For example, Poco should never rely on
anything from our project in the same way that a C header should
never rely on anything in C++, Poco, or project headers.
Also, includes ought to be sorted where possible, to improve
readability and avoid accidental duplicates (of which there
were a few).
Change-Id: I62cc1343e4a091d69195e37ed659dba20cfcb1ef
Reviewed-on: https://gerrit.libreoffice.org/25262
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
2016-05-21 09:23:07 -05:00
|
|
|
#include "Log.hpp"
|
2016-07-19 12:10:14 -05:00
|
|
|
#include "Storage.hpp"
|
2016-05-16 22:02:05 -05:00
|
|
|
#include "TileCache.hpp"
|
2016-03-23 11:55:28 -05:00
|
|
|
#include "Util.hpp"
|
2016-03-25 21:56:18 -05:00
|
|
|
|
|
|
|
// Forwards.
|
2016-05-16 19:49:36 -05:00
|
|
|
class DocumentBroker;
|
2016-03-10 20:42:33 -06:00
|
|
|
|
2016-04-03 20:40:14 -05:00
|
|
|
/// Represents a new LOK child that is read
|
|
|
|
/// to host a document.
|
|
|
|
class ChildProcess
|
|
|
|
{
|
|
|
|
public:
|
2016-04-29 05:06:45 -05:00
|
|
|
/// @param pid is the process ID of the child.
|
|
|
|
/// @param ws is the control WebSocket to the child.
|
2016-04-03 20:40:14 -05:00
|
|
|
ChildProcess(const Poco::Process::PID pid, const std::shared_ptr<Poco::Net::WebSocket>& ws) :
|
|
|
|
_pid(pid),
|
2016-05-02 06:21:30 -05:00
|
|
|
_ws(ws),
|
|
|
|
_stop(false)
|
2016-04-03 20:40:14 -05:00
|
|
|
{
|
2016-05-02 06:21:30 -05:00
|
|
|
_thread = std::thread([this]() { this->socketProcessor(); });
|
2016-04-03 20:40:14 -05:00
|
|
|
Log::info("ChildProcess ctor [" + std::to_string(_pid) + "].");
|
|
|
|
}
|
|
|
|
|
2016-05-01 19:30:52 -05:00
|
|
|
ChildProcess(ChildProcess&& other) = delete;
|
2016-04-03 20:40:14 -05:00
|
|
|
|
2016-05-01 19:30:52 -05:00
|
|
|
const ChildProcess& operator=(ChildProcess&& other) = delete;
|
2016-04-03 20:40:14 -05:00
|
|
|
|
|
|
|
~ChildProcess()
|
|
|
|
{
|
2016-04-17 11:04:23 -05:00
|
|
|
if (_pid > 0)
|
|
|
|
{
|
|
|
|
Log::info("~ChildProcess dtor [" + std::to_string(_pid) + "].");
|
|
|
|
close(false);
|
|
|
|
}
|
2016-04-03 20:40:14 -05:00
|
|
|
}
|
|
|
|
|
2016-05-02 06:21:30 -05:00
|
|
|
void setDocumentBroker(const std::shared_ptr<DocumentBroker>& docBroker)
|
|
|
|
{
|
|
|
|
_docBroker = docBroker;
|
|
|
|
}
|
|
|
|
|
2016-04-03 20:40:14 -05:00
|
|
|
void close(const bool rude)
|
|
|
|
{
|
2016-05-02 06:21:30 -05:00
|
|
|
_stop = true;
|
|
|
|
IoUtil::shutdownWebSocket(_ws);
|
|
|
|
_thread.join();
|
2016-04-05 21:37:29 -05:00
|
|
|
_ws.reset();
|
2016-04-03 20:40:14 -05:00
|
|
|
if (_pid != -1)
|
|
|
|
{
|
2016-04-17 11:04:23 -05:00
|
|
|
Log::info("Closing child [" + std::to_string(_pid) + "].");
|
2016-04-03 22:26:06 -05:00
|
|
|
if (rude && kill(_pid, SIGINT) != 0 && kill(_pid, 0) != 0)
|
2016-04-03 20:40:14 -05:00
|
|
|
{
|
2016-04-07 02:36:38 -05:00
|
|
|
Log::syserror("Cannot terminate lokit [" + std::to_string(_pid) + "]. Abandoning.");
|
2016-04-03 20:40:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
_pid = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Poco::Process::PID getPid() const { return _pid; }
|
|
|
|
std::shared_ptr<Poco::Net::WebSocket> getWebSocket() const { return _ws; }
|
|
|
|
|
2016-04-24 10:56:02 -05:00
|
|
|
/// Check whether this child is alive and able to respond.
|
|
|
|
bool isAlive() const
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (_pid > 1 && _ws && kill(_pid, 0) == 0)
|
|
|
|
{
|
|
|
|
// We don't care about the response (and shouldn't read here).
|
|
|
|
_ws->sendFrame("PING", 4, Poco::Net::WebSocket::FRAME_OP_PING);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const std::exception& exc)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-02 06:21:30 -05:00
|
|
|
private:
|
|
|
|
void socketProcessor();
|
|
|
|
|
2016-04-03 20:40:14 -05:00
|
|
|
private:
|
|
|
|
Poco::Process::PID _pid;
|
|
|
|
std::shared_ptr<Poco::Net::WebSocket> _ws;
|
2016-05-02 06:21:30 -05:00
|
|
|
std::weak_ptr<DocumentBroker> _docBroker;
|
|
|
|
std::thread _thread;
|
|
|
|
std::atomic<bool> _stop;
|
2016-04-03 20:40:14 -05:00
|
|
|
};
|
|
|
|
|
2016-05-16 06:37:02 -05:00
|
|
|
class PrisonerSession;
|
2016-05-16 06:46:27 -05:00
|
|
|
class ClientSession;
|
2016-05-16 06:37:02 -05:00
|
|
|
|
2016-03-12 18:29:17 -06:00
|
|
|
/// DocumentBroker is responsible for setting up a document
|
|
|
|
/// in jail and brokering loading it from Storage
|
|
|
|
/// and saving it back.
|
2016-03-10 20:42:33 -06:00
|
|
|
/// Contains URI, physical path, etc.
|
2016-03-12 18:29:17 -06:00
|
|
|
class DocumentBroker
|
2016-03-10 20:42:33 -06:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2016-04-13 03:05:00 -05:00
|
|
|
static Poco::URI sanitizeURI(const std::string& uri);
|
2016-03-19 17:49:36 -05:00
|
|
|
|
|
|
|
/// Returns a document-specific key based
|
|
|
|
/// on the URI of the document.
|
|
|
|
static
|
2016-03-23 07:41:18 -05:00
|
|
|
std::string getDocKey(const Poco::URI& uri);
|
2016-09-15 05:41:57 -05:00
|
|
|
|
|
|
|
/// Dummy document broker that is marked to destroy.
|
|
|
|
DocumentBroker();
|
2016-03-12 18:29:17 -06:00
|
|
|
|
2016-03-19 17:49:36 -05:00
|
|
|
DocumentBroker(const Poco::URI& uriPublic,
|
|
|
|
const std::string& docKey,
|
2016-04-03 20:40:14 -05:00
|
|
|
const std::string& childRoot,
|
|
|
|
std::shared_ptr<ChildProcess> childProcess);
|
2016-03-12 18:29:17 -06:00
|
|
|
|
|
|
|
~DocumentBroker()
|
|
|
|
{
|
2016-03-25 21:56:18 -05:00
|
|
|
Log::info() << "~DocumentBroker [" << _uriPublic.toString()
|
2016-04-10 12:03:57 -05:00
|
|
|
<< "] destroyed with " << getSessionsCount()
|
|
|
|
<< " sessions left." << Log::end;
|
2016-03-12 18:29:17 -06:00
|
|
|
}
|
|
|
|
|
2016-08-29 08:41:37 -05:00
|
|
|
const StorageBase::FileInfo validate(const Poco::URI& uri);
|
2016-03-21 18:12:00 -05:00
|
|
|
|
2016-03-12 18:29:17 -06:00
|
|
|
/// Loads a document from the public URI into the jail.
|
2016-03-23 07:41:18 -05:00
|
|
|
bool load(const std::string& jailId);
|
2016-04-29 22:07:09 -05:00
|
|
|
bool isLoaded() const { return _isLoaded; }
|
|
|
|
void setLoaded() { _isLoaded = true; }
|
2016-03-12 18:29:17 -06:00
|
|
|
|
2016-04-25 19:48:02 -05:00
|
|
|
/// Save the document to Storage if needs persisting.
|
2016-07-14 04:49:21 -05:00
|
|
|
bool save(bool success, const std::string& result = "");
|
2016-04-24 10:08:08 -05:00
|
|
|
bool isModified() const { return _isModified; }
|
|
|
|
void setModified(const bool value);
|
2016-03-10 21:01:34 -06:00
|
|
|
|
2016-04-25 19:48:02 -05:00
|
|
|
/// Save the document if the document is modified.
|
2016-04-29 05:06:45 -05:00
|
|
|
/// @param force when true, will force saving if there
|
2016-04-25 19:48:02 -05:00
|
|
|
/// has been any recent activity after the last save.
|
2016-04-29 05:06:45 -05:00
|
|
|
/// @param waitTimeoutMs when >0 will wait for the save to
|
2016-04-25 19:48:02 -05:00
|
|
|
/// complete before returning, or timeout.
|
2016-04-29 05:06:45 -05:00
|
|
|
/// @return true if attempts to save or it also waits
|
2016-04-25 19:48:02 -05:00
|
|
|
/// and receives save notification. Otherwise, false.
|
|
|
|
bool autoSave(const bool force, const size_t waitTimeoutMs);
|
2016-04-09 22:20:20 -05:00
|
|
|
|
2016-03-10 20:42:33 -06:00
|
|
|
Poco::URI getPublicUri() const { return _uriPublic; }
|
|
|
|
Poco::URI getJailedUri() const { return _uriJailed; }
|
2016-03-12 18:29:17 -06:00
|
|
|
const std::string& getJailId() const { return _jailId; }
|
|
|
|
const std::string& getDocKey() const { return _docKey; }
|
2016-04-14 15:29:31 -05:00
|
|
|
const std::string& getFilename() const { return _filename; };
|
2016-03-25 21:56:18 -05:00
|
|
|
TileCache& tileCache() { return *_tileCache; }
|
2016-04-24 10:56:02 -05:00
|
|
|
bool isAlive() const { return _childProcess && _childProcess->isAlive(); }
|
2016-04-16 16:18:51 -05:00
|
|
|
size_t getSessionsCount() const
|
2016-04-10 12:03:57 -05:00
|
|
|
{
|
2016-04-16 16:18:51 -05:00
|
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
|
|
|
return _sessions.size();
|
2016-04-10 12:03:57 -05:00
|
|
|
}
|
2016-03-10 20:42:33 -06:00
|
|
|
|
2016-04-29 05:06:45 -05:00
|
|
|
/// @eturn the time in milliseconds since last save.
|
2016-04-09 22:20:20 -05:00
|
|
|
double getTimeSinceLastSaveMs() const
|
|
|
|
{
|
|
|
|
const auto duration = (std::chrono::steady_clock::now() - _lastSaveTime);
|
|
|
|
return std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
|
|
|
|
}
|
|
|
|
|
2016-03-23 07:41:18 -05:00
|
|
|
std::string getJailRoot() const;
|
2016-03-12 19:14:32 -06:00
|
|
|
|
2016-04-16 16:18:51 -05:00
|
|
|
/// Add a new session. Returns the new number of sessions.
|
2016-05-16 06:46:27 -05:00
|
|
|
size_t addSession(std::shared_ptr<ClientSession>& session);
|
2016-04-24 21:09:13 -05:00
|
|
|
/// Connect a prison session to its client peer.
|
2016-05-16 06:37:02 -05:00
|
|
|
bool connectPeers(std::shared_ptr<PrisonerSession>& session);
|
2016-04-16 16:18:51 -05:00
|
|
|
/// Removes a session by ID. Returns the new number of sessions.
|
|
|
|
size_t removeSession(const std::string& id);
|
2016-03-23 11:55:28 -05:00
|
|
|
|
2016-09-28 14:07:07 -05:00
|
|
|
void alertAllUsersOfDocument(const std::string& cmd, const std::string& kind);
|
|
|
|
|
2016-05-22 15:47:22 -05:00
|
|
|
/// Invalidate the cursor position.
|
2016-09-01 15:15:13 -05:00
|
|
|
void invalidateCursor(int x, int y, int w, int h)
|
2016-05-22 15:47:22 -05:00
|
|
|
{
|
|
|
|
_cursorPosX = x;
|
|
|
|
_cursorPosY = y;
|
2016-09-01 15:15:13 -05:00
|
|
|
_cursorWidth = w;
|
|
|
|
_cursorHeight = h;
|
2016-05-22 15:47:22 -05:00
|
|
|
}
|
|
|
|
|
2016-09-20 21:19:52 -05:00
|
|
|
void invalidateTiles(const std::string& tiles);
|
2016-05-22 13:31:18 -05:00
|
|
|
void handleTileRequest(TileDesc& tile,
|
2016-05-16 19:49:36 -05:00
|
|
|
const std::shared_ptr<ClientSession>& session);
|
2016-05-22 10:45:28 -05:00
|
|
|
void handleTileCombinedRequest(TileCombined& tileCombined,
|
|
|
|
const std::shared_ptr<ClientSession>& session);
|
2016-08-30 22:15:44 -05:00
|
|
|
void cancelTileRequests(const std::shared_ptr<ClientSession>& session);
|
2016-05-01 19:50:11 -05:00
|
|
|
void handleTileResponse(const std::vector<char>& payload);
|
2016-05-22 10:45:28 -05:00
|
|
|
void handleTileCombinedResponse(const std::vector<char>& payload);
|
2016-05-01 19:50:11 -05:00
|
|
|
|
2016-10-08 09:31:35 -05:00
|
|
|
/// Called before destroying any session.
|
|
|
|
/// This method calculates and sets important states of
|
|
|
|
/// session being destroyed. Returns true if session id
|
|
|
|
/// is the last editable session.
|
|
|
|
bool startDestroy(const std::string& id);
|
2016-04-17 22:29:03 -05:00
|
|
|
bool isMarkedToDestroy() const { return _markToDestroy; }
|
2016-04-15 04:00:22 -05:00
|
|
|
|
2016-05-02 06:21:30 -05:00
|
|
|
bool handleInput(const std::vector<char>& payload);
|
|
|
|
|
2016-10-08 13:25:27 -05:00
|
|
|
/// Forward a message from client session to its respective child session.
|
|
|
|
bool forwardToChild(const std::string& viewId, const char *buffer, int length);
|
|
|
|
|
2016-04-25 19:48:02 -05:00
|
|
|
private:
|
|
|
|
|
|
|
|
/// Sends the .uno:Save command to LoKit.
|
2016-07-14 04:49:21 -05:00
|
|
|
bool sendUnoSave(const bool dontSaveIfUnmodified);
|
2016-04-25 19:48:02 -05:00
|
|
|
|
|
|
|
/// Saves the document to Storage (assuming LO Core saved to local copy).
|
|
|
|
bool saveToStorage();
|
|
|
|
|
2016-10-08 17:21:35 -05:00
|
|
|
/// Forward a message from child session to its respective client session.
|
2016-10-08 12:13:23 -05:00
|
|
|
bool forwardToClient(const std::string& prefix, const std::vector<char>& payload);
|
|
|
|
|
2016-03-10 20:42:33 -06:00
|
|
|
private:
|
|
|
|
const Poco::URI _uriPublic;
|
2016-03-12 18:29:17 -06:00
|
|
|
const std::string _docKey;
|
2016-03-12 19:14:32 -06:00
|
|
|
const std::string _childRoot;
|
2016-03-26 06:50:13 -05:00
|
|
|
const std::string _cacheRoot;
|
2016-04-24 10:08:08 -05:00
|
|
|
std::shared_ptr<ChildProcess> _childProcess;
|
2016-03-12 18:29:17 -06:00
|
|
|
Poco::URI _uriJailed;
|
|
|
|
std::string _jailId;
|
2016-03-21 18:12:00 -05:00
|
|
|
std::string _filename;
|
2016-04-09 22:20:20 -05:00
|
|
|
std::chrono::steady_clock::time_point _lastSaveTime;
|
2016-04-25 19:44:24 -05:00
|
|
|
Poco::Timestamp _lastFileModifiedTime;
|
2016-05-16 06:46:27 -05:00
|
|
|
std::map<std::string, std::shared_ptr<ClientSession>> _sessions;
|
2016-03-10 21:01:34 -06:00
|
|
|
std::unique_ptr<StorageBase> _storage;
|
2016-03-25 21:56:18 -05:00
|
|
|
std::unique_ptr<TileCache> _tileCache;
|
2016-04-25 04:21:54 -05:00
|
|
|
std::atomic<bool> _markToDestroy;
|
2016-07-10 23:50:25 -05:00
|
|
|
std::atomic<bool> _lastEditableSession;
|
2016-10-08 09:31:35 -05:00
|
|
|
std::atomic<bool> _isLoaded;
|
|
|
|
std::atomic<bool> _isModified;
|
2016-05-22 15:47:22 -05:00
|
|
|
int _cursorPosX;
|
|
|
|
int _cursorPosY;
|
2016-09-01 15:15:13 -05:00
|
|
|
int _cursorWidth;
|
|
|
|
int _cursorHeight;
|
2016-04-16 16:18:51 -05:00
|
|
|
mutable std::mutex _mutex;
|
2016-04-10 21:07:09 -05:00
|
|
|
std::condition_variable _saveCV;
|
|
|
|
std::mutex _saveMutex;
|
2016-04-09 22:20:20 -05:00
|
|
|
|
2016-05-22 13:31:18 -05:00
|
|
|
/// Versioning is used to prevent races between
|
|
|
|
/// painting and invalidation.
|
|
|
|
std::atomic<size_t> _tileVersion;
|
|
|
|
|
2016-04-09 22:20:20 -05:00
|
|
|
static constexpr auto IdleSaveDurationMs = 30 * 1000;
|
|
|
|
static constexpr auto AutoSaveDurationMs = 300 * 1000;
|
2016-03-10 20:42:33 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|