tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

RDDProcessManager.h (4432B)


      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 #ifndef _include_dom_media_ipc_RDDProcessManager_h_
      7 #define _include_dom_media_ipc_RDDProcessManager_h_
      8 #include "mozilla/MozPromise.h"
      9 #include "mozilla/PRDDChild.h"
     10 #include "mozilla/PRemoteMediaManagerChild.h"
     11 #include "mozilla/RDDProcessHost.h"
     12 #include "mozilla/dom/ipc/IdType.h"
     13 #include "mozilla/ipc/TaskFactory.h"
     14 #include "nsIObserver.h"
     15 
     16 namespace mozilla {
     17 
     18 class MemoryReportingProcess;
     19 class RDDChild;
     20 
     21 // The RDDProcessManager is a singleton responsible for creating RDD-bound
     22 // objects that may live in another process. Currently, it provides access
     23 // to the RDD process via ContentParent.
     24 class RDDProcessManager final : public RDDProcessHost::Listener {
     25  friend class RDDChild;
     26 
     27 public:
     28  static void Initialize();
     29  static void RDDProcessShutdown();
     30  static void Shutdown();
     31  static RDDProcessManager* Get();
     32 
     33  ~RDDProcessManager();
     34 
     35  using EnsureRDDPromise =
     36      MozPromise<ipc::Endpoint<PRemoteMediaManagerChild>, nsresult, true>;
     37  // Launch a new RDD process asynchronously
     38  RefPtr<GenericNonExclusivePromise> LaunchRDDProcess();
     39  // If not using a RDD process, launch a new RDD process asynchronously and
     40  // create a RemoteMediaManager bridge
     41  RefPtr<EnsureRDDPromise> EnsureRDDProcessAndCreateBridge(
     42      ipc::EndpointProcInfo aOtherProcess, dom::ContentParentId aParentId);
     43 
     44  void OnProcessUnexpectedShutdown(RDDProcessHost* aHost) override;
     45 
     46  // Notify the RDDProcessManager that a top-level PRDD protocol has been
     47  // terminated. This may be called from any thread.
     48  void NotifyRemoteActorDestroyed(const uint64_t& aProcessToken);
     49 
     50  // Returns -1 if there is no RDD process, or the platform pid for it.
     51  base::ProcessId RDDProcessPid();
     52 
     53  // Returns Invalid if there is no RDD process, or the proc info for it.
     54  ipc::EndpointProcInfo RDDEndpointProcInfo();
     55 
     56  // If a RDD process is present, create a MemoryReportingProcess object.
     57  // Otherwise, return null.
     58  RefPtr<MemoryReportingProcess> GetProcessMemoryReporter();
     59 
     60  // Returns access to the PRDD protocol if a RDD process is present.
     61  RDDChild* GetRDDChild() { return mRDDChild; }
     62 
     63  // Returns whether or not a RDD process was ever launched.
     64  bool AttemptedRDDProcess() const { return mNumProcessAttempts > 0; }
     65 
     66  // Returns the RDD Process
     67  RDDProcessHost* Process() { return mProcess; }
     68 
     69  /*
     70   * ** Test-only Method **
     71   *
     72   * Trigger RDD-process test metric instrumentation.
     73   */
     74  RefPtr<PRDDChild::TestTriggerMetricsPromise> TestTriggerMetrics();
     75 
     76 private:
     77  bool IsRDDProcessLaunching() const;
     78  bool IsRDDProcessAlive() const;
     79 
     80  // Called from our xpcom-shutdown observer.
     81  void OnXPCOMShutdown();
     82  void OnPreferenceChange(const char16_t* aData);
     83 
     84  RDDProcessManager();
     85 
     86  // Shutdown the RDD process.
     87  void DestroyProcess();
     88 
     89  bool IsShutdown() const;
     90 
     91  DISALLOW_COPY_AND_ASSIGN(RDDProcessManager);
     92 
     93  class Observer final : public nsIObserver {
     94   public:
     95    NS_DECL_ISUPPORTS
     96    NS_DECL_NSIOBSERVER
     97    explicit Observer(RDDProcessManager* aManager);
     98 
     99   protected:
    100    ~Observer() = default;
    101 
    102    RDDProcessManager* mManager;
    103  };
    104  friend class Observer;
    105 
    106  bool CreateContentBridge(
    107      ipc::EndpointProcInfo aOtherProcess, dom::ContentParentId aParentId,
    108      ipc::Endpoint<PRemoteMediaManagerChild>* aOutRemoteMediaManager);
    109 
    110  const RefPtr<Observer> mObserver;
    111  ipc::TaskFactory<RDDProcessManager> mTaskFactory;
    112  uint32_t mNumProcessAttempts = 0;
    113  uint32_t mNumUnexpectedCrashes = 0;
    114 
    115  // Fields that are associated with the current RDD process.
    116  RDDProcessHost* mProcess = nullptr;
    117  uint64_t mProcessToken = 0;
    118  RDDChild* mRDDChild = nullptr;
    119  // Collects any pref changes that occur during process launch (after
    120  // the initial map is passed in command-line arguments) to be sent
    121  // when the process can receive IPC messages.
    122  nsTArray<dom::Pref> mQueuedPrefs;
    123  // Promise will be resolved when the RDD process has been fully started and
    124  // VideoBridge configured. Only accessed on the main thread.
    125  RefPtr<GenericNonExclusivePromise> mLaunchRDDPromise;
    126 };
    127 
    128 }  // namespace mozilla
    129 
    130 #endif  // _include_dom_media_ipc_RDDProcessManager_h_