summaryrefslogtreecommitdiffstats
path: root/dom/media/ipc/RDDProcessManager.h
blob: 730ab4fa1f5bf8ff59adea118f2974e3b15d83a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef _include_dom_media_ipc_RDDProcessManager_h_
#define _include_dom_media_ipc_RDDProcessManager_h_
#include "mozilla/MozPromise.h"
#include "mozilla/PRemoteDecoderManagerChild.h"
#include "mozilla/RDDProcessHost.h"
#include "mozilla/dom/ipc/IdType.h"
#include "mozilla/ipc/TaskFactory.h"
#include "mozilla/PRDDChild.h"
#include "nsIObserver.h"

namespace mozilla {

class MemoryReportingProcess;
class RDDChild;

// The RDDProcessManager is a singleton responsible for creating RDD-bound
// objects that may live in another process. Currently, it provides access
// to the RDD process via ContentParent.
class RDDProcessManager final : public RDDProcessHost::Listener {
  friend class RDDChild;

 public:
  static void Initialize();
  static void RDDProcessShutdown();
  static void Shutdown();
  static RDDProcessManager* Get();

  ~RDDProcessManager();

  using EnsureRDDPromise =
      MozPromise<ipc::Endpoint<PRemoteDecoderManagerChild>, nsresult, true>;
  // Launch a new RDD process asynchronously
  RefPtr<GenericNonExclusivePromise> LaunchRDDProcess();
  // If not using a RDD process, launch a new RDD process asynchronously and
  // create a RemoteDecoderManager bridge
  RefPtr<EnsureRDDPromise> EnsureRDDProcessAndCreateBridge(
      base::ProcessId aOtherProcess, dom::ContentParentId aParentId);

  void OnProcessUnexpectedShutdown(RDDProcessHost* aHost) override;

  // Notify the RDDProcessManager that a top-level PRDD protocol has been
  // terminated. This may be called from any thread.
  void NotifyRemoteActorDestroyed(const uint64_t& aProcessToken);

  // Returns -1 if there is no RDD process, or the platform pid for it.
  base::ProcessId RDDProcessPid();

  // If a RDD process is present, create a MemoryReportingProcess object.
  // Otherwise, return null.
  RefPtr<MemoryReportingProcess> GetProcessMemoryReporter();

  // Returns access to the PRDD protocol if a RDD process is present.
  RDDChild* GetRDDChild() { return mRDDChild; }

  // Returns whether or not a RDD process was ever launched.
  bool AttemptedRDDProcess() const { return mNumProcessAttempts > 0; }

  // Returns the RDD Process
  RDDProcessHost* Process() { return mProcess; }

  /*
   * ** Test-only Method **
   *
   * Trigger RDD-process test metric instrumentation.
   */
  RefPtr<PRDDChild::TestTriggerMetricsPromise> TestTriggerMetrics();

 private:
  bool IsRDDProcessLaunching();
  bool IsRDDProcessDestroyed() const;
  bool CreateVideoBridge();

  // Called from our xpcom-shutdown observer.
  void OnXPCOMShutdown();
  void OnPreferenceChange(const char16_t* aData);

  RDDProcessManager();

  // Shutdown the RDD process.
  void DestroyProcess();

  bool IsShutdown() const;

  DISALLOW_COPY_AND_ASSIGN(RDDProcessManager);

  class Observer final : public nsIObserver {
   public:
    NS_DECL_ISUPPORTS
    NS_DECL_NSIOBSERVER
    explicit Observer(RDDProcessManager* aManager);

   protected:
    ~Observer() = default;

    RDDProcessManager* mManager;
  };
  friend class Observer;

  bool CreateContentBridge(
      base::ProcessId aOtherProcess, dom::ContentParentId aParentId,
      ipc::Endpoint<PRemoteDecoderManagerChild>* aOutRemoteDecoderManager);

  const RefPtr<Observer> mObserver;
  ipc::TaskFactory<RDDProcessManager> mTaskFactory;
  uint32_t mNumProcessAttempts = 0;
  uint32_t mNumUnexpectedCrashes = 0;

  // Fields that are associated with the current RDD process.
  RDDProcessHost* mProcess = nullptr;
  uint64_t mProcessToken = 0;
  RDDChild* mRDDChild = nullptr;
  // Collects any pref changes that occur during process launch (after
  // the initial map is passed in command-line arguments) to be sent
  // when the process can receive IPC messages.
  nsTArray<dom::Pref> mQueuedPrefs;
  // Promise will be resolved when the RDD process has been fully started and
  // VideoBridge configured. Only accessed on the main thread.
  RefPtr<GenericNonExclusivePromise> mLaunchRDDPromise;
};

}  // namespace mozilla

#endif  // _include_dom_media_ipc_RDDProcessManager_h_