Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(409)

Unified Diff: Source/core/css/MediaQueryList.cpp

Issue 348893004: Rework MediaQueryMatcher to batch up listener notification (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: missed one... Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/core/css/MediaQueryList.h ('k') | Source/core/css/MediaQueryListListener.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/core/css/MediaQueryList.cpp
diff --git a/Source/core/css/MediaQueryList.cpp b/Source/core/css/MediaQueryList.cpp
index d76b5bbec1599f0b80432c1c55c7184e98a28e46..f7d8554a789d0d2ab64e354f9952a75f74a1bd59 100644
--- a/Source/core/css/MediaQueryList.cpp
+++ b/Source/core/css/MediaQueryList.cpp
@@ -27,21 +27,27 @@
namespace WebCore {
-PassRefPtrWillBeRawPtr<MediaQueryList> MediaQueryList::create(PassRefPtrWillBeRawPtr<MediaQueryMatcher> vector, PassRefPtrWillBeRawPtr<MediaQuerySet> media, bool matches)
+PassRefPtrWillBeRawPtr<MediaQueryList> MediaQueryList::create(PassRefPtrWillBeRawPtr<MediaQueryMatcher> matcher, PassRefPtrWillBeRawPtr<MediaQuerySet> media)
{
- return adoptRefWillBeNoop(new MediaQueryList(vector, media, matches));
+ return adoptRefWillBeNoop(new MediaQueryList(matcher, media));
}
-MediaQueryList::MediaQueryList(PassRefPtrWillBeRawPtr<MediaQueryMatcher> vector, PassRefPtrWillBeRawPtr<MediaQuerySet> media, bool matches)
- : m_matcher(vector)
+MediaQueryList::MediaQueryList(PassRefPtrWillBeRawPtr<MediaQueryMatcher> matcher, PassRefPtrWillBeRawPtr<MediaQuerySet> media)
+ : m_matcher(matcher)
, m_media(media)
- , m_evaluationRound(m_matcher->evaluationRound())
- , m_changeRound(m_evaluationRound - 1) // m_evaluationRound and m_changeRound initial values must be different.
- , m_matches(matches)
+ , m_matchesDirty(true)
+ , m_matches(false)
{
+ m_matcher->addMediaQueryList(this);
+ updateMatches();
}
-DEFINE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(MediaQueryList)
+MediaQueryList::~MediaQueryList()
+{
+#if !ENABLE(OILPAN)
+ m_matcher->removeMediaQueryList(this);
+#endif
+}
String MediaQueryList::media() const
{
@@ -53,7 +59,8 @@ void MediaQueryList::addListener(PassRefPtrWillBeRawPtr<MediaQueryListListener>
if (!listener)
return;
- m_matcher->addListener(listener, this);
+ listener->setMediaQueryList(this);
+ m_listeners.add(listener);
}
void MediaQueryList::removeListener(PassRefPtrWillBeRawPtr<MediaQueryListListener> listener)
@@ -61,31 +68,47 @@ void MediaQueryList::removeListener(PassRefPtrWillBeRawPtr<MediaQueryListListene
if (!listener)
return;
- m_matcher->removeListener(listener.get(), this);
+ RefPtrWillBeRawPtr<MediaQueryList> protect(this);
+ listener->clearMediaQueryList();
+
+ for (ListenerList::iterator i = m_listeners.begin(), end = m_listeners.end(); i != end; ++i) {
+ // We can't just use m_listeners.remove() here, because we get a new wrapper for the
+ // listener callback every time. We have to use MediaQueryListListener::operator==.
+ if (**i == *listener.get()) {
+ m_listeners.remove(i);
+ break;
+ }
+ }
}
-bool MediaQueryList::evaluate(MediaQueryEvaluator* evaluator)
+void MediaQueryList::documentDetached()
{
- if (m_evaluationRound != m_matcher->evaluationRound() && evaluator)
- setMatches(evaluator->eval(m_media.get()));
- return m_changeRound == m_matcher->evaluationRound();
+ m_listeners.clear();
}
-void MediaQueryList::setMatches(bool newValue)
+void MediaQueryList::mediaFeaturesChanged(WillBeHeapVector<RefPtrWillBeMember<MediaQueryListListener> >* toNotify)
{
- m_evaluationRound = m_matcher->evaluationRound();
-
- if (newValue == m_matches)
+ m_matchesDirty = true;
+ if (!updateMatches())
return;
+ for (ListenerList::const_iterator i = m_listeners.begin(), end = m_listeners.end(); i != end; ++i) {
esprehn 2014/06/26 21:35:54 "it", single letter variable names are usually onl
+ toNotify->append(*i);
+ }
+}
- m_matches = newValue;
- m_changeRound = m_evaluationRound;
+bool MediaQueryList::updateMatches()
+{
+ m_matchesDirty = false;
+ if (m_matches != m_matcher->evaluate(m_media.get())) {
+ m_matches = !m_matches;
+ return true;
+ }
+ return false;
}
bool MediaQueryList::matches()
{
- if (m_evaluationRound != m_matcher->evaluationRound())
- setMatches(m_matcher->evaluate(m_media.get()));
+ updateMatches();
return m_matches;
}
@@ -93,6 +116,7 @@ void MediaQueryList::trace(Visitor* visitor)
{
visitor->trace(m_matcher);
visitor->trace(m_media);
+ visitor->trace(m_listeners);
}
}
« no previous file with comments | « Source/core/css/MediaQueryList.h ('k') | Source/core/css/MediaQueryListListener.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698