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"
42 class ContextFeaturesCache FINAL : public NoBaseWillBeGarbageCollectedFinalized<ContextFeaturesCache>, public DocumentSupplement {
43 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(ContextFeaturesCache);
54 : m_value(NeedsRefresh)
55 , m_defaultValue(false)
58 bool isEnabled() const
60 ASSERT(m_value != NeedsRefresh);
61 return m_value == IsEnabled;
64 void set(bool value, bool defaultValue)
66 m_value = value ? IsEnabled : IsDisabled;
67 m_defaultValue = defaultValue;
70 bool needsRefresh(bool defaultValue) const
72 return m_value == NeedsRefresh || m_defaultValue != defaultValue;
77 bool m_defaultValue; // Needs to be traked as a part of the signature since it can be changed dynamically.
80 static const char* supplementName();
81 static ContextFeaturesCache& from(Document&);
83 Entry& entryFor(ContextFeatures::FeatureType type)
85 size_t index = static_cast<size_t>(type);
86 ASSERT_WITH_SECURITY_IMPLICATION(index < ContextFeatures::FeatureTypeSize);
87 return m_entries[index];
90 void validateAgainst(Document*);
94 Entry m_entries[ContextFeatures::FeatureTypeSize];
97 const char* ContextFeaturesCache::supplementName()
99 return "ContextFeaturesCache";
102 ContextFeaturesCache& ContextFeaturesCache::from(Document& document)
104 ContextFeaturesCache* cache = static_cast<ContextFeaturesCache*>(DocumentSupplement::from(document, supplementName()));
106 cache = new ContextFeaturesCache();
107 DocumentSupplement::provideTo(document, supplementName(), adoptPtrWillBeNoop(cache));
113 void ContextFeaturesCache::validateAgainst(Document* document)
115 String currentDomain = document->securityOrigin()->domain();
116 if (currentDomain == m_domain)
118 m_domain = currentDomain;
119 for (size_t i = 0; i < ContextFeatures::FeatureTypeSize; ++i)
120 m_entries[i] = Entry();
123 bool ContextFeaturesClientImpl::isEnabled(Document* document, ContextFeatures::FeatureType type, bool defaultValue)
126 ContextFeaturesCache::Entry& cache = ContextFeaturesCache::from(*document).entryFor(type);
127 if (cache.needsRefresh(defaultValue))
128 cache.set(askIfIsEnabled(document, type, defaultValue), defaultValue);
129 return cache.isEnabled();
132 void ContextFeaturesClientImpl::urlDidChange(Document* document)
135 ContextFeaturesCache::from(*document).validateAgainst(document);
138 bool ContextFeaturesClientImpl::askIfIsEnabled(Document* document, ContextFeatures::FeatureType type, bool defaultValue)
140 WebLocalFrameImpl* frame = WebLocalFrameImpl::fromFrame(document->frame());
141 if (!frame || !frame->permissionClient())
145 case ContextFeatures::MutationEvents:
146 return frame->permissionClient()->allowMutationEvents(defaultValue);
147 case ContextFeatures::PushState:
148 return frame->permissionClient()->allowPushState();