File: browser_child_process_watcher.h

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (99 lines) | stat: -rw-r--r-- 3,956 bytes parent folder | download | duplicates (6)
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
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_PERFORMANCE_MANAGER_BROWSER_CHILD_PROCESS_WATCHER_H_
#define COMPONENTS_PERFORMANCE_MANAGER_BROWSER_CHILD_PROCESS_WATCHER_H_

#include <memory>

#include "base/containers/flat_map.h"
#include "base/process/process.h"
#include "components/performance_manager/public/browser_child_process_host_id.h"
#include "content/public/browser/browser_child_process_observer.h"

namespace base {
class Process;
}

namespace performance_manager {

class ProcessNodeImpl;

// Responsible for maintaining the process nodes for the browser, the GPU and
// utility process.
class BrowserChildProcessWatcher : public content::BrowserChildProcessObserver {
 public:
  BrowserChildProcessWatcher();

  BrowserChildProcessWatcher(const BrowserChildProcessWatcher&) = delete;
  BrowserChildProcessWatcher& operator=(const BrowserChildProcessWatcher&) =
      delete;

  ~BrowserChildProcessWatcher() override;

  // Initialize this watcher.
  void Initialize();

  // Tear down this watcher and any state it's gathered.
  void TearDown();

  // Returns the ProcessNode for the browser process or nullptr if it does not
  // exist, which can happen in tests.
  ProcessNodeImpl* browser_process_node() {
    return browser_process_node_.get();
  }

  // Returns the ProcessNode for `id` or nullptr if it does not exist.
  ProcessNodeImpl* GetChildProcessNode(BrowserChildProcessHostId id);

  // Allows tests to create a ProcessNode for `data`. In production the
  // ProcessNode is created when the host's child process is launched, but it's
  // not always possible to launch a real process in tests so this can simulate
  // it by passing a running process, possibly base::Process::Current(), in
  // data.GetProcess(). The ProcessNode must not already exist. It will not be
  // tied to the lifetime of the process.
  void CreateChildProcessNodeForTesting(const content::ChildProcessData& data);

  // Allows tests to delete the ProcessNode for `data`. In production this
  // happens when the host's child process exits, but nodes created with
  // CreateChildProcessNodeForTesting() won't be tied to the lifetime of
  // a real process. Must not be called again if the node was already deleted.
  void DeleteChildProcessNodeForTesting(const content::ChildProcessData& data);

  // Allows tests to delete the browser process node, which is created by
  // default in Initialize(). Must not be called again if the node was already
  // deleted.
  void DeleteBrowserProcessNodeForTesting();

 private:
  // BrowserChildProcessObserver overrides.
  void BrowserChildProcessLaunchedAndConnected(
      const content::ChildProcessData& data) override;
  void BrowserChildProcessHostDisconnected(
      const content::ChildProcessData& data) override;
  void BrowserChildProcessCrashed(
      const content::ChildProcessData& data,
      const content::ChildProcessTerminationInfo& info) override;
  void BrowserChildProcessKilled(
      const content::ChildProcessData& data,
      const content::ChildProcessTerminationInfo& info) override;

  void TrackedProcessExited(BrowserChildProcessHostId id, int exit_code);

  static void OnProcessLaunched(const base::Process& process,
                                const std::string& metrics_name,
                                ProcessNodeImpl* process_node);

  std::unique_ptr<ProcessNodeImpl> browser_process_node_;

  // This map keeps track of all GPU and Utility processes by their unique ID
  // from |content::ChildProcessData|. Apparently more than one GPU process can
  // be existent at a time, though secondaries are very transient.
  base::flat_map<BrowserChildProcessHostId, std::unique_ptr<ProcessNodeImpl>>
      tracked_process_nodes_;
};

}  // namespace performance_manager

#endif  // COMPONENTS_PERFORMANCE_MANAGER_BROWSER_CHILD_PROCESS_WATCHER_H_