Bug 1944416: Restore individual tabs from closed groups in closed windows r=dao,sessi...
[gecko.git] / browser / components / sessionstore / RunState.sys.mjs
blob94f9a86fcddce87697b65cda2c57f18eaa320315
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 const STATE_STOPPED = 0;
6 const STATE_RUNNING = 1;
7 const STATE_QUITTING = 2;
8 const STATE_CLOSING = 3;
9 const STATE_CLOSED = 4;
11 // We're initially stopped.
12 var state = STATE_STOPPED;
14 /**
15  * This module keeps track of SessionStore's current run state. We will
16  * always start out at STATE_STOPPED. After the session was read from disk and
17  * the initial browser window has loaded we switch to STATE_RUNNING. On the
18  * first notice that a browser shutdown was granted we switch to STATE_QUITTING.
19  */
20 export var RunState = Object.freeze({
21   // If we're stopped then SessionStore hasn't been initialized yet. As soon
22   // as the session is read from disk and the initial browser window has loaded
23   // the run state will change to STATE_RUNNING.
24   get isStopped() {
25     return state == STATE_STOPPED;
26   },
28   // STATE_RUNNING is our default mode of operation that we'll spend most of
29   // the time in. After the session was read from disk and the first browser
30   // window has loaded we remain running until the browser quits.
31   get isRunning() {
32     return state == STATE_RUNNING;
33   },
35   // We will enter STATE_QUITTING as soon as we receive notice that a browser
36   // shutdown was granted. SessionStore will use this information to prevent
37   // us from collecting partial information while the browser is shutting down
38   // as well as to allow a last single write to disk and block all writes after
39   // that.
40   get isQuitting() {
41     return state >= STATE_QUITTING;
42   },
44   // We will enter STATE_CLOSING as soon as SessionStore is uninitialized.
45   // The SessionFile module will know that a last write will happen in this
46   // state and it can do some necessary cleanup.
47   get isClosing() {
48     return state == STATE_CLOSING;
49   },
51   // We will enter STATE_CLOSED as soon as SessionFile has written to disk for
52   // the last time before shutdown and will not accept any further writes.
53   get isClosed() {
54     return state == STATE_CLOSED;
55   },
57   // Switch the run state to STATE_RUNNING. This must be called after the
58   // session was read from, the initial browser window has loaded and we're
59   // now ready to restore session data.
60   setRunning() {
61     if (this.isStopped) {
62       state = STATE_RUNNING;
63     }
64   },
66   // Switch the run state to STATE_CLOSING. This must be called *before* the
67   // last SessionFile.write() call so that SessionFile knows we're closing and
68   // can do some last cleanups and write a proper sessionstore.js file.
69   setClosing() {
70     if (this.isQuitting) {
71       state = STATE_CLOSING;
72     }
73   },
75   // Switch the run state to STATE_CLOSED. This must be called by SessionFile
76   // after the last write to disk was accepted and no further writes will be
77   // allowed. Any writes after this stage will cause exceptions.
78   setClosed() {
79     if (this.isClosing) {
80       state = STATE_CLOSED;
81     }
82   },
84   // Switch the run state to STATE_QUITTING. This should be called once we're
85   // certain that the browser is going away and before we start collecting the
86   // final window states to save in the session file.
87   setQuitting() {
88     if (this.isRunning) {
89       state = STATE_QUITTING;
90     }
91   },
92 });