summaryrefslogtreecommitdiffstats
path: root/browser/components/sessionstore/SessionLogger.sys.mjs
blob: a7c99f911e24e0afd65ddfa017690534aede0b3c (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
/* 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/. */

import { LogManager } from "resource://gre/modules/LogManager.sys.mjs";
// See Bug 1889052
// eslint-disable-next-line mozilla/use-console-createInstance
import { Log } from "resource://gre/modules/Log.sys.mjs";

const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
  AsyncShutdown: "resource://gre/modules/AsyncShutdown.sys.mjs",
  cancelIdleCallback: "resource://gre/modules/Timer.sys.mjs",
  requestIdleCallback: "resource://gre/modules/Timer.sys.mjs",
});

const loggerNames = ["SessionStore"];

export const sessionStoreLogger = Log.repository.getLogger("SessionStore");
sessionStoreLogger.manageLevelFromPref("browser.sessionstore.loglevel");

class SessionLogManager extends LogManager {
  #idleCallbackId = null;
  #observers = new Set();

  QueryInterface = ChromeUtils.generateQI([Ci.nsIObserver]);

  constructor(options = {}) {
    super(options);

    Services.obs.addObserver(this, "sessionstore-windows-restored");
    this.#observers.add("sessionstore-windows-restored");

    lazy.AsyncShutdown.profileBeforeChange.addBlocker(
      "SessionLogManager: finalize and flush any logs to disk",
      () => {
        return this.stop();
      }
    );
  }

  async stop() {
    if (this.#observers.has("sessionstore-windows-restored")) {
      Services.obs.removeObserver(this, "sessionstore-windows-restored");
      this.#observers.delete("sessionstore-windows-restored");
    }
    await this.requestLogFlush(true);
    this.finalize();
  }

  observe(subject, topic, _) {
    switch (topic) {
      case "sessionstore-windows-restored":
        // this represents the moment session restore is nominally complete
        // and is a good time to ensure any log messages are flushed to disk
        Services.obs.removeObserver(this, "sessionstore-windows-restored");
        this.#observers.delete("sessionstore-windows-restored");
        this.requestLogFlush();
        break;
    }
  }

  async requestLogFlush(immediate = false) {
    if (this.#idleCallbackId && !immediate) {
      return;
    }
    if (this.#idleCallbackId) {
      lazy.cancelIdleCallback(this.#idleCallbackId);
      this.#idleCallbackId = null;
    }
    if (!immediate) {
      await new Promise(resolve => {
        this.#idleCallbackId = lazy.requestIdleCallback(resolve);
      });
      this.#idleCallbackId = null;
    }
    await this.resetFileLog();
  }
}

export const logManager = new SessionLogManager({
  prefRoot: "browser.sessionstore.",
  logNames: loggerNames,
  logFilePrefix: "sessionrestore",
  logFileSubDirectoryEntries: ["sessionstore-logs"],
  testTopicPrefix: "sessionrestore:log-manager:",
});