1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "third_party/blink/renderer/core/css/media_feature_overrides.h"
6 
7 #include "third_party/blink/renderer/core/css/parser/css_parser_context.h"
8 #include "third_party/blink/renderer/core/css/parser/css_parser_token_range.h"
9 #include "third_party/blink/renderer/core/css/parser/css_tokenizer.h"
10 
11 namespace blink {
12 
SetOverride(const AtomicString & feature,const String & value_string)13 void MediaFeatureOverrides::SetOverride(const AtomicString& feature,
14                                         const String& value_string) {
15   CSSTokenizer tokenizer(value_string);
16   const auto tokens = tokenizer.TokenizeToEOF();
17   CSSParserTokenRange range(tokens);
18 
19   // TODO(xiaochengh): This is a fake CSSParserContext that only passes
20   // down the CSSParserMode. Plumb the real CSSParserContext through, so that
21   // web features can be counted correctly.
22   const CSSParserContext* fake_context = MakeGarbageCollected<CSSParserContext>(
23       kHTMLStandardMode, SecureContextMode::kInsecureContext);
24 
25   // MediaFeatureOverrides are used to emulate various media feature values.
26   // These don't need to pass an ExecutionContext, since the parsing of
27   // the actual CSS will determine whether or not the emulated values will come
28   // into play (i.e. if you can parse an origin trial enabled feature, you
29   // will never ask for the emulated override value).
30   // Note that once a real CSSParserContext is plumbed through we can use its
31   // Document to get the ExecutionContext so the extra parameter should be
32   // removed.
33   auto value =
34       MediaQueryExp::Create(feature, range, *fake_context, nullptr).ExpValue();
35 
36   if (value.IsValid())
37     overrides_.Set(feature, value);
38   else
39     overrides_.erase(feature);
40 }
41 
42 }  // namespace blink
43