2015-04-13 04:09:02 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
2015-03-04 17:14:04 -06:00
|
|
|
/*
|
|
|
|
* 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/.
|
|
|
|
*/
|
|
|
|
|
2015-04-27 07:51:33 -05:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2015-04-08 09:22:42 -05:00
|
|
|
#include <ftw.h>
|
2015-04-27 07:51:33 -05:00
|
|
|
#include <utime.h>
|
2015-04-08 09:22:42 -05:00
|
|
|
|
2015-03-17 18:56:15 -05:00
|
|
|
#include <cassert>
|
2015-05-07 08:29:36 -05:00
|
|
|
#include <condition_variable>
|
2015-03-12 09:18:35 -05:00
|
|
|
#include <cstring>
|
2015-03-04 17:14:04 -06:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
2015-04-30 07:58:13 -05:00
|
|
|
#include <iterator>
|
2015-04-08 09:22:42 -05:00
|
|
|
#include <map>
|
2015-03-12 09:18:35 -05:00
|
|
|
#include <memory>
|
2015-05-07 08:29:36 -05:00
|
|
|
#include <mutex>
|
2015-04-08 09:22:42 -05:00
|
|
|
#include <set>
|
2015-03-04 17:14:04 -06:00
|
|
|
|
2015-03-26 09:49:07 -05:00
|
|
|
#define LOK_USE_UNSTABLE_API
|
|
|
|
#include <LibreOfficeKit/LibreOfficeKit.h>
|
|
|
|
#include <LibreOfficeKit/LibreOfficeKitEnums.h>
|
|
|
|
|
2015-05-29 00:49:49 -05:00
|
|
|
#include <Poco/Exception.h>
|
2015-04-08 09:22:42 -05:00
|
|
|
#include <Poco/File.h>
|
2015-04-30 07:58:13 -05:00
|
|
|
#include <Poco/Net/HTTPStreamFactory.h>
|
2015-04-08 09:22:42 -05:00
|
|
|
#include <Poco/Net/WebSocket.h>
|
|
|
|
#include <Poco/Path.h>
|
2015-03-17 18:56:15 -05:00
|
|
|
#include <Poco/Process.h>
|
|
|
|
#include <Poco/Random.h>
|
2015-04-30 10:01:21 -05:00
|
|
|
#include <Poco/StreamCopier.h>
|
2015-03-04 17:14:04 -06:00
|
|
|
#include <Poco/String.h>
|
|
|
|
#include <Poco/StringTokenizer.h>
|
2015-05-07 12:15:05 -05:00
|
|
|
#include <Poco/ThreadLocal.h>
|
2015-03-23 15:13:57 -05:00
|
|
|
#include <Poco/URI.h>
|
2015-04-30 07:58:13 -05:00
|
|
|
#include <Poco/URIStreamOpener.h>
|
2015-03-17 18:56:15 -05:00
|
|
|
#include <Poco/Util/Application.h>
|
2015-03-04 17:14:04 -06:00
|
|
|
|
2015-03-28 06:22:15 -05:00
|
|
|
#include "LOKitHelper.hpp"
|
2015-03-26 09:49:07 -05:00
|
|
|
#include "LOOLProtocol.hpp"
|
2015-03-09 03:01:30 -05:00
|
|
|
#include "LOOLSession.hpp"
|
2015-03-17 18:56:15 -05:00
|
|
|
#include "LOOLWSD.hpp"
|
2015-03-12 09:18:35 -05:00
|
|
|
#include "TileCache.hpp"
|
2015-03-17 18:56:15 -05:00
|
|
|
#include "Util.hpp"
|
2015-03-04 17:14:04 -06:00
|
|
|
|
2015-03-26 09:49:07 -05:00
|
|
|
using namespace LOOLProtocol;
|
|
|
|
|
2015-04-08 09:22:42 -05:00
|
|
|
using Poco::File;
|
2015-05-29 00:49:49 -05:00
|
|
|
using Poco::IOException;
|
2015-04-30 07:58:13 -05:00
|
|
|
using Poco::Net::HTTPStreamFactory;
|
2015-03-04 17:14:04 -06:00
|
|
|
using Poco::Net::WebSocket;
|
2015-04-08 09:22:42 -05:00
|
|
|
using Poco::Path;
|
2015-03-17 18:56:15 -05:00
|
|
|
using Poco::Process;
|
|
|
|
using Poco::ProcessHandle;
|
|
|
|
using Poco::Random;
|
2015-04-30 10:01:21 -05:00
|
|
|
using Poco::StreamCopier;
|
2015-03-04 17:14:04 -06:00
|
|
|
using Poco::StringTokenizer;
|
2015-03-17 18:56:15 -05:00
|
|
|
using Poco::Thread;
|
2015-05-07 12:15:05 -05:00
|
|
|
using Poco::ThreadLocal;
|
2015-03-17 18:56:15 -05:00
|
|
|
using Poco::UInt64;
|
2015-03-23 15:13:57 -05:00
|
|
|
using Poco::URI;
|
2015-04-30 07:58:13 -05:00
|
|
|
using Poco::URIStreamOpener;
|
2015-03-17 18:56:15 -05:00
|
|
|
using Poco::Util::Application;
|
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
const std::string LOOLSession::jailDocumentURL = "/user/thedocument";
|
2015-04-08 09:22:42 -05:00
|
|
|
|
2015-05-28 08:42:38 -05:00
|
|
|
LOOLSession::LOOLSession(std::shared_ptr<WebSocket> ws, Kind kind) :
|
2015-04-20 09:43:31 -05:00
|
|
|
_kind(kind),
|
2015-05-28 08:42:38 -05:00
|
|
|
_ws(ws),
|
2015-04-22 11:48:20 -05:00
|
|
|
_docURL("")
|
2015-03-04 17:14:04 -06:00
|
|
|
{
|
2015-05-28 08:42:38 -05:00
|
|
|
std::cout << Util::logPrefix() << "LOOLSession ctor this=" << this << " " << _kind << " ws=" << _ws.get() << std::endl;
|
2015-03-04 17:14:04 -06:00
|
|
|
}
|
|
|
|
|
2015-03-09 03:01:30 -05:00
|
|
|
LOOLSession::~LOOLSession()
|
2015-03-07 05:23:46 -06:00
|
|
|
{
|
2015-04-20 09:43:31 -05:00
|
|
|
std::cout << Util::logPrefix() << "LOOLSession dtor this=" << this << " " << _kind << std::endl;
|
2015-04-22 03:14:11 -05:00
|
|
|
Util::shutdownWebSocket(*_ws);
|
2015-03-07 05:23:46 -06:00
|
|
|
}
|
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
void LOOLSession::sendTextFrame(const std::string& text)
|
|
|
|
{
|
2015-06-05 08:12:06 -05:00
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
_ws->sendFrame(text.data(), text.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void LOOLSession::sendBinaryFrame(const char *buffer, int length)
|
|
|
|
{
|
2015-06-05 08:12:06 -05:00
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
|
|
|
if (length > 1000)
|
|
|
|
{
|
|
|
|
std::string nextmessage = "nextmessage: size=" + std::to_string(length);
|
|
|
|
_ws->sendFrame(nextmessage.data(), nextmessage.size());
|
|
|
|
}
|
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
_ws->sendFrame(buffer, length, WebSocket::FRAME_BINARY);
|
|
|
|
}
|
2015-04-13 07:13:38 -05:00
|
|
|
|
2015-05-07 08:29:36 -05:00
|
|
|
std::map<Process::PID, UInt64> MasterProcessSession::_childProcesses;
|
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
std::set<UInt64> MasterProcessSession::_pendingPreSpawnedChildren;
|
2015-04-22 13:35:52 -05:00
|
|
|
std::set<std::shared_ptr<MasterProcessSession>> MasterProcessSession::_availableChildSessions;
|
2015-05-07 08:29:36 -05:00
|
|
|
std::mutex MasterProcessSession::_availableChildSessionMutex;
|
|
|
|
std::condition_variable MasterProcessSession::_availableChildSessionCV;
|
2015-05-29 00:49:49 -05:00
|
|
|
Random MasterProcessSession::_rng;
|
2015-05-07 12:01:40 -05:00
|
|
|
std::mutex MasterProcessSession::_rngMutex;
|
2015-04-20 09:43:31 -05:00
|
|
|
|
2015-05-28 08:42:38 -05:00
|
|
|
MasterProcessSession::MasterProcessSession(std::shared_ptr<WebSocket> ws, Kind kind) :
|
2015-04-20 09:43:31 -05:00
|
|
|
LOOLSession(ws, kind),
|
2015-05-28 10:39:05 -05:00
|
|
|
_childId(0),
|
|
|
|
_curPart(0)
|
2015-04-20 09:43:31 -05:00
|
|
|
{
|
2015-05-28 08:42:38 -05:00
|
|
|
std::cout << Util::logPrefix() << "MasterProcessSession ctor this=" << this << " ws=" << _ws.get() << std::endl;
|
2015-04-20 09:43:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
MasterProcessSession::~MasterProcessSession()
|
|
|
|
{
|
2015-04-22 13:35:52 -05:00
|
|
|
std::cout << Util::logPrefix() << "MasterProcessSession dtor this=" << this << " _peer=" << _peer.lock().get() << std::endl;
|
2015-04-22 03:14:11 -05:00
|
|
|
Util::shutdownWebSocket(*_ws);
|
2015-04-22 13:35:52 -05:00
|
|
|
auto peer = _peer.lock();
|
|
|
|
if (_kind == Kind::ToClient && peer)
|
2015-04-21 07:06:41 -05:00
|
|
|
{
|
2015-04-22 13:35:52 -05:00
|
|
|
Util::shutdownWebSocket(*(peer->_ws));
|
2015-04-21 07:06:41 -05:00
|
|
|
}
|
2015-04-20 09:43:31 -05:00
|
|
|
}
|
|
|
|
|
2015-06-08 08:35:52 -05:00
|
|
|
bool MasterProcessSession::handleInput(const char *buffer, int length)
|
2015-03-04 17:14:04 -06:00
|
|
|
{
|
2015-05-28 04:58:08 -05:00
|
|
|
Application::instance().logger().information(Util::logPrefix() + "Input: " + getAbbreviatedMessage(buffer, length));
|
2015-03-04 17:14:04 -06:00
|
|
|
|
2015-04-14 09:50:38 -05:00
|
|
|
std::string firstLine = getFirstLine(buffer, length);
|
|
|
|
StringTokenizer tokens(firstLine, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
|
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
if (haveSeparateProcess())
|
2015-03-17 18:56:15 -05:00
|
|
|
{
|
2015-04-14 09:50:38 -05:00
|
|
|
// Note that this handles both forwarding requests from the client to the child process, and
|
2015-04-20 09:43:31 -05:00
|
|
|
// forwarding replies from the child process to the client. Or does it?
|
2015-03-17 18:56:15 -05:00
|
|
|
|
2015-05-28 10:39:05 -05:00
|
|
|
// Snoop at some messages and manipulate tile cache information as needed
|
2015-04-22 13:35:52 -05:00
|
|
|
auto peer = _peer.lock();
|
2015-05-28 10:39:05 -05:00
|
|
|
|
|
|
|
if (_kind == Kind::ToPrisoner)
|
|
|
|
{
|
|
|
|
if (tokens[0] == "curpart:" &&
|
|
|
|
tokens.count() == 2 &&
|
|
|
|
getTokenInteger(tokens[1], "part", _curPart))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-22 13:35:52 -05:00
|
|
|
if (_kind == Kind::ToPrisoner && peer && peer->_tileCache)
|
2015-04-14 09:50:38 -05:00
|
|
|
{
|
|
|
|
if (tokens[0] == "tile:")
|
|
|
|
{
|
2015-04-27 13:24:05 -05:00
|
|
|
int part, width, height, tilePosX, tilePosY, tileWidth, tileHeight;
|
|
|
|
if (tokens.count() != 8 ||
|
|
|
|
!getTokenInteger(tokens[1], "part", part) ||
|
|
|
|
!getTokenInteger(tokens[2], "width", width) ||
|
|
|
|
!getTokenInteger(tokens[3], "height", height) ||
|
|
|
|
!getTokenInteger(tokens[4], "tileposx", tilePosX) ||
|
|
|
|
!getTokenInteger(tokens[5], "tileposy", tilePosY) ||
|
|
|
|
!getTokenInteger(tokens[6], "tilewidth", tileWidth) ||
|
|
|
|
!getTokenInteger(tokens[7], "tileheight", tileHeight))
|
2015-04-14 09:50:38 -05:00
|
|
|
assert(false);
|
|
|
|
|
|
|
|
assert(firstLine.size() < static_cast<std::string::size_type>(length));
|
2015-04-27 13:24:05 -05:00
|
|
|
peer->_tileCache->saveTile(part, width, height, tilePosX, tilePosY, tileWidth, tileHeight, buffer + firstLine.size() + 1, length - firstLine.size() - 1);
|
2015-04-14 09:50:38 -05:00
|
|
|
}
|
|
|
|
else if (tokens[0] == "status:")
|
|
|
|
{
|
|
|
|
assert(firstLine.size() == static_cast<std::string::size_type>(length));
|
2015-04-22 13:35:52 -05:00
|
|
|
peer->_tileCache->saveStatus(firstLine);
|
2015-04-14 09:50:38 -05:00
|
|
|
}
|
2015-05-28 10:39:05 -05:00
|
|
|
else if (tokens[0] == "invalidatetiles:")
|
|
|
|
{
|
|
|
|
assert(firstLine.size() == static_cast<std::string::size_type>(length));
|
|
|
|
peer->_tileCache->invalidateTiles(_curPart, firstLine);
|
|
|
|
}
|
2015-04-14 09:50:38 -05:00
|
|
|
}
|
2015-03-07 05:23:46 -06:00
|
|
|
|
2015-04-14 09:50:38 -05:00
|
|
|
forwardToPeer(buffer, length);
|
|
|
|
return true;
|
|
|
|
}
|
2015-03-04 17:14:04 -06:00
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
if (tokens[0] == "child")
|
2015-03-17 18:56:15 -05:00
|
|
|
{
|
2015-04-20 09:43:31 -05:00
|
|
|
if (_kind != Kind::ToPrisoner)
|
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=child kind=invalid");
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-22 13:35:52 -05:00
|
|
|
if (!_peer.expired())
|
2015-03-17 18:56:15 -05:00
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=child kind=invalid");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (tokens.count() != 2)
|
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=child kind=syntax");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
UInt64 childId = std::stoull(tokens[1]);
|
2015-04-15 07:17:15 -05:00
|
|
|
if (_pendingPreSpawnedChildren.find(childId) == _pendingPreSpawnedChildren.end())
|
2015-03-17 18:56:15 -05:00
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=child kind=notfound");
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-15 07:17:15 -05:00
|
|
|
_pendingPreSpawnedChildren.erase(childId);
|
2015-05-07 08:29:36 -05:00
|
|
|
std::unique_lock<std::mutex> lock(_availableChildSessionMutex);
|
2015-04-22 13:35:52 -05:00
|
|
|
_availableChildSessions.insert(shared_from_this());
|
2015-05-08 13:24:46 -05:00
|
|
|
std::cout << Util::logPrefix() << "Inserted " << this << " id=" << childId << " into _availableChildSessions, size=" << _availableChildSessions.size() << std::endl;
|
2015-04-08 09:22:42 -05:00
|
|
|
_childId = childId;
|
2015-05-08 13:24:46 -05:00
|
|
|
lock.unlock();
|
2015-05-07 08:29:36 -05:00
|
|
|
_availableChildSessionCV.notify_one();
|
2015-03-17 18:56:15 -05:00
|
|
|
}
|
2015-04-20 09:43:31 -05:00
|
|
|
else if (_kind == Kind::ToPrisoner)
|
|
|
|
{
|
|
|
|
// Message from child process to be forwarded to client.
|
|
|
|
|
|
|
|
// I think we should never get here
|
|
|
|
assert(false);
|
|
|
|
}
|
2015-03-17 18:56:15 -05:00
|
|
|
else if (tokens[0] == "load")
|
2015-03-04 17:14:04 -06:00
|
|
|
{
|
2015-03-17 18:56:15 -05:00
|
|
|
if (_docURL != "")
|
2015-03-04 17:14:04 -06:00
|
|
|
{
|
2015-04-13 09:03:55 -05:00
|
|
|
sendTextFrame("error: cmd=load kind=docalreadyloaded");
|
2015-03-07 05:23:46 -06:00
|
|
|
return false;
|
2015-03-04 17:14:04 -06:00
|
|
|
}
|
2015-03-17 18:56:15 -05:00
|
|
|
return loadDocument(buffer, length, tokens);
|
2015-03-04 17:14:04 -06:00
|
|
|
}
|
2015-05-28 10:39:05 -05:00
|
|
|
else if (tokens[0] != "invalidatetiles" &&
|
2015-05-27 09:14:13 -05:00
|
|
|
tokens[0] != "key" &&
|
|
|
|
tokens[0] != "mouse" &&
|
|
|
|
tokens[0] != "resetselection" &&
|
2015-05-28 10:39:05 -05:00
|
|
|
tokens[0] != "saveas" &&
|
|
|
|
tokens[0] != "selectgraphic" &&
|
|
|
|
tokens[0] != "selecttext" &&
|
|
|
|
tokens[0] != "status" &&
|
|
|
|
tokens[0] != "tile" &&
|
|
|
|
tokens[0] != "uno")
|
2015-05-27 09:14:13 -05:00
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=" + tokens[0] + " kind=unknown");
|
|
|
|
return false;
|
|
|
|
}
|
2015-03-17 18:56:15 -05:00
|
|
|
else if (_docURL == "")
|
2015-03-04 17:14:04 -06:00
|
|
|
{
|
2015-03-13 06:59:51 -05:00
|
|
|
sendTextFrame("error: cmd=" + tokens[0] + " kind=nodocloaded");
|
2015-03-07 05:23:46 -06:00
|
|
|
return false;
|
2015-03-04 17:14:04 -06:00
|
|
|
}
|
2015-05-28 10:39:05 -05:00
|
|
|
else if (tokens[0] == "invalidatetiles")
|
|
|
|
{
|
|
|
|
return invalidateTiles(buffer, length, tokens);
|
|
|
|
}
|
2015-03-04 17:14:04 -06:00
|
|
|
else if (tokens[0] == "status")
|
|
|
|
{
|
2015-03-17 18:56:15 -05:00
|
|
|
return getStatus(buffer, length);
|
2015-03-04 17:14:04 -06:00
|
|
|
}
|
|
|
|
else if (tokens[0] == "tile")
|
|
|
|
{
|
2015-03-17 18:56:15 -05:00
|
|
|
sendTile(buffer, length, tokens);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-03-20 08:50:23 -05:00
|
|
|
// All other commands are such that they always require a
|
|
|
|
// LibreOfficeKitDocument session, i.e. need to be handled in
|
|
|
|
// a child process.
|
|
|
|
|
2015-04-22 13:35:52 -05:00
|
|
|
if (_peer.expired())
|
2015-04-20 09:43:31 -05:00
|
|
|
dispatchChild();
|
|
|
|
forwardToPeer(buffer, length);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MasterProcessSession::haveSeparateProcess()
|
|
|
|
{
|
|
|
|
return _childId != 0;
|
|
|
|
}
|
|
|
|
|
2015-05-29 00:49:49 -05:00
|
|
|
Path MasterProcessSession::getJailPath(UInt64 childId)
|
2015-04-20 09:43:31 -05:00
|
|
|
{
|
|
|
|
return Path::forDirectory(LOOLWSD::childRoot + Path::separator() + std::to_string(childId));
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2015-05-07 12:15:05 -05:00
|
|
|
ThreadLocal<std::string> sourceForLinkOrCopy;
|
|
|
|
ThreadLocal<Path> destinationForLinkOrCopy;
|
2015-04-20 09:43:31 -05:00
|
|
|
|
|
|
|
int linkOrCopyFunction(const char *fpath,
|
|
|
|
const struct stat *sb,
|
|
|
|
int typeflag,
|
|
|
|
struct FTW *ftwbuf)
|
|
|
|
{
|
2015-05-07 12:15:05 -05:00
|
|
|
if (strcmp(fpath, sourceForLinkOrCopy->c_str()) == 0)
|
2015-04-20 09:43:31 -05:00
|
|
|
return 0;
|
|
|
|
|
2015-05-07 12:15:05 -05:00
|
|
|
assert(fpath[strlen(sourceForLinkOrCopy->c_str())] == '/');
|
|
|
|
const char *relativeOldPath = fpath + strlen(sourceForLinkOrCopy->c_str()) + 1;
|
2015-05-22 06:40:24 -05:00
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
if (strcmp(relativeOldPath, "PkgInfo") == 0)
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
2015-05-07 12:15:05 -05:00
|
|
|
Path newPath(*destinationForLinkOrCopy, Path(relativeOldPath));
|
2015-04-20 09:43:31 -05:00
|
|
|
|
|
|
|
switch (typeflag)
|
|
|
|
{
|
|
|
|
case FTW_F:
|
2015-04-27 07:51:33 -05:00
|
|
|
File(newPath.parent()).createDirectories();
|
2015-04-20 09:43:31 -05:00
|
|
|
if (link(fpath, newPath.toString().c_str()) == -1)
|
2015-03-20 08:50:23 -05:00
|
|
|
{
|
2015-04-20 09:43:31 -05:00
|
|
|
Application::instance().logger().error(Util::logPrefix() +
|
|
|
|
"link(\"" + fpath + "\",\"" + newPath.toString() + "\") failed: " +
|
|
|
|
strerror(errno));
|
|
|
|
exit(1);
|
2015-03-20 08:50:23 -05:00
|
|
|
}
|
2015-04-20 09:43:31 -05:00
|
|
|
break;
|
2015-04-27 07:51:33 -05:00
|
|
|
case FTW_DP:
|
2015-04-20 09:43:31 -05:00
|
|
|
{
|
2015-04-27 07:51:33 -05:00
|
|
|
struct stat st;
|
|
|
|
if (stat(fpath, &st) == -1)
|
|
|
|
{
|
|
|
|
Application::instance().logger().error(Util::logPrefix() +
|
|
|
|
"stat(\"" + fpath + "\") failed: " +
|
|
|
|
strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
File(newPath).createDirectories();
|
|
|
|
struct utimbuf ut;
|
|
|
|
ut.actime = st.st_atime;
|
|
|
|
ut.modtime = st.st_mtime;
|
|
|
|
if (utime(newPath.toString().c_str(), &ut) == -1)
|
|
|
|
{
|
|
|
|
Application::instance().logger().error(Util::logPrefix() +
|
|
|
|
"utime(\"" + newPath.toString() + "\", &ut) failed: " +
|
|
|
|
strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
2015-04-20 09:43:31 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FTW_DNR:
|
|
|
|
Application::instance().logger().error(Util::logPrefix() +
|
|
|
|
"Cannot read directory '" + fpath + "'");
|
|
|
|
return 1;
|
|
|
|
case FTW_NS:
|
|
|
|
Application::instance().logger().error(Util::logPrefix() +
|
|
|
|
"nftw: stat failed for '" + fpath + "'");
|
|
|
|
return 1;
|
|
|
|
case FTW_SLN:
|
2015-04-27 07:51:33 -05:00
|
|
|
Application::instance().logger().information(Util::logPrefix() +
|
|
|
|
"nftw: symlink to nonexistent file: '" + fpath + "', ignored");
|
|
|
|
break;
|
2015-04-20 09:43:31 -05:00
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void linkOrCopy(const std::string& source, const Path& destination)
|
|
|
|
{
|
2015-05-07 12:15:05 -05:00
|
|
|
*sourceForLinkOrCopy = source;
|
|
|
|
*destinationForLinkOrCopy = destination;
|
2015-04-27 07:51:33 -05:00
|
|
|
if (nftw(source.c_str(), linkOrCopyFunction, 10, FTW_DEPTH) == -1)
|
2015-04-24 11:09:01 -05:00
|
|
|
Application::instance().logger().error(Util::logPrefix() +
|
|
|
|
"linkOrCopy: nftw() failed for '" + source + "'");
|
2015-04-20 09:43:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MasterProcessSession::preSpawn()
|
|
|
|
{
|
|
|
|
// Create child-specific subtree that will become its chroot root
|
|
|
|
|
2015-05-07 12:01:40 -05:00
|
|
|
std::unique_lock<std::mutex> rngLock(_rngMutex);
|
|
|
|
UInt64 childId = (((UInt64)_rng.next()) << 32) | _rng.next() | 1;
|
|
|
|
rngLock.unlock();
|
2015-04-20 09:43:31 -05:00
|
|
|
|
|
|
|
Path jail = getJailPath(childId);
|
|
|
|
File(jail).createDirectory();
|
|
|
|
|
|
|
|
Path jailLOInstallation(jail, LOOLWSD::loSubPath);
|
2015-04-27 07:51:33 -05:00
|
|
|
jailLOInstallation.makeDirectory();
|
2015-04-20 09:43:31 -05:00
|
|
|
File(jailLOInstallation).createDirectory();
|
|
|
|
|
|
|
|
// Copy (link) LO installation and other necessary files into it from the template
|
|
|
|
|
|
|
|
linkOrCopy(LOOLWSD::sysTemplate, jail);
|
|
|
|
linkOrCopy(LOOLWSD::loTemplate, jailLOInstallation);
|
|
|
|
|
|
|
|
_pendingPreSpawnedChildren.insert(childId);
|
|
|
|
|
|
|
|
Process::Args args;
|
2015-05-22 08:34:21 -05:00
|
|
|
|
|
|
|
#if ENABLE_DEBUG
|
|
|
|
if (LOOLWSD::runningAsRoot)
|
|
|
|
args.push_back(Application::instance().commandPath());
|
|
|
|
#endif
|
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
args.push_back("--child=" + std::to_string(childId));
|
|
|
|
args.push_back("--port=" + std::to_string(LOOLWSD::portNumber));
|
|
|
|
args.push_back("--jail=" + jail.toString());
|
|
|
|
args.push_back("--losubpath=" + LOOLWSD::loSubPath);
|
|
|
|
|
2015-05-26 03:08:27 -05:00
|
|
|
std::string executable;
|
|
|
|
|
2015-05-22 11:42:36 -05:00
|
|
|
#if ENABLE_DEBUG
|
|
|
|
if (LOOLWSD::runningAsRoot)
|
2015-05-26 03:08:27 -05:00
|
|
|
{
|
2015-05-22 11:42:36 -05:00
|
|
|
args.push_back("--uid=" + std::to_string(LOOLWSD::uid));
|
2015-05-26 03:08:27 -05:00
|
|
|
executable = "/usr/bin/sudo";
|
|
|
|
}
|
|
|
|
else
|
2015-05-22 11:42:36 -05:00
|
|
|
#endif
|
2015-05-26 03:08:27 -05:00
|
|
|
{
|
|
|
|
executable = Application::instance().commandPath();
|
|
|
|
}
|
2015-04-20 09:43:31 -05:00
|
|
|
|
2015-05-22 08:34:21 -05:00
|
|
|
Application::instance().logger().information(Util::logPrefix() + "Launching child: " + executable + " " + Poco::cat(std::string(" "), args.begin(), args.end()));
|
|
|
|
|
|
|
|
#if ENABLE_DEBUG
|
|
|
|
ProcessHandle child = Process::launch(executable, args);
|
|
|
|
#else
|
2015-04-20 09:43:31 -05:00
|
|
|
ProcessHandle child = Process::launch(Application::instance().commandPath(), args);
|
2015-05-22 08:34:21 -05:00
|
|
|
#endif
|
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
_childProcesses[child.id()] = childId;
|
|
|
|
}
|
|
|
|
|
2015-05-28 10:39:05 -05:00
|
|
|
bool MasterProcessSession::invalidateTiles(const char *buffer, int length, StringTokenizer& tokens)
|
|
|
|
{
|
|
|
|
int part, tilePosX, tilePosY, tileWidth, tileHeight;
|
|
|
|
|
|
|
|
if (tokens.count() != 6 ||
|
|
|
|
!getTokenInteger(tokens[1], "part", part) ||
|
|
|
|
!getTokenInteger(tokens[2], "tileposx", tilePosX) ||
|
|
|
|
!getTokenInteger(tokens[3], "tileposy", tilePosY) ||
|
|
|
|
!getTokenInteger(tokens[4], "tilewidth", tileWidth) ||
|
|
|
|
!getTokenInteger(tokens[5], "tileheight", tileHeight))
|
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=invalidatetiles kind=syntax");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_tileCache->invalidateTiles(_curPart, tilePosX, tilePosY, tileWidth, tileHeight);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
bool MasterProcessSession::loadDocument(const char *buffer, int length, StringTokenizer& tokens)
|
|
|
|
{
|
|
|
|
if (tokens.count() != 2)
|
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=load kind=syntax");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tokens[1].find("url=") == 0)
|
|
|
|
_docURL = tokens[1].substr(strlen("url="));
|
|
|
|
else
|
|
|
|
_docURL = tokens[1];
|
|
|
|
|
|
|
|
_tileCache.reset(new TileCache(_docURL));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MasterProcessSession::getStatus(const char *buffer, int length)
|
|
|
|
{
|
|
|
|
std::string status;
|
|
|
|
|
|
|
|
status = _tileCache->getStatus();
|
|
|
|
if (status.size() > 0)
|
|
|
|
{
|
|
|
|
sendTextFrame(status);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-22 13:35:52 -05:00
|
|
|
if (_peer.expired())
|
2015-04-20 09:43:31 -05:00
|
|
|
dispatchChild();
|
|
|
|
forwardToPeer(buffer, length);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MasterProcessSession::sendTile(const char *buffer, int length, StringTokenizer& tokens)
|
|
|
|
{
|
2015-04-27 13:24:05 -05:00
|
|
|
int part, width, height, tilePosX, tilePosY, tileWidth, tileHeight;
|
|
|
|
|
|
|
|
if (tokens.count() != 8 ||
|
|
|
|
!getTokenInteger(tokens[1], "part", part) ||
|
|
|
|
!getTokenInteger(tokens[2], "width", width) ||
|
|
|
|
!getTokenInteger(tokens[3], "height", height) ||
|
|
|
|
!getTokenInteger(tokens[4], "tileposx", tilePosX) ||
|
|
|
|
!getTokenInteger(tokens[5], "tileposy", tilePosY) ||
|
|
|
|
!getTokenInteger(tokens[6], "tilewidth", tileWidth) ||
|
|
|
|
!getTokenInteger(tokens[7], "tileheight", tileHeight))
|
2015-04-20 09:43:31 -05:00
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=tile kind=syntax");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-27 13:24:05 -05:00
|
|
|
if (part < 0 ||
|
|
|
|
width <= 0 ||
|
2015-04-20 09:43:31 -05:00
|
|
|
height <= 0 ||
|
|
|
|
tilePosX < 0 ||
|
|
|
|
tilePosY < 0 ||
|
|
|
|
tileWidth <= 0 ||
|
|
|
|
tileHeight <= 0)
|
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=tile kind=invalid");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string response = "tile: " + Poco::cat(std::string(" "), tokens.begin() + 1, tokens.end()) + "\n";
|
|
|
|
|
|
|
|
std::vector<char> output;
|
|
|
|
output.reserve(4 * width * height);
|
|
|
|
output.resize(response.size());
|
|
|
|
std::memcpy(output.data(), response.data(), response.size());
|
|
|
|
|
2015-04-27 13:24:05 -05:00
|
|
|
std::unique_ptr<std::fstream> cachedTile = _tileCache->lookupTile(part, width, height, tilePosX, tilePosY, tileWidth, tileHeight);
|
2015-04-20 09:43:31 -05:00
|
|
|
if (cachedTile && cachedTile->is_open())
|
|
|
|
{
|
|
|
|
cachedTile->seekg(0, std::ios_base::end);
|
|
|
|
size_t pos = output.size();
|
|
|
|
std::streamsize size = cachedTile->tellg();
|
|
|
|
output.resize(pos + size);
|
|
|
|
cachedTile->seekg(0, std::ios_base::beg);
|
|
|
|
cachedTile->read(output.data() + pos, size);
|
|
|
|
cachedTile->close();
|
|
|
|
|
|
|
|
sendBinaryFrame(output.data(), output.size());
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-22 13:35:52 -05:00
|
|
|
if (_peer.expired())
|
2015-04-20 09:43:31 -05:00
|
|
|
dispatchChild();
|
|
|
|
forwardToPeer(buffer, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MasterProcessSession::dispatchChild()
|
|
|
|
{
|
|
|
|
// Copy document into jail using the fixed name
|
|
|
|
|
2015-05-07 08:29:36 -05:00
|
|
|
std::shared_ptr<MasterProcessSession> childSession;
|
|
|
|
std::unique_lock<std::mutex> lock(_availableChildSessionMutex);
|
|
|
|
|
2015-05-08 13:24:46 -05:00
|
|
|
std::cout << Util::logPrefix() << "_availableChildSessions size=" << _availableChildSessions.size() << " _pendingChildSessions size=" << _pendingPreSpawnedChildren.size() << std::endl;
|
|
|
|
|
2015-05-07 08:29:36 -05:00
|
|
|
if (_availableChildSessions.size() == 0)
|
|
|
|
{
|
2015-05-08 04:45:04 -05:00
|
|
|
if (_pendingPreSpawnedChildren.size() == 0)
|
|
|
|
{
|
|
|
|
// Running out of pre-spawned children, so spawn one more
|
|
|
|
Application::instance().logger().information(Util::logPrefix() + "Running out of pre-spawned childred, adding one more");
|
|
|
|
lock.unlock();
|
|
|
|
preSpawn();
|
|
|
|
lock.lock();
|
|
|
|
}
|
|
|
|
|
2015-05-07 08:29:36 -05:00
|
|
|
std::cout << Util::logPrefix() << "waiting for a child session to become available" << std::endl;
|
|
|
|
_availableChildSessionCV.wait(lock, [] { return _availableChildSessions.size() > 0; });
|
|
|
|
std::cout << Util::logPrefix() << "waiting done" << std::endl;
|
|
|
|
}
|
2015-04-20 09:43:31 -05:00
|
|
|
|
2015-05-07 08:29:36 -05:00
|
|
|
childSession = *(_availableChildSessions.begin());
|
2015-04-20 09:43:31 -05:00
|
|
|
|
|
|
|
_availableChildSessions.erase(childSession);
|
2015-05-08 13:24:46 -05:00
|
|
|
std::cout << Util::logPrefix() << "_availableChildSessions size=" << _availableChildSessions.size() << std::endl;
|
2015-05-07 08:29:36 -05:00
|
|
|
lock.unlock();
|
2015-04-20 09:43:31 -05:00
|
|
|
|
|
|
|
assert(jailDocumentURL[0] == '/');
|
|
|
|
Path copy(getJailPath(childSession->_childId), jailDocumentURL.substr(1));
|
|
|
|
Application::instance().logger().information(Util::logPrefix() + "Copying " + _docURL + " to " + copy.toString());
|
|
|
|
|
2015-04-30 07:58:13 -05:00
|
|
|
URIStreamOpener opener;
|
|
|
|
opener.registerStreamFactory("http", new HTTPStreamFactory());
|
2015-05-12 04:28:46 -05:00
|
|
|
try
|
2015-04-30 07:58:13 -05:00
|
|
|
{
|
2015-05-12 04:28:46 -05:00
|
|
|
std::istream *input = opener.open(_docURL);
|
|
|
|
std::ofstream output(copy.toString());
|
|
|
|
if (!output)
|
|
|
|
{
|
|
|
|
Application::instance().logger().error(Util::logPrefix() + "Could not open " + copy.toString() + " for writing");
|
|
|
|
sendTextFrame("error: cmd=load kind=internal");
|
|
|
|
|
|
|
|
// We did not use the child session after all
|
|
|
|
lock.lock();
|
|
|
|
_availableChildSessions.insert(childSession);
|
|
|
|
std::cout << Util::logPrefix() << "_availableChildSessions size=" << _availableChildSessions.size() << std::endl;
|
|
|
|
lock.unlock();
|
2015-05-29 00:49:49 -05:00
|
|
|
throw IOException(copy.toString());
|
2015-05-12 04:28:46 -05:00
|
|
|
}
|
|
|
|
StreamCopier::copyStream(*input, output);
|
|
|
|
output.close();
|
|
|
|
|
|
|
|
Application::instance().logger().information(Util::logPrefix() + "Copying done");
|
2015-04-30 07:58:13 -05:00
|
|
|
}
|
2015-05-29 00:49:49 -05:00
|
|
|
catch (IOException& exc)
|
2015-05-12 04:28:46 -05:00
|
|
|
{
|
|
|
|
Application::instance().logger().error(Util::logPrefix() + "Copying failed: " + exc.message());
|
|
|
|
sendTextFrame("error: cmd=load kind=failed");
|
2015-04-20 09:43:31 -05:00
|
|
|
|
2015-05-12 04:28:46 -05:00
|
|
|
lock.lock();
|
|
|
|
_availableChildSessions.insert(childSession);
|
|
|
|
std::cout << Util::logPrefix() << "_availableChildSessions size=" << _availableChildSessions.size() << std::endl;
|
|
|
|
lock.unlock();
|
|
|
|
|
|
|
|
throw;
|
|
|
|
}
|
2015-04-30 13:05:16 -05:00
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
_peer = childSession;
|
2015-04-22 13:35:52 -05:00
|
|
|
childSession->_peer = shared_from_this();
|
2015-04-20 09:43:31 -05:00
|
|
|
|
|
|
|
std::string loadRequest = "load url=" + _docURL;
|
|
|
|
forwardToPeer(loadRequest.c_str(), loadRequest.size());
|
|
|
|
|
|
|
|
// As we took one child process into use, spawn a new one
|
|
|
|
preSpawn();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MasterProcessSession::forwardToPeer(const char *buffer, int length)
|
|
|
|
{
|
|
|
|
Application::instance().logger().information(Util::logPrefix() + "forwardToPeer(" + getAbbreviatedMessage(buffer, length) + ")");
|
2015-04-22 13:35:52 -05:00
|
|
|
auto peer = _peer.lock();
|
2015-04-30 10:44:27 -05:00
|
|
|
if (!peer)
|
|
|
|
return;
|
2015-06-05 08:12:06 -05:00
|
|
|
peer->sendBinaryFrame(buffer, length);
|
2015-04-20 09:43:31 -05:00
|
|
|
}
|
2015-03-20 08:50:23 -05:00
|
|
|
|
2015-05-28 08:42:38 -05:00
|
|
|
ChildProcessSession::ChildProcessSession(std::shared_ptr<WebSocket> ws, LibreOfficeKit *loKit) :
|
2015-04-20 09:43:31 -05:00
|
|
|
LOOLSession(ws, Kind::ToMaster),
|
2015-05-28 10:39:05 -05:00
|
|
|
_loKitDocument(NULL),
|
|
|
|
_loKit(loKit)
|
2015-04-20 09:43:31 -05:00
|
|
|
{
|
2015-05-28 08:42:38 -05:00
|
|
|
std::cout << Util::logPrefix() << "ChildProcessSession ctor this=" << this << " ws=" << _ws.get() << std::endl;
|
2015-04-20 09:43:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ChildProcessSession::~ChildProcessSession()
|
|
|
|
{
|
|
|
|
std::cout << Util::logPrefix() << "ChildProcessSession dtor this=" << this << std::endl;
|
2015-05-06 09:08:44 -05:00
|
|
|
if (LIBREOFFICEKIT_HAS(_loKit, registerCallback))
|
|
|
|
_loKit->pClass->registerCallback(_loKit, 0, 0);
|
2015-04-22 03:14:11 -05:00
|
|
|
Util::shutdownWebSocket(*_ws);
|
2015-04-20 09:43:31 -05:00
|
|
|
}
|
|
|
|
|
2015-06-08 08:35:52 -05:00
|
|
|
bool ChildProcessSession::handleInput(const char *buffer, int length)
|
2015-04-20 09:43:31 -05:00
|
|
|
{
|
|
|
|
Application& app = Application::instance();
|
|
|
|
|
|
|
|
std::string firstLine = getFirstLine(buffer, length);
|
|
|
|
StringTokenizer tokens(firstLine, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
|
|
|
|
|
|
|
|
app.logger().information(Util::logPrefix() + "Input: " + getAbbreviatedMessage(buffer, length));
|
|
|
|
|
|
|
|
if (tokens[0] == "load")
|
|
|
|
{
|
|
|
|
if (_docURL != "")
|
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=load kind=docalreadyloaded");
|
|
|
|
return false;
|
2015-03-20 08:50:23 -05:00
|
|
|
}
|
2015-04-20 09:43:31 -05:00
|
|
|
return loadDocument(buffer, length, tokens);
|
|
|
|
}
|
|
|
|
else if (_docURL == "")
|
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=" + tokens[0] + " kind=nodocloaded");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "status")
|
|
|
|
{
|
|
|
|
return getStatus(buffer, length);
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "tile")
|
|
|
|
{
|
|
|
|
sendTile(buffer, length, tokens);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// All other commands are such that they always require a LibreOfficeKitDocument session,
|
|
|
|
// i.e. need to be handled in a child process.
|
|
|
|
|
|
|
|
assert(tokens[0] == "key" ||
|
|
|
|
tokens[0] == "mouse" ||
|
|
|
|
tokens[0] == "uno" ||
|
|
|
|
tokens[0] == "selecttext" ||
|
|
|
|
tokens[0] == "selectgraphic" ||
|
|
|
|
tokens[0] == "resetselection" ||
|
|
|
|
tokens[0] == "saveas");
|
2015-03-20 08:50:23 -05:00
|
|
|
|
|
|
|
if (tokens[0] == "key")
|
|
|
|
{
|
|
|
|
return keyEvent(buffer, length, tokens);
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "mouse")
|
|
|
|
{
|
|
|
|
return mouseEvent(buffer, length, tokens);
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "uno")
|
|
|
|
{
|
|
|
|
return unoCommand(buffer, length, tokens);
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "selecttext")
|
|
|
|
{
|
|
|
|
return selectText(buffer, length, tokens);
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "selectgraphic")
|
|
|
|
{
|
|
|
|
return selectGraphic(buffer, length, tokens);
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "resetselection")
|
|
|
|
{
|
2015-03-23 15:13:57 -05:00
|
|
|
return resetSelection(buffer, length, tokens);
|
|
|
|
}
|
|
|
|
else if (tokens[0] == "saveas")
|
|
|
|
{
|
|
|
|
return saveAs(buffer, length, tokens);
|
2015-03-20 08:50:23 -05:00
|
|
|
}
|
|
|
|
else
|
2015-04-20 09:43:31 -05:00
|
|
|
{
|
2015-03-20 08:50:23 -05:00
|
|
|
assert(false);
|
2015-04-20 09:43:31 -05:00
|
|
|
}
|
2015-03-04 17:14:04 -06:00
|
|
|
}
|
2015-03-07 05:23:46 -06:00
|
|
|
return true;
|
2015-03-04 17:14:04 -06:00
|
|
|
}
|
|
|
|
|
2015-03-07 05:23:46 -06:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
static void myCallback(int nType, const char* pPayload, void* pData)
|
|
|
|
{
|
2015-05-28 10:39:05 -05:00
|
|
|
ChildProcessSession *srv = reinterpret_cast<ChildProcessSession *>(pData);
|
2015-03-07 05:23:46 -06:00
|
|
|
|
|
|
|
switch ((LibreOfficeKitCallbackType) nType)
|
|
|
|
{
|
|
|
|
case LOK_CALLBACK_INVALIDATE_TILES:
|
2015-05-29 01:44:39 -05:00
|
|
|
{
|
|
|
|
int curPart = srv->_loKitDocument->pClass->getPart(srv->_loKitDocument);
|
|
|
|
srv->sendTextFrame("curpart: part=" + std::to_string(curPart));
|
|
|
|
srv->sendTextFrame("invalidatetiles: " + std::string(pPayload));
|
|
|
|
StringTokenizer tokens(std::string(pPayload), " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
|
|
|
|
if (tokens.count() == 4)
|
|
|
|
{
|
|
|
|
int width(std::stoi(tokens[0]));
|
|
|
|
int height(std::stoi(tokens[1]));
|
|
|
|
int x(std::stoi(tokens[2]));
|
|
|
|
int y(std::stoi(tokens[3]));
|
|
|
|
srv->sendTextFrame("invalidate:"
|
|
|
|
" part=" + std::to_string(curPart) +
|
|
|
|
" x=" + std::to_string(x) +
|
|
|
|
" y=" + std::to_string(y) +
|
|
|
|
" width=" + std::to_string(width) +
|
|
|
|
" height=" + std::to_string(height));
|
|
|
|
}
|
|
|
|
}
|
2015-03-07 05:23:46 -06:00
|
|
|
break;
|
|
|
|
case LOK_CALLBACK_INVALIDATE_VISIBLE_CURSOR:
|
2015-05-24 23:40:08 -05:00
|
|
|
srv->sendTextFrame("invalidatecursor: " + std::string(pPayload));
|
2015-03-13 04:25:00 -05:00
|
|
|
break;
|
2015-03-07 05:23:46 -06:00
|
|
|
case LOK_CALLBACK_TEXT_SELECTION:
|
|
|
|
srv->sendTextFrame("textselection: " + std::string(pPayload));
|
|
|
|
break;
|
|
|
|
case LOK_CALLBACK_TEXT_SELECTION_START:
|
|
|
|
srv->sendTextFrame("textselectionstart: " + std::string(pPayload));
|
|
|
|
break;
|
|
|
|
case LOK_CALLBACK_TEXT_SELECTION_END:
|
|
|
|
srv->sendTextFrame("textselectionend: " + std::string(pPayload));
|
|
|
|
break;
|
2015-03-13 04:30:23 -05:00
|
|
|
case LOK_CALLBACK_CURSOR_VISIBLE:
|
|
|
|
srv->sendTextFrame("cursorvisible: " + std::string(pPayload));
|
|
|
|
break;
|
|
|
|
case LOK_CALLBACK_GRAPHIC_SELECTION:
|
|
|
|
srv->sendTextFrame("graphicselection: " + std::string(pPayload));
|
|
|
|
break;
|
|
|
|
case LOK_CALLBACK_HYPERLINK_CLICKED:
|
|
|
|
srv->sendTextFrame("hyperlinkclicked: " + std::string(pPayload));
|
|
|
|
break;
|
2015-04-14 06:51:36 -05:00
|
|
|
case LOK_CALLBACK_STATE_CHANGED:
|
|
|
|
srv->sendTextFrame("statechanged: " + std::string(pPayload));
|
|
|
|
break;
|
2015-05-06 09:08:44 -05:00
|
|
|
case LOK_CALLBACK_STATUS_INDICATOR_START:
|
|
|
|
srv->sendTextFrame("statusindicatorstart:");
|
|
|
|
break;
|
|
|
|
case LOK_CALLBACK_STATUS_INDICATOR_SET_VALUE:
|
|
|
|
srv->sendTextFrame("statusindicatorsetvalue: " + std::string(pPayload));
|
|
|
|
break;
|
|
|
|
case LOK_CALLBACK_STATUS_INDICATOR_FINISH:
|
|
|
|
srv->sendTextFrame("statusindicatorfinish:");
|
|
|
|
break;
|
2015-05-19 03:56:04 -05:00
|
|
|
case LOK_CALLBACK_SEARCH_NOT_FOUND:
|
|
|
|
srv->sendTextFrame("searchnotfound: " + std::string(pPayload));
|
|
|
|
break;
|
2015-05-27 10:34:10 -05:00
|
|
|
case LOK_CALLBACK_DOCUMENT_SIZE_CHANGED:
|
2015-05-28 06:51:08 -05:00
|
|
|
srv->getStatus("", 0);
|
2015-05-23 05:26:11 -05:00
|
|
|
break;
|
2015-05-27 04:36:05 -05:00
|
|
|
case LOK_CALLBACK_SET_PART:
|
|
|
|
srv->sendTextFrame("setpart: " + std::string(pPayload));
|
|
|
|
break;
|
2015-03-07 05:23:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
bool ChildProcessSession::loadDocument(const char *buffer, int length, StringTokenizer& tokens)
|
2015-03-04 17:14:04 -06:00
|
|
|
{
|
|
|
|
if (tokens.count() != 2)
|
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=load kind=syntax");
|
2015-03-17 18:56:15 -05:00
|
|
|
return false;
|
2015-03-04 17:14:04 -06:00
|
|
|
}
|
2015-03-12 18:34:42 -05:00
|
|
|
|
2015-03-19 07:38:04 -05:00
|
|
|
if (tokens[1].find("url=") == 0)
|
|
|
|
_docURL = tokens[1].substr(strlen("url="));
|
|
|
|
else
|
|
|
|
_docURL = tokens[1];
|
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
// The URL in the request is the original one, not visible in the chroot jail.
|
|
|
|
// The child process uses the fixed name jailDocumentURL.
|
2015-04-10 04:20:42 -05:00
|
|
|
|
2015-05-06 09:08:44 -05:00
|
|
|
if (LIBREOFFICEKIT_HAS(_loKit, registerCallback))
|
|
|
|
_loKit->pClass->registerCallback(_loKit, myCallback, this);
|
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
if ((_loKitDocument = _loKit->pClass->documentLoad(_loKit, jailDocumentURL.c_str())) == NULL)
|
2015-04-14 09:50:38 -05:00
|
|
|
{
|
2015-04-20 09:43:31 -05:00
|
|
|
sendTextFrame("error: cmd=load kind=failed");
|
|
|
|
return false;
|
2015-04-14 09:50:38 -05:00
|
|
|
}
|
2015-03-17 18:56:15 -05:00
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
_loKitDocument->pClass->initializeForRendering(_loKitDocument);
|
|
|
|
|
|
|
|
if (!getStatus(buffer, length))
|
|
|
|
return false;
|
|
|
|
_loKitDocument->pClass->registerCallback(_loKitDocument, myCallback, this);
|
|
|
|
|
2015-03-17 18:56:15 -05:00
|
|
|
return true;
|
2015-03-04 17:14:04 -06:00
|
|
|
}
|
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
bool ChildProcessSession::getStatus(const char *buffer, int length)
|
2015-03-04 17:14:04 -06:00
|
|
|
{
|
2015-04-20 09:43:31 -05:00
|
|
|
std::string status = "status: " + LOKitHelper::documentStatus(_loKitDocument);
|
2015-03-17 18:56:15 -05:00
|
|
|
|
|
|
|
sendTextFrame(status);
|
|
|
|
|
|
|
|
return true;
|
2015-03-04 17:14:04 -06:00
|
|
|
}
|
|
|
|
|
2015-04-20 09:43:31 -05:00
|
|
|
void ChildProcessSession::sendTile(const char *buffer, int length, StringTokenizer& tokens)
|
2015-03-04 17:14:04 -06:00
|
|
|
{
|
2015-04-27 13:24:05 -05:00
|
|
|
int part, width, height, tilePosX, tilePosY, tileWidth, tileHeight;
|
|
|
|
|
|
|
|
if (tokens.count() != 8 ||
|
|
|
|
!getTokenInteger(tokens[1], "part", part) ||
|
|
|
|
!getTokenInteger(tokens[2], "width", width) ||
|
|
|
|
!getTokenInteger(tokens[3], "height", height) ||
|
|
|
|
!getTokenInteger(tokens[4], "tileposx", tilePosX) ||
|
|
|
|
!getTokenInteger(tokens[5], "tileposy", tilePosY) ||
|
|
|
|
!getTokenInteger(tokens[6], "tilewidth", tileWidth) ||
|
|
|
|
!getTokenInteger(tokens[7], "tileheight", tileHeight))
|
2015-03-04 17:14:04 -06:00
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=tile kind=syntax");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-27 13:24:05 -05:00
|
|
|
if (part < 0 ||
|
|
|
|
width <= 0 ||
|
2015-03-24 11:59:04 -05:00
|
|
|
height <= 0 ||
|
|
|
|
tilePosX < 0 ||
|
|
|
|
tilePosY < 0 ||
|
|
|
|
tileWidth <= 0 ||
|
|
|
|
tileHeight <= 0)
|
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=tile kind=invalid");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-12 09:18:35 -05:00
|
|
|
std::string response = "tile: " + Poco::cat(std::string(" "), tokens.begin() + 1, tokens.end()) + "\n";
|
|
|
|
|
|
|
|
std::vector<char> output;
|
|
|
|
output.reserve(4 * width * height);
|
|
|
|
output.resize(response.size());
|
2015-04-09 17:25:48 -05:00
|
|
|
std::memcpy(output.data(), response.data(), response.size());
|
2015-03-12 09:18:35 -05:00
|
|
|
|
2015-03-17 18:56:15 -05:00
|
|
|
unsigned char *pixmap = new unsigned char[4 * width * height];
|
2015-04-27 13:24:05 -05:00
|
|
|
_loKitDocument->pClass->setPart(_loKitDocument, part);
|
2015-03-20 06:16:41 -05:00
|
|
|
_loKitDocument->pClass->paintTile(_loKitDocument, pixmap, width, height, tilePosX, tilePosY, tileWidth, tileHeight);
|
2015-03-04 17:14:04 -06:00
|
|
|
|
2015-03-28 06:53:44 -05:00
|
|
|
if (!Util::encodePNGAndAppendToBuffer(pixmap, width, height, output))
|
2015-03-04 17:14:04 -06:00
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=tile kind=failure");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-17 18:56:15 -05:00
|
|
|
delete[] pixmap;
|
2015-03-04 17:14:04 -06:00
|
|
|
|
|
|
|
sendBinaryFrame(output.data(), output.size());
|
|
|
|
}
|
|
|
|
|
2015-05-29 00:49:49 -05:00
|
|
|
bool ChildProcessSession::keyEvent(const char *buffer, int length, StringTokenizer& tokens)
|
2015-03-19 07:38:04 -05:00
|
|
|
{
|
|
|
|
int type, charcode, keycode;
|
|
|
|
|
|
|
|
if (tokens.count() != 4 ||
|
2015-04-27 13:09:27 -05:00
|
|
|
!getTokenKeyword(tokens[1], "type",
|
|
|
|
{{"input", LOK_KEYEVENT_KEYINPUT}, {"up", LOK_KEYEVENT_KEYUP}},
|
|
|
|
type) ||
|
2015-03-26 09:49:07 -05:00
|
|
|
!getTokenInteger(tokens[2], "char", charcode) ||
|
|
|
|
!getTokenInteger(tokens[3], "key", keycode))
|
2015-03-19 07:38:04 -05:00
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=key kind=syntax");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_loKitDocument->pClass->postKeyEvent(_loKitDocument, type, charcode, keycode);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-29 00:49:49 -05:00
|
|
|
bool ChildProcessSession::mouseEvent(const char *buffer, int length, StringTokenizer& tokens)
|
2015-03-19 07:38:04 -05:00
|
|
|
{
|
|
|
|
int type, x, y, count;
|
|
|
|
|
|
|
|
if (tokens.count() != 5 ||
|
2015-04-27 13:09:27 -05:00
|
|
|
!getTokenKeyword(tokens[1], "type",
|
|
|
|
{{"buttondown", LOK_MOUSEEVENT_MOUSEBUTTONDOWN},
|
|
|
|
{"buttonup", LOK_MOUSEEVENT_MOUSEBUTTONUP},
|
|
|
|
{"move", LOK_MOUSEEVENT_MOUSEMOVE}},
|
|
|
|
type) ||
|
2015-03-26 09:49:07 -05:00
|
|
|
!getTokenInteger(tokens[2], "x", x) ||
|
|
|
|
!getTokenInteger(tokens[3], "y", y) ||
|
|
|
|
!getTokenInteger(tokens[4], "count", count))
|
2015-03-19 07:38:04 -05:00
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=mouse kind=syntax");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_loKitDocument->pClass->postMouseEvent(_loKitDocument, type, x, y, count);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-29 00:49:49 -05:00
|
|
|
bool ChildProcessSession::unoCommand(const char *buffer, int length, StringTokenizer& tokens)
|
2015-03-19 07:38:04 -05:00
|
|
|
{
|
|
|
|
if (tokens.count() == 1)
|
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=uno kind=syntax");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-22 13:26:50 -05:00
|
|
|
_loKitDocument->pClass->postUnoCommand(_loKitDocument, tokens[1].c_str(), Poco::cat(std::string(" "), tokens.begin() + 2, tokens.end()).c_str());
|
2015-03-19 07:38:04 -05:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-29 00:49:49 -05:00
|
|
|
bool ChildProcessSession::selectText(const char *buffer, int length, StringTokenizer& tokens)
|
2015-03-19 07:38:04 -05:00
|
|
|
{
|
|
|
|
int type, x, y;
|
|
|
|
|
2015-03-26 09:49:07 -05:00
|
|
|
if (tokens.count() != 4 ||
|
2015-04-27 13:09:27 -05:00
|
|
|
!getTokenKeyword(tokens[1], "type",
|
|
|
|
{{"start", LOK_SETTEXTSELECTION_START},
|
|
|
|
{"end", LOK_SETTEXTSELECTION_END},
|
|
|
|
{"reset", LOK_SETTEXTSELECTION_RESET}},
|
|
|
|
type) ||
|
2015-03-26 09:49:07 -05:00
|
|
|
!getTokenInteger(tokens[2], "x", x) ||
|
|
|
|
!getTokenInteger(tokens[3], "y", y))
|
2015-03-19 07:38:04 -05:00
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=selecttext kind=syntax");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_loKitDocument->pClass->setTextSelection(_loKitDocument, type, x, y);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-29 00:49:49 -05:00
|
|
|
bool ChildProcessSession::selectGraphic(const char *buffer, int length, StringTokenizer& tokens)
|
2015-03-19 07:38:04 -05:00
|
|
|
{
|
|
|
|
int type, x, y;
|
|
|
|
|
|
|
|
if (tokens.count() != 4 ||
|
2015-04-27 13:09:27 -05:00
|
|
|
!getTokenKeyword(tokens[1], "type",
|
|
|
|
{{"start", LOK_SETGRAPHICSELECTION_START},
|
|
|
|
{"end", LOK_SETGRAPHICSELECTION_END}},
|
|
|
|
type) ||
|
2015-03-26 09:49:07 -05:00
|
|
|
!getTokenInteger(tokens[2], "x", x) ||
|
|
|
|
!getTokenInteger(tokens[3], "y", y))
|
2015-03-19 07:38:04 -05:00
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=selectghraphic kind=syntax");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_loKitDocument->pClass->setGraphicSelection(_loKitDocument, type, x, y);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-29 00:49:49 -05:00
|
|
|
bool ChildProcessSession::resetSelection(const char *buffer, int length, StringTokenizer& tokens)
|
2015-03-19 07:38:04 -05:00
|
|
|
{
|
2015-03-20 07:56:37 -05:00
|
|
|
if (tokens.count() != 1)
|
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=resetselection kind=syntax");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-19 07:38:04 -05:00
|
|
|
_loKitDocument->pClass->resetSelection(_loKitDocument);
|
2015-03-20 07:56:37 -05:00
|
|
|
|
|
|
|
return true;
|
2015-03-19 07:38:04 -05:00
|
|
|
}
|
|
|
|
|
2015-05-29 00:49:49 -05:00
|
|
|
bool ChildProcessSession::saveAs(const char *buffer, int length, StringTokenizer& tokens)
|
2015-03-23 15:13:57 -05:00
|
|
|
{
|
|
|
|
std::string url, format, filterOptions;
|
|
|
|
|
|
|
|
if (tokens.count() < 4 ||
|
|
|
|
!getTokenString(tokens[1], "url", url) ||
|
|
|
|
!getTokenString(tokens[2], "format", format) ||
|
|
|
|
!getTokenString(tokens[3], "options", filterOptions))
|
|
|
|
{
|
|
|
|
sendTextFrame("error: cmd=saveas kind=syntax");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
URI::decode(url, url, true);
|
|
|
|
URI::decode(format, format, true);
|
|
|
|
|
|
|
|
if (tokens.count() > 4)
|
|
|
|
filterOptions += Poco::cat(std::string(" "), tokens.begin() + 4, tokens.end());
|
|
|
|
|
|
|
|
_loKitDocument->pClass->saveAs(_loKitDocument, url.c_str(), format.c_str(), filterOptions.c_str());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-04 17:14:04 -06:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|