• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "core/css/MediaQueryMatcher.h"
22 
23 #include "core/css/MediaList.h"
24 #include "core/css/MediaQueryEvaluator.h"
25 #include "core/css/MediaQueryList.h"
26 #include "core/css/MediaQueryListListener.h"
27 #include "core/css/resolver/StyleResolver.h"
28 #include "core/dom/Document.h"
29 #include "core/frame/Frame.h"
30 #include "core/frame/FrameView.h"
31 
32 namespace WebCore {
33 
Listener(PassRefPtr<MediaQueryListListener> listener,PassRefPtr<MediaQueryList> query)34 MediaQueryMatcher::Listener::Listener(PassRefPtr<MediaQueryListListener> listener, PassRefPtr<MediaQueryList> query)
35     : m_listener(listener)
36     , m_query(query)
37 {
38 }
39 
~Listener()40 MediaQueryMatcher::Listener::~Listener()
41 {
42 }
43 
evaluate(ScriptState * state,MediaQueryEvaluator * evaluator)44 void MediaQueryMatcher::Listener::evaluate(ScriptState* state, MediaQueryEvaluator* evaluator)
45 {
46     bool notify;
47     m_query->evaluate(evaluator, notify);
48     if (notify)
49         m_listener->queryChanged(state, m_query.get());
50 }
51 
MediaQueryMatcher(Document * document)52 MediaQueryMatcher::MediaQueryMatcher(Document* document)
53     : m_document(document)
54     , m_evaluationRound(1)
55 {
56     ASSERT(m_document);
57 }
58 
~MediaQueryMatcher()59 MediaQueryMatcher::~MediaQueryMatcher()
60 {
61 }
62 
documentDestroyed()63 void MediaQueryMatcher::documentDestroyed()
64 {
65     m_listeners.clear();
66     m_document = 0;
67 }
68 
mediaType() const69 AtomicString MediaQueryMatcher::mediaType() const
70 {
71     if (!m_document || !m_document->frame() || !m_document->frame()->view())
72         return nullAtom;
73 
74     return m_document->frame()->view()->mediaType();
75 }
76 
prepareEvaluator() const77 PassOwnPtr<MediaQueryEvaluator> MediaQueryMatcher::prepareEvaluator() const
78 {
79     if (!m_document || !m_document->frame())
80         return nullptr;
81 
82     Element* documentElement = m_document->documentElement();
83     if (!documentElement)
84         return nullptr;
85 
86     StyleResolver& styleResolver = m_document->ensureStyleResolver();
87     RefPtr<RenderStyle> rootStyle = styleResolver.styleForElement(documentElement, 0 /*defaultParent*/, DisallowStyleSharing, MatchOnlyUserAgentRules);
88 
89     return adoptPtr(new MediaQueryEvaluator(mediaType(), m_document->frame(), rootStyle.get()));
90 }
91 
evaluate(const MediaQuerySet * media)92 bool MediaQueryMatcher::evaluate(const MediaQuerySet* media)
93 {
94     if (!media)
95         return false;
96 
97     OwnPtr<MediaQueryEvaluator> evaluator(prepareEvaluator());
98     return evaluator && evaluator->eval(media);
99 }
100 
matchMedia(const String & query)101 PassRefPtr<MediaQueryList> MediaQueryMatcher::matchMedia(const String& query)
102 {
103     if (!m_document)
104         return 0;
105 
106     RefPtr<MediaQuerySet> media = MediaQuerySet::create(query);
107     // Add warning message to inspector whenever dpi/dpcm values are used for "screen" media.
108     reportMediaQueryWarningIfNeeded(m_document, media.get());
109     return MediaQueryList::create(this, media, evaluate(media.get()));
110 }
111 
addListener(PassRefPtr<MediaQueryListListener> listener,PassRefPtr<MediaQueryList> query)112 void MediaQueryMatcher::addListener(PassRefPtr<MediaQueryListListener> listener, PassRefPtr<MediaQueryList> query)
113 {
114     if (!m_document)
115         return;
116 
117     for (size_t i = 0; i < m_listeners.size(); ++i) {
118         if (*m_listeners[i]->listener() == *listener && m_listeners[i]->query() == query)
119             return;
120     }
121 
122     m_listeners.append(adoptPtr(new Listener(listener, query)));
123 }
124 
removeListener(MediaQueryListListener * listener,MediaQueryList * query)125 void MediaQueryMatcher::removeListener(MediaQueryListListener* listener, MediaQueryList* query)
126 {
127     if (!m_document)
128         return;
129 
130     for (size_t i = 0; i < m_listeners.size(); ++i) {
131         if (*m_listeners[i]->listener() == *listener && m_listeners[i]->query() == query) {
132             m_listeners.remove(i);
133             return;
134         }
135     }
136 }
137 
styleResolverChanged()138 void MediaQueryMatcher::styleResolverChanged()
139 {
140     if (!m_document)
141         return;
142 
143     ScriptState* scriptState = m_document->frame() ? mainWorldScriptState(m_document->frame()) : 0;
144     if (!scriptState)
145         return;
146 
147     ++m_evaluationRound;
148     OwnPtr<MediaQueryEvaluator> evaluator = prepareEvaluator();
149     if (!evaluator)
150         return;
151 
152     for (size_t i = 0; i < m_listeners.size(); ++i)
153         m_listeners[i]->evaluate(scriptState, evaluator.get());
154 }
155 
156 }
157