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 GFX_VR_PROCESS_PARENT_H
8 #define GFX_VR_PROCESS_PARENT_H
9 
10 #include "mozilla/UniquePtr.h"
11 
12 #include "mozilla/ipc/GeckoChildProcessHost.h"
13 #include "mozilla/ipc/TaskFactory.h"
14 
15 namespace mozilla {
16 namespace ipc {
17 class SharedPreferenceSerializer;
18 }
19 namespace gfx {
20 
21 class VRChild;
22 
23 class VRProcessParent final : public mozilla::ipc::GeckoChildProcessHost {
24  public:
25   class Listener {
26    public:
OnProcessLaunchComplete(VRProcessParent * aParent)27     virtual void OnProcessLaunchComplete(VRProcessParent* aParent) {}
28 
29     // Follow GPU and RDD process manager, adding this to avoid
30     // unexpectedly shutdown or had its connection severed.
31     // This is not called if an error occurs after calling Shutdown().
OnProcessUnexpectedShutdown(VRProcessParent * aParent)32     virtual void OnProcessUnexpectedShutdown(VRProcessParent* aParent) {}
33   };
34 
35   explicit VRProcessParent(Listener* aListener);
36 
37   // Launch the subprocess asynchronously. On failure, false is returned.
38   // Otherwise, true is returned, and the OnProcessLaunchComplete listener
39   // callback will be invoked either when a connection has been established, or
40   // if a connection could not be established due to an asynchronous error.
41   bool Launch();
42   // If the process is being launched, block until it has launched and
43   // connected. If a launch task is pending, it will fire immediately.
44   //
45   // Returns true if the process is successfully connected; false otherwise.
46   bool WaitForLaunch();
47   void Shutdown();
48   void DestroyProcess();
CanShutdown()49   bool CanShutdown() override { return true; }
50 
51   void OnChannelError() override;
52   void OnChannelConnected(int32_t peer_pid) override;
53   void OnChannelConnectedTask();
54   void OnChannelErrorTask();
55   void OnChannelClosed();
56   bool IsConnected() const;
57 
58   base::ProcessId OtherPid();
GetActor()59   VRChild* GetActor() const { return mVRChild.get(); }
60   // Return a unique id for this process, guaranteed not to be shared with any
61   // past or future instance of VRProcessParent.
62   uint64_t GetProcessToken() const;
63 
64  private:
65   ~VRProcessParent();
66 
67   DISALLOW_COPY_AND_ASSIGN(VRProcessParent);
68 
69   bool InitAfterConnect(bool aSucceeded);
70   void KillHard(const char* aReason);
71 
72   UniquePtr<VRChild> mVRChild;
73   mozilla::ipc::TaskFactory<VRProcessParent> mTaskFactory;
74   nsCOMPtr<nsIThread> mLaunchThread;
75   Listener* mListener;
76 
77   enum class LaunchPhase { Unlaunched, Waiting, Complete };
78   LaunchPhase mLaunchPhase;
79   bool mChannelClosed;
80   bool mShutdownRequested;
81   UniquePtr<mozilla::ipc::SharedPreferenceSerializer> mPrefSerializer;
82 };
83 
84 }  // namespace gfx
85 }  // namespace mozilla
86 
87 #endif  // ifndef GFX_VR_PROCESS_PARENT_H
88