Skip to content
Source
Sign inSign up
Open History to see the last commit

captureService.js

62 lines1.8 KB

1export const CLIENT_NAMES = [
2 "com.t3tools.T3Code.SnapShot",
3 "com.t3tools.T3Code.Development.SnapShot",
4];
5​
6export function isWaylandSession(meta) {
7 // GNOME 50 removed the X11 compositor and this API. GNOME 45–49 still need the check.
8 return typeof meta.is_wayland_compositor !== "function" || meta.is_wayland_compositor();
9}
10​
11export class CaptureService {
12 constructor({ getNameOwner, isAvailable, takeSnapshot, getProcessId, beginFeedback }) {
13 this._getNameOwner = getNameOwner;
14 this._isAvailable = isAvailable;
15 this._takeSnapshot = takeSnapshot;
16 this._getProcessId = getProcessId;
17 this._beginFeedback = beginFeedback;
18 this._enabled = true;
19 this._busy = false;
20 }
21​
22 disable() {
23 this._enabled = false;
24 }
25​
26 _checkSession() {
27 if (!this._enabled || !this._isAvailable()) {
28 throw new Error("SnapShots are unavailable in this session.");
29 }
30 }
31​
32 async capture(sender, options) {
33 this._checkSession();
34 if (this._busy) throw new Error("A snapshot is already in progress.");
35 this._busy = true;
36 try {
37 let allowed = false;
38 for (const name of CLIENT_NAMES) {
39 const owner = await this._getNameOwner(name);
40 if (owner === sender) {
41 allowed = true;
42 break;
43 }
44 }
45 if (!allowed) throw new Error("Only T3 Code may request a snapshot.");
46 const pid = options ? await this._getProcessId(sender) : undefined;
47 this._checkSession();
48 const snapshot = await this._takeSnapshot(options?.animate ?? false);
49 // Never deliver pixels after locking the session or disabling the extension.
50 this._checkSession();
51 if (options) {
52 return {
53 ...snapshot,
54 animationStarted: this._beginFeedback(sender, pid, snapshot, options),
55 };
56 }
57 return snapshot;
58 } finally {
59 this._busy = false;
60 }
61 }