1/*
2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2006, 2008, 2009, 2010, 2012 Apple Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB.  If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#ifndef MediaList_h
22#define MediaList_h
23
24#include "ExceptionCode.h"
25#include <memory>
26#include <wtf/Forward.h>
27#include <wtf/PassRefPtr.h>
28#include <wtf/RefCounted.h>
29#include <wtf/Vector.h>
30#include <wtf/text/WTFString.h>
31
32namespace WebCore {
33
34class CSSRule;
35class CSSStyleSheet;
36class Document;
37class MediaList;
38class MediaQuery;
39
40class MediaQuerySet : public RefCounted<MediaQuerySet> {
41public:
42    static PassRefPtr<MediaQuerySet> create()
43    {
44        return adoptRef(new MediaQuerySet());
45    }
46    static PassRefPtr<MediaQuerySet> create(const String& mediaString)
47    {
48        return adoptRef(new MediaQuerySet(mediaString, false));
49    }
50    static PassRefPtr<MediaQuerySet> createAllowingDescriptionSyntax(const String& mediaString)
51    {
52        return adoptRef(new MediaQuerySet(mediaString, true));
53    }
54    ~MediaQuerySet();
55
56    bool parse(const String&);
57    bool add(const String&);
58    bool remove(const String&);
59
60    void addMediaQuery(std::unique_ptr<MediaQuery>);
61
62    const Vector<std::unique_ptr<MediaQuery>>& queryVector() const { return m_queries; }
63
64    int lastLine() const { return m_lastLine; }
65    void setLastLine(int lastLine) { m_lastLine = lastLine; }
66
67    String mediaText() const;
68
69    PassRefPtr<MediaQuerySet> copy() const { return adoptRef(new MediaQuerySet(*this)); }
70
71private:
72    MediaQuerySet();
73    MediaQuerySet(const String& mediaQuery, bool fallbackToDescription);
74    MediaQuerySet(const MediaQuerySet&);
75
76    unsigned m_fallbackToDescriptor : 1; // true if failed media query parsing should fallback to media description parsing.
77    signed m_lastLine : 31;
78    Vector<std::unique_ptr<MediaQuery>> m_queries;
79};
80
81class MediaList : public RefCounted<MediaList> {
82public:
83    static PassRefPtr<MediaList> create(MediaQuerySet* mediaQueries, CSSStyleSheet* parentSheet)
84    {
85        return adoptRef(new MediaList(mediaQueries, parentSheet));
86    }
87    static PassRefPtr<MediaList> create(MediaQuerySet* mediaQueries, CSSRule* parentRule)
88    {
89        return adoptRef(new MediaList(mediaQueries, parentRule));
90    }
91
92    ~MediaList();
93
94    unsigned length() const { return m_mediaQueries->queryVector().size(); }
95    String item(unsigned index) const;
96    void deleteMedium(const String& oldMedium, ExceptionCode&);
97    void appendMedium(const String& newMedium, ExceptionCode&);
98
99    String mediaText() const { return m_mediaQueries->mediaText(); }
100    void setMediaText(const String&, ExceptionCode&);
101
102    // Not part of CSSOM.
103    CSSRule* parentRule() const { return m_parentRule; }
104    CSSStyleSheet* parentStyleSheet() const { return m_parentStyleSheet; }
105    void clearParentStyleSheet() { ASSERT(m_parentStyleSheet); m_parentStyleSheet = 0; }
106    void clearParentRule() { ASSERT(m_parentRule); m_parentRule = 0; }
107    const MediaQuerySet* queries() const { return m_mediaQueries.get(); }
108
109    void reattach(MediaQuerySet*);
110
111private:
112    MediaList();
113    MediaList(MediaQuerySet*, CSSStyleSheet* parentSheet);
114    MediaList(MediaQuerySet*, CSSRule* parentRule);
115
116    RefPtr<MediaQuerySet> m_mediaQueries;
117    CSSStyleSheet* m_parentStyleSheet;
118    CSSRule* m_parentRule;
119};
120
121#if ENABLE(RESOLUTION_MEDIA_QUERY)
122// Adds message to inspector console whenever dpi or dpcm values are used for "screen" media.
123void reportMediaQueryWarningIfNeeded(Document*, const MediaQuerySet*);
124#endif
125
126} // namespace
127
128#endif
129