no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / dom / ipc / ContentProcess.cpp
blob10118cd38ff0a9be1d85c71793d165c69650b248
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/ipc/IOThreadChild.h"
9 #include "ContentProcess.h"
10 #include "base/shared_memory.h"
11 #include "mozilla/Preferences.h"
13 #if defined(XP_MACOSX) && defined(MOZ_SANDBOX)
14 # include <stdlib.h>
15 # include "mozilla/Sandbox.h"
16 # include "mozilla/SandboxSettings.h"
17 #endif
19 #include "nsAppRunner.h"
20 #include "mozilla/ipc/BackgroundChild.h"
21 #include "mozilla/ipc/ProcessUtils.h"
22 #include "mozilla/GeckoArgs.h"
23 #include "mozilla/Omnijar.h"
24 #include "nsCategoryManagerUtils.h"
26 using mozilla::ipc::IOThreadChild;
28 namespace mozilla::dom {
30 static nsresult GetGREDir(nsIFile** aResult) {
31 nsCOMPtr<nsIFile> current;
32 nsresult rv = XRE_GetBinaryPath(getter_AddRefs(current));
33 NS_ENSURE_SUCCESS(rv, rv);
35 #ifdef XP_DARWIN
36 // Walk out of [subprocess].app/Contents/MacOS to the real GRE dir
37 const int depth = 4;
38 #else
39 const int depth = 1;
40 #endif
42 for (int i = 0; i < depth; ++i) {
43 nsCOMPtr<nsIFile> parent;
44 rv = current->GetParent(getter_AddRefs(parent));
45 NS_ENSURE_SUCCESS(rv, rv);
47 current = parent;
48 NS_ENSURE_TRUE(current, NS_ERROR_UNEXPECTED);
51 #ifdef XP_DARWIN
52 rv = current->SetNativeLeafName("Resources"_ns);
53 NS_ENSURE_SUCCESS(rv, rv);
54 #endif
56 current.forget(aResult);
58 return NS_OK;
61 ContentProcess::ContentProcess(ProcessId aParentPid,
62 const nsID& aMessageChannelId)
63 : ProcessChild(aParentPid, aMessageChannelId) {
64 NS_LogInit();
67 ContentProcess::~ContentProcess() { NS_LogTerm(); }
69 bool ContentProcess::Init(int aArgc, char* aArgv[]) {
70 Maybe<uint64_t> childID = geckoargs::sChildID.Get(aArgc, aArgv);
71 Maybe<bool> isForBrowser = Nothing();
72 Maybe<const char*> parentBuildID =
73 geckoargs::sParentBuildID.Get(aArgc, aArgv);
74 Maybe<uint64_t> jsInitHandle;
75 Maybe<uint64_t> jsInitLen = geckoargs::sJsInitLen.Get(aArgc, aArgv);
77 nsCOMPtr<nsIFile> appDirArg;
78 Maybe<const char*> appDir = geckoargs::sAppDir.Get(aArgc, aArgv);
79 if (appDir.isSome()) {
80 bool flag;
81 nsresult rv = XRE_GetFileFromPath(*appDir, getter_AddRefs(appDirArg));
82 if (NS_FAILED(rv) || NS_FAILED(appDirArg->Exists(&flag)) || !flag) {
83 NS_WARNING("Invalid application directory passed to content process.");
84 appDirArg = nullptr;
88 Maybe<bool> safeMode = geckoargs::sSafeMode.Get(aArgc, aArgv);
89 if (safeMode.isSome()) {
90 gSafeMode = *safeMode;
93 Maybe<bool> isForBrowerParam = geckoargs::sIsForBrowser.Get(aArgc, aArgv);
94 Maybe<bool> notForBrowserParam = geckoargs::sNotForBrowser.Get(aArgc, aArgv);
95 if (isForBrowerParam.isSome()) {
96 isForBrowser = Some(true);
98 if (notForBrowserParam.isSome()) {
99 isForBrowser = Some(false);
102 // command line: [-jsInitHandle handle] -jsInitLen length
103 #ifdef XP_WIN
104 jsInitHandle = geckoargs::sJsInitHandle.Get(aArgc, aArgv);
105 #endif
107 #if defined(XP_MACOSX) && defined(MOZ_SANDBOX)
108 nsCOMPtr<nsIFile> profileDir;
109 bool flag;
110 Maybe<const char*> profile = geckoargs::sProfile.Get(aArgc, aArgv);
111 // xpcshell self-test on macOS will hit this, so check isSome() otherwise
112 // Maybe<> assertions will MOZ_CRASH() us.
113 if (profile.isSome()) {
114 nsresult rv = XRE_GetFileFromPath(*profile, getter_AddRefs(profileDir));
115 if (NS_FAILED(rv) || NS_FAILED(profileDir->Exists(&flag)) || !flag) {
116 NS_WARNING("Invalid profile directory passed to content process.");
117 profileDir = nullptr;
119 } else {
120 NS_WARNING("No profile directory passed to content process.");
122 #endif /* XP_MACOSX && MOZ_SANDBOX */
124 // Did we find all the mandatory flags?
125 if (childID.isNothing() || isForBrowser.isNothing() ||
126 parentBuildID.isNothing()) {
127 return false;
130 if (!ProcessChild::InitPrefs(aArgc, aArgv)) {
131 return false;
134 if (!::mozilla::ipc::ImportSharedJSInit(jsInitHandle.valueOr(0),
135 jsInitLen.valueOr(0))) {
136 return false;
139 mContent.Init(TakeInitialEndpoint(), *parentBuildID, *childID, *isForBrowser);
141 nsCOMPtr<nsIFile> greDir;
142 nsresult rv = GetGREDir(getter_AddRefs(greDir));
143 if (NS_FAILED(rv)) {
144 return false;
147 nsCOMPtr<nsIFile> xpcomAppDir = appDirArg ? appDirArg : greDir;
149 rv = mDirProvider.Initialize(xpcomAppDir, greDir);
150 if (NS_FAILED(rv)) {
151 return false;
154 // Handle the -greomni/-appomni flags (unless the forkserver already
155 // preloaded the jar(s)).
156 if (!Omnijar::IsInitialized()) {
157 Omnijar::ChildProcessInit(aArgc, aArgv);
160 rv = NS_InitXPCOM(nullptr, xpcomAppDir, &mDirProvider);
161 if (NS_FAILED(rv)) {
162 return false;
165 // "app-startup" is the name of both the category and the event
166 NS_CreateServicesFromCategory("app-startup", nullptr, "app-startup", nullptr);
168 #if (defined(XP_MACOSX)) && defined(MOZ_SANDBOX)
169 mContent.SetProfileDir(profileDir);
170 # if defined(DEBUG)
171 if (IsContentSandboxEnabled()) {
172 AssertMacSandboxEnabled();
174 # endif /* DEBUG */
175 #endif /* XP_MACOSX && MOZ_SANDBOX */
177 // Do this as early as possible to get the parent process to initialize the
178 // background thread since we'll likely need database information very soon.
179 mozilla::ipc::BackgroundChild::Startup();
180 mozilla::ipc::BackgroundChild::InitContentStarter(&mContent);
182 return true;
185 // Note: CleanUp() never gets called in non-debug builds because we exit early
186 // in ContentChild::ActorDestroy().
187 void ContentProcess::CleanUp() {
188 mDirProvider.DoShutdown();
189 NS_ShutdownXPCOM(nullptr);
192 } // namespace mozilla::dom