summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/css/style_environment_variables.h
blob: 4a3e2b92b3bac527076a06c566017f7b0590d324 (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
// 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_CSS_STYLE_ENVIRONMENT_VARIABLES_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_CSS_STYLE_ENVIRONMENT_VARIABLES_H_

#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/css/css_variable_data.h"
#include "third_party/blink/renderer/platform/wtf/forward.h"
#include "third_party/blink/renderer/platform/wtf/hash_map.h"
#include "third_party/blink/renderer/platform/wtf/ref_counted.h"
#include "third_party/blink/renderer/platform/wtf/text/atomic_string_hash.h"

namespace blink {

// UADefinedVariable contains all the user agent defined environment variables.
// When adding a new variable the string equivalent needs to be added to
// |GetVariableName|.
enum class UADefinedVariable {
  // The safe area insets are four environment variables that define a
  // rectangle by its top, right, bottom, and left insets from the edge of
  // the viewport.
  kSafeAreaInsetTop,
  kSafeAreaInsetLeft,
  kSafeAreaInsetBottom,
  kSafeAreaInsetRight,

  // The keyboard area insets are four environment variables that define a
  // virtual keyboard rectangle by its top, right, bottom, and left insets
  // from the edge of the viewport.
  // Explainers:
  // https://github.com/MicrosoftEdge/MSEdgeExplainers/blob/main/VirtualKeyboardAPI/explainer.md
  kKeyboardInsetTop,
  kKeyboardInsetLeft,
  kKeyboardInsetBottom,
  kKeyboardInsetRight,
};

// StyleEnvironmentVariables stores user agent and user defined CSS environment
// variables. It has a static root instance that stores global values and
// each document has a child that stores document level values.
class CORE_EXPORT StyleEnvironmentVariables
    : public RefCounted<StyleEnvironmentVariables> {
 public:
  static StyleEnvironmentVariables& GetRootInstance();

  // Gets the name of a |UADefinedVariable| as a string.
  static const AtomicString GetVariableName(UADefinedVariable);

  // Create a new instance bound to |parent|.
  static scoped_refptr<StyleEnvironmentVariables> Create(
      StyleEnvironmentVariables& parent);

  virtual ~StyleEnvironmentVariables();

  // Set the value of the variable |name| and invalidate any dependents.
  void SetVariable(const AtomicString& name,
                   scoped_refptr<CSSVariableData> value);

  // Tokenize |value| and set it.
  void SetVariable(const AtomicString& name, const String& value);
  void SetVariable(const UADefinedVariable name, const String& value);

  // Remove the variable |name| and invalidate any dependents.
  void RemoveVariable(const AtomicString& name);

  // Resolve the variable |name| by traversing the tree of
  // |StyleEnvironmentVariables|.
  virtual CSSVariableData* ResolveVariable(const AtomicString& name);

  // Detach |this| from |parent|.
  void DetachFromParent();

 protected:
  friend class StyleEnvironmentVariablesTest;

  void ClearForTesting();

  // Bind this instance to a |parent|. This should only be called once.
  void BindToParent(StyleEnvironmentVariables& parent);

  // Called by the parent to tell the child that variable |name| has changed.
  void ParentInvalidatedVariable(const AtomicString& name);

  StyleEnvironmentVariables() : parent_(nullptr) {}

  // Called when variable |name| is changed. This will notify any children that
  // this variable has changed.
  virtual void InvalidateVariable(const AtomicString& name);

 private:
  class RootOwner;

  Vector<scoped_refptr<StyleEnvironmentVariables>> children_;
  HashMap<AtomicString, scoped_refptr<CSSVariableData>> data_;
  scoped_refptr<StyleEnvironmentVariables> parent_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_CSS_STYLE_ENVIRONMENT_VARIABLES_H_