File: pressure_observer.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,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 (154 lines) | stat: -rw-r--r-- 6,113 bytes parent folder | download | duplicates (5)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// 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 THIRD_PARTY_BLINK_RENDERER_MODULES_COMPUTE_PRESSURE_PRESSURE_OBSERVER_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_COMPUTE_PRESSURE_PRESSURE_OBSERVER_H_

#include <array>

#include "services/device/public/mojom/pressure_manager.mojom-blink.h"
#include "services/device/public/mojom/pressure_update.mojom-blink.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_pressure_source.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_pressure_state.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_pressure_update_callback.h"
#include "third_party/blink/renderer/core/dom/dom_high_res_time_stamp.h"
#include "third_party/blink/renderer/modules/compute_pressure/change_rate_monitor.h"
#include "third_party/blink/renderer/platform/bindings/exception_code.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/heap/collection_support/heap_hash_set.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cancellable_task.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "third_party/blink/renderer/platform/wtf/wtf_size_t.h"

namespace blink {

namespace {

// https://w3c.github.io/compute-pressure/#dfn-max-queued-records
constexpr wtf_size_t kMaxQueuedRecords = 10;

}  // namespace

class ExecutionContext;
class ExceptionState;
class PressureObserverManager;
class PressureObserverOptions;
class PressureRecord;
class ScriptState;

class MODULES_EXPORT PressureObserver final : public ScriptWrappable {
  DEFINE_WRAPPERTYPEINFO();

 public:
  explicit PressureObserver(V8PressureUpdateCallback*);
  ~PressureObserver() override;

  static PressureObserver* Create(V8PressureUpdateCallback*);

  // PressureObserver IDL implementation.
  ScriptPromise<IDLUndefined> observe(ScriptState*,
                                      V8PressureSource,
                                      PressureObserverOptions*,
                                      ExceptionState&);
  void unobserve(V8PressureSource);
  void disconnect();
  HeapVector<Member<PressureRecord>> takeRecords();
  static Vector<V8PressureSource> knownSources();

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

  // GarbageCollected implementation.
  void Trace(blink::Visitor*) const override;

  // Called by PressureClientImpl.
  void OnUpdate(ExecutionContext*,
                V8PressureSource::Enum,
                V8PressureState::Enum,
                double own_contribution_estimate,
                DOMHighResTimeStamp);

  // Called by PressureObserverManager.
  void OnBindingSucceeded(V8PressureSource::Enum);
  void OnBindingFailed(V8PressureSource::Enum, DOMExceptionCode);
  void OnConnectionError();

  ChangeRateMonitor& change_rate_monitor_for_testing() {
    return change_rate_monitor_;
  }

 private:
  // Verifies if the latest update was at least longer than the sample period.
  bool PassesRateTest(V8PressureSource::Enum, const DOMHighResTimeStamp&) const;

  // Verifies if there is data change in between last update and new one.
  bool ShouldDispatch(V8PressureSource::Enum,
                      V8PressureState::Enum,
                      double own_contribution_estimate) const;

  // Verifies if there is data changes in a defined time span is not too high.
  bool PassesRateObfuscation(V8PressureSource::Enum) const;

  // Queues valid `PressureRecord` to be reported after penalty.
  void QueueAfterPenaltyRecord(ExecutionContext*, V8PressureSource::Enum);

  // Queues valid `PressureRecord` to be reported.
  void QueuePressureRecord(ExecutionContext*,
                           V8PressureSource::Enum,
                           PressureRecord*);

  // Resolve/reject pending resolvers.
  void ResolvePendingResolvers(V8PressureSource::Enum);
  void RejectPendingResolvers(V8PressureSource::Enum,
                              DOMExceptionCode,
                              const String&);

  // Scheduled method to invoke callback.
  void ReportToCallback(ExecutionContext*);

  // Manages registered observer list for each source.
  WeakMember<PressureObserverManager> manager_;

  // The callback that receives pressure state updates.
  Member<V8PressureUpdateCallback> observer_callback_;

  // Requested sample interval from the user.
  // https://w3c.github.io/compute-pressure/#dfn-sampleinterval
  uint32_t sample_interval_ = 0;

  std::array<HeapHashSet<Member<ScriptPromiseResolver<IDLUndefined>>>,
             V8PressureSource::kEnumSize>
      pending_resolvers_;

  // Manages rate obfuscation mitigation parameters.
  ChangeRateMonitor change_rate_monitor_;

  // Last received valid record from PressureClientImpl.
  // Stored to avoid sending updates whenever the new record is the same.
  std::array<Member<PressureRecord>, V8PressureSource::kEnumSize>
      last_record_map_;

  // Last received valid record from PressureClientImpl during
  // the penalty duration, to restore when the penalty duration is over.
  std::array<Member<PressureRecord>, V8PressureSource::kEnumSize>
      after_penalty_records_;

  // Last received records from the platform collector.
  // The records are only collected when there is a change in the status.
  HeapVector<Member<PressureRecord>, kMaxQueuedRecords> records_;

  // Task handle to check if the posted task is still pending.
  TaskHandle pending_report_to_callback_;

  // Task handle array to check if the posted task is still pending.
  std::array<TaskHandle, V8PressureSource::kEnumSize>
      pending_delayed_report_to_callback_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_COMPUTE_PRESSURE_PRESSURE_OBSERVER_H_