Bug 1431441 - Part 1 - Move GetAppPaths and GetDirectoryPath to nsMacUtilsImpl as static methods r=Alex_Gaynor
Differential Revision:
https://phabricator.services.mozilla.com/D6717
--- a/dom/ipc/ContentChild.cpp
+++ b/dom/ipc/ContentChild.cpp
@@ -189,16 +189,17 @@
#ifdef XP_WIN
#include <process.h>
#define getpid _getpid
#include "mozilla/widget/AudioSession.h"
#include "mozilla/audio/AudioNotificationReceiver.h"
#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"
// Info.plist key associated with the developer repo object directory
#define MAC_DEV_OBJ_KEY "MozillaDeveloperObjPath"
#endif /* XP_MACOSX */
#ifdef MOZ_X11
@@ -1518,120 +1519,16 @@ ContentChild::RecvReinitRenderingForDevi
if (tabChild->GetLayersId().IsValid()) {
tabChild->ReinitRenderingForDeviceReset();
}
}
return IPC_OK();
}
#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);
void CGSShutdownServerConnections();
};
static bool
StartMacOSContentSandbox()
@@ -1654,17 +1551,17 @@ StartMacOSContentSandbox()
CGError result = CGSSetDenyWindowServerConnections(true);
MOZ_DIAGNOSTIC_ASSERT(result == kCGErrorSuccess);
#if !MOZ_DIAGNOSTIC_ASSERT_ENABLED
Unused << result;
#endif
}
nsAutoCString appPath, appBinaryPath, appDir;
- if (!GetAppPaths(appPath, appBinaryPath, appDir)) {
+ if (!nsMacUtilsImpl::GetAppPaths(appPath, appBinaryPath, appDir)) {
MOZ_CRASH("Error resolving child process path");
}
ContentChild* cc = ContentChild::GetSingleton();
nsresult rv;
nsCOMPtr<nsIFile> profileDir;
cc->GetProfileDir(getter_AddRefs(profileDir));
@@ -1737,17 +1634,18 @@ StartMacOSContentSandbox()
#ifdef DEBUG
// When a content process dies intentionally (|NoteIntentionalCrash|), for
// tests it wants to log that it did this. Allow writing to this location
// that the testrunner wants.
char *bloatLog = PR_GetEnv("XPCOM_MEM_BLOAT_LOG");
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
std::string err;
if (!mozilla::StartMacSandbox(info, err)) {
NS_WARNING(err.c_str());
MOZ_CRASH("sandbox_init() failed");
--- a/xpcom/base/nsMacUtilsImpl.cpp
+++ b/xpcom/base/nsMacUtilsImpl.cpp
@@ -1,16 +1,23 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#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)
nsresult
nsMacUtilsImpl::GetArchString(nsAString& aArchString)
{
if (!mBinaryArchs.IsEmpty()) {
@@ -120,8 +127,113 @@ nsMacUtilsImpl::GetIsTranslated(bool* aI
#else
// Translation only exists for ppc code. Other architectures aren't
// translated.
*aIsTranslated = false;
#endif
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 */
--- a/xpcom/base/nsMacUtilsImpl.h
+++ b/xpcom/base/nsMacUtilsImpl.h
@@ -16,16 +16,26 @@ class nsMacUtilsImpl final : public nsIM
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIMACUTILS
nsMacUtilsImpl()
{
}
+#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()
{
}
nsresult GetArchString(nsAString& aArchString);
// A string containing a "-" delimited list of architectures