summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/controller/bloated_renderer_detector.h
blob: c2e4faede408dbb5f5a9c69e490817a0a4847b47 (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
// Copyright 2018 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_CONTROLLER_BLOATED_RENDERER_DETECTOR_H_
#define THIRD_PARTY_BLINK_RENDERER_CONTROLLER_BLOATED_RENDERER_DETECTOR_H_

#include "base/gtest_prod_util.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_initializer.h"
#include "third_party/blink/renderer/controller/controller_export.h"
#include "third_party/blink/renderer/platform/wtf/allocator.h"
#include "third_party/blink/renderer/platform/wtf/time.h"

namespace blink {

// Singleton class for detecting and handling bloated renderer conditions.
// Different parts of the renderer call the corresponding methods of this
// class to notify about potential bloat conditions. It currently works only
// for V8. In future it will be extended to the whole renderer.
class CONTROLLER_EXPORT BloatedRendererDetector {
  USING_FAST_MALLOC(BloatedRendererDetector);

 public:
  // Sets up the global singleton instance.
  static void Initialize();

  // Called when the main V8 isolate is close to reach its heap limit.
  static NearV8HeapLimitHandling OnNearV8HeapLimitOnMainThread();

 private:
  friend class BloatedRendererDetectorTest;
  FRIEND_TEST_ALL_PREFIXES(BloatedRendererDetectorTest, CooldownTime);
  FRIEND_TEST_ALL_PREFIXES(BloatedRendererDetectorTest, ForwardToBrowser);
  FRIEND_TEST_ALL_PREFIXES(BloatedRendererDetectorTest, MultipleDetections);

  // The time which should pass between 2 successive detections of bloated
  // renderer.
  static const int kMinimumCooldownInMinutes = 10;

  explicit BloatedRendererDetector(WTF::TimeTicks time)
      : last_detection_time_(time) {}

  NearV8HeapLimitHandling OnNearV8HeapLimitOnMainThreadImpl();

  WTF::TimeTicks last_detection_time_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CONTROLLER_BLOATED_RENDERER_DETECTOR_H_