File: task_attribution_tracker.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 (194 lines) | stat: -rw-r--r-- 6,871 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
// 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_PLATFORM_SCHEDULER_PUBLIC_TASK_ATTRIBUTION_TRACKER_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_SCHEDULER_PUBLIC_TASK_ATTRIBUTION_TRACKER_H_

#include <optional>
#include <utility>

#include "base/functional/function_ref.h"
#include "base/memory/stack_allocated.h"
#include "third_party/blink/public/common/scheduler/task_attribution_id.h"
#include "third_party/blink/renderer/platform/bindings/v8_per_isolate_data.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/wtf/hash_set.h"

namespace blink {
class SchedulerTaskContext;
class ScriptState;
class ScriptWrappableTaskStateBase;
class SoftNavigationContext;
}  // namespace blink

namespace v8 {
class Isolate;
}  // namespace v8

namespace blink::scheduler {

class TaskAttributionInfo;
class TaskAttributionTrackerImpl;

// This public interface enables platform/ and core/ callers to create a task
// scope on the one hand, and check on the ID of the currently running task as
// well as its ancestry on the other.
class PLATFORM_EXPORT TaskAttributionTracker {
 public:
  enum class TaskScopeType {
    kCallback,
    kScheduledAction,
    kScriptExecution,
    kPostMessage,
    kPopState,
    kSchedulerPostTask,
    kRequestIdleCallback,
    kXMLHttpRequest,
    kSoftNavigation,
  };

  // `TaskScope` stores state for the current task, which is propagated to tasks
  // and promise reactions created with in the scope. `TaskScope`s are meant
  // to be only used for JavaScript execution, and "task" here approximately
  // means "the current JavaScript execution, excluding microtasks", which
  // roughly aligns with a top-level JS callback.
  class TaskScope final {
    STACK_ALLOCATED();

   public:
    ~TaskScope() {
      if (task_tracker_) {
        task_tracker_->OnTaskScopeDestroyed(*this);
      }
    }

    TaskScope(TaskScope&& other)
        : task_tracker_(std::exchange(other.task_tracker_, nullptr)),
          script_state_(other.script_state_),
          previous_task_state_(other.previous_task_state_) {}

    TaskScope& operator=(TaskScope&& other) {
      task_tracker_ = std::exchange(other.task_tracker_, nullptr);
      script_state_ = other.script_state_;
      previous_task_state_ = other.previous_task_state_;
      return *this;
    }

   private:
    // Only `TaskAttributionTrackerImpl` can create `TaskScope`s.
    friend class TaskAttributionTrackerImpl;

    TaskScope(TaskAttributionTracker* tracker,
              ScriptState* script_state,
              ScriptWrappableTaskStateBase* previous_task_state)
        : task_tracker_(tracker),
          script_state_(script_state),
          previous_task_state_(previous_task_state) {}

    // `task_tracker_` is tied to the lifetime of the isolate, which will
    // outlive the current task.
    TaskAttributionTracker* task_tracker_;

    // The rest are on the Oilpan heap, so these are stored as raw pointers
    // since the class is stack allocated.
    ScriptState* script_state_;
    ScriptWrappableTaskStateBase* previous_task_state_;
  };

  class Observer : public GarbageCollectedMixin {
   public:
    virtual void OnCreateTaskScope(TaskAttributionInfo&) = 0;
  };

  class PLATFORM_EXPORT ObserverScope {
    STACK_ALLOCATED();

   public:
    ObserverScope(ObserverScope&& other)
        : task_tracker_(std::exchange(other.task_tracker_, nullptr)),
          previous_observer_(std::exchange(other.previous_observer_, nullptr)) {
    }

    ObserverScope& operator=(ObserverScope&& other) {
      task_tracker_ = std::exchange(other.task_tracker_, nullptr);
      previous_observer_ = std::exchange(other.previous_observer_, nullptr);
      return *this;
    }

    ~ObserverScope() {
      if (task_tracker_) {
        task_tracker_->OnObserverScopeDestroyed(*this);
      }
    }

   private:
    friend class TaskAttributionTrackerImpl;

    ObserverScope(TaskAttributionTracker* tracker,
                  Observer* observer,
                  Observer* previous_observer)
        : task_tracker_(tracker), previous_observer_(previous_observer) {}

    Observer* PreviousObserver() const { return previous_observer_; }

    TaskAttributionTracker* task_tracker_;
    Observer* previous_observer_;
  };

  static TaskAttributionTracker* From(v8::Isolate* isolate) {
    return V8PerIsolateData::From(isolate)->GetTaskAttributionTracker();
  }

  virtual ~TaskAttributionTracker() = default;

  // Creates a new `TaskScope` to propagate `task_state` to descendant tasks and
  // continuations.
  virtual TaskScope CreateTaskScope(ScriptState*,
                                    TaskAttributionInfo* task_state,
                                    TaskScopeType type) = 0;

  // Create a new `TaskScope` to propagate the given `SoftNavigationContext`,
  // initiating propagation for the context.
  virtual TaskScope CreateTaskScope(ScriptState*, SoftNavigationContext*) = 0;

  // Creates a new `TaskScope` with web scheduling context. `task_state` will be
  // propagated to descendant tasks and continuations; `continuation_context`
  // will only be propagated to continuations.
  virtual TaskScope CreateTaskScope(
      ScriptState*,
      TaskAttributionInfo* task_state,
      TaskScopeType type,
      SchedulerTaskContext* continuation_context) = 0;

  // Conditionally create a `TaskScope` for a generic v8 callback. A `TaskScope`
  // is always created if `task_state` is non-null, and one is additionally
  // created if there isn't an active `TaskScope`.
  virtual std::optional<TaskScope> MaybeCreateTaskScopeForCallback(
      ScriptState*,
      TaskAttributionInfo* task_state) = 0;

  // Get the `TaskAttributionInfo` for the currently running task.
  virtual TaskAttributionInfo* RunningTask() const = 0;

  // Registers an observer to be notified when a `TaskScope` has been created.
  // Multiple `Observer`s can be registered, but only the innermost one will
  // receive callbacks.
  virtual ObserverScope RegisterObserver(Observer* observer) = 0;

  // Setter and getter for a pointer to a pending same-document navigation task,
  // to ensure the task's lifetime.
  virtual void AddSameDocumentNavigationTask(TaskAttributionInfo* task) = 0;
  virtual void ResetSameDocumentNavigationTasks() = 0;
  virtual TaskAttributionInfo* CommitSameDocumentNavigation(
      TaskAttributionId) = 0;

 protected:
  virtual void OnTaskScopeDestroyed(const TaskScope&) = 0;
  virtual void OnObserverScopeDestroyed(const ObserverScope&) = 0;
};

}  // namespace blink::scheduler

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_SCHEDULER_PUBLIC_TASK_ATTRIBUTION_TRACKER_H_