File: subscriber.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 (296 lines) | stat: -rw-r--r-- 11,913 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Copyright 2023 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/core/dom/subscriber.h"

#include "base/containers/adapters.h"
#include "base/containers/contains.h"
#include "third_party/blink/public/mojom/use_counter/metrics/web_feature.mojom-blink.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_observer.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_observer_callback.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_observer_complete_callback.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_script_runner.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_subscribe_options.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_void_function.h"
#include "third_party/blink/renderer/core/dom/abort_controller.h"
#include "third_party/blink/renderer/core/dom/abort_signal.h"
#include "third_party/blink/renderer/core/dom/observable.h"
#include "third_party/blink/renderer/core/dom/observable_internal_observer.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/platform/instrumentation/use_counter.h"

namespace blink {

using PassKey = base::PassKey<Subscriber>;

class Subscriber::ConsumerAbortSubscriptionAlgorithm final
    : public AbortSignal::Algorithm {
 public:
  explicit ConsumerAbortSubscriptionAlgorithm(
      Subscriber& subscriber,
      ObservableInternalObserver& associated_observer,
      AbortSignal& signal,
      ScriptState& script_state)
      : subscriber_(subscriber),
        associated_observer_(associated_observer),
        signal_(signal),
        script_state_(script_state) {
    CHECK(script_state_->ContextIsValid());
  }
  ~ConsumerAbortSubscriptionAlgorithm() override = default;

  void Run() override {
    subscriber_->ConsumerUnsubscribe(script_state_, associated_observer_,
                                     signal_->reason(script_state_));
  }

  void Trace(Visitor* visitor) const override {
    visitor->Trace(subscriber_);
    visitor->Trace(associated_observer_);
    visitor->Trace(signal_);
    visitor->Trace(script_state_);
    Algorithm::Trace(visitor);
  }

 private:
  Member<Subscriber> subscriber_;
  // This is the observer associated with the `signal_` that it subscribed to
  // `subscriber_` with. `this` keeps both around so when the `signal_` gets
  // aborted (i.e., `Run()` is called above) we can alert `subscriber_` as to
  // which observer needs to be unregistered so that it doesn't receive values
  // from the producer.
  Member<ObservableInternalObserver> associated_observer_;
  Member<AbortSignal> signal_;
  Member<ScriptState> script_state_;
};

Subscriber::Subscriber(base::PassKey<Observable>,
                       ScriptState* script_state,
                       ObservableInternalObserver* internal_observer,
                       SubscribeOptions* options)
    : ExecutionContextClient(ExecutionContext::From(script_state)),
      subscription_controller_(AbortController::Create(script_state)) {
  internal_observers_.push_back(internal_observer);

  // If a downstream `AbortSignal` is provided, setup an instance of
  // `ConsumerAbortSubscriptionAlgorithm` as one of its internal abort
  // algorithms. It enables `this` to close the subscription that `this`
  // represents in response to downstream aborts.
  if (options->hasSignal()) {
    AbortSignal* downstream_signal = options->signal();

    if (downstream_signal->aborted()) {
      internal_observers_.pop_back();
      CloseSubscription(
          script_state,
          /*abort_reason=*/downstream_signal->reason(script_state));
    } else {
      // Add an abort algorithm to the consumer's signal. Keep the algorithm
      // alive by associating it with the `internal_observer`.
      consumer_abort_algorithms_.insert(
          internal_observer,
          downstream_signal->AddAlgorithm(
              MakeGarbageCollected<ConsumerAbortSubscriptionAlgorithm>(
                  *this, *internal_observer, *downstream_signal,
                  *script_state)));
    }
  }
}

void Subscriber::next(ScriptValue value) {
  if (!active_) {
    return;
  }

  // Call `Next()` on all observers. Do this by iterating over a *copy* of the
  // list of observers, because `Next()` can actually complete one of the
  // observers' subscriptions, thus removing `observer` from
  // `internal_observers_`. That means `internal_observers_` can be mutated
  // throughout this process, and we cannot iterate over it while it is
  // mutating.
  HeapVector<Member<ObservableInternalObserver>> internal_observers =
      internal_observers_;
  for (auto& observer : internal_observers) {
    observer->Next(value);
  }
}

void Subscriber::complete(ScriptState* script_state) {
  if (!active_) {
    return;
  }

  // `CloseSubscription()` makes it impossible to invoke user-provided callbacks
  // via `internal_observers_` anymore/re-entrantly, which is why we pull the
  // `internal_observer` out before calling this.
  CloseSubscription(script_state, /*abort_reason=*/std::nullopt);

  // See the documentation in `Subscriber::next()`.
  HeapVector<Member<ObservableInternalObserver>> internal_observers =
      internal_observers_;
  for (auto& observer : internal_observers) {
    observer->Complete();
  }
}

void Subscriber::error(ScriptState* script_state, ScriptValue error_value) {
  if (!active_) {
    // If `active_` is false, the subscription has already been closed by
    // `CloseSubscription()`. In this case, if the observable is still producing
    // errors, we must surface them to the global via "report the exception":
    // https://html.spec.whatwg.org/C#report-the-exception.
    //
    // Reporting the exception requires a valid `ScriptState`, which we don't
    // have if we're in a detached context. See observable-constructor.window.js
    // for tests.
    if (!script_state->ContextIsValid()) {
      CHECK(!GetExecutionContext());
      return;
    }
    ScriptState::Scope scope(script_state);
    V8ScriptRunner::ReportException(script_state->GetIsolate(),
                                    error_value.V8Value());
    return;
  }

  // `CloseSubscription()` makes it impossible to invoke user-provided callbacks
  // via `internal_observers_` anymore/re-entrantly, which is why we pull the
  // `internal_observer` out before calling this.
  CloseSubscription(script_state, error_value);

  // See the documentation in `Subscriber::next()`.
  HeapVector<Member<ObservableInternalObserver>> internal_observers =
      internal_observers_;
  for (auto& observer : internal_observers) {
    observer->Error(script_state, error_value);
  }
}

void Subscriber::addTeardown(V8VoidFunction* teardown) {
  if (active_) {
    teardown_callbacks_.push_back(teardown);
  } else {
    // If the subscription is inactive, invoke the teardown immediately, because
    // if we just queue it to `teardown_callbacks_` it will never run!
    teardown->InvokeAndReportException(nullptr);
  }
}

AbortSignal* Subscriber::signal() const {
  return subscription_controller_->signal();
}

void Subscriber::ConsumerUnsubscribe(
    ScriptState* script_state,
    ObservableInternalObserver* associated_observer,
    std::optional<ScriptValue> abort_reason) {
  // If the producer closes the subscription before any consumer abort signal
  // algorithms attempt, the consumer abort signal algorithms can still fire,
  // attempting to close the subscription. Do nothing if it's already closed.
  if (!active_) {
    return;
  }

  // Now that the abort algorithm has run, clear the
  // `AbortSignal::AlgorithmHandle` associated with `associated_observer` that's
  // keeping it alive.
  DCHECK(base::Contains(consumer_abort_algorithms_, associated_observer));
  consumer_abort_algorithms_.erase(associated_observer);

  // Also remove `associated_observer` from `internal_observers_`, since it no
  // longer cares about values `this` produces.
  DCHECK(base::Contains(internal_observers_, associated_observer));
  internal_observers_.erase(
      std::ranges::find(internal_observers_, associated_observer));

  if (internal_observers_.empty()) {
    CloseSubscription(script_state, abort_reason);
  }
}

void Subscriber::CloseSubscription(ScriptState* script_state,
                                   std::optional<ScriptValue> abort_reason) {
  // Guard against re-entrant invocation, which can happen during
  // producer-initiated unsubscription. For example: `complete()` ->
  // `CloseSubscription()` -> Run script (either by aborting an `AbortSignal` or
  // running a teardown) -> Script aborts the downstream `AbortSignal` (the one
  // passed in via `SubscribeOptions` in the constructor) -> the downstream
  // signal's internal abort algorithm runs ->
  // `Subscriber::ConsumerAbortSubscriptionAlgorithm::Run()` ->
  // `CloseSubscription()`.
  if (!active_) {
    return;
  }

  // We no longer need to hold onto the consumer abort algorithms.
  consumer_abort_algorithms_.clear();

  // There are three things to do when the signal associated with a subscription
  // gets aborted.
  //  1. Mark the subscription as inactive. This only makes the web-exposed
  //     `Subscriber#active` false, and makes it impossible for `this` to emit
  //     any more values to downstream `Observer`-provided callbacks.
  active_ = false;

  // 2. Abort `subscription_controller_`. This actually does two things:
  //    (a) Immediately aborts any "upstream" subscriptions, i.e., any
  //        observables that the observable associated with `this` had
  //        subscribed to, if any exist.
  //    (2) Fires the abort event at `this`'s signal.
  CHECK(!subscription_controller_->signal()->aborted());
  if (abort_reason) {
    subscription_controller_->abort(script_state, *abort_reason);
  } else {
    subscription_controller_->abort(script_state);
  }

  // 3. Run all teardown callbacks that were registered with
  //    `Subscriber#addTeardown()` in LIFO order, and then remove all of them.
  //
  // Note that since the subscription is now inactive, `teardown_callbacks_`
  // cannot be modified anymore. If any of these callbacks below invoke
  // `addTeardown()` with a *new* callback, it will be invoked synchronously
  // instead of added to this vector.
  for (Member<V8VoidFunction> teardown : base::Reversed(teardown_callbacks_)) {
    teardown->InvokeAndReportException(nullptr);
  }
  teardown_callbacks_.clear();
}

void Subscriber::RegisterNewObserver(ScriptState* script_state,
                                     ObservableInternalObserver* observer,
                                     SubscribeOptions* options) {
  // We can only hit this path if there is already at least one subscriber that
  // forced the creation of `this` and subscribed via `this`'s constructor.
  CHECK_GE(internal_observers_.size(), 1u);
  internal_observers_.push_back(observer);

  if (options->hasSignal()) {
    AbortSignal* downstream_signal = options->signal();

    if (downstream_signal->aborted()) {
      internal_observers_.pop_back();
      return;
    }

    ConsumerAbortSubscriptionAlgorithm* abort_algorithm =
        MakeGarbageCollected<ConsumerAbortSubscriptionAlgorithm>(
            *this, *observer, *downstream_signal, *script_state);
    AbortSignal::AlgorithmHandle* maybe_close_algorithm_handle =
        downstream_signal->AddAlgorithm(abort_algorithm);
    consumer_abort_algorithms_.insert(observer, maybe_close_algorithm_handle);
  }
}

void Subscriber::Trace(Visitor* visitor) const {
  visitor->Trace(subscription_controller_);
  visitor->Trace(consumer_abort_algorithms_);
  visitor->Trace(teardown_callbacks_);
  visitor->Trace(internal_observers_);

  ScriptWrappable::Trace(visitor);
  ExecutionContextClient::Trace(visitor);
}

}  // namespace blink