Bug 1799470 - Eliminate dead code around mAppProvider + cleanup r=nika

Differential Revision: https://phabricator.services.mozilla.com/D165415
This commit is contained in:
Chris Martin 2023-01-18 15:46:10 +00:00
parent 5e33cb17af
commit c6d3e364c7
3 changed files with 37 additions and 153 deletions

View File

@ -205,7 +205,7 @@ bool ContentProcess::Init(int aArgc, char* aArgv[]) {
nsCOMPtr<nsIFile> xpcomAppDir = appDirArg ? appDirArg : greDir; nsCOMPtr<nsIFile> xpcomAppDir = appDirArg ? appDirArg : greDir;
rv = mDirProvider.Initialize(xpcomAppDir, greDir, nullptr); rv = mDirProvider.Initialize(xpcomAppDir, greDir);
if (NS_FAILED(rv)) { if (NS_FAILED(rv)) {
return false; return false;
} }

View File

@ -131,9 +131,7 @@ static const char* GetAppVendor() {
} }
#endif #endif
nsXREDirProvider::nsXREDirProvider() : mProfileNotified(false) { nsXREDirProvider::nsXREDirProvider() { gDirServiceProvider = this; }
gDirServiceProvider = this;
}
nsXREDirProvider::~nsXREDirProvider() { nsXREDirProvider::~nsXREDirProvider() {
gDirServiceProvider = nullptr; gDirServiceProvider = nullptr;
@ -148,35 +146,16 @@ already_AddRefed<nsXREDirProvider> nsXREDirProvider::GetSingleton() {
return do_AddRef(gDirServiceProvider); return do_AddRef(gDirServiceProvider);
} }
nsresult nsXREDirProvider::Initialize( nsresult nsXREDirProvider::Initialize(nsIFile* aXULAppDir, nsIFile* aGREDir) {
nsIFile* aXULAppDir, nsIFile* aGREDir,
nsIDirectoryServiceProvider* aAppProvider) {
NS_ENSURE_ARG(aXULAppDir); NS_ENSURE_ARG(aXULAppDir);
NS_ENSURE_ARG(aGREDir); NS_ENSURE_ARG(aGREDir);
mAppProvider = aAppProvider;
mXULAppDir = aXULAppDir; mXULAppDir = aXULAppDir;
mGREDir = aGREDir; mGREDir = aGREDir;
nsCOMPtr<nsIFile> binaryPath; nsCOMPtr<nsIFile> binaryPath;
nsresult rv = XRE_GetBinaryPath(getter_AddRefs(binaryPath)); nsresult rv = XRE_GetBinaryPath(getter_AddRefs(binaryPath));
if (NS_FAILED(rv)) return rv; NS_ENSURE_SUCCESS(rv, rv);
rv = binaryPath->GetParent(getter_AddRefs(mGREBinDir)); return binaryPath->GetParent(getter_AddRefs(mGREBinDir));
if (NS_FAILED(rv)) return rv;
if (!mProfileDir) {
nsCOMPtr<nsIDirectoryServiceProvider> app(mAppProvider);
if (app) {
bool per = false;
app->GetFile(NS_APP_USER_PROFILE_50_DIR, &per,
getter_AddRefs(mProfileDir));
NS_ASSERTION(per, "NS_APP_USER_PROFILE_50_DIR must be persistent!");
NS_ASSERTION(
mProfileDir,
"NS_APP_USER_PROFILE_50_DIR not defined! This shouldn't happen!");
}
}
return NS_OK;
} }
nsresult nsXREDirProvider::SetProfile(nsIFile* aDir, nsIFile* aLocalDir) { nsresult nsXREDirProvider::SetProfile(nsIFile* aDir, nsIFile* aLocalDir) {
@ -330,37 +309,19 @@ nsXREDirProvider::GetFile(const char* aProperty, bool* aPersistent,
nsIFile** aFile) { nsIFile** aFile) {
nsresult rv; nsresult rv;
bool gettingProfile = false;
if (!strcmp(aProperty, NS_APP_USER_PROFILE_LOCAL_50_DIR)) { if (!strcmp(aProperty, NS_APP_USER_PROFILE_LOCAL_50_DIR)) {
// If XRE_NotifyProfile hasn't been called, don't fall through to NS_ENSURE_TRUE(mProfileNotified, NS_ERROR_FAILURE);
// mAppProvider on the profile keys.
if (!mProfileNotified) return NS_ERROR_FAILURE;
if (mProfileLocalDir) return mProfileLocalDir->Clone(aFile); if (mProfileLocalDir) {
return mProfileLocalDir->Clone(aFile);
}
if (mAppProvider) NS_ENSURE_TRUE(mProfileDir, NS_ERROR_FAILURE);
return mAppProvider->GetFile(aProperty, aPersistent, aFile); return mProfileDir->Clone(aFile);
// This falls through to the case below
gettingProfile = true;
} }
if (!strcmp(aProperty, NS_APP_USER_PROFILE_50_DIR) || gettingProfile) { if (!strcmp(aProperty, NS_APP_USER_PROFILE_50_DIR)) {
if (!mProfileNotified) return NS_ERROR_FAILURE; NS_ENSURE_TRUE(mProfileDir && mProfileNotified, NS_ERROR_FAILURE);
return mProfileDir->Clone(aFile);
if (mProfileDir) return mProfileDir->Clone(aFile);
if (mAppProvider)
return mAppProvider->GetFile(aProperty, aPersistent, aFile);
// If we don't succeed here, bail early so that we aren't reentrant
// through the "GetProfileDir" call below.
return NS_ERROR_FAILURE;
}
if (mAppProvider) {
rv = mAppProvider->GetFile(aProperty, aPersistent, aFile);
if (NS_SUCCEEDED(rv) && *aFile) return rv;
} }
*aPersistent = true; *aPersistent = true;
@ -447,10 +408,6 @@ nsXREDirProvider::GetFile(const char* aProperty, bool* aPersistent,
if (mProfileLocalDir) return mProfileLocalDir->Clone(aFile); if (mProfileLocalDir) return mProfileLocalDir->Clone(aFile);
if (mProfileDir) return mProfileDir->Clone(aFile); if (mProfileDir) return mProfileDir->Clone(aFile);
if (mAppProvider)
return mAppProvider->GetFile(NS_APP_PROFILE_DIR_STARTUP, aPersistent,
aFile);
} }
#if defined(XP_UNIX) || defined(XP_MACOSX) #if defined(XP_UNIX) || defined(XP_MACOSX)
else if (!strcmp(aProperty, XRE_SYS_LOCAL_EXTENSION_PARENT_DIR)) { else if (!strcmp(aProperty, XRE_SYS_LOCAL_EXTENSION_PARENT_DIR)) {
@ -524,8 +481,6 @@ nsXREDirProvider::GetFile(const char* aProperty, bool* aPersistent,
return NS_OK; return NS_OK;
} }
bool ensureFilePermissions = false;
if (NS_SUCCEEDED(GetProfileDir(getter_AddRefs(file)))) { if (NS_SUCCEEDED(GetProfileDir(getter_AddRefs(file)))) {
if (!strcmp(aProperty, NS_APP_PREFS_50_DIR)) { if (!strcmp(aProperty, NS_APP_PREFS_50_DIR)) {
rv = NS_OK; rv = NS_OK;
@ -544,23 +499,13 @@ nsXREDirProvider::GetFile(const char* aProperty, bool* aPersistent,
} }
} }
} }
if (NS_FAILED(rv) || !file) return NS_ERROR_FAILURE;
if (ensureFilePermissions) { if (NS_SUCCEEDED(rv) && file) {
bool fileToEnsureExists; file.forget(aFile);
bool isWritable; return NS_OK;
if (NS_SUCCEEDED(file->Exists(&fileToEnsureExists)) && fileToEnsureExists &&
NS_SUCCEEDED(file->IsWritable(&isWritable)) && !isWritable) {
uint32_t permissions;
if (NS_SUCCEEDED(file->GetPermissions(&permissions))) {
rv = file->SetPermissions(permissions | 0600);
NS_ASSERTION(NS_SUCCEEDED(rv), "failed to ensure file permissions");
}
}
} }
file.forget(aFile); return NS_ERROR_FAILURE;
return NS_OK;
} }
static void LoadDirIntoArray(nsIFile* dir, const char* const* aAppendList, static void LoadDirIntoArray(nsIFile* dir, const char* const* aAppendList,
@ -581,40 +526,6 @@ static void LoadDirIntoArray(nsIFile* dir, const char* const* aAppendList,
} }
} }
NS_IMETHODIMP
nsXREDirProvider::GetFiles(const char* aProperty,
nsISimpleEnumerator** aResult) {
nsresult rv;
nsCOMPtr<nsISimpleEnumerator> appEnum;
nsCOMPtr<nsIDirectoryServiceProvider2> appP2(do_QueryInterface(mAppProvider));
if (appP2) {
rv = appP2->GetFiles(aProperty, getter_AddRefs(appEnum));
if (NS_FAILED(rv)) {
appEnum = nullptr;
} else if (rv != NS_SUCCESS_AGGREGATE_RESULT) {
appEnum.forget(aResult);
return NS_OK;
}
}
nsCOMPtr<nsISimpleEnumerator> xreEnum;
rv = GetFilesInternal(aProperty, getter_AddRefs(xreEnum));
if (NS_FAILED(rv)) {
if (appEnum) {
appEnum.forget(aResult);
return NS_SUCCESS_AGGREGATE_RESULT;
}
return rv;
}
rv = NS_NewUnionEnumerator(aResult, appEnum, xreEnum);
if (NS_FAILED(rv)) return rv;
return NS_SUCCESS_AGGREGATE_RESULT;
}
#if defined(MOZ_SANDBOX) #if defined(MOZ_SANDBOX)
static const char* GetProcessTempBaseDirKey() { static const char* GetProcessTempBaseDirKey() {
@ -788,9 +699,10 @@ static const char* const kAppendBackgroundTasksPrefDir[] = {
"defaults", "backgroundtasks", nullptr}; "defaults", "backgroundtasks", nullptr};
#endif #endif
nsresult nsXREDirProvider::GetFilesInternal(const char* aProperty, NS_IMETHODIMP
nsISimpleEnumerator** aResult) { nsXREDirProvider::GetFiles(const char* aProperty,
nsresult rv = NS_OK; nsISimpleEnumerator** aResult) {
nsresult rv = NS_ERROR_FAILURE;
*aResult = nullptr; *aResult = nullptr;
if (!strcmp(aProperty, NS_APP_PREFS_DEFAULTS_DIR_LIST)) { if (!strcmp(aProperty, NS_APP_PREFS_DEFAULTS_DIR_LIST)) {
@ -814,10 +726,10 @@ nsresult nsXREDirProvider::GetFilesInternal(const char* aProperty,
LoadDirIntoArray(mXULAppDir, kAppendChromeDir, directories); LoadDirIntoArray(mXULAppDir, kAppendChromeDir, directories);
rv = NS_NewArrayEnumerator(aResult, directories, NS_GET_IID(nsIFile)); rv = NS_NewArrayEnumerator(aResult, directories, NS_GET_IID(nsIFile));
} else }
rv = NS_ERROR_FAILURE; NS_ENSURE_SUCCESS(rv, rv);
return rv; return NS_SUCCESS_AGGREGATE_RESULT;
} }
NS_IMETHODIMP NS_IMETHODIMP
@ -1290,34 +1202,16 @@ nsresult nsXREDirProvider::GetUpdateRootDir(nsIFile** aResult,
} }
nsresult nsXREDirProvider::GetProfileStartupDir(nsIFile** aResult) { nsresult nsXREDirProvider::GetProfileStartupDir(nsIFile** aResult) {
if (mProfileDir) return mProfileDir->Clone(aResult); NS_ENSURE_TRUE(mProfileDir, NS_ERROR_FAILURE);
return mProfileDir->Clone(aResult);
if (mAppProvider) {
nsCOMPtr<nsIFile> needsclone;
bool dummy;
nsresult rv = mAppProvider->GetFile(NS_APP_PROFILE_DIR_STARTUP, &dummy,
getter_AddRefs(needsclone));
if (NS_SUCCEEDED(rv)) return needsclone->Clone(aResult);
}
return NS_ERROR_FAILURE;
} }
nsresult nsXREDirProvider::GetProfileDir(nsIFile** aResult) { nsresult nsXREDirProvider::GetProfileDir(nsIFile** aResult) {
if (mProfileDir) { if (mProfileDir) {
if (!mProfileNotified) return NS_ERROR_FAILURE; NS_ENSURE_TRUE(mProfileNotified, NS_ERROR_FAILURE);
return mProfileDir->Clone(aResult); return mProfileDir->Clone(aResult);
} }
if (mAppProvider) {
nsCOMPtr<nsIFile> needsclone;
bool dummy;
nsresult rv = mAppProvider->GetFile(NS_APP_USER_PROFILE_50_DIR, &dummy,
getter_AddRefs(needsclone));
if (NS_SUCCEEDED(rv)) return needsclone->Clone(aResult);
}
return NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, aResult); return NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, aResult);
} }

View File

@ -43,9 +43,7 @@ class nsXREDirProvider final : public nsIDirectoryServiceProvider2,
nsXREDirProvider(); nsXREDirProvider();
// if aXULAppDir is null, use gArgv[0] nsresult Initialize(nsIFile* aXULAppDir, nsIFile* aGREDir);
nsresult Initialize(nsIFile* aXULAppDir, nsIFile* aGREDir,
nsIDirectoryServiceProvider* aAppProvider = nullptr);
~nsXREDirProvider(); ~nsXREDirProvider();
static already_AddRefed<nsXREDirProvider> GetSingleton(); static already_AddRefed<nsXREDirProvider> GetSingleton();
@ -60,10 +58,6 @@ class nsXREDirProvider final : public nsIDirectoryServiceProvider2,
nsresult GetLegacyInstallHash(nsAString& aPathHash); nsresult GetLegacyInstallHash(nsAString& aPathHash);
// We only set the profile dir, we don't ensure that it exists;
// that is the responsibility of the toolkit profile service.
// We also don't fire profile-changed notifications... that is
// the responsibility of the apprunner.
nsresult SetProfile(nsIFile* aProfileDir, nsIFile* aProfileLocalDir); nsresult SetProfile(nsIFile* aProfileDir, nsIFile* aProfileLocalDir);
void InitializeUserPrefs(); void InitializeUserPrefs();
@ -78,7 +72,6 @@ class nsXREDirProvider final : public nsIDirectoryServiceProvider2,
return GetUserDataDirectory(aFile, true); return GetUserDataDirectory(aFile, true);
} }
// GetUserDataDirectory gets the profile path from gAppData.
static nsresult GetUserDataDirectory(nsIFile** aFile, bool aLocal); static nsresult GetUserDataDirectory(nsIFile** aFile, bool aLocal);
/* make sure you clone it, if you need to do stuff to it */ /* make sure you clone it, if you need to do stuff to it */
@ -104,20 +97,19 @@ class nsXREDirProvider final : public nsIDirectoryServiceProvider2,
nsresult GetUpdateRootDir(nsIFile** aResult, bool aGetOldLocation = false); nsresult GetUpdateRootDir(nsIFile** aResult, bool aGetOldLocation = false);
/** /**
* Get the profile startup directory as determined by this class or by * Get the profile startup directory.
* mAppProvider. This method may be called before XPCOM is started. aResult * This method may be called before XPCOM is started. aResult
* is a clone, it may be modified. * is a clone, it may be modified.
*/ */
nsresult GetProfileStartupDir(nsIFile** aResult); nsresult GetProfileStartupDir(nsIFile** aResult);
/** /**
* Get the profile directory as determined by this class or by an * Get the profile directory. Only call this method
* embedder-provided XPCOM directory provider. Only call this method
* when XPCOM is initialized! aResult is a clone, it may be modified. * when XPCOM is initialized! aResult is a clone, it may be modified.
*/ */
nsresult GetProfileDir(nsIFile** aResult); nsresult GetProfileDir(nsIFile** aResult);
protected: private:
nsresult GetFilesInternal(const char* aProperty, nsresult GetFilesInternal(const char* aProperty,
nsISimpleEnumerator** aResult); nsISimpleEnumerator** aResult);
static nsresult GetUserDataDirectoryHome(nsIFile** aFile, bool aLocal); static nsresult GetUserDataDirectoryHome(nsIFile** aFile, bool aLocal);
@ -137,6 +129,9 @@ class nsXREDirProvider final : public nsIDirectoryServiceProvider2,
// delimiters. // delimiters.
static inline nsresult AppendProfileString(nsIFile* aFile, const char* aPath); static inline nsresult AppendProfileString(nsIFile* aFile, const char* aPath);
static nsresult SetUserDataProfileDirectory(nsCOMPtr<nsIFile>& aFile,
bool aLocal);
#if defined(MOZ_SANDBOX) #if defined(MOZ_SANDBOX)
// Load the temp directory for sandboxed content processes // Load the temp directory for sandboxed content processes
nsresult LoadContentProcessTempDir(); nsresult LoadContentProcessTempDir();
@ -144,7 +139,6 @@ class nsXREDirProvider final : public nsIDirectoryServiceProvider2,
void Append(nsIFile* aDirectory); void Append(nsIFile* aDirectory);
nsCOMPtr<nsIDirectoryServiceProvider> mAppProvider;
// On OSX, mGREDir points to .app/Contents/Resources // On OSX, mGREDir points to .app/Contents/Resources
nsCOMPtr<nsIFile> mGREDir; nsCOMPtr<nsIFile> mGREDir;
// On OSX, mGREBinDir points to .app/Contents/MacOS // On OSX, mGREBinDir points to .app/Contents/MacOS
@ -153,16 +147,12 @@ class nsXREDirProvider final : public nsIDirectoryServiceProvider2,
nsCOMPtr<nsIFile> mXULAppDir; nsCOMPtr<nsIFile> mXULAppDir;
nsCOMPtr<nsIFile> mProfileDir; nsCOMPtr<nsIFile> mProfileDir;
nsCOMPtr<nsIFile> mProfileLocalDir; nsCOMPtr<nsIFile> mProfileLocalDir;
bool mProfileNotified; bool mProfileNotified = false;
bool mPrefsInitialized = false; bool mPrefsInitialized = false;
#if defined(MOZ_SANDBOX) #if defined(MOZ_SANDBOX)
nsCOMPtr<nsIFile> mContentTempDir; nsCOMPtr<nsIFile> mContentTempDir;
nsCOMPtr<nsIFile> mContentProcessSandboxTempDir; nsCOMPtr<nsIFile> mContentProcessSandboxTempDir;
#endif #endif
private:
static nsresult SetUserDataProfileDirectory(nsCOMPtr<nsIFile>& aFile,
bool aLocal);
}; };
#endif #endif