initial import
[vuplus_webkit] / Source / WebCore / css / MediaQueryMatcher.cpp
1 /*
2  * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
3  *
4  *  This library is free software; you can redistribute it and/or
5  *  modify it under the terms of the GNU Library General Public
6  *  License as published by the Free Software Foundation; either
7  *  version 2 of the License, or (at your option) any later version.
8  *
9  *  This library is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *  Library General Public License for more details.
13  *
14  *  You should have received a copy of the GNU Library General Public License
15  *  along with this library; see the file COPYING.LIB.  If not, write to
16  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  *  Boston, MA 02110-1301, USA.
18  */
19
20 #include "config.h"
21 #include "MediaQueryMatcher.h"
22
23 #include "CSSStyleSelector.h"
24 #include "Document.h"
25 #include "Element.h"
26 #include "Frame.h"
27 #include "FrameView.h"
28 #include "MediaList.h"
29 #include "MediaQueryEvaluator.h"
30 #include "MediaQueryList.h"
31 #include "MediaQueryListListener.h"
32
33 namespace WebCore {
34
35 MediaQueryMatcher::Listener::Listener(PassRefPtr<MediaQueryListListener> listener, PassRefPtr<MediaQueryList> query)
36     : m_listener(listener)
37     , m_query(query)
38 {
39 }
40
41 MediaQueryMatcher::Listener::~Listener()
42 {
43 }
44
45 void MediaQueryMatcher::Listener::evaluate(ScriptState* state, MediaQueryEvaluator* evaluator)
46 {
47     bool notify;
48     m_query->evaluate(evaluator, notify);
49     if (notify)
50         m_listener->queryChanged(state, m_query.get());
51 }
52
53 MediaQueryMatcher::MediaQueryMatcher(Document* document)
54     : m_document(document)
55     , m_evaluationRound(1)
56 {
57     ASSERT(m_document);
58 }
59
60 MediaQueryMatcher::~MediaQueryMatcher()
61 {
62 }
63
64 void MediaQueryMatcher::documentDestroyed()
65 {
66     m_listeners.clear();
67     m_document = 0;
68 }
69
70 String MediaQueryMatcher::mediaType() const
71 {
72     if (!m_document || !m_document->frame() || !m_document->frame()->view())
73         return String();
74
75     return m_document->frame()->view()->mediaType();
76 }
77
78 PassOwnPtr<MediaQueryEvaluator> MediaQueryMatcher::prepareEvaluator() const
79 {
80     if (!m_document || !m_document->frame())
81         return nullptr;
82
83     Element* documentElement = m_document->documentElement();
84     if (!documentElement)
85         return nullptr;
86
87     CSSStyleSelector* styleSelector = m_document->styleSelector();
88     if (!styleSelector)
89         return nullptr;
90
91     RefPtr<RenderStyle> rootStyle = styleSelector->styleForElement(documentElement, 0 /*defaultParent*/, false /*allowSharing*/, true /*resolveForRootDefault*/);
92
93     return adoptPtr(new MediaQueryEvaluator(mediaType(), m_document->frame(), rootStyle.get()));
94 }
95
96 bool MediaQueryMatcher::evaluate(MediaList* media)
97 {
98     if (!media)
99         return false;
100
101     OwnPtr<MediaQueryEvaluator> evaluator(prepareEvaluator());
102     return evaluator && evaluator->eval(media);
103 }
104
105 PassRefPtr<MediaQueryList> MediaQueryMatcher::matchMedia(const String& query)
106 {
107     if (!m_document)
108         return 0;
109
110     RefPtr<MediaList> media = MediaList::create(query, false);
111     return MediaQueryList::create(this, media, evaluate(media.get()));
112 }
113
114 void MediaQueryMatcher::addListener(PassRefPtr<MediaQueryListListener> listener, PassRefPtr<MediaQueryList> query)
115 {
116     if (!m_document)
117         return;
118
119     for (size_t i = 0; i < m_listeners.size(); ++i) {
120         if (*m_listeners[i]->listener() == *listener && m_listeners[i]->query() == query)
121             return;
122     }
123
124     m_listeners.append(adoptPtr(new Listener(listener, query)));
125 }
126
127 void MediaQueryMatcher::removeListener(MediaQueryListListener* listener, MediaQueryList* query)
128 {
129     if (!m_document)
130         return;
131
132     for (size_t i = 0; i < m_listeners.size(); ++i) {
133         if (*m_listeners[i]->listener() == *listener && m_listeners[i]->query() == query) {
134             m_listeners.remove(i);
135             return;
136         }
137     }
138 }
139
140 void MediaQueryMatcher::styleSelectorChanged()
141 {
142     ASSERT(m_document);
143
144     ScriptState* scriptState = mainWorldScriptState(m_document->frame());
145     if (!scriptState)
146         return;
147
148     ++m_evaluationRound;
149     OwnPtr<MediaQueryEvaluator> evaluator = prepareEvaluator();
150     if (!evaluator)
151         return;
152
153     for (size_t i = 0; i < m_listeners.size(); ++i)
154         m_listeners[i]->evaluate(scriptState, evaluator.get());
155 }
156
157 }