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/html/imports/HTMLImportsController.h"
12 #include "core/rendering/RenderObject.h"
16 PassRefPtr<MediaValues> MediaValuesCached::create()
18 return adoptRef(new MediaValuesCached());
21 PassRefPtr<MediaValues> MediaValuesCached::create(MediaValuesCachedData& data)
23 return adoptRef(new MediaValuesCached(data));
26 PassRefPtr<MediaValues> MediaValuesCached::create(Document& document)
28 Document* executingDocument = document.importsController() ? document.importsController()->master() : &document;
29 ASSERT(executingDocument);
30 return MediaValuesCached::create(executingDocument->frame());
33 PassRefPtr<MediaValues> MediaValuesCached::create(LocalFrame* frame)
35 // FIXME - Added an assert here so we can better understand when a frame is present without its view().
36 ASSERT(!frame || frame->view());
37 // FIXME - Because of crbug.com/371084, document()->renderView() may be null here.
38 if (!frame || !frame->view() || !frame->document() || !frame->document()->renderView())
39 return adoptRef(new MediaValuesCached());
40 return adoptRef(new MediaValuesCached(frame));
43 MediaValuesCached::MediaValuesCached()
47 MediaValuesCached::MediaValuesCached(LocalFrame* frame)
49 ASSERT(isMainThread());
51 // In case that frame is missing (e.g. for images that their document does not have a frame)
52 // We simply leave the MediaValues object with the default MediaValuesCachedData values.
53 m_data.viewportWidth = calculateViewportWidth(frame);
54 m_data.viewportHeight = calculateViewportHeight(frame);
55 m_data.deviceWidth = calculateDeviceWidth(frame);
56 m_data.deviceHeight = calculateDeviceHeight(frame);
57 m_data.devicePixelRatio = calculateDevicePixelRatio(frame);
58 m_data.colorBitsPerComponent = calculateColorBitsPerComponent(frame);
59 m_data.monochromeBitsPerComponent = calculateMonochromeBitsPerComponent(frame);
60 m_data.pointer = calculateLeastCapablePrimaryPointerDeviceType(frame);
61 m_data.defaultFontSize = calculateDefaultFontSize(frame);
62 m_data.threeDEnabled = calculateThreeDEnabled(frame);
63 m_data.scanMediaType = calculateScanMediaType(frame);
64 m_data.screenMediaType = calculateScreenMediaType(frame);
65 m_data.printMediaType = calculatePrintMediaType(frame);
66 m_data.strictMode = calculateStrictMode(frame);
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::UnitTypes 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::UnitTypes 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 MediaValues::PointerDeviceType MediaValuesCached::pointer() const
131 return m_data.pointer;
134 bool MediaValuesCached::threeDEnabled() const
136 return m_data.threeDEnabled;
139 bool MediaValuesCached::scanMediaType() const
141 return m_data.scanMediaType;
144 bool MediaValuesCached::screenMediaType() const
146 return m_data.screenMediaType;
149 bool MediaValuesCached::printMediaType() const
151 return m_data.printMediaType;
154 bool MediaValuesCached::strictMode() const
156 return m_data.strictMode;
159 Document* MediaValuesCached::document() const
164 bool MediaValuesCached::hasValues() const