summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/layout/ng/custom/css_layout_definition.h
blob: 5596cc1d38fd42449a2f90fe9dddbade85d69acc (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// 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_CORE_LAYOUT_NG_CUSTOM_CSS_LAYOUT_DEFINITION_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_LAYOUT_NG_CUSTOM_CSS_LAYOUT_DEFINITION_H_

#include "third_party/blink/renderer/core/css/css_property_names.h"
#include "third_party/blink/renderer/core/css/cssom/css_style_value.h"
#include "third_party/blink/renderer/platform/bindings/name_client.h"
#include "third_party/blink/renderer/platform/bindings/scoped_persistent.h"
#include "third_party/blink/renderer/platform/bindings/trace_wrapper_v8_reference.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "v8/include/v8.h"

namespace blink {

class CustomLayoutScope;
class FragmentResultOptions;
class IntrinsicSizesResultOptions;
class LayoutUnit;
struct LogicalSize;
class NGBlockNode;
struct NGBoxStrut;
class NGConstraintSpace;
class ScriptState;
class SerializedScriptValue;
class V8IntrinsicSizesCallback;
class V8LayoutCallback;
class V8NoArgumentConstructor;

// Represents a javascript class registered on the LayoutWorkletGlobalScope by
// the author.
// https://drafts.css-houdini.org/css-layout-api/#layout-definition
class CSSLayoutDefinition final : public GarbageCollected<CSSLayoutDefinition>,
                                  public NameClient {
 public:
  CSSLayoutDefinition(
      ScriptState*,
      V8NoArgumentConstructor* constructor,
      V8IntrinsicSizesCallback* intrinsic_sizes,
      V8LayoutCallback* layout,
      const Vector<CSSPropertyID>& native_invalidation_properties,
      const Vector<AtomicString>& custom_invalidation_properties,
      const Vector<CSSPropertyID>& child_native_invalidation_properties,
      const Vector<AtomicString>& child_custom_invalidation_properties);
  virtual ~CSSLayoutDefinition();

  // This class represents an instance of the layout class defined by the
  // CSSLayoutDefinition.
  class Instance final : public GarbageCollected<Instance> {
   public:
    Instance(CSSLayoutDefinition*, v8::Local<v8::Value> instance);

    // Runs the web developer defined layout, returns true if everything
    // succeeded. It populates the FragmentResultOptions dictionary, and
    // fragment_result_data.
    bool Layout(
        const NGConstraintSpace&,
        const Document&,
        const NGBlockNode&,
        const LogicalSize& border_box_size,
        const NGBoxStrut& border_scrollbar_padding,
        CustomLayoutScope*,
        FragmentResultOptions*&,
        scoped_refptr<SerializedScriptValue>* fragment_result_data);

    // Runs the web developer defined intrinsicSizes, returns true if everything
    // succeeded. It populates the IntrinsicSizesResultOptions dictionary.
    bool IntrinsicSizes(
        const NGConstraintSpace&,
        const Document&,
        const NGBlockNode&,
        const LogicalSize& border_box_size,
        const NGBoxStrut& border_scrollbar_padding,
        const LayoutUnit child_percentage_resolution_block_size_for_min_max,
        CustomLayoutScope*,
        IntrinsicSizesResultOptions**,
        bool* child_depends_on_percentage_block_size);

    void Trace(Visitor*) const;

   private:
    void ReportException(ExceptionState*);

    Member<CSSLayoutDefinition> definition_;
    TraceWrapperV8Reference<v8::Value> instance_;
  };

  // Creates an instance of the web developer defined class. May return a
  // nullptr if constructing the instance failed.
  Instance* CreateInstance();

  const Vector<CSSPropertyID>& NativeInvalidationProperties() const {
    return native_invalidation_properties_;
  }
  const Vector<AtomicString>& CustomInvalidationProperties() const {
    return custom_invalidation_properties_;
  }
  const Vector<CSSPropertyID>& ChildNativeInvalidationProperties() const {
    return child_native_invalidation_properties_;
  }
  const Vector<AtomicString>& ChildCustomInvalidationProperties() const {
    return child_custom_invalidation_properties_;
  }

  ScriptState* GetScriptState() const { return script_state_; }

  virtual void Trace(Visitor* visitor) const;

  const char* NameInHeapSnapshot() const override {
    return "CSSLayoutDefinition";
  }

 private:
  Member<ScriptState> script_state_;

  // This object keeps the class instances, constructor function, intrinsic
  // sizes function, and layout function alive. It participates in wrapper
  // tracing as it holds onto V8 wrappers.
  Member<V8NoArgumentConstructor> constructor_;
  Member<V8IntrinsicSizesCallback> intrinsic_sizes_;
  Member<V8LayoutCallback> layout_;

  // If a constructor call ever fails, we'll refuse to create any more
  // instances of the web developer provided class.
  bool constructor_has_failed_ = false;

  Vector<CSSPropertyID> native_invalidation_properties_;
  Vector<AtomicString> custom_invalidation_properties_;
  Vector<CSSPropertyID> child_native_invalidation_properties_;
  Vector<AtomicString> child_custom_invalidation_properties_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_LAYOUT_NG_CUSTOM_CSS_LAYOUT_DEFINITION_H_