OLD | NEW |
| (Empty) |
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 "web/ContextFeaturesClientImpl.h" | |
32 | |
33 #include "core/dom/Document.h" | |
34 #include "platform/weborigin/SecurityOrigin.h" | |
35 #include "public/web/WebContentSettingsClient.h" | |
36 #include "public/web/WebDocument.h" | |
37 #include "web/WebLocalFrameImpl.h" | |
38 | |
39 namespace blink { | |
40 | |
41 class ContextFeaturesCache final | |
42 : public GarbageCollectedFinalized<ContextFeaturesCache>, | |
43 public Supplement<Document> { | |
44 USING_GARBAGE_COLLECTED_MIXIN(ContextFeaturesCache); | |
45 | |
46 public: | |
47 class Entry { | |
48 public: | |
49 enum Value { IsEnabled, IsDisabled, NeedsRefresh }; | |
50 | |
51 Entry() : m_value(NeedsRefresh), m_defaultValue(false) {} | |
52 | |
53 bool isEnabled() const { | |
54 DCHECK_NE(m_value, NeedsRefresh); | |
55 return m_value == IsEnabled; | |
56 } | |
57 | |
58 void set(bool value, bool defaultValue) { | |
59 m_value = value ? IsEnabled : IsDisabled; | |
60 m_defaultValue = defaultValue; | |
61 } | |
62 | |
63 bool needsRefresh(bool defaultValue) const { | |
64 return m_value == NeedsRefresh || m_defaultValue != defaultValue; | |
65 } | |
66 | |
67 private: | |
68 Value m_value; | |
69 bool m_defaultValue; // Needs to be traked as a part of the signature since | |
70 // it can be changed dynamically. | |
71 }; | |
72 | |
73 static const char* supplementName(); | |
74 static ContextFeaturesCache& from(Document&); | |
75 | |
76 Entry& entryFor(ContextFeatures::FeatureType type) { | |
77 size_t index = static_cast<size_t>(type); | |
78 SECURITY_DCHECK(index < ContextFeatures::FeatureTypeSize); | |
79 return m_entries[index]; | |
80 } | |
81 | |
82 void validateAgainst(Document*); | |
83 | |
84 DEFINE_INLINE_VIRTUAL_TRACE() { Supplement<Document>::trace(visitor); } | |
85 | |
86 private: | |
87 explicit ContextFeaturesCache(Document& document) | |
88 : Supplement<Document>(document) {} | |
89 | |
90 String m_domain; | |
91 Entry m_entries[ContextFeatures::FeatureTypeSize]; | |
92 }; | |
93 | |
94 const char* ContextFeaturesCache::supplementName() { | |
95 return "ContextFeaturesCache"; | |
96 } | |
97 | |
98 ContextFeaturesCache& ContextFeaturesCache::from(Document& document) { | |
99 ContextFeaturesCache* cache = static_cast<ContextFeaturesCache*>( | |
100 Supplement<Document>::from(document, supplementName())); | |
101 if (!cache) { | |
102 cache = new ContextFeaturesCache(document); | |
103 Supplement<Document>::provideTo(document, supplementName(), cache); | |
104 } | |
105 | |
106 return *cache; | |
107 } | |
108 | |
109 void ContextFeaturesCache::validateAgainst(Document* document) { | |
110 String currentDomain = document->getSecurityOrigin()->domain(); | |
111 if (currentDomain == m_domain) | |
112 return; | |
113 m_domain = currentDomain; | |
114 for (size_t i = 0; i < ContextFeatures::FeatureTypeSize; ++i) | |
115 m_entries[i] = Entry(); | |
116 } | |
117 | |
118 bool ContextFeaturesClientImpl::isEnabled(Document* document, | |
119 ContextFeatures::FeatureType type, | |
120 bool defaultValue) { | |
121 DCHECK(document); | |
122 ContextFeaturesCache::Entry& cache = | |
123 ContextFeaturesCache::from(*document).entryFor(type); | |
124 if (cache.needsRefresh(defaultValue)) | |
125 cache.set(askIfIsEnabled(document, type, defaultValue), defaultValue); | |
126 return cache.isEnabled(); | |
127 } | |
128 | |
129 void ContextFeaturesClientImpl::urlDidChange(Document* document) { | |
130 DCHECK(document); | |
131 ContextFeaturesCache::from(*document).validateAgainst(document); | |
132 } | |
133 | |
134 bool ContextFeaturesClientImpl::askIfIsEnabled( | |
135 Document* document, | |
136 ContextFeatures::FeatureType type, | |
137 bool defaultValue) { | |
138 WebLocalFrameImpl* frame = WebLocalFrameImpl::fromFrame(document->frame()); | |
139 if (!frame || !frame->contentSettingsClient()) | |
140 return defaultValue; | |
141 | |
142 switch (type) { | |
143 case ContextFeatures::MutationEvents: | |
144 return frame->contentSettingsClient()->allowMutationEvents(defaultValue); | |
145 default: | |
146 return defaultValue; | |
147 } | |
148 } | |
149 | |
150 } // namespace blink | |
OLD | NEW |