OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "config.h" | 5 #include "config.h" |
6 #include "core/css/MediaValues.h" | 6 #include "core/css/MediaValues.h" |
7 | 7 |
8 #include "core/css/MediaValuesCached.h" | |
9 #include "core/css/MediaValuesDynamic.h" | |
8 #include "core/dom/Document.h" | 10 #include "core/dom/Document.h" |
9 #include "core/dom/Element.h" | 11 #include "core/dom/Element.h" |
10 #include "core/frame/FrameHost.h" | 12 #include "core/frame/FrameHost.h" |
11 #include "core/frame/FrameView.h" | 13 #include "core/frame/FrameView.h" |
12 #include "core/frame/LocalFrame.h" | 14 #include "core/frame/LocalFrame.h" |
13 #include "core/frame/Settings.h" | 15 #include "core/frame/Settings.h" |
14 #include "core/html/imports/HTMLImportsController.h" | 16 #include "core/html/imports/HTMLImportsController.h" |
15 #include "core/page/Page.h" | 17 #include "core/page/Page.h" |
16 #include "core/rendering/RenderObject.h" | 18 #include "core/rendering/RenderObject.h" |
17 #include "core/rendering/RenderView.h" | 19 #include "core/rendering/RenderView.h" |
18 #include "core/rendering/compositing/RenderLayerCompositor.h" | 20 #include "core/rendering/compositing/RenderLayerCompositor.h" |
19 #include "core/rendering/style/RenderStyle.h" | 21 #include "core/rendering/style/RenderStyle.h" |
20 #include "platform/PlatformScreen.h" | 22 #include "platform/PlatformScreen.h" |
21 | 23 |
22 namespace WebCore { | 24 namespace WebCore { |
23 | 25 |
24 static int calculateViewportWidth(LocalFrame* frame, RenderStyle* style) | 26 Document* MediaValues::getExecutingDocument(Document& document) |
27 { | |
28 Document* executingDocument = document.importsController() ? document.import sController()->master() : &document; | |
29 ASSERT(executingDocument); | |
30 ASSERT(executingDocument->renderer()); | |
31 return executingDocument; | |
32 } | |
33 | |
34 int MediaValues::calculateViewportWidth(LocalFrame* frame, RenderStyle* style) c onst | |
25 { | 35 { |
26 ASSERT(frame && frame->view() && style); | 36 ASSERT(frame && frame->view() && style); |
27 int viewportWidth = frame->view()->layoutSize(IncludeScrollbars).width(); | 37 int viewportWidth = frame->view()->layoutSize(IncludeScrollbars).width(); |
28 return adjustForAbsoluteZoom(viewportWidth, style); | 38 return adjustForAbsoluteZoom(viewportWidth, style); |
29 } | 39 } |
30 | 40 |
31 static int calculateViewportHeight(LocalFrame* frame, RenderStyle* style) | 41 int MediaValues::calculateViewportHeight(LocalFrame* frame, RenderStyle* style) const |
32 { | 42 { |
33 ASSERT(frame && frame->view() && style); | 43 ASSERT(frame && frame->view() && style); |
34 int viewportHeight = frame->view()->layoutSize(IncludeScrollbars).height(); | 44 int viewportHeight = frame->view()->layoutSize(IncludeScrollbars).height(); |
35 return adjustForAbsoluteZoom(viewportHeight, style); | 45 return adjustForAbsoluteZoom(viewportHeight, style); |
36 } | 46 } |
37 | 47 |
38 static int calculateDeviceWidth(LocalFrame* frame) | 48 int MediaValues::calculateDeviceWidth(LocalFrame* frame) const |
39 { | 49 { |
40 ASSERT(frame && frame->view() && frame->settings() && frame->host()); | 50 ASSERT(frame && frame->view() && frame->settings() && frame->host()); |
41 int deviceWidth = static_cast<int>(screenRect(frame->view()).width()); | 51 int deviceWidth = static_cast<int>(screenRect(frame->view()).width()); |
42 if (frame->settings()->reportScreenSizeInPhysicalPixelsQuirk()) | 52 if (frame->settings()->reportScreenSizeInPhysicalPixelsQuirk()) |
43 deviceWidth = lroundf(deviceWidth * frame->host()->deviceScaleFactor()); | 53 deviceWidth = lroundf(deviceWidth * frame->host()->deviceScaleFactor()); |
44 return deviceWidth; | 54 return deviceWidth; |
45 } | 55 } |
46 | 56 |
47 static int calculateDeviceHeight(LocalFrame* frame) | 57 int MediaValues::calculateDeviceHeight(LocalFrame* frame) const |
48 { | 58 { |
49 ASSERT(frame && frame->view() && frame->settings() && frame->host()); | 59 ASSERT(frame && frame->view() && frame->settings() && frame->host()); |
50 int deviceHeight = static_cast<int>(screenRect(frame->view()).height()); | 60 int deviceHeight = static_cast<int>(screenRect(frame->view()).height()); |
51 if (frame->settings()->reportScreenSizeInPhysicalPixelsQuirk()) | 61 if (frame->settings()->reportScreenSizeInPhysicalPixelsQuirk()) |
52 deviceHeight = lroundf(deviceHeight * frame->host()->deviceScaleFactor() ); | 62 deviceHeight = lroundf(deviceHeight * frame->host()->deviceScaleFactor() ); |
53 return deviceHeight; | 63 return deviceHeight; |
54 } | 64 } |
55 | 65 |
56 static bool calculateStrictMode(LocalFrame* frame) | 66 bool MediaValues::calculateStrictMode(LocalFrame* frame) const |
57 { | 67 { |
58 ASSERT(frame && frame->document()); | 68 ASSERT(frame && frame->document()); |
59 return !frame->document()->inQuirksMode(); | 69 return !frame->document()->inQuirksMode(); |
60 } | 70 } |
61 | 71 |
62 static float calculateDevicePixelRatio(LocalFrame* frame) | 72 float MediaValues::calculateDevicePixelRatio(LocalFrame* frame) const |
63 { | 73 { |
64 return frame->devicePixelRatio(); | 74 return frame->devicePixelRatio(); |
65 } | 75 } |
66 | 76 |
67 static int calculateColorBitsPerComponent(LocalFrame* frame) | 77 int MediaValues::calculateColorBitsPerComponent(LocalFrame* frame) const |
68 { | 78 { |
69 ASSERT(frame && frame->page() && frame->page()->mainFrame()); | 79 ASSERT(frame && frame->page() && frame->page()->mainFrame()); |
70 if (screenIsMonochrome(frame->page()->mainFrame()->view())) | 80 if (screenIsMonochrome(frame->page()->mainFrame()->view())) |
71 return 0; | 81 return 0; |
72 return screenDepthPerComponent(frame->view()); | 82 return screenDepthPerComponent(frame->view()); |
73 } | 83 } |
74 | 84 |
75 static int calculateMonochromeBitsPerComponent(LocalFrame* frame) | 85 int MediaValues::calculateMonochromeBitsPerComponent(LocalFrame* frame) const |
76 { | 86 { |
77 ASSERT(frame && frame->page() && frame->page()->mainFrame()); | 87 ASSERT(frame && frame->page() && frame->page()->mainFrame()); |
78 if (screenIsMonochrome(frame->page()->mainFrame()->view())) | 88 if (screenIsMonochrome(frame->page()->mainFrame()->view())) |
79 return screenDepthPerComponent(frame->view()); | 89 return screenDepthPerComponent(frame->view()); |
80 return 0; | 90 return 0; |
81 } | 91 } |
82 | 92 |
83 static int calculateDefaultFontSize(RenderStyle* style) | 93 int MediaValues::calculateDefaultFontSize(RenderStyle* style) const |
eseidel
2014/04/15 18:47:03
A bit odd that these are in MediaValues instead of
| |
84 { | 94 { |
85 return style->fontDescription().specifiedSize(); | 95 return style->fontDescription().specifiedSize(); |
86 } | 96 } |
87 | 97 |
88 static bool calculateScanMediaType(LocalFrame* frame) | 98 int MediaValues::calculateComputedFontSize(RenderStyle* style) const |
99 { | |
100 return style->fontDescription().computedSize(); | |
101 } | |
102 | |
103 bool MediaValues::calculateHasXHeight(RenderStyle* style) const | |
104 { | |
105 return style->fontMetrics().hasXHeight(); | |
106 } | |
107 | |
108 double MediaValues::calculateXHeight(RenderStyle* style) const | |
109 { | |
110 return style->fontMetrics().xHeight(); | |
111 } | |
112 | |
113 double MediaValues::calculateZeroWidth(RenderStyle* style) const | |
114 { | |
115 return style->fontMetrics().zeroWidth(); | |
116 } | |
117 | |
118 bool MediaValues::calculateScanMediaType(LocalFrame* frame) const | |
89 { | 119 { |
90 ASSERT(frame && frame->view()); | 120 ASSERT(frame && frame->view()); |
91 // Scan only applies to 'tv' media. | 121 // Scan only applies to 'tv' media. |
92 return equalIgnoringCase(frame->view()->mediaType(), "tv"); | 122 return equalIgnoringCase(frame->view()->mediaType(), "tv"); |
93 } | 123 } |
94 | 124 |
95 static bool calculateScreenMediaType(LocalFrame* frame) | 125 bool MediaValues::calculateScreenMediaType(LocalFrame* frame) const |
96 { | 126 { |
97 ASSERT(frame && frame->view()); | 127 ASSERT(frame && frame->view()); |
98 return equalIgnoringCase(frame->view()->mediaType(), "screen"); | 128 return equalIgnoringCase(frame->view()->mediaType(), "screen"); |
99 } | 129 } |
100 | 130 |
101 static bool calculatePrintMediaType(LocalFrame* frame) | 131 bool MediaValues::calculatePrintMediaType(LocalFrame* frame) const |
102 { | 132 { |
103 ASSERT(frame && frame->view()); | 133 ASSERT(frame && frame->view()); |
104 return equalIgnoringCase(frame->view()->mediaType(), "print"); | 134 return equalIgnoringCase(frame->view()->mediaType(), "print"); |
105 } | 135 } |
106 | 136 |
107 static bool calculateThreeDEnabled(LocalFrame* frame) | 137 bool MediaValues::calculateThreeDEnabled(LocalFrame* frame) const |
108 { | 138 { |
109 ASSERT(frame && frame->contentRenderer() && frame->contentRenderer()->compos itor()); | 139 ASSERT(frame && frame->contentRenderer() && frame->contentRenderer()->compos itor()); |
110 bool threeDEnabled = false; | 140 bool threeDEnabled = false; |
111 if (RenderView* view = frame->contentRenderer()) | 141 if (RenderView* view = frame->contentRenderer()) |
112 threeDEnabled = view->compositor()->canRender3DTransforms(); | 142 threeDEnabled = view->compositor()->canRender3DTransforms(); |
113 return threeDEnabled; | 143 return threeDEnabled; |
114 } | 144 } |
115 | 145 |
116 static MediaValues::PointerDeviceType calculateLeastCapablePrimaryPointerDeviceT ype(LocalFrame* frame) | 146 MediaValues::PointerDeviceType MediaValues::calculateLeastCapablePrimaryPointerD eviceType(LocalFrame* frame) const |
117 { | 147 { |
118 ASSERT(frame && frame->settings()); | 148 ASSERT(frame && frame->settings()); |
119 if (frame->settings()->deviceSupportsTouch()) | 149 if (frame->settings()->deviceSupportsTouch()) |
120 return MediaValues::TouchPointer; | 150 return MediaValues::TouchPointer; |
121 | 151 |
122 // FIXME: We should also try to determine if we know we have a mouse. | 152 // FIXME: We should also try to determine if we know we have a mouse. |
123 // When we do this, we'll also need to differentiate between known not to | 153 // When we do this, we'll also need to differentiate between known not to |
124 // have mouse or touch screen (NoPointer) and unknown (UnknownPointer). | 154 // have mouse or touch screen (NoPointer) and unknown (UnknownPointer). |
125 // We could also take into account other preferences like accessibility | 155 // We could also take into account other preferences like accessibility |
126 // settings to decide which of the available pointers should be considered | 156 // settings to decide which of the available pointers should be considered |
127 // "primary". | 157 // "primary". |
128 | 158 |
129 return MediaValues::UnknownPointer; | 159 return MediaValues::UnknownPointer; |
130 } | 160 } |
131 | 161 |
132 PassRefPtr<MediaValues> MediaValues::create(MediaValuesMode mode, | |
133 int viewportWidth, | |
134 int viewportHeight, | |
135 int deviceWidth, | |
136 int deviceHeight, | |
137 float devicePixelRatio, | |
138 int colorBitsPerComponent, | |
139 int monochromeBitsPerComponent, | |
140 PointerDeviceType pointer, | |
141 int defaultFontSize, | |
142 bool threeDEnabled, | |
143 bool scanMediaType, | |
144 bool screenMediaType, | |
145 bool printMediaType, | |
146 bool strictMode) | |
147 { | |
148 ASSERT(mode == CachingMode); | |
149 RefPtr<MediaValues> mediaValues = adoptRef(new MediaValues(0, nullptr, mode) ); | |
150 mediaValues->m_viewportWidth = viewportWidth; | |
151 mediaValues->m_viewportHeight = viewportHeight; | |
152 mediaValues->m_deviceWidth = deviceWidth; | |
153 mediaValues->m_deviceHeight = deviceHeight; | |
154 mediaValues->m_devicePixelRatio = devicePixelRatio; | |
155 mediaValues->m_colorBitsPerComponent = colorBitsPerComponent; | |
156 mediaValues->m_monochromeBitsPerComponent = monochromeBitsPerComponent; | |
157 mediaValues->m_pointer = pointer; | |
158 mediaValues->m_defaultFontSize = defaultFontSize; | |
159 mediaValues->m_threeDEnabled = threeDEnabled; | |
160 mediaValues->m_scanMediaType = scanMediaType; | |
161 mediaValues->m_screenMediaType = screenMediaType; | |
162 mediaValues->m_printMediaType = printMediaType; | |
163 mediaValues->m_strictMode = strictMode; | |
164 | |
165 return mediaValues; | |
166 } | |
167 | |
168 PassRefPtr<MediaValues> MediaValues::create(LocalFrame* frame, RenderStyle* styl e, MediaValuesMode mode) | |
169 { | |
170 ASSERT(frame && style); | |
171 RefPtr<MediaValues> mediaValues; | |
172 mediaValues = adoptRef(new MediaValues(frame, style, mode)); | |
173 if (mode == CachingMode) { | |
174 mediaValues->m_viewportWidth = calculateViewportWidth(frame, style); | |
175 mediaValues->m_viewportHeight = calculateViewportHeight(frame, style), | |
176 mediaValues->m_deviceWidth = calculateDeviceWidth(frame), | |
177 mediaValues->m_deviceHeight = calculateDeviceHeight(frame), | |
178 mediaValues->m_devicePixelRatio = calculateDevicePixelRatio(frame), | |
179 mediaValues->m_colorBitsPerComponent = calculateColorBitsPerComponent(fr ame), | |
180 mediaValues->m_monochromeBitsPerComponent = calculateMonochromeBitsPerCo mponent(frame), | |
181 mediaValues->m_pointer = calculateLeastCapablePrimaryPointerDeviceType(f rame), | |
182 mediaValues->m_defaultFontSize = calculateDefaultFontSize(style), | |
183 mediaValues->m_threeDEnabled = calculateThreeDEnabled(frame), | |
184 mediaValues->m_scanMediaType = calculateScanMediaType(frame), | |
185 mediaValues->m_screenMediaType = calculateScreenMediaType(frame), | |
186 mediaValues->m_printMediaType = calculatePrintMediaType(frame), | |
187 mediaValues->m_strictMode = calculateStrictMode(frame); | |
188 | |
189 mediaValues->m_style.clear(); | |
190 mediaValues->m_frame = 0; | |
191 } | |
192 | |
193 return mediaValues; | |
194 } | |
195 | |
196 PassRefPtr<MediaValues> MediaValues::create(Document* document, MediaValuesMode mode) | |
197 { | |
198 ASSERT(document); | |
199 Document* executingDocument = document->importsController() ? document->impo rtsController()->master() : document; | |
200 ASSERT(executingDocument->frame()); | |
201 ASSERT(executingDocument->renderer()); | |
202 ASSERT(executingDocument->renderer()->style()); | |
203 LocalFrame* frame = executingDocument->frame(); | |
204 RenderStyle* style = executingDocument->renderer()->style(); | |
205 | |
206 return MediaValues::create(frame, style, mode); | |
207 } | |
208 | |
209 PassRefPtr<MediaValues> MediaValues::copy() const | |
210 { | |
211 ASSERT(m_mode == CachingMode && !m_style.get() && !m_frame); | |
212 RefPtr<MediaValues> mediaValues = adoptRef(new MediaValues(0, nullptr, m_mod e)); | |
213 mediaValues->m_viewportWidth = m_viewportWidth; | |
214 mediaValues->m_viewportHeight = m_viewportHeight; | |
215 mediaValues->m_deviceWidth = m_deviceWidth; | |
216 mediaValues->m_deviceHeight = m_deviceHeight; | |
217 mediaValues->m_devicePixelRatio = m_devicePixelRatio; | |
218 mediaValues->m_colorBitsPerComponent = m_colorBitsPerComponent; | |
219 mediaValues->m_monochromeBitsPerComponent = m_monochromeBitsPerComponent; | |
220 mediaValues->m_pointer = m_pointer; | |
221 mediaValues->m_defaultFontSize = m_defaultFontSize; | |
222 mediaValues->m_threeDEnabled = m_threeDEnabled; | |
223 mediaValues->m_scanMediaType = m_scanMediaType; | |
224 mediaValues->m_screenMediaType = m_screenMediaType; | |
225 mediaValues->m_printMediaType = m_printMediaType; | |
226 mediaValues->m_strictMode = m_strictMode; | |
227 | |
228 return mediaValues; | |
229 } | |
230 | |
231 bool MediaValues::isSafeToSendToAnotherThread() const | |
232 { | |
233 return (!m_frame && !m_style && m_mode == CachingMode && hasOneRef()); | |
234 } | |
235 | |
236 int MediaValues::viewportWidth() const | |
237 { | |
238 if (m_mode == DynamicMode) | |
239 return calculateViewportWidth(m_frame, m_style.get()); | |
240 return m_viewportWidth; | |
241 } | |
242 | |
243 int MediaValues::viewportHeight() const | |
244 { | |
245 if (m_mode == DynamicMode) | |
246 return calculateViewportHeight(m_frame, m_style.get()); | |
247 return m_viewportHeight; | |
248 } | |
249 | |
250 int MediaValues::deviceWidth() const | |
251 { | |
252 if (m_mode == DynamicMode) | |
253 return calculateDeviceWidth(m_frame); | |
254 return m_deviceWidth; | |
255 } | |
256 | |
257 int MediaValues::deviceHeight() const | |
258 { | |
259 if (m_mode == DynamicMode) | |
260 return calculateDeviceHeight(m_frame); | |
261 return m_deviceHeight; | |
262 } | |
263 | |
264 float MediaValues::devicePixelRatio() const | |
265 { | |
266 if (m_mode == DynamicMode) | |
267 return calculateDevicePixelRatio(m_frame); | |
268 return m_devicePixelRatio; | |
269 } | |
270 | |
271 int MediaValues::colorBitsPerComponent() const | |
272 { | |
273 if (m_mode == DynamicMode) | |
274 return calculateColorBitsPerComponent(m_frame); | |
275 return m_colorBitsPerComponent; | |
276 } | |
277 | |
278 int MediaValues::monochromeBitsPerComponent() const | |
279 { | |
280 if (m_mode == DynamicMode) | |
281 return calculateMonochromeBitsPerComponent(m_frame); | |
282 return m_monochromeBitsPerComponent; | |
283 } | |
284 | |
285 MediaValues::PointerDeviceType MediaValues::pointer() const | |
286 { | |
287 if (m_mode == DynamicMode) | |
288 return calculateLeastCapablePrimaryPointerDeviceType(m_frame); | |
289 return m_pointer; | |
290 } | |
291 | |
292 int MediaValues::defaultFontSize() const | |
293 { | |
294 if (m_mode == DynamicMode) | |
295 return calculateDefaultFontSize(m_style.get()); | |
296 return m_defaultFontSize; | |
297 } | |
298 | |
299 bool MediaValues::threeDEnabled() const | |
300 { | |
301 if (m_mode == DynamicMode) | |
302 return calculateThreeDEnabled(m_frame); | |
303 return m_threeDEnabled; | |
304 } | |
305 | |
306 bool MediaValues::scanMediaType() const | |
307 { | |
308 if (m_mode == DynamicMode) | |
309 return calculateScanMediaType(m_frame); | |
310 return m_scanMediaType; | |
311 } | |
312 | |
313 bool MediaValues::screenMediaType() const | |
314 { | |
315 if (m_mode == DynamicMode) | |
316 return calculateScreenMediaType(m_frame); | |
317 return m_screenMediaType; | |
318 } | |
319 | |
320 bool MediaValues::printMediaType() const | |
321 { | |
322 if (m_mode == DynamicMode) | |
323 return calculatePrintMediaType(m_frame); | |
324 return m_printMediaType; | |
325 } | |
326 | |
327 bool MediaValues::strictMode() const | |
328 { | |
329 if (m_mode == DynamicMode) | |
330 return calculateStrictMode(m_frame); | |
331 return m_strictMode; | |
332 } | |
333 | |
334 Document* MediaValues::document() const | |
335 { | |
336 if (!m_frame) | |
337 return 0; | |
338 return m_frame->document(); | |
339 } | |
340 | |
341 } // namespace | 162 } // namespace |
OLD | NEW |