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.primaryPointerType = calculatePrimaryPointerType(frame);
58 m_data.availablePointerTypes = calculateAvailablePointerTypes(frame);
59 m_data.primaryHoverType = calculatePrimaryHoverType(frame);
60 m_data.availableHoverTypes = calculateAvailableHoverTypes(frame);
61 m_data.defaultFontSize = calculateDefaultFontSize(frame);
62 m_data.threeDEnabled = calculateThreeDEnabled(frame);
63 m_data.strictMode = calculateStrictMode(frame);
64 const String mediaType = calculateMediaType(frame);
65 if (!mediaType.isEmpty())
66 m_data.mediaType = mediaType.isolatedCopy();
69 MediaValuesCached::MediaValuesCached(const MediaValuesCachedData& data)
74 PassRefPtr<MediaValues> MediaValuesCached::copy() const
76 return adoptRef(new MediaValuesCached(m_data));
79 bool MediaValuesCached::computeLength(double value, CSSPrimitiveValue::UnitType type, int& result) const
81 return MediaValues::computeLength(value, type, m_data.defaultFontSize, m_data.viewportWidth, m_data.viewportHeight, result);
84 bool MediaValuesCached::computeLength(double value, CSSPrimitiveValue::UnitType type, double& result) const
86 return MediaValues::computeLength(value, type, m_data.defaultFontSize, m_data.viewportWidth, m_data.viewportHeight, result);
89 bool MediaValuesCached::isSafeToSendToAnotherThread() const
94 int MediaValuesCached::viewportWidth() const
96 return m_data.viewportWidth;
99 int MediaValuesCached::viewportHeight() const
101 return m_data.viewportHeight;
104 int MediaValuesCached::deviceWidth() const
106 return m_data.deviceWidth;
109 int MediaValuesCached::deviceHeight() const
111 return m_data.deviceHeight;
114 float MediaValuesCached::devicePixelRatio() const
116 return m_data.devicePixelRatio;
119 int MediaValuesCached::colorBitsPerComponent() const
121 return m_data.colorBitsPerComponent;
124 int MediaValuesCached::monochromeBitsPerComponent() const
126 return m_data.monochromeBitsPerComponent;
129 PointerType MediaValuesCached::primaryPointerType() const
131 return m_data.primaryPointerType;
134 int MediaValuesCached::availablePointerTypes() const
136 return m_data.availablePointerTypes;
139 HoverType MediaValuesCached::primaryHoverType() const
141 return m_data.primaryHoverType;
144 int MediaValuesCached::availableHoverTypes() const
146 return m_data.availableHoverTypes;
149 bool MediaValuesCached::threeDEnabled() const
151 return m_data.threeDEnabled;
154 bool MediaValuesCached::strictMode() const
156 return m_data.strictMode;
158 const String MediaValuesCached::mediaType() const
160 return m_data.mediaType;
163 Document* MediaValuesCached::document() const
168 bool MediaValuesCached::hasValues() const