1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
2 /* vim: set sw=2 ts=2 sts=2 et */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 * Migrates from a Firefox profile in a lossy manner in order to clean up a
9 * user's profile. Data is only migrated where the benefits outweigh the
10 * potential problems caused by importing undesired/invalid configurations
11 * from the source profile.
14 import { MigrationUtils } from "resource:///modules/MigrationUtils.sys.mjs";
16 import { MigratorBase } from "resource:///modules/MigratorBase.sys.mjs";
20 ChromeUtils.defineESModuleGetters(lazy, {
21 FileUtils: "resource://gre/modules/FileUtils.sys.mjs",
22 PlacesBackups: "resource://gre/modules/PlacesBackups.sys.mjs",
23 ProfileAge: "resource://gre/modules/ProfileAge.sys.mjs",
24 SessionMigration: "resource:///modules/sessionstore/SessionMigration.sys.mjs",
28 * Firefox profile migrator. Currently, this class only does "pave over"
29 * migrations, where various parts of an old profile overwrite a new
30 * profile. This is distinct from other migrators which attempt to import
31 * old profile data into the existing profile.
33 * This migrator is what powers the "Profile Refresh" mechanism.
35 export class FirefoxProfileMigrator extends MigratorBase {
40 static get displayNameL10nID() {
41 return "migration-wizard-migrator-display-name-firefox";
44 static get brandImage() {
45 return "chrome://branding/content/icon128.png";
49 let allProfiles = new Map();
50 let profileService = Cc[
51 "@mozilla.org/toolkit/profile-service;1"
52 ].getService(Ci.nsIToolkitProfileService);
53 for (let profile of profileService.profiles) {
54 let rootDir = profile.rootDir;
58 rootDir.isReadable() &&
59 !rootDir.equals(MigrationUtils.profileStartup.directory)
61 allProfiles.set(profile.name, rootDir);
68 let sorter = (a, b) => {
69 return a.id.toLocaleLowerCase().localeCompare(b.id.toLocaleLowerCase());
72 return [...this._getAllProfiles().keys()]
73 .map(x => ({ id: x, name: x }))
77 _getFileObject(dir, fileName) {
78 let file = dir.clone();
79 file.append(fileName);
81 // File resources are monolithic. We don't make partial copies since
82 // they are not expected to work alone. Return null to avoid trying to
83 // copy non-existing files.
84 return file.exists() ? file : null;
87 getResources(aProfile) {
88 let sourceProfileDir = aProfile
89 ? this._getAllProfiles().get(aProfile.id)
90 : Cc["@mozilla.org/toolkit/profile-service;1"].getService(
91 Ci.nsIToolkitProfileService
92 ).defaultProfile.rootDir;
95 !sourceProfileDir.exists() ||
96 !sourceProfileDir.isReadable()
101 // Being a startup-only migrator, we can rely on
102 // MigrationUtils.profileStartup being set.
103 let currentProfileDir = MigrationUtils.profileStartup.directory;
105 // Surely data cannot be imported from the current profile.
106 if (sourceProfileDir.equals(currentProfileDir)) {
110 return this._getResourcesInternal(sourceProfileDir, currentProfileDir);
114 // We always pretend we're really old, so that we don't mess
115 // up the determination of which browser is the most 'recent'
117 return Promise.resolve(new Date(0));
120 _getResourcesInternal(sourceProfileDir, currentProfileDir) {
121 let getFileResource = (aMigrationType, aFileNames) => {
123 for (let fileName of aFileNames) {
124 let file = this._getFileObject(sourceProfileDir, fileName);
133 type: aMigrationType,
135 for (let file of files) {
136 file.copyTo(currentProfileDir, "");
143 let _oldRawPrefsMemoized = null;
144 async function readOldPrefs() {
145 if (!_oldRawPrefsMemoized) {
146 let prefsPath = PathUtils.join(sourceProfileDir.path, "prefs.js");
147 if (await IOUtils.exists(prefsPath)) {
148 _oldRawPrefsMemoized = await IOUtils.readUTF8(prefsPath, {
154 return _oldRawPrefsMemoized;
157 function savePrefs() {
158 // If we've used the pref service to write prefs for the new profile, it's too
159 // early in startup for the service to have a profile directory, so we have to
160 // manually tell it where to save the prefs file.
161 let newPrefsFile = currentProfileDir.clone();
162 newPrefsFile.append("prefs.js");
163 Services.prefs.savePrefFile(newPrefsFile);
166 function configureHomepage(resetSession) {
167 // We just refreshed the profile, so don't show the profile reset prompt
169 Services.prefs.setBoolPref("browser.disableResetPrompt", true);
171 // We're resetting the user's session, not creating a new one. Set the
172 // homepage_override prefs so that the browser doesn't override our
173 // session with an unwanted homepage.
174 let buildID = Services.appinfo.platformBuildID;
175 let mstone = Services.appinfo.platformVersion;
176 Services.prefs.setCharPref(
177 "browser.startup.homepage_override.mstone",
180 Services.prefs.setCharPref(
181 "browser.startup.homepage_override.buildID",
187 let types = MigrationUtils.resourceTypes;
188 let places = getFileResource(types.HISTORY, [
192 let favicons = getFileResource(types.HISTORY, [
194 "favicons.sqlite-wal",
196 let cookies = getFileResource(types.COOKIES, [
198 "cookies.sqlite-wal",
200 let passwords = getFileResource(types.PASSWORDS, [
205 let formData = getFileResource(types.FORMDATA, [
206 "formhistory.sqlite",
207 "autofill-profiles.json",
209 let bookmarksBackups = getFileResource(types.OTHERDATA, [
210 lazy.PlacesBackups.profileRelativeFolderPath,
212 let dictionary = getFileResource(types.OTHERDATA, ["persdict.dat"]);
214 // Determine if we want to restore the previous session or start a new one
215 const NEW_SESSION = "0";
216 const RESTORE_SESSION = "1";
217 let resetSession = Services.env.get("MOZ_RESET_PROFILE_SESSION");
218 Services.env.set("MOZ_RESET_PROFILE_SESSION", "");
221 if (resetSession === RESTORE_SESSION) {
222 // We only want to restore the previous firefox session if the profile
223 // refresh was triggered by the user, such as through about:support. In
224 // these cases, MOZ_RESET_PROFILE_SESSION is set to restore, signaling
225 // that session data migration is required.
226 let sessionCheckpoints = this._getFileObject(
228 "sessionCheckpoints.json"
230 let sessionFile = this._getFileObject(
232 "sessionstore.jsonlz4"
238 sessionCheckpoints.copyTo(
240 "sessionCheckpoints.json"
242 let newSessionFile = currentProfileDir.clone();
243 newSessionFile.append("sessionstore.jsonlz4");
244 let migrationPromise = lazy.SessionMigration.migrate(
248 migrationPromise.then(
250 // Force the browser to one-off resume the session that we give it:
251 Services.prefs.setBoolPref(
252 "browser.sessionstore.resume_session_once",
255 configureHomepage(true);
266 } else if (resetSession === NEW_SESSION) {
267 // If this is first startup and the profile refresh was triggered via the
268 // command line, such as through the stub installer, we do not restore the
274 // Sync/FxA related data
276 name: "sync", // name is used only by tests.
277 type: types.OTHERDATA,
278 migrate: async aCallback => {
279 // Try and parse a signedInUser.json file from the source directory and
280 // if we can, copy it to the new profile and set sync's username pref
281 // (which acts as a de-facto flag to indicate if sync is configured)
283 let oldPath = PathUtils.join(
284 sourceProfileDir.path,
287 let exists = await IOUtils.exists(oldPath);
289 let data = await IOUtils.readJSON(oldPath);
290 if (data && data.accountData && data.accountData.email) {
291 let username = data.accountData.email;
292 // copy the file itself.
295 PathUtils.join(currentProfileDir.path, "signedInUser.json")
297 // Now we need to know whether Sync is actually configured for this
298 // user. The only way we know is by looking at the prefs file from
299 // the old profile. We avoid trying to do a full parse of the prefs
300 // file and even avoid parsing the single string value we care
302 let oldRawPrefs = await readOldPrefs();
303 if (/^user_pref\("services\.sync\.username"/m.test(oldRawPrefs)) {
304 // sync's configured in the source profile - ensure it is in the
306 // Write it to prefs.js and flush the file.
307 Services.prefs.setStringPref(
308 "services.sync.username",
323 // Telemetry related migrations.
325 name: "times", // name is used only by tests.
326 type: types.OTHERDATA,
327 migrate: aCallback => {
328 let file = this._getFileObject(sourceProfileDir, "times.json");
330 file.copyTo(currentProfileDir, "");
332 // And record the fact a migration (ie, a reset) happened.
333 let recordMigration = async () => {
335 let profileTimes = await lazy.ProfileAge(currentProfileDir.path);
336 await profileTimes.recordProfileReset();
347 name: "telemetry", // name is used only by tests...
348 type: types.OTHERDATA,
349 migrate: async aCallback => {
350 let createSubDir = name => {
351 let dir = currentProfileDir.clone();
353 dir.create(Ci.nsIFile.DIRECTORY_TYPE, lazy.FileUtils.PERMS_DIRECTORY);
357 // If the 'datareporting' directory exists we migrate files from it.
358 let dataReportingDir = this._getFileObject(
362 if (dataReportingDir && dataReportingDir.isDirectory()) {
363 // Copy only specific files.
364 let toCopy = ["state.json", "session-state.json"];
366 let dest = createSubDir("datareporting");
367 let enumerator = dataReportingDir.directoryEntries;
368 while (enumerator.hasMoreElements()) {
369 let file = enumerator.nextFile;
370 if (file.isDirectory() || !toCopy.includes(file.leafName)) {
373 file.copyTo(dest, "");
378 let oldRawPrefs = await readOldPrefs();
379 let writePrefs = false;
380 const PREFS = ["bookmarks", "csvpasswords", "history", "passwords"];
382 for (let pref of PREFS) {
383 let fullPref = `browser\.migrate\.interactions\.${pref}`;
384 let regex = new RegExp('^user_pref\\("' + fullPref, "m");
385 if (regex.test(oldRawPrefs)) {
386 Services.prefs.setBoolPref(fullPref, true);
418 get startupOnlyMigrator() {