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-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 "LOOLWebSocket.hpp"
|
|
|
|
#include "TileDesc.hpp"
|
2016-03-23 11:55:28 -05:00
|
|
|
#include "Util.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.
|
2016-05-16 19:49:36 -05:00
|
|
|
class DocumentBroker;
|
2016-12-21 14:37:22 -06:00
|
|
|
class StorageBase;
|
|
|
|
class TileCache;
|
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.
|
2016-11-10 02:47:25 -06:00
|
|
|
/// @param ws is the control LOOLWebSocket to the child.
|
|
|
|
ChildProcess(const Poco::Process::PID pid, const std::shared_ptr<LOOLWebSocket>& ws) :
|
2016-04-03 20:40:14 -05:00
|
|
|
_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-11-06 10:59:59 -06:00
|
|
|
LOG_INF("ChildProcess ctor [" << _pid << "].");
|
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()
|
|
|
|
{
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_DBG("~ChildProcess dtor [" << _pid << "].");
|
2016-10-22 18:10:07 -05:00
|
|
|
close(true);
|
2016-04-03 20:40:14 -05:00
|
|
|
}
|
|
|
|
|
2016-05-02 06:21:30 -05:00
|
|
|
void setDocumentBroker(const std::shared_ptr<DocumentBroker>& docBroker)
|
|
|
|
{
|
2016-10-09 10:28:22 -05:00
|
|
|
assert(docBroker && "Invalid DocumentBroker instance.");
|
2016-05-02 06:21:30 -05:00
|
|
|
_docBroker = docBroker;
|
|
|
|
}
|
|
|
|
|
2016-10-22 18:10:07 -05:00
|
|
|
void stop()
|
|
|
|
{
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_DBG("Stopping ChildProcess [" << _pid << "]");
|
2016-10-22 18:10:07 -05:00
|
|
|
_stop = true;
|
2016-11-06 20:16:11 -06:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2016-12-30 16:17:07 -06:00
|
|
|
if (isAlive())
|
2016-11-06 20:16:11 -06:00
|
|
|
{
|
|
|
|
sendTextFrame("exit");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const std::exception&)
|
|
|
|
{
|
2016-11-22 23:21:12 -06:00
|
|
|
// Already logged in sendTextFrame.
|
2016-11-06 20:16:11 -06:00
|
|
|
}
|
2016-10-22 18:10:07 -05:00
|
|
|
}
|
|
|
|
|
2016-04-03 20:40:14 -05:00
|
|
|
void close(const bool rude)
|
|
|
|
{
|
2016-10-14 07:37:43 -05:00
|
|
|
try
|
2016-04-03 20:40:14 -05:00
|
|
|
{
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_DBG("Closing ChildProcess [" << _pid << "].");
|
2016-10-22 18:10:07 -05:00
|
|
|
stop();
|
2016-10-14 07:37:43 -05:00
|
|
|
IoUtil::shutdownWebSocket(_ws);
|
|
|
|
if (_thread.joinable())
|
2016-04-03 20:40:14 -05:00
|
|
|
{
|
2016-10-14 07:37:43 -05:00
|
|
|
_thread.join();
|
2016-04-03 20:40:14 -05:00
|
|
|
}
|
|
|
|
|
2016-10-14 07:37:43 -05:00
|
|
|
_ws.reset();
|
2016-11-13 15:12:01 -06:00
|
|
|
if (_pid != -1 && rude && kill(_pid, 0) != 0 && errno != ESRCH)
|
2016-10-14 07:37:43 -05:00
|
|
|
{
|
2016-11-13 15:12:01 -06:00
|
|
|
LOG_INF("Killing child [" << _pid << "].");
|
2016-11-14 07:58:04 -06:00
|
|
|
if (SigUtil::killChild(_pid))
|
2016-10-14 07:37:43 -05:00
|
|
|
{
|
2016-11-13 15:12:01 -06:00
|
|
|
LOG_ERR("Cannot terminate lokit [" << _pid << "]. Abandoning.");
|
2016-10-14 07:37:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const std::exception& ex)
|
|
|
|
{
|
2016-11-06 10:59:59 -06:00
|
|
|
LOG_ERR("Error while closing child process: " << ex.what());
|
2016-04-03 20:40:14 -05:00
|
|
|
}
|
2016-11-13 15:12:01 -06:00
|
|
|
|
|
|
|
_pid = -1;
|
2016-04-03 20:40:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Poco::Process::PID getPid() const { return _pid; }
|
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)
|
|
|
|
{
|
|
|
|
LOG_TRC("DocBroker to Child: " << data);
|
|
|
|
_ws->sendFrame(data.data(), data.size());
|
|
|
|
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 [" <<
|
|
|
|
data << "] due to: " << exc.what());
|
2016-10-11 17:22:51 -05:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2016-11-06 20:16:11 -06:00
|
|
|
LOG_WRN("No socket between DocBroker and child to send [" << 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
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2016-11-08 23:47:42 -06:00
|
|
|
return (_pid > 1 && _ws && kill(_pid, 0) == 0 &&
|
2016-12-21 09:09:14 -06:00
|
|
|
!_ws->poll(Poco::Timespan(0), Poco::Net::Socket::SelectMode::SELECT_ERROR));
|
2016-04-24 10:56:02 -05:00
|
|
|
}
|
|
|
|
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;
|
2016-11-10 02:47:25 -06:00
|
|
|
std::shared_ptr<LOOLWebSocket> _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: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
|
|
|
{
|
|
|
|
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
|
|
|
|
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,
|
2016-10-14 21:52:33 -05:00
|
|
|
const std::shared_ptr<ChildProcess>& childProcess);
|
2016-03-12 18:29:17 -06:00
|
|
|
|
2016-10-22 09:25:57 -05:00
|
|
|
~DocumentBroker();
|
2016-03-12 18:29:17 -06:00
|
|
|
|
|
|
|
/// Loads a document from the public URI into the jail.
|
2016-11-06 22:14:23 -06:00
|
|
|
bool load(std::shared_ptr<ClientSession>& session, 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-10-16 11:40:52 -05:00
|
|
|
bool save(const std::string& sesionId, 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.
|
2016-11-05 21:15:44 -05:00
|
|
|
bool autoSave(const bool force, const size_t waitTimeoutMs, std::unique_lock<std::mutex>& lock);
|
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-11-05 21:15:44 -05:00
|
|
|
Util::assertIsLocked(_mutex);
|
2016-04-16 16:18:51 -05:00
|
|
|
return _sessions.size();
|
2016-04-10 12:03:57 -05:00
|
|
|
}
|
2016-03-10 20:42:33 -06:00
|
|
|
|
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-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-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);
|
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.
|
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; }
|
|
|
|
|
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-11-05 22:00:16 -05:00
|
|
|
/// This gracefully terminates the connection
|
|
|
|
/// with the child and cleans up ChildProcess etc.
|
|
|
|
/// We must be called under lock and it must be
|
|
|
|
/// passed to us so we unlock before waiting on
|
|
|
|
/// the ChildProcess thread, which can take our lock.
|
2016-11-08 07:37:28 -06:00
|
|
|
void terminateChild(std::unique_lock<std::mutex>& lock, const std::string& closeReason = "");
|
2016-11-05 22:00:16 -05:00
|
|
|
|
2016-10-17 06:45:58 -05:00
|
|
|
/// Get the PID of the associated child process
|
|
|
|
Poco::Process::PID getPid() const { return _childProcess->getPid(); }
|
|
|
|
|
2016-11-05 21:15:44 -05:00
|
|
|
std::unique_lock<std::mutex> getLock() { return std::unique_lock<std::mutex>(_mutex); }
|
|
|
|
|
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
|
|
|
|
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;
|
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
|
|
|
|
|
|
|
/// 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;
|
2016-10-29 20:15:00 -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-10-11 07:39:56 -05:00
|
|
|
int _debugRenderedTileCount;
|
|
|
|
|
2017-01-01 18:42:19 -06:00
|
|
|
std::chrono::steady_clock::time_point _lastActivityTime;
|
2016-12-14 10:12:57 -06:00
|
|
|
|
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: */
|