Bug 1431441 - Part 1 - Move GetAppPaths and GetDirectoryPath to nsMacUtilsImpl as static methods r=Alex_Gaynor

Differential Revision: https://phabricator.services.mozilla.com/D6717

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Haik Aftandilian 2018-10-18 20:44:47 +00:00
parent 2afea4f01b
commit 6344601010
3 changed files with 126 additions and 106 deletions

View File

@ -194,6 +194,7 @@
#endif
#if defined(XP_MACOSX)
#include "nsMacUtilsImpl.h"
#include <CoreServices/CoreServices.h>
// 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 <stdlib.h>
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<nsIFile> 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<nsIFile> appDir;
nsCOMPtr<nsIProperties> 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<nsIFile> 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<nsIFile> 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<nsIFile> 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

View File

@ -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 <CoreFoundation/CoreFoundation.h>
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<nsIFile> 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<nsIFile> appDir;
nsCOMPtr<nsIProperties> 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<nsIFile> 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<nsIFile> 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<nsIFile> 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 */

View File

@ -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()
{