Bug 1347530 - Part 2: Use insertTree API to import bookmarks from 360se. r?Gijs
MozReview-Commit-ID: H2iI9Pva6Yc
--- a/browser/components/migration/360seProfileMigrator.js
+++ b/browser/components/migration/360seProfileMigrator.js
@@ -13,16 +13,18 @@ Cu.import("resource://gre/modules/FileUt
Cu.import("resource://gre/modules/osfile.jsm");
Cu.import("resource:///modules/MigrationUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
"resource://gre/modules/PlacesUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Sqlite",
"resource://gre/modules/Sqlite.jsm");
+Cu.importGlobalProperties(["URL"]);
+
const kBookmarksFileName = "360sefav.db";
function copyToTempUTF8File(file, charset) {
let inputStream = Cc["@mozilla.org/network/file-input-stream;1"]
.createInstance(Ci.nsIFileInputStream);
inputStream.init(file, -1, -1, 0);
let inputStr = NetUtil.readInputStreamToString(
inputStream, inputStream.available(), { charset });
@@ -105,23 +107,18 @@ Bookmarks.prototype = {
type: MigrationUtils.resourceTypes.BOOKMARKS,
get exists() {
return this._file.exists() && this._file.isReadable();
},
migrate(aCallback) {
return (async () => {
- let idToGuid = new Map();
- let folderGuid = PlacesUtils.bookmarks.toolbarGuid;
- if (!MigrationUtils.isStartupMigration) {
- folderGuid =
- await MigrationUtils.createImportedBookmarksFolder("360se", folderGuid);
- }
- idToGuid.set(0, folderGuid);
+ let folderMap = new Map();
+ let toolbarBMs = [];
let connection = await Sqlite.openConnection({
path: this._file.path
});
try {
let rows = await connection.execute(
`WITH RECURSIVE
@@ -137,49 +134,57 @@ Bookmarks.prototype = {
for (let row of rows) {
let id = parseInt(row.getResultByName("id"), 10);
let parent_id = parseInt(row.getResultByName("parent_id"), 10);
let is_folder = parseInt(row.getResultByName("is_folder"), 10);
let title = row.getResultByName("title");
let url = row.getResultByName("url");
- let parentGuid = idToGuid.get(parent_id) || idToGuid.get("fallback");
- if (!parentGuid) {
- parentGuid = PlacesUtils.bookmarks.unfiledGuid;
- if (!MigrationUtils.isStartupMigration) {
- parentGuid =
- await MigrationUtils.createImportedBookmarksFolder("360se", parentGuid);
+ let bmToInsert;
+
+ if (is_folder) {
+ bmToInsert = {
+ children: [],
+ title,
+ type: PlacesUtils.bookmarks.TYPE_FOLDER
+ };
+ folderMap.set(id, bmToInsert);
+ } else {
+ try {
+ new URL(url);
+ } catch (ex) {
+ Cu.reportError(`Ignoring ${url} when importing from 360se because of exception: ${ex}`);
+ continue;
}
- idToGuid.set("fallback", parentGuid);
+
+ bmToInsert = {
+ title,
+ url
+ };
}
- try {
- if (is_folder == 1) {
- let newFolderGuid = (await MigrationUtils.insertBookmarkWrapper({
- parentGuid,
- type: PlacesUtils.bookmarks.TYPE_FOLDER,
- title
- })).guid;
-
- idToGuid.set(id, newFolderGuid);
- } else {
- await MigrationUtils.insertBookmarkWrapper({
- parentGuid,
- url,
- title
- });
- }
- } catch (ex) {
- Cu.reportError(ex);
+ if (folderMap.has(parent_id)) {
+ folderMap.get(parent_id).children.push(bmToInsert);
+ } else if (parent_id === 0) {
+ toolbarBMs.push(bmToInsert);
}
}
} finally {
await connection.close();
}
+
+ if (toolbarBMs.length) {
+ let parentGuid = PlacesUtils.bookmarks.toolbarGuid;
+ if (!MigrationUtils.isStartupMigration) {
+ parentGuid =
+ await MigrationUtils.createImportedBookmarksFolder("360se", parentGuid);
+ }
+ await MigrationUtils.insertManyBookmarksWrapper(toolbarBMs, parentGuid);
+ }
})().then(() => aCallback(true),
e => { Cu.reportError(e); aCallback(false) });
}
};
function Qihoo360seProfileMigrator() {
let paths = [
// for v6 and above