| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) | 2 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) |
| 3 * | 3 * |
| 4 * This library is free software; you can redistribute it and/or | 4 * This library is free software; you can redistribute it and/or |
| 5 * modify it under the terms of the GNU Library General Public | 5 * modify it under the terms of the GNU Library General Public |
| 6 * License as published by the Free Software Foundation; either | 6 * License as published by the Free Software Foundation; either |
| 7 * version 2 of the License, or (at your option) any later version. | 7 * version 2 of the License, or (at your option) any later version. |
| 8 * | 8 * |
| 9 * This library is distributed in the hope that it will be useful, | 9 * This library is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 * Library General Public License for more details. | 12 * Library General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU Library General Public License | 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 | 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, | 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 * Boston, MA 02110-1301, USA. | 17 * Boston, MA 02110-1301, USA. |
| 18 */ | 18 */ |
| 19 | 19 |
| 20 #include "config.h" | 20 #include "config.h" |
| 21 #include "core/css/MediaQueryList.h" | 21 #include "core/css/MediaQueryList.h" |
| 22 | 22 |
| 23 #include "core/css/MediaList.h" | 23 #include "core/css/MediaList.h" |
| 24 #include "core/css/MediaQueryEvaluator.h" | 24 #include "core/css/MediaQueryEvaluator.h" |
| 25 #include "core/css/MediaQueryListListener.h" | 25 #include "core/css/MediaQueryListListener.h" |
| 26 #include "core/css/MediaQueryMatcher.h" | 26 #include "core/css/MediaQueryMatcher.h" |
| 27 | 27 |
| 28 namespace WebCore { | 28 namespace WebCore { |
| 29 | 29 |
| 30 PassRefPtrWillBeRawPtr<MediaQueryList> MediaQueryList::create(PassRefPtrWillBeRa
wPtr<MediaQueryMatcher> vector, PassRefPtrWillBeRawPtr<MediaQuerySet> media, boo
l matches) | 30 PassRefPtrWillBeRawPtr<MediaQueryList> MediaQueryList::create(PassRefPtrWillBeRa
wPtr<MediaQueryMatcher> matcher, PassRefPtrWillBeRawPtr<MediaQuerySet> media) |
| 31 { | 31 { |
| 32 return adoptRefWillBeNoop(new MediaQueryList(vector, media, matches)); | 32 return adoptRefWillBeNoop(new MediaQueryList(matcher, media)); |
| 33 } | 33 } |
| 34 | 34 |
| 35 MediaQueryList::MediaQueryList(PassRefPtrWillBeRawPtr<MediaQueryMatcher> vector,
PassRefPtrWillBeRawPtr<MediaQuerySet> media, bool matches) | 35 MediaQueryList::MediaQueryList(PassRefPtrWillBeRawPtr<MediaQueryMatcher> matcher
, PassRefPtrWillBeRawPtr<MediaQuerySet> media) |
| 36 : m_matcher(vector) | 36 : m_matcher(matcher) |
| 37 , m_media(media) | 37 , m_media(media) |
| 38 , m_evaluationRound(m_matcher->evaluationRound()) | 38 , m_matchesDirty(true) |
| 39 , m_changeRound(m_evaluationRound - 1) // m_evaluationRound and m_changeRoun
d initial values must be different. | 39 , m_matches(false) |
| 40 , m_matches(matches) | |
| 41 { | 40 { |
| 41 m_matcher->addMediaQueryList(this); |
| 42 updateMatches(); |
| 42 } | 43 } |
| 43 | 44 |
| 44 DEFINE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(MediaQueryList) | 45 MediaQueryList::~MediaQueryList() |
| 46 { |
| 47 #if !ENABLE(OILPAN) |
| 48 m_matcher->removeMediaQueryList(this); |
| 49 #endif |
| 50 } |
| 45 | 51 |
| 46 String MediaQueryList::media() const | 52 String MediaQueryList::media() const |
| 47 { | 53 { |
| 48 return m_media->mediaText(); | 54 return m_media->mediaText(); |
| 49 } | 55 } |
| 50 | 56 |
| 51 void MediaQueryList::addListener(PassRefPtrWillBeRawPtr<MediaQueryListListener>
listener) | 57 void MediaQueryList::addListener(PassRefPtrWillBeRawPtr<MediaQueryListListener>
listener) |
| 52 { | 58 { |
| 53 if (!listener) | 59 if (!listener) |
| 54 return; | 60 return; |
| 55 | 61 |
| 56 m_matcher->addListener(listener, this); | 62 listener->setMediaQueryList(this); |
| 63 m_listeners.add(listener); |
| 57 } | 64 } |
| 58 | 65 |
| 59 void MediaQueryList::removeListener(PassRefPtrWillBeRawPtr<MediaQueryListListene
r> listener) | 66 void MediaQueryList::removeListener(PassRefPtrWillBeRawPtr<MediaQueryListListene
r> listener) |
| 60 { | 67 { |
| 61 if (!listener) | 68 if (!listener) |
| 62 return; | 69 return; |
| 63 | 70 |
| 64 m_matcher->removeListener(listener.get(), this); | 71 RefPtrWillBeRawPtr<MediaQueryList> protect(this); |
| 72 listener->clearMediaQueryList(); |
| 73 |
| 74 for (ListenerList::iterator it = m_listeners.begin(), end = m_listeners.end(
); it != end; ++it) { |
| 75 // We can't just use m_listeners.remove() here, because we get a new wra
pper for the |
| 76 // listener callback every time. We have to use MediaQueryListListener::
operator==. |
| 77 if (**it == *listener.get()) { |
| 78 m_listeners.remove(it); |
| 79 break; |
| 80 } |
| 81 } |
| 65 } | 82 } |
| 66 | 83 |
| 67 bool MediaQueryList::evaluate(MediaQueryEvaluator* evaluator) | 84 void MediaQueryList::documentDetached() |
| 68 { | 85 { |
| 69 if (m_evaluationRound != m_matcher->evaluationRound() && evaluator) | 86 m_listeners.clear(); |
| 70 setMatches(evaluator->eval(m_media.get())); | |
| 71 return m_changeRound == m_matcher->evaluationRound(); | |
| 72 } | 87 } |
| 73 | 88 |
| 74 void MediaQueryList::setMatches(bool newValue) | 89 void MediaQueryList::mediaFeaturesChanged(WillBeHeapVector<RefPtrWillBeMember<Me
diaQueryListListener> >* toNotify) |
| 75 { | 90 { |
| 76 m_evaluationRound = m_matcher->evaluationRound(); | 91 m_matchesDirty = true; |
| 92 if (!updateMatches()) |
| 93 return; |
| 94 for (ListenerList::const_iterator it = m_listeners.begin(), end = m_listener
s.end(); it != end; ++it) { |
| 95 toNotify->append(*it); |
| 96 } |
| 97 } |
| 77 | 98 |
| 78 if (newValue == m_matches) | 99 bool MediaQueryList::updateMatches() |
| 79 return; | 100 { |
| 80 | 101 m_matchesDirty = false; |
| 81 m_matches = newValue; | 102 if (m_matches != m_matcher->evaluate(m_media.get())) { |
| 82 m_changeRound = m_evaluationRound; | 103 m_matches = !m_matches; |
| 104 return true; |
| 105 } |
| 106 return false; |
| 83 } | 107 } |
| 84 | 108 |
| 85 bool MediaQueryList::matches() | 109 bool MediaQueryList::matches() |
| 86 { | 110 { |
| 87 if (m_evaluationRound != m_matcher->evaluationRound()) | 111 updateMatches(); |
| 88 setMatches(m_matcher->evaluate(m_media.get())); | |
| 89 return m_matches; | 112 return m_matches; |
| 90 } | 113 } |
| 91 | 114 |
| 92 void MediaQueryList::trace(Visitor* visitor) | 115 void MediaQueryList::trace(Visitor* visitor) |
| 93 { | 116 { |
| 94 visitor->trace(m_matcher); | 117 visitor->trace(m_matcher); |
| 95 visitor->trace(m_media); | 118 visitor->trace(m_media); |
| 119 visitor->trace(m_listeners); |
| 96 } | 120 } |
| 97 | 121 |
| 98 } | 122 } |
| OLD | NEW |