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/MediaValuesCached.h"
7
8#include "core/css/CSSPrimitiveValue.h"
9#include "core/dom/Document.h"
10#include "core/frame/LocalFrame.h"
11#include "core/rendering/RenderObject.h"
12
13namespace blink {
14
15PassRefPtr<MediaValues> MediaValuesCached::create()
16{
17    return adoptRef(new MediaValuesCached());
18}
19
20PassRefPtr<MediaValues> MediaValuesCached::create(MediaValuesCachedData& data)
21{
22    return adoptRef(new MediaValuesCached(data));
23}
24
25PassRefPtr<MediaValues> MediaValuesCached::create(Document& document)
26{
27    return MediaValuesCached::create(frameFrom(document));
28}
29
30PassRefPtr<MediaValues> MediaValuesCached::create(LocalFrame* frame)
31{
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));
38}
39
40MediaValuesCached::MediaValuesCached()
41{
42}
43
44MediaValuesCached::MediaValuesCached(LocalFrame* frame)
45{
46    ASSERT(isMainThread());
47    ASSERT(frame);
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();
67}
68
69MediaValuesCached::MediaValuesCached(const MediaValuesCachedData& data)
70    : m_data(data)
71{
72}
73
74PassRefPtr<MediaValues> MediaValuesCached::copy() const
75{
76    return adoptRef(new MediaValuesCached(m_data));
77}
78
79bool MediaValuesCached::computeLength(double value, CSSPrimitiveValue::UnitType type, int& result) const
80{
81    return MediaValues::computeLength(value, type, m_data.defaultFontSize, m_data.viewportWidth, m_data.viewportHeight, result);
82}
83
84bool MediaValuesCached::computeLength(double value, CSSPrimitiveValue::UnitType type, double& result) const
85{
86    return MediaValues::computeLength(value, type, m_data.defaultFontSize, m_data.viewportWidth, m_data.viewportHeight, result);
87}
88
89bool MediaValuesCached::isSafeToSendToAnotherThread() const
90{
91    return hasOneRef();
92}
93
94int MediaValuesCached::viewportWidth() const
95{
96    return m_data.viewportWidth;
97}
98
99int MediaValuesCached::viewportHeight() const
100{
101    return m_data.viewportHeight;
102}
103
104int MediaValuesCached::deviceWidth() const
105{
106    return m_data.deviceWidth;
107}
108
109int MediaValuesCached::deviceHeight() const
110{
111    return m_data.deviceHeight;
112}
113
114float MediaValuesCached::devicePixelRatio() const
115{
116    return m_data.devicePixelRatio;
117}
118
119int MediaValuesCached::colorBitsPerComponent() const
120{
121    return m_data.colorBitsPerComponent;
122}
123
124int MediaValuesCached::monochromeBitsPerComponent() const
125{
126    return m_data.monochromeBitsPerComponent;
127}
128
129PointerType MediaValuesCached::primaryPointerType() const
130{
131    return m_data.primaryPointerType;
132}
133
134int MediaValuesCached::availablePointerTypes() const
135{
136    return m_data.availablePointerTypes;
137}
138
139HoverType MediaValuesCached::primaryHoverType() const
140{
141    return m_data.primaryHoverType;
142}
143
144int MediaValuesCached::availableHoverTypes() const
145{
146    return m_data.availableHoverTypes;
147}
148
149bool MediaValuesCached::threeDEnabled() const
150{
151    return m_data.threeDEnabled;
152}
153
154bool MediaValuesCached::strictMode() const
155{
156    return m_data.strictMode;
157}
158const String MediaValuesCached::mediaType() const
159{
160    return m_data.mediaType;
161}
162
163Document* MediaValuesCached::document() const
164{
165    return 0;
166}
167
168bool MediaValuesCached::hasValues() const
169{
170    return true;
171}
172
173} // namespace
174