Bug 735312 - Properly handle importing default bookmarks from omni.ja.
authorMarco Bonardo <mbonardo@mozilla.com>
Wed, 14 Mar 2012 18:57:00 +0100
changeset 91846 245b87d73a55dfd06d979ef83bf0ac650afefeda
parent 91845 ce052bf33131e1819ee6e05ead445c8827626b95
child 91847 ae7c1ff7e8d802fc7cf8386dc1fd6b1332f0cc0f
push idunknown
push userunknown
push dateunknown
bugs735312
milestone13.0a2
Bug 735312 - Properly handle importing default bookmarks from omni.ja. r=felipe a=akeybl
browser/components/migration/src/nsBrowserProfileMigratorUtils.cpp
browser/components/migration/src/nsBrowserProfileMigratorUtils.h
browser/components/migration/src/nsIEProfileMigrator.cpp
browser/components/migration/src/nsSafariProfileMigrator.cpp
browser/components/migration/tests/unit/bookmarks.html
browser/components/migration/tests/unit/head_migration.js
--- a/browser/components/migration/src/nsBrowserProfileMigratorUtils.cpp
+++ b/browser/components/migration/src/nsBrowserProfileMigratorUtils.cpp
@@ -51,17 +51,17 @@
 
 #include "nsAppDirectoryServiceDefs.h"
 #include "nsIRDFService.h"
 #include "nsIStringBundle.h"
 #include "nsXPCOMCID.h"
 
 #define MIGRATION_BUNDLE "chrome://browser/locale/migration/migration.properties"
 
-#define BOOKMARKS_FILE_NAME NS_LITERAL_STRING("bookmarks.html")
+#define DEFAULT_BOOKMARKS NS_LITERAL_CSTRING("resource:///defaults/profile/bookmarks.html")
 
 void SetUnicharPref(const char* aPref, const nsAString& aValue,
                     nsIPrefBranch* aPrefs)
 {
   nsCOMPtr<nsISupportsString> supportsString =
     do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID);    
   if (supportsString) {
      supportsString->SetData(aValue); 
@@ -159,77 +159,24 @@ GetProfilePath(nsIProfileStartup* aStart
     if (dirSvc) {
       dirSvc->Get(NS_APP_USER_PROFILE_50_DIR, NS_GET_IID(nsIFile),
                   (void**) getter_AddRefs(aProfileDir));
     }
   }
 }
 
 nsresult
-ImportBookmarksHTML(nsIFile* aBookmarksFile, 
-                    bool aImportIntoRoot,
-                    bool aOverwriteDefaults,
-                    const PRUnichar* aImportSourceNameKey)
+ImportDefaultBookmarks()
 {
-  nsresult rv;
-
-  nsCOMPtr<nsILocalFile> localFile(do_QueryInterface(aBookmarksFile));
-  NS_ENSURE_TRUE(localFile, NS_ERROR_FAILURE);
-  nsCOMPtr<nsIPlacesImportExportService> importer = do_GetService(NS_PLACESIMPORTEXPORTSERVICE_CONTRACTID, &rv);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  // Import file directly into the bookmarks root folder.
-  if (aImportIntoRoot) {
-    rv = importer->ImportHTMLFromFile(localFile, aOverwriteDefaults);
-    NS_ENSURE_SUCCESS(rv, rv);
-    return NS_OK;
-  }
-
-  // Get the source application name.
-  nsCOMPtr<nsIStringBundleService> bundleService =
-    do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
-  NS_ENSURE_SUCCESS(rv, rv);
-  nsCOMPtr<nsIStringBundle> bundle;
-  rv = bundleService->CreateBundle(MIGRATION_BUNDLE, getter_AddRefs(bundle));
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  nsString sourceName;
-  rv = bundle->GetStringFromName(aImportSourceNameKey,
-                                 getter_Copies(sourceName));
-  NS_ENSURE_SUCCESS(rv, rv);
+  nsCOMPtr<nsIPlacesImportExportService> importer =
+    do_GetService(NS_PLACESIMPORTEXPORTSERVICE_CONTRACTID);
+  NS_ENSURE_STATE(importer);
 
-  const PRUnichar* sourceNameStrings[] = { sourceName.get() };
-  nsString importedBookmarksTitle;
-  rv = bundle->FormatStringFromName(NS_LITERAL_STRING("importedBookmarksFolder").get(),
-                                    sourceNameStrings, 1, 
-                                    getter_Copies(importedBookmarksTitle));
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  // Get the bookmarks service.
-  nsCOMPtr<nsINavBookmarksService> bms =
-    do_GetService(NS_NAVBOOKMARKSSERVICE_CONTRACTID, &rv);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  // Create an imported bookmarks folder under the bookmarks menu.
-  PRInt64 root;
-  rv = bms->GetBookmarksMenuFolder(&root);
-  NS_ENSURE_SUCCESS(rv, rv);
+  nsCOMPtr<nsIIOService> ioService = mozilla::services::GetIOService();
+  NS_ENSURE_STATE(ioService);
+  nsCOMPtr<nsIURI> bookmarksURI;
+  nsresult rv = ioService->NewURI(DEFAULT_BOOKMARKS, nsnull, nsnull,
+                                  getter_AddRefs(bookmarksURI));
+  if (NS_FAILED(rv))
+    return rv;
 
-  PRInt64 folder;
-  rv = bms->CreateFolder(root, NS_ConvertUTF16toUTF8(importedBookmarksTitle),
-                         nsINavBookmarksService::DEFAULT_INDEX, &folder);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  // Import the bookmarks into the folder.
-  return importer->ImportHTMLFromFileToFolder(localFile, folder, false);
+  return importer->ImportHTMLFromURI(bookmarksURI, true);
 }
-
-nsresult
-InitializeBookmarks(nsIFile* aTargetProfile)
-{
-  nsCOMPtr<nsIFile> bookmarksFile;
-  aTargetProfile->Clone(getter_AddRefs(bookmarksFile));
-  bookmarksFile->Append(BOOKMARKS_FILE_NAME);
-  
-  nsresult rv = ImportBookmarksHTML(bookmarksFile, true, true, EmptyString().get());
-  NS_ENSURE_SUCCESS(rv, rv);
-  return NS_OK;
-}
--- a/browser/components/migration/src/nsBrowserProfileMigratorUtils.h
+++ b/browser/components/migration/src/nsBrowserProfileMigratorUtils.h
@@ -94,23 +94,15 @@ void GetMigrateDataFromArray(MigrationDa
                              nsIFile* aSourceProfile, 
                              PRUint16* aResult);
 
 
 // get the base directory of the *target* profile
 // this is already cloned, modify it to your heart's content
 void GetProfilePath(nsIProfileStartup* aStartup, nsCOMPtr<nsIFile>& aProfileDir);
 
-// In-place import from aBookmarksFile into a folder in the user's bookmarks.
-// If the importIntoRoot parameter has a value of true, the bookmarks will be
-// imported into the bookmarks root folder. Otherwise, they'll be imported into
-// a new folder with the name "From (STR:aImportSourceNameKey)".
-// aImportSourceNameKey is a key into migration.properties with the pretty name
-// of the application.
-nsresult ImportBookmarksHTML(nsIFile* aBookmarksFile, 
-                             bool aImportIntoRoot,
-                             bool aOverwriteDefaults,
-                             const PRUnichar* aImportSourceNameKey);
-
-nsresult InitializeBookmarks(nsIFile* aTargetProfile);
+/**
+ * Imports default bookmarks to the profile.
+ */
+nsresult ImportDefaultBookmarks();
 
 #endif
 
--- a/browser/components/migration/src/nsIEProfileMigrator.cpp
+++ b/browser/components/migration/src/nsIEProfileMigrator.cpp
@@ -1399,21 +1399,20 @@ nsIEProfileMigrator::CopyFavoritesBatche
 
     rv = bms->CreateFolder(bookmarksMenuFolderId,
                            NS_ConvertUTF16toUTF8(importedIEFavsTitle),
                            nsINavBookmarksService::DEFAULT_INDEX,
                            &folder);
     NS_ENSURE_SUCCESS(rv, rv);
   }
   else {
-    // Initialize the default bookmarks
-    nsCOMPtr<nsIFile> profile;
-    GetProfilePath(nsnull, profile);
-    rv = InitializeBookmarks(profile);
-    NS_ENSURE_SUCCESS(rv, rv);
+    // If importing defaults fails for whatever reason, let the import process
+    // continue.
+    DebugOnly<nsresult> rv = ImportDefaultBookmarks();
+    MOZ_ASSERT(NS_SUCCEEDED(rv), "Should be able to import default bookmarks");
 
     // Locate the Links toolbar folder, we want to replace the Personal Toolbar
     // content with Favorites in this folder.
     // On versions minor or equal to IE6 the folder name is stored in the
     // LinksFolderName registry key, but in newer versions it may be just a
     // Links subfolder inside the default Favorites folder.
     nsCOMPtr<nsIWindowsRegKey> regKey =
       do_CreateInstance("@mozilla.org/windows-registry-key;1");
--- a/browser/components/migration/src/nsSafariProfileMigrator.cpp
+++ b/browser/components/migration/src/nsSafariProfileMigrator.cpp
@@ -60,27 +60,31 @@
 #include "nsIServiceManager.h"
 #include "nsIStringBundle.h"
 #include "nsISupportsPrimitives.h"
 #include "nsSafariProfileMigrator.h"
 #include "nsToolkitCompsCID.h"
 #include "nsNetUtil.h"
 #include "nsTArray.h"
 
+#include "mozilla/Util.h"
+
 #include <Carbon/Carbon.h>
 
 #define SAFARI_PREFERENCES_FILE_NAME      NS_LITERAL_STRING("com.apple.Safari.plist")
 #define SAFARI_BOOKMARKS_FILE_NAME        NS_LITERAL_STRING("Bookmarks.plist")
 #define SAFARI_HISTORY_FILE_NAME          NS_LITERAL_STRING("History.plist")
 #define SAFARI_COOKIES_FILE_NAME          NS_LITERAL_STRING("Cookies.plist")
 #define SAFARI_COOKIE_BEHAVIOR_FILE_NAME  NS_LITERAL_STRING("com.apple.WebFoundation.plist")
 #define SAFARI_DATE_OFFSET                978307200
 #define SAFARI_HOME_PAGE_PREF             "HomePage"
 #define MIGRATION_BUNDLE                  "chrome://browser/locale/migration/migration.properties"
 
+using namespace mozilla;
+
 ///////////////////////////////////////////////////////////////////////////////
 // nsSafariProfileMigrator
 
 NS_IMPL_ISUPPORTS1(nsSafariProfileMigrator, nsIBrowserProfileMigrator)
 
 nsSafariProfileMigrator::nsSafariProfileMigrator()
 {
   mObserverService = do_GetService("@mozilla.org/observer-service;1");
@@ -972,20 +976,21 @@ nsSafariProfileMigrator::CopyBookmarksBa
     NS_ENSURE_SUCCESS(rv, rv);
 
     rv = bms->CreateFolder(bookmarksMenuFolderId,
                            NS_ConvertUTF16toUTF8(importedSafariBookmarksTitle),
                            nsINavBookmarksService::DEFAULT_INDEX, &folder);
     NS_ENSURE_SUCCESS(rv, rv);
   }
   else {
-    nsCOMPtr<nsIFile> profile;
-    GetProfilePath(nsnull, profile);
-    rv = InitializeBookmarks(profile);
-    NS_ENSURE_SUCCESS(rv, rv);
+    // If importing defaults fails for whatever reason, let the import process
+    // continue.
+    DebugOnly<nsresult> rv = ImportDefaultBookmarks();
+    MOZ_ASSERT(NS_SUCCEEDED(rv), "Should be able to import default bookmarks");
+
     // In replace mode we are merging at the top level.
     folder = bookmarksMenuFolderId;
   }
 
   nsCOMPtr<nsIProperties> fileLocator(do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID));
   nsCOMPtr<nsILocalFile> safariBookmarksFile;
   fileLocator->Get(NS_MAC_USER_LIB_DIR, NS_GET_IID(nsILocalFile),
                    getter_AddRefs(safariBookmarksFile));
deleted file mode 100644
--- a/browser/components/migration/tests/unit/bookmarks.html
+++ /dev/null
@@ -1,16 +0,0 @@
-<!DOCTYPE NETSCAPE-Bookmark-file-1>
-<!-- This is an automatically generated file.
-     It will be read and overwritten.
-     DO NOT EDIT! -->
-<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
-<TITLE>Bookmarks</TITLE>
-<H1>Bookmarks Menu</H1>
-
-<DL><p>
-    <DT><A HREF="http://example.com/" ADD_DATE="1233157972" LAST_MODIFIED="1233157984">example</A>
-    <DT><H3 ADD_DATE="1233157910" LAST_MODIFIED="1233157972" PERSONAL_TOOLBAR_FOLDER="true">Bookmarks Toolbar</H3>
-<DD>Add bookmarks to this folder to see them displayed on the Bookmarks Toolbar
-    <DL><p>
-        <DT><A HREF="http://example.com/" ADD_DATE="1233157972" LAST_MODIFIED="1233157984">example</A>
-    </DL><p>
-</DL><p>
--- a/browser/components/migration/tests/unit/head_migration.js
+++ b/browser/components/migration/tests/unit/head_migration.js
@@ -17,12 +17,8 @@ XPCOMUtils.defineLazyModuleGetter(this, 
 
 // Initialize profile.
 let gProfD = do_get_profile();
 
 function newMigratorFor(aKey) {
   let cid = "@mozilla.org/profile/migrator;1?app=browser&type=" + aKey;
   return Cc[cid].createInstance(Ci.nsIBrowserProfileMigrator);
 }
-
-let (bookmarkshtml = do_get_file("bookmarks.html")) {
-  bookmarkshtml.copyTo(gProfD, "bookmarks.html");
-}