From 0df4377cec656df8af92ea20079cd920c48fdda9 Mon Sep 17 00:00:00 2001 From: Dave Townsend Date: Tue, 5 Jul 2022 14:49:53 +0000 Subject: [PATCH] Bug 1778082: Ensure nsToolkitProfileService is a singleton regardless of how it is accessed. r=kmag Differential Revision: https://phabricator.services.mozilla.com/D151020 --- toolkit/profile/nsToolkitProfileService.cpp | 20 ++++++++++---------- toolkit/profile/nsToolkitProfileService.h | 7 ++++--- toolkit/xre/components.conf | 3 ++- toolkit/xre/nsAppRunner.cpp | 9 ++------- toolkit/xre/nsAppRunner.h | 2 -- 5 files changed, 18 insertions(+), 23 deletions(-) diff --git a/toolkit/profile/nsToolkitProfileService.cpp b/toolkit/profile/nsToolkitProfileService.cpp index b788d383c201..156960935c8d 100644 --- a/toolkit/profile/nsToolkitProfileService.cpp +++ b/toolkit/profile/nsToolkitProfileService.cpp @@ -497,7 +497,6 @@ nsToolkitProfileService::nsToolkitProfileService() #ifdef MOZ_DEV_EDITION mUseDevEditionProfile = true; #endif - gService = this; } nsToolkitProfileService::~nsToolkitProfileService() { @@ -2071,17 +2070,18 @@ nsToolkitProfileService::Flush() { return NS_OK; } -nsresult NS_NewToolkitProfileService(nsToolkitProfileService** aResult) { - nsToolkitProfileService* profileService = new nsToolkitProfileService(); - nsresult rv = profileService->Init(); - if (NS_FAILED(rv)) { - NS_ERROR("nsToolkitProfileService::Init failed!"); - delete profileService; - return rv; +already_AddRefed NS_GetToolkitProfileService() { + if (!nsToolkitProfileService::gService) { + nsToolkitProfileService::gService = new nsToolkitProfileService(); + nsresult rv = nsToolkitProfileService::gService->Init(); + if (NS_FAILED(rv)) { + NS_ERROR("nsToolkitProfileService::Init failed!"); + delete nsToolkitProfileService::gService; + return nullptr; + } } - NS_ADDREF(*aResult = profileService); - return NS_OK; + return do_AddRef(nsToolkitProfileService::gService); } nsresult XRE_GetFileFromPath(const char* aPath, nsIFile** aResult) { diff --git a/toolkit/profile/nsToolkitProfileService.h b/toolkit/profile/nsToolkitProfileService.h index 97ef74855956..dea1af012440 100644 --- a/toolkit/profile/nsToolkitProfileService.h +++ b/toolkit/profile/nsToolkitProfileService.h @@ -80,9 +80,8 @@ class nsToolkitProfileService final : public nsIToolkitProfileService { private: friend class nsToolkitProfile; - friend nsresult NS_NewToolkitProfileService(nsToolkitProfileService**); - friend nsresult mozilla::xpcom::CreateInstanceImpl( - mozilla::xpcom::ModuleID aID, const nsIID& aIID, void** aResult); + friend already_AddRefed + NS_GetToolkitProfileService(); nsToolkitProfileService(); ~nsToolkitProfileService(); @@ -173,4 +172,6 @@ class nsToolkitProfileService final : public nsIToolkitProfileService { }; }; +already_AddRefed NS_GetToolkitProfileService(); + #endif diff --git a/toolkit/xre/components.conf b/toolkit/xre/components.conf index 5f23dc4a13b0..626fa65e9a07 100644 --- a/toolkit/xre/components.conf +++ b/toolkit/xre/components.conf @@ -62,8 +62,9 @@ Classes = [ { 'cid': '{5f5e59ce-27bc-47eb-9d1f-b09ca9049836}', 'contract_ids': ['@mozilla.org/toolkit/profile-service;1'], + 'singleton': True, + 'constructor': 'NS_GetToolkitProfileService', 'type': 'nsToolkitProfileService', - 'init_method': 'Init', 'headers': ['/toolkit/profile/nsToolkitProfileService.h'], }, ] diff --git a/toolkit/xre/nsAppRunner.cpp b/toolkit/xre/nsAppRunner.cpp index 47ddc99374e5..45d81edd2fcb 100644 --- a/toolkit/xre/nsAppRunner.cpp +++ b/toolkit/xre/nsAppRunner.cpp @@ -4892,13 +4892,8 @@ int XREMain::XRE_mainStartup(bool* aExitFlag) { } #endif - rv = NS_NewToolkitProfileService(getter_AddRefs(mProfileSvc)); - if (rv == NS_ERROR_FILE_ACCESS_DENIED) { - PR_fprintf(PR_STDERR, - "Error: Access was denied while trying to open files in " - "your profile directory.\n"); - } - if (NS_FAILED(rv)) { + mProfileSvc = NS_GetToolkitProfileService(); + if (!mProfileSvc) { // We failed to choose or create profile - notify user and quit ProfileMissingDialog(mNativeApp); return 1; diff --git a/toolkit/xre/nsAppRunner.h b/toolkit/xre/nsAppRunner.h index a09d665e3aa0..e364f81a9f74 100644 --- a/toolkit/xre/nsAppRunner.h +++ b/toolkit/xre/nsAppRunner.h @@ -78,8 +78,6 @@ int32_t CompareCompatVersions(const nsACString& aOldCompatVersion, nsresult NS_CreateNativeAppSupport(nsINativeAppSupport** aResult); already_AddRefed NS_GetNativeAppSupport(); -nsresult NS_NewToolkitProfileService(nsIToolkitProfileService** aResult); - /** * Try to acquire exclusive access to the specified profile directory. *