1/*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Neither the name of Google Inc. nor the names of its
11 * contributors may be used to endorse or promote products derived from
12 * this software without specific prior written permission.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#ifndef ContextFeatures_h
28#define ContextFeatures_h
29
30#include "RefCountedSupplement.h"
31#include <wtf/PassOwnPtr.h>
32
33namespace WebCore {
34
35class ContextFeaturesClient;
36class Document;
37class Page;
38
39class ContextFeatures : public RefCountedSupplement<Page, ContextFeatures> {
40public:
41    enum FeatureType {
42        DialogElement = 0,
43        StyleScoped,
44        HTMLNotifications,
45        MutationEvents,
46        PushState,
47        FeatureTypeSize // Should be the last entry.
48    };
49
50    static const char* supplementName();
51    static ContextFeatures* defaultSwitch();
52    static PassRefPtr<ContextFeatures> create(ContextFeaturesClient*);
53
54    static bool dialogElementEnabled(Document*);
55    static bool styleScopedEnabled(Document*);
56    static bool htmlNotificationsEnabled(Document*);
57    static bool mutationEventsEnabled(Document*);
58    static bool pushStateEnabled(Document*);
59
60    bool isEnabled(Document*, FeatureType, bool) const;
61    void urlDidChange(Document*);
62
63private:
64    explicit ContextFeatures(ContextFeaturesClient* client)
65        : m_client(client)
66    { }
67
68    virtual void hostDestroyed() OVERRIDE;
69
70    ContextFeaturesClient* m_client;
71};
72
73inline void ContextFeatures::hostDestroyed()
74{
75    m_client = 0;
76}
77
78
79class ContextFeaturesClient {
80    WTF_MAKE_FAST_ALLOCATED;
81public:
82    static ContextFeaturesClient* empty();
83
84    virtual ~ContextFeaturesClient() { }
85    virtual bool isEnabled(Document*, ContextFeatures::FeatureType, bool defaultValue) { return defaultValue; }
86    virtual void urlDidChange(Document*) { }
87};
88
89void provideContextFeaturesTo(Page*, ContextFeaturesClient*);
90void provideContextFeaturesToDocumentFrom(Document*, Page*);
91
92inline PassRefPtr<ContextFeatures> ContextFeatures::create(ContextFeaturesClient* client)
93{
94    return adoptRef(new ContextFeatures(client));
95}
96
97inline bool ContextFeatures::isEnabled(Document* document, FeatureType type, bool defaultValue) const
98{
99    if (!m_client)
100        return defaultValue;
101    return m_client->isEnabled(document, type, defaultValue);
102}
103
104inline void ContextFeatures::urlDidChange(Document* document)
105{
106    if (m_client)
107        return;
108    m_client->urlDidChange(document);
109}
110
111} // namespace WebCore
112
113#endif // ContextFeatures_h
114