Bug 1094876 - Migrators should use new Bookmarks.jsm API. r=ttaubert

This commit is contained in:
Marco Bonardo 2015-04-20 16:32:49 +02:00
parent bcc3cc9eaa
commit 5040c74332
4 changed files with 143 additions and 150 deletions

View File

@ -20,6 +20,7 @@ Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/NetUtil.jsm");
Cu.import("resource://gre/modules/FileUtils.jsm");
Cu.import("resource://gre/modules/Task.jsm");
Cu.import("resource:///modules/MigrationUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
@ -42,29 +43,24 @@ function chromeTimeToDate(aTime)
/**
* Insert bookmark items into specific folder.
*
* @param aFolderId
* id of folder where items will be inserted
* @param aItems
* @param parentGuid
* GUID of the folder where items will be inserted
* @param items
* bookmark items to be inserted
*/
function insertBookmarkItems(aFolderId, aItems)
{
for (let i = 0; i < aItems.length; i++) {
let item = aItems[i];
function* insertBookmarkItems(parentGuid, items) {
for (let item of items) {
try {
if (item.type == "url") {
PlacesUtils.bookmarks.insertBookmark(aFolderId,
NetUtil.newURI(item.url),
PlacesUtils.bookmarks.DEFAULT_INDEX,
item.name);
yield PlacesUtils.bookmarks.insert({
parentGuid, url: item.url, title: item.name
});
} else if (item.type == "folder") {
let newFolderId =
PlacesUtils.bookmarks.createFolder(aFolderId,
item.name,
PlacesUtils.bookmarks.DEFAULT_INDEX);
let newFolderGuid = (yield PlacesUtils.bookmarks.insert({
parentGuid, type: PlacesUtils.bookmarks.TYPE_FOLDER, title: item.name
})).guid;
insertBookmarkItems(newFolderId, item.children);
yield insertBookmarkItems(newFolderGuid, item.children);
}
} catch (e) {
Cu.reportError(e);
@ -189,48 +185,51 @@ function GetBookmarksResource(aProfileFolder) {
type: MigrationUtils.resourceTypes.BOOKMARKS,
migrate: function(aCallback) {
NetUtil.asyncFetch2(bookmarksFile, MigrationUtils.wrapMigrateFunction(
function(aInputStream, aResultCode) {
if (!Components.isSuccessCode(aResultCode))
throw new Error("Could not read Bookmarks file");
// Parse Chrome bookmark file that is JSON format
let bookmarkJSON = NetUtil.readInputStreamToString(
aInputStream, aInputStream.available(), { charset : "UTF-8" });
let roots = JSON.parse(bookmarkJSON).roots;
PlacesUtils.bookmarks.runInBatchMode({
runBatched: function() {
// Importing bookmark bar items
if (roots.bookmark_bar.children &&
roots.bookmark_bar.children.length > 0) {
// Toolbar
let parentId = PlacesUtils.toolbarFolderId;
if (!MigrationUtils.isStartupMigration) {
parentId = MigrationUtils.createImportedBookmarksFolder(
"Chrome", parentId);
}
insertBookmarkItems(parentId, roots.bookmark_bar.children);
}
return Task.spawn(function* () {
let jsonStream = yield new Promise(resolve =>
NetUtil.asyncFetch({ uri: NetUtil.newURI(bookmarksFile),
loadUsingSystemPrincipal: true
},
(inputStream, resultCode) => {
if (Components.isSuccessCode(resultCode)) {
resolve(inputStream);
} else {
reject(new Error("Could not read Bookmarks file"));
}
}
)
);
// Importing bookmark menu items
if (roots.other.children &&
roots.other.children.length > 0) {
// Bookmark menu
let parentId = PlacesUtils.bookmarksMenuFolderId;
if (!MigrationUtils.isStartupMigration) {
parentId = MigrationUtils.createImportedBookmarksFolder(
"Chrome", parentId);
}
insertBookmarkItems(parentId, roots.other.children);
}
}
}, null);
}, aCallback),
null, // aLoadingNode
Services.scriptSecurityManager.getSystemPrincipal(),
null, // aTriggeringPrincipal
Ci.nsILoadInfo.SEC_NORMAL,
Ci.nsIContentPolicy.TYPE_OTHER);
// Parse Chrome bookmark file that is JSON format
let bookmarkJSON = NetUtil.readInputStreamToString(
jsonStream, jsonStream.available(), { charset : "UTF-8" });
let roots = JSON.parse(bookmarkJSON).roots;
// Importing bookmark bar items
if (roots.bookmark_bar.children &&
roots.bookmark_bar.children.length > 0) {
// Toolbar
let parentGuid = PlacesUtils.bookmarks.toolbarGuid;
if (!MigrationUtils.isStartupMigration) {
parentGuid =
yield MigrationUtils.createImportedBookmarksFolder("Chrome", parentGuid);
}
yield insertBookmarkItems(parentGuid, roots.bookmark_bar.children);
}
// Importing bookmark menu items
if (roots.other.children &&
roots.other.children.length > 0) {
// Bookmark menu
let parentGuid = PlacesUtils.bookmarks.menuGuid;
if (!MigrationUtils.isStartupMigration) {
parentGuid =
yield MigrationUtils.createImportedBookmarksFolder("Chrome", parentGuid);
}
yield insertBookmarkItems(parentGuid, roots.other.children);
}
}.bind(this)).then(() => aCallback(true),
e => { Cu.reportError(e); aCallback(false) });
}
};
}

View File

@ -13,7 +13,7 @@ const kMainKey = "Software\\Microsoft\\Internet Explorer\\Main";
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/NetUtil.jsm");
Cu.import("resource://gre/modules/Task.jsm");
Cu.import("resource:///modules/MigrationUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
@ -169,23 +169,19 @@ Bookmarks.prototype = {
},
migrate: function B_migrate(aCallback) {
PlacesUtils.bookmarks.runInBatchMode({
runBatched: (function migrateBatched() {
// Import to the bookmarks menu.
let destFolderId = PlacesUtils.bookmarksMenuFolderId;
if (!MigrationUtils.isStartupMigration) {
destFolderId =
MigrationUtils.createImportedBookmarksFolder("IE", destFolderId);
}
this._migrateFolder(this._favoritesFolder, destFolderId);
aCallback(true);
}).bind(this)
}, null);
return Task.spawn(function* () {
// Import to the bookmarks menu.
let folderGuid = PlacesUtils.bookmarks.menuGuid;
if (!MigrationUtils.isStartupMigration) {
folderGuid =
yield MigrationUtils.createImportedBookmarksFolder("IE", folderGuid);
}
yield this._migrateFolder(this._favoritesFolder, folderGuid);
}.bind(this)).then(() => aCallback(true),
e => { Cu.reportError(e); aCallback(false) });
},
_migrateFolder: function B__migrateFolder(aSourceFolder, aDestFolderId) {
_migrateFolder: Task.async(function* (aSourceFolder, aDestFolderGuid) {
// TODO (bug 741993): the favorites order is stored in the Registry, at
// HCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MenuOrder\Favorites
// Until we support it, bookmarks are imported in alphabetical order.
@ -198,26 +194,28 @@ Bookmarks.prototype = {
// Don't use isSymlink(), since it would throw for invalid
// lnk files pointing to URLs or to unresolvable paths.
if (entry.path == entry.target && entry.isDirectory()) {
let destFolderId;
let folderGuid;
if (entry.leafName == this._toolbarFolderName &&
entry.parent.equals(this._favoritesFolder)) {
// Import to the bookmarks toolbar.
destFolderId = PlacesUtils.toolbarFolderId;
folderGuid = PlacesUtils.bookmarks.toolbarGuid;
if (!MigrationUtils.isStartupMigration) {
destFolderId =
MigrationUtils.createImportedBookmarksFolder("IE", destFolderId);
folderGuid =
yield MigrationUtils.createImportedBookmarksFolder("IE", folderGuid);
}
}
else {
// Import to a new folder.
destFolderId =
PlacesUtils.bookmarks.createFolder(aDestFolderId, entry.leafName,
PlacesUtils.bookmarks.DEFAULT_INDEX);
folderGuid = (yield PlacesUtils.bookmarks.insert({
type: PlacesUtils.bookmarks.TYPE_FOLDER,
parentGuid: aDestFolderGuid,
title: entry.leafName
})).guid;
}
if (entry.isReadable()) {
// Recursively import the folder.
this._migrateFolder(entry, destFolderId);
yield this._migrateFolder(entry, folderGuid);
}
}
else {
@ -230,17 +228,16 @@ Bookmarks.prototype = {
let uri = fileHandler.readURLFile(entry);
let title = matches[1];
PlacesUtils.bookmarks.insertBookmark(aDestFolderId,
uri,
PlacesUtils.bookmarks.DEFAULT_INDEX,
title);
yield PlacesUtils.bookmarks.insert({
parentGuid: aDestFolderGuid, url: uri, title
});
}
}
} catch (ex) {
Components.utils.reportError("Unable to import IE favorite (" + entry.leafName + "): " + ex);
}
}
}
})
};
function History() {

View File

@ -15,11 +15,10 @@ const TOPIC_DID_IMPORT_BOOKMARKS = "initial-migration-did-import-default-bookmar
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/Task.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
"resource://gre/modules/PlacesUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
"resource://gre/modules/NetUtil.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "BookmarkHTMLUtils",
"resource://gre/modules/BookmarkHTMLUtils.jsm");
@ -422,23 +421,22 @@ this.MigrationUtils = Object.freeze({
* Helper for creating a folder for imported bookmarks from a particular
* migration source. The folder is created at the end of the given folder.
*
* @param aSourceNameStr
* @param sourceNameStr
* the source name (first letter capitalized). This is used
* for reading the localized source name from the migration
* bundle (e.g. if aSourceNameStr is Mosaic, this will try to read
* sourceNameMosaic from the migration bundle).
* @param aParentId
* the item-id of the folder in which the new folder should be
* created.
* @return the item-id of the new folder.
* @param parentGuid
* the GUID of the folder in which the new folder should be created.
* @return the GUID of the new folder.
*/
createImportedBookmarksFolder:
function MU_createImportedBookmarksFolder(aSourceNameStr, aParentId) {
let source = this.getLocalizedString("sourceName" + aSourceNameStr);
let label = this.getLocalizedString("importedBookmarksFolder", [source]);
return PlacesUtils.bookmarks.createFolder(
aParentId, label, PlacesUtils.bookmarks.DEFAULT_INDEX);
},
createImportedBookmarksFolder: Task.async(function* (sourceNameStr, parentGuid) {
let source = this.getLocalizedString("sourceName" + sourceNameStr);
let title = this.getLocalizedString("importedBookmarksFolder", [source]);
return (yield PlacesUtils.bookmarks.insert({
type: PlacesUtils.bookmarks.TYPE_FOLDER, parentGuid, title
})).guid;
}),
get _migrators() {
return gMigrators ? gMigrators : gMigrators = new Map();

View File

@ -32,23 +32,21 @@ Bookmarks.prototype = {
type: MigrationUtils.resourceTypes.BOOKMARKS,
migrate: function B_migrate(aCallback) {
PropertyListUtils.read(this._file,
MigrationUtils.wrapMigrateFunction(function migrateBookmarks(aDict) {
if (!aDict)
throw new Error("Could not read Bookmarks.plist");
return Task.spawn(function* () {
let dict = yield new Promise(resolve =>
PropertyListUtils.read(this._file, resolve)
);
if (!dict)
throw new Error("Could not read Bookmarks.plist");
let children = dict.get("Children");
if (!children)
throw new Error("Invalid Bookmarks.plist format");
let children = aDict.get("Children");;
if (!children)
throw new Error("Invalid Bookmarks.plist format");
PlacesUtils.bookmarks.runInBatchMode({
runBatched: function() {
let collection = aDict.get("Title") == "com.apple.ReadingList" ?
this.READING_LIST_COLLECTION : this.ROOT_COLLECTION;
this._migrateCollection(children, collection);
}.bind(this)
}, null);
}.bind(this), aCallback));
let collection = dict.get("Title") == "com.apple.ReadingList" ?
this.READING_LIST_COLLECTION : this.ROOT_COLLECTION;
yield this._migrateCollection(children, collection);
}.bind(this)).then(() => aCallback(true),
e => { Cu.reportError(e); aCallback(false) });
},
// Bookmarks collections in Safari. Constants for migrateCollection.
@ -65,7 +63,7 @@ Bookmarks.prototype = {
* @param aCollection
* one of the values above.
*/
_migrateCollection: function B__migrateCollection(aEntries, aCollection) {
_migrateCollection: Task.async(function* (aEntries, aCollection) {
// A collection of bookmarks in Safari resembles places roots. In the
// property list files (Bookmarks.plist, ReadingList.plist) they are
// stored as regular bookmarks folders, and thus can only be distinguished
@ -79,11 +77,11 @@ Bookmarks.prototype = {
let title = entry.get("Title");
let children = entry.get("Children");
if (title == "BookmarksBar")
this._migrateCollection(children, this.TOOLBAR_COLLECTION);
yield this._migrateCollection(children, this.TOOLBAR_COLLECTION);
else if (title == "BookmarksMenu")
this._migrateCollection(children, this.MENU_COLLECTION);
yield this._migrateCollection(children, this.MENU_COLLECTION);
else if (title == "com.apple.ReadingList")
this._migrateCollection(children, this.READING_LIST_COLLECTION);
yield this._migrateCollection(children, this.READING_LIST_COLLECTION);
else if (entry.get("ShouldOmitFromUI") !== true)
entriesFiltered.push(entry);
}
@ -99,7 +97,7 @@ Bookmarks.prototype = {
if (entriesFiltered.length == 0)
return;
let folder = -1;
let folderGuid = -1;
switch (aCollection) {
case this.ROOT_COLLECTION: {
// In Safari, it is possible (though quite cumbersome) to move
@ -108,22 +106,22 @@ Bookmarks.prototype = {
// both the places root and the unfiled-bookmarks root.
// Because the former is only an implementation detail in our UI,
// the unfiled root seems to be the best choice.
folder = PlacesUtils.unfiledBookmarksFolderId;
folderGuid = PlacesUtils.bookmarks.unfiledGuid;
break;
}
case this.MENU_COLLECTION: {
folder = PlacesUtils.bookmarksMenuFolderId;
folderGuid = PlacesUtils.bookmarks.menuGuid;
if (!MigrationUtils.isStartupMigration) {
folder = MigrationUtils.createImportedBookmarksFolder("Safari",
folder);
folderGuid =
yield MigrationUtils.createImportedBookmarksFolder("Safari", folderGuid);
}
break;
}
case this.TOOLBAR_COLLECTION: {
folder = PlacesUtils.toolbarFolderId;
folderGuid = PlacesUtils.bookmarks.toolbarGuid;
if (!MigrationUtils.isStartupMigration) {
folder = MigrationUtils.createImportedBookmarksFolder("Safari",
folder);
folderGuid =
yield MigrationUtils.createImportedBookmarksFolder("Safari", folderGuid);
}
break;
}
@ -131,51 +129,52 @@ Bookmarks.prototype = {
// Reading list items are imported as regular bookmarks.
// They are imported under their own folder, created either under the
// bookmarks menu (in the case of startup migration).
folder = PlacesUtils.bookmarks.createFolder(
PlacesUtils.bookmarksMenuFolderId,
MigrationUtils.getLocalizedString("importedSafariReadingList"),
PlacesUtils.bookmarks.DEFAULT_INDEX);
folderGuid = (yield PlacesUtils.bookmarks.insert({
parentGuid: PlacesUtils.bookmarks.menuGuid,
type: PlacesUtils.bookmarks.TYPE_FOLDER,
title: MigrationUtils.getLocalizedString("importedSafariReadingList"),
})).guid;
break;
}
default:
throw new Error("Unexpected value for aCollection!");
}
if (folderGuid == -1)
throw new Error("Invalid folder GUID");
this._migrateEntries(entriesFiltered, folder);
},
yield this._migrateEntries(entriesFiltered, folderGuid);
}),
// migrate the given array of safari bookmarks to the given places
// folder.
_migrateEntries: function B__migrateEntries(aEntries, aFolderId) {
for (let entry of aEntries) {
_migrateEntries: Task.async(function* (entries, parentGuid) {
for (let entry of entries) {
let type = entry.get("WebBookmarkType");
if (type == "WebBookmarkTypeList" && entry.has("Children")) {
let title = entry.get("Title");
let folderId = PlacesUtils.bookmarks.createFolder(
aFolderId, title, PlacesUtils.bookmarks.DEFAULT_INDEX);
let newFolderGuid = (yield PlacesUtils.bookmarks.insert({
parentGuid, type: PlacesUtils.bookmarks.TYPE_FOLDER, title
})).guid;
// Empty folders may not have a children array.
if (entry.has("Children"))
this._migrateEntries(entry.get("Children"), folderId, false);
yield this._migrateEntries(entry.get("Children"), newFolderGuid, false);
}
else if (type == "WebBookmarkTypeLeaf" && entry.has("URLString")) {
let title, uri;
let title;
if (entry.has("URIDictionary"))
title = entry.get("URIDictionary").get("title");
try {
uri = NetUtil.newURI(entry.get("URLString"));
}
catch(ex) {
Cu.reportError("Invalid uri set for Safari bookmark: " + entry.get("URLString"));
}
if (uri) {
PlacesUtils.bookmarks.insertBookmark(aFolderId, uri,
PlacesUtils.bookmarks.DEFAULT_INDEX, title);
yield PlacesUtils.bookmarks.insert({
parentGuid, url: entry.get("URLString"), title
});
} catch(ex) {
Cu.reportError("Invalid Safari bookmark: " + ex);
}
}
}
}
})
};
function History(aHistoryFile) {