-
Notifications
You must be signed in to change notification settings - Fork 6
/
restoreSession.js
executable file
·380 lines (332 loc) · 16.3 KB
/
restoreSession.js
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
'use strict';
const { Shell, Gio, GLib } = imports.gi;
const Util = imports.misc.util;
const { ByteArray } = imports.byteArray;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const FileUtils = Me.imports.utils.fileUtils;
const Log = Me.imports.utils.log;
const PrefsUtils = Me.imports.utils.prefsUtils;
const SubprocessUtils = Me.imports.utils.subprocessUtils;
const DateUtils = Me.imports.utils.dateUtils;
const UiHelper = Me.imports.ui.uiHelper;
// All launching apps by Shell.App#launch()
var restoringApps = new Map();
var RestoreSession = class {
constructor() {
this._log = new Log.Log();
this._prefsUtils = new PrefsUtils.PrefsUtils();
this._settings = this._prefsUtils.getSettings();
this.sessionName = FileUtils.default_sessionName;
this._defaultAppSystem = Shell.AppSystem.get_default();
this._windowTracker = Shell.WindowTracker.get_default();
this._restore_session_interval = this._settings.get_int('restore-session-interval');
// TODO Add to Preferences?
// Launch apps using discrete graphics card might cause issues, like the white main window of superproductivity
this._useDiscreteGraphicsCard = false;
// All launched apps info by Shell.App#launch()
this._restoredApps = new Map();
// Tracking cmd and appId mapping
this._cmdAppIdMap = new Map();
this._display = global.display;
this._connectIds = [];
}
/**
* Restore workspaces and make them persistent, etc
*/
static restoreFromSummary() {
Log.Log.getDefault().debug(`Prepare to restore summary`);
FileUtils.loadSummary().then(([summary, path]) => {
Log.Log.getDefault().info(`Restoring summary from ${path}`);
const savedNWorkspace = summary.n_workspace;
const workspaceManager = global.workspace_manager;
const currentNWorkspace = workspaceManager.n_workspaces;
const moreWorkspace = savedNWorkspace - currentNWorkspace;
if (moreWorkspace) {
for (let i = currentNWorkspace; i <= savedNWorkspace; i++) {
workspaceManager.append_new_workspace(false, DateUtils.get_current_time());
workspaceManager.get_workspace_by_index(i)._keepAliveId = true;
}
}
}).catch(e => Log.Log.getDefault().error(e));
}
restoreSession(sessionName) {
if (!sessionName) {
sessionName = this.sessionName;
}
const sessions_path = FileUtils.get_sessions_path();
const session_file_path = GLib.build_filenamev([sessions_path, sessionName]);
if (!GLib.file_test(session_file_path, GLib.FileTest.EXISTS)) {
logError(new Error(`Session file not found: ${session_file_path}`));
return;
}
this._log.info(`Restoring saved session from ${session_file_path}`);
try {
this.restoreSessionFromFile(session_file_path);
} catch (e) {
logError(e, `Failed to restore ${session_file_path}`);
}
}
restoreSessionFromFile(session_file_path) {
const session_file = Gio.File.new_for_path(session_file_path);
let [success, contents] = session_file.load_contents(null);
if (!success) {
return;
}
let session_config = FileUtils.getJsonObj(contents);
let session_config_objects = session_config.x_session_config_objects;
if (!(session_config_objects && session_config_objects.length)) {
this._log.error(new Error(`Session details not found: ${session_file_path}`));
global.notify_error(`No session to restore from ${session_file_path}`, `session config is empty.`);
return;
}
session_config_objects = session_config_objects.filter(session_config_object => {
const desktop_file_id = session_config_object.desktop_file_id;
if (!desktop_file_id) {
return true;
}
const shellApp = this._defaultAppSystem.lookup_app(desktop_file_id);
if (!shellApp) {
return true;
}
if (this._appIsRunning(shellApp)) {
this._log.debug(`${shellApp.get_name()} is already running`)
return false;
}
return true;
});
if (session_config_objects.length === 0) return;
this._restoreOneSession(session_config_objects.shift());
if (session_config_objects.length === 0) return;
this._restoreSessionTimeoutId = GLib.timeout_add(GLib.PRIORITY_DEFAULT,
// In milliseconds.
// Note that this timing might not be precise, see https://gjs-docs.gnome.org/glib20~2.66.1/glib.timeout_add
this._restore_session_interval,
() => {
if (!session_config_objects.length) {
return GLib.SOURCE_REMOVE;
}
this._restoreOneSession(session_config_objects.shift());
return GLib.SOURCE_CONTINUE;
}
);
}
async restorePreviousSession(removeAfterRestore) {
try {
this._log.info(`Restoring the previous session from ${FileUtils.current_session_path}`);
const ignoringPaths = [GLib.build_filenamev([FileUtils.current_session_path, 'null'])];
FileUtils.listAllSessions(FileUtils.current_session_path, true, (file, info) => {
const contentType = info.get_content_type();
if (contentType === 'application/json' && !ignoringPaths.includes(file.get_parent().get_path())) {
file.load_contents_async(
null,
(file, asyncResult) => {
const [success, contents, _] = file.load_contents_finish(asyncResult);
if (!success) {
return;
}
const sessionConfig = FileUtils.getJsonObj(contents);
this._restoreOneSession(sessionConfig).then(([launched, running]) => {
if (removeAfterRestore && launched && !running) {
const path = file.get_path();
this._log.debug(`Restored ${sessionConfig.window_title}(${sessionConfig.app_name}), cleaning ${path}`);
FileUtils.removeFile(path);
}
}).catch(e => this._log.error(e));
});
}
});
} catch (error) {
this._log.error(error);
}
}
async _restoreOneSession(session_config_object) {
const app_name = session_config_object.app_name;
let launched = false;
let running = false;
try {
return await new Promise((resolve, reject) => {
let desktop_file_id = session_config_object.desktop_file_id;
const shell_app = desktop_file_id ? this._defaultAppSystem.lookup_app(desktop_file_id) : null;
if (shell_app) {
const restoringShellAppData = restoringApps.get(shell_app);
if (restoringShellAppData) {
restoringShellAppData.saved_window_sessions.push(session_config_object);
} else {
restoringApps.set(shell_app, {
saved_window_sessions: [session_config_object]
});
}
const desktopNumber = session_config_object.desktop_number;
[launched, running] = this.launch(shell_app, desktopNumber);
if (launched) {
if (!running) {
this._log.info(`${app_name} has been launched! Preparing to restore window ${session_config_object.window_title}(${app_name})!`);
}
const existingShellAppData = this._restoredApps.get(shell_app);
if (existingShellAppData) {
existingShellAppData.saved_window_sessions.push(session_config_object);
} else {
this._restoredApps.set(shell_app, {
saved_window_sessions: [session_config_object]
});
}
} else {
this._log.error(`Failed to launch ${app_name}`, `Failed to launch ${app_name}`);
global.notify_error(`Failed to launch ${app_name}`, `Failed to launch ${app_name}`);
}
resolve([launched, running]);
} else {
// https://gjs-docs.gnome.org/gio20~2.0/gio.subprocesslauncher#method-set_environ
// TODO Support snap apps
const cmd = session_config_object.cmd;
if (cmd && cmd.length) {
const cmdString = cmd.join(' ');
const pid = this._cmdAppIdMap.get(cmdString);
if (pid) {
this._log.debug(`${app_name} might be running, preparing to restore window (${session_config_object.window_title}) states.`);
// Here we use pid as the key, because the associated ShellApp might not be instantiated at this moment
const restoringShellAppData = restoringApps.get(pid);
if (restoringShellAppData) {
restoringShellAppData.saved_window_sessions.push(session_config_object);
} else {
restoringApps.set(pid, {
saved_window_sessions: [session_config_object]
});
}
}
const launchAppTemplate = FileUtils.desktop_template_launch_app_shell_script;
const launchAppShellScript = FileUtils.loadTemplate(launchAppTemplate).fill({cmdString});
this._log.info(`Launching ${app_name} via command line ${cmdString}!`);
SubprocessUtils.trySpawnCmdstr(`bash -c '${launchAppShellScript}'`).then(
([success, status, stdoutInputStream, stderrInputStream]) => {
if (success) {
stdoutInputStream.read_line_async(
GLib.PRIORITY_DEFAULT,
null,
(stream, res) => {
try {
let pid = stream.read_line_finish_utf8(res)[0];
if (!pid) return;
pid = Number(pid);
this._cmdAppIdMap.set(cmdString, pid);
const restoringShellAppData = restoringApps.get(pid);
if (restoringShellAppData) {
restoringShellAppData.saved_window_sessions.push(session_config_object);
} else {
restoringApps.set(pid, {
saved_window_sessions: [session_config_object]
});
}
launched = true;
resolve([launched, running]);
} catch (e) {
this._log.error(e);
reject(e);
}
}
);
} else {
if (status === 79) {
launched = true;
running = true;
this._log.info(`${app_name} is running, skipping`)
} else {
const msg = `Failed to launch ${app_name} via command line`;
this._log.error(`${msg}. output: ${stderr}`);
global.notify_error(`${msg}`, `${stderr}.`);
}
resolve([launched, running]);
}
}).catch(e => {
this._log.error(e)
reject(e);
});
} else {
// TODO try to launch via app_info by searching the app name?
let errorMsg = `Failed to launch ${app_name} via command line`;
this._log.error(errorMsg, `Invalid command line: ${cmd}`);
global.notify_error(errorMsg, `Invalid command line: ${cmd}`);
resolve([launched, running]);
}
}
});
} catch (e) {
logError(e, `Failed to restore ${app_name}`);
if (!launched) {
global.notify_error(`Failed to restore ${app_name}`, e.message);
}
return [launched, running];
}
}
launch(shellApp, desktopNumber) {
if (this._restoredApps.has(shellApp)) {
this._log.info(`${shellApp.get_name()} is restored, skipping`);
return [true, false];
}
if (this._appIsRunning(shellApp)) {
this._log.info(`${shellApp.get_name()} is running, skipping`);
// Delete shellApp from restoringApps to prevent it move the same app when close and open it manually.
restoringApps.delete(shellApp);
return [true, true];
}
const launched = shellApp.launch(
// 0 for current event timestamp
0,
desktopNumber,
this._getProperGpuPref(shellApp));
return [launched, false];
}
_appIsRunning(app) {
// Running apps can be empty even if there are apps running when gnome-shell starting
const running_apps = this._defaultAppSystem.get_running();
for (const running_app of running_apps) {
if (running_app.get_id() === app.get_id() &&
running_app.get_state() >= Shell.AppState.STARTING) {
return true;
}
}
return false;
}
_getProperGpuPref(shell_app) {
if (this._useDiscreteGraphicsCard) {
const app_info = shell_app.get_app_info();
if (app_info) {
return app_info.get_boolean('PrefersNonDefaultGPU')
? Shell.AppLaunchGpu.DEFAULT
: Shell.AppLaunchGpu.DISCRETE;
}
}
return Shell.AppLaunchGpu.DEFAULT;
}
destroy() {
if (restoringApps) {
restoringApps.clear();
restoringApps = null;
}
if (this._restoredApps) {
this._restoredApps.clear();
this._restoredApps = null;
}
if (this._defaultAppSystem) {
this._defaultAppSystem = null;
}
if (this._windowTracker) {
this._windowTracker = null;
}
if (this._log) {
this._log.destroy();
this._log = null;
}
if (this._connectIds) {
for (let [obj, id] of this._connectIds) {
obj.disconnect(id);
}
this._connectIds = null;
}
if (this._restoreSessionTimeoutId) {
GLib.Source.remove(this._restoreSessionTimeoutId);
this._restoreSessionTimeoutId = null;
}
}
}