diff --git a/dom/ipc/ContentChild.cpp b/dom/ipc/ContentChild.cpp index 3c6521461907..857ddafc3cd4 100644 --- a/dom/ipc/ContentChild.cpp +++ b/dom/ipc/ContentChild.cpp @@ -194,6 +194,7 @@ #endif #if defined(XP_MACOSX) +#include "nsMacUtilsImpl.h" #include // Info.plist key associated with the developer repo path #define MAC_DEV_REPO_KEY "MozillaDeveloperRepoPath" @@ -1523,110 +1524,6 @@ ContentChild::RecvReinitRenderingForDeviceReset() } #if defined(XP_MACOSX) && defined(MOZ_CONTENT_SANDBOX) - -#include - -static bool -GetAppPaths(nsCString &aAppPath, nsCString &aAppBinaryPath, nsCString &aAppDir) -{ - nsAutoCString appPath; - nsAutoCString appBinaryPath( - (CommandLine::ForCurrentProcess()->argv()[0]).c_str()); - - nsAutoCString::const_iterator start, end; - appBinaryPath.BeginReading(start); - appBinaryPath.EndReading(end); - if (RFindInReadable(NS_LITERAL_CSTRING(".app/Contents/MacOS/"), start, end)) { - end = start; - ++end; ++end; ++end; ++end; - appBinaryPath.BeginReading(start); - appPath.Assign(Substring(start, end)); - } else { - return false; - } - - nsCOMPtr app, appBinary; - nsresult rv = NS_NewLocalFile(NS_ConvertUTF8toUTF16(appPath), - true, getter_AddRefs(app)); - if (NS_FAILED(rv)) { - return false; - } - rv = NS_NewLocalFile(NS_ConvertUTF8toUTF16(appBinaryPath), - true, getter_AddRefs(appBinary)); - if (NS_FAILED(rv)) { - return false; - } - - nsCOMPtr appDir; - nsCOMPtr dirSvc = - do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID); - if (!dirSvc) { - return false; - } - rv = dirSvc->Get(NS_GRE_DIR, - NS_GET_IID(nsIFile), getter_AddRefs(appDir)); - if (NS_FAILED(rv)) { - return false; - } - bool exists; - rv = appDir->Exists(&exists); - if (NS_FAILED(rv) || !exists) { - return false; - } - - // appDir points to .app/Contents/Resources, for our purposes we want - // .app/Contents. - nsCOMPtr appDirParent; - rv = appDir->GetParent(getter_AddRefs(appDirParent)); - if (NS_FAILED(rv)) { - return false; - } - - rv = app->Normalize(); - if (NS_FAILED(rv)) { - return false; - } - app->GetNativePath(aAppPath); - - rv = appBinary->Normalize(); - if (NS_FAILED(rv)) { - return false; - } - appBinary->GetNativePath(aAppBinaryPath); - - rv = appDirParent->Normalize(); - if (NS_FAILED(rv)) { - return false; - } - appDirParent->GetNativePath(aAppDir); - - return true; -} - -// This function is only used in an |#ifdef DEBUG| path. -#ifdef DEBUG -// Given a path to a file, return the directory which contains it. -static nsAutoCString -GetDirectoryPath(const char *aPath) { - nsCOMPtr file = do_CreateInstance(NS_LOCAL_FILE_CONTRACTID); - if (!file || - NS_FAILED(file->InitWithNativePath(nsDependentCString(aPath)))) { - MOZ_CRASH("Failed to create or init an nsIFile"); - } - nsCOMPtr directoryFile; - if (NS_FAILED(file->GetParent(getter_AddRefs(directoryFile))) || - !directoryFile) { - MOZ_CRASH("Failed to get parent for an nsIFile"); - } - directoryFile->Normalize(); - nsAutoCString directoryPath; - if (NS_FAILED(directoryFile->GetNativePath(directoryPath))) { - MOZ_CRASH("Failed to get path for an nsIFile"); - } - return directoryPath; -} -#endif // DEBUG - extern "C" { CGError CGSSetDenyWindowServerConnections(bool); @@ -1659,7 +1556,7 @@ StartMacOSContentSandbox() } nsAutoCString appPath, appBinaryPath, appDir; - if (!GetAppPaths(appPath, appBinaryPath, appDir)) { + if (!nsMacUtilsImpl::GetAppPaths(appPath, appBinaryPath, appDir)) { MOZ_CRASH("Error resolving child process path"); } @@ -1742,7 +1639,8 @@ StartMacOSContentSandbox() if (bloatLog != nullptr) { // |bloatLog| points to a specific file, but we actually write to a sibling // of that path. - nsAutoCString bloatDirectoryPath = GetDirectoryPath(bloatLog); + nsAutoCString bloatDirectoryPath = + nsMacUtilsImpl::GetDirectoryPath(bloatLog); info.debugWriteDir.assign(bloatDirectoryPath.get()); } #endif // DEBUG diff --git a/xpcom/base/nsMacUtilsImpl.cpp b/xpcom/base/nsMacUtilsImpl.cpp index 36f1f64a29f3..3ffc1b142d1a 100644 --- a/xpcom/base/nsMacUtilsImpl.cpp +++ b/xpcom/base/nsMacUtilsImpl.cpp @@ -6,6 +6,13 @@ #include "nsMacUtilsImpl.h" +#include "base/command_line.h" +#include "nsDirectoryServiceDefs.h" +#include "nsCOMPtr.h" +#include "nsIFile.h" +#include "nsIProperties.h" +#include "nsServiceManagerUtils.h" + #include NS_IMPL_ISUPPORTS(nsMacUtilsImpl, nsIMacUtils) @@ -125,3 +132,108 @@ nsMacUtilsImpl::GetIsTranslated(bool* aIsTranslated) return NS_OK; } + +#if defined(MOZ_CONTENT_SANDBOX) +bool +nsMacUtilsImpl::GetAppPaths(nsCString &aAppPath, + nsCString &aAppBinaryPath, + nsCString &aAppDir) +{ + nsAutoCString appPath; + nsAutoCString appBinaryPath( + (CommandLine::ForCurrentProcess()->argv()[0]).c_str()); + + nsAutoCString::const_iterator start, end; + appBinaryPath.BeginReading(start); + appBinaryPath.EndReading(end); + if (RFindInReadable(NS_LITERAL_CSTRING(".app/Contents/MacOS/"), start, end)) { + end = start; + ++end; ++end; ++end; ++end; + appBinaryPath.BeginReading(start); + appPath.Assign(Substring(start, end)); + } else { + return false; + } + + nsCOMPtr app, appBinary; + nsresult rv = NS_NewLocalFile(NS_ConvertUTF8toUTF16(appPath), + true, getter_AddRefs(app)); + if (NS_FAILED(rv)) { + return false; + } + rv = NS_NewLocalFile(NS_ConvertUTF8toUTF16(appBinaryPath), + true, getter_AddRefs(appBinary)); + if (NS_FAILED(rv)) { + return false; + } + + nsCOMPtr appDir; + nsCOMPtr dirSvc = + do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID); + if (!dirSvc) { + return false; + } + rv = dirSvc->Get(NS_GRE_DIR, + NS_GET_IID(nsIFile), getter_AddRefs(appDir)); + if (NS_FAILED(rv)) { + return false; + } + bool exists; + rv = appDir->Exists(&exists); + if (NS_FAILED(rv) || !exists) { + return false; + } + + // appDir points to .app/Contents/Resources, for our purposes we want + // .app/Contents. + nsCOMPtr appDirParent; + rv = appDir->GetParent(getter_AddRefs(appDirParent)); + if (NS_FAILED(rv)) { + return false; + } + + rv = app->Normalize(); + if (NS_FAILED(rv)) { + return false; + } + app->GetNativePath(aAppPath); + + rv = appBinary->Normalize(); + if (NS_FAILED(rv)) { + return false; + } + appBinary->GetNativePath(aAppBinaryPath); + + rv = appDirParent->Normalize(); + if (NS_FAILED(rv)) { + return false; + } + appDirParent->GetNativePath(aAppDir); + + return true; +} + +#if defined(DEBUG) +// Given a path to a file, return the directory which contains it. +nsAutoCString +nsMacUtilsImpl::GetDirectoryPath(const char *aPath) +{ + nsCOMPtr file = do_CreateInstance(NS_LOCAL_FILE_CONTRACTID); + if (!file || + NS_FAILED(file->InitWithNativePath(nsDependentCString(aPath)))) { + MOZ_CRASH("Failed to create or init an nsIFile"); + } + nsCOMPtr directoryFile; + if (NS_FAILED(file->GetParent(getter_AddRefs(directoryFile))) || + !directoryFile) { + MOZ_CRASH("Failed to get parent for an nsIFile"); + } + directoryFile->Normalize(); + nsAutoCString directoryPath; + if (NS_FAILED(directoryFile->GetNativePath(directoryPath))) { + MOZ_CRASH("Failed to get path for an nsIFile"); + } + return directoryPath; +} +#endif /* DEBUG */ +#endif /* MOZ_CONTENT_SANDBOX */ diff --git a/xpcom/base/nsMacUtilsImpl.h b/xpcom/base/nsMacUtilsImpl.h index 4a480ffe54e9..19e6d09f1d1e 100644 --- a/xpcom/base/nsMacUtilsImpl.h +++ b/xpcom/base/nsMacUtilsImpl.h @@ -21,6 +21,16 @@ public: { } +#if defined(MOZ_CONTENT_SANDBOX) + static bool GetAppPaths(nsCString &aAppPath, + nsCString &aAppBinaryPath, + nsCString &aAppDir); + +#ifdef DEBUG + static nsAutoCString GetDirectoryPath(const char *aPath); +#endif /* DEBUG */ +#endif /* MOZ_CONTENT_SANDBOX */ + private: ~nsMacUtilsImpl() {