summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/ProfileAge.sys.mjs
blob: 94e991749f3def97a906de04ad3ab145cdd54912 (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
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
/* 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 { Log } from "resource://gre/modules/Log.sys.mjs";

const FILE_TIMES = "times.json";

/**
 * Traverse the contents of the profile directory, finding the oldest file
 * and returning its creation timestamp.
 */
async function getOldestProfileTimestamp(profilePath, log) {
  let start = Date.now();
  let oldest = start + 1000;
  log.debug("Iterating over profile " + profilePath);

  try {
    for (const childPath of await IOUtils.getChildren(profilePath)) {
      try {
        let info = await IOUtils.stat(childPath);
        let timestamp;
        if (info.creationTime !== undefined) {
          timestamp = info.creationTime;
        } else {
          // We only support file creation times on Mac and Windows. We have to
          // settle for mtime on Linux.
          log.debug("No birth date. Using mtime.");
          timestamp = info.lastModified;
        }

        log.debug(`Using date: ${childPath} = ${timestamp}`);
        if (timestamp < oldest) {
          oldest = timestamp;
        }
      } catch (e) {
        // Never mind.
        log.debug("Stat failure", e);
      }
    }
  } catch (reason) {
    throw new Error("Unable to fetch oldest profile entry: " + reason);
  }

  return oldest;
}

/**
 * Profile access to times.json (eg, creation/reset time).
 * This is separate from the provider to simplify testing and enable extraction
 * to a shared location in the future.
 */
class ProfileAgeImpl {
  constructor(profile, times) {
    this._profilePath = profile;
    this._times = times;
    this._log = Log.repository.getLogger("Toolkit.ProfileAge");

    if ("firstUse" in this._times && this._times.firstUse === null) {
      // Indicates that this is a new profile that needs a first use timestamp.
      this._times.firstUse = Date.now();
      this.writeTimes();
    }
  }

  get profilePath() {
    if (!this._profilePath) {
      this._profilePath = Services.dirsvc.get("ProfD", Ci.nsIFile).path;
    }

    return this._profilePath;
  }

  /**
   * There are two ways we can get our creation time:
   *
   * 1. From the on-disk JSON file.
   * 2. By calculating it from the filesystem.
   *
   * If we have to calculate, we write out the file; if we have
   * to touch the file, we persist in-memory.
   *
   * @return a promise that resolves to the profile's creation time.
   */
  get created() {
    // This can be an expensive operation so make sure we only do it once.
    if (this._created) {
      return this._created;
    }

    if (!this._times.created) {
      this._created = this.computeAndPersistCreated();
    } else {
      this._created = Promise.resolve(this._times.created);
    }

    return this._created;
  }

  /**
   * Returns a promise to the time of first use of the profile. This may be
   * undefined if the first use time is unknown.
   */
  get firstUse() {
    if ("firstUse" in this._times) {
      return Promise.resolve(this._times.firstUse);
    }
    return Promise.resolve(undefined);
  }

  /**
   * Return a promise representing the writing the current times to the profile.
   */
  async writeTimes() {
    try {
      await IOUtils.writeJSON(
        PathUtils.join(this.profilePath, FILE_TIMES),
        this._times
      );
    } catch (e) {
      if (
        !DOMException.isInstance(e) ||
        e.name !== "AbortError" ||
        e.message !== "IOUtils: Shutting down and refusing additional I/O tasks"
      ) {
        throw e;
      }
    }
  }

  /**
   * Calculates the created time by scanning the profile directory, sets it in
   * the current set of times and persists it to the profile. Returns a promise
   * that resolves when all of that is complete.
   */
  async computeAndPersistCreated() {
    let oldest = await getOldestProfileTimestamp(this.profilePath, this._log);
    this._times.created = oldest;
    await this.writeTimes();
    return oldest;
  }

  /**
   * Record (and persist) when a profile reset happened.  We just store a
   * single value - the timestamp of the most recent reset - but there is scope
   * to keep a list of reset times should our health-reporter successor
   * be able to make use of that.
   * Returns a promise that is resolved once the file has been written.
   */
  recordProfileReset(time = Date.now()) {
    this._times.reset = time;
    return this.writeTimes();
  }

  /* Returns a promise that resolves to the time the profile was reset,
   * or undefined if not recorded.
   */
  get reset() {
    if ("reset" in this._times) {
      return Promise.resolve(this._times.reset);
    }
    return Promise.resolve(undefined);
  }

  /**
   * Record (and persist) when a backup recovery happened.  We just store a
   * single value - the timestamp at the time of recovery.
   *
   * Returns a promise that is resolved once the file has been written.
   */
  recordRecoveredFromBackup(time = Date.now()) {
    this._times.recoveredFromBackup = time;
    return this.writeTimes();
  }

  /* Returns a promise that resolves to the time the profile was recovered from
   * a backup or undefined if not recorded.
   */
  get recoveredFromBackup() {
    if ("recoveredFromBackup" in this._times) {
      return Promise.resolve(this._times.recoveredFromBackup);
    }
    return Promise.resolve(undefined);
  }
}

// A Map from profile directory to a promise that resolves to the ProfileAgeImpl.
const PROFILES = new Map();

async function initProfileAge(profile) {
  let timesPath = PathUtils.join(profile, FILE_TIMES);

  try {
    let times = await IOUtils.readJSON(timesPath);
    return new ProfileAgeImpl(profile, times || {});
  } catch (e) {
    // Indicates that the file was missing or broken. In this case we want to
    // record the first use time as now. The constructor will set this and write
    // times.json
    return new ProfileAgeImpl(profile, { firstUse: null });
  }
}

/**
 * Returns a promise that resolves to an instance of ProfileAgeImpl. Will always
 * return the same instance for every call for the same profile.
 *
 * @param {string} profile The path to the profile directory.
 * @return {Promise<ProfileAgeImpl>} Resolves to the ProfileAgeImpl.
 */
export function ProfileAge(profile) {
  if (!profile) {
    profile = PathUtils.profileDir;
  }

  if (PROFILES.has(profile)) {
    return PROFILES.get(profile);
  }

  let promise = initProfileAge(profile);
  PROFILES.set(profile, promise);
  return promise;
}