File: internals_compute_pressure.cc

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 (187 lines) | stat: -rw-r--r-- 7,968 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/modules/compute_pressure/testing/internals_compute_pressure.h"

#include "mojo/public/cpp/bindings/remote.h"
#include "services/device/public/mojom/pressure_manager.mojom-blink.h"
#include "services/device/public/mojom/pressure_update.mojom-blink.h"
#include "third_party/blink/public/test/mojom/compute_pressure/web_pressure_manager_automation.test-mojom-blink.h"
#include "third_party/blink/renderer/bindings/core/v8/idl_types.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_create_virtual_pressure_source_options.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/heap/persistent.h"

namespace blink {

namespace {

device::mojom::blink::VirtualPressureSourceMetadataPtr ToMojoPressureMetadata(
    CreateVirtualPressureSourceOptions* options) {
  if (!options) {
    return device::mojom::blink::VirtualPressureSourceMetadata::New();
  }

  auto metadata = device::mojom::blink::VirtualPressureSourceMetadata::New();
  metadata->available = options->supported();
  return metadata;
}

device::mojom::blink::PressureSource ToMojoPressureSource(
    V8PressureSource::Enum source) {
  switch (source) {
    case blink::V8PressureSource::Enum::kCpu:
      return device::mojom::blink::PressureSource::kCpu;
  }
}

device::mojom::blink::PressureState ToMojoPressureState(
    V8PressureState::Enum state) {
  switch (state) {
    case blink::V8PressureState::Enum::kNominal:
      return device::mojom::blink::PressureState::kNominal;
    case blink::V8PressureState::Enum::kFair:
      return device::mojom::blink::PressureState::kFair;
    case blink::V8PressureState::Enum::kSerious:
      return device::mojom::blink::PressureState::kSerious;
    case blink::V8PressureState::Enum::kCritical:
      return device::mojom::blink::PressureState::kCritical;
  }
}

ExecutionContext* GetExecutionContext(ScriptState* script_state) {
  // Although this API is available for workers as well, the
  // InternalsComputePressure calls are always made on a Window object via
  // testdriver.js.
  LocalDOMWindow* window = LocalDOMWindow::From(script_state);
  CHECK(window);
  return window;
}

}  // namespace

// static
ScriptPromise<IDLUndefined>
InternalsComputePressure::createVirtualPressureSource(
    ScriptState* script_state,
    Internals&,
    V8PressureSource source,
    CreateVirtualPressureSourceOptions* options) {
  auto* execution_context = GetExecutionContext(script_state);
  mojo::Remote<test::mojom::blink::WebPressureManagerAutomation>
      web_pressure_manager_automation;
  execution_context->GetBrowserInterfaceBroker().GetInterface(
      web_pressure_manager_automation.BindNewPipeAndPassReceiver());

  auto* resolver =
      MakeGarbageCollected<ScriptPromiseResolver<IDLUndefined>>(script_state);
  auto promise = resolver->Promise();
  auto* raw_pressure_manager_automation = web_pressure_manager_automation.get();
  raw_pressure_manager_automation->CreateVirtualPressureSource(
      ToMojoPressureSource(source.AsEnum()), ToMojoPressureMetadata(options),
      WTF::BindOnce(
          // While we only really need |resolver|, we also take the
          // mojo::Remote<> so that it remains alive after this function exits.
          [](ScriptPromiseResolver<IDLUndefined>* resolver,
             mojo::Remote<test::mojom::blink::WebPressureManagerAutomation>,
             test::mojom::blink::CreateVirtualPressureSourceResult result) {
            switch (result) {
              case test::mojom::blink::CreateVirtualPressureSourceResult::
                  kSuccess:
                resolver->Resolve();
                break;
              case test::mojom::blink::CreateVirtualPressureSourceResult::
                  kSourceTypeAlreadyOverridden:
                resolver->Reject(
                    "This pressure source type has already been created");
                break;
            }
            resolver->Resolve();
          },
          WrapPersistent(resolver),
          std::move(web_pressure_manager_automation)));
  return promise;
}

// static
ScriptPromise<IDLUndefined>
InternalsComputePressure::removeVirtualPressureSource(ScriptState* script_state,
                                                      Internals&,
                                                      V8PressureSource source) {
  auto* execution_context = GetExecutionContext(script_state);
  mojo::Remote<test::mojom::blink::WebPressureManagerAutomation>
      web_pressure_manager_automation;
  execution_context->GetBrowserInterfaceBroker().GetInterface(
      web_pressure_manager_automation.BindNewPipeAndPassReceiver());

  auto* resolver =
      MakeGarbageCollected<ScriptPromiseResolver<IDLUndefined>>(script_state);
  auto promise = resolver->Promise();
  auto* raw_pressure_manager_automation = web_pressure_manager_automation.get();
  raw_pressure_manager_automation->RemoveVirtualPressureSource(
      ToMojoPressureSource(source.AsEnum()),
      WTF::BindOnce(
          // While we only really need |resolver|, we also take the
          // mojo::Remote<> so that it remains alive after this function exits.
          [](ScriptPromiseResolver<IDLUndefined>* resolver,
             mojo::Remote<test::mojom::blink::WebPressureManagerAutomation>) {
            resolver->Resolve();
          },
          WrapPersistent(resolver),
          std::move(web_pressure_manager_automation)));
  return promise;
}

// static
ScriptPromise<IDLUndefined>
InternalsComputePressure::updateVirtualPressureSource(
    ScriptState* script_state,
    Internals&,
    V8PressureSource source,
    V8PressureState state,
    double own_contribution_estimate) {
  auto* execution_context = GetExecutionContext(script_state);
  mojo::Remote<test::mojom::blink::WebPressureManagerAutomation>
      web_pressure_manager_automation;
  execution_context->GetBrowserInterfaceBroker().GetInterface(
      web_pressure_manager_automation.BindNewPipeAndPassReceiver());

  auto* resolver =
      MakeGarbageCollected<ScriptPromiseResolver<IDLUndefined>>(script_state);
  auto promise = resolver->Promise();
  auto* raw_pressure_manager_automation = web_pressure_manager_automation.get();
  raw_pressure_manager_automation->UpdateVirtualPressureSourceData(
      ToMojoPressureSource(source.AsEnum()),
      ToMojoPressureState(state.AsEnum()), own_contribution_estimate,
      WTF::BindOnce(
          // While we only really need |resolver|, we also take the
          // mojo::Remote<> so that it remains alive after this function exits.
          [](ScriptPromiseResolver<IDLUndefined>* resolver,
             mojo::Remote<test::mojom::blink::WebPressureManagerAutomation>,
             test::mojom::UpdateVirtualPressureSourceDataResult result) {
            switch (result) {
              case test::mojom::blink::UpdateVirtualPressureSourceDataResult::
                  kSuccess: {
                resolver->Resolve();
                break;
              }
              case test::mojom::blink::UpdateVirtualPressureSourceDataResult::
                  kSourceTypeNotOverridden:
                resolver->Reject(
                    "A virtual pressure source with this type has not been "
                    "created");
                break;
            }
          },
          WrapPersistent(resolver),
          std::move(web_pressure_manager_automation)));
  return promise;
}

}  // namespace blink