From a75ea6d23b556e2dd6c0bc2cc842657988751220 Mon Sep 17 00:00:00 2001 From: Shawn Wilsher Date: Mon, 27 Oct 2008 18:52:22 -0400 Subject: [PATCH] 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 --- .../components/places/src/nsNavBookmarks.cpp | 29 ++++++-------- .../components/places/src/nsNavBookmarks.h | 3 -- .../components/places/src/nsNavHistory.cpp | 40 +++++++++++++------ toolkit/components/places/src/nsNavHistory.h | 8 ++-- 4 files changed, 43 insertions(+), 37 deletions(-) diff --git a/toolkit/components/places/src/nsNavBookmarks.cpp b/toolkit/components/places/src/nsNavBookmarks.cpp index 07b4c05d8e1..56180677bd3 100644 --- a/toolkit/components/places/src/nsNavBookmarks.cpp +++ b/toolkit/components/places/src/nsNavBookmarks.cpp @@ -105,20 +105,12 @@ NS_IMPL_ISUPPORTS3(nsNavBookmarks, nsresult nsNavBookmarks::Init() { - nsresult rv; - nsCOMPtr 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(); @@ -546,34 +538,37 @@ nsNavBookmarks::InitRoots() 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); diff --git a/toolkit/components/places/src/nsNavBookmarks.h b/toolkit/components/places/src/nsNavBookmarks.h index 2b4a55cdf79..f968832d5f7 100644 --- a/toolkit/components/places/src/nsNavBookmarks.h +++ b/toolkit/components/places/src/nsNavBookmarks.h @@ -41,7 +41,6 @@ #include "nsINavBookmarksService.h" #include "nsIAnnotationService.h" -#include "nsIStringBundle.h" #include "nsITransaction.h" #include "nsNavHistory.h" #include "nsNavHistoryResult.h" // need for Int64 hashtable @@ -213,8 +212,6 @@ private: nsCOMPtr mDBGetKeywordForBookmark; nsCOMPtr mDBGetURIForKeyword; - nsCOMPtr mBundle; - class RemoveFolderTransaction : public nsITransaction { public: RemoveFolderTransaction(PRInt64 aID) : mID(aID) {} diff --git a/toolkit/components/places/src/nsNavHistory.cpp b/toolkit/components/places/src/nsNavHistory.cpp index 7935c0fa94d..ce2ac76a311 100644 --- a/toolkit/components/places/src/nsNavHistory.cpp +++ b/toolkit/components/places/src/nsNavHistory.cpp @@ -453,15 +453,6 @@ nsNavHistory::Init() mLastSessionID = 1; } - // string bundle for localization - nsCOMPtr 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(); @@ -6579,12 +6570,16 @@ nsNavHistory::TitleForDomain(const nsCString& domain, nsACString& 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 @@ -6594,8 +6589,12 @@ nsNavHistory::GetAgeInDaysString(PRInt32 aInt, const PRUnichar *aName, nsACStrin 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 @@ -7389,6 +7388,23 @@ nsNavHistory::GetCollation() return mCollation; } +nsIStringBundle * +nsNavHistory::GetBundle() +{ + if (mBundle) + return mBundle; + + nsCOMPtr 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 diff --git a/toolkit/components/places/src/nsNavHistory.h b/toolkit/components/places/src/nsNavHistory.h index 856186c48d4..a983d1925d1 100644 --- a/toolkit/components/places/src/nsNavHistory.h +++ b/toolkit/components/places/src/nsNavHistory.h @@ -247,10 +247,11 @@ public: * 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 || InPrivateBrowsingMode(); } @@ -609,9 +610,6 @@ protected: nsNavHistoryQueryOptions* aOptions, nsCOMArray* aResults); - void GetAgeInDaysString(PRInt32 aInt, const PRUnichar *aName, - nsACString& aResult); - void TitleForDomain(const nsCString& domain, nsACString& aTitle); nsresult SetPageTitleInternal(nsIURI* aURI, const nsAString& aTitle);