Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(392)

Side by Side Diff: Source/core/css/MediaValues.cpp

Issue 201813002: Enable Media query evaluation in the preload scanner (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Fix Debug crash. Values based MediaValues::create Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/core/css/MediaValues.h ('k') | Source/core/html/parser/HTMLDocumentParser.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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.
4
5 #include "config.h"
6 #include "core/css/MediaValues.h"
7
8 #include "core/dom/Document.h"
9 #include "core/dom/Element.h"
10 #include "core/frame/FrameHost.h"
11 #include "core/frame/FrameView.h"
12 #include "core/frame/LocalFrame.h"
13 #include "core/frame/Settings.h"
14 #include "core/html/imports/HTMLImport.h"
15 #include "core/page/Page.h"
16 #include "core/rendering/RenderObject.h"
17 #include "core/rendering/RenderView.h"
18 #include "core/rendering/compositing/RenderLayerCompositor.h"
19 #include "core/rendering/style/RenderStyle.h"
20 #include "platform/PlatformScreen.h"
21
22 namespace WebCore {
23
24 static int calculateViewportWidth(LocalFrame* frame, RenderStyle* style)
25 {
26 ASSERT(frame && frame->view() && style);
27 int viewportWidth = frame->view()->layoutSize(IncludeScrollbars).width();
28 return adjustForAbsoluteZoom(viewportWidth, style);
29 }
30
31 static int calculateViewportHeight(LocalFrame* frame, RenderStyle* style)
32 {
33 ASSERT(frame && frame->view() && style);
34 int viewportHeight = frame->view()->layoutSize(IncludeScrollbars).height();
35 return adjustForAbsoluteZoom(viewportHeight, style);
36 }
37
38 static int calculateDeviceWidth(LocalFrame* frame)
39 {
40 ASSERT(frame && frame->view() && frame->settings() && frame->host());
41 int deviceWidth = static_cast<int>(screenRect(frame->view()).width());
42 if (frame->settings()->reportScreenSizeInPhysicalPixelsQuirk())
43 deviceWidth = lroundf(deviceWidth * frame->host()->deviceScaleFactor());
44 return deviceWidth;
45 }
46
47 static int calculateDeviceHeight(LocalFrame* frame)
48 {
49 ASSERT(frame && frame->view() && frame->settings() && frame->host());
50 int deviceHeight = static_cast<int>(screenRect(frame->view()).height());
51 if (frame->settings()->reportScreenSizeInPhysicalPixelsQuirk())
52 deviceHeight = lroundf(deviceHeight * frame->host()->deviceScaleFactor() );
53 return deviceHeight;
54 }
55
56 static bool calculateStrictMode(LocalFrame* frame)
57 {
58 ASSERT(frame && frame->document());
59 return !frame->document()->inQuirksMode();
60 }
61
62 static float calculateDevicePixelRatio(LocalFrame* frame)
63 {
64 return frame->devicePixelRatio();
65 }
66
67 static int calculateColorBitsPerComponent(LocalFrame* frame)
68 {
69 ASSERT(frame && frame->page() && frame->page()->mainFrame());
70 if (screenIsMonochrome(frame->page()->mainFrame()->view()))
71 return 0;
72 return screenDepthPerComponent(frame->view());
73 }
74
75 static int calculateMonochromeBitsPerComponent(LocalFrame* frame)
76 {
77 ASSERT(frame && frame->page() && frame->page()->mainFrame());
78 if (screenIsMonochrome(frame->page()->mainFrame()->view()))
79 return screenDepthPerComponent(frame->view());
80 return 0;
81 }
82
83 static int calculateDefaultFontSize(RenderStyle* style)
84 {
85 return style->fontDescription().specifiedSize();
86 }
87
88 static bool calculateScanMediaType(LocalFrame* frame)
89 {
90 ASSERT(frame && frame->view());
91 // Scan only applies to 'tv' media.
92 return equalIgnoringCase(frame->view()->mediaType(), "tv");
93 }
94
95 static bool calculateScreenMediaType(LocalFrame* frame)
96 {
97 ASSERT(frame && frame->view());
98 return equalIgnoringCase(frame->view()->mediaType(), "screen");
99 }
100
101 static bool calculatePrintMediaType(LocalFrame* frame)
102 {
103 ASSERT(frame && frame->view());
104 return equalIgnoringCase(frame->view()->mediaType(), "print");
105 }
106
107 static bool calculateThreeDEnabled(LocalFrame* frame)
108 {
109 ASSERT(frame && frame->contentRenderer() && frame->contentRenderer()->compos itor());
110 bool threeDEnabled = false;
111 if (RenderView* view = frame->contentRenderer())
112 threeDEnabled = view->compositor()->canRender3DTransforms();
113 return threeDEnabled;
114 }
115
116 static MediaValues::PointerDeviceType calculateLeastCapablePrimaryPointerDeviceT ype(LocalFrame* frame)
117 {
118 ASSERT(frame && frame->settings());
119 if (frame->settings()->deviceSupportsTouch())
120 return MediaValues::TouchPointer;
121
122 // 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
124 // have mouse or touch screen (NoPointer) and unknown (UnknownPointer).
125 // We could also take into account other preferences like accessibility
126 // settings to decide which of the available pointers should be considered
127 // "primary".
128
129 return MediaValues::UnknownPointer;
130 }
131
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->import() ? document->import()->maste r() : 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
OLDNEW
« no previous file with comments | « Source/core/css/MediaValues.h ('k') | Source/core/html/parser/HTMLDocumentParser.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698