-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
media_feature_overrides.cc
122 lines (106 loc) · 4.5 KB
/
media_feature_overrides.cc
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
// Copyright 2019 The Chromium Authors
// 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/media_feature_overrides.h"
#include "third_party/blink/renderer/core/css/media_features.h"
#include "third_party/blink/renderer/core/css/media_query_exp.h"
#include "third_party/blink/renderer/core/css/media_values.h"
#include "third_party/blink/renderer/core/css/parser/css_parser_context.h"
#include "third_party/blink/renderer/core/css/parser/css_parser_token_range.h"
#include "third_party/blink/renderer/core/css/parser/css_tokenizer.h"
#include "third_party/blink/renderer/core/execution_context/security_context.h"
#include "third_party/blink/renderer/platform/graphics/color_space_gamut.h"
namespace blink {
namespace {
absl::optional<ColorSpaceGamut> ConvertColorGamut(
const MediaQueryExpValue& value) {
if (!value.IsValid()) {
return absl::nullopt;
}
if (value.Id() == CSSValueID::kSRGB) {
return ColorSpaceGamut::SRGB;
}
if (value.Id() == CSSValueID::kP3) {
return ColorSpaceGamut::P3;
}
// Rec. 2020 is also known as ITU-R-Empfehlung BT.2020.
if (value.Id() == CSSValueID::kRec2020) {
return ColorSpaceGamut::BT2020;
}
return absl::nullopt;
}
absl::optional<mojom::blink::PreferredColorScheme> ConvertPreferredColorScheme(
const MediaQueryExpValue& value) {
if (!value.IsValid()) {
return absl::nullopt;
}
return CSSValueIDToPreferredColorScheme(value.Id());
}
absl::optional<mojom::blink::PreferredContrast> ConvertPreferredContrast(
const MediaQueryExpValue& value) {
if (!value.IsValid()) {
return absl::nullopt;
}
return CSSValueIDToPreferredContrast(value.Id());
}
absl::optional<bool> ConvertPrefersReducedMotion(
const MediaQueryExpValue& value) {
if (!value.IsValid()) {
return absl::nullopt;
}
return value.Id() == CSSValueID::kReduce;
}
absl::optional<bool> ConvertPrefersReducedData(
const MediaQueryExpValue& value) {
if (!value.IsValid()) {
return absl::nullopt;
}
return value.Id() == CSSValueID::kReduce;
}
absl::optional<ForcedColors> ConvertForcedColors(
const MediaQueryExpValue& value) {
if (!value.IsValid()) {
return absl::nullopt;
}
return CSSValueIDToForcedColors(value.Id());
}
} // namespace
void MediaFeatureOverrides::SetOverride(const AtomicString& feature,
const String& value_string) {
CSSTokenizer tokenizer(value_string);
auto [tokens, raw_offsets] = tokenizer.TokenizeToEOFWithOffsets();
CSSParserTokenRange range(tokens);
CSSParserTokenOffsets offsets(tokens, std::move(raw_offsets), value_string);
// TODO(xiaochengh): This is a fake CSSParserContext that only passes
// down the CSSParserMode. Plumb the real CSSParserContext through, so that
// web features can be counted correctly.
const CSSParserContext* fake_context = MakeGarbageCollected<CSSParserContext>(
kHTMLStandardMode, SecureContextMode::kInsecureContext);
// MediaFeatureOverrides are used to emulate various media feature values.
// These don't need to pass an ExecutionContext, since the parsing of
// the actual CSS will determine whether or not the emulated values will come
// into play (i.e. if you can parse an origin trial enabled feature, you
// will never ask for the emulated override value).
// Note that once a real CSSParserContext is plumbed through we can use its
// Document to get the ExecutionContext so the extra parameter should be
// removed.
MediaQueryExpBounds bounds =
MediaQueryExp::Create(feature, range, offsets, *fake_context).Bounds();
DCHECK(!bounds.left.IsValid());
MediaQueryExpValue value = bounds.right.value;
if (feature == media_feature_names::kColorGamutMediaFeature) {
color_gamut_ = ConvertColorGamut(value);
} else if (feature == media_feature_names::kPrefersColorSchemeMediaFeature) {
preferred_color_scheme_ = ConvertPreferredColorScheme(value);
} else if (feature == media_feature_names::kPrefersContrastMediaFeature) {
preferred_contrast_ = ConvertPreferredContrast(value);
} else if (feature ==
media_feature_names::kPrefersReducedMotionMediaFeature) {
prefers_reduced_motion_ = ConvertPrefersReducedMotion(value);
} else if (feature == media_feature_names::kPrefersReducedDataMediaFeature) {
prefers_reduced_data_ = ConvertPrefersReducedData(value);
} else if (feature == media_feature_names::kForcedColorsMediaFeature) {
forced_colors_ = ConvertForcedColors(value);
}
}
} // namespace blink