diff --git a/dom/ipc/ContentChild.cpp b/dom/ipc/ContentChild.cpp index dfc06abfcb6e..0d3673cd734d 100644 --- a/dom/ipc/ContentChild.cpp +++ b/dom/ipc/ContentChild.cpp @@ -208,11 +208,6 @@ #if defined(XP_MACOSX) # include "nsMacUtilsImpl.h" -# include -// Info.plist key associated with the developer repo path -# define MAC_DEV_REPO_KEY "MozillaDeveloperRepoPath" -// Info.plist key associated with the developer repo object directory -# define MAC_DEV_OBJ_KEY "MozillaDeveloperObjPath" #endif /* XP_MACOSX */ #ifdef MOZ_X11 @@ -1661,7 +1656,7 @@ static bool StartMacOSContentSandbox() { if (mozilla::IsDevelopmentBuild()) { nsCOMPtr repoDir; - rv = mozilla::GetRepoDir(getter_AddRefs(repoDir)); + rv = nsMacUtilsImpl::GetRepoDir(getter_AddRefs(repoDir)); if (NS_FAILED(rv)) { MOZ_CRASH("Failed to get path to repo dir"); } @@ -1670,7 +1665,7 @@ static bool StartMacOSContentSandbox() { info.testingReadPath3.assign(repoDirPath.get()); nsCOMPtr objDir; - rv = mozilla::GetObjDir(getter_AddRefs(objDir)); + rv = nsMacUtilsImpl::GetObjDir(getter_AddRefs(objDir)); if (NS_FAILED(rv)) { MOZ_CRASH("Failed to get path to build object dir"); } @@ -4058,103 +4053,4 @@ bool IsDevelopmentBuild() { } #endif /* !XP_WIN */ -#if defined(XP_MACOSX) -/* - * Helper function to read a string value for a given key from the .app's - * Info.plist. - */ -static nsresult GetStringValueFromBundlePlist(const nsAString& aKey, - nsAutoCString& aValue) { - CFBundleRef mainBundle = CFBundleGetMainBundle(); - if (mainBundle == nullptr) { - return NS_ERROR_FAILURE; - } - - // Read this app's bundle Info.plist as a dictionary - CFDictionaryRef bundleInfoDict = CFBundleGetInfoDictionary(mainBundle); - if (bundleInfoDict == nullptr) { - return NS_ERROR_FAILURE; - } - - nsAutoCString keyAutoCString = NS_ConvertUTF16toUTF8(aKey); - CFStringRef key = CFStringCreateWithCString( - kCFAllocatorDefault, keyAutoCString.get(), kCFStringEncodingUTF8); - if (key == nullptr) { - return NS_ERROR_FAILURE; - } - - CFStringRef value = (CFStringRef)CFDictionaryGetValue(bundleInfoDict, key); - CFRelease(key); - if (value == nullptr) { - return NS_ERROR_FAILURE; - } - - CFIndex valueLength = CFStringGetLength(value); - if (valueLength == 0) { - return NS_ERROR_FAILURE; - } - - const char* valueCString = - CFStringGetCStringPtr(value, kCFStringEncodingUTF8); - if (valueCString) { - aValue.Assign(valueCString); - return NS_OK; - } - - CFIndex maxLength = - CFStringGetMaximumSizeForEncoding(valueLength, kCFStringEncodingUTF8) + 1; - char* valueBuffer = static_cast(moz_xmalloc(maxLength)); - - if (!CFStringGetCString(value, valueBuffer, maxLength, - kCFStringEncodingUTF8)) { - free(valueBuffer); - return NS_ERROR_FAILURE; - } - - aValue.Assign(valueBuffer); - free(valueBuffer); - return NS_OK; -} - -/* - * Helper function for reading a path string from the .app's Info.plist - * and returning a directory object for that path with symlinks resolved. - */ -static nsresult GetDirFromBundlePlist(const nsAString& aKey, nsIFile** aDir) { - nsresult rv; - - nsAutoCString dirPath; - rv = GetStringValueFromBundlePlist(aKey, dirPath); - NS_ENSURE_SUCCESS(rv, rv); - - nsCOMPtr dir; - rv = NS_NewLocalFile(NS_ConvertUTF8toUTF16(dirPath), false, - getter_AddRefs(dir)); - NS_ENSURE_SUCCESS(rv, rv); - - rv = dir->Normalize(); - NS_ENSURE_SUCCESS(rv, rv); - - bool isDirectory = false; - rv = dir->IsDirectory(&isDirectory); - NS_ENSURE_SUCCESS(rv, rv); - if (!isDirectory) { - return NS_ERROR_FILE_NOT_DIRECTORY; - } - - dir.swap(*aDir); - return NS_OK; -} - -nsresult GetRepoDir(nsIFile** aRepoDir) { - MOZ_ASSERT(IsDevelopmentBuild()); - return GetDirFromBundlePlist(NS_LITERAL_STRING(MAC_DEV_REPO_KEY), aRepoDir); -} - -nsresult GetObjDir(nsIFile** aObjDir) { - MOZ_ASSERT(IsDevelopmentBuild()); - return GetDirFromBundlePlist(NS_LITERAL_STRING(MAC_DEV_OBJ_KEY), aObjDir); -} -#endif /* XP_MACOSX */ - } // namespace mozilla diff --git a/dom/ipc/ContentChild.h b/dom/ipc/ContentChild.h index 30d608588de5..12951b0d3b7e 100644 --- a/dom/ipc/ContentChild.h +++ b/dom/ipc/ContentChild.h @@ -57,14 +57,6 @@ using mozilla::loader::PScriptCacheChild; bool IsDevelopmentBuild(); #endif /* !XP_WIN */ -#if defined(XP_MACOSX) -// Return the repo directory and the repo object directory respectively. These -// should only be used on Mac developer builds to determine the path to the -// repo or object directory. -nsresult GetRepoDir(nsIFile** aRepoDir); -nsresult GetObjDir(nsIFile** aObjDir); -#endif /* XP_MACOSX */ - namespace ipc { class URIParams; } // namespace ipc diff --git a/dom/ipc/ContentParent.cpp b/dom/ipc/ContentParent.cpp index 0ae7b2898674..3a4fc7308e87 100644 --- a/dom/ipc/ContentParent.cpp +++ b/dom/ipc/ContentParent.cpp @@ -1989,7 +1989,7 @@ static void CacheSandboxParams(std::vector& aCachedParams) { if (mozilla::IsDevelopmentBuild()) { // Repo dir nsCOMPtr repoDir; - rv = mozilla::GetRepoDir(getter_AddRefs(repoDir)); + rv = nsMacUtilsImpl::GetRepoDir(getter_AddRefs(repoDir)); if (NS_FAILED(rv)) { MOZ_CRASH("Failed to get path to repo dir"); } @@ -1999,7 +1999,7 @@ static void CacheSandboxParams(std::vector& aCachedParams) { // Object dir nsCOMPtr objDir; - rv = mozilla::GetObjDir(getter_AddRefs(objDir)); + rv = nsMacUtilsImpl::GetObjDir(getter_AddRefs(objDir)); if (NS_FAILED(rv)) { MOZ_CRASH("Failed to get path to build object dir"); } diff --git a/netwerk/protocol/res/ExtensionProtocolHandler.cpp b/netwerk/protocol/res/ExtensionProtocolHandler.cpp index 2538cdbddd21..fb96bf4b1755 100644 --- a/netwerk/protocol/res/ExtensionProtocolHandler.cpp +++ b/netwerk/protocol/res/ExtensionProtocolHandler.cpp @@ -52,6 +52,10 @@ # include "WinUtils.h" #endif +#if defined(XP_MACOSX) +# include "nsMacUtilsImpl.h" +#endif + #define EXTENSION_SCHEME "moz-extension" using mozilla::dom::Promise; using mozilla::ipc::FileDescriptor; @@ -612,7 +616,7 @@ Result ExtensionProtocolHandler::DevRepoContains( // On the first invocation, set mDevRepo if (!mAlreadyCheckedDevRepo) { mAlreadyCheckedDevRepo = true; - MOZ_TRY(mozilla::GetRepoDir(getter_AddRefs(mDevRepo))); + MOZ_TRY(nsMacUtilsImpl::GetRepoDir(getter_AddRefs(mDevRepo))); if (MOZ_LOG_TEST(gExtProtocolLog, LogLevel::Debug)) { nsAutoCString repoPath; Unused << mDevRepo->GetNativePath(repoPath); diff --git a/netwerk/protocol/res/moz.build b/netwerk/protocol/res/moz.build index ec6a57069896..d6791156da8d 100644 --- a/netwerk/protocol/res/moz.build +++ b/netwerk/protocol/res/moz.build @@ -35,4 +35,5 @@ FINAL_LIBRARY = 'xul' LOCAL_INCLUDES += [ '/netwerk/base', + '/xpcom/base', ] diff --git a/xpcom/base/nsMacUtilsImpl.cpp b/xpcom/base/nsMacUtilsImpl.cpp index 5aad7864b070..ecec6733ac75 100644 --- a/xpcom/base/nsMacUtilsImpl.cpp +++ b/xpcom/base/nsMacUtilsImpl.cpp @@ -8,6 +8,7 @@ #include "base/command_line.h" #include "mozilla/ClearOnShutdown.h" +#include "mozilla/dom/ContentChild.h" #include "nsDirectoryServiceDefs.h" #include "nsCOMPtr.h" #include "nsIFile.h" @@ -16,7 +17,12 @@ #include "nsXULAppAPI.h" #include "prenv.h" +#if defined(MOZ_SANDBOX) +#include "mozilla/SandboxSettings.h" +#endif + #include +#include #include NS_IMPL_ISUPPORTS(nsMacUtilsImpl, nsIMacUtils) @@ -29,6 +35,11 @@ StaticAutoPtr nsMacUtilsImpl::sCachedAppPath; StaticMutex nsMacUtilsImpl::sCachedAppPathMutex; #endif +// Info.plist key associated with the developer repo path +#define MAC_DEV_REPO_KEY "MozillaDeveloperRepoPath" +// Info.plist key associated with the developer repo object directory +#define MAC_DEV_OBJ_KEY "MozillaDeveloperObjPath" + // Initialize with Unknown until we've checked if TCSM is available to set Atomic nsMacUtilsImpl::sTCSMStatus(TCSM_Unknown); @@ -207,6 +218,92 @@ bool nsMacUtilsImpl::GetAppPath(nsCString& aAppPath) { return true; } +/* + * Helper function to read a string value for a given key from the .app's + * Info.plist. + */ +static nsresult GetStringValueFromBundlePlist(const nsAString& aKey, + nsAutoCString& aValue) { + CFBundleRef mainBundle = CFBundleGetMainBundle(); + if (mainBundle == nullptr) { + return NS_ERROR_FAILURE; + } + + // Read this app's bundle Info.plist as a dictionary + CFDictionaryRef bundleInfoDict = CFBundleGetInfoDictionary(mainBundle); + if (bundleInfoDict == nullptr) { + return NS_ERROR_FAILURE; + } + + nsAutoCString keyAutoCString = NS_ConvertUTF16toUTF8(aKey); + CFStringRef key = CFStringCreateWithCString( + kCFAllocatorDefault, keyAutoCString.get(), kCFStringEncodingUTF8); + if (key == nullptr) { + return NS_ERROR_FAILURE; + } + + CFStringRef value = (CFStringRef)CFDictionaryGetValue(bundleInfoDict, key); + CFRelease(key); + if (value == nullptr) { + return NS_ERROR_FAILURE; + } + + CFIndex valueLength = CFStringGetLength(value); + if (valueLength == 0) { + return NS_ERROR_FAILURE; + } + + const char* valueCString = + CFStringGetCStringPtr(value, kCFStringEncodingUTF8); + if (valueCString) { + aValue.Assign(valueCString); + return NS_OK; + } + + CFIndex maxLength = + CFStringGetMaximumSizeForEncoding(valueLength, kCFStringEncodingUTF8) + 1; + char* valueBuffer = static_cast(moz_xmalloc(maxLength)); + + if (!CFStringGetCString(value, valueBuffer, maxLength, + kCFStringEncodingUTF8)) { + free(valueBuffer); + return NS_ERROR_FAILURE; + } + + aValue.Assign(valueBuffer); + free(valueBuffer); + return NS_OK; +} +/* + * Helper function for reading a path string from the .app's Info.plist + * and returning a directory object for that path with symlinks resolved. + */ +static nsresult GetDirFromBundlePlist(const nsAString& aKey, nsIFile** aDir) { + nsresult rv; + + nsAutoCString dirPath; + rv = GetStringValueFromBundlePlist(aKey, dirPath); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr dir; + rv = NS_NewLocalFile(NS_ConvertUTF8toUTF16(dirPath), false, + getter_AddRefs(dir)); + NS_ENSURE_SUCCESS(rv, rv); + + rv = dir->Normalize(); + NS_ENSURE_SUCCESS(rv, rv); + + bool isDirectory = false; + rv = dir->IsDirectory(&isDirectory); + NS_ENSURE_SUCCESS(rv, rv); + if (!isDirectory) { + return NS_ERROR_FILE_NOT_DIRECTORY; + } + + dir.swap(*aDir); + return NS_OK; +} + # if defined(DEBUG) // If XPCOM_MEM_BLOAT_LOG or XPCOM_MEM_LEAK_LOG is set to a log file // path, return the path to the parent directory (where sibling log @@ -315,3 +412,105 @@ uint32_t nsMacUtilsImpl::GetPhysicalCPUCount() { } return oldVal; } + +/* + * Helper function to read a string value for a given key from the .app's + * Info.plist. + */ +static nsresult GetStringValueFromBundlePlist(const nsAString& aKey, + nsAutoCString& aValue) { + CFBundleRef mainBundle = CFBundleGetMainBundle(); + if (mainBundle == nullptr) { + return NS_ERROR_FAILURE; + } + + // Read this app's bundle Info.plist as a dictionary + CFDictionaryRef bundleInfoDict = CFBundleGetInfoDictionary(mainBundle); + if (bundleInfoDict == nullptr) { + return NS_ERROR_FAILURE; + } + + nsAutoCString keyAutoCString = NS_ConvertUTF16toUTF8(aKey); + CFStringRef key = CFStringCreateWithCString( + kCFAllocatorDefault, keyAutoCString.get(), kCFStringEncodingUTF8); + if (key == nullptr) { + return NS_ERROR_FAILURE; + } + + CFStringRef value = (CFStringRef)CFDictionaryGetValue(bundleInfoDict, key); + CFRelease(key); + if (value == nullptr) { + return NS_ERROR_FAILURE; + } + + CFIndex valueLength = CFStringGetLength(value); + if (valueLength == 0) { + return NS_ERROR_FAILURE; + } + + const char* valueCString = + CFStringGetCStringPtr(value, kCFStringEncodingUTF8); + if (valueCString) { + aValue.Assign(valueCString); + return NS_OK; + } + + CFIndex maxLength = + CFStringGetMaximumSizeForEncoding(valueLength, kCFStringEncodingUTF8) + 1; + char* valueBuffer = static_cast(moz_xmalloc(maxLength)); + + if (!CFStringGetCString(value, valueBuffer, maxLength, + kCFStringEncodingUTF8)) { + free(valueBuffer); + return NS_ERROR_FAILURE; + } + + aValue.Assign(valueBuffer); + free(valueBuffer); + return NS_OK; +} + +/* + * Helper function for reading a path string from the .app's Info.plist + * and returning a directory object for that path with symlinks resolved. + */ +static nsresult GetDirFromBundlePlist(const nsAString& aKey, nsIFile** aDir) { + nsresult rv; + + nsAutoCString dirPath; + rv = GetStringValueFromBundlePlist(aKey, dirPath); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr dir; + rv = NS_NewLocalFile(NS_ConvertUTF8toUTF16(dirPath), false, + getter_AddRefs(dir)); + NS_ENSURE_SUCCESS(rv, rv); + + rv = dir->Normalize(); + NS_ENSURE_SUCCESS(rv, rv); + + bool isDirectory = false; + rv = dir->IsDirectory(&isDirectory); + NS_ENSURE_SUCCESS(rv, rv); + if (!isDirectory) { + return NS_ERROR_FILE_NOT_DIRECTORY; + } + + dir.swap(*aDir); + return NS_OK; +} + +nsresult nsMacUtilsImpl::GetRepoDir(nsIFile** aRepoDir) { +#if defined(MOZ_SANDBOX) + MOZ_ASSERT(mozilla::IsDevelopmentBuild()); +#endif + return GetDirFromBundlePlist(NS_LITERAL_STRING(MAC_DEV_REPO_KEY), aRepoDir); +} + +nsresult nsMacUtilsImpl::GetObjDir(nsIFile** aObjDir) { +#if defined(MOZ_SANDBOX) + MOZ_ASSERT(mozilla::IsDevelopmentBuild()); +#endif + return GetDirFromBundlePlist(NS_LITERAL_STRING(MAC_DEV_OBJ_KEY), aObjDir); +} + diff --git a/xpcom/base/nsMacUtilsImpl.h b/xpcom/base/nsMacUtilsImpl.h index cdbd4aad3758..1fc9254bb9c3 100644 --- a/xpcom/base/nsMacUtilsImpl.h +++ b/xpcom/base/nsMacUtilsImpl.h @@ -25,9 +25,14 @@ class nsMacUtilsImpl final : public nsIMacUtils { nsMacUtilsImpl() {} + // Return the repo directory and the repo object directory respectively. + // These should only be used on Mac developer builds to determine the path + // to the repo or object directory. + static nsresult GetRepoDir(nsIFile** aRepoDir); + static nsresult GetObjDir(nsIFile** aObjDir); + #if defined(MOZ_SANDBOX) static bool GetAppPath(nsCString& aAppPath); - # ifdef DEBUG static nsresult GetBloatLogDir(nsCString& aDirectoryPath); static nsresult GetDirectoryPath(const char* aPath,