• 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 #ifndef MediaQueryMatcher_h
21 #define MediaQueryMatcher_h
22 
23 #include "platform/heap/Handle.h"
24 #include "wtf/Forward.h"
25 #include "wtf/RefCounted.h"
26 #include "wtf/Vector.h"
27 
28 namespace WebCore {
29 
30 class Document;
31 class MediaQueryList;
32 class MediaQueryListListener;
33 class MediaQueryEvaluator;
34 class MediaQuerySet;
35 
36 // MediaQueryMatcher class is responsible for keeping a vector of pairs
37 // MediaQueryList x MediaQueryListListener. It is responsible for evaluating the queries
38 // whenever it is needed and to call the listeners if the corresponding query has changed.
39 // The listeners must be called in the very same order in which they have been added.
40 
41 class MediaQueryMatcher FINAL : public RefCountedWillBeGarbageCollected<MediaQueryMatcher> {
42     DECLARE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(MediaQueryMatcher);
43 public:
create(Document * document)44     static PassRefPtrWillBeRawPtr<MediaQueryMatcher> create(Document* document) { return adoptRefWillBeNoop(new MediaQueryMatcher(document)); }
45     void documentDestroyed();
46 
47     void addListener(PassRefPtrWillBeRawPtr<MediaQueryListListener>, PassRefPtrWillBeRawPtr<MediaQueryList>);
48     void removeListener(MediaQueryListListener*, MediaQueryList*);
49 
50     PassRefPtrWillBeRawPtr<MediaQueryList> matchMedia(const String&);
51 
evaluationRound()52     unsigned evaluationRound() const { return m_evaluationRound; }
53     void styleResolverChanged();
54     bool evaluate(const MediaQuerySet*);
55 
56     void trace(Visitor*);
57 
58 private:
59     class Listener FINAL : public NoBaseWillBeGarbageCollected<Listener> {
60     public:
61         Listener(PassRefPtrWillBeRawPtr<MediaQueryListListener>, PassRefPtrWillBeRawPtr<MediaQueryList>);
62         void evaluate(MediaQueryEvaluator*);
63 
listener()64         MediaQueryListListener* listener() { return m_listener.get(); }
query()65         MediaQueryList* query() { return m_query.get(); }
66 
67         void trace(Visitor*);
68 
69     private:
70         RefPtrWillBeMember<MediaQueryListListener> m_listener;
71         RefPtrWillBeMember<MediaQueryList> m_query;
72     };
73 
74     MediaQueryMatcher(Document*);
75     PassOwnPtr<MediaQueryEvaluator> prepareEvaluator() const;
76     AtomicString mediaType() const;
77 
78     RawPtrWillBeMember<Document> m_document;
79     WillBeHeapVector<OwnPtrWillBeMember<Listener> > m_listeners;
80 
81     // This value is incremented at style selector changes.
82     // It is used to avoid evaluating queries more then once and to make sure
83     // that a media query result change is notified exactly once.
84     unsigned m_evaluationRound;
85 };
86 
87 }
88 
89 #endif // MediaQueryMatcher_h
90