зеркало из https://github.com/mozilla/gecko-dev.git
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:
Родитель
65a234391e
Коммит
db0cc96407
|
@ -131,7 +131,8 @@ export var BookmarkHTMLUtils = Object.freeze({
|
||||||
* imported bookmarks. Defaults to `RESTORE` if `replace = true`, or
|
* imported bookmarks. Defaults to `RESTORE` if `replace = true`, or
|
||||||
* `IMPORT` otherwise.
|
* `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.
|
* @resolves When the new bookmarks have been created.
|
||||||
* @rejects JavaScript exception.
|
* @rejects JavaScript exception.
|
||||||
*/
|
*/
|
||||||
|
@ -144,10 +145,11 @@ export var BookmarkHTMLUtils = Object.freeze({
|
||||||
: PlacesUtils.bookmarks.SOURCES.IMPORT,
|
: PlacesUtils.bookmarks.SOURCES.IMPORT,
|
||||||
} = {}
|
} = {}
|
||||||
) {
|
) {
|
||||||
|
let bookmarkCount;
|
||||||
notifyObservers(PlacesUtils.TOPIC_BOOKMARKS_RESTORE_BEGIN, aInitialImport);
|
notifyObservers(PlacesUtils.TOPIC_BOOKMARKS_RESTORE_BEGIN, aInitialImport);
|
||||||
try {
|
try {
|
||||||
let importer = new BookmarkImporter(aInitialImport, aSource);
|
let importer = new BookmarkImporter(aInitialImport, aSource);
|
||||||
await importer.importFromURL(aSpec);
|
bookmarkCount = await importer.importFromURL(aSpec);
|
||||||
|
|
||||||
notifyObservers(
|
notifyObservers(
|
||||||
PlacesUtils.TOPIC_BOOKMARKS_RESTORE_SUCCESS,
|
PlacesUtils.TOPIC_BOOKMARKS_RESTORE_SUCCESS,
|
||||||
|
@ -161,6 +163,7 @@ export var BookmarkHTMLUtils = Object.freeze({
|
||||||
);
|
);
|
||||||
throw ex;
|
throw ex;
|
||||||
}
|
}
|
||||||
|
return bookmarkCount;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -176,7 +179,8 @@ export var BookmarkHTMLUtils = Object.freeze({
|
||||||
* imported bookmarks. Defaults to `RESTORE` if `replace = true`, or
|
* imported bookmarks. Defaults to `RESTORE` if `replace = true`, or
|
||||||
* `IMPORT` otherwise.
|
* `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.
|
* @resolves When the new bookmarks have been created.
|
||||||
* @rejects JavaScript exception.
|
* @rejects JavaScript exception.
|
||||||
*/
|
*/
|
||||||
|
@ -189,6 +193,7 @@ export var BookmarkHTMLUtils = Object.freeze({
|
||||||
: PlacesUtils.bookmarks.SOURCES.IMPORT,
|
: PlacesUtils.bookmarks.SOURCES.IMPORT,
|
||||||
} = {}
|
} = {}
|
||||||
) {
|
) {
|
||||||
|
let bookmarkCount;
|
||||||
notifyObservers(PlacesUtils.TOPIC_BOOKMARKS_RESTORE_BEGIN, aInitialImport);
|
notifyObservers(PlacesUtils.TOPIC_BOOKMARKS_RESTORE_BEGIN, aInitialImport);
|
||||||
try {
|
try {
|
||||||
if (!(await IOUtils.exists(aFilePath))) {
|
if (!(await IOUtils.exists(aFilePath))) {
|
||||||
|
@ -197,7 +202,9 @@ export var BookmarkHTMLUtils = Object.freeze({
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
let importer = new BookmarkImporter(aInitialImport, aSource);
|
let importer = new BookmarkImporter(aInitialImport, aSource);
|
||||||
await importer.importFromURL(PathUtils.toFileURI(aFilePath));
|
bookmarkCount = await importer.importFromURL(
|
||||||
|
PathUtils.toFileURI(aFilePath)
|
||||||
|
);
|
||||||
|
|
||||||
notifyObservers(
|
notifyObservers(
|
||||||
PlacesUtils.TOPIC_BOOKMARKS_RESTORE_SUCCESS,
|
PlacesUtils.TOPIC_BOOKMARKS_RESTORE_SUCCESS,
|
||||||
|
@ -211,6 +218,7 @@ export var BookmarkHTMLUtils = Object.freeze({
|
||||||
);
|
);
|
||||||
throw ex;
|
throw ex;
|
||||||
}
|
}
|
||||||
|
return bookmarkCount;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -793,6 +801,8 @@ BookmarkImporter.prototype = {
|
||||||
*
|
*
|
||||||
* @param {BookmarkImporter} importer The importer from which to get the
|
* @param {BookmarkImporter} importer The importer from which to get the
|
||||||
* bookmark information.
|
* bookmark information.
|
||||||
|
* @returns {number} The number of imported bookmarks, not including
|
||||||
|
* folders and separators
|
||||||
*/
|
*/
|
||||||
async _importBookmarks() {
|
async _importBookmarks() {
|
||||||
if (this._isImportDefaults) {
|
if (this._isImportDefaults) {
|
||||||
|
@ -800,6 +810,7 @@ BookmarkImporter.prototype = {
|
||||||
}
|
}
|
||||||
|
|
||||||
let bookmarksTrees = this._getBookmarkTrees();
|
let bookmarksTrees = this._getBookmarkTrees();
|
||||||
|
let bookmarkCount = 0;
|
||||||
for (let tree of bookmarksTrees) {
|
for (let tree of bookmarksTrees) {
|
||||||
if (!tree.children.length) {
|
if (!tree.children.length) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -807,17 +818,24 @@ BookmarkImporter.prototype = {
|
||||||
|
|
||||||
// Give the tree the source.
|
// Give the tree the source.
|
||||||
tree.source = this._source;
|
tree.source = this._source;
|
||||||
await PlacesUtils.bookmarks.insertTree(tree, {
|
let bookmarks = await PlacesUtils.bookmarks.insertTree(tree, {
|
||||||
fixupOrSkipInvalidEntries: true,
|
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);
|
insertFaviconsForTree(tree);
|
||||||
}
|
}
|
||||||
|
return bookmarkCount;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Imports data into the places database from the supplied url.
|
* Imports data into the places database from the supplied url.
|
||||||
*
|
*
|
||||||
* @param {String} href The url to import data from.
|
* @param {String} href The url to import data from.
|
||||||
|
* @returns {number} The number of imported bookmarks, not including
|
||||||
|
* folders and separators.
|
||||||
*/
|
*/
|
||||||
async importFromURL(href) {
|
async importFromURL(href) {
|
||||||
let data = await fetchData(href);
|
let data = await fetchData(href);
|
||||||
|
@ -841,7 +859,7 @@ BookmarkImporter.prototype = {
|
||||||
}
|
}
|
||||||
|
|
||||||
this._walkTreeForImport(data);
|
this._walkTreeForImport(data);
|
||||||
await this._importBookmarks();
|
return this._importBookmarks();
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -106,6 +106,42 @@ add_task(async function setup() {
|
||||||
await PlacesUtils.bookmarks.eraseEverything();
|
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() {
|
add_task(async function test_import_new() {
|
||||||
// Test importing a Places bookmarks.html file.
|
// Test importing a Places bookmarks.html file.
|
||||||
// 1. import bookmarks.exported.html
|
// 1. import bookmarks.exported.html
|
||||||
|
|
Загрузка…
Ссылка в новой задаче