Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / css / MediaValuesCached.cpp
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
13 namespace blink {
14
15 PassRefPtr<MediaValues> MediaValuesCached::create()
16 {
17     return adoptRef(new MediaValuesCached());
18 }
19
20 PassRefPtr<MediaValues> MediaValuesCached::create(MediaValuesCachedData& data)
21 {
22     return adoptRef(new MediaValuesCached(data));
23 }
24
25 PassRefPtr<MediaValues> MediaValuesCached::create(Document& document)
26 {
27     return MediaValuesCached::create(frameFrom(document));
28 }
29
30 PassRefPtr<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
40 MediaValuesCached::MediaValuesCached()
41 {
42 }
43
44 MediaValuesCached::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.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();
64 }
65
66 MediaValuesCached::MediaValuesCached(const MediaValuesCachedData& data)
67     : m_data(data)
68 {
69 }
70
71 PassRefPtr<MediaValues> MediaValuesCached::copy() const
72 {
73     return adoptRef(new MediaValuesCached(m_data));
74 }
75
76 bool MediaValuesCached::computeLength(double value, CSSPrimitiveValue::UnitType type, int& result) const
77 {
78     return MediaValues::computeLength(value, type, m_data.defaultFontSize, m_data.viewportWidth, m_data.viewportHeight, result);
79 }
80
81 bool MediaValuesCached::computeLength(double value, CSSPrimitiveValue::UnitType type, double& result) const
82 {
83     return MediaValues::computeLength(value, type, m_data.defaultFontSize, m_data.viewportWidth, m_data.viewportHeight, result);
84 }
85
86 bool MediaValuesCached::isSafeToSendToAnotherThread() const
87 {
88     return hasOneRef();
89 }
90
91 int MediaValuesCached::viewportWidth() const
92 {
93     return m_data.viewportWidth;
94 }
95
96 int MediaValuesCached::viewportHeight() const
97 {
98     return m_data.viewportHeight;
99 }
100
101 int MediaValuesCached::deviceWidth() const
102 {
103     return m_data.deviceWidth;
104 }
105
106 int MediaValuesCached::deviceHeight() const
107 {
108     return m_data.deviceHeight;
109 }
110
111 float MediaValuesCached::devicePixelRatio() const
112 {
113     return m_data.devicePixelRatio;
114 }
115
116 int MediaValuesCached::colorBitsPerComponent() const
117 {
118     return m_data.colorBitsPerComponent;
119 }
120
121 int MediaValuesCached::monochromeBitsPerComponent() const
122 {
123     return m_data.monochromeBitsPerComponent;
124 }
125
126 MediaValues::PointerDeviceType MediaValuesCached::pointer() const
127 {
128     return m_data.pointer;
129 }
130
131 bool MediaValuesCached::threeDEnabled() const
132 {
133     return m_data.threeDEnabled;
134 }
135
136 bool MediaValuesCached::strictMode() const
137 {
138     return m_data.strictMode;
139 }
140 const String MediaValuesCached::mediaType() const
141 {
142     return m_data.mediaType;
143 }
144
145 Document* MediaValuesCached::document() const
146 {
147     return 0;
148 }
149
150 bool MediaValuesCached::hasValues() const
151 {
152     return true;
153 }
154
155 } // namespace