summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/loader/long_task_detector.h
blob: f98bc1d430e24d729990fd7fab188db4cfc25861 (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
// Copyright 2017 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_CORE_LOADER_LONG_TASK_DETECTOR_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_LONG_TASK_DETECTOR_H_

#include "base/macros.h"
#include "base/task/sequence_manager/task_time_observer.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/scheduler/public/thread.h"
#include "third_party/blink/renderer/platform/wtf/time.h"

namespace blink {

class CORE_EXPORT LongTaskObserver : public GarbageCollectedMixin {
 public:
  virtual ~LongTaskObserver() = default;

  virtual void OnLongTaskDetected(TimeTicks start_time, TimeTicks end_time) = 0;
};

// LongTaskDetector detects tasks longer than kLongTaskThreshold and notifies
// observers. When it has non-zero LongTaskObserver, it adds itself as a
// TaskTimeObserver on the main thread and observes every task. When the number
// of LongTaskObservers drop to zero it automatically removes itself as a
// TaskTimeObserver.
class CORE_EXPORT LongTaskDetector final
    : public GarbageCollectedFinalized<LongTaskDetector>,
      public base::sequence_manager::TaskTimeObserver {
 public:
  static LongTaskDetector& Instance();

  LongTaskDetector();

  void RegisterObserver(LongTaskObserver*);
  void UnregisterObserver(LongTaskObserver*);

  void Trace(blink::Visitor*);

  static constexpr base::TimeDelta kLongTaskThreshold =
      base::TimeDelta::FromMilliseconds(50);

 private:
  // scheduler::TaskTimeObserver implementation
  void WillProcessTask(base::TimeTicks start_time) override {}
  void DidProcessTask(base::TimeTicks start_time,
                      base::TimeTicks end_time) override;

  HeapHashSet<Member<LongTaskObserver>> observers_;

  DISALLOW_COPY_AND_ASSIGN(LongTaskDetector);
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_LONG_TASK_DETECTOR_H_