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/public/common/css/preferred_color_scheme.h"
10 #include "third_party/blink/renderer/core/css/css_primitive_value.h"
11 #include "third_party/blink/renderer/core/css/css_resolution_units.h"
12 #include "third_party/blink/renderer/core/css/css_to_length_conversion_data.h"
13 #include "third_party/blink/renderer/core/css/media_values_cached.h"
14 #include "third_party/blink/renderer/core/dom/document.h"
15 #include "third_party/blink/renderer/core/frame/local_frame.h"
16 
17 namespace blink {
18 
Create(Document & document)19 MediaValues* MediaValuesDynamic::Create(Document& document) {
20   return MediaValuesDynamic::Create(document.GetFrameOfMasterDocument());
21 }
22 
Create(LocalFrame * frame)23 MediaValues* MediaValuesDynamic::Create(LocalFrame* frame) {
24   if (!frame || !frame->View() || !frame->GetDocument() ||
25       !frame->GetDocument()->GetLayoutView())
26     return MakeGarbageCollected<MediaValuesCached>();
27   return MakeGarbageCollected<MediaValuesDynamic>(frame);
28 }
29 
MediaValuesDynamic(LocalFrame * frame)30 MediaValuesDynamic::MediaValuesDynamic(LocalFrame* frame)
31     : frame_(frame),
32       viewport_dimensions_overridden_(false),
33       viewport_width_override_(0),
34       viewport_height_override_(0) {
35   DCHECK(frame_);
36 }
37 
MediaValuesDynamic(LocalFrame * frame,bool overridden_viewport_dimensions,double viewport_width,double viewport_height)38 MediaValuesDynamic::MediaValuesDynamic(LocalFrame* frame,
39                                        bool overridden_viewport_dimensions,
40                                        double viewport_width,
41                                        double viewport_height)
42     : frame_(frame),
43       viewport_dimensions_overridden_(overridden_viewport_dimensions),
44       viewport_width_override_(viewport_width),
45       viewport_height_override_(viewport_height) {
46   DCHECK(frame_);
47 }
48 
Copy() const49 MediaValues* MediaValuesDynamic::Copy() const {
50   return MakeGarbageCollected<MediaValuesDynamic>(
51       frame_, viewport_dimensions_overridden_, viewport_width_override_,
52       viewport_height_override_);
53 }
54 
ComputeLength(double value,CSSPrimitiveValue::UnitType type,int & result) const55 bool MediaValuesDynamic::ComputeLength(double value,
56                                        CSSPrimitiveValue::UnitType type,
57                                        int& result) const {
58   return MediaValues::ComputeLength(value, type,
59                                     CalculateDefaultFontSize(frame_),
60                                     ViewportWidth(), ViewportHeight(), result);
61 }
62 
ComputeLength(double value,CSSPrimitiveValue::UnitType type,double & result) const63 bool MediaValuesDynamic::ComputeLength(double value,
64                                        CSSPrimitiveValue::UnitType type,
65                                        double& result) const {
66   return MediaValues::ComputeLength(value, type,
67                                     CalculateDefaultFontSize(frame_),
68                                     ViewportWidth(), ViewportHeight(), result);
69 }
70 
ViewportWidth() const71 double MediaValuesDynamic::ViewportWidth() const {
72   if (viewport_dimensions_overridden_)
73     return viewport_width_override_;
74   return CalculateViewportWidth(frame_);
75 }
76 
ViewportHeight() const77 double MediaValuesDynamic::ViewportHeight() const {
78   if (viewport_dimensions_overridden_)
79     return viewport_height_override_;
80   return CalculateViewportHeight(frame_);
81 }
82 
DeviceWidth() const83 int MediaValuesDynamic::DeviceWidth() const {
84   return CalculateDeviceWidth(frame_);
85 }
86 
DeviceHeight() const87 int MediaValuesDynamic::DeviceHeight() const {
88   return CalculateDeviceHeight(frame_);
89 }
90 
DevicePixelRatio() const91 float MediaValuesDynamic::DevicePixelRatio() const {
92   return CalculateDevicePixelRatio(frame_);
93 }
94 
ColorBitsPerComponent() const95 int MediaValuesDynamic::ColorBitsPerComponent() const {
96   return CalculateColorBitsPerComponent(frame_);
97 }
98 
MonochromeBitsPerComponent() const99 int MediaValuesDynamic::MonochromeBitsPerComponent() const {
100   return CalculateMonochromeBitsPerComponent(frame_);
101 }
102 
PrimaryPointerType() const103 PointerType MediaValuesDynamic::PrimaryPointerType() const {
104   return CalculatePrimaryPointerType(frame_);
105 }
106 
AvailablePointerTypes() const107 int MediaValuesDynamic::AvailablePointerTypes() const {
108   return CalculateAvailablePointerTypes(frame_);
109 }
110 
PrimaryHoverType() const111 HoverType MediaValuesDynamic::PrimaryHoverType() const {
112   return CalculatePrimaryHoverType(frame_);
113 }
114 
AvailableHoverTypes() const115 int MediaValuesDynamic::AvailableHoverTypes() const {
116   return CalculateAvailableHoverTypes(frame_);
117 }
118 
ThreeDEnabled() const119 bool MediaValuesDynamic::ThreeDEnabled() const {
120   return CalculateThreeDEnabled(frame_);
121 }
122 
InImmersiveMode() const123 bool MediaValuesDynamic::InImmersiveMode() const {
124   return CalculateInImmersiveMode(frame_);
125 }
126 
MediaType() const127 const String MediaValuesDynamic::MediaType() const {
128   return CalculateMediaType(frame_);
129 }
130 
DisplayMode() const131 blink::mojom::DisplayMode MediaValuesDynamic::DisplayMode() const {
132   return CalculateDisplayMode(frame_);
133 }
134 
StrictMode() const135 bool MediaValuesDynamic::StrictMode() const {
136   return CalculateStrictMode(frame_);
137 }
138 
GetDisplayShape() const139 DisplayShape MediaValuesDynamic::GetDisplayShape() const {
140   return CalculateDisplayShape(frame_);
141 }
142 
ColorGamut() const143 ColorSpaceGamut MediaValuesDynamic::ColorGamut() const {
144   return CalculateColorGamut(frame_);
145 }
146 
GetPreferredColorScheme() const147 PreferredColorScheme MediaValuesDynamic::GetPreferredColorScheme() const {
148   return CalculatePreferredColorScheme(frame_);
149 }
150 
PrefersReducedMotion() const151 bool MediaValuesDynamic::PrefersReducedMotion() const {
152   return CalculatePrefersReducedMotion(frame_);
153 }
154 
GetForcedColors() const155 ForcedColors MediaValuesDynamic::GetForcedColors() const {
156   return CalculateForcedColors();
157 }
158 
GetNavigationControls() const159 NavigationControls MediaValuesDynamic::GetNavigationControls() const {
160   return CalculateNavigationControls(frame_);
161 }
162 
GetDocument() const163 Document* MediaValuesDynamic::GetDocument() const {
164   return frame_->GetDocument();
165 }
166 
HasValues() const167 bool MediaValuesDynamic::HasValues() const {
168   return frame_;
169 }
170 
Trace(Visitor * visitor)171 void MediaValuesDynamic::Trace(Visitor* visitor) {
172   visitor->Trace(frame_);
173   MediaValues::Trace(visitor);
174 }
175 
OverrideViewportDimensions(double width,double height)176 void MediaValuesDynamic::OverrideViewportDimensions(double width,
177                                                     double height) {
178   viewport_dimensions_overridden_ = true;
179   viewport_width_override_ = width;
180   viewport_height_override_ = height;
181 }
182 
183 }  // namespace blink
184