File: pressure_service_base.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; 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 (127 lines) | stat: -rw-r--r-- 4,722 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_COMPUTE_PRESSURE_PRESSURE_SERVICE_BASE_H_
#define CONTENT_BROWSER_COMPUTE_PRESSURE_PRESSURE_SERVICE_BASE_H_

#include <array>
#include <optional>

#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/process/process_metrics.h"
#include "base/sequence_checker.h"
#include "base/thread_annotations.h"
#include "base/unguessable_token.h"
#include "content/browser/compute_pressure/pressure_client_impl.h"
#include "content/browser/compute_pressure/web_contents_pressure_manager_proxy.h"
#include "content/common/content_export.h"
#include "mojo/public/cpp/bindings/pending_associated_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/device/public/cpp/compute_pressure/cpu_pressure_converter.h"
#include "services/device/public/mojom/pressure_manager.mojom.h"
#include "third_party/blink/public/mojom/compute_pressure/web_pressure_manager.mojom.h"

namespace content {

class RenderFrameHost;

// This class holds common functions for both frame and workers. It serves all
// the Compute Pressure API mojo requests.
//
// This class is not thread-safe, so each instance must be used on one sequence.
class CONTENT_EXPORT PressureServiceBase
    : public blink::mojom::WebPressureManager,
      public WebContentsPressureManagerProxy::Observer {
 public:
  ~PressureServiceBase() override;

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

  // https://www.w3.org/TR/compute-pressure/#dfn-document-has-implicit-focus
  static bool HasImplicitFocus(RenderFrameHost* render_frame_host);

  void BindReceiver(
      mojo::PendingReceiver<blink::mojom::WebPressureManager> receiver);

  virtual bool CanCallAddClient() const;

  // blink::mojom::WebPressureManager implementation.
  void AddClient(
      device::mojom::PressureSource source,
      mojo::PendingAssociatedRemote<blink::mojom::WebPressureClient> client,
      AddClientCallback callback) override;

  // WebContentsPressureManagerProxy::Observer implementation.
  void DidAddVirtualPressureSource(device::mojom::PressureSource) override;
  void DidRemoveVirtualPressureSource(device::mojom::PressureSource) override;

  // Verifies if the data should be delivered according to focus status.
  virtual bool ShouldDeliverUpdate() const = 0;
  virtual double CalculateOwnContributionEstimate(
      double global_cpu_utilization) = 0;
  device::mojom::PressureState CalculateState(double global_cpu_utilization);

  // Returns a token for use with automation calls when one is set.
  virtual std::optional<base::UnguessableToken> GetTokenFor(
      device::mojom::PressureSource) const = 0;

  bool IsManagerReceiverBoundForTesting() const {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

    return manager_receiver_.is_bound();
  }

  const PressureClientImpl& GetPressureClientForTesting(
      device::mojom::PressureSource source) const {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

    return source_to_client_[static_cast<size_t>(source)];
  }

 protected:
  PressureServiceBase();

  SEQUENCE_CHECKER(sequence_checker_);

  // boolean parameter when true allows creation of the object if not found.
  WebContentsPressureManagerProxy* GetWebContentsPressureManagerProxy(
      bool allow_creation = false) const;

 private:
  virtual RenderFrameHost* GetRenderFrameHost() const;

  void AddMessageToConsole(const std::string&) const;

  void OnPressureManagerDisconnected();

  void DidAddClient(device::mojom::PressureSource source,
                    const std::optional<base::UnguessableToken>&,
                    AddClientCallback client_callback,
                    device::mojom::PressureManagerAddClientResult);

  // Handles break calibration mitigation and conversion from
  // cpu_utilization to PressureState.
  device::CpuPressureConverter converter_;

  // Services side.
  mojo::Remote<device::mojom::PressureManager> manager_remote_
      GUARDED_BY_CONTEXT(sequence_checker_);

  // Blink side.
  mojo::Receiver<blink::mojom::WebPressureManager> GUARDED_BY_CONTEXT(
      sequence_checker_) manager_receiver_{this};

  std::array<PressureClientImpl,
             static_cast<size_t>(device::mojom::PressureSource::kMaxValue) + 1>
      source_to_client_ GUARDED_BY_CONTEXT(sequence_checker_);

  base::WeakPtrFactory<PressureServiceBase> weak_ptr_factory_{this};
};

}  // namespace content

#endif  // CONTENT_BROWSER_COMPUTE_PRESSURE_PRESSURE_SERVICE_BASE_H_