SocketProcessHost.h (4692B)
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/. */ 5 6 #ifndef mozilla_net_SocketProcessHost_h 7 #define mozilla_net_SocketProcessHost_h 8 9 #include "mozilla/Maybe.h" 10 #include "mozilla/ipc/GeckoChildProcessHost.h" 11 #include "mozilla/MemoryReportingProcess.h" 12 #include "mozilla/ipc/TaskFactory.h" 13 14 namespace mozilla { 15 16 #if defined(XP_LINUX) && defined(MOZ_SANDBOX) 17 class SandboxBroker; 18 #endif 19 20 namespace net { 21 22 class SocketProcessParent; 23 24 // SocketProcessHost is the "parent process" container for a subprocess handle 25 // and IPC connection. It owns the parent process IPDL actor, which in this 26 // case, is a SocketProcessParent. 27 // SocketProcessHost is allocated and managed by nsIOService in parent process. 28 class SocketProcessHost final : public mozilla::ipc::GeckoChildProcessHost { 29 friend class SocketProcessParent; 30 31 public: 32 class Listener { 33 public: 34 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Listener) 35 36 // Called when the process of launching the process is complete. 37 virtual void OnProcessLaunchComplete(SocketProcessHost* aHost, 38 bool aSucceeded) = 0; 39 40 // Called when the channel is closed but Shutdown() is not invoked. 41 virtual void OnProcessUnexpectedShutdown(SocketProcessHost* aHost) = 0; 42 43 protected: 44 virtual ~Listener() = default; 45 }; 46 47 explicit SocketProcessHost(Listener* listener); 48 49 // Launch the socket process asynchronously. 50 // The OnProcessLaunchComplete listener callback will be invoked 51 // either when a connection has been established, or if a connection 52 // could not be established due to an asynchronous error. 53 bool Launch(); 54 55 // Inform the socket process that it should clean up its resources and shut 56 // down. This initiates an asynchronous shutdown sequence. After this method 57 // returns, it is safe for the caller to forget its pointer to the 58 // SocketProcessHost. 59 void Shutdown(); 60 61 // Return the actor for the top-level actor of the process. Return null if 62 // the process is not connected. 63 SocketProcessParent* GetActor() const { 64 MOZ_ASSERT(NS_IsMainThread()); 65 66 return mSocketProcessParent.get(); 67 } 68 69 bool IsConnected() const { 70 MOZ_ASSERT(NS_IsMainThread()); 71 72 return !!mSocketProcessParent; 73 } 74 75 // Called on the IO thread. 76 void OnChannelConnected(base::ProcessId peer_pid) override; 77 78 #if defined(XP_MACOSX) && defined(MOZ_SANDBOX) 79 // Return the sandbox type to be used with this process type. 80 static MacSandboxType GetMacSandboxType(); 81 #endif 82 83 private: 84 ~SocketProcessHost(); 85 86 // Called on the main thread. 87 void OnChannelConnectedTask(); 88 89 // Called on the main thread after a connection has been established. 90 void InitAfterConnect(bool aSucceeded); 91 92 // Called on the main thread when the mSocketParent actor is shutting down. 93 void OnChannelClosed(); 94 95 void DestroyProcess(); 96 97 #if defined(XP_MACOSX) && defined(MOZ_SANDBOX) 98 static bool sLaunchWithMacSandbox; 99 100 // Sandbox the Socket process at launch for all instances 101 bool IsMacSandboxLaunchEnabled() override { return sLaunchWithMacSandbox; } 102 103 // Override so we can turn on Socket process-specific sandbox logging 104 bool FillMacSandboxInfo(MacSandboxInfo& aInfo) override; 105 #endif 106 107 DISALLOW_COPY_AND_ASSIGN(SocketProcessHost); 108 109 RefPtr<Listener> mListener; 110 mozilla::Maybe<mozilla::ipc::TaskFactory<SocketProcessHost>> mTaskFactory; 111 112 enum class LaunchPhase { Unlaunched, Waiting, Complete }; 113 LaunchPhase mLaunchPhase; 114 115 RefPtr<SocketProcessParent> mSocketProcessParent; 116 // mShutdownRequested is set to true only when Shutdown() is called. 117 // If mShutdownRequested is false and the IPC channel is closed, 118 // OnProcessUnexpectedShutdown will be invoked. 119 bool mShutdownRequested; 120 bool mChannelClosed; 121 122 #if defined(XP_LINUX) && defined(MOZ_SANDBOX) 123 RefPtr<SandboxBroker> mSandboxBroker; 124 #endif 125 }; 126 127 class SocketProcessMemoryReporter : public MemoryReportingProcess { 128 public: 129 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SocketProcessMemoryReporter, override) 130 131 SocketProcessMemoryReporter() = default; 132 133 bool IsAlive() const override; 134 135 bool SendRequestMemoryReport( 136 const uint32_t& aGeneration, const bool& aAnonymize, 137 const bool& aMinimizeMemoryUsage, 138 const Maybe<mozilla::ipc::FileDescriptor>& aDMDFile) override; 139 140 int32_t Pid() const override; 141 142 protected: 143 virtual ~SocketProcessMemoryReporter() = default; 144 }; 145 146 } // namespace net 147 } // namespace mozilla 148 149 #endif // mozilla_net_SocketProcessHost_h