Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / web / ContextFeaturesClientImpl.cpp
blobe0375c2755427d2e463255e719961f5517a76956
1 /*
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
6 * met:
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.
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.
31 #include "config.h"
32 #include "web/ContextFeaturesClientImpl.h"
34 #include "core/dom/Document.h"
35 #include "platform/weborigin/SecurityOrigin.h"
36 #include "public/web/WebContentSettingsClient.h"
37 #include "public/web/WebDocument.h"
38 #include "web/WebLocalFrameImpl.h"
40 namespace blink {
42 class ContextFeaturesCache final : public NoBaseWillBeGarbageCollectedFinalized<ContextFeaturesCache>, public WillBeHeapSupplement<Document> {
43 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(ContextFeaturesCache);
44 public:
45 class Entry {
46 public:
47 enum Value {
48 IsEnabled,
49 IsDisabled,
50 NeedsRefresh
53 Entry()
54 : m_value(NeedsRefresh)
55 , m_defaultValue(false)
56 { }
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;
75 private:
76 Value m_value;
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*);
92 DEFINE_INLINE_VIRTUAL_TRACE()
94 WillBeHeapSupplement<Document>::trace(visitor);
97 private:
98 String m_domain;
99 Entry m_entries[ContextFeatures::FeatureTypeSize];
102 const char* ContextFeaturesCache::supplementName()
104 return "ContextFeaturesCache";
107 ContextFeaturesCache& ContextFeaturesCache::from(Document& document)
109 ContextFeaturesCache* cache = static_cast<ContextFeaturesCache*>(WillBeHeapSupplement<Document>::from(document, supplementName()));
110 if (!cache) {
111 cache = new ContextFeaturesCache();
112 WillBeHeapSupplement<Document>::provideTo(document, supplementName(), adoptPtrWillBeNoop(cache));
115 return *cache;
118 void ContextFeaturesCache::validateAgainst(Document* document)
120 String currentDomain = document->securityOrigin()->domain();
121 if (currentDomain == m_domain)
122 return;
123 m_domain = currentDomain;
124 for (size_t i = 0; i < ContextFeatures::FeatureTypeSize; ++i)
125 m_entries[i] = Entry();
128 bool ContextFeaturesClientImpl::isEnabled(Document* document, ContextFeatures::FeatureType type, bool defaultValue)
130 ASSERT(document);
131 ContextFeaturesCache::Entry& cache = ContextFeaturesCache::from(*document).entryFor(type);
132 if (cache.needsRefresh(defaultValue))
133 cache.set(askIfIsEnabled(document, type, defaultValue), defaultValue);
134 return cache.isEnabled();
137 void ContextFeaturesClientImpl::urlDidChange(Document* document)
139 ASSERT(document);
140 ContextFeaturesCache::from(*document).validateAgainst(document);
143 bool ContextFeaturesClientImpl::askIfIsEnabled(Document* document, ContextFeatures::FeatureType type, bool defaultValue)
145 WebLocalFrameImpl* frame = WebLocalFrameImpl::fromFrame(document->frame());
146 if (!frame || !frame->contentSettingsClient())
147 return defaultValue;
149 switch (type) {
150 case ContextFeatures::MutationEvents:
151 return frame->contentSettingsClient()->allowMutationEvents(defaultValue);
152 default:
153 return defaultValue;
157 } // namespace blink