summaryrefslogtreecommitdiffstats
path: root/dom/webscheduling/TaskSignal.h
blob: 2cc02cc2fc25accd4b75cd4bad0f4f2d649e1a37 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:expandtab:shiftwidth=2:tabstop=2:
 */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_dom_TaskSignal_h
#define mozilla_dom_TaskSignal_h

#include "mozilla/DOMEventTargetHelper.h"
#include "mozilla/dom/AbortSignal.h"
#include "mozilla/dom/WebTaskSchedulingBinding.h"
#include "WebTaskScheduler.h"

namespace mozilla::dom {
class TaskSignal : public AbortSignal {
 public:
  TaskSignal(nsIGlobalObject* aGlobal, TaskPriority aPriority)
      : AbortSignal(aGlobal, false, JS::UndefinedHandleValue),
        mPriority(aPriority),
        mPriorityChanging(false) {}

  IMPL_EVENT_HANDLER(prioritychange);

  TaskPriority Priority() const { return mPriority; }

  JSObject* WrapObject(JSContext* aCx,
                       JS::Handle<JSObject*> aGivenProto) override {
    return TaskSignal_Binding::Wrap(aCx, this, aGivenProto);
  }

  void SetPriority(TaskPriority aPriority) { mPriority = aPriority; }

  bool IsTaskSignal() const override { return true; }

  bool PriorityChanging() const { return mPriorityChanging; }

  void SetPriorityChanging(bool aPriorityChanging) {
    mPriorityChanging = aPriorityChanging;
  }

  void SetWebTaskScheduler(WebTaskScheduler* aScheduler) {
    mSchedulers.AppendElement(aScheduler);
  }

  void RunPriorityChangeAlgorithms() {
    for (const WeakPtr<WebTaskScheduler>& scheduler : mSchedulers) {
      scheduler->RunTaskSignalPriorityChange(this);
    }
  }

 private:
  TaskPriority mPriority;

  bool mPriorityChanging;

  nsTArray<WeakPtr<WebTaskScheduler>> mSchedulers;

  ~TaskSignal() = default;
};
}  // namespace mozilla::dom
#endif