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.
6 #include "core/css/MediaValuesCached.h"
8 #include "core/css/CSSPrimitiveValue.h"
9 #include "core/dom/Document.h"
10 #include "core/frame/LocalFrame.h"
11 #include "core/rendering/RenderObject.h"
15 PassRefPtr<MediaValues> MediaValuesCached::create()
17 return adoptRef(new MediaValuesCached());
20 PassRefPtr<MediaValues> MediaValuesCached::create(MediaValuesCachedData& data)
22 return adoptRef(new MediaValuesCached(data));
25 PassRefPtr<MediaValues> MediaValuesCached::create(Document& document)
27 return MediaValuesCached::create(frameFrom(document));
30 PassRefPtr<MediaValues> MediaValuesCached::create(LocalFrame* frame)
32 // FIXME - Added an assert here so we can better understand when a frame is present without its view().
33 ASSERT(!frame || frame->view());
34 // FIXME - Because of crbug.com/371084, document()->renderView() may be null here.
35 if (!frame || !frame->view() || !frame->document() || !frame->document()->renderView())
36 return adoptRef(new MediaValuesCached());
37 return adoptRef(new MediaValuesCached(frame));
40 MediaValuesCached::MediaValuesCached()
44 MediaValuesCached::MediaValuesCached(LocalFrame* frame)
46 ASSERT(isMainThread());
48 // In case that frame is missing (e.g. for images that their document does not have a frame)
49 // We simply leave the MediaValues object with the default MediaValuesCachedData values.
50 m_data.viewportWidth = calculateViewportWidth(frame);
51 m_data.viewportHeight = calculateViewportHeight(frame);
52 m_data.deviceWidth = calculateDeviceWidth(frame);
53 m_data.deviceHeight = calculateDeviceHeight(frame);
54 m_data.devicePixelRatio = calculateDevicePixelRatio(frame);
55 m_data.colorBitsPerComponent = calculateColorBitsPerComponent(frame);
56 m_data.monochromeBitsPerComponent = calculateMonochromeBitsPerComponent(frame);
57 m_data.pointer = calculateLeastCapablePrimaryPointerDeviceType(frame);
58 m_data.defaultFontSize = calculateDefaultFontSize(frame);
59 m_data.threeDEnabled = calculateThreeDEnabled(frame);
60 m_data.strictMode = calculateStrictMode(frame);
61 const String mediaType = calculateMediaType(frame);
62 if (!mediaType.isEmpty())
63 m_data.mediaType = mediaType.isolatedCopy();
66 MediaValuesCached::MediaValuesCached(const MediaValuesCachedData& data)
71 PassRefPtr<MediaValues> MediaValuesCached::copy() const
73 return adoptRef(new MediaValuesCached(m_data));
76 bool MediaValuesCached::computeLength(double value, CSSPrimitiveValue::UnitType type, int& result) const
78 return MediaValues::computeLength(value, type, m_data.defaultFontSize, m_data.viewportWidth, m_data.viewportHeight, result);
81 bool MediaValuesCached::computeLength(double value, CSSPrimitiveValue::UnitType type, double& result) const
83 return MediaValues::computeLength(value, type, m_data.defaultFontSize, m_data.viewportWidth, m_data.viewportHeight, result);
86 bool MediaValuesCached::isSafeToSendToAnotherThread() const
91 int MediaValuesCached::viewportWidth() const
93 return m_data.viewportWidth;
96 int MediaValuesCached::viewportHeight() const
98 return m_data.viewportHeight;
101 int MediaValuesCached::deviceWidth() const
103 return m_data.deviceWidth;
106 int MediaValuesCached::deviceHeight() const
108 return m_data.deviceHeight;
111 float MediaValuesCached::devicePixelRatio() const
113 return m_data.devicePixelRatio;
116 int MediaValuesCached::colorBitsPerComponent() const
118 return m_data.colorBitsPerComponent;
121 int MediaValuesCached::monochromeBitsPerComponent() const
123 return m_data.monochromeBitsPerComponent;
126 MediaValues::PointerDeviceType MediaValuesCached::pointer() const
128 return m_data.pointer;
131 bool MediaValuesCached::threeDEnabled() const
133 return m_data.threeDEnabled;
136 bool MediaValuesCached::strictMode() const
138 return m_data.strictMode;
140 const String MediaValuesCached::mediaType() const
142 return m_data.mediaType;
145 Document* MediaValuesCached::document() const
150 bool MediaValuesCached::hasValues() const