Bug 1225798 - part 2: move reading list import to its own bookmarks migrator source, r=MattN

This commit is contained in:
Gijs Kruitbosch 2015-11-18 11:56:02 +00:00
parent 3fc29d69cf
commit a408952fe5
2 changed files with 114 additions and 76 deletions

View File

@ -7,6 +7,7 @@ const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/AppConstants.jsm");
Cu.import("resource://gre/modules/Task.jsm");
Cu.import("resource:///modules/MigrationUtils.jsm");
Cu.import("resource:///modules/MSMigrationUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
@ -15,6 +16,7 @@ XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
const kEdgeRegistryRoot = "SOFTWARE\\Classes\\Local Settings\\Software\\" +
"Microsoft\\Windows\\CurrentVersion\\AppContainer\\Storage\\" +
"microsoft.microsoftedge_8wekyb3d8bbwe\\MicrosoftEdge";
const kEdgeReadingListPath = "AC\\MicrosoftEdge\\User\\Default\\DataStore\\Data\\";
function EdgeTypedURLMigrator() {
}
@ -79,6 +81,112 @@ EdgeTypedURLMigrator.prototype = {
},
}
function EdgeReadingListMigrator() {
}
EdgeReadingListMigrator.prototype = {
type: MigrationUtils.resourceTypes.BOOKMARKS,
get exists() {
return !!MSMigrationUtils.getEdgeLocalDataFolder();
},
migrate(callback) {
this._migrateReadingList(PlacesUtils.bookmarks.menuGuid).then(
() => callback(true),
ex => {
Cu.reportError(ex);
callback(false);
}
);
},
_migrateReadingList: Task.async(function*(parentGuid) {
let edgeDir = MSMigrationUtils.getEdgeLocalDataFolder();
if (!edgeDir) {
return;
}
this._readingListExtractor = Cc["@mozilla.org/profile/migrator/edgereadinglistextractor;1"].
createInstance(Ci.nsIEdgeReadingListExtractor);
edgeDir.appendRelativePath(kEdgeReadingListPath);
let errorProduced = null;
if (edgeDir.exists() && edgeDir.isReadable() && edgeDir.isDirectory()) {
let expectedDir = edgeDir.clone();
expectedDir.appendRelativePath("nouser1\\120712-0049");
if (expectedDir.exists() && expectedDir.isReadable() && expectedDir.isDirectory()) {
yield this._migrateReadingListDB(expectedDir, parentGuid).catch(ex => {
if (!errorProduced)
errorProduced = ex;
});
} else {
let getSubdirs = someDir => {
let subdirs = someDir.directoryEntries;
let rv = [];
while (subdirs.hasMoreElements()) {
let subdir = subdirs.getNext().QueryInterface(Ci.nsIFile);
if (subdir.isDirectory() && subdir.isReadable()) {
rv.push(subdir);
}
}
return rv;
};
let dirs = getSubdirs(edgeDir).map(getSubdirs);
for (let dir of dirs) {
yield this._migrateReadingListDB(dir, parentGuid).catch(ex => {
if (!errorProduced)
errorProduced = ex;
});
}
}
}
if (errorProduced) {
throw errorProduced;
}
}),
_migrateReadingListDB: Task.async(function*(dbFile, parentGuid) {
dbFile.appendRelativePath("DBStore\\spartan.edb");
if (!dbFile.exists() || !dbFile.isReadable() || !dbFile.isFile()) {
return;
}
let readingListItems;
try {
readingListItems = this._readingListExtractor.extract(dbFile.path);
} catch (ex) {
Cu.reportError("Failed to extract Edge reading list information from " +
"the database at " + dbFile.path + " due to the following error: " + ex);
// Deliberately make this fail so we expose failure in the UI:
throw ex;
return;
}
if (!readingListItems.length) {
return;
}
let destFolderGuid = yield this._ensureReadingListFolder(parentGuid);
for (let i = 0; i < readingListItems.length; i++) {
let readingListItem = readingListItems.queryElementAt(i, Ci.nsIPropertyBag2);
let url = readingListItem.get("uri");
let title = readingListItem.get("title");
let time = readingListItem.get("time");
// time is a PRTime, which is microseconds (since unix epoch), or null.
// We need milliseconds for the date constructor, so divide by 1000:
let dateAdded = time ? new Date(time / 1000) : new Date();
yield PlacesUtils.bookmarks.insert({
parentGuid: destFolderGuid, url: url, title, dateAdded
});
}
}),
_ensureReadingListFolder: Task.async(function*(parentGuid) {
if (!this.__readingListFolderGuid) {
let folderTitle = MigrationUtils.getLocalizedString("importedEdgeReadingList");
let folderSpec = {type: PlacesUtils.bookmarks.TYPE_FOLDER, parentGuid, title: folderTitle};
this.__readingListFolderGuid = (yield PlacesUtils.bookmarks.insert(folderSpec)).guid;
}
return this.__readingListFolderGuid;
}),
};
function EdgeProfileMigrator() {
}
@ -89,6 +197,7 @@ EdgeProfileMigrator.prototype.getResources = function() {
MSMigrationUtils.getBookmarksMigrator(MSMigrationUtils.MIGRATION_TYPE_EDGE),
MSMigrationUtils.getCookiesMigrator(MSMigrationUtils.MIGRATION_TYPE_EDGE),
new EdgeTypedURLMigrator(),
new EdgeReadingListMigrator(),
];
let windowsVaultFormPasswordsMigrator =
MSMigrationUtils.getWindowsVaultFormPasswordsMigrator();

View File

@ -26,7 +26,6 @@ XPCOMUtils.defineLazyModuleGetter(this, "ctypes",
const EDGE_COOKIE_PATH_OPTIONS = ["", "#!001\\", "#!002\\"];
const EDGE_COOKIES_SUFFIX = "MicrosoftEdge\\Cookies";
const EDGE_FAVORITES = "AC\\MicrosoftEdge\\User\\Default\\Favorites";
const EDGE_READINGLIST = "AC\\MicrosoftEdge\\User\\Default\\DataStore\\Data\\";
const FREE_CLOSE_FAILED = 0;
const INTERNET_EXPLORER_EDGE_GUID = [0x3CCD5499,
0x4B1087A8,
@ -376,10 +375,6 @@ Bookmarks.prototype = {
yield MigrationUtils.createImportedBookmarksFolder(this.importedAppLabel, folderGuid);
}
yield this._migrateFolder(this._favoritesFolder, folderGuid);
if (this._migrationType == MSMigrationUtils.MIGRATION_TYPE_EDGE) {
yield this._migrateEdgeReadingList(PlacesUtils.bookmarks.menuGuid);
}
}.bind(this)).then(() => aCallback(true),
e => { Cu.reportError(e); aCallback(false) });
},
@ -390,6 +385,7 @@ Bookmarks.prototype = {
// for IE, and in a similar location for Edge.
// Until we support it, bookmarks are imported in alphabetical order.
let entries = aSourceFolder.directoryEntries;
let succeeded = true;
while (entries.hasMoreElements()) {
let entry = entries.getNext().QueryInterface(Ci.nsIFile);
try {
@ -439,82 +435,14 @@ Bookmarks.prototype = {
}
} catch (ex) {
Components.utils.reportError("Unable to import " + this.importedAppLabel + " favorite (" + entry.leafName + "): " + ex);
succeeded = false;
}
}
}),
_migrateEdgeReadingList: Task.async(function*(parentGuid) {
let edgeDir = getEdgeLocalDataFolder();
if (!edgeDir) {
return;
}
this._readingListExtractor = Cc["@mozilla.org/profile/migrator/edgereadinglistextractor;1"].
createInstance(Ci.nsIEdgeReadingListExtractor);
edgeDir.appendRelativePath(EDGE_READINGLIST);
if (edgeDir.exists() && edgeDir.isReadable() && edgeDir.isDirectory()) {
let expectedDir = edgeDir.clone();
expectedDir.appendRelativePath("nouser1\\120712-0049");
if (expectedDir.exists() && expectedDir.isReadable() && expectedDir.isDirectory()) {
yield this._migrateEdgeReadingListDB(expectedDir, parentGuid);
} else {
let getSubdirs = someDir => {
let subdirs = someDir.directoryEntries;
let rv = [];
while (subdirs.hasMoreElements()) {
let subdir = subdirs.getNext().QueryInterface(Ci.nsIFile);
if (subdir.isDirectory() && subdir.isReadable()) {
rv.push(subdir);
}
}
return rv;
};
let dirs = getSubdirs(edgeDir).map(getSubdirs);
for (let dir of dirs) {
yield this._migrateEdgeReadingListDB(dir, parentGuid);
}
}
}
}),
_migrateEdgeReadingListDB: Task.async(function*(dbFile, parentGuid) {
dbFile.appendRelativePath("DBStore\\spartan.edb");
if (!dbFile.exists() || !dbFile.isReadable() || !dbFile.isFile()) {
return;
}
let readingListItems;
try {
readingListItems = this._readingListExtractor.extract(dbFile.path);
} catch (ex) {
Cu.reportError("Failed to extract Edge reading list information from " +
"the database at " + dbPath + " due to the following error: " + ex);
return;
}
if (!readingListItems.length) {
return;
}
let destFolderGuid = yield this._ensureEdgeReadingListFolder(parentGuid);
for (let i = 0; i < readingListItems.length; i++) {
let readingListItem = readingListItems.queryElementAt(i, Ci.nsIPropertyBag2);
let url = readingListItem.get("uri");
let title = readingListItem.get("title");
let time = readingListItem.get("time");
// time is a PRTime, which is microseconds (since unix epoch), or null.
// We need milliseconds for the date constructor, so divide by 1000:
let dateAdded = time ? new Date(time / 1000) : new Date();
yield PlacesUtils.bookmarks.insert({
parentGuid: destFolderGuid, url: url, title, dateAdded
});
if (!succeeded) {
throw new Error("Failed to import all bookmarks correctly.");
}
}),
_ensureEdgeReadingListFolder: Task.async(function*(parentGuid) {
if (!this.__edgeReadingListFolderGuid) {
let folderTitle = MigrationUtils.getLocalizedString("importedEdgeReadingList");
let folderSpec = {type: PlacesUtils.bookmarks.TYPE_FOLDER, parentGuid, title: folderTitle};
this.__edgeReadingListFolderGuid = (yield PlacesUtils.bookmarks.insert(folderSpec)).guid;
}
return this.__edgeReadingListFolderGuid;
}),
};
function Cookies(migrationType) {
@ -948,4 +876,5 @@ var MSMigrationUtils = {
return new WindowsVaultFormPasswords();
},
getTypedURLs,
getEdgeLocalDataFolder,
};