forked from CollaboraOnline/online
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKit.hpp
490 lines (366 loc) · 15.1 KB
/
Kit.hpp
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* Copyright the Collabora Online contributors.
*
* SPDX-License-Identifier: MPL-2.0
*
* 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 <Poco/Util/XMLConfiguration.h>
#include <map>
#include <string>
#include <common/Util.hpp>
#include <common/StateEnum.hpp>
#include <common/Session.hpp>
#include <common/ThreadPool.hpp>
#include <kit/KitQueue.hpp>
#include <wsd/TileDesc.hpp>
#include "Socket.hpp"
#define LOK_USE_UNSTABLE_API
#include <LibreOfficeKit/LibreOfficeKit.hxx>
#if MOBILEAPP
#include "ClientSession.hpp"
#include "DocumentBroker.hpp"
#endif
void lokit_main(
#if !MOBILEAPP
const std::string& childRoot, const std::string& jailId, const std::string& sysTemplate,
const std::string& loTemplate, bool noCapabilities, bool noSeccomp, bool useMountNamespaces,
bool queryVersionInfo, bool displayVersion, bool sysTemplateIncomplete,
#else
int docBrokerSocket, const std::string& userInterface,
#endif
std::size_t numericIdentifier);
#ifdef IOS
void runKitLoopInAThread();
#endif
bool globalPreinit(const std::string& loTemplate);
/// Wrapper around private Document::ViewCallback().
void documentViewCallback(const int type, const char* p, void* data);
class Document;
class DeltaGenerator;
/// Descriptor class used to link a LOK
/// callback to a specific view.
struct CallbackDescriptor
{
CallbackDescriptor(Document* const doc, const int viewId)
: _doc(doc), _viewId(viewId)
{
}
Document* getDoc() const { return _doc; }
int getViewId() const { return _viewId; }
private:
Document* const _doc;
const int _viewId;
};
/// User Info container used to store user information
/// till the end of process lifecycle - including
/// after any child session goes away
struct UserInfo
{
UserInfo()
: _readOnly(false)
, _connected(false)
{
}
UserInfo(const std::string& userId, const std::string& userName,
const std::string& userExtraInfo, const std::string& userPrivateInfo, bool readOnly)
: _userId(userId)
, _userName(userName)
, _userExtraInfo(userExtraInfo)
, _userPrivateInfo(userPrivateInfo)
, _readOnly(readOnly)
, _connected(true)
{
}
const std::string& getUserId() const { return _userId; }
const std::string& getUserName() const { return _userName; }
const std::string& getUserExtraInfo() const { return _userExtraInfo; }
const std::string& getUserPrivateInfo() const { return _userPrivateInfo; }
bool isReadOnly() const { return _readOnly; }
bool isConnected() const { return _connected; }
void setDisconnected() { _connected = false; }
private:
std::string _userId;
std::string _userName;
std::string _userExtraInfo;
std::string _userPrivateInfo;
bool _readOnly;
bool _connected;
};
/// We have two types of password protected documents
/// 1) Documents which require password to view
/// 2) Document which require password to modify
enum class DocumentPasswordType
{
ToView,
ToModify
};
/// Check the ForkCounter, and if non-zero, fork more of them accordingly.
void forkLibreOfficeKit(const std::string& childRoot, const std::string& sysTemplate,
const std::string& loTemplate, bool useMountNamespaces);
class Document;
/// The main main-loop of the Kit process
class KitSocketPoll final : public SocketPoll
{
std::chrono::steady_clock::time_point _pollEnd;
std::shared_ptr<Document> _document;
static KitSocketPoll* mainPoll;
KitSocketPoll();
public:
~KitSocketPoll();
void drainQueue();
static void dumpGlobalState(std::ostream& oss);
static std::shared_ptr<KitSocketPoll> create();
static void cleanupChildProcess();
virtual void wakeupHook() override;
static KitSocketPoll* getMainPoll() { return mainPoll; }
#if ENABLE_DEBUG
struct ReEntrancyGuard
{
std::atomic<int>& _count;
ReEntrancyGuard(std::atomic<int>& count)
: _count(count)
{
count++;
}
~ReEntrancyGuard() { _count--; }
};
#endif
int kitPoll(int timeoutMicroS);
void setDocument(std::shared_ptr<Document> document) { _document = std::move(document); }
std::shared_ptr<Document> getDocument() const { return _document; }
// unusual LOK event from another thread, push into our loop to process.
static bool pushToMainThread(LibreOfficeKitCallback callback, int type, const char* p,
void* data);
#ifdef IOS
static std::mutex KSPollsMutex;
static std::condition_variable KSPollsCV;
static std::vector<std::weak_ptr<KitSocketPoll>> KSPolls;
std::mutex terminationMutex;
std::condition_variable terminationCV;
bool terminationFlag;
#endif
};
class KitQueue;
class ChildSession;
/// A document container.
/// Owns LOKitDocument instance and connections.
/// Manages the lifetime of a document.
/// Technically, we can host multiple documents
/// per process. But for security reasons don't.
/// However, we could have a coolkit instance
/// per user or group of users (a trusted circle).
class Document final : public std::enable_shared_from_this<Document>
{
public:
Document(const std::shared_ptr<lok::Office>& loKit, const std::string& jailId,
const std::string& docKey, const std::string& docId, const std::string& url,
const std::shared_ptr<WebSocketHandler>& websocketHandler, unsigned mobileAppDocId);
virtual ~Document();
const std::string& getUrl() const { return _url; }
/// Post the message - in the unipoll world we're in the right thread anyway
bool postMessage(const char* data, int size, const WSOpCode code) const;
bool createSession(const std::string& sessionId);
/// Purges dead connections and returns
/// the remaining number of clients.
/// Returns -1 on failure.
std::size_t purgeSessions();
void setDocumentPassword(int passwordType);
void renderTiles(TileCombined& tileCombined);
bool sendTextFrame(const std::string& message)
{
return sendFrame(message.data(), message.size());
}
bool sendFrame(const char* buffer, int length, WSOpCode opCode = WSOpCode::Text);
void alertNotAsync()
{
// load unfortunately enables inputprocessing in some cases.
if (processInputEnabled() && !_duringLoad && !isBackgroundSaveProcess())
notifyAll("error: cmd=notasync kind=failure");
}
void alertAllUsers(const std::string& cmd, const std::string& kind)
{
sendTextFrame("errortoall: cmd=" + cmd + " kind=" + kind);
}
/// Notify all views with the given message
bool notifyAll(const std::string& msg)
{
// Broadcast updated viewinfo to all clients.
return sendTextFrame("client-all " + msg);
}
unsigned getMobileAppDocId() const { return _mobileAppDocId; }
/// See if we should clear out our memory
void trimIfInactive();
void trimAfterInactivity();
// LibreOfficeKit callback entry points
static void GlobalCallback(const int type, const char* p, void* data);
static void ViewCallback(const int type, const char* p, void* data);
private:
/// Helper method to broadcast callback and its payload to all clients
void broadcastCallbackToClients(const int type, const std::string& payload)
{
_queue->putCallback(-1, type, payload);
}
public:
/// Request loading a document, or a new view, if one exists,
/// and register callbacks.
bool onLoad(const std::string& sessionId, const std::string& uriAnonym,
const std::string& renderOpts);
/// Unload a client session, which unloads the document
/// if it is the last and only.
void onUnload(const ChildSession& session);
/// Get a view ID <-> UserInfo map.
std::map<int, UserInfo> getViewInfo() { return _sessionUserInfo; }
int getEditorId() const { return _editorId; }
bool isDocPasswordProtected() const { return _isDocPasswordProtected; }
bool haveDocPassword() const { return _haveDocPassword; }
std::string getDocPassword() const { return _docPassword; }
DocumentPasswordType getDocPasswordType() const { return _docPasswordType; }
void updateActivityHeader() const;
bool joinThreads();
void startThreads();
bool forkToSave(const std::function<void()> &childSave, int viewId);
void handleSaveMessage(const std::string &msg);
/// Notify all views of viewId and their associated usernames
void notifyViewInfo();
std::shared_ptr<ChildSession> findSessionByViewId(int viewId);
void invalidateCanonicalId(const std::string& sessionId);
std::string getViewProps(const std::shared_ptr<ChildSession>& session);
void updateEditorSpeeds(int id, int speed);
private:
// Get the color value for all author names from the core
std::map<std::string, int> getViewColors();
std::string getDefaultTheme(const std::shared_ptr<ChildSession>& session) const;
std::string getDefaultBackgroundTheme(const std::shared_ptr<ChildSession>& session) const;
std::shared_ptr<lok::Document> load(const std::shared_ptr<ChildSession>& session,
const std::string& renderOpts);
bool forwardToChild(const std::string& prefix, const std::vector<char>& payload);
static std::string makeRenderParams(const std::string& renderOpts, const std::string& userName,
const std::string& spellOnline, const std::string& theme,
const std::string& backgroundTheme);
bool isTileRequestInsideVisibleArea(const TileCombined& tileCombined);
public:
bool processInputEnabled() const;
/// A new message from wsd for the queue
void queueMessage(const std::string &msg) { _queue->put(msg); }
bool hasQueueItems() const { return _queue && !_queue->isEmpty(); }
bool hasCallbacks() const { return _queue && _queue->callbackSize() > 0; }
/// Should we get through the SocketPoll fast to process queus ?
bool needsQuickPoll() const
{
if (hasCallbacks())
return true;
if (hasQueueItems() && processInputEnabled())
return true;
return false;
}
// poll is idle, are we ?
void checkIdle();
void drainQueue();
void drainCallbacks();
void dumpState(std::ostream& oss);
/// Return access to the lok::Office instance.
std::shared_ptr<lok::Office> getLOKit() { return _loKit; }
/// Return access to the lok::Document instance.
std::shared_ptr<lok::Document> getLOKitDocument();
std::string getObfuscatedFileId() { return _obfuscatedFileId; }
bool isBackgroundSaveProcess() const { return _isBgSaveProcess; }
/// Save is async, so we need to set 'unmodified' while we are saving
/// but this can transition back to modified if save fails.
STATE_ENUM(ModifiedState, Modified, UnModifiedButSaving, UnModified);
ModifiedState getModified() const { return _modified; }
/// Lets us get notified whether a doc is modified during bgsave.
void forceDocUnmodifiedForBgSave(int viewId);
/// Restore the Document's 'modified' state if necessary
void updateModifiedOnFailedBgSave();
/// Let WSD know our true modified state affter bg save success.
void notifySyntheticUnmodifiedState();
/// Snoop document modified, and return true if filtering notification
bool trackDocModifiedState(const std::string &stateChanged);
/// Permanantly disable background save for this process
void disableBgSave(const std::string &reason);
/// Are we currently performing a load ?
bool isLoadOngoing() const { return _duringLoad > 0; }
std::shared_ptr<KitQueue> getQueue() const { return _queue; }
private:
void postForceModifiedCommand(bool modified);
/// Stops theads, flushes buffers, and exits the process.
void flushAndExit(int code);
private:
std::shared_ptr<lok::Office> _loKit;
const std::string _jailId;
/// URL-based key. May be repeated during the lifetime of WSD.
const std::string _docKey;
/// Short numerical ID. Unique during the lifetime of WSD.
const std::string _docId;
const std::string _url;
const std::string _obfuscatedFileId;
std::string _jailedUrl;
std::string _renderOpts;
std::shared_ptr<lok::Document> _loKitDocument;
#ifdef __ANDROID__
static std::shared_ptr<lok::Document> _loKitDocumentForAndroidOnly;
#endif
std::shared_ptr<KitQueue> _queue;
// Connection to the coolwsd process
std::shared_ptr<WebSocketHandler> _websocketHandler;
// Connection a child background save process has to its parent: a precious thing.
std::weak_ptr<WebSocketHandler> _saveProcessParent;
ModifiedState _modified;
bool _isBgSaveProcess;
bool _isBgSaveDisabled;
// Document password provided
std::string _docPassword;
// Whether password was provided or not
bool _haveDocPassword;
// Whether document is password protected
bool _isDocPasswordProtected;
// Whether password is required to view the document, or modify it
DocumentPasswordType _docPasswordType;
std::atomic<bool> _stop;
ThreadPool _deltaPool;
std::unique_ptr<DeltaGenerator> _deltaGen;
int _editorId;
bool _editorChangeWarning;
std::map<int, std::unique_ptr<CallbackDescriptor>> _viewIdToCallbackDescr;
SessionMap<ChildSession> _sessions;
/// The timestamp of the last memory trimming.
std::chrono::steady_clock::time_point _lastMemTrimTime;
std::map<int, std::chrono::steady_clock::time_point> _lastUpdatedAt;
std::map<int, int> _speedCount;
/// For showing disconnected user info in the doc repair dialog.
std::map<int, UserInfo> _sessionUserInfo;
#ifdef __ANDROID__
friend std::shared_ptr<lok::Document> getLOKDocumentForAndroidOnly();
#endif
const unsigned _mobileAppDocId;
int _duringLoad;
};
/// main function of the forkit process or thread
int forkit_main(int argc, char** argv);
/// Anonymize the basename of filenames, preserving the path and extension.
std::string anonymizeUrl(const std::string& url);
/// Anonymize usernames.
std::string anonymizeUsername(const std::string& username);
/// Ensure there is no fatal system setup problem
void consistencyCheckJail();
/// check how many theads we have currently
int getCurrentThreadCount();
/// Fetch the latest montonically incrementing wire-id
TileWireId getCurrentWireId(bool increment = false);
#ifdef __ANDROID__
/// For the Android app, for now, we need access to the one and only document open to perform eg. saveAs() for printing.
std::shared_ptr<lok::Document> getLOKDocumentForAndroidOnly();
#endif
extern _LibreOfficeKit* loKitPtr;
/// Check if URP is enabled
bool isURPEnabled();
/// Start a URP connection, checking if URP is enabled and there is not already an active URP session
bool startURP(std::shared_ptr<lok::Office> LOKit, void** ppURPContext);
/// Ensure all recorded traces hit the disk
void flushTraceEventRecordings();
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */