OLD | NEW |
---|---|
(Empty) | |
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. | |
abarth-chromium
2014/03/27 21:01:55
Please add a blank line after this line.
| |
4 #include "config.h" | |
5 #include "core/css/MediaValues.h" | |
6 | |
7 #include "core/dom/Document.h" | |
8 #include "core/dom/Element.h" | |
9 #include "core/frame/FrameHost.h" | |
10 #include "core/frame/FrameView.h" | |
11 #include "core/frame/LocalFrame.h" | |
12 #include "core/frame/Settings.h" | |
13 #include "core/html/imports/HTMLImport.h" | |
14 #include "core/page/Page.h" | |
15 #include "core/rendering/RenderObject.h" | |
16 #include "core/rendering/RenderView.h" | |
17 #include "core/rendering/compositing/RenderLayerCompositor.h" | |
18 #include "core/rendering/style/RenderStyle.h" | |
19 #include "platform/PlatformScreen.h" | |
20 | |
21 namespace WebCore { | |
22 | |
23 inline static int calculateViewportWidth(LocalFrame* frame, RenderStyle* style) | |
abarth-chromium
2014/03/27 21:01:55
The inline keyword is redundant with the static ke
| |
24 { | |
25 ASSERT(frame && frame->view() && style); | |
26 int viewportWidth = frame->view()->layoutSize(IncludeScrollbars).width(); | |
27 return adjustForAbsoluteZoom(viewportWidth, style); | |
28 } | |
29 | |
30 inline static int calculateViewportHeight(LocalFrame* frame, RenderStyle* style) | |
31 { | |
32 ASSERT(frame && frame->view() && style); | |
33 int viewportHeight = frame->view()->layoutSize(IncludeScrollbars).height(); | |
34 return adjustForAbsoluteZoom(viewportHeight, style); | |
35 } | |
36 | |
37 inline static int calculateDeviceWidth(LocalFrame* frame) | |
38 { | |
39 ASSERT(frame && frame->view() && frame->settings() && frame->host()); | |
40 int deviceWidth = static_cast<int>(screenRect(frame->view()).width()); | |
41 if (frame->settings()->reportScreenSizeInPhysicalPixelsQuirk()) | |
42 deviceWidth = lroundf(deviceWidth * frame->host()->deviceScaleFactor()); | |
43 return deviceWidth; | |
44 } | |
45 | |
46 inline static int calculateDeviceHeight(LocalFrame* frame) | |
47 { | |
48 ASSERT(frame && frame->view() && frame->settings() && frame->host()); | |
49 int deviceHeight = static_cast<int>(screenRect(frame->view()).height()); | |
50 if (frame->settings()->reportScreenSizeInPhysicalPixelsQuirk()) | |
51 deviceHeight = lroundf(deviceHeight * frame->host()->deviceScaleFactor() ); | |
52 return deviceHeight; | |
53 } | |
54 | |
55 inline static bool calculateStrictMode(LocalFrame* frame) | |
56 { | |
57 ASSERT(frame && frame->document()); | |
58 return !frame->document()->inQuirksMode(); | |
59 } | |
60 | |
61 inline static int calculateDevicePixelRatio(LocalFrame* frame) | |
62 { | |
63 return frame->devicePixelRatio(); | |
64 } | |
65 | |
66 inline static int calculateColorBitsPerComponent(LocalFrame* frame) | |
67 { | |
68 ASSERT(frame && frame->page() && frame->page()->mainFrame()); | |
69 if (screenIsMonochrome(frame->page()->mainFrame()->view())) | |
70 return 0; | |
71 return screenDepthPerComponent(frame->view()); | |
72 } | |
73 | |
74 inline static int calculateMonochromeBitsPerComponent(LocalFrame* frame) | |
75 { | |
76 ASSERT(frame && frame->page() && frame->page()->mainFrame()); | |
77 if (screenIsMonochrome(frame->page()->mainFrame()->view())) | |
78 return screenDepthPerComponent(frame->view()); | |
79 return 0; | |
80 } | |
81 | |
82 inline static int calculateDefaultFontSize(RenderStyle* style) | |
83 { | |
84 return style->fontDescription().specifiedSize(); | |
85 } | |
86 | |
87 inline static bool calculateScanMediaType(LocalFrame* frame) | |
88 { | |
89 ASSERT(frame && frame->view()); | |
90 // Scan only applies to 'tv' media. | |
91 return equalIgnoringCase(frame->view()->mediaType(), "tv"); | |
92 } | |
93 | |
94 inline static bool calculateScreenMediaType(LocalFrame* frame) | |
95 { | |
96 ASSERT(frame && frame->view()); | |
97 return equalIgnoringCase(frame->view()->mediaType(), "screen"); | |
98 } | |
99 | |
100 inline static bool calculatePrintMediaType(LocalFrame* frame) | |
101 { | |
102 ASSERT(frame && frame->view()); | |
103 return equalIgnoringCase(frame->view()->mediaType(), "print"); | |
104 } | |
105 | |
106 inline static bool calculateThreeDEnabled(LocalFrame* frame) | |
107 { | |
108 ASSERT(frame && frame->contentRenderer() && frame->contentRenderer()->compos itor()); | |
109 bool threeDEnabled = false; | |
110 if (RenderView* view = frame->contentRenderer()) | |
111 threeDEnabled = view->compositor()->canRender3DTransforms(); | |
112 return threeDEnabled; | |
113 } | |
114 | |
115 inline static MediaValues::PointerDeviceType calculateLeastCapablePrimaryPointer DeviceType(LocalFrame* frame) | |
116 { | |
117 ASSERT(frame && frame->settings()); | |
118 if (frame->settings()->deviceSupportsTouch()) | |
119 return MediaValues::TouchPointer; | |
120 | |
121 // FIXME: We should also try to determine if we know we have a mouse. | |
122 // When we do this, we'll also need to differentiate between known not to | |
123 // have mouse or touch screen (NoPointer) and unknown (UnknownPointer). | |
124 // We could also take into account other preferences like accessibility | |
125 // settings to decide which of the available pointers should be considered | |
126 // "primary". | |
127 | |
128 return MediaValues::UnknownPointer; | |
129 } | |
130 | |
131 PassRefPtr<MediaValues> MediaValues::create(LocalFrame* frame, RenderStyle* styl e, MediaValuesMode mode) | |
132 { | |
133 ASSERT(frame && style); | |
134 if (mode == DynamicMode) | |
135 return adoptRef(new MediaValues(style, frame, mode)); | |
abarth-chromium
2014/03/27 21:01:55
It's odd that the constructor uses a different ord
| |
136 | |
137 int viewportWidth = calculateViewportWidth(frame, style); | |
138 int viewportHeight = calculateViewportWidth(frame, style); | |
139 int deviceWidth = calculateDeviceWidth(frame); | |
140 int deviceHeight = calculateDeviceHeight(frame); | |
141 bool strictMode = calculateStrictMode(frame); | |
142 float devicePixelRatio = calculateDevicePixelRatio(frame); | |
143 int colorBitsPerComponent = calculateColorBitsPerComponent(frame); | |
144 int monochromeBitsPerComponent = calculateMonochromeBitsPerComponent(frame); | |
145 int defaultFontSize = calculateDefaultFontSize(style); | |
146 bool scanMediaType = calculateScanMediaType(frame); | |
147 bool screenMediaType = calculateScreenMediaType(frame); | |
148 bool printMediaType = calculatePrintMediaType(frame); | |
149 bool threeDEnabled = calculateThreeDEnabled(frame); | |
150 MediaValues::PointerDeviceType pointer = calculateLeastCapablePrimaryPointer DeviceType(frame); | |
151 | |
152 return adoptRef(new MediaValues(mode, | |
153 viewportWidth, | |
154 viewportHeight, | |
155 deviceWidth, | |
156 deviceHeight, | |
157 devicePixelRatio, | |
158 colorBitsPerComponent, | |
159 monochromeBitsPerComponent, | |
160 pointer, | |
161 defaultFontSize, | |
162 threeDEnabled, | |
163 scanMediaType, | |
164 screenMediaType, | |
165 printMediaType, | |
166 strictMode)); | |
abarth-chromium
2014/03/27 21:01:55
Another option is to skip this constructor with th
| |
167 } | |
168 | |
169 PassRefPtr<MediaValues> MediaValues::create(Document* document, MediaValuesMode mode) | |
170 { | |
171 Document* executingDocument = document->import() ? document->import()->maste r() : document; | |
172 ASSERT(executingDocument->frame()); | |
173 ASSERT(executingDocument->renderer()); | |
174 ASSERT(executingDocument->renderer()->style()); | |
175 LocalFrame* frame = executingDocument->frame(); | |
176 RenderStyle* style = executingDocument->renderer()->style(); | |
177 | |
178 return MediaValues::create(frame, style, mode); | |
179 } | |
180 | |
181 PassRefPtr<MediaValues> MediaValues::copy() const | |
182 { | |
183 ASSERT(m_mode == CachingMode && !m_style.get() && !m_frame); | |
184 return adoptRef(new MediaValues(m_mode, | |
185 m_viewportWidth, | |
186 m_viewportHeight, | |
187 m_deviceWidth, | |
188 m_deviceHeight, | |
189 m_devicePixelRatio, | |
190 m_colorBitsPerComponent, | |
191 m_monochromeBitsPerComponent, | |
192 m_pointer, | |
193 m_defaultFontSize, | |
194 m_threeDEnabled, | |
195 m_scanMediaType, | |
196 m_screenMediaType, | |
197 m_printMediaType, | |
198 m_strictMode)); | |
abarth-chromium
2014/03/27 21:01:55
Again, you can do away with the large constructor
| |
199 } | |
200 | |
201 int MediaValues::viewportWidth() const | |
202 { | |
203 if (m_mode == DynamicMode) | |
204 return calculateViewportWidth(m_frame, m_style.get()); | |
205 return m_viewportWidth; | |
206 } | |
207 | |
208 int MediaValues::viewportHeight() const | |
209 { | |
210 if (m_mode == DynamicMode) | |
211 return calculateViewportHeight(m_frame, m_style.get()); | |
212 return m_viewportHeight; | |
213 } | |
214 | |
215 int MediaValues::deviceWidth() const | |
216 { | |
217 if (m_mode == DynamicMode) | |
218 return calculateDeviceWidth(m_frame); | |
219 return m_deviceWidth; | |
220 } | |
221 | |
222 int MediaValues::deviceHeight() const | |
223 { | |
224 if (m_mode == DynamicMode) | |
225 return calculateDeviceHeight(m_frame); | |
226 return m_deviceHeight; | |
227 } | |
228 | |
229 float MediaValues::devicePixelRatio() const | |
230 { | |
231 if (m_mode == DynamicMode) | |
232 return calculateDevicePixelRatio(m_frame); | |
233 return m_devicePixelRatio; | |
234 } | |
235 | |
236 int MediaValues::colorBitsPerComponent() const | |
237 { | |
238 if (m_mode == DynamicMode) | |
239 return calculateColorBitsPerComponent(m_frame); | |
240 return m_colorBitsPerComponent; | |
241 } | |
242 | |
243 int MediaValues::monochromeBitsPerComponent() const | |
244 { | |
245 if (m_mode == DynamicMode) | |
246 return calculateMonochromeBitsPerComponent(m_frame); | |
247 return m_monochromeBitsPerComponent; | |
248 } | |
249 | |
250 MediaValues::PointerDeviceType MediaValues::pointer() const | |
251 { | |
252 if (m_mode == DynamicMode) | |
253 return calculateLeastCapablePrimaryPointerDeviceType(m_frame); | |
254 return m_pointer; | |
255 } | |
256 | |
257 int MediaValues::defaultFontSize() const | |
258 { | |
259 if (m_mode == DynamicMode) | |
260 return calculateDefaultFontSize(m_style.get()); | |
261 return m_defaultFontSize; | |
262 } | |
263 | |
264 bool MediaValues::threeDEnabled() const | |
265 { | |
266 if (m_mode == DynamicMode) | |
267 return calculateThreeDEnabled(m_frame); | |
268 return m_threeDEnabled; | |
269 } | |
270 | |
271 bool MediaValues::scanMediaType() const | |
272 { | |
273 if (m_mode == DynamicMode) | |
274 return calculateScanMediaType(m_frame); | |
275 return m_scanMediaType; | |
276 } | |
277 | |
278 bool MediaValues::screenMediaType() const | |
279 { | |
280 if (m_mode == DynamicMode) | |
281 return calculateScreenMediaType(m_frame); | |
282 return m_screenMediaType; | |
283 } | |
284 | |
285 bool MediaValues::printMediaType() const | |
286 { | |
287 if (m_mode == DynamicMode) | |
288 return calculatePrintMediaType(m_frame); | |
289 return m_printMediaType; | |
290 } | |
291 | |
292 bool MediaValues::strictMode() const | |
293 { | |
294 if (m_mode == DynamicMode) | |
295 return calculateStrictMode(m_frame); | |
296 return m_strictMode; | |
297 } | |
298 | |
299 Document* MediaValues::document() const | |
300 { | |
301 if (!m_frame) | |
302 return 0; | |
303 return m_frame->document(); | |
304 } | |
305 | |
306 } // namespace | |
OLD | NEW |