summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/layout/ng/custom/custom_layout_scope.h
blob: 3cc70061f06c119a7c0fc0368b3199aa737a841c (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
// 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_CORE_LAYOUT_NG_CUSTOM_CUSTOM_LAYOUT_SCOPE_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_LAYOUT_NG_CUSTOM_CUSTOM_LAYOUT_SCOPE_H_

#include "third_party/blink/renderer/core/layout/ng/custom/custom_layout_work_task.h"
#include "third_party/blink/renderer/platform/heap/handle.h"

namespace blink {

// The work queue is a list of work tasks which will either produce fragment(s)
// or intrinsic-size(s) for the custom-layout class.
typedef Vector<CustomLayoutWorkTask, 4> CustomLayoutWorkQueue;

// This heap allocated class is used to indicate which custom-layout (heap)
// objects are still valid.
//
// Any objects which have a pointer to this object with |is_detached_| being
// true, or not matching the current scope token should be considered invalid.
class CustomLayoutToken : public GarbageCollected<CustomLayoutToken> {
 public:
  CustomLayoutToken() : is_detached_(false) {}
  void Trace(Visitor* visitor) {}
  bool IsValid() const;

 private:
  friend class CustomLayoutScope;
  bool is_detached_;
};

// This scope object is used to track which custom-layout (heap) objects are
// valid.
//
// Also maintains the current work queue. Work should only be pushed onto this
// queue if this is the current scope.
class CustomLayoutScope {
  STACK_ALLOCATED();

 public:
  static CustomLayoutScope* Current() { return current_scope_; }

  CustomLayoutScope()
      : prev_scope_(current_scope_),
        token_(MakeGarbageCollected<CustomLayoutToken>()) {
    current_scope_ = this;
  }

  // Marks the scope-token as "detached" for any classes with a pointer to it.
  ~CustomLayoutScope() {
    token_->is_detached_ = true;
    current_scope_ = current_scope_->prev_scope_;
  }

  CustomLayoutWorkQueue* Queue() {
    DCHECK_EQ(this, current_scope_);
    return &queue_;
  }
  CustomLayoutToken* Token() { return token_; }

 private:
  static CustomLayoutScope* current_scope_;

  CustomLayoutScope* prev_scope_;
  CustomLayoutWorkQueue queue_;
  CustomLayoutToken* token_;
};

inline bool CustomLayoutToken::IsValid() const {
  return !is_detached_ && this == CustomLayoutScope::Current()->Token();
}

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_LAYOUT_NG_CUSTOM_CUSTOM_LAYOUT_SCOPE_H_