summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/css/style_environment_variables.cc
blob: 0be4a09d55b88fe94c897ece169a86a9981263bf (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// 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.

#include "third_party/blink/renderer/core/css/style_environment_variables.h"

#include "third_party/blink/renderer/core/css/parser/css_tokenizer.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"

namespace blink {

namespace {

// This is the default value for all safe-area-inset-* variables.
static const char kSafeAreaInsetDefault[] = "0px";
// This is the default value for all keyboard-inset-* variables.
static const char kKeyboardInsetDefault[] = "0px";

// Use this to set default values for environment variables when the root
// instance is created.
void SetDefaultEnvironmentVariables(StyleEnvironmentVariables* instance) {
  instance->SetVariable(UADefinedVariable::kSafeAreaInsetTop,
                        kSafeAreaInsetDefault);
  instance->SetVariable(UADefinedVariable::kSafeAreaInsetLeft,
                        kSafeAreaInsetDefault);
  instance->SetVariable(UADefinedVariable::kSafeAreaInsetBottom,
                        kSafeAreaInsetDefault);
  instance->SetVariable(UADefinedVariable::kSafeAreaInsetRight,
                        kSafeAreaInsetDefault);
  if (RuntimeEnabledFeatures::VirtualKeyboardEnabled()) {
    instance->SetVariable(UADefinedVariable::kKeyboardInsetTop,
                          kKeyboardInsetDefault);
    instance->SetVariable(UADefinedVariable::kKeyboardInsetLeft,
                          kKeyboardInsetDefault);
    instance->SetVariable(UADefinedVariable::kKeyboardInsetBottom,
                          kKeyboardInsetDefault);
    instance->SetVariable(UADefinedVariable::kKeyboardInsetRight,
                          kKeyboardInsetDefault);
    instance->SetVariable(UADefinedVariable::kKeyboardInsetWidth,
                          kKeyboardInsetDefault);
    instance->SetVariable(UADefinedVariable::kKeyboardInsetHeight,
                          kKeyboardInsetDefault);
  }
}

}  // namespace.

// This owns the static root instance.
class StyleEnvironmentVariables::RootOwner {
 public:
  StyleEnvironmentVariables& GetRoot() {
    if (!instance_) {
      instance_ = base::AdoptRef(new StyleEnvironmentVariables());
      SetDefaultEnvironmentVariables(instance_.get());
    }

    return *instance_.get();
  }

 private:
  scoped_refptr<StyleEnvironmentVariables> instance_;
};

// static
StyleEnvironmentVariables& StyleEnvironmentVariables::GetRootInstance() {
  static auto* instance = new StyleEnvironmentVariables::RootOwner();
  return instance->GetRoot();
}

// static
const AtomicString StyleEnvironmentVariables::GetVariableName(
    UADefinedVariable variable) {
  switch (variable) {
    case UADefinedVariable::kSafeAreaInsetTop:
      return "safe-area-inset-top";
    case UADefinedVariable::kSafeAreaInsetLeft:
      return "safe-area-inset-left";
    case UADefinedVariable::kSafeAreaInsetBottom:
      return "safe-area-inset-bottom";
    case UADefinedVariable::kSafeAreaInsetRight:
      return "safe-area-inset-right";
    case UADefinedVariable::kKeyboardInsetTop:
      DCHECK(RuntimeEnabledFeatures::VirtualKeyboardEnabled());
      return "keyboard-inset-top";
    case UADefinedVariable::kKeyboardInsetLeft:
      DCHECK(RuntimeEnabledFeatures::VirtualKeyboardEnabled());
      return "keyboard-inset-left";
    case UADefinedVariable::kKeyboardInsetBottom:
      DCHECK(RuntimeEnabledFeatures::VirtualKeyboardEnabled());
      return "keyboard-inset-bottom";
    case UADefinedVariable::kKeyboardInsetRight:
      DCHECK(RuntimeEnabledFeatures::VirtualKeyboardEnabled());
      return "keyboard-inset-right";
    case UADefinedVariable::kKeyboardInsetWidth:
      DCHECK(RuntimeEnabledFeatures::VirtualKeyboardEnabled());
      return "keyboard-inset-width";
    case UADefinedVariable::kKeyboardInsetHeight:
      DCHECK(RuntimeEnabledFeatures::VirtualKeyboardEnabled());
      return "keyboard-inset-height";
    case UADefinedVariable::kFoldTop:
      DCHECK(RuntimeEnabledFeatures::CSSFoldablesEnabled());
      return "fold-top";
    case UADefinedVariable::kFoldRight:
      DCHECK(RuntimeEnabledFeatures::CSSFoldablesEnabled());
      return "fold-right";
    case UADefinedVariable::kFoldBottom:
      DCHECK(RuntimeEnabledFeatures::CSSFoldablesEnabled());
      return "fold-bottom";
    case UADefinedVariable::kFoldLeft:
      DCHECK(RuntimeEnabledFeatures::CSSFoldablesEnabled());
      return "fold-left";
    case UADefinedVariable::kFoldWidth:
      DCHECK(RuntimeEnabledFeatures::CSSFoldablesEnabled());
      return "fold-width";
    case UADefinedVariable::kFoldHeight:
      DCHECK(RuntimeEnabledFeatures::CSSFoldablesEnabled());
      return "fold-height";
    default:
      break;
  }

  NOTREACHED();
}

// static
scoped_refptr<StyleEnvironmentVariables> StyleEnvironmentVariables::Create(
    StyleEnvironmentVariables& parent) {
  scoped_refptr<StyleEnvironmentVariables> obj =
      base::AdoptRef(new StyleEnvironmentVariables());

  // Add a reference to this instance from the parent.
  obj->BindToParent(parent);

  return obj;
}

StyleEnvironmentVariables::~StyleEnvironmentVariables() {
  // Remove a reference to this instance from the parent.
  if (parent_) {
    auto it = parent_->children_.Find(this);
    DCHECK(it != kNotFound);
    parent_->children_.EraseAt(it);
  }
}

void StyleEnvironmentVariables::SetVariable(
    const AtomicString& name,
    scoped_refptr<CSSVariableData> value) {
  data_.Set(name, std::move(value));
  InvalidateVariable(name);
}

void StyleEnvironmentVariables::SetVariable(const AtomicString& name,
                                            const String& value) {
  CSSTokenizer tokenizer(value);
  Vector<CSSParserToken> tokens;
  tokens.AppendVector(tokenizer.TokenizeToEOF());

  Vector<String> backing_strings;
  backing_strings.push_back(value);

  SetVariable(
      name,
      CSSVariableData::CreateResolved(
          std::move(tokens), std::move(backing_strings),
          false /* is_animation_tainted */, false /* has_font_units */,
          false /* has_root_font_units*/, g_null_atom, WTF::TextEncoding()));
}

void StyleEnvironmentVariables::SetVariable(const UADefinedVariable name,
                                            const String& value) {
  SetVariable(GetVariableName(name), value);
}

void StyleEnvironmentVariables::RemoveVariable(const AtomicString& name) {
  data_.erase(name);
  InvalidateVariable(name);
}

CSSVariableData* StyleEnvironmentVariables::ResolveVariable(
    const AtomicString& name) {
  auto result = data_.find(name);
  if (result == data_.end() && parent_)
    return parent_->ResolveVariable(name);
  if (result == data_.end())
    return nullptr;
  return result->value.get();
}

void StyleEnvironmentVariables::DetachFromParent() {
  DCHECK(parent_);

  // Remove any reference the |parent| has to |this|.
  auto it = parent_->children_.Find(this);
  if (it != kNotFound)
    parent_->children_.EraseAt(it);

  parent_ = nullptr;
}

String StyleEnvironmentVariables::FormatPx(int value) {
  return String::Format("%dpx", value);
}

void StyleEnvironmentVariables::ClearForTesting() {
  data_.clear();

  // If we are the root then we should re-apply the default variables.
  if (!parent_)
    SetDefaultEnvironmentVariables(this);
}

void StyleEnvironmentVariables::BindToParent(
    StyleEnvironmentVariables& parent) {
  DCHECK_EQ(nullptr, parent_);
  parent_ = &parent;
  parent.children_.push_back(this);
}

void StyleEnvironmentVariables::ParentInvalidatedVariable(
    const AtomicString& name) {
  // If we have not overridden the variable then we should invalidate it
  // locally.
  if (data_.find(name) == data_.end())
    InvalidateVariable(name);
}

void StyleEnvironmentVariables::InvalidateVariable(const AtomicString& name) {
  for (auto& it : children_)
    it->ParentInvalidatedVariable(name);
}

}  // namespace blink