Bug 1347530 - Part 2: Use insertTree API to import bookmarks from 360se. r=Gijs
authorHector Zhao <bzhao@mozilla.com>
Thu, 08 Jun 2017 10:42:39 +0800
changeset 411299 99fe8edb8296f2cd4f704e7dcad482a4926ca1ac
parent 411298 300d2598ec40fec50f611e62db2d51bf90748782
child 411300 3888304a47f36783634e92edc1704663b1c84ce5
push id7391
push usermtabara@mozilla.com
push dateMon, 12 Jun 2017 13:08:53 +0000
treeherdermozilla-beta@2191d7f87e2e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersGijs
bugs1347530
milestone55.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
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