Bug 739041. (Bv1a) Port |Bug 482911 - [HTML5] Re-implement bookmarks.html parsing using the HTML5 parser| to SeaMonkey, application part. r=neil.
authorSerge Gautherie <sgautherie.bz@free.fr>
Mon, 09 Apr 2012 03:20:03 +0200
changeset 11581 abbe21c88c1aa64976bc16a69a3d476c6c2f448d
parent 11580 1c784b29d21da8db66016acfb85904552f12b4c9
child 11582 104c0b09c8332361292083ba2434d2f20d76bafd
push id529
push userbugzilla@standard8.plus.com
push dateMon, 04 Jun 2012 19:55:55 +0000
treeherdercomm-beta@109334822255 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersneil
bugs739041, 482911
Bug 739041. (Bv1a) Port |Bug 482911 - [HTML5] Re-implement bookmarks.html parsing using the HTML5 parser| to SeaMonkey, application part. r=neil.
suite/common/bookmarks/bookmarksManager.js
suite/common/src/nsSuiteGlue.js
--- a/suite/common/bookmarks/bookmarksManager.js
+++ b/suite/common/bookmarks/bookmarksManager.js
@@ -352,20 +352,19 @@ var PlacesOrganizer = {
    */
   importFromFile: function PO_importFromFile() {
     var fp = Components.classes["@mozilla.org/filepicker;1"]
                        .createInstance(Components.interfaces.nsIFilePicker);
     fp.init(window, PlacesUIUtils.getString("SelectImport"),
             Components.interfaces.nsIFilePicker.modeOpen);
     fp.appendFilters(Components.interfaces.nsIFilePicker.filterHTML);
     if (fp.show() != Components.interfaces.nsIFilePicker.returnCancel) {
-      if (fp.file) {
-        var importer = Components.classes["@mozilla.org/browser/places/import-export-service;1"]
-                                 .getService(Components.interfaces.nsIPlacesImportExportService);
-        importer.importHTMLFromFile(fp.file, false);
+      if (fp.fileURL) {
+        Components.utils.import("resource://gre/modules/BookmarkHTMLUtils.jsm");
+        BookmarkHTMLUtils.importFromURL(fp.fileURL.spec, false);
       }
     }
   },
 
   /**
    * Allows simple exporting of bookmarks.
    */
   exportBookmarks: function PO_exportBookmarks() {
--- a/suite/common/src/nsSuiteGlue.js
+++ b/suite/common/src/nsSuiteGlue.js
@@ -42,16 +42,19 @@
 const XULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
 
 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
 Components.utils.import("resource://gre/modules/Services.jsm");
 Components.utils.import("resource://gre/modules/AddonManager.jsm");
 Components.utils.import("resource:///modules/Sanitizer.jsm");
 Components.utils.import("resource:///modules/mailnewsMigrator.js");
 
+XPCOMUtils.defineLazyModuleGetter(this, "BookmarkHTMLUtils",
+                                  "resource://gre/modules/BookmarkHTMLUtils.jsm");
+
 // We try to backup bookmarks at idle times, to avoid doing that at shutdown.
 // Number of idle seconds before trying to backup bookmarks.  15 minutes.
 const BOOKMARKS_BACKUP_IDLE_TIME = 15 * 60;
 // Minimum interval in milliseconds between backups.
 const BOOKMARKS_BACKUP_INTERVAL = 86400 * 1000;
 // Maximum number of backups to create.  Old ones will be purged.
 const BOOKMARKS_BACKUP_MAX_BACKUPS = 10;
 
@@ -201,22 +204,18 @@ SuiteGlue.prototype = {
         }
         // places-shutdown is fired when the profile is about to disappear.
         this._onProfileShutdown();
         break;
       case "idle":
         if (this._idleService.idleTime > BOOKMARKS_BACKUP_IDLE_TIME * 1000)
           this._backupBookmarks();
         break;
-      case "bookmarks-restore-success":
-      case "bookmarks-restore-failed":
-        Services.obs.removeObserver(this, "bookmarks-restore-success");
-        Services.obs.removeObserver(this, "bookmarks-restore-failed");
-        if (topic == "bookmarks-restore-success" && data == "html-initial")
-          this.ensurePlacesDefaultQueriesInitialized();
+      case "initial-migration":
+        this._initialMigrationPerformed = true;
         break;
     }
   },
 
   // nsIWebProgressListener partial implementation
   onLocationChange: function(aWebProgress, aRequest, aLocation, aFlags)
   {
     if (aWebProgress.DOMWindow.top == aWebProgress.DOMWindow &&
@@ -659,33 +658,20 @@ SuiteGlue.prototype = {
 
     Components.utils.import("resource://gre/modules/PlacesUtils.jsm");
     var bookmarksBackupFile = PlacesUtils.backups.getMostRecent("json");
 
     // If the database is corrupt or has been newly created we should
     // import bookmarks. Same if we don't have any JSON backups, which
     // probably means that we never have used bookmarks in places yet.
     var databaseStatus = histsvc.databaseStatus;
-    var importBookmarks = databaseStatus == histsvc.DATABASE_STATUS_CREATE ||
-                          databaseStatus == histsvc.DATABASE_STATUS_CORRUPT ||
-                          !bookmarksBackupFile;
-
-    if (databaseStatus == histsvc.DATABASE_STATUS_CREATE ||
-        !bookmarksBackupFile) {
-      // If the database has just been created or we miss a JSON backup, but
-      // we already have any bookmark despite that, this is not the initial
-      // import. This can happen after a migration from a different browser
-      // since migrators run before us, or when someone cleaned out backups.
-      // In such a case we should not import, unless some pref has been set.
-      var bmsvc = Components.classes["@mozilla.org/browser/nav-bookmarks-service;1"]
-                            .getService(Components.interfaces.nsINavBookmarksService);
-      if (bmsvc.getIdForItemAt(bmsvc.bookmarksMenuFolder, 0) != -1 ||
-          bmsvc.getIdForItemAt(bmsvc.toolbarFolder, 0) != -1)
-        importBookmarks = false;
-    }
+    var importBookmarks = !this._initialMigrationPerformed &&
+                          (databaseStatus == histsvc.DATABASE_STATUS_CREATE ||
+                           databaseStatus == histsvc.DATABASE_STATUS_CORRUPT ||
+                           !bookmarksBackupFile);
 
     // Check if user or an extension has required to import bookmarks.html
     var importBookmarksHTML = false;
     try {
       importBookmarksHTML =
         Services.prefs.getBoolPref("browser.places.importBookmarksHTML");
       if (importBookmarksHTML)
         importBookmarks = true;
@@ -759,36 +745,36 @@ SuiteGlue.prototype = {
       else {
         // Get bookmarks.html file location.
         var bookmarksFile = Services.dirsvc.get("BMarks", Components.interfaces.nsILocalFile);
         if (bookmarksFile.exists())
           bookmarksURI = Services.io.newFileURI(bookmarksFile);
       }
 
       if (bookmarksURI) {
-        // Add an import observer.  It will ensure that smart bookmarks are
-        // created once the operation is complete.
-        Services.obs.addObserver(this, "bookmarks-restore-success", false);
-        Services.obs.addObserver(this, "bookmarks-restore-failed", false);
-
         // Import from bookmarks.html file.
         try {
-          var importer = Components.classes["@mozilla.org/browser/places/import-export-service;1"]
-                                   .getService(Components.interfaces.nsIPlacesImportExportService);
-          importer.importHTMLFromURI(bookmarksURI, true /* overwrite existing */);
+          BookmarkHTMLUtils.importFromURL(bookmarksURI.spec, true, (function (success) {
+            if (success) {
+              // Ensure that smart bookmarks are created once the operation is
+              // complete.
+              this.ensurePlacesDefaultQueriesInitialized();
+            }
+            else {
+              Components.utils.reportError("Bookmarks.html file could be corrupt.");
+            }
+          }).bind(this));
         }
         catch(ex) {
-          // Report the error, but ignore it.
           Components.utils.reportError("bookmarks.html file could be corrupt. " + ex);
-          Services.obs.removeObserver(this, "bookmarks-restore-success");
-          Services.obs.removeObserver(this, "bookmarks-restore-failed");
         }
       }
-      else
+      else {
         Components.utils.reportError("Unable to find bookmarks.html file.");
+      }
 
       // Reset preferences, so we won't try to import again at next run.
       if (importBookmarksHTML)
         Services.prefs.setBoolPref("browser.places.importBookmarksHTML", false);
       if (restoreDefaultBookmarks)
         Services.prefs.setBoolPref("browser.bookmarks.restore_default_bookmarks",
                                    false);
     }