• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "config.h"
32 #include "web/ContextFeaturesClientImpl.h"
33 
34 #include "core/dom/Document.h"
35 #include "platform/weborigin/SecurityOrigin.h"
36 #include "public/web/WebDocument.h"
37 #include "public/web/WebPermissionClient.h"
38 #include "web/WebLocalFrameImpl.h"
39 
40 using namespace WebCore;
41 
42 namespace blink {
43 
44 class ContextFeaturesCache FINAL : public NoBaseWillBeGarbageCollectedFinalized<ContextFeaturesCache>, public DocumentSupplement {
45     WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(ContextFeaturesCache);
46 public:
47     class Entry {
48     public:
49         enum Value {
50             IsEnabled,
51             IsDisabled,
52             NeedsRefresh
53         };
54 
Entry()55         Entry()
56             : m_value(NeedsRefresh)
57             , m_defaultValue(false)
58         { }
59 
isEnabled() const60         bool isEnabled() const
61         {
62             ASSERT(m_value != NeedsRefresh);
63             return m_value == IsEnabled;
64         }
65 
set(bool value,bool defaultValue)66         void set(bool value, bool defaultValue)
67         {
68             m_value = value ? IsEnabled : IsDisabled;
69             m_defaultValue = defaultValue;
70         }
71 
needsRefresh(bool defaultValue) const72         bool needsRefresh(bool defaultValue) const
73         {
74             return m_value == NeedsRefresh || m_defaultValue != defaultValue;
75         }
76 
77     private:
78         Value m_value;
79         bool m_defaultValue; // Needs to be traked as a part of the signature since it can be changed dynamically.
80     };
81 
82     static const char* supplementName();
83     static ContextFeaturesCache& from(Document&);
84 
entryFor(ContextFeatures::FeatureType type)85     Entry& entryFor(ContextFeatures::FeatureType type)
86     {
87         size_t index = static_cast<size_t>(type);
88         ASSERT_WITH_SECURITY_IMPLICATION(index < ContextFeatures::FeatureTypeSize);
89         return m_entries[index];
90     }
91 
92     void validateAgainst(Document*);
93 
94 private:
95     String m_domain;
96     Entry m_entries[ContextFeatures::FeatureTypeSize];
97 };
98 
supplementName()99 const char* ContextFeaturesCache::supplementName()
100 {
101     return "ContextFeaturesCache";
102 }
103 
from(Document & document)104 ContextFeaturesCache& ContextFeaturesCache::from(Document& document)
105 {
106     ContextFeaturesCache* cache = static_cast<ContextFeaturesCache*>(DocumentSupplement::from(document, supplementName()));
107     if (!cache) {
108         cache = new ContextFeaturesCache();
109         DocumentSupplement::provideTo(document, supplementName(), adoptPtrWillBeNoop(cache));
110     }
111 
112     return *cache;
113 }
114 
validateAgainst(Document * document)115 void ContextFeaturesCache::validateAgainst(Document* document)
116 {
117     String currentDomain = document->securityOrigin()->domain();
118     if (currentDomain == m_domain)
119         return;
120     m_domain = currentDomain;
121     for (size_t i = 0; i < ContextFeatures::FeatureTypeSize; ++i)
122         m_entries[i] = Entry();
123 }
124 
isEnabled(Document * document,ContextFeatures::FeatureType type,bool defaultValue)125 bool ContextFeaturesClientImpl::isEnabled(Document* document, ContextFeatures::FeatureType type, bool defaultValue)
126 {
127     ASSERT(document);
128     ContextFeaturesCache::Entry& cache = ContextFeaturesCache::from(*document).entryFor(type);
129     if (cache.needsRefresh(defaultValue))
130         cache.set(askIfIsEnabled(document, type, defaultValue), defaultValue);
131     return cache.isEnabled();
132 }
133 
urlDidChange(Document * document)134 void ContextFeaturesClientImpl::urlDidChange(Document* document)
135 {
136     ASSERT(document);
137     ContextFeaturesCache::from(*document).validateAgainst(document);
138 }
139 
askIfIsEnabled(Document * document,ContextFeatures::FeatureType type,bool defaultValue)140 bool ContextFeaturesClientImpl::askIfIsEnabled(Document* document, ContextFeatures::FeatureType type, bool defaultValue)
141 {
142     WebLocalFrameImpl* frame = WebLocalFrameImpl::fromFrame(document->frame());
143     if (!frame || !frame->permissionClient())
144         return defaultValue;
145 
146     switch (type) {
147     case ContextFeatures::MutationEvents:
148         return frame->permissionClient()->allowMutationEvents(defaultValue);
149     case ContextFeatures::PushState:
150         return frame->permissionClient()->allowPushState();
151     default:
152         return defaultValue;
153     }
154 }
155 
156 } // namespace blink
157