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 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/.
|
|
|
|
*/
|
|
|
|
|
2020-04-18 03:39:50 -05:00
|
|
|
#pragma once
|
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
|
|
|
|
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"
|
2020-11-15 15:47:35 -06:00
|
|
|
#include "Storage.hpp"
|
2016-03-25 21:56:18 -05:00
|
|
|
|
2016-11-14 07:58:04 -06:00
|
|
|
#include "common/SigUtil.hpp"
|
2020-01-02 15:11:54 -06:00
|
|
|
#include "common/Session.hpp"
|
2016-11-14 07:58:04 -06:00
|
|
|
|
2019-11-28 10:09:58 -06:00
|
|
|
#if !MOBILEAPP
|
2019-11-12 03:50:33 -06:00
|
|
|
#include "Admin.hpp"
|
2019-11-28 10:09:58 -06:00
|
|
|
#endif
|
2019-11-12 03:50:33 -06:00
|
|
|
|
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;
|
2019-11-21 10:28:19 -06:00
|
|
|
struct LockContext;
|
2016-12-21 14:37:22 -06:00
|
|
|
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
|
|
|
|
{
|
2019-08-08 02:10:59 -05:00
|
|
|
return SocketPoll::continuePolling() && !SigUtil::getTerminationFlag();
|
2017-03-07 11:08:54 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-02 10:11:36 -05:00
|
|
|
#include "LOOLWSD.hpp"
|
|
|
|
|
2020-04-18 08:44:50 -05:00
|
|
|
/// A ChildProcess object represents a KIT process that hosts a document and manipulates the
|
|
|
|
/// document using the LibreOfficeKit API. It isn't actually a child of the WSD process, but a
|
|
|
|
/// grandchild. The comments loosely talk about "child" anyway.
|
|
|
|
|
2020-04-02 10:11:36 -05:00
|
|
|
class ChildProcess : public WSProcess
|
2016-04-03 20:40:14 -05:00
|
|
|
{
|
|
|
|
public:
|
2016-04-29 05:06:45 -05:00
|
|
|
/// @param pid is the process ID of the child.
|
2020-10-31 14:08:08 -05:00
|
|
|
/// @param socket is the underlying Socket to the child.
|
2019-11-26 07:15:38 -06:00
|
|
|
ChildProcess(const pid_t pid,
|
2017-05-07 10:05:34 -05:00
|
|
|
const std::string& jailId,
|
|
|
|
const std::shared_ptr<StreamSocket>& socket,
|
|
|
|
const Poco::Net::HTTPRequest &request) :
|
2020-04-02 10:11:36 -05:00
|
|
|
WSProcess("ChildProcess", pid, socket, std::make_shared<WebSocketHandler>(socket, request)),
|
2020-04-23 05:35:42 -05:00
|
|
|
_jailId(jailId),
|
|
|
|
_smapsFD(-1)
|
2016-04-03 20:40:14 -05:00
|
|
|
{
|
|
|
|
}
|
2018-09-11 01:30:55 -05:00
|
|
|
|
2016-05-01 19:30:52 -05:00
|
|
|
ChildProcess(ChildProcess&& other) = delete;
|
2016-04-03 20:40:14 -05:00
|
|
|
|
2020-04-23 05:35:42 -05:00
|
|
|
virtual ~ChildProcess(){ ::close(_smapsFD); }
|
|
|
|
|
2016-05-01 19:30:52 -05:00
|
|
|
const ChildProcess& operator=(ChildProcess&& other) = delete;
|
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(); }
|
2017-05-07 10:05:34 -05:00
|
|
|
const std::string& getJailId() const { return _jailId; }
|
2020-04-23 05:35:42 -05:00
|
|
|
void setSMapsFD(int smapsFD) { _smapsFD = smapsFD;}
|
|
|
|
int getSMapsFD(){ return _smapsFD; }
|
2016-10-11 17:22:51 -05:00
|
|
|
|
2016-04-03 20:40:14 -05:00
|
|
|
private:
|
2017-05-07 10:05:34 -05:00
|
|
|
const std::string _jailId;
|
2016-05-02 06:21:30 -05:00
|
|
|
std::weak_ptr<DocumentBroker> _docBroker;
|
2020-04-23 05:35:42 -05:00
|
|
|
int _smapsFD;
|
2016-04-03 20:40:14 -05:00
|
|
|
};
|
|
|
|
|
2020-05-12 15:10:56 -05:00
|
|
|
class RequestDetails;
|
2016-05-16 06:46:27 -05:00
|
|
|
class ClientSession;
|
2016-05-16 06:37:02 -05:00
|
|
|
|
2020-04-18 02:41:39 -05: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.
|
2020-04-18 02:41:39 -05:00
|
|
|
|
|
|
|
/// There is one DocumentBroker object in the WSD process for each document that is open (in 1..n sessions).
|
|
|
|
|
2021-01-26 19:40:22 -06:00
|
|
|
|
|
|
|
/// To disambiguate between Storage and Core, we
|
|
|
|
/// use 'Download' for Reading from the Storage,
|
|
|
|
/// and 'Load' for Loading a document in Core.
|
|
|
|
/// Similarly, we 'Upload' to Storage after we
|
|
|
|
/// 'Save' the document in Core.
|
|
|
|
|
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;
|
2020-05-02 13:13:14 -05:00
|
|
|
|
|
|
|
void setupPriorities();
|
|
|
|
|
2016-03-10 20:42:33 -06:00
|
|
|
public:
|
2020-05-02 13:13:14 -05:00
|
|
|
/// How to prioritize this document.
|
|
|
|
enum class ChildType {
|
|
|
|
Interactive, Batch
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2020-05-02 13:13:14 -05:00
|
|
|
DocumentBroker(ChildType type,
|
|
|
|
const std::string& uri,
|
2017-02-05 20:59:08 -06:00
|
|
|
const Poco::URI& uriPublic,
|
2020-04-24 02:46:54 -05:00
|
|
|
const std::string& docKey,
|
|
|
|
unsigned mobileAppDocId = 0);
|
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
|
|
|
|
2019-10-04 04:30:49 -05:00
|
|
|
/// Called when removed from the DocBrokers list
|
|
|
|
virtual void dispose() {}
|
|
|
|
|
2020-12-04 11:43:47 -06:00
|
|
|
/// setup the transfer of a socket into this DocumentBroker poll.
|
|
|
|
void setupTransfer(SocketDisposition &disposition,
|
|
|
|
SocketDisposition::MoveFunction transferFn);
|
2017-03-13 07:00:31 -05:00
|
|
|
|
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
|
|
|
|
2019-07-04 04:50:33 -05:00
|
|
|
/// Hard removes a session by ID, only for ClientSession.
|
|
|
|
void finalRemoveSession(const std::string& id);
|
|
|
|
|
2020-03-04 15:56:48 -06:00
|
|
|
/// Create new client session
|
2020-03-06 11:43:46 -06:00
|
|
|
std::shared_ptr<ClientSession> createNewClientSession(
|
|
|
|
const std::shared_ptr<ProtocolHandlerInterface> &ws,
|
|
|
|
const std::string& id,
|
|
|
|
const Poco::URI& uriPublic,
|
|
|
|
const bool isReadOnly,
|
2020-05-12 15:10:56 -05:00
|
|
|
const RequestDetails &requestDetails);
|
2020-03-04 15:56:48 -06:00
|
|
|
|
2020-03-04 07:54:04 -06:00
|
|
|
/// Find or create a new client session for the PHP proxy
|
|
|
|
void handleProxyRequest(
|
|
|
|
const std::string& id,
|
|
|
|
const Poco::URI& uriPublic,
|
|
|
|
const bool isReadOnly,
|
2020-05-12 15:10:56 -05:00
|
|
|
const RequestDetails &requestDetails,
|
2020-05-09 13:41:40 -05:00
|
|
|
const std::shared_ptr<StreamSocket> &socket);
|
2020-03-04 07:54:04 -06:00
|
|
|
|
2017-03-31 14:58:33 -05:00
|
|
|
/// Thread safe termination of this broker if it has a lingering thread
|
|
|
|
void joinThread();
|
|
|
|
|
2019-12-11 23:09:35 -06:00
|
|
|
/// Notify that the load has completed
|
|
|
|
virtual void setLoaded();
|
2016-03-12 18:29:17 -06:00
|
|
|
|
2020-07-14 14:19:14 -05:00
|
|
|
/// If not yet locked, try to lock
|
|
|
|
bool attemptLock(const ClientSession& session, std::string& failReason);
|
|
|
|
|
2017-06-01 07:56:54 -05:00
|
|
|
bool isDocumentChangedInStorage() { return _documentChangedInStorage; }
|
|
|
|
|
2021-01-26 19:40:48 -06:00
|
|
|
bool isLastStorageUploadSuccessful() { return _lastStorageUploadSuccessful; }
|
2020-08-17 04:00:38 -05:00
|
|
|
|
2021-01-26 19:43:57 -06:00
|
|
|
/// Upload the document to Storage if it needs persisting.
|
2020-12-06 12:17:23 -06:00
|
|
|
/// Results are logged and broadcast to users.
|
2021-01-26 19:43:57 -06:00
|
|
|
void uploadToStorage(const std::string& sesionId, bool success, const std::string& result,
|
|
|
|
bool force);
|
2017-10-20 11:12:05 -05:00
|
|
|
|
2021-01-26 19:47:23 -06:00
|
|
|
/// UploadAs the document to Storage, with a new name.
|
|
|
|
/// @param uploadAsPath Absolute path to the jailed file.
|
|
|
|
void uploadAsToStorage(const std::string& sesionId, const std::string& uploadAsPath,
|
|
|
|
const std::string& uploadAsFilename, 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.
|
2019-06-06 04:48:54 -05:00
|
|
|
/// @param dontSaveIfUnmodified when true, save will fail if the document is not modified.
|
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.
|
2019-06-06 04:48:54 -05:00
|
|
|
bool autoSave(const bool force, const bool dontSaveIfUnmodified = true);
|
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; }
|
wsd: avoid UB in DocumentBroker::cancelTileRequests()
With this, it's possible to open a document and type keys with
sanitizers enabled.
/home/vmiklos/lode/opt_private/gcc-7.3.0/lib/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../include/c++/7.3.0/bits/unique_ptr.h:323:9: runtime error: reference binding to null pointer of type 'TileCache'
#0 0x911996 in std::unique_ptr<TileCache, std::default_delete<TileCache> >::operator*() const /home/vmiklos/lode/opt_private/gcc-7.3.0/lib/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../include/c++/7.3.0/bits/unique_ptr.h:323:2
#1 0x8ecb2a in DocumentBroker::tileCache() /home/vmiklos/lode/dev/online/./wsd/DocumentBroker.hpp:265:37
#2 0x8c6a63 in DocumentBroker::cancelTileRequests(std::shared_ptr<ClientSession> const&) /home/vmiklos/lode/dev/online/wsd/DocumentBroker.cpp:1586:37
#3 0xb32b0e in ClientSession::_handleInput(char const*, int) /home/vmiklos/lode/dev/online/wsd/ClientSession.cpp:194:20
#4 0xd45d3c in Session::handleMessage(bool, WSOpCode, std::vector<char, std::allocator<char> >&) /home/vmiklos/lode/dev/online/common/Session.cpp:219:13
#5 0x768080 in WebSocketHandler::handleTCPStream(std::shared_ptr<StreamSocket> const&) /home/vmiklos/lode/dev/online/./net/WebSocketHandler.hpp:368:13
#6 0x6f800d in WebSocketHandler::handleIncomingMessage(SocketDisposition&) /home/vmiklos/lode/dev/online/./net/WebSocketHandler.hpp:419:20
#7 0xb2c644 in ClientSession::handleIncomingMessage(SocketDisposition&) /home/vmiklos/lode/dev/online/wsd/ClientSession.cpp:74:14
#8 0xa6f641 in StreamSocket::handlePoll(SocketDisposition&, std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long, std::ratio<1l, 1000000000l> > >, int) /home/vmiklos/lode/dev/online/./net/Socket.hpp:1037:29
#9 0x6ec63d in SocketPoll::poll(int) /home/vmiklos/lode/dev/online/./net/Socket.hpp:570:34
#10 0x83af99 in DocumentBroker::pollThread() /home/vmiklos/lode/dev/online/wsd/DocumentBroker.cpp:387:16
#11 0x8fc778 in DocumentBroker::DocumentBrokerPoll::pollingThread() /home/vmiklos/lode/dev/online/wsd/DocumentBroker.cpp:165:20
#12 0xdff935 in SocketPoll::pollingThreadEntry() /home/vmiklos/lode/dev/online/net/Socket.cpp:184:9
#13 0xe487bd in void std::__invoke_impl<void, void (SocketPoll::*)(), SocketPoll*>(std::__invoke_memfun_deref, void (SocketPoll::*&&)(), SocketPoll*&&) /home/vmiklos/lode/opt_private/gcc-7.3.0/lib/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../include/c++/7.3.0/bits/invoke.h:73:14
#14 0xe482ca in std::__invoke_result<void (SocketPoll::*)(), SocketPoll*>::type std::__invoke<void (SocketPoll::*)(), SocketPoll*>(void (SocketPoll::*&&)(), SocketPoll*&&) /home/vmiklos/lode/opt_private/gcc-7.3.0/lib/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../include/c++/7.3.0/bits/invoke.h:95:14
#15 0xe4817d in decltype(std::__invoke(_S_declval<0ul>(), _S_declval<1ul>())) std::thread::_Invoker<std::tuple<void (SocketPoll::*)(), SocketPoll*> >::_M_invoke<0ul, 1ul>(std::_Index_tuple<0ul, 1ul>) /home/vmiklos/lode/opt_private/gcc-7.3.0/lib/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../include/c++/7.3.0/thread:234:13
#16 0xe47f87 in std::thread::_Invoker<std::tuple<void (SocketPoll::*)(), SocketPoll*> >::operator()() /home/vmiklos/lode/opt_private/gcc-7.3.0/lib/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../include/c++/7.3.0/thread:243:11
#17 0xe4734a in std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (SocketPoll::*)(), SocketPoll*> > >::_M_run() /home/vmiklos/lode/opt_private/gcc-7.3.0/lib/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../include/c++/7.3.0/thread:186:13
#18 0x7f5c2ce55e2e in execute_native_thread_routine /home/vmiklos/lode/packages/gccbuild/x86_64-pc-linux-gnu/libstdc++-v3/src/c++11/../../../../../gcc-7.3.0/libstdc++-v3/src/c++11/thread.cc:83
#19 0x7f5c2c832558 in start_thread (/lib64/libpthread.so.0+0x7558)
#20 0x7f5c2bf4682e in clone (/lib64/libc.so.6+0xf882e)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /home/vmiklos/lode/opt_private/gcc-7.3.0/lib/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../include/c++/7.3.0/bits/unique_ptr.h:323:9 in
Change-Id: Ief574a11c838c77f7f159b1133beeef0deada201
2019-05-24 02:04:07 -05:00
|
|
|
bool hasTileCache() { return _tileCache != nullptr; }
|
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.
|
2020-11-15 11:03:45 -06:00
|
|
|
std::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.
|
2020-11-15 11:03:45 -06:00
|
|
|
std::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
|
|
|
|
2020-11-23 05:07:44 -06:00
|
|
|
/// Sets the log level of kit.
|
|
|
|
void setKitLogLevel(const std::string& level);
|
2020-11-15 06:23:54 -06: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
|
|
|
}
|
|
|
|
|
2020-07-12 11:25:49 -05:00
|
|
|
void invalidateTiles(const std::string& tiles, int normalizedViewId)
|
|
|
|
{
|
|
|
|
// Remove from cache.
|
|
|
|
_tileCache->invalidateTiles(tiles, normalizedViewId);
|
|
|
|
}
|
|
|
|
|
2020-11-13 10:21:08 -06:00
|
|
|
void handleTileRequest(const StringVector &tokens,
|
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);
|
2019-06-22 11:45:36 -05:00
|
|
|
|
|
|
|
enum ClipboardRequest {
|
|
|
|
CLIP_REQUEST_SET,
|
2019-06-25 19:43:07 -05:00
|
|
|
CLIP_REQUEST_GET,
|
|
|
|
CLIP_REQUEST_GET_RICH_HTML_ONLY
|
2019-06-22 11:45:36 -05:00
|
|
|
};
|
|
|
|
void handleClipboardRequest(ClipboardRequest type, const std::shared_ptr<StreamSocket> &socket,
|
|
|
|
const std::string &viewId, const std::string &tag,
|
|
|
|
const std::shared_ptr<std::string> &data);
|
2019-07-04 04:50:33 -05:00
|
|
|
static bool lookupSendClipboardTag(const std::shared_ptr<StreamSocket> &socket,
|
|
|
|
const std::string &tag, bool sendError = false);
|
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-17 06:45:58 -05:00
|
|
|
/// Get the PID of the associated child process
|
2019-11-26 07:15:38 -06:00
|
|
|
pid_t 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); }
|
|
|
|
|
2016-12-05 14:11:07 -06:00
|
|
|
void updateLastActivityTime();
|
|
|
|
|
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,
|
2019-06-16 13:42:11 -05:00
|
|
|
bool isExitSave = false, const std::string& extendedData = std::string());
|
2017-05-15 00:59:04 -05:00
|
|
|
|
2017-06-01 11:11:11 -05:00
|
|
|
/// Sends a message to all sessions
|
2020-11-15 16:22:03 -06:00
|
|
|
void broadcastMessage(const std::string& message) const;
|
2017-06-01 11:11:11 -05:00
|
|
|
|
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);
|
|
|
|
|
2019-06-21 05:47:07 -05:00
|
|
|
/// For testing only [!]
|
|
|
|
std::vector<std::shared_ptr<ClientSession>> getSessionsTestOnlyUnsafe();
|
|
|
|
|
2019-10-28 08:26:15 -05:00
|
|
|
/// Estimate memory usage / bytes
|
2020-11-15 11:03:45 -06:00
|
|
|
std::size_t getMemorySize() const;
|
2019-10-28 08:26:15 -05:00
|
|
|
|
2020-09-03 04:34:13 -05:00
|
|
|
/// Get URL for corresponding download id if registered, or empty string otherwise
|
|
|
|
std::string getDownloadURL(const std::string& downloadId);
|
|
|
|
|
|
|
|
/// Remove download id mapping
|
|
|
|
void unregisterDownloadId(const std::string& downloadId);
|
|
|
|
|
2016-04-25 19:48:02 -05:00
|
|
|
private:
|
2019-11-27 02:43:05 -06:00
|
|
|
/// get the session id of a session that can write the document for save / locking.
|
|
|
|
std::string getWriteableSessionId() const;
|
|
|
|
|
|
|
|
void refreshLock();
|
2017-05-21 18:13:55 -05:00
|
|
|
|
2019-09-29 22:32:12 -05:00
|
|
|
/// Loads a document from the public URI into the jail.
|
2021-01-19 21:04:07 -06:00
|
|
|
bool download(const std::shared_ptr<ClientSession>& session, const std::string& jailId);
|
2019-09-29 22:32:12 -05:00
|
|
|
bool isLoaded() const { return _isLoaded; }
|
|
|
|
|
|
|
|
std::size_t getIdleTimeSecs() const
|
|
|
|
{
|
|
|
|
const auto duration = (std::chrono::steady_clock::now() - _lastActivityTime);
|
|
|
|
return std::chrono::duration_cast<std::chrono::seconds>(duration).count();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> getDeferredLock() { return std::unique_lock<std::mutex>(_mutex, std::defer_lock); }
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
void childSocketTerminated();
|
|
|
|
void handleTileResponse(const std::vector<char>& payload);
|
|
|
|
void handleDialogPaintResponse(const std::vector<char>& payload, bool child);
|
|
|
|
void handleTileCombinedResponse(const std::vector<char>& payload);
|
|
|
|
void handleDialogRequest(const std::string& dialogCmd);
|
|
|
|
|
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.
|
2020-12-06 12:17:23 -06:00
|
|
|
void saveToStorageInternal(const std::string& sesionId, bool success,
|
2019-10-22 09:28:57 -05:00
|
|
|
const std::string& result = std::string(),
|
|
|
|
const std::string& saveAsPath = std::string(),
|
|
|
|
const std::string& saveAsFilename = std::string(),
|
|
|
|
const bool isRename = false, const bool force = false);
|
2017-03-09 22:50:36 -06:00
|
|
|
|
2020-11-15 15:47:35 -06:00
|
|
|
struct StorageUploadDetails
|
|
|
|
{
|
|
|
|
const std::string uriAnonym;
|
|
|
|
const std::chrono::system_clock::time_point newFileModifiedTime;
|
2020-11-15 19:58:58 -06:00
|
|
|
const std::weak_ptr<class ClientSession> session;
|
2020-11-15 15:47:35 -06:00
|
|
|
const bool isSaveAs;
|
|
|
|
const bool isRename;
|
|
|
|
};
|
|
|
|
|
2020-12-06 12:17:23 -06:00
|
|
|
void handleUploadToStorageResponse(const StorageUploadDetails& details,
|
2020-11-29 18:11:59 -06:00
|
|
|
const StorageBase::UploadResult& storageSaveResult);
|
2020-11-15 15:47:35 -06:00
|
|
|
|
2020-07-01 05:46:58 -05:00
|
|
|
/**
|
|
|
|
* Report back the save result to PostMessage users (Action_Save_Resp)
|
|
|
|
* @param success: Whether saving was successful
|
|
|
|
* @param result: Short message why saving was (not) successful
|
|
|
|
* @param errorMsg: Long error msg (Error message from WOPI host if any)
|
|
|
|
*/
|
|
|
|
void broadcastSaveResult(bool success, const std::string& result = "", const std::string& errorMsg = "");
|
|
|
|
|
2020-11-15 16:22:03 -06:00
|
|
|
/// Broadcasts to all sessions the last modification time of the document.
|
|
|
|
void broadcastLastModificationTime(const std::shared_ptr<ClientSession>& session = nullptr) const;
|
|
|
|
|
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.
|
2020-04-09 09:37:33 -05:00
|
|
|
bool isPossiblyModified() const { return isModified() || (_lastSaveRequestTime < _lastActivityTime); }
|
2018-01-14 23:49:14 -06:00
|
|
|
|
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.
|
2020-11-15 11:03:45 -06:00
|
|
|
std::size_t addSessionInternal(const std::shared_ptr<ClientSession>& session);
|
2017-05-22 14:04:35 -05:00
|
|
|
|
2019-07-04 04:50:33 -05:00
|
|
|
/// Starts the Kit <-> DocumentBroker shutdown handshake
|
|
|
|
void disconnectSessionInternal(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);
|
|
|
|
|
2020-11-22 16:36:44 -06:00
|
|
|
/// Returns true iff this is a Convert-To request.
|
|
|
|
/// This is needed primarily for security reasons,
|
|
|
|
/// because we can't trust the given file-path is
|
|
|
|
/// a convert-to request or doctored to look like one.
|
|
|
|
virtual bool isConvertTo() const { return false; }
|
|
|
|
|
2019-03-01 15:25:44 -06:00
|
|
|
protected:
|
2019-12-11 21:38:50 -06:00
|
|
|
/// Seconds to live for, or 0 forever
|
2020-12-06 13:02:46 -06:00
|
|
|
std::chrono::seconds _limitLifeSeconds;
|
2019-10-04 04:30:49 -05:00
|
|
|
std::string _uriOrig;
|
2021-01-19 21:04:07 -06:00
|
|
|
|
2020-12-03 20:14:30 -06:00
|
|
|
private:
|
2020-05-02 13:13:14 -05:00
|
|
|
/// What type are we: affects priority.
|
|
|
|
ChildType _type;
|
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;
|
|
|
|
|
2021-01-26 19:43:57 -06:00
|
|
|
/// Indicates whether the last uploadToStorage operation was successful.
|
2021-01-26 19:40:48 -06:00
|
|
|
bool _lastStorageUploadSuccessful;
|
2020-08-17 04:00:38 -05:00
|
|
|
|
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.
|
2019-08-30 12:37:55 -05:00
|
|
|
std::chrono::system_clock::time_point _documentLastModifiedTime;
|
2017-01-01 18:42:19 -06:00
|
|
|
|
|
|
|
/// The jailed file last-modified time.
|
2019-08-30 12:37:55 -05:00
|
|
|
std::chrono::system_clock::time_point _lastFileModifiedTime;
|
2018-01-14 17:59:11 -06:00
|
|
|
|
|
|
|
/// All session of this DocBroker by ID.
|
2020-01-02 15:11:54 -06:00
|
|
|
SessionMap<ClientSession> _sessions;
|
2017-03-06 12:05:49 -06:00
|
|
|
|
2020-04-26 15:55:16 -05:00
|
|
|
/// If we set the user-requested initial (on load) settings to be forced.
|
2018-04-24 11:09:37 -05:00
|
|
|
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;
|
2019-11-19 16:51:45 -06:00
|
|
|
std::unique_ptr<LockContext> _lockCtx;
|
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.
|
2020-11-15 11:03:45 -06:00
|
|
|
std::atomic<std::size_t> _tileVersion;
|
2016-05-22 13:31:18 -05:00
|
|
|
|
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;
|
2019-11-12 03:50:33 -06:00
|
|
|
std::chrono::milliseconds _wopiLoadDuration;
|
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;
|
2020-04-24 02:46:54 -05:00
|
|
|
|
|
|
|
// Relevant only in the mobile apps
|
|
|
|
const unsigned _mobileAppDocId;
|
2020-09-03 04:34:13 -05:00
|
|
|
|
|
|
|
// Maps download id -> URL
|
|
|
|
std::map<std::string, std::string> _registeredDownloadLinks;
|
2016-03-10 20:42:33 -06:00
|
|
|
};
|
|
|
|
|
2020-05-05 12:38:04 -05:00
|
|
|
#if !MOBILEAPP
|
2020-06-03 11:30:40 -05:00
|
|
|
class ConvertToBroker final : public DocumentBroker
|
2019-03-01 15:25:44 -06:00
|
|
|
{
|
2019-12-11 23:09:35 -06:00
|
|
|
const std::string _format;
|
|
|
|
const std::string _sOptions;
|
|
|
|
std::shared_ptr<ClientSession> _clientSession;
|
|
|
|
|
2019-03-01 15:25:44 -06:00
|
|
|
public:
|
|
|
|
/// Construct DocumentBroker with URI and docKey
|
|
|
|
ConvertToBroker(const std::string& uri,
|
|
|
|
const Poco::URI& uriPublic,
|
2019-12-11 23:09:35 -06:00
|
|
|
const std::string& docKey,
|
|
|
|
const std::string& format,
|
|
|
|
const std::string& sOptions);
|
2019-03-01 15:25:44 -06:00
|
|
|
virtual ~ConvertToBroker();
|
2019-03-12 09:41:54 -05:00
|
|
|
|
2019-12-11 23:09:35 -06:00
|
|
|
/// Move socket to this broker for response & do conversion
|
|
|
|
bool startConversion(SocketDisposition &disposition, const std::string &id);
|
|
|
|
|
2019-10-04 04:30:49 -05:00
|
|
|
/// Called when removed from the DocBrokers list
|
|
|
|
void dispose() override;
|
|
|
|
|
2019-12-11 23:09:35 -06:00
|
|
|
/// When the load completes - lets start saving
|
|
|
|
void setLoaded() override;
|
|
|
|
|
2019-03-12 09:41:54 -05:00
|
|
|
/// How many live conversions are running.
|
2020-11-15 11:03:45 -06:00
|
|
|
static std::size_t getInstanceCount();
|
2019-05-21 13:50:17 -05:00
|
|
|
|
|
|
|
/// Cleanup path and its parent
|
|
|
|
static void removeFile(const std::string &uri);
|
2020-11-22 16:36:44 -06:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool isConvertTo() const override { return true; }
|
2019-03-01 15:25:44 -06:00
|
|
|
};
|
2020-05-05 12:38:04 -05:00
|
|
|
#endif
|
2019-03-01 15:25:44 -06:00
|
|
|
|
2016-03-10 20:42:33 -06:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|