summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/scheduler/dom_task_queue.h
blob: 417fa14f7338dabc24955c21c73f32361c2d8827 (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
75
76
77
78
79
80
81
82
83
84
85
86
// 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_TASK_QUEUE_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_SCHEDULER_DOM_TASK_QUEUE_H_

#include <memory>

#include "base/memory/scoped_refptr.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/wtf/text/atomic_string.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"

namespace base {
class SingleThreadTaskRunner;
}  // namespace base

namespace blink {

class DOMScheduler;
class DOMTask;
class Document;
class ExecutionContext;
class ScriptValue;
class TaskQueuePostTaskOptions;
class V8Function;
class WebSchedulingTaskQueue;

namespace scheduler {
class WebSchedulingTaskQueue;
}  // namespace scheduler

class MODULES_EXPORT DOMTaskQueue : public ScriptWrappable,
                                    ContextLifecycleObserver {
  DEFINE_WRAPPERTYPEINFO();
  USING_GARBAGE_COLLECTED_MIXIN(DOMTaskQueue);

 public:
  DOMTaskQueue(Document*, WebSchedulingPriority, DOMScheduler*);

  // Returns the priority of the DOMTaskQueue.
  AtomicString priority() const;

  // postTask creates and queues a DOMTask in this DOMTaskQueue and returns the
  // DOMTask. If the underlying context is destroyed, e.g. for detached
  // documents, this returns nullptr.
  DOMTask* postTask(V8Function*,
                    TaskQueuePostTaskOptions*,
                    const HeapVector<ScriptValue>& args);

  // Move the task from its current DOMTaskQueue to this one. For pending
  // non-delayed tasks, the task is enqueued at the end of this DOMTaskQueue.
  // For delayed tasks, the delay is adjusted before reposting it.
  void take(DOMTask*);

  const scoped_refptr<base::SingleThreadTaskRunner>& GetTaskRunner() {
    return task_runner_;
  }

  DOMScheduler* GetScheduler() { return scheduler_.Get(); }

  void ContextDestroyed(ExecutionContext*) override;

  void Trace(Visitor*) override;

 private:
  void RunTaskCallback(DOMTask*);
  void ScheduleTask(DOMTask*, base::TimeDelta delay);

  const WebSchedulingPriority priority_;
  // This is destroyed when the Context is destroyed, and we rely on this
  // happening before the underlying FrameScheduler is destroyed.
  std::unique_ptr<WebSchedulingTaskQueue> web_scheduling_task_queue_;
  const scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  Member<DOMScheduler> scheduler_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_SCHEDULER_DOM_TASK_QUEUE_H_