2014-06-25 09:12:07 +04:00
|
|
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
|
2012-05-29 19:52:43 +04:00
|
|
|
* vim: 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/. */
|
2011-11-29 14:30:40 +04:00
|
|
|
|
2012-04-14 13:50:44 +04:00
|
|
|
"use strict";
|
|
|
|
|
2011-11-29 14:30:40 +04:00
|
|
|
/*
|
2012-04-14 13:50:44 +04:00
|
|
|
* 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.
|
2011-11-29 14:30:40 +04:00
|
|
|
*/
|
|
|
|
|
2015-02-15 00:29:49 +03:00
|
|
|
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
2016-10-18 18:03:06 +03:00
|
|
|
Cu.import("resource:///modules/MigrationUtils.jsm"); /* globals MigratorPrototype */
|
2015-02-15 00:29:49 +03:00
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
2013-04-17 10:08:11 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "PlacesBackups",
|
|
|
|
"resource://gre/modules/PlacesBackups.jsm");
|
2013-06-29 01:45:05 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "SessionMigration",
|
|
|
|
"resource:///modules/sessionstore/SessionMigration.jsm");
|
2014-10-23 08:00:22 +04:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "OS",
|
|
|
|
"resource://gre/modules/osfile.jsm");
|
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "FileUtils",
|
|
|
|
"resource://gre/modules/FileUtils.jsm");
|
2015-03-07 19:53:36 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "ProfileAge",
|
|
|
|
"resource://gre/modules/ProfileAge.jsm");
|
2016-05-19 13:01:03 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetter(this, "AppConstants",
|
|
|
|
"resource://gre/modules/AppConstants.jsm");
|
2012-04-14 13:50:44 +04:00
|
|
|
|
2014-10-23 08:00:22 +04:00
|
|
|
|
|
|
|
function FirefoxProfileMigrator() {
|
|
|
|
this.wrappedJSObject = this; // for testing...
|
|
|
|
}
|
2012-04-14 13:50:44 +04:00
|
|
|
|
|
|
|
FirefoxProfileMigrator.prototype = Object.create(MigratorPrototype);
|
|
|
|
|
2016-10-18 18:03:06 +03:00
|
|
|
FirefoxProfileMigrator.prototype._getAllProfiles = function() {
|
2015-02-15 00:29:49 +03:00
|
|
|
let allProfiles = new Map();
|
|
|
|
let profiles =
|
|
|
|
Components.classes["@mozilla.org/toolkit/profile-service;1"]
|
|
|
|
.getService(Components.interfaces.nsIToolkitProfileService)
|
|
|
|
.profiles;
|
|
|
|
while (profiles.hasMoreElements()) {
|
|
|
|
let profile = profiles.getNext().QueryInterface(Ci.nsIToolkitProfile);
|
|
|
|
let rootDir = profile.rootDir;
|
|
|
|
|
|
|
|
if (rootDir.exists() && rootDir.isReadable() &&
|
|
|
|
!rootDir.equals(MigrationUtils.profileStartup.directory)) {
|
|
|
|
allProfiles.set(profile.name, rootDir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return allProfiles;
|
|
|
|
};
|
|
|
|
|
|
|
|
function sorter(a, b) {
|
|
|
|
return a.id.toLocaleLowerCase().localeCompare(b.id.toLocaleLowerCase());
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.defineProperty(FirefoxProfileMigrator.prototype, "sourceProfiles", {
|
2016-12-30 02:34:54 +03:00
|
|
|
get() {
|
2016-01-14 22:15:22 +03:00
|
|
|
return [...this._getAllProfiles().keys()].map(x => ({id: x, name: x})).sort(sorter);
|
2015-02-15 00:29:49 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-06-29 01:45:05 +04:00
|
|
|
FirefoxProfileMigrator.prototype._getFileObject = function(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;
|
2015-02-15 00:29:49 +03:00
|
|
|
};
|
2013-06-29 01:45:05 +04:00
|
|
|
|
2015-02-15 00:29:49 +03:00
|
|
|
FirefoxProfileMigrator.prototype.getResources = function(aProfile) {
|
|
|
|
let sourceProfileDir = aProfile ? this._getAllProfiles().get(aProfile.id) :
|
2012-04-14 13:50:44 +04:00
|
|
|
Components.classes["@mozilla.org/toolkit/profile-service;1"]
|
|
|
|
.getService(Components.interfaces.nsIToolkitProfileService)
|
2015-02-15 00:29:49 +03:00
|
|
|
.selectedProfile.rootDir;
|
2012-04-14 13:50:44 +04:00
|
|
|
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;
|
|
|
|
|
2016-10-18 18:03:06 +03:00
|
|
|
return this._getResourcesInternal(sourceProfileDir, currentProfileDir);
|
2015-02-15 00:29:49 +03:00
|
|
|
};
|
2014-10-23 08:00:22 +04:00
|
|
|
|
2016-05-30 19:10:54 +03:00
|
|
|
FirefoxProfileMigrator.prototype.getLastUsedDate = function() {
|
|
|
|
// 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));
|
|
|
|
};
|
|
|
|
|
2016-10-18 18:03:06 +03:00
|
|
|
FirefoxProfileMigrator.prototype._getResourcesInternal = function(sourceProfileDir, currentProfileDir) {
|
2017-04-27 01:25:45 +03:00
|
|
|
let getFileResource = (aMigrationType, aFileNames) => {
|
2012-04-14 13:50:44 +04:00
|
|
|
let files = [];
|
|
|
|
for (let fileName of aFileNames) {
|
2013-06-29 01:45:05 +04:00
|
|
|
let file = this._getFileObject(sourceProfileDir, fileName);
|
2014-11-12 03:35:02 +03:00
|
|
|
if (file)
|
|
|
|
files.push(file);
|
|
|
|
}
|
|
|
|
if (!files.length) {
|
|
|
|
return null;
|
2012-04-14 13:50:44 +04:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
type: aMigrationType,
|
2016-12-30 02:34:54 +03:00
|
|
|
migrate(aCallback) {
|
2012-04-14 13:50:44 +04:00
|
|
|
for (let file of files) {
|
|
|
|
file.copyTo(currentProfileDir, "");
|
|
|
|
}
|
|
|
|
aCallback(true);
|
2011-11-29 14:30:40 +04:00
|
|
|
}
|
2012-04-14 13:50:44 +04:00
|
|
|
};
|
2017-04-27 01:25:45 +03:00
|
|
|
};
|
2012-04-14 13:50:44 +04:00
|
|
|
|
|
|
|
let types = MigrationUtils.resourceTypes;
|
|
|
|
let places = getFileResource(types.HISTORY, ["places.sqlite"]);
|
2017-03-30 14:08:05 +03:00
|
|
|
let favicons = getFileResource(types.HISTORY, ["favicons.sqlite"]);
|
2012-04-14 13:50:44 +04:00
|
|
|
let cookies = getFileResource(types.COOKIES, ["cookies.sqlite"]);
|
|
|
|
let passwords = getFileResource(types.PASSWORDS,
|
2017-01-03 18:07:50 +03:00
|
|
|
["signons.sqlite", "logins.json", "key3.db",
|
|
|
|
"signedInUser.json"]);
|
2012-04-14 13:50:44 +04:00
|
|
|
let formData = getFileResource(types.FORMDATA, ["formhistory.sqlite"]);
|
|
|
|
let bookmarksBackups = getFileResource(types.OTHERDATA,
|
2013-04-17 10:08:11 +04:00
|
|
|
[PlacesBackups.profileRelativeFolderPath]);
|
2013-04-09 01:17:18 +04:00
|
|
|
let dictionary = getFileResource(types.OTHERDATA, ["persdict.dat"]);
|
2012-04-14 13:50:44 +04:00
|
|
|
|
2014-08-12 00:41:27 +04:00
|
|
|
let sessionCheckpoints = this._getFileObject(sourceProfileDir, "sessionCheckpoints.json");
|
2013-06-29 01:45:05 +04:00
|
|
|
let sessionFile = this._getFileObject(sourceProfileDir, "sessionstore.js");
|
|
|
|
let session;
|
|
|
|
if (sessionFile) {
|
2016-05-05 23:38:49 +03:00
|
|
|
session = {
|
2013-06-29 01:45:05 +04:00
|
|
|
type: types.SESSION,
|
2016-12-30 02:34:54 +03:00
|
|
|
migrate(aCallback) {
|
2014-08-12 00:41:27 +04:00
|
|
|
sessionCheckpoints.copyTo(currentProfileDir, "sessionCheckpoints.json");
|
2013-06-29 01:45:05 +04:00
|
|
|
let newSessionFile = currentProfileDir.clone();
|
|
|
|
newSessionFile.append("sessionstore.js");
|
|
|
|
let migrationPromise = 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);
|
|
|
|
// It's too early in startup for the pref 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);
|
|
|
|
aCallback(true);
|
|
|
|
}, function() {
|
|
|
|
aCallback(false);
|
|
|
|
});
|
|
|
|
}
|
2016-05-05 23:38:49 +03:00
|
|
|
};
|
2013-06-29 01:45:05 +04:00
|
|
|
}
|
|
|
|
|
2016-01-18 18:29:44 +03:00
|
|
|
// Telemetry related migrations.
|
2014-10-23 08:00:23 +04:00
|
|
|
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.
|
2015-03-07 19:53:36 +03:00
|
|
|
let timesAccessor = new ProfileAge(currentProfileDir.path);
|
2014-10-23 08:00:23 +04:00
|
|
|
timesAccessor.recordProfileReset().then(
|
|
|
|
() => aCallback(true),
|
|
|
|
() => aCallback(false)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2016-01-18 18:29:44 +03:00
|
|
|
let telemetry = {
|
|
|
|
name: "telemetry", // name is used only by tests...
|
2014-10-23 08:00:22 +04:00
|
|
|
type: types.OTHERDATA,
|
|
|
|
migrate: aCallback => {
|
2016-01-18 18:29:44 +03:00
|
|
|
let createSubDir = (name) => {
|
|
|
|
let dir = currentProfileDir.clone();
|
|
|
|
dir.append(name);
|
|
|
|
dir.create(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
|
|
|
|
return dir;
|
|
|
|
};
|
|
|
|
|
|
|
|
// If the 'datareporting' directory exists we migrate files from it.
|
|
|
|
let haveStateFile = false;
|
2016-10-18 18:03:06 +03:00
|
|
|
let dataReportingDir = this._getFileObject(sourceProfileDir, "datareporting");
|
|
|
|
if (dataReportingDir && dataReportingDir.isDirectory()) {
|
2016-01-18 18:29:44 +03:00
|
|
|
// Copy only specific files.
|
|
|
|
let toCopy = ["state.json", "session-state.json"];
|
|
|
|
|
|
|
|
let dest = createSubDir("datareporting");
|
2016-10-18 18:03:06 +03:00
|
|
|
let enumerator = dataReportingDir.directoryEntries;
|
2014-10-23 08:00:22 +04:00
|
|
|
while (enumerator.hasMoreElements()) {
|
2016-01-18 18:29:44 +03:00
|
|
|
let file = enumerator.getNext().QueryInterface(Ci.nsIFile);
|
|
|
|
if (file.isDirectory() || toCopy.indexOf(file.leafName) == -1) {
|
2014-10-23 08:00:22 +04:00
|
|
|
continue;
|
|
|
|
}
|
2016-01-18 18:29:44 +03:00
|
|
|
|
|
|
|
if (file.leafName == "state.json") {
|
|
|
|
haveStateFile = true;
|
|
|
|
}
|
2014-10-23 08:00:22 +04:00
|
|
|
file.copyTo(dest, "");
|
|
|
|
}
|
|
|
|
}
|
2016-01-18 18:29:44 +03:00
|
|
|
|
|
|
|
if (!haveStateFile) {
|
|
|
|
// Fall back to migrating the state file that contains the client id from healthreport/.
|
|
|
|
// We first moved the client id management from the FHR implementation to the datareporting
|
|
|
|
// service.
|
|
|
|
// Consequently, we try to migrate an existing FHR state file here as a fallback.
|
2016-10-18 18:03:06 +03:00
|
|
|
let healthReportDir = this._getFileObject(sourceProfileDir, "healthreport");
|
|
|
|
if (healthReportDir && healthReportDir.isDirectory()) {
|
|
|
|
let stateFile = this._getFileObject(healthReportDir, "state.json");
|
2016-01-18 18:29:44 +03:00
|
|
|
if (stateFile) {
|
|
|
|
let dest = createSubDir("healthreport");
|
|
|
|
stateFile.copyTo(dest, "");
|
|
|
|
}
|
2014-10-23 08:00:22 +04:00
|
|
|
}
|
|
|
|
}
|
2016-01-18 18:29:44 +03:00
|
|
|
|
2014-10-23 08:00:22 +04:00
|
|
|
aCallback(true);
|
|
|
|
}
|
2016-10-18 18:03:06 +03:00
|
|
|
};
|
2014-10-23 08:00:22 +04:00
|
|
|
|
2015-10-18 19:27:39 +03:00
|
|
|
return [places, cookies, passwords, formData, dictionary, bookmarksBackups,
|
2017-03-30 14:08:05 +03:00
|
|
|
session, times, telemetry, favicons].filter(r => r);
|
2015-02-15 00:29:49 +03:00
|
|
|
};
|
2011-11-29 14:30:40 +04:00
|
|
|
|
2012-04-14 13:50:44 +04:00
|
|
|
Object.defineProperty(FirefoxProfileMigrator.prototype, "startupOnlyMigrator", {
|
2015-09-23 12:36:21 +03:00
|
|
|
get: () => true
|
2012-04-14 13:50:44 +04:00
|
|
|
});
|
2011-11-29 14:30:40 +04:00
|
|
|
|
|
|
|
|
2012-04-14 13:50:44 +04:00
|
|
|
FirefoxProfileMigrator.prototype.classDescription = "Firefox Profile Migrator";
|
|
|
|
FirefoxProfileMigrator.prototype.contractID = "@mozilla.org/profile/migrator;1?app=browser&type=firefox";
|
|
|
|
FirefoxProfileMigrator.prototype.classID = Components.ID("{91185366-ba97-4438-acba-48deaca63386}");
|
2011-11-29 14:30:40 +04:00
|
|
|
|
2012-10-31 20:13:28 +04:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([FirefoxProfileMigrator]);
|