summaryrefslogtreecommitdiff
path: root/kit/ChildSession.hpp
blob: de01fce8ea2f389248d5f1d3bf53557a781d3a99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* -*- 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/.
 */

#ifndef INCLUDED_CHILDSESSION_HPP
#define INCLUDED_CHILDSESSION_HPP

#include <mutex>
#include <unordered_map>
#include <queue>

#define LOK_USE_UNSTABLE_API
#include <LibreOfficeKit/LibreOfficeKit.hxx>

#include <Poco/Net/WebSocket.h>

#include "Common.hpp"
#include "Kit.hpp"
#include "Session.hpp"

class Watermark;
class ChildSession;

enum class LokEventTargetEnum
{
    Document,
    Window
};

// An abstract interface.
class DocumentManagerInterface
{
public:
    virtual ~DocumentManagerInterface()  {}

    /// Reqest loading a document, or a new view, if one exists.
    virtual bool onLoad(const std::string& sessionId,
                        const std::string& uriAnonym,
                        const std::string& renderOpts,
                        const std::string& docTemplate) = 0;

    /// Unload a client session, which unloads the document
    /// if it is the last and only.
    virtual void onUnload(const ChildSession& session) = 0;

    /// Access to the Kit instance.
    virtual std::shared_ptr<lok::Office> getLOKit() = 0;

    /// Access to the document instance.
    virtual std::shared_ptr<lok::Document> getLOKitDocument() = 0;

    /// Send msg to all active sessions.
    virtual bool notifyAll(const std::string& msg) = 0;

    /// Send updated view info to all active sessions.
    virtual void notifyViewInfo() = 0;
    virtual void updateEditorSpeeds(int id, int speed) = 0;

    virtual int getEditorId() = 0;

    /// Get a view ID <-> UserInfo map.
    virtual std::map<int, UserInfo> getViewInfo() = 0;
    virtual std::mutex& getMutex() = 0;

    virtual std::string getObfuscatedFileId() = 0;

    virtual std::shared_ptr<TileQueue>& getTileQueue() = 0;

    virtual bool sendFrame(const char* buffer, int length, WSOpCode opCode = WSOpCode::Text) = 0;
};

struct RecordedEvent
{
private:
    int _type = 0;
    std::string _payload;

public:
    RecordedEvent()
    {
    }

    RecordedEvent(int type, const std::string& payload)
        : _type(type),
        _payload(payload)
    {
    }

    void setType(int type)
    {
        _type = type;
    }

    int getType() const
    {
        return _type;
    }

    void setPayload(const std::string& payload)
    {
        _payload = payload;
    }

    const std::string& getPayload() const
    {
        return _payload;
    }
};

/// When the session is inactive, we need to record its state for a replay.
class StateRecorder
{
private:
    bool _invalidate;
    std::unordered_map<std::string, std::string> _recordedStates;
    std::unordered_map<int, std::unordered_map<int, RecordedEvent>> _recordedViewEvents;
    std::unordered_map<int, RecordedEvent> _recordedEvents;
    std::vector<RecordedEvent> _recordedEventsVector;

public:
    StateRecorder() : _invalidate(false) {}

    // TODO Remember the maximal area we need to invalidate - grow it step by step.
    void recordInvalidate()
    {
        _invalidate = true;
    }

    bool isInvalidate() const
    {
        return _invalidate;
    }

    const std::unordered_map<std::string, std::string>& getRecordedStates() const
    {
        return _recordedStates;
    }

    const std::unordered_map<int, std::unordered_map<int, RecordedEvent>>& getRecordedViewEvents() const
    {
        return _recordedViewEvents;
    }

    const std::unordered_map<int, RecordedEvent>& getRecordedEvents() const
    {
        return _recordedEvents;
    }

    const std::vector<RecordedEvent>& getRecordedEventsVector() const
    {
        return _recordedEventsVector;
    }

    void recordEvent(const int type, const std::string& payload)
    {
        _recordedEvents[type] = RecordedEvent(type, payload);
    }

    void recordViewEvent(const int viewId, const int type, const std::string& payload)
    {
        _recordedViewEvents[viewId][type] = {type, payload};
    }

    void recordState(const std::string& name, const std::string& value)
    {
        _recordedStates[name] = value;
    }

    /// In the case we need to rememeber all the events that come, not just
    /// the final state.
    void recordEventSequence(const int type, const std::string& payload)
    {
        _recordedEventsVector.emplace_back(type, payload);
    }

    void clear()
    {
        _invalidate = false;
        _recordedEvents.clear();
        _recordedViewEvents.clear();
        _recordedStates.clear();
        _recordedEventsVector.clear();
    }
};

/// Represents a session to the WSD process, in a Kit process. Note that this is not a singleton.
class ChildSession final : public Session
{
public:
    /// Create a new ChildSession
    /// ws The socket between master and kit (jailed).
    /// loKit The LOKit instance.
    /// loKitDocument The instance to an existing document (when opening
    ///                 a new view) or nullptr (when first view).
    /// jailId The JailID of the jail root directory,
    //         used by downloadas to construct jailed path.
    ChildSession(const std::string& id,
                 const std::string& jailId,
                 DocumentManagerInterface& docManager);
    virtual ~ChildSession();

    bool getStatus(const char* buffer, int length);
    int getViewId() const { return _viewId; }
    void setViewId(const int viewId) { _viewId = viewId; }
    const std::string& getViewUserId() const { return getUserId(); }
    const std::string& getViewUserName() const { return getUserName(); }
    const std::string& getViewUserExtraInfo() const { return getUserExtraInfo(); }
    void updateSpeed();
    int getSpeed();

    void loKitCallback(const int type, const std::string& payload);

    std::shared_ptr<Watermark> _docWatermark;

    bool sendTextFrame(const char* buffer, int length) override
    {
        const auto msg = "client-" + getId() + ' ' + std::string(buffer, length);
        const std::unique_lock<std::mutex> lock = getLock();
        return _docManager->sendFrame(msg.data(), msg.size(), WSOpCode::Text);
    }

    bool sendBinaryFrame(const char* buffer, int length) override
    {
        const auto msg = "client-" + getId() + ' ' + std::string(buffer, length);
        const std::unique_lock<std::mutex> lock = getLock();
        return _docManager->sendFrame(msg.data(), msg.size(), WSOpCode::Binary);
    }

    using Session::sendTextFrame;

    bool getClipboard(const char* buffer, int length, const std::vector<std::string>& tokens);

    void resetDocManager()
    {
#if MOBILEAPP
        // I suspect this might be useful even for the non-mobile case, but
        // not 100% sure, so rather do it mobile-only for now
        disconnect();
#endif
        _docManager = nullptr;
    }

private:
    bool loadDocument(const char* buffer, int length, const std::vector<std::string>& tokens);

    bool sendFontRendering(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool getCommandValues(const char* buffer, int length, const std::vector<std::string>& tokens);

    bool clientZoom(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool clientVisibleArea(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool outlineState(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool downloadAs(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool getChildId();
    bool getTextSelection(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool setClipboard(const char* buffer, int length, const std::vector<std::string>& tokens);
    std::string getTextSelectionInternal(const std::string& mimeType);
    bool paste(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool insertFile(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool keyEvent(const char* buffer, int length, const std::vector<std::string>& tokens, const LokEventTargetEnum target);
    bool extTextInputEvent(const char* /*buffer*/, int /*length*/, const std::vector<std::string>& tokens);
    bool dialogKeyEvent(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool mouseEvent(const char* buffer, int length, const std::vector<std::string>& tokens, const LokEventTargetEnum target);
    bool gestureEvent(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool dialogEvent(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool completeFunction(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool unoCommand(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool selectText(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool selectGraphic(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool renderWindow(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool resizeWindow(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool resetSelection(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool saveAs(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool setClientPart(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool selectClientPart(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool moveSelectedClientParts(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool setPage(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool sendWindowCommand(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool signDocumentContent(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool askSignatureStatus(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool uploadSignedDocument(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool exportSignAndUploadDocument(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool renderShapeSelection(const char* buffer, int length, const std::vector<std::string>& tokens);
    bool removeTextContext(const char* /*buffer*/, int /*length*/, const std::vector<std::string>& tokens);

    void rememberEventsForInactiveUser(const int type, const std::string& payload);

    virtual void disconnect() override;
    virtual bool _handleInput(const char* buffer, int length) override;

    std::shared_ptr<lok::Document> getLOKitDocument()
    {
        return _docManager->getLOKitDocument();
    }

    std::string getLOKitLastError()
    {
        char *lastErr = _docManager->getLOKit()->getError();
        std::string ret;
        if (lastErr)
        {
            ret = std::string(lastErr, strlen(lastErr));
            free (lastErr);
        }
        return ret;
    }

private:
    const std::string _jailId;
    DocumentManagerInterface* _docManager;

    std::queue<std::chrono::steady_clock::time_point> _cursorInvalidatedEvent;
    const unsigned _eventStorageIntervalMs = 15*1000;

    /// View ID, returned by createView() or 0 by default.
    int _viewId;

    /// Whether document has been opened succesfuly
    bool _isDocLoaded;

    std::string _docType;

    StateRecorder _stateRecorder;

    /// If we are copying to clipboard.
    bool _copyToClipboard;

    /// Synchronize _loKitDocument access.
    /// This should be owned by Document.
    static std::recursive_mutex Mutex;
};

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */