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 "MediaQueryList.h"
22
23#include "MediaList.h"
24#include "MediaQueryEvaluator.h"
25#include "MediaQueryListListener.h"
26#include "MediaQueryMatcher.h"
27
28namespace WebCore {
29
30inline MediaQueryList::MediaQueryList(MediaQueryMatcher& matcher, Ref<MediaQuerySet>&& media, bool matches)
31 : m_matcher(matcher)
32 , m_media(WTFMove(media))
33 , m_evaluationRound(m_matcher->evaluationRound())
34 , m_changeRound(m_evaluationRound - 1) // Any value that is not the same as m_evaluationRound would do.
35 , m_matches(matches)
36{
37}
38
39Ref<MediaQueryList> MediaQueryList::create(MediaQueryMatcher& matcher, Ref<MediaQuerySet>&& media, bool matches)
40{
41 return adoptRef(*new MediaQueryList(matcher, WTFMove(media), matches));
42}
43
44MediaQueryList::~MediaQueryList() = default;
45
46String MediaQueryList::media() const
47{
48 return m_media->mediaText();
49}
50
51void MediaQueryList::addListener(RefPtr<MediaQueryListListener>&& listener)
52{
53 if (!listener)
54 return;
55
56 m_matcher->addListener(listener.releaseNonNull(), *this);
57}
58
59void MediaQueryList::removeListener(RefPtr<MediaQueryListListener>&& listener)
60{
61 if (!listener)
62 return;
63
64 m_matcher->removeListener(*listener, *this);
65}
66
67void MediaQueryList::evaluate(MediaQueryEvaluator& evaluator, bool& notificationNeeded)
68{
69 if (m_evaluationRound != m_matcher->evaluationRound())
70 setMatches(evaluator.evaluate(m_media.get()));
71 notificationNeeded = m_changeRound == m_matcher->evaluationRound();
72}
73
74void MediaQueryList::setMatches(bool newValue)
75{
76 m_evaluationRound = m_matcher->evaluationRound();
77
78 if (newValue == m_matches)
79 return;
80
81 m_matches = newValue;
82 m_changeRound = m_evaluationRound;
83}
84
85bool MediaQueryList::matches()
86{
87 if (m_evaluationRound != m_matcher->evaluationRound())
88 setMatches(m_matcher->evaluate(m_media.get()));
89 return m_matches;
90}
91
92}
93