Bug 1347530 - Part 2: Use insertTree API to import bookmarks from 360se. r?Gijs draft
authorHector Zhao <bzhao@mozilla.com>
Thu, 08 Jun 2017 10:42:39 +0800
changeset 590760 da6922c40070667c989b47e768849098d86f5287
parent 590759 9d1655c741c00de13e5e631b66414edd08fbae4e
child 632300 52339cbb2a829c5af4981e3c0e9cd016d24747e8
push id62821
push userbmo:bzhao@mozilla.com
push dateThu, 08 Jun 2017 02:45:31 +0000
reviewersGijs
bugs1347530
milestone55.0a1
Bug 1347530 - Part 2: Use insertTree API to import bookmarks from 360se. r?Gijs MozReview-Commit-ID: H2iI9Pva6Yc
browser/components/migration/360seProfileMigrator.js
--- 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