summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/testing/death_aware_script_wrappable.h
blob: 2666aa5bf80bdc46e4fbd7812532d219378d695e (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
87
88
89
90
91
92
93
// Copyright 2016 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_TESTING_DEATH_AWARE_SCRIPT_WRAPPABLE_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_TESTING_DEATH_AWARE_SCRIPT_WRAPPABLE_H_

#include <signal.h>
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/heap/heap.h"

namespace blink {

class DeathAwareScriptWrappable;

namespace internal {

class InObjectContainer {
  DISALLOW_NEW();

 public:
  explicit InObjectContainer(DeathAwareScriptWrappable* dependency)
      : dependency_(dependency) {}

  virtual ~InObjectContainer() {}

  virtual void Trace(Visitor* visitor) { visitor->Trace(dependency_); }

 private:
  Member<DeathAwareScriptWrappable> dependency_;
};

}  // namespace internal

// ScriptWrappable that can be used to
// (a) build a graph of ScriptWrappables, and
// (b) observe a single DeathAwareScriptWrappable for being garbage collected.
class DeathAwareScriptWrappable : public ScriptWrappable {
  DEFINE_WRAPPERTYPEINFO();
  static DeathAwareScriptWrappable* instance_;
  static bool has_died_;

 public:
  typedef Member<DeathAwareScriptWrappable> Wrapper;

  static bool HasDied() { return has_died_; }
  static void ObserveDeathsOf(DeathAwareScriptWrappable* instance) {
    has_died_ = false;
    instance_ = instance;
  }

  DeathAwareScriptWrappable() = default;
  ~DeathAwareScriptWrappable() override {
    if (this == instance_) {
      has_died_ = true;
    }
  }

  void Trace(Visitor* visitor) override {
    visitor->Trace(wrapped_dependency_);
    visitor->Trace(wrapped_vector_dependency_);
    visitor->Trace(wrapped_hash_map_dependency_);
    visitor->Trace(in_object_dependency_);
    ScriptWrappable::Trace(visitor);
  }

  void SetWrappedDependency(DeathAwareScriptWrappable* dependency) {
    wrapped_dependency_ = dependency;
  }

  void AddWrappedVectorDependency(DeathAwareScriptWrappable* dependency) {
    wrapped_vector_dependency_.push_back(dependency);
  }

  void AddWrappedHashMapDependency(DeathAwareScriptWrappable* key,
                                   DeathAwareScriptWrappable* value) {
    wrapped_hash_map_dependency_.insert(key, value);
  }

  void AddInObjectDependency(DeathAwareScriptWrappable* dependency) {
    in_object_dependency_.push_back(internal::InObjectContainer(dependency));
  }

 private:
  Wrapper wrapped_dependency_;
  HeapVector<Wrapper> wrapped_vector_dependency_;
  HeapHashMap<Wrapper, Wrapper> wrapped_hash_map_dependency_;
  HeapVector<internal::InObjectContainer> in_object_dependency_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_TESTING_DEATH_AWARE_SCRIPT_WRAPPABLE_H_