File: dom_task.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 (272 lines) | stat: -rw-r--r-- 10,980 bytes parent folder | download | duplicates (2)
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
// Copyright 2019 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/scheduler/dom_task.h"

#include <optional>
#include <utility>

#include "base/check_op.h"
#include "base/metrics/histogram_macros.h"
#include "third_party/blink/public/common/scheduler/task_attribution_id.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/core/v8/script_value.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_scheduler_post_task_callback.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_throw_dom_exception.h"
#include "third_party/blink/renderer/core/dom/abort_signal.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/frame/web_feature.h"
#include "third_party/blink/renderer/core/inspector/inspector_trace_events.h"
#include "third_party/blink/renderer/core/probe/core_probes.h"
#include "third_party/blink/renderer/core/scheduler/dom_task_signal.h"
#include "third_party/blink/renderer/core/scheduler/scheduler_task_context.h"
#include "third_party/blink/renderer/core/scheduler/script_wrappable_task_state.h"
#include "third_party/blink/renderer/core/scheduler/web_scheduling_task_state.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"
#include "third_party/blink/renderer/platform/instrumentation/use_counter.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/scheduler/public/task_attribution_info.h"
#include "third_party/blink/renderer/platform/scheduler/public/task_attribution_tracker.h"
#include "third_party/blink/renderer/platform/scheduler/public/web_scheduling_priority.h"
#include "third_party/blink/renderer/platform/scheduler/public/web_scheduling_task_queue.h"
#include "third_party/blink/renderer/platform/wtf/casting.h"
#include "third_party/perfetto/include/perfetto/tracing/traced_value_forward.h"

namespace blink {

namespace {

class TaskPromiseResolveHandler final
    : public ThenCallable<IDLAny, TaskPromiseResolveHandler> {
 public:
  explicit TaskPromiseResolveHandler(DOMTask* task) : task_(task) {}

  void Trace(Visitor* visitor) const override {
    ThenCallable<IDLAny, TaskPromiseResolveHandler>::Trace(visitor);
    visitor->Trace(task_);
  }

  void React(ScriptState*, ScriptValue) { task_->OnPendingPromiseSettled(); }

 private:
  Member<DOMTask> task_;
};

}  // namespace

DOMTask::DOMTask(ScriptPromiseResolver<IDLAny>* resolver,
                 V8SchedulerPostTaskCallback* callback,
                 SchedulerTaskContext* task_context,
                 DOMScheduler::DOMTaskQueue* task_queue,
                 base::TimeDelta delay,
                 uint64_t task_id_for_tracing)
    : callback_(callback),
      resolver_(resolver),
      scheduler_task_context_(task_context),
      task_queue_(task_queue),
      delay_(delay),
      task_id_for_tracing_(task_id_for_tracing) {
  CHECK(task_queue_);
  CHECK(callback_);
  CHECK(scheduler_task_context_);

  if (AbortSignal* abort_source = scheduler_task_context_->AbortSource();
      abort_source && abort_source->CanAbort()) {
    abort_handle_ = abort_source->AddAlgorithm(
        WTF::BindOnce(&DOMTask::OnAbort, WrapWeakPersistent(this)));
  }

  task_handle_ = PostDelayedCancellableTask(
      task_queue_->GetTaskRunner(), FROM_HERE,
      WTF::BindOnce(&DOMTask::Invoke, WrapPersistent(this)), delay);

  ScriptState* script_state =
      callback_->CallbackRelevantScriptStateOrReportError("DOMTask", "Create");
  DCHECK(script_state && script_state->ContextIsValid());

  if (script_state->World().IsMainWorld()) {
    if (auto* tracker = scheduler::TaskAttributionTracker::From(
            script_state->GetIsolate())) {
      parent_task_ = tracker->RunningTask();
    }
  }

  auto* context = ExecutionContext::From(script_state);
  DEVTOOLS_TIMELINE_TRACE_EVENT_INSTANT(
      "SchedulePostTaskCallback", inspector_scheduler_schedule_event::Data,
      context, task_id_for_tracing_, task_queue_->GetPriority(),
      delay_.InMillisecondsF());
  async_task_context_.Schedule(context, "postTask");
}

void DOMTask::Trace(Visitor* visitor) const {
  visitor->Trace(callback_);
  visitor->Trace(resolver_);
  visitor->Trace(scheduler_task_context_);
  visitor->Trace(abort_handle_);
  visitor->Trace(task_queue_);
  visitor->Trace(parent_task_);
}

void DOMTask::Invoke() {
  CHECK(callback_);
  CHECK_EQ(execution_state_, ExecutionState::kNotStarted);

  // Tasks are not runnable if the document associated with this task's
  // scheduler's global is not fully active, which happens if the
  // ExecutionContext is detached. Note that this context can be different
  // from the the callback's relevant context.
  ExecutionContext* scheduler_context = resolver_->GetExecutionContext();
  if (!scheduler_context || scheduler_context->IsContextDestroyed()) {
    RemoveAbortAlgorithm();
    execution_state_ = ExecutionState::kFinished;
    return;
  }

  ScriptState* script_state =
      callback_->CallbackRelevantScriptStateOrReportError("DOMTask", "Invoke");
  if (!script_state || !script_state->ContextIsValid()) {
    DCHECK(resolver_->GetExecutionContext() &&
           !resolver_->GetExecutionContext()->IsContextDestroyed());
    // The scheduler's context is still attached, but the task's callback's
    // relvant context is not. This happens, for example, if an attached main
    // frame's scheduler schedules a task that runs a callback defined in a
    // detached child frame. The callback's relvant context must be valid to run
    // the callback (enforced in the bindings layer). Since we can't run this
    // task, and therefore won't settle the associated promise, we need to clean
    // up the ScriptPromiseResolverBase since it is associated with a different
    // context.
    resolver_->Detach();
    RemoveAbortAlgorithm();
    execution_state_ = ExecutionState::kFinished;
    return;
  }

  InvokeInternal(script_state);
  if (execution_state_ == ExecutionState::kFinished) {
    RemoveAbortAlgorithm();
  }
  callback_.Release();
}

void DOMTask::InvokeInternal(ScriptState* script_state) {
  v8::Isolate* isolate = script_state->GetIsolate();
  ScriptState::Scope scope(script_state);
  v8::TryCatch try_catch(isolate);

  ExecutionContext* context = ExecutionContext::From(script_state);
  DCHECK(context);
  DEVTOOLS_TIMELINE_TRACE_EVENT(
      "RunPostTaskCallback", inspector_scheduler_run_event::Data, context,
      task_id_for_tracing_, task_queue_->GetPriority(),
      delay_.InMillisecondsF());
  probe::AsyncTask async_task(context, &async_task_context_);

  std::optional<scheduler::TaskAttributionTracker::TaskScope>
      task_attribution_scope;
  // For the main thread (tracker exists), create the task scope with the signal
  // to set up propagation. On workers, set the current context here since there
  // is no tracker.
  auto* tracker =
      scheduler::TaskAttributionTracker::From(script_state->GetIsolate());
  if (tracker) {
    task_attribution_scope = tracker->CreateTaskScope(
        script_state, parent_task_,
        scheduler::TaskAttributionTracker::TaskScopeType::kSchedulerPostTask,
        scheduler_task_context_);
  } else {
    auto* task_state = MakeGarbageCollected<WebSchedulingTaskState>(
        /*TaskAttributionInfo=*/nullptr, scheduler_task_context_);
    ScriptWrappableTaskState::SetCurrent(script_state, task_state);
  }

  execution_state_ = ExecutionState::kRunningSync;
  ScriptValue result;
  ScriptPromise<IDLAny> pending_result;
  if (callback_->Invoke(nullptr).To(&result)) {
    v8::Local<v8::Value> v8_result = result.V8Value();
    if (v8_result->IsPromise()) {
      auto promise = v8_result.As<v8::Promise>();
      if (promise->State() == v8::Promise::PromiseState::kPending) {
        pending_result = ScriptPromise<IDLAny>::FromV8Promise(isolate, promise);
        auto* handler = MakeGarbageCollected<TaskPromiseResolveHandler>(this);
        pending_result.Then(script_state, handler, handler);
      }
    }
    resolver_->Resolve(result);
  } else if (try_catch.HasCaught()) {
    resolver_->Reject(try_catch.Exception());
  }
  execution_state_ = pending_result.IsEmpty() ? ExecutionState::kFinished
                                              : ExecutionState::kRunningAsync;
  // If this is a worker, clear the context to prevent it from leaking to the
  // next task (`task_attribution_scope` handles this on the main thread).
  if (!tracker) {
    ScriptWrappableTaskState::SetCurrent(script_state, nullptr);
  }
}

void DOMTask::OnAbort() {
  // If the task finished, `RemoveAbortAlgorithm()` should have been called.
  CHECK_NE(execution_state_, ExecutionState::kFinished);

  task_handle_.Cancel();
  async_task_context_.Cancel();

  DCHECK(resolver_);

  ScriptState* const resolver_script_state = resolver_->GetScriptState();

  if (!IsInParallelAlgorithmRunnable(resolver_->GetExecutionContext(),
                                     resolver_script_state)) {
    return;
  }

  auto* context = ExecutionContext::From(resolver_script_state);
  CHECK(context);

  if (execution_state_ == ExecutionState::kRunningAsync) {
    UseCounter::Count(*context, WebFeature::kSchedulerPostTaskAsyncAbort);
    // The abort won't have an effect because the promise has already been
    // resolved.
    return;
  }
  UseCounter::Count(*context,
                    execution_state_ == ExecutionState::kRunningSync
                        ? WebFeature::kSchedulerPostTaskSelfAbort
                        : WebFeature::kSchedulerPostTaskAbortBeforeRunning);

  // Switch to the resolver's context to let DOMException pick up the resolver's
  // JS stack.
  ScriptState::Scope script_state_scope(resolver_script_state);

  DEVTOOLS_TIMELINE_TRACE_EVENT("AbortPostTaskCallback",
                                inspector_scheduler_abort_event::Data, context,
                                task_id_for_tracing_);

  // TODO(crbug.com/1293949): Add an error message.
  CHECK(scheduler_task_context_->AbortSource());
  resolver_->Reject(scheduler_task_context_->AbortSource()
                        ->reason(resolver_script_state)
                        .V8ValueFor(resolver_script_state));
}

void DOMTask::RemoveAbortAlgorithm() {
  if (abort_handle_) {
    AbortSignal* abort_source = scheduler_task_context_->AbortSource();
    CHECK(abort_source);
    abort_source->RemoveAlgorithm(abort_handle_);
    abort_handle_ = nullptr;
  }
}

void DOMTask::OnPendingPromiseSettled() {
  execution_state_ = ExecutionState::kFinished;
  RemoveAbortAlgorithm();
}

}  // namespace blink