File: RemoteWorkerManager.h

package info (click to toggle)
firefox 143.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,617,328 kB
  • sloc: cpp: 7,478,492; javascript: 6,417,157; ansic: 3,720,058; python: 1,396,372; xml: 627,523; asm: 438,677; java: 186,156; sh: 63,477; makefile: 19,171; objc: 13,059; perl: 12,983; yacc: 4,583; cs: 3,846; pascal: 3,405; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (112 lines) | stat: -rw-r--r-- 4,516 bytes parent folder | download | duplicates (4)
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
/* -*- 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 mozilla_dom_RemoteWorkerManager_h
#define mozilla_dom_RemoteWorkerManager_h

#include "base/process.h"
#include "mozilla/RefPtr.h"
#include "mozilla/dom/ContentParent.h"
#include "mozilla/dom/RemoteWorkerTypes.h"
#include "mozilla/dom/WorkerPrivate.h"  // WorkerKind enum
#include "nsISupportsImpl.h"
#include "nsTArray.h"

namespace mozilla::dom {

class RemoteWorkerController;
class RemoteWorkerServiceParent;
class RemoteWorkerNonLifeCycleOpControllerParent;

/**
 * PBackground instance that keeps tracks of RemoteWorkerServiceParent actors
 * (1 per process, including the main process). Decides which
 * RemoteWorkerServerParent to use internally via SelectTargetActor in order to
 * select a BackgroundParent manager on which to create a RemoteWorkerParent.
 */
class RemoteWorkerManager final {
 public:
  NS_INLINE_DECL_REFCOUNTING(RemoteWorkerManager)

  static already_AddRefed<RemoteWorkerManager> GetOrCreate();

  void RegisterActor(RemoteWorkerServiceParent* aActor);

  void UnregisterActor(RemoteWorkerServiceParent* aActor);

  void Launch(RemoteWorkerController* aController,
              const RemoteWorkerData& aData, base::ProcessId aProcessId);

  static bool MatchRemoteType(const nsACString& processRemoteType,
                              const nsACString& workerRemoteType);

  /**
   * Get the child process RemoteType where a RemoteWorker should be
   * launched.
   */
  static Result<nsCString, nsresult> GetRemoteType(
      const nsCOMPtr<nsIPrincipal>& aPrincipal, WorkerKind aWorkerKind);

  static bool HasExtensionPrincipal(const RemoteWorkerData& aData);

 private:
  RemoteWorkerManager();
  ~RemoteWorkerManager();

  struct TargetActorAndKeepAlive {
    RefPtr<RemoteWorkerServiceParent> mActor;
    UniqueThreadsafeContentParentKeepAlive mKeepAlive;
  };

  TargetActorAndKeepAlive SelectTargetActor(const RemoteWorkerData& aData,
                                            base::ProcessId aProcessId);

  TargetActorAndKeepAlive SelectTargetActorInternal(
      const RemoteWorkerData& aData, base::ProcessId aProcessId) const;

  void LaunchInternal(RemoteWorkerController* aController,
                      RemoteWorkerServiceParent* aTargetActor,
                      UniqueThreadsafeContentParentKeepAlive&& aKeepAlive,
                      const RemoteWorkerData& aData);

  using LaunchProcessPromise =
      MozPromise<TargetActorAndKeepAlive, nsresult, true>;
  RefPtr<LaunchProcessPromise> LaunchNewContentProcess(
      const RemoteWorkerData& aData);

  void AsyncCreationFailed(RemoteWorkerController* aController);

  // Iterate through all RemoteWorkerServiceParent actors with the given
  // remoteType, starting from the actor related to a child process with pid
  // aProcessId if needed and available or from a random index otherwise (as if
  // iterating through a circular array).
  //
  // aCallback should be a invokable object with a function signature of
  //   bool (RemoteWorkerServiceParent*, RefPtr<ContentParent>&&)
  //
  // aCallback is called with the actor and corresponding ContentParent, should
  // return false to abort iteration before all actors have been traversed (e.g.
  // if the desired actor is found), and must not mutate mChildActors (which
  // shouldn't be an issue because this function is const). aCallback also
  // doesn't need to worry about proxy-releasing the ContentParent if it isn't
  // moved out of the parameter.
  template <typename Callback>
  void ForEachActor(Callback&& aCallback, const nsACString& aRemoteType,
                    Maybe<base::ProcessId> aProcessId = Nothing()) const;

  // The list of existing RemoteWorkerServiceParent actors for child processes.
  // Raw pointers because RemoteWorkerServiceParent actors unregister themselves
  // when destroyed.
  // XXX For Fission, where we could have a lot of child actors, should we maybe
  // instead keep either a hash table (PID->actor) or perhaps store the actors
  // in order, sorted by PID, to avoid linear lookup times?
  nsTArray<RemoteWorkerServiceParent*> mChildActors;
  RemoteWorkerServiceParent* mParentActor;
};

}  // namespace mozilla::dom

#endif  // mozilla_dom_RemoteWorkerManager_h