Bug 459781 - create our string bundle in a more sane way
authorShawn Wilsher <sdwilsh@shawnwilsher.com>
Fri, 17 Oct 2008 06:12:53 -0400
changeset 20578 c8f530de3268d69976e03307ffe27c206bd2f955
parent 20577 bfdcb1fbba182669a5047c7e638a20abdf4a4e49
child 20579 6977d70ecf04afbda7a0844933c15b2b93d219ad
push idunknown
push userunknown
push dateunknown
bugs459781
milestone1.9.1b2pre
Bug 459781 - create our string bundle in a more sane way This changeset only creates the string bundle when we need it, and only in one place as opposed to two separate places. r=dietrich
toolkit/components/places/src/nsNavBookmarks.cpp
toolkit/components/places/src/nsNavBookmarks.h
toolkit/components/places/src/nsNavHistory.cpp
toolkit/components/places/src/nsNavHistory.h
--- a/toolkit/components/places/src/nsNavBookmarks.cpp
+++ b/toolkit/components/places/src/nsNavBookmarks.cpp
@@ -100,30 +100,22 @@ nsNavBookmarks::~nsNavBookmarks()
 NS_IMPL_ISUPPORTS3(nsNavBookmarks,
                    nsINavBookmarksService,
                    nsINavHistoryObserver,
                    nsIAnnotationObserver)
 
 nsresult
 nsNavBookmarks::Init()
 {
-  nsresult rv;
-  nsCOMPtr<nsIStringBundleService> bundleService =
-    do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
-  NS_ENSURE_SUCCESS(rv, rv);
-  rv = bundleService->CreateBundle("chrome://places/locale/places.properties",
-                                   getter_AddRefs(mBundle));
-  NS_ENSURE_SUCCESS(rv, rv);
-
   nsNavHistory *history = History();
   NS_ENSURE_TRUE(history, NS_ERROR_OUT_OF_MEMORY);
   mDBConn = history->GetStorageConnection();
   mozStorageTransaction transaction(mDBConn, PR_FALSE);
 
-  rv = InitStatements();
+  nsresult rv = InitStatements();
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = FillBookmarksHash();
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = InitRoots();
   NS_ENSURE_SUCCESS(rv, rv);
 
@@ -541,44 +533,47 @@ nsNavBookmarks::InitRoots()
 // nsNavBookmarks::InitDefaults
 //
 // Initializes default bookmarks and containers.
 // Pulls from places.propertes for l10n.
 // Replaces the old default_places.html file.
 nsresult
 nsNavBookmarks::InitDefaults()
 {
+  nsIStringBundle *bundle = History()->GetBundle();
+  NS_ENSURE_TRUE(bundle, NS_ERROR_OUT_OF_MEMORY);
+
   // Bookmarks Menu
   nsXPIDLString bookmarksTitle;
-  nsresult rv = mBundle->GetStringFromName(NS_LITERAL_STRING("BookmarksMenuFolderTitle").get(),
-                                           getter_Copies(bookmarksTitle));
+  nsresult rv = bundle->GetStringFromName(NS_LITERAL_STRING("BookmarksMenuFolderTitle").get(),
+                                          getter_Copies(bookmarksTitle));
   NS_ENSURE_SUCCESS(rv, rv);
   rv = SetItemTitle(mBookmarksRoot, NS_ConvertUTF16toUTF8(bookmarksTitle));
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Bookmarks Toolbar
   nsXPIDLString toolbarTitle;
-  rv = mBundle->GetStringFromName(NS_LITERAL_STRING("BookmarksToolbarFolderTitle").get(),
-                                  getter_Copies(toolbarTitle));
+  rv = bundle->GetStringFromName(NS_LITERAL_STRING("BookmarksToolbarFolderTitle").get(),
+                                 getter_Copies(toolbarTitle));
   NS_ENSURE_SUCCESS(rv, rv);
   rv = SetItemTitle(mToolbarFolder, NS_ConvertUTF16toUTF8(toolbarTitle));
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Unsorted Bookmarks
   nsXPIDLString unfiledTitle;
-  rv = mBundle->GetStringFromName(NS_LITERAL_STRING("UnsortedBookmarksFolderTitle").get(),
-                                  getter_Copies(unfiledTitle));
+  rv = bundle->GetStringFromName(NS_LITERAL_STRING("UnsortedBookmarksFolderTitle").get(),
+                                 getter_Copies(unfiledTitle));
   NS_ENSURE_SUCCESS(rv, rv);
   rv = SetItemTitle(mUnfiledRoot, NS_ConvertUTF16toUTF8(unfiledTitle));
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Tags
   nsXPIDLString tagsTitle;
-  rv = mBundle->GetStringFromName(NS_LITERAL_STRING("TagsFolderTitle").get(),
-                                  getter_Copies(tagsTitle));
+  rv = bundle->GetStringFromName(NS_LITERAL_STRING("TagsFolderTitle").get(),
+                                 getter_Copies(tagsTitle));
   NS_ENSURE_SUCCESS(rv, rv);
   rv = SetItemTitle(mTagRoot, NS_ConvertUTF16toUTF8(tagsTitle));
   NS_ENSURE_SUCCESS(rv, rv);
 
   return NS_OK;
 }
 
 // nsNavBookmarks::CreateRoot
--- a/toolkit/components/places/src/nsNavBookmarks.h
+++ b/toolkit/components/places/src/nsNavBookmarks.h
@@ -36,17 +36,16 @@
  *
  * ***** END LICENSE BLOCK ***** */
 
 #ifndef nsNavBookmarks_h_
 #define nsNavBookmarks_h_
 
 #include "nsINavBookmarksService.h"
 #include "nsIAnnotationService.h"
-#include "nsIStringBundle.h"
 #include "nsITransaction.h"
 #include "nsNavHistory.h"
 #include "nsNavHistoryResult.h" // need for Int64 hashtable
 #include "nsToolkitCompsCID.h"
 
 class nsIOutputStream;
 
 class nsNavBookmarks : public nsINavBookmarksService,
@@ -208,18 +207,16 @@ private:
   nsCOMPtr<mozIStorageStatement> mDBSetItemLastModified;
   nsCOMPtr<mozIStorageStatement> mDBSetItemIndex;
 
   // keywords
   nsCOMPtr<mozIStorageStatement> mDBGetKeywordForURI;
   nsCOMPtr<mozIStorageStatement> mDBGetKeywordForBookmark;
   nsCOMPtr<mozIStorageStatement> mDBGetURIForKeyword;
 
-  nsCOMPtr<nsIStringBundle> mBundle;
-
   class RemoveFolderTransaction : public nsITransaction {
   public:
     RemoveFolderTransaction(PRInt64 aID) : mID(aID) {}
 
     NS_DECL_ISUPPORTS
 
     NS_IMETHOD DoTransaction() {
       nsNavBookmarks* bookmarks = nsNavBookmarks::GetBookmarksService();
--- a/toolkit/components/places/src/nsNavHistory.cpp
+++ b/toolkit/components/places/src/nsNavHistory.cpp
@@ -445,25 +445,16 @@ nsNavHistory::Init()
     NS_ENSURE_SUCCESS(rv, rv);
     PRBool hasSession;
     if (NS_SUCCEEDED(selectSession->ExecuteStep(&hasSession)) && hasSession)
       mLastSessionID = selectSession->AsInt64(0);
     else
       mLastSessionID = 1;
   }
 
-  // string bundle for localization
-  nsCOMPtr<nsIStringBundleService> bundleService =
-    do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
-  NS_ENSURE_SUCCESS(rv, rv);
-  rv = bundleService->CreateBundle(
-      "chrome://places/locale/places.properties",
-      getter_AddRefs(mBundle));
-  NS_ENSURE_SUCCESS(rv, rv);
-
   // initialize idle timer
   InitializeIdleTimer();
 
   // recent events hash tables
   NS_ENSURE_TRUE(mRecentTyped.Init(128), NS_ERROR_OUT_OF_MEMORY);
   NS_ENSURE_TRUE(mRecentBookmark.Init(128), NS_ERROR_OUT_OF_MEMORY);
   NS_ENSURE_TRUE(mRecentRedirects.Init(128), NS_ERROR_OUT_OF_MEMORY);
 
@@ -6557,33 +6548,41 @@ nsNavHistory::TitleForDomain(const nsCSt
 
   // use the localized one instead
   GetStringFromName(NS_LITERAL_STRING("localhost").get(), aTitle);
 }
 
 void
 nsNavHistory::GetAgeInDaysString(PRInt32 aInt, const PRUnichar *aName, nsACString& aResult)
 {
+  nsIStringBundle *bundle = GetBundle();
+  if (!bundle)
+    aResult.Truncate(0);
+
   nsAutoString intString;
   intString.AppendInt(aInt);
   const PRUnichar* strings[1] = { intString.get() };
   nsXPIDLString value;
-  nsresult rv = mBundle->FormatStringFromName(aName, strings, 
-                                              1, getter_Copies(value));
+  nsresult rv = bundle->FormatStringFromName(aName, strings,
+                                             1, getter_Copies(value));
   if (NS_SUCCEEDED(rv))
     CopyUTF16toUTF8(value, aResult);
   else
     aResult.Truncate(0);
 }
 
 void
 nsNavHistory::GetStringFromName(const PRUnichar *aName, nsACString& aResult)
 {
+  nsIStringBundle *bundle = GetBundle();
+  if (!bundle)
+    aResult.Truncate(0);
+
   nsXPIDLString value;
-  nsresult rv = mBundle->GetStringFromName(aName, getter_Copies(value));
+  nsresult rv = bundle->GetStringFromName(aName, getter_Copies(value));
   if (NS_SUCCEEDED(rv))
     CopyUTF16toUTF8(value, aResult);
   else
     aResult.Truncate(0);
 }
 
 // nsNavHistory::SetPageTitleInternal
 //
@@ -7367,16 +7366,33 @@ nsNavHistory::GetCollation()
     do_CreateInstance(NS_COLLATIONFACTORY_CONTRACTID);
   NS_ENSURE_TRUE(cfact, nsnull);
   rv = cfact->CreateCollation(locale, getter_AddRefs(mCollation));
   NS_ENSURE_SUCCESS(rv, nsnull);
 
   return mCollation;
 }
 
+nsIStringBundle *
+nsNavHistory::GetBundle()
+{
+  if (mBundle)
+    return mBundle;
+
+  nsCOMPtr<nsIStringBundleService> bundleService =
+    do_GetService(NS_STRINGBUNDLE_CONTRACTID);
+  NS_ENSURE_TRUE(bundleService, nsnull);
+  nsresult rv = bundleService->CreateBundle(
+      "chrome://places/locale/places.properties",
+      getter_AddRefs(mBundle));
+  NS_ENSURE_SUCCESS(rv, nsnull);
+
+  return mBundle;
+}
+
 // nsICharsetResolver **********************************************************
 
 NS_IMETHODIMP
 nsNavHistory::RequestCharset(nsIWebNavigation* aWebNavigation,
                              nsIChannel* aChannel,
                              PRBool* aWantCharset,
                              nsISupports** aClosure,
                              nsACString& aResult)
--- a/toolkit/components/places/src/nsNavHistory.h
+++ b/toolkit/components/places/src/nsNavHistory.h
@@ -234,20 +234,21 @@ public:
   {
     return mDBConn;
   }
 
   /**
    * These functions return non-owning references to the locale-specific
    * objects for places components.
    */
-  nsIStringBundle* GetBundle()
-    { return mBundle; }
+  nsIStringBundle* GetBundle();
   nsICollation* GetCollation();
   void GetStringFromName(const PRUnichar* aName, nsACString& aResult);
+  void GetAgeInDaysString(PRInt32 aInt, const PRUnichar *aName,
+                          nsACString& aResult);
 
   // returns true if history has been disabled
   PRBool IsHistoryDisabled() { return mExpireDaysMax == 0; }
 
   // get the statement for selecting a history row by URL
   mozIStorageStatement* DBGetURLPageInfo() { return mDBGetURLPageInfo; }
 
   // Constants for the columns returned by the above statement.
@@ -581,19 +582,16 @@ protected:
                                      PRInt32 aQueryIndex,
                                      nsNavHistoryQuery* aQuery,
                                      nsNavHistoryQueryOptions* aOptions);
 
   nsresult ResultsAsList(mozIStorageStatement* statement,
                          nsNavHistoryQueryOptions* aOptions,
                          nsCOMArray<nsNavHistoryResultNode>* aResults);
 
-  void GetAgeInDaysString(PRInt32 aInt, const PRUnichar *aName, 
-                          nsACString& aResult);
-
   void TitleForDomain(const nsCString& domain, nsACString& aTitle);
 
   nsresult SetPageTitleInternal(nsIURI* aURI, const nsAString& aTitle);
 
   nsresult FilterResultSet(nsNavHistoryQueryResultNode *aParentNode,
                            const nsCOMArray<nsNavHistoryResultNode>& aSet,
                            nsCOMArray<nsNavHistoryResultNode>* aFiltered,
                            const nsCOMArray<nsNavHistoryQuery>& aQueries,