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#pragma once
21
22#include <memory>
23#include <wtf/Forward.h>
24#include <wtf/RefCounted.h>
25#include <wtf/Vector.h>
26#include <wtf/WeakPtr.h>
27
28namespace WebCore {
29
30class Document;
31class MediaQueryList;
32class MediaQueryListListener;
33class MediaQueryEvaluator;
34class MediaQuerySet;
35class RenderStyle;
36
37// MediaQueryMatcher class is responsible for keeping a vector of pairs
38// MediaQueryList x MediaQueryListListener. It is responsible for evaluating the queries
39// whenever it is needed and to call the listeners if the corresponding query has changed.
40// The listeners must be called in the order in which they were added.
41
42class MediaQueryMatcher final : public RefCounted<MediaQueryMatcher> {
43public:
44 static Ref<MediaQueryMatcher> create(Document& document) { return adoptRef(*new MediaQueryMatcher(document)); }
45 ~MediaQueryMatcher();
46
47 void documentDestroyed();
48
49 void addListener(Ref<MediaQueryListListener>&&, MediaQueryList&);
50 void removeListener(MediaQueryListListener&, MediaQueryList&);
51
52 RefPtr<MediaQueryList> matchMedia(const String&);
53
54 unsigned evaluationRound() const { return m_evaluationRound; }
55
56 void styleResolverChanged();
57
58 bool evaluate(const MediaQuerySet&);
59
60private:
61 struct Listener {
62 Ref<MediaQueryListListener> listener;
63 Ref<MediaQueryList> query;
64 };
65
66 explicit MediaQueryMatcher(Document&);
67 std::unique_ptr<RenderStyle> documentElementUserAgentStyle() const;
68 String mediaType() const;
69
70 WeakPtr<Document> m_document;
71 Vector<Listener> m_listeners;
72
73 // This value is incremented at style selector changes.
74 // It is used to avoid evaluating queries more then once and to make sure
75 // that a media query result change is notified exactly once.
76 unsigned m_evaluationRound { 1 };
77};
78
79} // namespace WebCore
80