Bug 1833429 - Return the number of imported bookmarks when importing HTML files. r=mak

When calling importFromFile or importFromUrl, we return the count of
imported bookmarks from the HTML bookmarks file.

Differential Revision: https://phabricator.services.mozilla.com/D178463
This commit is contained in:
Tim Giles 2023-05-23 20:54:54 +00:00
Родитель 65a234391e
Коммит db0cc96407
2 изменённых файлов: 60 добавлений и 6 удалений

Просмотреть файл

@ -131,7 +131,8 @@ export var BookmarkHTMLUtils = Object.freeze({
* imported bookmarks. Defaults to `RESTORE` if `replace = true`, or
* `IMPORT` otherwise.
*
* @return {Promise}
* @returns {Promise<number>} The number of imported bookmarks, not including
* folders and separators.
* @resolves When the new bookmarks have been created.
* @rejects JavaScript exception.
*/
@ -144,10 +145,11 @@ export var BookmarkHTMLUtils = Object.freeze({
: PlacesUtils.bookmarks.SOURCES.IMPORT,
} = {}
) {
let bookmarkCount;
notifyObservers(PlacesUtils.TOPIC_BOOKMARKS_RESTORE_BEGIN, aInitialImport);
try {
let importer = new BookmarkImporter(aInitialImport, aSource);
await importer.importFromURL(aSpec);
bookmarkCount = await importer.importFromURL(aSpec);
notifyObservers(
PlacesUtils.TOPIC_BOOKMARKS_RESTORE_SUCCESS,
@ -161,6 +163,7 @@ export var BookmarkHTMLUtils = Object.freeze({
);
throw ex;
}
return bookmarkCount;
},
/**
@ -176,7 +179,8 @@ export var BookmarkHTMLUtils = Object.freeze({
* imported bookmarks. Defaults to `RESTORE` if `replace = true`, or
* `IMPORT` otherwise.
*
* @return {Promise}
* @returns {Promise<number>} The number of imported bookmarks, not including
* folders and separators
* @resolves When the new bookmarks have been created.
* @rejects JavaScript exception.
*/
@ -189,6 +193,7 @@ export var BookmarkHTMLUtils = Object.freeze({
: PlacesUtils.bookmarks.SOURCES.IMPORT,
} = {}
) {
let bookmarkCount;
notifyObservers(PlacesUtils.TOPIC_BOOKMARKS_RESTORE_BEGIN, aInitialImport);
try {
if (!(await IOUtils.exists(aFilePath))) {
@ -197,7 +202,9 @@ export var BookmarkHTMLUtils = Object.freeze({
);
}
let importer = new BookmarkImporter(aInitialImport, aSource);
await importer.importFromURL(PathUtils.toFileURI(aFilePath));
bookmarkCount = await importer.importFromURL(
PathUtils.toFileURI(aFilePath)
);
notifyObservers(
PlacesUtils.TOPIC_BOOKMARKS_RESTORE_SUCCESS,
@ -211,6 +218,7 @@ export var BookmarkHTMLUtils = Object.freeze({
);
throw ex;
}
return bookmarkCount;
},
/**
@ -793,6 +801,8 @@ BookmarkImporter.prototype = {
*
* @param {BookmarkImporter} importer The importer from which to get the
* bookmark information.
* @returns {number} The number of imported bookmarks, not including
* folders and separators
*/
async _importBookmarks() {
if (this._isImportDefaults) {
@ -800,6 +810,7 @@ BookmarkImporter.prototype = {
}
let bookmarksTrees = this._getBookmarkTrees();
let bookmarkCount = 0;
for (let tree of bookmarksTrees) {
if (!tree.children.length) {
continue;
@ -807,17 +818,24 @@ BookmarkImporter.prototype = {
// Give the tree the source.
tree.source = this._source;
await PlacesUtils.bookmarks.insertTree(tree, {
let bookmarks = await PlacesUtils.bookmarks.insertTree(tree, {
fixupOrSkipInvalidEntries: true,
});
// We want to count only bookmarks, not folders or separators
bookmarkCount += bookmarks.filter(
bookmark => bookmark.type == PlacesUtils.bookmarks.TYPE_BOOKMARK
).length;
insertFaviconsForTree(tree);
}
return bookmarkCount;
},
/**
* Imports data into the places database from the supplied url.
*
* @param {String} href The url to import data from.
* @returns {number} The number of imported bookmarks, not including
* folders and separators.
*/
async importFromURL(href) {
let data = await fetchData(href);
@ -841,7 +859,7 @@ BookmarkImporter.prototype = {
}
this._walkTreeForImport(data);
await this._importBookmarks();
return this._importBookmarks();
},
};

Просмотреть файл

@ -106,6 +106,42 @@ add_task(async function setup() {
await PlacesUtils.bookmarks.eraseEverything();
});
add_task(async function test_import_count() {
// Ensure the bookmarks count is correct when importing in various cases
let count = await BookmarkHTMLUtils.importFromFile(gBookmarksFileNew, {
replace: true,
});
Assert.equal(
count,
8,
"There should be 8 imported bookmarks when importing from an empty database"
);
await PlacesTestUtils.promiseAsyncUpdates();
await BookmarkHTMLUtils.exportToFile(gBookmarksFileNew);
await PlacesTestUtils.promiseAsyncUpdates();
count = -1;
count = await BookmarkHTMLUtils.importFromFile(gBookmarksFileNew, {
replace: true,
});
Assert.equal(
count,
8,
"There should be 8 imported bookmarks when replacing existing bookmarks"
);
await PlacesTestUtils.promiseAsyncUpdates();
count = -1;
count = await BookmarkHTMLUtils.importFromFile(gBookmarksFileNew);
Assert.equal(
count,
8,
"There should be 8 imported bookmarks even when we are not replacing existing bookmarks"
);
await PlacesTestUtils.promiseAsyncUpdates();
await PlacesUtils.bookmarks.eraseEverything();
});
add_task(async function test_import_new() {
// Test importing a Places bookmarks.html file.
// 1. import bookmarks.exported.html