summaryrefslogtreecommitdiffstats
path: root/browser/components/migration/FirefoxProfileMigrator.sys.mjs
blob: 2ce57c2c7b8080a0c6ab66647dec7eecded54ebc (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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sw=2 ts=2 sts=2 et */
/* 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/. */

/*
 * Migrates from a Firefox profile in a lossy manner in order to clean up a
 * user's profile.  Data is only migrated where the benefits outweigh the
 * potential problems caused by importing undesired/invalid configurations
 * from the source profile.
 */

import { MigrationUtils } from "resource:///modules/MigrationUtils.sys.mjs";

import { MigratorBase } from "resource:///modules/MigratorBase.sys.mjs";

const lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  FileUtils: "resource://gre/modules/FileUtils.sys.mjs",
  PlacesBackups: "resource://gre/modules/PlacesBackups.sys.mjs",
  ProfileAge: "resource://gre/modules/ProfileAge.sys.mjs",
  SessionMigration: "resource:///modules/sessionstore/SessionMigration.sys.mjs",
});

/**
 * Firefox profile migrator. Currently, this class only does "pave over"
 * migrations, where various parts of an old profile overwrite a new
 * profile. This is distinct from other migrators which attempt to import
 * old profile data into the existing profile.
 *
 * This migrator is what powers the "Profile Refresh" mechanism.
 */
export class FirefoxProfileMigrator extends MigratorBase {
  static get key() {
    return "firefox";
  }

  static get displayNameL10nID() {
    return "migration-wizard-migrator-display-name-firefox";
  }

  _getAllProfiles() {
    let allProfiles = new Map();
    let profileService = Cc[
      "@mozilla.org/toolkit/profile-service;1"
    ].getService(Ci.nsIToolkitProfileService);
    for (let profile of profileService.profiles) {
      let rootDir = profile.rootDir;

      if (
        rootDir.exists() &&
        rootDir.isReadable() &&
        !rootDir.equals(MigrationUtils.profileStartup.directory)
      ) {
        allProfiles.set(profile.name, rootDir);
      }
    }
    return allProfiles;
  }

  getSourceProfiles() {
    let sorter = (a, b) => {
      return a.id.toLocaleLowerCase().localeCompare(b.id.toLocaleLowerCase());
    };

    return [...this._getAllProfiles().keys()]
      .map(x => ({ id: x, name: x }))
      .sort(sorter);
  }

  _getFileObject(dir, fileName) {
    let file = dir.clone();
    file.append(fileName);

    // File resources are monolithic.  We don't make partial copies since
    // they are not expected to work alone. Return null to avoid trying to
    // copy non-existing files.
    return file.exists() ? file : null;
  }

  getResources(aProfile) {
    let sourceProfileDir = aProfile
      ? this._getAllProfiles().get(aProfile.id)
      : Cc["@mozilla.org/toolkit/profile-service;1"].getService(
          Ci.nsIToolkitProfileService
        ).defaultProfile.rootDir;
    if (
      !sourceProfileDir ||
      !sourceProfileDir.exists() ||
      !sourceProfileDir.isReadable()
    ) {
      return null;
    }

    // Being a startup-only migrator, we can rely on
    // MigrationUtils.profileStartup being set.
    let currentProfileDir = MigrationUtils.profileStartup.directory;

    // Surely data cannot be imported from the current profile.
    if (sourceProfileDir.equals(currentProfileDir)) {
      return null;
    }

    return this._getResourcesInternal(sourceProfileDir, currentProfileDir);
  }

  getLastUsedDate() {
    // We always pretend we're really old, so that we don't mess
    // up the determination of which browser is the most 'recent'
    // to import from.
    return Promise.resolve(new Date(0));
  }

  _getResourcesInternal(sourceProfileDir, currentProfileDir) {
    let getFileResource = (aMigrationType, aFileNames) => {
      let files = [];
      for (let fileName of aFileNames) {
        let file = this._getFileObject(sourceProfileDir, fileName);
        if (file) {
          files.push(file);
        }
      }
      if (!files.length) {
        return null;
      }
      return {
        type: aMigrationType,
        migrate(aCallback) {
          for (let file of files) {
            file.copyTo(currentProfileDir, "");
          }
          aCallback(true);
        },
      };
    };

    let _oldRawPrefsMemoized = null;
    async function readOldPrefs() {
      if (!_oldRawPrefsMemoized) {
        let prefsPath = PathUtils.join(sourceProfileDir.path, "prefs.js");
        if (await IOUtils.exists(prefsPath)) {
          _oldRawPrefsMemoized = await IOUtils.readUTF8(prefsPath, {
            encoding: "utf-8",
          });
        }
      }

      return _oldRawPrefsMemoized;
    }

    function savePrefs() {
      // If we've used the pref service to write prefs for the new profile, it's too
      // early in startup for the service to have a profile directory, so we have to
      // manually tell it where to save the prefs file.
      let newPrefsFile = currentProfileDir.clone();
      newPrefsFile.append("prefs.js");
      Services.prefs.savePrefFile(newPrefsFile);
    }

    let types = MigrationUtils.resourceTypes;
    let places = getFileResource(types.HISTORY, [
      "places.sqlite",
      "places.sqlite-wal",
    ]);
    let favicons = getFileResource(types.HISTORY, [
      "favicons.sqlite",
      "favicons.sqlite-wal",
    ]);
    let cookies = getFileResource(types.COOKIES, [
      "cookies.sqlite",
      "cookies.sqlite-wal",
    ]);
    let passwords = getFileResource(types.PASSWORDS, [
      "signons.sqlite",
      "logins.json",
      "key3.db",
      "key4.db",
    ]);
    let formData = getFileResource(types.FORMDATA, [
      "formhistory.sqlite",
      "autofill-profiles.json",
    ]);
    let bookmarksBackups = getFileResource(types.OTHERDATA, [
      lazy.PlacesBackups.profileRelativeFolderPath,
    ]);
    let dictionary = getFileResource(types.OTHERDATA, ["persdict.dat"]);

    let session;
    if (Services.env.get("MOZ_RESET_PROFILE_MIGRATE_SESSION")) {
      // We only want to restore the previous firefox session if the profile refresh was
      // triggered by user. The MOZ_RESET_PROFILE_MIGRATE_SESSION would be set when a user-triggered
      // profile refresh happened in nsAppRunner.cpp. Hence, we detect the MOZ_RESET_PROFILE_MIGRATE_SESSION
      // to see if session data migration is required.
      Services.env.set("MOZ_RESET_PROFILE_MIGRATE_SESSION", "");
      let sessionCheckpoints = this._getFileObject(
        sourceProfileDir,
        "sessionCheckpoints.json"
      );
      let sessionFile = this._getFileObject(
        sourceProfileDir,
        "sessionstore.jsonlz4"
      );
      if (sessionFile) {
        session = {
          type: types.SESSION,
          migrate(aCallback) {
            sessionCheckpoints.copyTo(
              currentProfileDir,
              "sessionCheckpoints.json"
            );
            let newSessionFile = currentProfileDir.clone();
            newSessionFile.append("sessionstore.jsonlz4");
            let migrationPromise = lazy.SessionMigration.migrate(
              sessionFile.path,
              newSessionFile.path
            );
            migrationPromise.then(
              function () {
                let buildID = Services.appinfo.platformBuildID;
                let mstone = Services.appinfo.platformVersion;
                // Force the browser to one-off resume the session that we give it:
                Services.prefs.setBoolPref(
                  "browser.sessionstore.resume_session_once",
                  true
                );
                // Reset the homepage_override prefs so that the browser doesn't override our
                // session with the "what's new" page:
                Services.prefs.setCharPref(
                  "browser.startup.homepage_override.mstone",
                  mstone
                );
                Services.prefs.setCharPref(
                  "browser.startup.homepage_override.buildID",
                  buildID
                );
                savePrefs();
                aCallback(true);
              },
              function () {
                aCallback(false);
              }
            );
          },
        };
      }
    }

    // Sync/FxA related data
    let sync = {
      name: "sync", // name is used only by tests.
      type: types.OTHERDATA,
      migrate: async aCallback => {
        // Try and parse a signedInUser.json file from the source directory and
        // if we can, copy it to the new profile and set sync's username pref
        // (which acts as a de-facto flag to indicate if sync is configured)
        try {
          let oldPath = PathUtils.join(
            sourceProfileDir.path,
            "signedInUser.json"
          );
          let exists = await IOUtils.exists(oldPath);
          if (exists) {
            let data = await IOUtils.readJSON(oldPath);
            if (data && data.accountData && data.accountData.email) {
              let username = data.accountData.email;
              // copy the file itself.
              await IOUtils.copy(
                oldPath,
                PathUtils.join(currentProfileDir.path, "signedInUser.json")
              );
              // Now we need to know whether Sync is actually configured for this
              // user. The only way we know is by looking at the prefs file from
              // the old profile. We avoid trying to do a full parse of the prefs
              // file and even avoid parsing the single string value we care
              // about.
              let oldRawPrefs = await readOldPrefs();
              if (/^user_pref\("services\.sync\.username"/m.test(oldRawPrefs)) {
                // sync's configured in the source profile - ensure it is in the
                // new profile too.
                // Write it to prefs.js and flush the file.
                Services.prefs.setStringPref(
                  "services.sync.username",
                  username
                );
                savePrefs();
              }
            }
          }
        } catch (ex) {
          aCallback(false);
          return;
        }
        aCallback(true);
      },
    };

    // Telemetry related migrations.
    let times = {
      name: "times", // name is used only by tests.
      type: types.OTHERDATA,
      migrate: aCallback => {
        let file = this._getFileObject(sourceProfileDir, "times.json");
        if (file) {
          file.copyTo(currentProfileDir, "");
        }
        // And record the fact a migration (ie, a reset) happened.
        let recordMigration = async () => {
          try {
            let profileTimes = await lazy.ProfileAge(currentProfileDir.path);
            await profileTimes.recordProfileReset();
            aCallback(true);
          } catch (e) {
            aCallback(false);
          }
        };

        recordMigration();
      },
    };
    let telemetry = {
      name: "telemetry", // name is used only by tests...
      type: types.OTHERDATA,
      migrate: async aCallback => {
        let createSubDir = name => {
          let dir = currentProfileDir.clone();
          dir.append(name);
          dir.create(Ci.nsIFile.DIRECTORY_TYPE, lazy.FileUtils.PERMS_DIRECTORY);
          return dir;
        };

        // If the 'datareporting' directory exists we migrate files from it.
        let dataReportingDir = this._getFileObject(
          sourceProfileDir,
          "datareporting"
        );
        if (dataReportingDir && dataReportingDir.isDirectory()) {
          // Copy only specific files.
          let toCopy = ["state.json", "session-state.json"];

          let dest = createSubDir("datareporting");
          let enumerator = dataReportingDir.directoryEntries;
          while (enumerator.hasMoreElements()) {
            let file = enumerator.nextFile;
            if (file.isDirectory() || !toCopy.includes(file.leafName)) {
              continue;
            }
            file.copyTo(dest, "");
          }
        }

        try {
          let oldRawPrefs = await readOldPrefs();
          let writePrefs = false;
          const PREFS = ["bookmarks", "history", "passwords"];

          for (let pref of PREFS) {
            let fullPref = `browser\.migrate\.interactions\.${pref}`;
            let regex = new RegExp('^user_pref\\("' + fullPref, "m");
            if (regex.test(oldRawPrefs)) {
              Services.prefs.setBoolPref(fullPref, true);
              writePrefs = true;
            }
          }

          if (writePrefs) {
            savePrefs();
          }
        } catch (e) {
          aCallback(false);
          return;
        }

        aCallback(true);
      },
    };

    return [
      places,
      cookies,
      passwords,
      formData,
      dictionary,
      bookmarksBackups,
      session,
      sync,
      times,
      telemetry,
      favicons,
    ].filter(r => r);
  }

  get startupOnlyMigrator() {
    return true;
  }
}