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

Unified 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: Debug and clang fixes 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 side-by-side diff with in-line comments
Download patch
Index: Source/core/css/MediaValues.cpp
diff --git a/Source/core/css/MediaValues.cpp b/Source/core/css/MediaValues.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..fd76503bb5740a4c58ea3471657292a55bf26886
--- /dev/null
+++ b/Source/core/css/MediaValues.cpp
@@ -0,0 +1,306 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
abarth-chromium 2014/03/27 21:01:55 Please add a blank line after this line.
+#include "config.h"
+#include "core/css/MediaValues.h"
+
+#include "core/dom/Document.h"
+#include "core/dom/Element.h"
+#include "core/frame/FrameHost.h"
+#include "core/frame/FrameView.h"
+#include "core/frame/LocalFrame.h"
+#include "core/frame/Settings.h"
+#include "core/html/imports/HTMLImport.h"
+#include "core/page/Page.h"
+#include "core/rendering/RenderObject.h"
+#include "core/rendering/RenderView.h"
+#include "core/rendering/compositing/RenderLayerCompositor.h"
+#include "core/rendering/style/RenderStyle.h"
+#include "platform/PlatformScreen.h"
+
+namespace WebCore {
+
+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
+{
+ ASSERT(frame && frame->view() && style);
+ int viewportWidth = frame->view()->layoutSize(IncludeScrollbars).width();
+ return adjustForAbsoluteZoom(viewportWidth, style);
+}
+
+inline static int calculateViewportHeight(LocalFrame* frame, RenderStyle* style)
+{
+ ASSERT(frame && frame->view() && style);
+ int viewportHeight = frame->view()->layoutSize(IncludeScrollbars).height();
+ return adjustForAbsoluteZoom(viewportHeight, style);
+}
+
+inline static int calculateDeviceWidth(LocalFrame* frame)
+{
+ ASSERT(frame && frame->view() && frame->settings() && frame->host());
+ int deviceWidth = static_cast<int>(screenRect(frame->view()).width());
+ if (frame->settings()->reportScreenSizeInPhysicalPixelsQuirk())
+ deviceWidth = lroundf(deviceWidth * frame->host()->deviceScaleFactor());
+ return deviceWidth;
+}
+
+inline static int calculateDeviceHeight(LocalFrame* frame)
+{
+ ASSERT(frame && frame->view() && frame->settings() && frame->host());
+ int deviceHeight = static_cast<int>(screenRect(frame->view()).height());
+ if (frame->settings()->reportScreenSizeInPhysicalPixelsQuirk())
+ deviceHeight = lroundf(deviceHeight * frame->host()->deviceScaleFactor());
+ return deviceHeight;
+}
+
+inline static bool calculateStrictMode(LocalFrame* frame)
+{
+ ASSERT(frame && frame->document());
+ return !frame->document()->inQuirksMode();
+}
+
+inline static int calculateDevicePixelRatio(LocalFrame* frame)
+{
+ return frame->devicePixelRatio();
+}
+
+inline static int calculateColorBitsPerComponent(LocalFrame* frame)
+{
+ ASSERT(frame && frame->page() && frame->page()->mainFrame());
+ if (screenIsMonochrome(frame->page()->mainFrame()->view()))
+ return 0;
+ return screenDepthPerComponent(frame->view());
+}
+
+inline static int calculateMonochromeBitsPerComponent(LocalFrame* frame)
+{
+ ASSERT(frame && frame->page() && frame->page()->mainFrame());
+ if (screenIsMonochrome(frame->page()->mainFrame()->view()))
+ return screenDepthPerComponent(frame->view());
+ return 0;
+}
+
+inline static int calculateDefaultFontSize(RenderStyle* style)
+{
+ return style->fontDescription().specifiedSize();
+}
+
+inline static bool calculateScanMediaType(LocalFrame* frame)
+{
+ ASSERT(frame && frame->view());
+ // Scan only applies to 'tv' media.
+ return equalIgnoringCase(frame->view()->mediaType(), "tv");
+}
+
+inline static bool calculateScreenMediaType(LocalFrame* frame)
+{
+ ASSERT(frame && frame->view());
+ return equalIgnoringCase(frame->view()->mediaType(), "screen");
+}
+
+inline static bool calculatePrintMediaType(LocalFrame* frame)
+{
+ ASSERT(frame && frame->view());
+ return equalIgnoringCase(frame->view()->mediaType(), "print");
+}
+
+inline static bool calculateThreeDEnabled(LocalFrame* frame)
+{
+ ASSERT(frame && frame->contentRenderer() && frame->contentRenderer()->compositor());
+ bool threeDEnabled = false;
+ if (RenderView* view = frame->contentRenderer())
+ threeDEnabled = view->compositor()->canRender3DTransforms();
+ return threeDEnabled;
+}
+
+inline static MediaValues::PointerDeviceType calculateLeastCapablePrimaryPointerDeviceType(LocalFrame* frame)
+{
+ ASSERT(frame && frame->settings());
+ if (frame->settings()->deviceSupportsTouch())
+ return MediaValues::TouchPointer;
+
+ // FIXME: We should also try to determine if we know we have a mouse.
+ // When we do this, we'll also need to differentiate between known not to
+ // have mouse or touch screen (NoPointer) and unknown (UnknownPointer).
+ // We could also take into account other preferences like accessibility
+ // settings to decide which of the available pointers should be considered
+ // "primary".
+
+ return MediaValues::UnknownPointer;
+}
+
+PassRefPtr<MediaValues> MediaValues::create(LocalFrame* frame, RenderStyle* style, MediaValuesMode mode)
+{
+ ASSERT(frame && style);
+ if (mode == DynamicMode)
+ 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
+
+ int viewportWidth = calculateViewportWidth(frame, style);
+ int viewportHeight = calculateViewportWidth(frame, style);
+ int deviceWidth = calculateDeviceWidth(frame);
+ int deviceHeight = calculateDeviceHeight(frame);
+ bool strictMode = calculateStrictMode(frame);
+ float devicePixelRatio = calculateDevicePixelRatio(frame);
+ int colorBitsPerComponent = calculateColorBitsPerComponent(frame);
+ int monochromeBitsPerComponent = calculateMonochromeBitsPerComponent(frame);
+ int defaultFontSize = calculateDefaultFontSize(style);
+ bool scanMediaType = calculateScanMediaType(frame);
+ bool screenMediaType = calculateScreenMediaType(frame);
+ bool printMediaType = calculatePrintMediaType(frame);
+ bool threeDEnabled = calculateThreeDEnabled(frame);
+ MediaValues::PointerDeviceType pointer = calculateLeastCapablePrimaryPointerDeviceType(frame);
+
+ return adoptRef(new MediaValues(mode,
+ viewportWidth,
+ viewportHeight,
+ deviceWidth,
+ deviceHeight,
+ devicePixelRatio,
+ colorBitsPerComponent,
+ monochromeBitsPerComponent,
+ pointer,
+ defaultFontSize,
+ threeDEnabled,
+ scanMediaType,
+ screenMediaType,
+ printMediaType,
+ strictMode));
abarth-chromium 2014/03/27 21:01:55 Another option is to skip this constructor with th
+}
+
+PassRefPtr<MediaValues> MediaValues::create(Document* document, MediaValuesMode mode)
+{
+ Document* executingDocument = document->import() ? document->import()->master() : document;
+ ASSERT(executingDocument->frame());
+ ASSERT(executingDocument->renderer());
+ ASSERT(executingDocument->renderer()->style());
+ LocalFrame* frame = executingDocument->frame();
+ RenderStyle* style = executingDocument->renderer()->style();
+
+ return MediaValues::create(frame, style, mode);
+}
+
+PassRefPtr<MediaValues> MediaValues::copy() const
+{
+ ASSERT(m_mode == CachingMode && !m_style.get() && !m_frame);
+ return adoptRef(new MediaValues(m_mode,
+ m_viewportWidth,
+ m_viewportHeight,
+ m_deviceWidth,
+ m_deviceHeight,
+ m_devicePixelRatio,
+ m_colorBitsPerComponent,
+ m_monochromeBitsPerComponent,
+ m_pointer,
+ m_defaultFontSize,
+ m_threeDEnabled,
+ m_scanMediaType,
+ m_screenMediaType,
+ m_printMediaType,
+ m_strictMode));
abarth-chromium 2014/03/27 21:01:55 Again, you can do away with the large constructor
+}
+
+int MediaValues::viewportWidth() const
+{
+ if (m_mode == DynamicMode)
+ return calculateViewportWidth(m_frame, m_style.get());
+ return m_viewportWidth;
+}
+
+int MediaValues::viewportHeight() const
+{
+ if (m_mode == DynamicMode)
+ return calculateViewportHeight(m_frame, m_style.get());
+ return m_viewportHeight;
+}
+
+int MediaValues::deviceWidth() const
+{
+ if (m_mode == DynamicMode)
+ return calculateDeviceWidth(m_frame);
+ return m_deviceWidth;
+}
+
+int MediaValues::deviceHeight() const
+{
+ if (m_mode == DynamicMode)
+ return calculateDeviceHeight(m_frame);
+ return m_deviceHeight;
+}
+
+float MediaValues::devicePixelRatio() const
+{
+ if (m_mode == DynamicMode)
+ return calculateDevicePixelRatio(m_frame);
+ return m_devicePixelRatio;
+}
+
+int MediaValues::colorBitsPerComponent() const
+{
+ if (m_mode == DynamicMode)
+ return calculateColorBitsPerComponent(m_frame);
+ return m_colorBitsPerComponent;
+}
+
+int MediaValues::monochromeBitsPerComponent() const
+{
+ if (m_mode == DynamicMode)
+ return calculateMonochromeBitsPerComponent(m_frame);
+ return m_monochromeBitsPerComponent;
+}
+
+MediaValues::PointerDeviceType MediaValues::pointer() const
+{
+ if (m_mode == DynamicMode)
+ return calculateLeastCapablePrimaryPointerDeviceType(m_frame);
+ return m_pointer;
+}
+
+int MediaValues::defaultFontSize() const
+{
+ if (m_mode == DynamicMode)
+ return calculateDefaultFontSize(m_style.get());
+ return m_defaultFontSize;
+}
+
+bool MediaValues::threeDEnabled() const
+{
+ if (m_mode == DynamicMode)
+ return calculateThreeDEnabled(m_frame);
+ return m_threeDEnabled;
+}
+
+bool MediaValues::scanMediaType() const
+{
+ if (m_mode == DynamicMode)
+ return calculateScanMediaType(m_frame);
+ return m_scanMediaType;
+}
+
+bool MediaValues::screenMediaType() const
+{
+ if (m_mode == DynamicMode)
+ return calculateScreenMediaType(m_frame);
+ return m_screenMediaType;
+}
+
+bool MediaValues::printMediaType() const
+{
+ if (m_mode == DynamicMode)
+ return calculatePrintMediaType(m_frame);
+ return m_printMediaType;
+}
+
+bool MediaValues::strictMode() const
+{
+ if (m_mode == DynamicMode)
+ return calculateStrictMode(m_frame);
+ return m_strictMode;
+}
+
+Document* MediaValues::document() const
+{
+ if (!m_frame)
+ return 0;
+ return m_frame->document();
+}
+
+} // namespace

Powered by Google App Engine
This is Rietveld 408576698