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 #ifndef _include_dom_media_ipc_RDDProcessHost_h_
8 #define _include_dom_media_ipc_RDDProcessHost_h_
9 #include "mozilla/ipc/GeckoChildProcessHost.h"
10 
11 #include "mozilla/Maybe.h"
12 #include "mozilla/UniquePtr.h"
13 #include "mozilla/ipc/ProtocolUtils.h"
14 #include "mozilla/ipc/TaskFactory.h"
15 
16 namespace mozilla {
17 namespace ipc {
18 class SharedPreferenceSerializer;
19 }
20 }  // namespace mozilla
21 class nsITimer;
22 
23 namespace mozilla {
24 
25 class RDDChild;
26 
27 // RDDProcessHost is the "parent process" container for a subprocess handle and
28 // IPC connection. It owns the parent process IPDL actor, which in this case,
29 // is a RDDChild.
30 //
31 // RDDProcessHosts are allocated and managed by RDDProcessManager. For all
32 // intents and purposes it is a singleton, though more than one may be allocated
33 // at a time due to its shutdown being asynchronous.
34 class RDDProcessHost final : public mozilla::ipc::GeckoChildProcessHost {
35   friend class RDDChild;
36 
37  public:
38   class Listener {
39    public:
OnProcessLaunchComplete(RDDProcessHost * aHost)40     virtual void OnProcessLaunchComplete(RDDProcessHost* aHost) {}
41 
42     // The RDDProcessHost has unexpectedly shutdown or had its connection
43     // severed. This is not called if an error occurs after calling
44     // Shutdown().
OnProcessUnexpectedShutdown(RDDProcessHost * aHost)45     virtual void OnProcessUnexpectedShutdown(RDDProcessHost* aHost) {}
46   };
47 
48   explicit RDDProcessHost(Listener* listener);
49 
50   // Launch the subprocess asynchronously. On failure, false is returned.
51   // Otherwise, true is returned, and the OnProcessLaunchComplete listener
52   // callback will be invoked either when a connection has been established, or
53   // if a connection could not be established due to an asynchronous error.
54   //
55   // @param aExtraOpts (StringVector)
56   //        Extra options to pass to the subprocess.
57   bool Launch(StringVector aExtraOpts);
58 
59   // If the process is being launched, block until it has launched and
60   // connected. If a launch task is pending, it will fire immediately.
61   //
62   // Returns true if the process is successfully connected; false otherwise.
63   bool WaitForLaunch();
64 
65   // Inform the process that it should clean up its resources and shut
66   // down. This initiates an asynchronous shutdown sequence. After this
67   // method returns, it is safe for the caller to forget its pointer to
68   // the RDDProcessHost.
69   //
70   // After this returns, the attached Listener is no longer used.
71   void Shutdown();
72 
73   // Return the actor for the top-level actor of the process. If the process
74   // has not connected yet, this returns null.
GetActor()75   RDDChild* GetActor() const { return mRDDChild.get(); }
76 
77   // Return a unique id for this process, guaranteed not to be shared with any
78   // past or future instance of RDDProcessHost.
79   uint64_t GetProcessToken() const;
80 
IsConnected()81   bool IsConnected() const { return !!mRDDChild; }
82 
83   // Return the time stamp for when we tried to launch the RDD process.
84   // This is currently used for Telemetry so that we can determine how
85   // long RDD processes take to spin up. Note this doesn't denote a
86   // successful launch, just when we attempted launch.
GetLaunchTime()87   TimeStamp GetLaunchTime() const { return mLaunchTime; }
88 
89   // Called on the IO thread.
90   void OnChannelConnected(int32_t peer_pid) override;
91   void OnChannelError() override;
92 
93   void SetListener(Listener* aListener);
94 
95   // Used for tests and diagnostics
96   void KillProcess();
97 
98 #if defined(XP_MACOSX) && defined(MOZ_SANDBOX)
99   // Return the sandbox type to be used with this process type.
100   static MacSandboxType GetMacSandboxType();
101 #endif
102 
103  private:
104   ~RDDProcessHost();
105 
106   // Called on the main thread.
107   void OnChannelConnectedTask();
108   void OnChannelErrorTask();
109 
110   // Called on the main thread after a connection has been established.
111   void InitAfterConnect(bool aSucceeded);
112 
113   // Called on the main thread when the mRDDChild actor is shutting down.
114   void OnChannelClosed();
115 
116   // Kill the remote process, triggering IPC shutdown.
117   void KillHard(const char* aReason);
118 
119   void DestroyProcess();
120 
121 #if defined(XP_MACOSX) && defined(MOZ_SANDBOX)
122   static bool sLaunchWithMacSandbox;
123 
124   // Sandbox the RDD process at launch for all instances
IsMacSandboxLaunchEnabled()125   bool IsMacSandboxLaunchEnabled() override { return sLaunchWithMacSandbox; }
126 
127   // Override so we can turn on RDD process-specific sandbox logging
128   bool FillMacSandboxInfo(MacSandboxInfo& aInfo) override;
129 #endif
130 
131   DISALLOW_COPY_AND_ASSIGN(RDDProcessHost);
132 
133   Listener* mListener;
134   mozilla::ipc::TaskFactory<RDDProcessHost> mTaskFactory;
135 
136   enum class LaunchPhase { Unlaunched, Waiting, Complete };
137   LaunchPhase mLaunchPhase;
138 
139   UniquePtr<RDDChild> mRDDChild;
140   uint64_t mProcessToken;
141 
142   UniquePtr<ipc::SharedPreferenceSerializer> mPrefSerializer;
143 
144   bool mShutdownRequested;
145   bool mChannelClosed;
146 
147   TimeStamp mLaunchTime;
148 };
149 
150 }  // namespace mozilla
151 
152 #endif  // _include_dom_media_ipc_RDDProcessHost_h_
153