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>
|
2017-03-06 09:05:11 -06:00
|
|
|
#include <deque>
|
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-10-29 20:15:00 -05:00
|
|
|
#include <Poco/URI.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-12-21 14:37:22 -06:00
|
|
|
#include "TileDesc.hpp"
|
2016-03-23 11:55:28 -05:00
|
|
|
#include "Util.hpp"
|
2017-03-04 17:07:17 -06:00
|
|
|
#include "net/Socket.hpp"
|
2017-03-06 13:50:06 -06:00
|
|
|
#include "net/WebSocketHandler.hpp"
|
2016-03-25 21:56:18 -05:00
|
|
|
|
2016-11-14 07:58:04 -06:00
|
|
|
#include "common/SigUtil.hpp"
|
|
|
|
|
2016-03-25 21:56:18 -05:00
|
|
|
// Forwards.
|
2017-03-06 13:50:06 -06:00
|
|
|
class PrisonerRequestDispatcher;
|
2016-05-16 19:49:36 -05:00
|
|
|
class DocumentBroker;
|
2016-12-21 14:37:22 -06:00
|
|
|
class StorageBase;
|
|
|
|
class TileCache;
|
2017-01-22 22:18:37 -06:00
|
|
|
class Message;
|
2016-03-10 20:42:33 -06:00
|
|
|
|
2017-03-07 11:08:54 -06:00
|
|
|
class TerminatingPoll : public SocketPoll
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TerminatingPoll(const std::string &threadName) :
|
2017-03-13 07:00:31 -05:00
|
|
|
SocketPoll(threadName) {}
|
2017-03-07 11:08:54 -06:00
|
|
|
|
|
|
|
bool continuePolling() override
|
|
|
|
{
|
2017-04-18 23:54:42 -05:00
|
|
|
return SocketPoll::continuePolling() && !TerminationFlag;
|
2017-03-07 11:08:54 -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.
|
2017-03-06 16:03:11 -06:00
|
|
|
/// @param socket is the underlying Sockeet to the child.
|
2017-05-07 10:05:34 -05:00
|
|
|
ChildProcess(const Poco::Process::PID pid,
|
|
|
|
const std::string& jailId,
|
|
|
|
const std::shared_ptr<StreamSocket>& socket,
|
|
|
|
const Poco::Net::HTTPRequest &request) :
|
2017-03-06 13:50:06 -06:00
|
|
|
|
2016-04-03 20:40:14 -05:00
|
|
|
_pid(pid),
|
2017-05-07 10:05:34 -05:00
|
|
|
_jailId(jailId),
|
2017-03-06 13:50:06 -06:00
|
|
|
_ws(std::make_shared<WebSocketHandler>(socket, request)),
|
|
|
|
_socket(socket)
|
2016-04-03 20:40:14 -05:00
|
|
|
{
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_INF("ChildProcess ctor [" << _pid << "].");
|
2016-04-03 20:40:14 -05:00
|
|
|
}
|
2018-09-11 01:30:55 -05:00
|
|
|
|
2016-04-03 20:40:14 -05:00
|
|
|
|
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()
|
|
|
|
{
|
2018-11-07 17:00:32 -06:00
|
|
|
LOG_DBG("~ChildProcess dtor [" << _pid << "].");
|
|
|
|
|
2018-05-18 06:00:16 -05:00
|
|
|
if (_pid <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
terminate();
|
|
|
|
|
|
|
|
// No need for the socket anymore.
|
|
|
|
_ws.reset();
|
|
|
|
_socket.reset();
|
2016-04-03 20:40:14 -05:00
|
|
|
}
|
|
|
|
|
2017-03-07 22:46:02 -06:00
|
|
|
void setDocumentBroker(const std::shared_ptr<DocumentBroker>& docBroker);
|
2017-03-12 18:18:12 -05:00
|
|
|
std::shared_ptr<DocumentBroker> getDocumentBroker() const { return _docBroker.lock(); }
|
2016-05-02 06:21:30 -05:00
|
|
|
|
2018-05-18 06:00:16 -05:00
|
|
|
/// Let the child close a nice way.
|
|
|
|
void close()
|
2016-10-22 18:10:07 -05:00
|
|
|
{
|
2018-05-18 06:00:16 -05:00
|
|
|
if (_pid < 0)
|
|
|
|
return;
|
|
|
|
|
2016-11-06 20:16:11 -06:00
|
|
|
try
|
|
|
|
{
|
2018-05-18 06:00:16 -05:00
|
|
|
LOG_DBG("Closing ChildProcess [" << _pid << "].");
|
|
|
|
|
|
|
|
// Request the child to exit
|
2016-12-30 16:17:07 -06:00
|
|
|
if (isAlive())
|
2016-11-06 20:16:11 -06:00
|
|
|
{
|
2017-02-04 14:46:52 -06:00
|
|
|
LOG_DBG("Stopping ChildProcess [" << _pid << "]");
|
2016-11-06 20:16:11 -06:00
|
|
|
sendTextFrame("exit");
|
|
|
|
}
|
2018-05-18 06:00:16 -05:00
|
|
|
|
|
|
|
// Shutdown the socket.
|
|
|
|
if (_ws)
|
|
|
|
_ws->shutdown();
|
2016-11-06 20:16:11 -06:00
|
|
|
}
|
2018-05-18 06:00:16 -05:00
|
|
|
catch (const std::exception& ex)
|
2016-11-06 20:16:11 -06:00
|
|
|
{
|
2018-05-18 06:00:16 -05:00
|
|
|
LOG_ERR("Error while closing child process: " << ex.what());
|
2016-11-06 20:16:11 -06:00
|
|
|
}
|
2018-05-18 06:00:16 -05:00
|
|
|
|
|
|
|
_pid = -1;
|
2016-10-22 18:10:07 -05:00
|
|
|
}
|
|
|
|
|
2018-05-18 06:00:16 -05:00
|
|
|
/// Kill or abandon the child.
|
|
|
|
void terminate()
|
2016-04-03 20:40:14 -05:00
|
|
|
{
|
2017-02-04 14:46:52 -06:00
|
|
|
if (_pid < 0)
|
|
|
|
return;
|
|
|
|
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2018-05-18 06:00:16 -05:00
|
|
|
if (::kill(_pid, 0) == 0)
|
2017-02-04 14:46:52 -06:00
|
|
|
{
|
|
|
|
LOG_INF("Killing child [" << _pid << "].");
|
2018-05-17 13:47:37 -05:00
|
|
|
if (!SigUtil::killChild(_pid))
|
2017-02-04 14:46:52 -06:00
|
|
|
{
|
|
|
|
LOG_ERR("Cannot terminate lokit [" << _pid << "]. Abandoning.");
|
|
|
|
}
|
|
|
|
}
|
2018-09-13 11:16:00 -05:00
|
|
|
#else
|
|
|
|
// What to do? Throw some unique exception that the outermost call in the thread catches and
|
|
|
|
// exits from the thread?
|
2018-09-11 01:30:55 -05:00
|
|
|
#endif
|
2018-09-13 11:16:00 -05:00
|
|
|
_pid = -1;
|
2016-04-03 20:40:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Poco::Process::PID getPid() const { return _pid; }
|
2017-05-07 10:05:34 -05:00
|
|
|
const std::string& getJailId() const { return _jailId; }
|
2016-10-11 17:22:51 -05:00
|
|
|
|
|
|
|
/// Send a text payload to the child-process WS.
|
|
|
|
bool sendTextFrame(const std::string& data)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2016-11-06 20:16:11 -06:00
|
|
|
if (_ws)
|
|
|
|
{
|
2018-11-28 16:57:34 -06:00
|
|
|
LOG_TRC("Send DocBroker to Child message: [" << LOOLProtocol::getAbbreviatedMessage(data) << "].");
|
2017-03-29 19:03:01 -05:00
|
|
|
_ws->sendMessage(data);
|
2016-11-06 20:16:11 -06:00
|
|
|
return true;
|
|
|
|
}
|
2016-10-11 17:22:51 -05:00
|
|
|
}
|
|
|
|
catch (const std::exception& exc)
|
|
|
|
{
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_ERR("Failed to send child [" << _pid << "] data [" <<
|
2018-11-28 16:57:34 -06:00
|
|
|
LOOLProtocol::getAbbreviatedMessage(data) << "] due to: " << exc.what());
|
2016-10-11 17:22:51 -05:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2018-11-28 16:57:34 -06:00
|
|
|
LOG_WRN("No socket between DocBroker and child to send [" << LOOLProtocol::getAbbreviatedMessage(data) << "]");
|
2016-10-11 17:22:51 -05:00
|
|
|
return false;
|
|
|
|
}
|
2016-04-03 20:40:14 -05:00
|
|
|
|
2016-12-21 09:09:14 -06:00
|
|
|
/// Check whether this child is alive and socket not in error.
|
2016-11-13 15:12:01 -06:00
|
|
|
/// Note: zombies will show as alive, and sockets have waiting
|
|
|
|
/// time after the other end-point closes. So this isn't accurate.
|
2016-04-24 10:56:02 -05:00
|
|
|
bool isAlive() const
|
|
|
|
{
|
2019-02-12 05:16:40 -06:00
|
|
|
#if !MOBILEAPP
|
2016-04-24 10:56:02 -05:00
|
|
|
try
|
|
|
|
{
|
2018-05-18 06:00:16 -05:00
|
|
|
return _pid > 1 && _ws && ::kill(_pid, 0) == 0;
|
2016-04-24 10:56:02 -05:00
|
|
|
}
|
2017-02-04 14:46:52 -06:00
|
|
|
catch (const std::exception&)
|
2016-04-24 10:56:02 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2018-09-11 01:30:55 -05:00
|
|
|
#else
|
2018-10-30 12:20:30 -05:00
|
|
|
return _pid > 1;
|
2018-09-11 01:30:55 -05:00
|
|
|
#endif
|
2016-04-24 10:56:02 -05:00
|
|
|
}
|
|
|
|
|
2016-04-03 20:40:14 -05:00
|
|
|
private:
|
|
|
|
Poco::Process::PID _pid;
|
2017-05-07 10:05:34 -05:00
|
|
|
const std::string _jailId;
|
2017-03-06 13:50:06 -06:00
|
|
|
std::shared_ptr<WebSocketHandler> _ws;
|
|
|
|
std::shared_ptr<Socket> _socket;
|
2016-05-02 06:21:30 -05:00
|
|
|
std::weak_ptr<DocumentBroker> _docBroker;
|
2016-04-03 20:40:14 -05:00
|
|
|
};
|
|
|
|
|
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-10-12 22:04:07 -05:00
|
|
|
class DocumentBroker : public std::enable_shared_from_this<DocumentBroker>
|
2016-03-10 20:42:33 -06:00
|
|
|
{
|
2017-03-07 11:34:01 -06:00
|
|
|
class DocumentBrokerPoll;
|
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.
|
2016-10-29 20:15:00 -05:00
|
|
|
static 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
|
|
|
|
2017-03-12 12:56:42 -05:00
|
|
|
/// Construct DocumentBroker with URI, docKey, and root path.
|
2017-02-05 20:59:08 -06:00
|
|
|
DocumentBroker(const std::string& uri,
|
|
|
|
const Poco::URI& uriPublic,
|
2019-03-01 15:03:35 -06:00
|
|
|
const std::string& docKey);
|
2017-03-09 13:23:21 -06:00
|
|
|
|
2019-03-01 15:25:44 -06:00
|
|
|
virtual ~DocumentBroker();
|
2016-03-12 18:29:17 -06:00
|
|
|
|
2017-03-13 07:00:31 -05:00
|
|
|
/// Start processing events
|
|
|
|
void startThread();
|
|
|
|
|
2018-02-02 01:47:45 -06:00
|
|
|
/// Flag for termination. Note that this doesn't save any unsaved changes in the document
|
2018-01-14 19:49:11 -06:00
|
|
|
void stop(const std::string& reason);
|
2017-03-29 21:50:29 -05:00
|
|
|
|
2017-03-31 14:58:33 -05:00
|
|
|
/// Thread safe termination of this broker if it has a lingering thread
|
|
|
|
void joinThread();
|
|
|
|
|
2016-03-12 18:29:17 -06:00
|
|
|
/// Loads a document from the public URI into the jail.
|
2017-03-25 12:56:17 -05:00
|
|
|
bool load(const std::shared_ptr<ClientSession>& session, const std::string& jailId);
|
2016-04-29 22:07:09 -05:00
|
|
|
bool isLoaded() const { return _isLoaded; }
|
2017-03-11 16:01:27 -06:00
|
|
|
void setLoaded();
|
2016-03-12 18:29:17 -06:00
|
|
|
|
2017-06-01 07:56:54 -05:00
|
|
|
bool isDocumentChangedInStorage() { return _documentChangedInStorage; }
|
|
|
|
|
2017-03-09 12:19:53 -06:00
|
|
|
/// Save the document to Storage if it needs persisting.
|
2017-06-01 09:16:03 -05:00
|
|
|
bool saveToStorage(const std::string& sesionId, bool success, const std::string& result = "", bool force = false);
|
2017-10-20 11:12:05 -05:00
|
|
|
|
|
|
|
/// Save As the document to Storage.
|
|
|
|
/// @param saveAsPath Absolute path to the jailed file.
|
2019-04-30 09:21:44 -05:00
|
|
|
bool saveAsToStorage(const std::string& sesionId, const std::string& saveAsPath, const std::string& saveAsFilename, const bool isRename);
|
2017-10-20 11:12:05 -05:00
|
|
|
|
2016-04-24 10:08:08 -05:00
|
|
|
bool isModified() const { return _isModified; }
|
|
|
|
void setModified(const bool value);
|
2018-01-03 22:15:44 -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
|
|
|
/// @return true if attempts to save or it also waits
|
2016-04-25 19:48:02 -05:00
|
|
|
/// and receives save notification. Otherwise, false.
|
2017-03-09 12:19:53 -06:00
|
|
|
bool autoSave(const bool force);
|
2016-04-09 22:20:20 -05:00
|
|
|
|
2016-03-10 20:42:33 -06:00
|
|
|
Poco::URI getPublicUri() const { return _uriPublic; }
|
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; }
|
2017-03-04 17:07:17 -06:00
|
|
|
bool isAlive() const;
|
2016-03-10 20:42:33 -06:00
|
|
|
|
2017-03-15 07:07:17 -05:00
|
|
|
/// Are we running in either shutdown, or the polling thread.
|
2017-04-05 07:48:49 -05:00
|
|
|
/// Asserts in the debug builds, otherwise just logs.
|
2018-01-14 10:16:10 -06:00
|
|
|
void assertCorrectThread() const;
|
2017-03-15 07:07:17 -05:00
|
|
|
|
|
|
|
/// Pretty print internal state to a stream.
|
2017-03-12 18:03:45 -05:00
|
|
|
void dumpState(std::ostream& os);
|
2017-03-06 09:45:34 -06:00
|
|
|
|
2016-03-23 07:41:18 -05:00
|
|
|
std::string getJailRoot() const;
|
2016-03-12 19:14:32 -06:00
|
|
|
|
2017-04-01 18:20:54 -05:00
|
|
|
/// Add a new session. Returns the new number of sessions.
|
|
|
|
size_t addSession(const std::shared_ptr<ClientSession>& session);
|
2017-03-06 09:05:11 -06:00
|
|
|
|
2016-04-16 16:18:51 -05:00
|
|
|
/// Removes a session by ID. Returns the new number of sessions.
|
2018-01-14 16:17:00 -06:00
|
|
|
size_t removeSession(const std::string& id);
|
2016-03-23 11:55:28 -05:00
|
|
|
|
2017-03-20 12:18:41 -05:00
|
|
|
/// Add a callback to be invoked in our polling thread.
|
2017-04-11 01:54:09 -05:00
|
|
|
void addCallback(const SocketPoll::CallbackFn& fn);
|
2017-03-20 12:18:41 -05:00
|
|
|
|
|
|
|
/// Transfer this socket into our polling thread / loop.
|
2017-03-07 11:34:01 -06:00
|
|
|
void addSocketToPoll(const std::shared_ptr<Socket>& socket);
|
2017-03-07 01:00:51 -06:00
|
|
|
|
2016-11-17 08:00:05 -06:00
|
|
|
void alertAllUsers(const std::string& msg);
|
|
|
|
|
|
|
|
void alertAllUsers(const std::string& cmd, const std::string& kind)
|
|
|
|
{
|
|
|
|
alertAllUsers("error: cmd=" + cmd + " kind=" + kind);
|
|
|
|
}
|
2016-09-28 14:07:07 -05:00
|
|
|
|
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);
|
2018-06-07 06:13:36 -05:00
|
|
|
void sendRequestedTiles(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
|
|
|
|
2017-04-06 11:58:41 -05:00
|
|
|
bool isMarkedToDestroy() const { return _markToDestroy || _stop; }
|
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.
|
2016-10-16 12:43:44 -05:00
|
|
|
bool forwardToChild(const std::string& viewId, const std::string& message);
|
2016-10-08 13:25:27 -05:00
|
|
|
|
2016-10-11 07:39:56 -05:00
|
|
|
int getRenderedTileCount() { return _debugRenderedTileCount; }
|
|
|
|
|
2018-02-02 01:47:45 -06:00
|
|
|
/// Ask the document broker to close. Makes sure that the document is saved.
|
2016-11-08 07:37:28 -06:00
|
|
|
void closeDocument(const std::string& reason);
|
|
|
|
|
2016-10-29 20:15:00 -05:00
|
|
|
/// Called by the ChildProcess object to notify
|
|
|
|
/// that it has terminated on its own.
|
|
|
|
/// This happens either when the child exists
|
|
|
|
/// or upon failing to process an incoming message.
|
2016-10-15 16:07:40 -05:00
|
|
|
void childSocketTerminated();
|
|
|
|
|
2016-10-17 06:45:58 -05:00
|
|
|
/// Get the PID of the associated child process
|
2019-05-03 07:51:49 -05:00
|
|
|
Poco::Process::PID getPid() const { return _childProcess ? _childProcess->getPid() : 0; }
|
2016-10-17 06:45:58 -05:00
|
|
|
|
2016-11-05 21:15:44 -05:00
|
|
|
std::unique_lock<std::mutex> getLock() { return std::unique_lock<std::mutex>(_mutex); }
|
2017-02-10 00:43:32 -06:00
|
|
|
std::unique_lock<std::mutex> getDeferredLock() { return std::unique_lock<std::mutex>(_mutex, std::defer_lock); }
|
2016-11-05 21:15:44 -05:00
|
|
|
|
2016-12-05 14:11:07 -06:00
|
|
|
void updateLastActivityTime();
|
|
|
|
|
2017-01-01 17:16:56 -06:00
|
|
|
std::size_t getIdleTimeSecs() const
|
|
|
|
{
|
2017-01-01 18:42:19 -06:00
|
|
|
const auto duration = (std::chrono::steady_clock::now() - _lastActivityTime);
|
2017-01-01 17:16:56 -06:00
|
|
|
return std::chrono::duration_cast<std::chrono::seconds>(duration).count();
|
|
|
|
}
|
2016-12-14 10:12:57 -06:00
|
|
|
|
2017-05-15 00:59:04 -05:00
|
|
|
/// Sends the .uno:Save command to LoKit.
|
2018-12-04 01:49:49 -06:00
|
|
|
bool sendUnoSave(const std::string& sessionId, bool dontTerminateEdit = true,
|
|
|
|
bool dontSaveIfUnmodified = true, bool isAutosave = false,
|
|
|
|
bool isExitSave = false);
|
2017-05-15 00:59:04 -05:00
|
|
|
|
2017-06-01 11:11:11 -05:00
|
|
|
/// Sends a message to all sessions
|
|
|
|
void broadcastMessage(const std::string& message);
|
|
|
|
|
2018-04-24 11:09:37 -05:00
|
|
|
/// Returns true iff an initial setting by the given name is already initialized.
|
|
|
|
bool isInitialSettingSet(const std::string& name) const;
|
|
|
|
|
|
|
|
/// Sets the initialization flag of a given initial setting.
|
|
|
|
void setInitialSetting(const std::string& name);
|
|
|
|
|
2016-04-25 19:48:02 -05:00
|
|
|
private:
|
2017-05-21 18:13:55 -05:00
|
|
|
|
|
|
|
/// Shutdown all client connections with the given reason.
|
|
|
|
void shutdownClients(const std::string& closeReason);
|
|
|
|
|
2017-03-29 21:50:29 -05:00
|
|
|
/// This gracefully terminates the connection
|
|
|
|
/// with the child and cleans up ChildProcess etc.
|
2017-06-21 05:07:38 -05:00
|
|
|
void terminateChild(const std::string& closeReason);
|
2017-03-29 21:50:29 -05:00
|
|
|
|
2017-03-09 22:50:36 -06:00
|
|
|
/// Saves the doc to the storage.
|
2019-04-30 09:21:44 -05:00
|
|
|
bool saveToStorageInternal(const std::string& sesionId, bool success, const std::string& result = "", const std::string& saveAsPath = std::string(), const std::string& saveAsFilename = std::string(), const bool isRename = false);
|
2017-03-09 22:50:36 -06:00
|
|
|
|
2018-01-14 17:59:11 -06:00
|
|
|
/// True iff a save is in progress (requested but not completed).
|
|
|
|
bool isSaving() const { return _lastSaveResponseTime < _lastSaveRequestTime; }
|
|
|
|
|
2018-01-14 23:49:14 -06:00
|
|
|
/// True if we know the doc is modified or
|
|
|
|
/// if there has been activity from a client after we last *requested* saving,
|
|
|
|
/// since there are race conditions vis-a-vis user activity while saving.
|
|
|
|
bool isPossiblyModified() const { return _isModified || (_lastSaveRequestTime < _lastActivityTime); }
|
|
|
|
|
2018-01-14 16:17:00 -06:00
|
|
|
/// True iff there is at least one non-readonly session other than the given.
|
|
|
|
/// Since only editable sessions can save, we need to use the last to
|
|
|
|
/// save modified documents, otherwise we'll potentially have to save on
|
|
|
|
/// every editable session disconnect, lest we lose data due to racing.
|
|
|
|
bool haveAnotherEditableSession(const std::string& id) const;
|
|
|
|
|
2017-05-22 14:04:35 -05:00
|
|
|
/// Loads a new session and adds to the sessions container.
|
|
|
|
size_t addSessionInternal(const std::shared_ptr<ClientSession>& session);
|
|
|
|
|
2017-03-09 22:50:36 -06:00
|
|
|
/// Removes a session by ID. Returns the new number of sessions.
|
2017-04-11 01:54:09 -05:00
|
|
|
size_t removeSessionInternal(const std::string& id);
|
2016-04-25 19:48:02 -05:00
|
|
|
|
2016-10-08 17:21:35 -05:00
|
|
|
/// Forward a message from child session to its respective client session.
|
2017-01-22 22:18:37 -06:00
|
|
|
bool forwardToClient(const std::shared_ptr<Message>& payload);
|
2016-10-08 12:13:23 -05:00
|
|
|
|
2017-03-04 17:07:17 -06:00
|
|
|
/// The thread function that all of the I/O for all sessions
|
|
|
|
/// associated with this document.
|
2017-03-07 19:46:16 -06:00
|
|
|
void pollThread();
|
2017-03-04 17:07:17 -06:00
|
|
|
|
2017-06-03 16:53:57 -05:00
|
|
|
/// Sum the I/O stats from all connected sessions
|
2017-05-31 21:40:01 -05:00
|
|
|
void getIOStats(uint64_t &sent, uint64_t &recv);
|
|
|
|
|
2019-03-01 15:25:44 -06:00
|
|
|
protected:
|
2017-02-05 20:59:08 -06:00
|
|
|
const std::string _uriOrig;
|
2019-03-01 15:25:44 -06:00
|
|
|
private:
|
2016-03-10 20:42:33 -06:00
|
|
|
const Poco::URI _uriPublic;
|
2017-04-02 14:55:56 -05:00
|
|
|
/// URL-based key. May be repeated during the lifetime of WSD.
|
2016-03-12 18:29:17 -06:00
|
|
|
const std::string _docKey;
|
2017-04-02 14:55:56 -05:00
|
|
|
/// Short numerical ID. Unique during the lifetime of WSD.
|
|
|
|
const std::string _docId;
|
2016-03-12 19:14:32 -06:00
|
|
|
const std::string _childRoot;
|
2016-04-24 10:08:08 -05:00
|
|
|
std::shared_ptr<ChildProcess> _childProcess;
|
2018-06-10 10:41:17 -05:00
|
|
|
std::string _uriJailed;
|
|
|
|
std::string _uriJailedAnonym;
|
2016-03-12 18:29:17 -06:00
|
|
|
std::string _jailId;
|
2016-03-21 18:12:00 -05:00
|
|
|
std::string _filename;
|
2017-01-01 18:42:19 -06:00
|
|
|
|
2017-06-01 07:56:54 -05:00
|
|
|
/// Set to true when document changed in storage and we are waiting
|
|
|
|
/// for user's command to act.
|
|
|
|
bool _documentChangedInStorage;
|
|
|
|
|
2017-01-01 18:42:19 -06:00
|
|
|
/// The last time we tried saving, regardless of whether the
|
|
|
|
/// document was modified and saved or not.
|
2016-04-09 22:20:20 -05:00
|
|
|
std::chrono::steady_clock::time_point _lastSaveTime;
|
2017-01-01 18:42:19 -06:00
|
|
|
|
2018-01-14 17:59:11 -06:00
|
|
|
/// The last time we sent a save request to lokit.
|
2017-03-20 20:57:21 -05:00
|
|
|
std::chrono::steady_clock::time_point _lastSaveRequestTime;
|
|
|
|
|
2018-01-14 17:59:11 -06:00
|
|
|
/// The last time we received a response for a save request from lokit.
|
|
|
|
std::chrono::steady_clock::time_point _lastSaveResponseTime;
|
|
|
|
|
2017-01-01 18:42:19 -06:00
|
|
|
/// The document's last-modified time on storage.
|
WIP: Check if the document has been modified behind our back
For now, do the check only when a new session connects to the
document, because at that point we fetch the document information (in
separate function for WOPI and local files) and construct the
FileInfo, including timestamp.
For now, just log an ERR message if we notice that the document in its
storage system (WOPI or local file system) has an unexpected last
modified time. What should we do? If we don't have unsaved changes,
most likely we should just silently reload the document and force all
sessions to refresh. But if we have unsaved changes, and the document
has changed underneath, we have a problem.
We need to fetch the timestamp also also after saving ("persisting")
as we can't assume that the clock on the machine running loolwsd and
that of the storage (as reported in the WOPI case in CheckFileInfo)
are in synch. (Assuming separate machines, they certainly won't ever
exactly in synch, but aren't necessarily even just a few seconds apart
(think incorrectly set up timezone etc), so no amount of tolerance in
the comparison would be good enough, because after all, it might be
that in the problematic cases we are looking for the timestamps also
are separated by a quite short time.)
Yes, this means there is a race condition; what if the document is
modified behind out back right after we have persisted it, before we
ask for its timestamp? It would be much better if the persisting
operation atomically also told what the timestamp of the document in
the storage is after persisting, but alas, WOPI doesn't do that.
Rename the DocumentBroker::origDocumentLastModifiedTime field to
_documentLastModifiedTime as that is less misleading. It is not the
"original" document timestamp but the timestamp of the document in its
storage system.
This needs much more work: Ideally the timestamp of the document in
its storage system should be retrieved and checked against the
expected value also before we are about to save it.
But unfortunately experience has shown that the WOPI CheckFileInfo
operation can be expensive, so we'll see what can be done. Ideally
WOPI should contain the optional functionality to return an error if,
when saving a document, its timestamp (and size?) in storage are not
what the saving client expects.
Also add a few FIXME comments.
Change-Id: I5a9b55d4b55a8db0c9ee8638edd368dc0aa325d5
2016-12-20 10:01:03 -06:00
|
|
|
Poco::Timestamp _documentLastModifiedTime;
|
2017-01-01 18:42:19 -06:00
|
|
|
|
|
|
|
/// The jailed file last-modified time.
|
2016-04-25 19:44:24 -05:00
|
|
|
Poco::Timestamp _lastFileModifiedTime;
|
2018-01-14 17:59:11 -06:00
|
|
|
|
|
|
|
/// All session of this DocBroker by ID.
|
2016-10-29 20:15:00 -05:00
|
|
|
std::map<std::string, std::shared_ptr<ClientSession> > _sessions;
|
2017-03-06 12:05:49 -06:00
|
|
|
|
2018-04-24 11:09:37 -05:00
|
|
|
/// If we set the user-requested inital (on load) settings to be forced.
|
|
|
|
std::set<std::string> _isInitialStateSet;
|
|
|
|
|
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;
|
2018-02-01 22:28:20 -06:00
|
|
|
std::atomic<bool> _closeRequest;
|
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;
|
2017-03-07 11:34:01 -06:00
|
|
|
std::unique_ptr<DocumentBrokerPoll> _poll;
|
2017-03-04 17:07:17 -06:00
|
|
|
std::atomic<bool> _stop;
|
2017-07-07 06:42:19 -05:00
|
|
|
std::string _closeReason;
|
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-10-11 07:39:56 -05:00
|
|
|
int _debugRenderedTileCount;
|
|
|
|
|
2017-01-01 18:42:19 -06:00
|
|
|
std::chrono::steady_clock::time_point _lastActivityTime;
|
2017-03-11 16:01:27 -06:00
|
|
|
std::chrono::steady_clock::time_point _threadStart;
|
|
|
|
std::chrono::milliseconds _loadDuration;
|
2016-12-14 10:12:57 -06:00
|
|
|
|
2017-04-02 14:55:56 -05:00
|
|
|
/// Unique DocBroker ID for tracing and debugging.
|
|
|
|
static std::atomic<unsigned> DocBrokerId;
|
2016-03-10 20:42:33 -06:00
|
|
|
};
|
|
|
|
|
2019-03-01 15:25:44 -06:00
|
|
|
class ConvertToBroker : public DocumentBroker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/// Construct DocumentBroker with URI and docKey
|
|
|
|
ConvertToBroker(const std::string& uri,
|
|
|
|
const Poco::URI& uriPublic,
|
2019-03-12 09:41:54 -05:00
|
|
|
const std::string& docKey);
|
2019-03-01 15:25:44 -06:00
|
|
|
virtual ~ConvertToBroker();
|
2019-03-12 09:41:54 -05:00
|
|
|
|
|
|
|
/// How many live conversions are running.
|
|
|
|
static size_t getInstanceCount();
|
2019-03-01 15:25:44 -06:00
|
|
|
};
|
|
|
|
|
2016-03-10 20:42:33 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|