Bug 327331 - nsNavHistory::Init allows improper initialization. r=bsmedberg

This commit is contained in:
sdwilsh@shawnwilsher.com 2008-02-17 19:36:32 -08:00
parent 0a801009ce
commit 8c0fc2862e
3 changed files with 36 additions and 10 deletions

View File

@ -293,7 +293,25 @@ static const char* gXpcomShutdown = "xpcom-shutdown";
const char nsNavHistory::kAnnotationPreviousEncoding[] = "history/encoding";
nsNavHistory* nsNavHistory::gHistoryService;
nsNavHistory *nsNavHistory::gHistoryService = nsnull;
nsNavHistory *
nsNavHistory::GetSingleton()
{
if (gHistoryService) {
NS_ADDREF(gHistoryService);
return gHistoryService;
}
gHistoryService = new nsNavHistory();
if (gHistoryService) {
NS_ADDREF(gHistoryService);
if (NS_FAILED(gHistoryService->Init()))
NS_RELEASE(gHistoryService);
}
return gHistoryService;
}
// nsNavHistory::nsNavHistory

View File

@ -139,6 +139,15 @@ public:
NS_DECL_NSIAUTOCOMPLETESIMPLERESULTLISTENER
#endif
/**
* Obtains the nsNavHistory object.
*/
static nsNavHistory *GetSingleton();
/**
* Initializes the nsNavHistory object. This should only be called once.
*/
nsresult Init();
/**
@ -148,15 +157,13 @@ public:
*/
static nsNavHistory* GetHistoryService()
{
if (! gHistoryService) {
nsresult rv;
nsCOMPtr<nsINavHistoryService> serv(do_GetService("@mozilla.org/browser/nav-history-service;1", &rv));
NS_ENSURE_SUCCESS(rv, nsnull);
if (gHistoryService)
return gHistoryService;
nsCOMPtr<nsINavHistoryService> serv =
do_GetService("@mozilla.org/browser/nav-history-service;1");
NS_ENSURE_TRUE(serv, nsnull);
// our constructor should have set the static variable. If it didn't,
// something is wrong.
NS_ASSERTION(gHistoryService, "History service creation failed");
}
return gHistoryService;
}

View File

@ -7,7 +7,8 @@
#include "nsFaviconService.h"
#include "nsDocShellCID.h"
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsNavHistory, Init)
NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(nsNavHistory,
nsNavHistory::GetSingleton)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsAnnoProtocolHandler)
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsAnnotationService, Init)
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsNavBookmarks, Init)