2016-03-23 07:41:18 -05: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/.
|
|
|
|
*/
|
|
|
|
|
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 "DocumentBroker.hpp"
|
|
|
|
#include "config.h"
|
|
|
|
|
2016-03-31 01:48:34 -05:00
|
|
|
#include <cassert>
|
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 <fstream>
|
2016-03-31 01:48:34 -05:00
|
|
|
|
2016-03-23 07:41:18 -05:00
|
|
|
#include <Poco/Path.h>
|
2016-03-26 06:50:13 -05:00
|
|
|
#include <Poco/SHA1Engine.h>
|
2016-03-23 07:41: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 "ClientSession.hpp"
|
2016-04-17 10:05:39 -05:00
|
|
|
#include "Exceptions.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 "LOOLProtocol.hpp"
|
2016-04-17 10:05:39 -05:00
|
|
|
#include "LOOLWSD.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"
|
|
|
|
#include "PrisonerSession.hpp"
|
2016-03-25 21:56:18 -05:00
|
|
|
#include "Storage.hpp"
|
|
|
|
#include "TileCache.hpp"
|
2016-05-20 21:31:53 -05:00
|
|
|
#include "Unit.hpp"
|
2016-05-01 19:50:11 -05:00
|
|
|
|
|
|
|
using namespace LOOLProtocol;
|
|
|
|
|
|
|
|
void ChildProcess::socketProcessor()
|
|
|
|
{
|
|
|
|
IoUtil::SocketProcessor(_ws,
|
|
|
|
[this](const std::vector<char>& payload)
|
|
|
|
{
|
2016-05-20 21:31:53 -05:00
|
|
|
if (UnitWSD::get().filterChildMessage(payload))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-01 19:50:11 -05:00
|
|
|
auto docBroker = this->_docBroker.lock();
|
|
|
|
if (docBroker)
|
|
|
|
{
|
|
|
|
return docBroker->handleInput(payload);
|
|
|
|
}
|
|
|
|
|
2016-05-07 08:45:57 -05:00
|
|
|
Log::warn() << "Child " << this->_pid << " has no DocumentBroker to handle message: ["
|
|
|
|
<< LOOLProtocol::getAbbreviatedMessage(payload) << "]." << Log::end;
|
2016-05-01 19:50:11 -05:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
[]() { },
|
|
|
|
[this]() { return !!this->_stop; });
|
|
|
|
}
|
2016-03-23 07:41:18 -05:00
|
|
|
|
2016-03-26 06:50:13 -05:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
/// Returns the cache path for a given document URI.
|
|
|
|
std::string getCachePath(const std::string& uri)
|
|
|
|
{
|
|
|
|
Poco::SHA1Engine digestEngine;
|
|
|
|
|
|
|
|
digestEngine.update(uri.c_str(), uri.size());
|
|
|
|
|
|
|
|
return (LOOLWSD::Cache + "/" +
|
|
|
|
Poco::DigestEngine::digestToHex(digestEngine.digest()).insert(3, "/").insert(2, "/").insert(1, "/"));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-04-13 03:05:00 -05:00
|
|
|
Poco::URI DocumentBroker::sanitizeURI(const std::string& uri)
|
2016-03-23 07:41:18 -05:00
|
|
|
{
|
|
|
|
// The URI of the document should be url-encoded.
|
|
|
|
std::string decodedUri;
|
|
|
|
Poco::URI::decode(uri, decodedUri);
|
|
|
|
auto uriPublic = Poco::URI(decodedUri);
|
|
|
|
|
|
|
|
if (uriPublic.isRelative() || uriPublic.getScheme() == "file")
|
|
|
|
{
|
|
|
|
// TODO: Validate and limit access to local paths!
|
|
|
|
uriPublic.normalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uriPublic.getPath().empty())
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Invalid URI.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return uriPublic;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DocumentBroker::getDocKey(const Poco::URI& uri)
|
|
|
|
{
|
|
|
|
// Keep the host as part of the key to close a potential security hole.
|
|
|
|
std::string docKey;
|
|
|
|
Poco::URI::encode(uri.getHost() + uri.getPath(), "", docKey);
|
|
|
|
return docKey;
|
|
|
|
}
|
|
|
|
|
2016-09-15 05:41:57 -05:00
|
|
|
DocumentBroker::DocumentBroker() :
|
|
|
|
_uriPublic(),
|
|
|
|
_docKey(),
|
|
|
|
_childRoot(),
|
|
|
|
_cacheRoot(),
|
|
|
|
_childProcess(),
|
|
|
|
_lastSaveTime(std::chrono::steady_clock::now()),
|
|
|
|
_markToDestroy(true),
|
|
|
|
_lastEditableSession(true),
|
|
|
|
_cursorPosX(0),
|
|
|
|
_cursorPosY(0),
|
|
|
|
_cursorWidth(0),
|
|
|
|
_cursorHeight(0),
|
|
|
|
_isLoaded(false),
|
|
|
|
_isModified(false),
|
|
|
|
_tileVersion(0)
|
|
|
|
{
|
|
|
|
Log::info("Empty DocumentBroker (marked to destroy) created.");
|
|
|
|
}
|
|
|
|
|
2016-03-23 07:41:18 -05:00
|
|
|
DocumentBroker::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-23 07:41:18 -05:00
|
|
|
_uriPublic(uriPublic),
|
|
|
|
_docKey(docKey),
|
|
|
|
_childRoot(childRoot),
|
2016-03-26 06:50:13 -05:00
|
|
|
_cacheRoot(getCachePath(uriPublic.toString())),
|
2016-07-18 03:25:05 -05:00
|
|
|
_childProcess(std::move(childProcess)),
|
2016-04-24 10:08:08 -05:00
|
|
|
_lastSaveTime(std::chrono::steady_clock::now()),
|
2016-04-21 23:11:24 -05:00
|
|
|
_markToDestroy(false),
|
2016-07-10 23:50:25 -05:00
|
|
|
_lastEditableSession(false),
|
2016-05-22 15:47:22 -05:00
|
|
|
_cursorPosX(0),
|
|
|
|
_cursorPosY(0),
|
2016-09-01 15:15:13 -05:00
|
|
|
_cursorWidth(0),
|
|
|
|
_cursorHeight(0),
|
2016-04-29 22:07:09 -05:00
|
|
|
_isLoaded(false),
|
2016-05-22 13:31:18 -05:00
|
|
|
_isModified(false),
|
|
|
|
_tileVersion(0)
|
2016-03-23 07:41:18 -05:00
|
|
|
{
|
|
|
|
assert(!_docKey.empty());
|
|
|
|
assert(!_childRoot.empty());
|
2016-05-02 06:21:30 -05:00
|
|
|
|
2016-03-23 07:41:18 -05:00
|
|
|
Log::info("DocumentBroker [" + _uriPublic.toString() + "] created. DocKey: [" + _docKey + "]");
|
|
|
|
}
|
|
|
|
|
2016-08-29 08:41:37 -05:00
|
|
|
const StorageBase::FileInfo DocumentBroker::validate(const Poco::URI& uri)
|
2016-03-23 07:41:18 -05:00
|
|
|
{
|
2016-08-31 19:23:56 -05:00
|
|
|
const auto uriString = uri.toString();
|
|
|
|
Log::info("Validating: " + uriString);
|
2016-04-17 10:05:39 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
auto storage = StorageBase::create("", "", uri);
|
2016-08-29 08:41:37 -05:00
|
|
|
auto fileinfo = storage->getFileInfo(uri);
|
2016-08-31 19:23:56 -05:00
|
|
|
Log::info("After checkfileinfo: " + uriString + " -> " + fileinfo._filename);
|
2016-08-31 10:05:10 -05:00
|
|
|
if (!fileinfo.isValid())
|
2016-04-17 10:05:39 -05:00
|
|
|
{
|
2016-08-31 19:23:56 -05:00
|
|
|
Log::error("Invalid file info for uri " + uriString);
|
2016-04-17 10:05:39 -05:00
|
|
|
throw BadRequestException("Invalid URI or access denied.");
|
|
|
|
}
|
2016-08-29 08:41:37 -05:00
|
|
|
|
|
|
|
return fileinfo;
|
2016-04-17 10:05:39 -05:00
|
|
|
}
|
2016-08-31 19:23:56 -05:00
|
|
|
catch (const std::exception& ex)
|
2016-04-06 23:32:28 -05:00
|
|
|
{
|
2016-08-31 19:23:56 -05:00
|
|
|
Log::error("Exception while getting file info for uri " + uriString + ": " + ex.what());
|
2016-04-17 10:05:39 -05:00
|
|
|
throw BadRequestException("Invalid URI or access denied.");
|
2016-04-06 23:32:28 -05:00
|
|
|
}
|
2016-03-23 07:41:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DocumentBroker::load(const std::string& jailId)
|
|
|
|
{
|
|
|
|
Log::debug("Loading from URI: " + _uriPublic.toString());
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2016-04-17 22:29:03 -05:00
|
|
|
if (_markToDestroy)
|
|
|
|
{
|
|
|
|
// Tearing down.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-23 07:41:18 -05:00
|
|
|
if (_storage)
|
|
|
|
{
|
|
|
|
// Already loaded. Nothing to do.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
_jailId = jailId;
|
|
|
|
|
|
|
|
// The URL is the publicly visible one, not visible in the chroot jail.
|
|
|
|
// We need to map it to a jailed path and copy the file there.
|
|
|
|
|
|
|
|
// user/doc/jailId
|
2016-03-28 06:01:19 -05:00
|
|
|
const auto jailPath = Poco::Path(JAILED_DOCUMENT_ROOT, jailId);
|
2016-08-01 03:05:37 -05:00
|
|
|
std::string jailRoot = getJailRoot();
|
2016-03-23 07:41:18 -05:00
|
|
|
|
|
|
|
Log::info("jailPath: " + jailPath.toString() + ", jailRoot: " + jailRoot);
|
|
|
|
|
2016-08-01 03:05:37 -05:00
|
|
|
if (LOOLWSD::NoCapsForKit)
|
|
|
|
jailRoot = jailPath.toString() + "/" + getJailRoot();
|
|
|
|
|
2016-04-25 19:44:24 -05:00
|
|
|
auto storage = StorageBase::create(jailRoot, jailPath.toString(), _uriPublic);
|
2016-04-07 15:59:27 -05:00
|
|
|
if (storage)
|
|
|
|
{
|
|
|
|
const auto fileInfo = storage->getFileInfo(_uriPublic);
|
2016-04-19 02:10:07 -05:00
|
|
|
_filename = fileInfo._filename;
|
2016-03-25 21:56:18 -05:00
|
|
|
|
2016-04-25 19:44:24 -05:00
|
|
|
const auto localPath = storage->loadStorageFileToLocal();
|
2016-04-07 15:59:27 -05:00
|
|
|
_uriJailed = Poco::URI(Poco::URI("file://"), localPath);
|
2016-03-23 07:41:18 -05:00
|
|
|
|
2016-04-25 19:44:24 -05:00
|
|
|
// Use the local temp file's timestamp.
|
|
|
|
_lastFileModifiedTime = Poco::File(storage->getLocalRootPath()).getLastModified();
|
|
|
|
_tileCache.reset(new TileCache(_uriPublic.toString(), _lastFileModifiedTime, _cacheRoot));
|
|
|
|
|
2016-07-18 03:25:05 -05:00
|
|
|
_storage = std::move(storage);
|
2016-04-07 15:59:27 -05:00
|
|
|
return true;
|
|
|
|
}
|
2016-04-10 12:03:57 -05:00
|
|
|
|
|
|
|
return false;
|
2016-03-23 07:41:18 -05:00
|
|
|
}
|
|
|
|
|
2016-07-14 04:49:21 -05:00
|
|
|
bool DocumentBroker::save(bool success, const std::string& result)
|
2016-03-23 07:41:18 -05:00
|
|
|
{
|
2016-04-10 21:07:09 -05:00
|
|
|
std::unique_lock<std::mutex> lock(_saveMutex);
|
|
|
|
|
2016-04-10 12:03:57 -05:00
|
|
|
const auto uri = _uriPublic.toString();
|
2016-04-27 19:52:32 -05:00
|
|
|
|
2016-07-14 04:49:21 -05:00
|
|
|
// If save requested, but core didn't save because document was unmodified
|
|
|
|
// notify the waiting thread, if any.
|
|
|
|
if (!success && result == "unmodified")
|
|
|
|
{
|
|
|
|
Log::debug() << "Save skipped as document was not modified";
|
|
|
|
_saveCV.notify_all();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-10 23:50:25 -05:00
|
|
|
// If we aren't destroying the last editable session just yet, and the file
|
2016-05-09 20:15:09 -05:00
|
|
|
// timestamp hasn't changed, skip saving.
|
2016-04-25 19:44:24 -05:00
|
|
|
const auto newFileModifiedTime = Poco::File(_storage->getLocalRootPath()).getLastModified();
|
2016-07-10 23:50:25 -05:00
|
|
|
if (!isLastEditableSession() && newFileModifiedTime == _lastFileModifiedTime)
|
2016-04-25 19:44:24 -05:00
|
|
|
{
|
|
|
|
// Nothing to do.
|
2016-05-08 09:07:17 -05:00
|
|
|
Log::debug() << "Skipping unnecessary saving to URI [" << uri
|
2016-05-09 20:15:09 -05:00
|
|
|
<< "]. File last modified "
|
|
|
|
<< _lastFileModifiedTime.elapsed() / 1000000
|
|
|
|
<< " seconds ago." << Log::end;
|
2016-04-25 19:44:24 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-10 12:03:57 -05:00
|
|
|
Log::debug("Saving to URI [" + uri + "].");
|
2016-03-23 07:41:18 -05:00
|
|
|
|
2016-03-26 08:10:53 -05:00
|
|
|
assert(_storage && _tileCache);
|
|
|
|
if (_storage->saveLocalFileToStorage())
|
|
|
|
{
|
2016-04-21 23:11:24 -05:00
|
|
|
_isModified = false;
|
2016-04-22 06:00:11 -05:00
|
|
|
_tileCache->setUnsavedChanges(false);
|
2016-04-25 19:44:24 -05:00
|
|
|
_lastFileModifiedTime = newFileModifiedTime;
|
|
|
|
_tileCache->saveLastModified(_lastFileModifiedTime);
|
|
|
|
_lastSaveTime = std::chrono::steady_clock::now();
|
2016-04-10 12:03:57 -05:00
|
|
|
Log::debug("Saved to URI [" + uri + "] and updated tile cache.");
|
2016-04-10 21:07:09 -05:00
|
|
|
_saveCV.notify_all();
|
2016-03-26 08:10:53 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-10 12:03:57 -05:00
|
|
|
Log::error("Failed to save to URI [" + uri + "].");
|
2016-03-26 08:10:53 -05:00
|
|
|
return false;
|
2016-03-23 07:41:18 -05:00
|
|
|
}
|
|
|
|
|
2016-04-25 19:48:02 -05:00
|
|
|
bool DocumentBroker::autoSave(const bool force, const size_t waitTimeoutMs)
|
2016-04-09 22:20:20 -05:00
|
|
|
{
|
2016-04-16 16:18:51 -05:00
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
2016-05-09 00:11:09 -05:00
|
|
|
if (_sessions.empty() || _storage == nullptr || !_isLoaded ||
|
|
|
|
(!_isModified && !force))
|
2016-04-09 22:20:20 -05:00
|
|
|
{
|
2016-04-29 22:07:09 -05:00
|
|
|
// Nothing to do.
|
|
|
|
Log::trace("Nothing to autosave [" + _docKey + "].");
|
|
|
|
return true;
|
2016-04-09 22:20:20 -05:00
|
|
|
}
|
|
|
|
|
2016-05-09 00:11:09 -05:00
|
|
|
// Remeber the last save time, since this is the predicate.
|
|
|
|
const auto lastSaveTime = _lastSaveTime;
|
2016-05-09 20:18:58 -05:00
|
|
|
Log::trace("Checking to autosave [" + _docKey + "].");
|
2016-05-09 00:11:09 -05:00
|
|
|
|
2016-04-25 19:48:02 -05:00
|
|
|
bool sent = false;
|
2016-05-02 21:58:53 -05:00
|
|
|
if (force)
|
2016-04-09 22:20:20 -05:00
|
|
|
{
|
2016-05-09 00:11:09 -05:00
|
|
|
Log::trace("Sending forced save command for [" + _docKey + "].");
|
2016-07-14 04:49:21 -05:00
|
|
|
sent = sendUnoSave(true);
|
2016-04-09 22:20:20 -05:00
|
|
|
}
|
2016-05-02 21:58:53 -05:00
|
|
|
else if (_isModified)
|
2016-04-09 22:20:20 -05:00
|
|
|
{
|
2016-04-25 19:48:02 -05:00
|
|
|
// Find the most recent activity.
|
|
|
|
double inactivityTimeMs = std::numeric_limits<double>::max();
|
|
|
|
for (auto& sessionIt: _sessions)
|
2016-04-09 22:20:20 -05:00
|
|
|
{
|
2016-04-25 19:48:02 -05:00
|
|
|
inactivityTimeMs = std::min(sessionIt.second->getInactivityMS(), inactivityTimeMs);
|
|
|
|
}
|
2016-04-09 22:20:20 -05:00
|
|
|
|
2016-04-25 19:48:02 -05:00
|
|
|
Log::trace("Most recent activity was " + std::to_string((int)inactivityTimeMs) + " ms ago.");
|
|
|
|
const auto timeSinceLastSaveMs = getTimeSinceLastSaveMs();
|
|
|
|
Log::trace("Time since last save is " + std::to_string((int)timeSinceLastSaveMs) + " ms.");
|
2016-04-10 11:30:33 -05:00
|
|
|
|
2016-05-02 21:58:53 -05:00
|
|
|
// Either we've been idle long enough, or it's auto-save time.
|
|
|
|
if (inactivityTimeMs >= IdleSaveDurationMs ||
|
|
|
|
timeSinceLastSaveMs >= AutoSaveDurationMs)
|
2016-04-25 19:48:02 -05:00
|
|
|
{
|
2016-05-09 00:11:09 -05:00
|
|
|
Log::trace("Sending timed save command for [" + _docKey + "].");
|
2016-07-14 04:49:21 -05:00
|
|
|
sent = sendUnoSave(true);
|
2016-04-25 19:48:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sent && waitTimeoutMs > 0)
|
|
|
|
{
|
2016-05-09 00:11:09 -05:00
|
|
|
Log::trace("Waiting for save event for [" + _docKey + "].");
|
2016-04-25 19:48:02 -05:00
|
|
|
if (_saveCV.wait_for(lock, std::chrono::milliseconds(waitTimeoutMs)) == std::cv_status::no_timeout)
|
|
|
|
{
|
2016-07-14 04:49:21 -05:00
|
|
|
Log::debug("Successfully persisted document [" + _docKey + "] or document was not modified");
|
2016-04-25 19:48:02 -05:00
|
|
|
return true;
|
2016-04-09 22:20:20 -05:00
|
|
|
}
|
2016-04-25 19:48:02 -05:00
|
|
|
|
|
|
|
return (lastSaveTime != _lastSaveTime);
|
2016-04-09 22:20:20 -05:00
|
|
|
}
|
2016-04-10 21:07:09 -05:00
|
|
|
|
2016-04-25 19:48:02 -05:00
|
|
|
return sent;
|
2016-04-10 21:07:09 -05:00
|
|
|
}
|
|
|
|
|
2016-07-14 04:49:21 -05:00
|
|
|
bool DocumentBroker::sendUnoSave(const bool dontSaveIfUnmodified)
|
2016-04-10 21:07:09 -05:00
|
|
|
{
|
2016-04-25 19:48:02 -05:00
|
|
|
Log::info("Autosave triggered for doc [" + _docKey + "].");
|
2016-05-02 18:17:46 -05:00
|
|
|
Util::assertIsLocked(_mutex);
|
2016-04-10 21:07:09 -05:00
|
|
|
|
2016-05-17 07:17:21 -05:00
|
|
|
// Save using session holding the edit-lock (or first if multview).
|
2016-04-25 19:48:02 -05:00
|
|
|
for (auto& sessionIt: _sessions)
|
2016-04-10 21:07:09 -05:00
|
|
|
{
|
2016-09-19 02:03:31 -05:00
|
|
|
// Invalidate the timestamp to force persisting.
|
|
|
|
_lastFileModifiedTime.fromEpochTime(0);
|
|
|
|
|
|
|
|
// We do not want save to terminate editing mode if we are in edit mode now
|
2016-05-09 20:15:09 -05:00
|
|
|
|
2016-09-19 02:03:31 -05:00
|
|
|
std::ostringstream oss;
|
|
|
|
// arguments init
|
|
|
|
oss << "{";
|
2016-09-01 07:25:32 -05:00
|
|
|
|
2016-09-19 02:03:31 -05:00
|
|
|
// Mention DontTerminateEdit always
|
|
|
|
oss << "\"DontTerminateEdit\":"
|
|
|
|
<< "{"
|
|
|
|
<< "\"type\":\"boolean\","
|
|
|
|
<< "\"value\":true"
|
|
|
|
<< "}";
|
2016-07-14 04:49:21 -05:00
|
|
|
|
2016-09-19 02:03:31 -05:00
|
|
|
// Mention DontSaveIfUnmodified
|
|
|
|
if (dontSaveIfUnmodified)
|
|
|
|
{
|
|
|
|
oss << ","
|
|
|
|
<< "\"DontSaveIfUnmodified\":"
|
2016-07-14 04:49:21 -05:00
|
|
|
<< "{"
|
|
|
|
<< "\"type\":\"boolean\","
|
|
|
|
<< "\"value\":true"
|
|
|
|
<< "}";
|
2016-09-19 02:03:31 -05:00
|
|
|
}
|
2016-07-14 04:49:21 -05:00
|
|
|
|
2016-09-19 02:03:31 -05:00
|
|
|
// arguments end
|
|
|
|
oss << "}";
|
2016-07-14 04:49:21 -05:00
|
|
|
|
2016-08-31 22:34:41 -05:00
|
|
|
const auto saveArgs = oss.str();
|
|
|
|
Log::trace(".uno:Save arguments: " + saveArgs);
|
|
|
|
const auto command = "uno .uno:Save " + saveArgs;
|
|
|
|
sessionIt.second->handleInput(command.data(), command.size());
|
2016-09-19 02:03:31 -05:00
|
|
|
return true;
|
2016-04-10 21:07:09 -05:00
|
|
|
}
|
|
|
|
|
2016-04-25 19:48:02 -05:00
|
|
|
Log::error("Failed to auto-save doc [" + _docKey + "]: No valid sessions.");
|
|
|
|
return false;
|
2016-04-09 22:20:20 -05:00
|
|
|
}
|
|
|
|
|
2016-03-23 07:41:18 -05:00
|
|
|
std::string DocumentBroker::getJailRoot() const
|
|
|
|
{
|
|
|
|
assert(!_jailId.empty());
|
|
|
|
return Poco::Path(_childRoot, _jailId).toString();
|
|
|
|
}
|
|
|
|
|
2016-05-16 06:46:27 -05:00
|
|
|
size_t DocumentBroker::addSession(std::shared_ptr<ClientSession>& session)
|
2016-03-23 11:55:28 -05:00
|
|
|
{
|
2016-04-16 16:18:51 -05:00
|
|
|
const auto id = session->getId();
|
2016-05-09 20:18:58 -05:00
|
|
|
const std::string aMessage = "session " + id + " " + _docKey + "\n";
|
2016-04-16 16:18:51 -05:00
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
2016-04-10 12:03:57 -05:00
|
|
|
|
2016-04-24 11:40:23 -05:00
|
|
|
// Request a new session from the child kit.
|
|
|
|
Log::debug("DocBroker to Child: " + aMessage.substr(0, aMessage.length() - 1));
|
|
|
|
_childProcess->getWebSocket()->sendFrame(aMessage.data(), aMessage.size());
|
|
|
|
|
2016-04-16 16:18:51 -05:00
|
|
|
auto ret = _sessions.emplace(id, session);
|
2016-03-23 11:55:28 -05:00
|
|
|
if (!ret.second)
|
|
|
|
{
|
2016-04-24 10:08:08 -05:00
|
|
|
Log::warn("DocumentBroker: Trying to add already existing session.");
|
2016-03-23 11:55:28 -05:00
|
|
|
}
|
2016-04-03 20:40:14 -05:00
|
|
|
|
2016-07-10 23:50:25 -05:00
|
|
|
if (session->isReadOnly())
|
|
|
|
{
|
|
|
|
Log::debug("Adding a readonly session [" + id + "]");
|
|
|
|
}
|
2016-07-10 23:40:23 -05:00
|
|
|
|
|
|
|
// Below values are recalculated when startDestroy() is called (before destroying the
|
|
|
|
// document). It is safe to reset their values to their defaults whenever a new session is added
|
|
|
|
_lastEditableSession = false;
|
|
|
|
_markToDestroy = false;
|
2016-04-10 12:35:06 -05:00
|
|
|
|
2016-04-16 16:18:51 -05:00
|
|
|
return _sessions.size();
|
2016-03-23 11:55:28 -05:00
|
|
|
}
|
|
|
|
|
2016-05-16 06:37:02 -05:00
|
|
|
bool DocumentBroker::connectPeers(std::shared_ptr<PrisonerSession>& session)
|
2016-04-24 21:09:13 -05:00
|
|
|
{
|
|
|
|
const auto id = session->getId();
|
|
|
|
|
|
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
|
|
|
|
|
|
|
auto it = _sessions.find(id);
|
|
|
|
if (it != _sessions.end())
|
|
|
|
{
|
|
|
|
it->second->setPeer(session);
|
|
|
|
session->setPeer(it->second);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-16 16:18:51 -05:00
|
|
|
size_t DocumentBroker::removeSession(const std::string& id)
|
2016-03-23 11:55:28 -05:00
|
|
|
{
|
2016-04-16 16:18:51 -05:00
|
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
2016-04-10 12:06:35 -05:00
|
|
|
|
2016-04-16 16:18:51 -05:00
|
|
|
auto it = _sessions.find(id);
|
|
|
|
if (it != _sessions.end())
|
2016-03-23 11:55:28 -05:00
|
|
|
{
|
2016-04-16 16:18:51 -05:00
|
|
|
_sessions.erase(it);
|
2016-03-23 11:55:28 -05:00
|
|
|
}
|
2016-04-16 16:18:51 -05:00
|
|
|
|
|
|
|
return _sessions.size();
|
2016-03-23 11:55:28 -05:00
|
|
|
}
|
|
|
|
|
2016-05-01 19:50:11 -05:00
|
|
|
bool DocumentBroker::handleInput(const std::vector<char>& payload)
|
|
|
|
{
|
2016-09-19 18:18:07 -05:00
|
|
|
const auto msg = LOOLProtocol::getAbbreviatedMessage(payload);
|
|
|
|
Log::trace("DocumentBroker got child message: [" + msg + "].");
|
2016-05-01 19:50:11 -05:00
|
|
|
|
2016-09-19 18:18:07 -05:00
|
|
|
LOOLWSD::dumpOutgoingTrace(getJailId(), "0", msg);
|
2016-07-31 06:54:47 -05:00
|
|
|
|
2016-09-19 18:18:07 -05:00
|
|
|
if (msg.find("tile:") == 0)
|
2016-05-01 19:50:11 -05:00
|
|
|
{
|
|
|
|
handleTileResponse(payload);
|
|
|
|
}
|
2016-09-19 18:18:07 -05:00
|
|
|
else if (msg.find("tilecombine:") == 0)
|
2016-05-22 10:45:28 -05:00
|
|
|
{
|
|
|
|
handleTileCombinedResponse(payload);
|
|
|
|
}
|
2016-09-19 18:18:07 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Log::error("Unexpected message: [" + msg + "].");
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-01 19:50:11 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-22 13:31:18 -05:00
|
|
|
void DocumentBroker::handleTileRequest(TileDesc& tile,
|
2016-05-16 19:49:36 -05:00
|
|
|
const std::shared_ptr<ClientSession>& session)
|
2016-05-01 19:50:11 -05:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2016-05-22 13:31:18 -05:00
|
|
|
tile.setVersion(++_tileVersion);
|
|
|
|
const auto tileMsg = tile.serialize();
|
|
|
|
Log::trace() << "Tile request for " << tile.serialize() << Log::end;
|
2016-05-01 19:50:11 -05:00
|
|
|
|
2016-05-22 13:31:18 -05:00
|
|
|
std::unique_ptr<std::fstream> cachedTile = _tileCache->lookupTile(tile);
|
2016-05-01 19:50:11 -05:00
|
|
|
if (cachedTile)
|
|
|
|
{
|
|
|
|
#if ENABLE_DEBUG
|
2016-05-22 13:31:18 -05:00
|
|
|
const std::string response = tile.serialize("tile:") + " renderid=cached\n";
|
2016-05-04 20:14:39 -05:00
|
|
|
#else
|
2016-08-31 22:41:09 -05:00
|
|
|
const std::string response = tile.serialize("tile:") + '\n';
|
2016-05-01 19:50:11 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
std::vector<char> output;
|
2016-07-18 03:25:05 -05:00
|
|
|
output.reserve(static_cast<size_t>(4) * tile.getWidth() * tile.getHeight());
|
2016-05-01 19:50:11 -05:00
|
|
|
output.resize(response.size());
|
|
|
|
std::memcpy(output.data(), response.data(), response.size());
|
|
|
|
|
|
|
|
assert(cachedTile->is_open());
|
|
|
|
cachedTile->seekg(0, std::ios_base::end);
|
2016-08-31 22:41:09 -05:00
|
|
|
const auto pos = output.size();
|
2016-05-01 19:50:11 -05:00
|
|
|
std::streamsize size = cachedTile->tellg();
|
|
|
|
output.resize(pos + size);
|
|
|
|
cachedTile->seekg(0, std::ios_base::beg);
|
|
|
|
cachedTile->read(output.data() + pos, size);
|
|
|
|
cachedTile->close();
|
|
|
|
|
|
|
|
session->sendBinaryFrame(output.data(), output.size());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-18 09:28:55 -05:00
|
|
|
if (tileCache().subscribeToTileRendering(tile, session) > 0)
|
2016-05-22 13:31:18 -05:00
|
|
|
{
|
|
|
|
Log::debug() << "Sending render request for tile (" << tile.getPart() << ',' << tile.getTilePosX() << ',' << tile.getTilePosY() << ")." << Log::end;
|
2016-05-04 21:05:09 -05:00
|
|
|
|
2016-05-22 13:31:18 -05:00
|
|
|
// Forward to child to render.
|
|
|
|
const std::string request = "tile " + tile.serialize();
|
|
|
|
_childProcess->getWebSocket()->sendFrame(request.data(), request.size());
|
|
|
|
}
|
2016-05-01 19:50:11 -05:00
|
|
|
}
|
|
|
|
|
2016-05-22 10:45:28 -05:00
|
|
|
void DocumentBroker::handleTileCombinedRequest(TileCombined& tileCombined,
|
|
|
|
const std::shared_ptr<ClientSession>& session)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2016-05-22 13:31:18 -05:00
|
|
|
Log::trace() << "TileCombined request for " << tileCombined.serialize() << Log::end;
|
|
|
|
|
2016-05-22 10:45:28 -05:00
|
|
|
// Satisfy as many tiles from the cache.
|
2016-05-29 16:40:27 -05:00
|
|
|
for (auto& tile : tileCombined.getTiles())
|
2016-05-22 10:45:28 -05:00
|
|
|
{
|
|
|
|
std::unique_ptr<std::fstream> cachedTile = _tileCache->lookupTile(tile);
|
|
|
|
if (cachedTile)
|
|
|
|
{
|
2016-08-18 09:28:55 -05:00
|
|
|
//TODO: Combine the response to reduce latency.
|
2016-05-22 10:45:28 -05:00
|
|
|
#if ENABLE_DEBUG
|
|
|
|
const std::string response = tile.serialize("tile:") + " renderid=cached\n";
|
|
|
|
#else
|
|
|
|
const std::string response = tile.serialize("tile:") + "\n";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
std::vector<char> output;
|
2016-07-18 03:25:05 -05:00
|
|
|
output.reserve(static_cast<size_t>(4) * tile.getWidth() * tile.getHeight());
|
2016-05-22 10:45:28 -05:00
|
|
|
output.resize(response.size());
|
|
|
|
std::memcpy(output.data(), response.data(), response.size());
|
|
|
|
|
|
|
|
assert(cachedTile->is_open());
|
|
|
|
cachedTile->seekg(0, std::ios_base::end);
|
2016-09-13 17:40:23 -05:00
|
|
|
const auto pos = output.size();
|
2016-05-22 10:45:28 -05:00
|
|
|
std::streamsize size = cachedTile->tellg();
|
|
|
|
output.resize(pos + size);
|
|
|
|
cachedTile->seekg(0, std::ios_base::beg);
|
|
|
|
cachedTile->read(output.data() + pos, size);
|
|
|
|
cachedTile->close();
|
|
|
|
|
|
|
|
session->sendBinaryFrame(output.data(), output.size());
|
|
|
|
}
|
2016-05-22 13:31:18 -05:00
|
|
|
else
|
2016-05-22 10:45:28 -05:00
|
|
|
{
|
2016-08-18 09:28:55 -05:00
|
|
|
// Not cached, needs rendering.
|
2016-09-19 19:29:40 -05:00
|
|
|
tile.setVersion(++_tileVersion);
|
2016-08-18 09:28:55 -05:00
|
|
|
const auto ver = tileCache().subscribeToTileRendering(tile, session);
|
2016-05-22 13:31:18 -05:00
|
|
|
if (ver <= 0)
|
|
|
|
{
|
2016-05-22 15:47:22 -05:00
|
|
|
// Already rendering. Skip.
|
2016-05-29 16:40:27 -05:00
|
|
|
continue;
|
2016-05-22 15:47:22 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const auto req = tile.serialize("tile");
|
2016-09-19 19:29:40 -05:00
|
|
|
Log::debug() << "Tile request: " << req << Log::end;
|
2016-05-22 15:47:22 -05:00
|
|
|
_childProcess->getWebSocket()->sendFrame(req.data(), req.size());
|
2016-05-22 13:31:18 -05:00
|
|
|
}
|
2016-05-22 10:45:28 -05:00
|
|
|
}
|
2016-05-29 16:40:27 -05:00
|
|
|
}
|
2016-05-22 10:45:28 -05:00
|
|
|
}
|
|
|
|
|
2016-08-30 22:15:44 -05:00
|
|
|
void DocumentBroker::cancelTileRequests(const std::shared_ptr<ClientSession>& session)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
|
|
|
tileCache().cancelTiles(session);
|
|
|
|
}
|
|
|
|
|
2016-05-01 19:50:11 -05:00
|
|
|
void DocumentBroker::handleTileResponse(const std::vector<char>& payload)
|
|
|
|
{
|
|
|
|
const std::string firstLine = getFirstLine(payload);
|
2016-05-15 17:47:08 -05:00
|
|
|
try
|
2016-05-05 09:19:13 -05:00
|
|
|
{
|
2016-05-15 17:47:08 -05:00
|
|
|
auto tile = TileDesc::parse(firstLine);
|
|
|
|
const auto buffer = payload.data();
|
|
|
|
const auto length = payload.size();
|
2016-05-01 19:50:11 -05:00
|
|
|
|
2016-05-22 15:47:22 -05:00
|
|
|
if (firstLine.size() < static_cast<std::string::size_type>(length) - 1)
|
2016-05-15 17:47:08 -05:00
|
|
|
{
|
2016-09-01 15:15:13 -05:00
|
|
|
tileCache().saveTileAndNotify(
|
|
|
|
tile, buffer + firstLine.size() + 1,
|
2016-09-19 19:21:18 -05:00
|
|
|
length - firstLine.size() - 1);
|
2016-05-15 17:47:08 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log::debug() << "Render request declined for " << firstLine << Log::end;
|
|
|
|
std::unique_lock<std::mutex> tileBeingRenderedLock(tileCache().getTilesBeingRenderedLock());
|
|
|
|
tileCache().forgetTileBeingRendered(tile);
|
|
|
|
}
|
2016-05-05 01:37:40 -05:00
|
|
|
}
|
2016-05-15 17:47:08 -05:00
|
|
|
catch (const std::exception& exc)
|
2016-05-05 01:37:40 -05:00
|
|
|
{
|
2016-05-15 17:47:08 -05:00
|
|
|
Log::error("Failed to process tile response [" + firstLine + "]: " + exc.what() + ".");
|
|
|
|
//FIXME: Return error.
|
|
|
|
//sendTextFrame("error: cmd=tile kind=syntax");
|
2016-05-05 01:37:40 -05:00
|
|
|
}
|
2016-05-01 19:50:11 -05:00
|
|
|
}
|
|
|
|
|
2016-05-22 10:45:28 -05:00
|
|
|
void DocumentBroker::handleTileCombinedResponse(const std::vector<char>& payload)
|
|
|
|
{
|
|
|
|
const std::string firstLine = getFirstLine(payload);
|
|
|
|
Log::debug("Handling tile combined: " + firstLine);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto tileCombined = TileCombined::parse(firstLine);
|
|
|
|
const auto buffer = payload.data();
|
|
|
|
const auto length = payload.size();
|
|
|
|
auto offset = firstLine.size() + 1;
|
|
|
|
|
|
|
|
if (firstLine.size() < static_cast<std::string::size_type>(length) - 1)
|
|
|
|
{
|
|
|
|
for (const auto& tile : tileCombined.getTiles())
|
|
|
|
{
|
2016-09-19 19:21:18 -05:00
|
|
|
tileCache().saveTileAndNotify(tile, buffer + offset, tile.getImgSize());
|
2016-05-22 10:45:28 -05:00
|
|
|
offset += tile.getImgSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log::error() << "Render request failed for " << firstLine << Log::end;
|
|
|
|
std::unique_lock<std::mutex> tileBeingRenderedLock(tileCache().getTilesBeingRenderedLock());
|
|
|
|
for (const auto& tile : tileCombined.getTiles())
|
|
|
|
{
|
|
|
|
tileCache().forgetTileBeingRendered(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (const std::exception& exc)
|
|
|
|
{
|
|
|
|
Log::error("Failed to process tile response [" + firstLine + "]: " + exc.what() + ".");
|
|
|
|
//FIXME: Return error.
|
|
|
|
//sendTextFrame("error: cmd=tile kind=syntax");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-10 23:50:25 -05:00
|
|
|
void DocumentBroker::startDestroy(const std::string& id)
|
2016-04-17 22:29:03 -05:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2016-07-10 23:50:25 -05:00
|
|
|
auto currentSession = _sessions.find(id);
|
|
|
|
assert(currentSession != _sessions.end());
|
|
|
|
|
|
|
|
// Check if session which is being destroyed is last non-readonly session
|
2016-07-11 01:27:58 -05:00
|
|
|
bool lastEditableSession = !currentSession->second->isReadOnly();
|
2016-07-10 23:50:25 -05:00
|
|
|
for (auto& it: _sessions)
|
|
|
|
{
|
|
|
|
if (it.second->getId() == id)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!it.second->isReadOnly())
|
|
|
|
{
|
2016-07-11 01:27:58 -05:00
|
|
|
lastEditableSession = false;
|
2016-07-10 23:50:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Last editable session going away
|
2016-07-11 01:27:58 -05:00
|
|
|
_lastEditableSession = lastEditableSession;
|
2016-07-10 23:50:25 -05:00
|
|
|
|
2016-05-08 09:07:17 -05:00
|
|
|
// Last view going away, can destroy.
|
|
|
|
_markToDestroy = (_sessions.size() <= 1);
|
2016-04-17 22:29:03 -05:00
|
|
|
}
|
|
|
|
|
2016-04-22 06:00:11 -05:00
|
|
|
void DocumentBroker::setModified(const bool value)
|
|
|
|
{
|
|
|
|
_tileCache->setUnsavedChanges(value);
|
|
|
|
_isModified = value;
|
|
|
|
}
|
|
|
|
|
2016-03-23 07:41:18 -05:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|