summaryrefslogtreecommitdiffstats
path: root/common/Session.hpp
blob: 43d41ea22c70990efdf7220ab16f1f3b18dd0969 (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#pragma once

#include <atomic>
#include <cassert>
#include <memory>
#include <mutex>
#include <map>
#include <ostream>
#include <type_traits>

#include <Poco/Buffer.h>
#include <Poco/Path.h>
#include <Poco/Types.h>

#include "Protocol.hpp"
#include "Log.hpp"
#include "MessageQueue.hpp"
#include "Message.hpp"
#include "TileCache.hpp"
#include "WebSocketHandler.hpp"

class Session;

template<class T>
class SessionMap : public std::map<std::string, std::shared_ptr<T> >
{
    std::map<std::string, int> _canonicalIds;
public:
    SessionMap() {
        static_assert(std::is_base_of<Session, T>::value, "sessions must have base of Session");
    }
    /// Generate a unique key for this set of view properties
    int getCanonicalId(const std::string &viewProps)
    {
        if (viewProps.empty())
            return 0;
        for (auto &it : _canonicalIds) {
            if (it.first == viewProps)
                return it.second;
        }
        size_t id = _canonicalIds.size() + 1;
        _canonicalIds[viewProps] = id;
        return id;
    }
    std::shared_ptr<T> findByCanonicalId(int id)
    {
        for (const auto &it : *this) {
            if (it.second->getCanonicalViewId() == id)
                return it.second;
        }
        return std::shared_ptr<T>();
    }
    void dumpState(std::ostream& oss)
    {
        for (const auto &it : *this) {
            oss << "\tsession '" << it.first << "'\n";
            it.second->dumpState(oss);
        }
    }
};

/// Base class of a WebSocket session.
class Session : public MessageHandlerInterface
{
public:
    const std::string& getId() const { return _id; }
    const std::string& getName() const { return _name; }
    bool isDisconnected() const { return _disconnected; }

    virtual void setReadOnly(bool bValue = true) { _isReadOnly = bValue; }
    bool isReadOnly() const { return _isReadOnly; }

    void setAllowChangeComments(bool bValue = true)
    {
        _isAllowChangeComments = bValue;
    }
    bool isAllowChangeComments() const { return _isAllowChangeComments; }

    /// overridden to prepend client ids on messages by the Kit
    virtual bool sendBinaryFrame(const char* buffer, int length);
    virtual bool sendTextFrame(const char* buffer, const int length);

    /// Get notified that the underlying transports disconnected
    void onDisconnect() override { /* ignore */ }

    bool hasQueuedMessages() const override
    {
        // queued in Socket output buffer
        return false;
    }

    // By default rely on the socket buffer.
    void writeQueuedMessages() override
    {
        assert(false);
    }

    /// Sends a WebSocket Text message.
    int sendMessage(const std::string& msg)
    {
        return sendTextFrame(msg.data(), msg.size());
    }

    // FIXME: remove synonym - and clean from WebSocketHandler too ... (?)
    bool sendTextFrame(const std::string& text)
    {
        return sendTextFrame(text.data(), text.size());
    }

    template <std::size_t N>
    bool sendTextFrame(const char (&buffer)[N])
    {
        return (buffer != nullptr && N > 0 ? sendTextFrame(buffer, N) : false);
    }

    bool sendTextFrame(const char* buffer)
    {
        return (buffer != nullptr ? sendTextFrame(buffer, std::strlen(buffer)) : false);
    }

    bool sendTextFrameAndLogError(const std::string& text)
    {
        LOG_ERR(text);
        return sendTextFrame(text.data(), text.size());
    }

    bool sendTextFrameAndLogError(const char* buffer)
    {
        LOG_ERR(buffer);
        return (buffer != nullptr ? sendTextFrame(buffer, std::strlen(buffer)) : false);
    }

    virtual void handleMessage(const std::vector<char> &data) override;

    /// Invoked when we want to disconnect a session.
    virtual void disconnect();

    /// clean & normal shutdown
    void shutdownNormal(const std::string& statusMessage = "")    { shutdown(false, statusMessage); }

    /// abnormal / hash shutdown end-point going away
    void shutdownGoingAway(const std::string& statusMessage = "") { shutdown(true, statusMessage); }

    bool isActive() const { return _isActive; }
    void setIsActive(bool active) { _isActive = active; }

    /// Returns the inactivity time of the client in milliseconds.
    double getInactivityMS() const
    {
        const auto duration = (std::chrono::steady_clock::now() - _lastActivityTime);
        return std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
    }

    void closeFrame() { _isCloseFrame = true; };
    bool isCloseFrame() const { return _isCloseFrame; }

    void getIOStats(uint64_t &sent, uint64_t &recv);

    void setUserId(const std::string& userId) { _userId = userId; }

    const std::string& getUserId() const { return _userId; }

    void setWatermarkText(const std::string& watermarkText) { _watermarkText = watermarkText; }

    void setUserExtraInfo(const std::string& userExtraInfo) { _userExtraInfo = userExtraInfo; }

    void setUserName(const std::string& userName) { _userName = userName; }

    const std::string& getUserName() const {return _userName; }

    const std::string& getUserNameAnonym() const { return _userNameAnonym; }

    bool isDocPasswordProtected() const { return _isDocPasswordProtected; }

    const std::string& getDocOptions() const { return _docOptions; }

    bool hasWatermark() const { return !_watermarkText.empty() && _watermarkOpacity > 0.0; }

    const std::string& getWatermarkText() const { return _watermarkText; }

    double getWatermarkOpacity() const { return _watermarkOpacity; }

    const std::string& getLang() const { return _lang; }

    bool getHaveDocPassword() const { return _haveDocPassword; }

    void setHaveDocPassword(const bool val) { _haveDocPassword = val; }

    void setDocPassword(const std::string& password) { _docPassword = password; }

    const std::string& getDocPassword() const { return _docPassword; }

    const std::string& getUserExtraInfo() const { return _userExtraInfo; }

    const std::string& getDocURL() const { return  _docURL; }

    const std::string& getJailedFilePath() const { return _jailedFilePath; }

    const std::string& getJailedFilePathAnonym() const { return _jailedFilePathAnonym; }

    int  getCanonicalViewId() { return _canonicalViewId; }
    template<class T> void recalcCanonicalViewId(SessionMap<T> &map)
    {
        _canonicalViewId = map.getCanonicalId(_watermarkText);
    }

    const std::string& getDeviceFormFactor() const { return _deviceFormFactor; }

protected:
    Session(const std::shared_ptr<ProtocolHandlerInterface> &handler,
            const std::string& name, const std::string& id, bool readonly);
    virtual ~Session();

    /// Parses the options of the "load" command,
    /// shared between MasterProcessSession::loadDocument() and ChildProcessSession::loadDocument().
    void parseDocOptions(const StringVector& tokens, int& part, std::string& timestamp, std::string& doctemplate);

    void updateLastActivityTime()
    {
        _lastActivityTime = std::chrono::steady_clock::now();
    }

    void dumpState(std::ostream& os) override;

private:

    void shutdown(bool goingAway = false, const std::string& statusMessage = "");

    virtual bool _handleInput(const char* buffer, int length) = 0;

    /// A session ID specific to an end-to-end connection (from user to lokit).
    const std::string _id;

    /// A readable name that identifies our peer and ID.
    const std::string _name;

    /// True if we have been disconnected.
    std::atomic<bool> _disconnected;
    /// True if the user is active, otherwise false (switched tabs).
    std::atomic<bool> _isActive;

    /// Time of the last interactive event being received
    std::chrono::steady_clock::time_point _lastActivityTime;

    // Whether websocket received close frame.  Closing Handshake
    std::atomic<bool> _isCloseFrame;

    std::mutex _mutex;

    /// Whether the session is opened as readonly
    bool _isReadOnly;

    /// If the session is read-only, are comments allowed
    bool _isAllowChangeComments;

    /// The actual URL, also in the child, even if the child never accesses that.
    std::string _docURL;

    /// The Jailed document path.
    std::string _jailedFilePath;

    /// The Jailed document path, anonymized for logging.
    std::string _jailedFilePathAnonym;

    /// Password provided, if any, to open the document
    std::string _docPassword;

    /// If password is provided or not
    bool _haveDocPassword;

    /// Whether document is password protected
    bool _isDocPasswordProtected;

    /// Document options: a JSON string, containing options (rendering, also possibly load in the future).
    std::string _docOptions;

    /// Id of the user to whom the session belongs to.
    std::string _userId;

    /// Id of the user to whom the session belongs to, anonymized for logging.
    std::string _userIdAnonym;

    /// Name of the user to whom the session belongs to.
    std::string _userName;

    /// Name of the user to whom the session belongs to, anonymized for logging.
    std::string _userNameAnonym;

    /// Extra info per user, mostly mail, avatar, links, etc.
    std::string _userExtraInfo;

    /// In case a watermark has to be rendered on each tile.
    std::string _watermarkText;

    /// Opacity in case a watermark has to be rendered on each tile.
    double _watermarkOpacity;

    /// Language for the document based on what the user has in the UI.
    std::string _lang;

    /// the canonical id unique to the set of rendering properties of this session
    int _canonicalViewId;

    /// The form factor of the device where the client is running: desktop, tablet, mobile.
    std::string _deviceFormFactor;
};

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