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/. */
6 
7 #include "mozilla/ipc/ProcessChild.h"
8 
9 #include "nsDebug.h"
10 
11 #ifdef XP_WIN
12 #  include <stdlib.h>  // for _exit()
13 #else
14 #  include <unistd.h>  // for _exit()
15 #endif
16 
17 #include "nsAppRunner.h"
18 #include "mozilla/AppShutdown.h"
19 #include "mozilla/ipc/CrashReporterClient.h"
20 #include "mozilla/ipc/IOThreadChild.h"
21 #include "mozilla/GeckoArgs.h"
22 
23 namespace mozilla {
24 namespace ipc {
25 
26 ProcessChild* ProcessChild::gProcessChild;
27 
28 static Atomic<bool> sExpectingShutdown(false);
29 
ProcessChild(ProcessId aParentPid)30 ProcessChild::ProcessChild(ProcessId aParentPid)
31     : ChildProcess(new IOThreadChild()),
32       mUILoop(MessageLoop::current()),
33       mParentPid(aParentPid) {
34   MOZ_ASSERT(mUILoop, "UILoop should be created by now");
35   MOZ_ASSERT(!gProcessChild, "should only be one ProcessChild");
36   gProcessChild = this;
37 }
38 
39 /* static */
AddPlatformBuildID(std::vector<std::string> & aExtraArgs)40 void ProcessChild::AddPlatformBuildID(std::vector<std::string>& aExtraArgs) {
41   nsCString parentBuildID(mozilla::PlatformBuildID());
42   geckoargs::sParentBuildID.Put(parentBuildID.get(), aExtraArgs);
43 }
44 
45 /* static */
InitPrefs(int aArgc,char * aArgv[])46 bool ProcessChild::InitPrefs(int aArgc, char* aArgv[]) {
47   Maybe<uint64_t> prefsHandle = Some(0);
48   Maybe<uint64_t> prefMapHandle = Some(0);
49   Maybe<uint64_t> prefsLen = geckoargs::sPrefsLen.Get(aArgc, aArgv);
50   Maybe<uint64_t> prefMapSize = geckoargs::sPrefMapSize.Get(aArgc, aArgv);
51 
52   if (prefsLen.isNothing() || prefMapSize.isNothing()) {
53     return false;
54   }
55 
56 #ifdef XP_WIN
57   prefsHandle = geckoargs::sPrefsHandle.Get(aArgc, aArgv);
58   prefMapHandle = geckoargs::sPrefMapHandle.Get(aArgc, aArgv);
59 
60   if (prefsHandle.isNothing() || prefMapHandle.isNothing()) {
61     return false;
62   }
63 #endif
64 
65   SharedPreferenceDeserializer deserializer;
66   return deserializer.DeserializeFromSharedMemory(*prefsHandle, *prefMapHandle,
67                                                   *prefsLen, *prefMapSize);
68 }
69 
~ProcessChild()70 ProcessChild::~ProcessChild() { gProcessChild = nullptr; }
71 
72 /* static */
NotifiedImpendingShutdown()73 void ProcessChild::NotifiedImpendingShutdown() {
74   sExpectingShutdown = true;
75   CrashReporter::AnnotateCrashReport(
76       CrashReporter::Annotation::IPCShutdownState,
77       "NotifyImpendingShutdown received."_ns);
78 }
79 
80 /* static */
ExpectingShutdown()81 bool ProcessChild::ExpectingShutdown() { return sExpectingShutdown; }
82 
83 /* static */
QuickExit()84 void ProcessChild::QuickExit() { AppShutdown::DoImmediateExit(); }
85 
86 }  // namespace ipc
87 }  // namespace mozilla
88