summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/scheduler/dom_scheduler.h
blob: f83cd93f4e179f432f6eb303fc179cebd9182971 (plain)
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
// Copyright 2019 The Chromium Authors. All rights reserved.
// 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_SCHEDULER_DOM_SCHEDULER_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_SCHEDULER_DOM_SCHEDULER_H_

#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/execution_context/context_lifecycle_observer.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/scheduler/public/web_scheduling_priority.h"
#include "third_party/blink/renderer/platform/supplementable.h"
#include "third_party/blink/renderer/platform/wtf/text/atomic_string.h"

namespace blink {

class DOMTask;
class ExecutionContext;
class SchedulerPostTaskOptions;
class ScriptValue;
class V8Function;
class WebSchedulingTaskQueue;

class MODULES_EXPORT DOMScheduler : public ScriptWrappable,
                                    public ContextLifecycleObserver,
                                    public Supplement<Document> {
  DEFINE_WRAPPERTYPEINFO();
  USING_GARBAGE_COLLECTED_MIXIN(DOMScheduler);

 public:
  static const char kSupplementName[];

  static DOMScheduler* From(Document&);

  explicit DOMScheduler(Document*);

  // postTask creates and queues a DOMTask and returns a Promise that will
  // resolve when it completes. The task will be scheduled in the queue
  // corresponding to the priority in the SchedulerPostTaskOptions, or in a
  // queue associated with the given DOMTaskSignal if one is provided. If the
  // underlying context is destroyed, e.g. for detached documents, this will
  // return a rejected promise.
  ScriptPromise postTask(ScriptState*,
                         V8Function*,
                         SchedulerPostTaskOptions*,
                         const HeapVector<ScriptValue>& args);

  // Callbacks invoked by DOMTasks when they run.
  void OnTaskStarted(DOMTask*);
  void OnTaskCompleted(DOMTask*);

  void ContextDestroyed(ExecutionContext*) override;

  void Trace(Visitor*) override;

 private:
  static constexpr size_t kWebSchedulingPriorityCount =
      static_cast<size_t>(WebSchedulingPriority::kLastPriority) + 1;

  void CreateGlobalTaskQueues(Document*);
  base::SingleThreadTaskRunner* GetTaskRunnerFor(WebSchedulingPriority);

  // |global_task_queues_| is initialized with one entry per priority, indexed
  // by priority. This will be empty when the document is detached.
  Vector<std::unique_ptr<WebSchedulingTaskQueue>, kWebSchedulingPriorityCount>
      global_task_queues_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_SCHEDULER_DOM_SCHEDULER_H_