1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef mozilla_net_SocketProcessHost_h
7 #define mozilla_net_SocketProcessHost_h
9 #include "mozilla/Maybe.h"
10 #include "mozilla/UniquePtr.h"
11 #include "mozilla/ipc/GeckoChildProcessHost.h"
12 #include "mozilla/MemoryReportingProcess.h"
13 #include "mozilla/ipc/TaskFactory.h"
17 #if defined(XP_LINUX) && defined(MOZ_SANDBOX)
23 class SocketProcessParent
;
25 // SocketProcessHost is the "parent process" container for a subprocess handle
26 // and IPC connection. It owns the parent process IPDL actor, which in this
27 // case, is a SocketProcessParent.
28 // SocketProcessHost is allocated and managed by nsIOService in parent process.
29 class SocketProcessHost final
: public mozilla::ipc::GeckoChildProcessHost
{
30 friend class SocketProcessParent
;
35 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Listener
)
37 // Called when the process of launching the process is complete.
38 virtual void OnProcessLaunchComplete(SocketProcessHost
* aHost
,
41 // Called when the channel is closed but Shutdown() is not invoked.
42 virtual void OnProcessUnexpectedShutdown(SocketProcessHost
* aHost
) = 0;
45 virtual ~Listener() = default;
48 explicit SocketProcessHost(Listener
* listener
);
50 // Launch the socket process asynchronously.
51 // The OnProcessLaunchComplete listener callback will be invoked
52 // either when a connection has been established, or if a connection
53 // could not be established due to an asynchronous error.
56 // Inform the socket process that it should clean up its resources and shut
57 // down. This initiates an asynchronous shutdown sequence. After this method
58 // returns, it is safe for the caller to forget its pointer to the
62 // Return the actor for the top-level actor of the process. Return null if
63 // the process is not connected.
64 SocketProcessParent
* GetActor() const {
65 MOZ_ASSERT(NS_IsMainThread());
67 return mSocketProcessParent
.get();
70 bool IsConnected() const {
71 MOZ_ASSERT(NS_IsMainThread());
73 return !!mSocketProcessParent
;
76 // Called on the IO thread.
77 void OnChannelConnected(base::ProcessId peer_pid
) override
;
79 #if defined(XP_MACOSX) && defined(MOZ_SANDBOX)
80 // Return the sandbox type to be used with this process type.
81 static MacSandboxType
GetMacSandboxType();
87 // Called on the main thread.
88 void OnChannelConnectedTask();
90 // Called on the main thread after a connection has been established.
91 void InitAfterConnect(bool aSucceeded
);
93 // Called on the main thread when the mSocketParent actor is shutting down.
94 void OnChannelClosed();
96 void DestroyProcess();
98 #if defined(XP_MACOSX) && defined(MOZ_SANDBOX)
99 static bool sLaunchWithMacSandbox
;
101 // Sandbox the Socket process at launch for all instances
102 bool IsMacSandboxLaunchEnabled() override
{ return sLaunchWithMacSandbox
; }
104 // Override so we can turn on Socket process-specific sandbox logging
105 bool FillMacSandboxInfo(MacSandboxInfo
& aInfo
) override
;
108 DISALLOW_COPY_AND_ASSIGN(SocketProcessHost
);
110 RefPtr
<Listener
> mListener
;
111 mozilla::Maybe
<mozilla::ipc::TaskFactory
<SocketProcessHost
>> mTaskFactory
;
113 enum class LaunchPhase
{ Unlaunched
, Waiting
, Complete
};
114 LaunchPhase mLaunchPhase
;
116 RefPtr
<SocketProcessParent
> mSocketProcessParent
;
117 // mShutdownRequested is set to true only when Shutdown() is called.
118 // If mShutdownRequested is false and the IPC channel is closed,
119 // OnProcessUnexpectedShutdown will be invoked.
120 bool mShutdownRequested
;
123 #if defined(XP_LINUX) && defined(MOZ_SANDBOX)
124 UniquePtr
<SandboxBroker
> mSandboxBroker
;
128 class SocketProcessMemoryReporter
: public MemoryReportingProcess
{
130 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SocketProcessMemoryReporter
, override
)
132 SocketProcessMemoryReporter() = default;
134 bool IsAlive() const override
;
136 bool SendRequestMemoryReport(
137 const uint32_t& aGeneration
, const bool& aAnonymize
,
138 const bool& aMinimizeMemoryUsage
,
139 const Maybe
<mozilla::ipc::FileDescriptor
>& aDMDFile
) override
;
141 int32_t Pid() const override
;
144 virtual ~SocketProcessMemoryReporter() = default;
148 } // namespace mozilla
150 #endif // mozilla_net_SocketProcessHost_h