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
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
* 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/. */
"use strict";
/*
* 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.
*/
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource:///modules/MigrationUtils.jsm"); /* globals MigratorPrototype */
Cu.import("resource://gre/modules/Services.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "PlacesBackups",
"resource://gre/modules/PlacesBackups.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "SessionMigration",
"resource:///modules/sessionstore/SessionMigration.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "OS",
"resource://gre/modules/osfile.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "FileUtils",
"resource://gre/modules/FileUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "ProfileAge",
"resource://gre/modules/ProfileAge.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "AppConstants",
"resource://gre/modules/AppConstants.jsm");
function FirefoxProfileMigrator() {
this.wrappedJSObject = this; // for testing...
}
FirefoxProfileMigrator.prototype = Object.create(MigratorPrototype);
FirefoxProfileMigrator.prototype._getAllProfiles = function() {
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", {
get: function() {
return [...this._getAllProfiles().keys()].map(x => ({id: x, name: x})).sort(sorter);
}
});
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;
};
FirefoxProfileMigrator.prototype.getResources = function(aProfile) {
let sourceProfileDir = aProfile ? this._getAllProfiles().get(aProfile.id) :
Components.classes["@mozilla.org/toolkit/profile-service;1"]
.getService(Components.interfaces.nsIToolkitProfileService)
.selectedProfile.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);
};
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));
};
FirefoxProfileMigrator.prototype._getResourcesInternal = function(sourceProfileDir, currentProfileDir) {
let getFileResource = function(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: function(aCallback) {
for (let file of files) {
file.copyTo(currentProfileDir, "");
}
aCallback(true);
}
};
}.bind(this);
let types = MigrationUtils.resourceTypes;
let places = getFileResource(types.HISTORY, ["places.sqlite", "places.sqlite-wal"]);
let cookies = getFileResource(types.COOKIES, ["cookies.sqlite", "cookies.sqlite-wal"]);
let passwords = getFileResource(types.PASSWORDS,
["signons.sqlite", "logins.json", "key3.db",
"signedInUser.json"]);
let formData = getFileResource(types.FORMDATA, ["formhistory.sqlite"]);
let bookmarksBackups = getFileResource(types.OTHERDATA,
[PlacesBackups.profileRelativeFolderPath]);
let dictionary = getFileResource(types.OTHERDATA, ["persdict.dat"]);
let sessionCheckpoints = this._getFileObject(sourceProfileDir, "sessionCheckpoints.json");
let sessionFile = this._getFileObject(sourceProfileDir, "sessionstore.js");
let session;
if (sessionFile) {
session = {
type: types.SESSION,
migrate: function(aCallback) {
sessionCheckpoints.copyTo(currentProfileDir, "sessionCheckpoints.json");
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);
});
}
};
}
// 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 timesAccessor = new ProfileAge(currentProfileDir.path);
timesAccessor.recordProfileReset().then(
() => aCallback(true),
() => aCallback(false)
);
}
};
let telemetry = {
name: "telemetry", // name is used only by tests...
type: types.OTHERDATA,
migrate: aCallback => {
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;
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.getNext().QueryInterface(Ci.nsIFile);
if (file.isDirectory() || toCopy.indexOf(file.leafName) == -1) {
continue;
}
if (file.leafName == "state.json") {
haveStateFile = true;
}
file.copyTo(dest, "");
}
}
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.
let healthReportDir = this._getFileObject(sourceProfileDir, "healthreport");
if (healthReportDir && healthReportDir.isDirectory()) {
let stateFile = this._getFileObject(healthReportDir, "state.json");
if (stateFile) {
let dest = createSubDir("healthreport");
stateFile.copyTo(dest, "");
}
}
}
aCallback(true);
}
};
return [places, cookies, passwords, formData, dictionary, bookmarksBackups,
session, times, telemetry].filter(r => r);
};
Object.defineProperty(FirefoxProfileMigrator.prototype, "startupOnlyMigrator", {
get: () => true
});
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}");
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([FirefoxProfileMigrator]);
|