Bug 1874684 - Part 4: Prefer const references instead of copying Instant values....
[gecko.git] / netwerk / ipc / SocketProcessHost.h
blob44648b7fe7bc2c8cf0b50475d05f51fcacf310df
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"
15 namespace mozilla {
17 #if defined(XP_LINUX) && defined(MOZ_SANDBOX)
18 class SandboxBroker;
19 #endif
21 namespace net {
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;
32 public:
33 class Listener {
34 public:
35 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Listener)
37 // Called when the process of launching the process is complete.
38 virtual void OnProcessLaunchComplete(SocketProcessHost* aHost,
39 bool aSucceeded) = 0;
41 // Called when the channel is closed but Shutdown() is not invoked.
42 virtual void OnProcessUnexpectedShutdown(SocketProcessHost* aHost) = 0;
44 protected:
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.
54 bool Launch();
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
59 // SocketProcessHost.
60 void Shutdown();
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();
82 #endif
84 private:
85 ~SocketProcessHost();
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;
106 #endif
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;
121 bool mChannelClosed;
123 #if defined(XP_LINUX) && defined(MOZ_SANDBOX)
124 UniquePtr<SandboxBroker> mSandboxBroker;
125 #endif
128 class SocketProcessMemoryReporter : public MemoryReportingProcess {
129 public:
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;
143 protected:
144 virtual ~SocketProcessMemoryReporter() = default;
147 } // namespace net
148 } // namespace mozilla
150 #endif // mozilla_net_SocketProcessHost_h