1 // Copyright 2014 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_values_dynamic.h"
6 
7 #include "third_party/blink/public/common/css/forced_colors.h"
8 #include "third_party/blink/public/common/css/navigation_controls.h"
9 #include "third_party/blink/renderer/core/css/css_primitive_value.h"
10 #include "third_party/blink/renderer/core/css/css_resolution_units.h"
11 #include "third_party/blink/renderer/core/css/css_to_length_conversion_data.h"
12 #include "third_party/blink/renderer/core/css/media_values_cached.h"
13 #include "third_party/blink/renderer/core/dom/document.h"
14 #include "third_party/blink/renderer/core/frame/local_frame.h"
15 
16 namespace blink {
17 
Create(Document & document)18 MediaValues* MediaValuesDynamic::Create(Document& document) {
19   return MediaValuesDynamic::Create(document.GetFrameOfTreeRootDocument());
20 }
21 
Create(LocalFrame * frame)22 MediaValues* MediaValuesDynamic::Create(LocalFrame* frame) {
23   if (!frame || !frame->View() || !frame->GetDocument() ||
24       !frame->GetDocument()->GetLayoutView())
25     return MakeGarbageCollected<MediaValuesCached>();
26   return MakeGarbageCollected<MediaValuesDynamic>(frame);
27 }
28 
MediaValuesDynamic(LocalFrame * frame)29 MediaValuesDynamic::MediaValuesDynamic(LocalFrame* frame)
30     : frame_(frame),
31       viewport_dimensions_overridden_(false),
32       viewport_width_override_(0),
33       viewport_height_override_(0) {
34   DCHECK(frame_);
35 }
36 
MediaValuesDynamic(LocalFrame * frame,bool overridden_viewport_dimensions,double viewport_width,double viewport_height)37 MediaValuesDynamic::MediaValuesDynamic(LocalFrame* frame,
38                                        bool overridden_viewport_dimensions,
39                                        double viewport_width,
40                                        double viewport_height)
41     : frame_(frame),
42       viewport_dimensions_overridden_(overridden_viewport_dimensions),
43       viewport_width_override_(viewport_width),
44       viewport_height_override_(viewport_height) {
45   DCHECK(frame_);
46 }
47 
Copy() const48 MediaValues* MediaValuesDynamic::Copy() const {
49   return MakeGarbageCollected<MediaValuesDynamic>(
50       frame_, viewport_dimensions_overridden_, viewport_width_override_,
51       viewport_height_override_);
52 }
53 
ComputeLength(double value,CSSPrimitiveValue::UnitType type,int & result) const54 bool MediaValuesDynamic::ComputeLength(double value,
55                                        CSSPrimitiveValue::UnitType type,
56                                        int& result) const {
57   return MediaValues::ComputeLength(value, type,
58                                     CalculateDefaultFontSize(frame_),
59                                     ViewportWidth(), ViewportHeight(), result);
60 }
61 
ComputeLength(double value,CSSPrimitiveValue::UnitType type,double & result) const62 bool MediaValuesDynamic::ComputeLength(double value,
63                                        CSSPrimitiveValue::UnitType type,
64                                        double& result) const {
65   return MediaValues::ComputeLength(value, type,
66                                     CalculateDefaultFontSize(frame_),
67                                     ViewportWidth(), ViewportHeight(), result);
68 }
69 
ViewportWidth() const70 double MediaValuesDynamic::ViewportWidth() const {
71   if (viewport_dimensions_overridden_)
72     return viewport_width_override_;
73   return CalculateViewportWidth(frame_);
74 }
75 
ViewportHeight() const76 double MediaValuesDynamic::ViewportHeight() const {
77   if (viewport_dimensions_overridden_)
78     return viewport_height_override_;
79   return CalculateViewportHeight(frame_);
80 }
81 
DeviceWidth() const82 int MediaValuesDynamic::DeviceWidth() const {
83   return CalculateDeviceWidth(frame_);
84 }
85 
DeviceHeight() const86 int MediaValuesDynamic::DeviceHeight() const {
87   return CalculateDeviceHeight(frame_);
88 }
89 
DevicePixelRatio() const90 float MediaValuesDynamic::DevicePixelRatio() const {
91   return CalculateDevicePixelRatio(frame_);
92 }
93 
ColorBitsPerComponent() const94 int MediaValuesDynamic::ColorBitsPerComponent() const {
95   return CalculateColorBitsPerComponent(frame_);
96 }
97 
MonochromeBitsPerComponent() const98 int MediaValuesDynamic::MonochromeBitsPerComponent() const {
99   return CalculateMonochromeBitsPerComponent(frame_);
100 }
101 
PrimaryPointerType() const102 ui::PointerType MediaValuesDynamic::PrimaryPointerType() const {
103   return CalculatePrimaryPointerType(frame_);
104 }
105 
AvailablePointerTypes() const106 int MediaValuesDynamic::AvailablePointerTypes() const {
107   return CalculateAvailablePointerTypes(frame_);
108 }
109 
PrimaryHoverType() const110 ui::HoverType MediaValuesDynamic::PrimaryHoverType() const {
111   return CalculatePrimaryHoverType(frame_);
112 }
113 
AvailableHoverTypes() const114 int MediaValuesDynamic::AvailableHoverTypes() const {
115   return CalculateAvailableHoverTypes(frame_);
116 }
117 
ThreeDEnabled() const118 bool MediaValuesDynamic::ThreeDEnabled() const {
119   return CalculateThreeDEnabled(frame_);
120 }
121 
InImmersiveMode() const122 bool MediaValuesDynamic::InImmersiveMode() const {
123   return CalculateInImmersiveMode(frame_);
124 }
125 
MediaType() const126 const String MediaValuesDynamic::MediaType() const {
127   return CalculateMediaType(frame_);
128 }
129 
DisplayMode() const130 blink::mojom::DisplayMode MediaValuesDynamic::DisplayMode() const {
131   return CalculateDisplayMode(frame_);
132 }
133 
StrictMode() const134 bool MediaValuesDynamic::StrictMode() const {
135   return CalculateStrictMode(frame_);
136 }
137 
ColorGamut() const138 ColorSpaceGamut MediaValuesDynamic::ColorGamut() const {
139   return CalculateColorGamut(frame_);
140 }
141 
GetPreferredColorScheme() const142 mojom::blink::PreferredColorScheme MediaValuesDynamic::GetPreferredColorScheme()
143     const {
144   return CalculatePreferredColorScheme(frame_);
145 }
146 
GetPreferredContrast() const147 mojom::blink::PreferredContrast MediaValuesDynamic::GetPreferredContrast()
148     const {
149   return CalculatePreferredContrast(frame_);
150 }
151 
PrefersReducedMotion() const152 bool MediaValuesDynamic::PrefersReducedMotion() const {
153   return CalculatePrefersReducedMotion(frame_);
154 }
155 
PrefersReducedData() const156 bool MediaValuesDynamic::PrefersReducedData() const {
157   return CalculatePrefersReducedData(frame_);
158 }
159 
GetForcedColors() const160 ForcedColors MediaValuesDynamic::GetForcedColors() const {
161   return CalculateForcedColors();
162 }
163 
GetNavigationControls() const164 NavigationControls MediaValuesDynamic::GetNavigationControls() const {
165   return CalculateNavigationControls(frame_);
166 }
167 
GetScreenSpanning() const168 ScreenSpanning MediaValuesDynamic::GetScreenSpanning() const {
169   return CalculateScreenSpanning(frame_);
170 }
171 
GetDocument() const172 Document* MediaValuesDynamic::GetDocument() const {
173   return frame_->GetDocument();
174 }
175 
HasValues() const176 bool MediaValuesDynamic::HasValues() const {
177   return frame_;
178 }
179 
Trace(Visitor * visitor) const180 void MediaValuesDynamic::Trace(Visitor* visitor) const {
181   visitor->Trace(frame_);
182   MediaValues::Trace(visitor);
183 }
184 
OverrideViewportDimensions(double width,double height)185 void MediaValuesDynamic::OverrideViewportDimensions(double width,
186                                                     double height) {
187   viewport_dimensions_overridden_ = true;
188   viewport_width_override_ = width;
189   viewport_height_override_ = height;
190 }
191 
192 }  // namespace blink
193