2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "web/ContextFeaturesClientImpl.h"
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"
40 using namespace WebCore;
44 class ContextFeaturesCache FINAL : public NoBaseWillBeGarbageCollectedFinalized<ContextFeaturesCache>, public DocumentSupplement {
45 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(ContextFeaturesCache);
56 : m_value(NeedsRefresh)
57 , m_defaultValue(false)
60 bool isEnabled() const
62 ASSERT(m_value != NeedsRefresh);
63 return m_value == IsEnabled;
66 void set(bool value, bool defaultValue)
68 m_value = value ? IsEnabled : IsDisabled;
69 m_defaultValue = defaultValue;
72 bool needsRefresh(bool defaultValue) const
74 return m_value == NeedsRefresh || m_defaultValue != defaultValue;
79 bool m_defaultValue; // Needs to be traked as a part of the signature since it can be changed dynamically.
82 static const char* supplementName();
83 static ContextFeaturesCache& from(Document&);
85 Entry& entryFor(ContextFeatures::FeatureType type)
87 size_t index = static_cast<size_t>(type);
88 ASSERT_WITH_SECURITY_IMPLICATION(index < ContextFeatures::FeatureTypeSize);
89 return m_entries[index];
92 void validateAgainst(Document*);
96 Entry m_entries[ContextFeatures::FeatureTypeSize];
99 const char* ContextFeaturesCache::supplementName()
101 return "ContextFeaturesCache";
104 ContextFeaturesCache& ContextFeaturesCache::from(Document& document)
106 ContextFeaturesCache* cache = static_cast<ContextFeaturesCache*>(DocumentSupplement::from(document, supplementName()));
108 cache = new ContextFeaturesCache();
109 DocumentSupplement::provideTo(document, supplementName(), adoptPtrWillBeNoop(cache));
115 void ContextFeaturesCache::validateAgainst(Document* document)
117 String currentDomain = document->securityOrigin()->domain();
118 if (currentDomain == m_domain)
120 m_domain = currentDomain;
121 for (size_t i = 0; i < ContextFeatures::FeatureTypeSize; ++i)
122 m_entries[i] = Entry();
125 bool ContextFeaturesClientImpl::isEnabled(Document* document, ContextFeatures::FeatureType type, bool defaultValue)
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();
134 void ContextFeaturesClientImpl::urlDidChange(Document* document)
137 ContextFeaturesCache::from(*document).validateAgainst(document);
140 bool ContextFeaturesClientImpl::askIfIsEnabled(Document* document, ContextFeatures::FeatureType type, bool defaultValue)
142 WebLocalFrameImpl* frame = WebLocalFrameImpl::fromFrame(document->frame());
143 if (!frame || !frame->permissionClient())
147 case ContextFeatures::StyleScoped:
148 return frame->permissionClient()->allowWebComponents(defaultValue);
149 case ContextFeatures::MutationEvents:
150 return frame->permissionClient()->allowMutationEvents(defaultValue);
151 case ContextFeatures::PushState:
152 return frame->permissionClient()->allowPushState();