This source file includes following definitions.
- m_defaultValue
- isEnabled
- set
- needsRefresh
- entryFor
- supplementName
- from
- validateAgainst
- isEnabled
- urlDidChange
- askIfIsEnabled
#include "config.h"
#include "ContextFeaturesClientImpl.h"
#include "WebDocument.h"
#include "WebFrameImpl.h"
#include "WebPermissionClient.h"
#include "core/dom/Document.h"
#include "platform/weborigin/SecurityOrigin.h"
using namespace WebCore;
namespace blink {
class ContextFeaturesCache : public DocumentSupplement {
public:
class Entry {
public:
enum Value {
IsEnabled,
IsDisabled,
NeedsRefresh
};
Entry()
: m_value(NeedsRefresh)
, m_defaultValue(false)
{ }
bool isEnabled() const
{
ASSERT(m_value != NeedsRefresh);
return m_value == IsEnabled;
}
void set(bool value, bool defaultValue)
{
m_value = value ? IsEnabled : IsDisabled;
m_defaultValue = defaultValue;
}
bool needsRefresh(bool defaultValue) const
{
return m_value == NeedsRefresh || m_defaultValue != defaultValue;
}
private:
Value m_value;
bool m_defaultValue;
};
static const char* supplementName();
static ContextFeaturesCache& from(Document&);
Entry& entryFor(ContextFeatures::FeatureType type)
{
size_t index = static_cast<size_t>(type);
ASSERT_WITH_SECURITY_IMPLICATION(index < ContextFeatures::FeatureTypeSize);
return m_entries[index];
}
void validateAgainst(Document*);
private:
String m_domain;
Entry m_entries[ContextFeatures::FeatureTypeSize];
};
const char* ContextFeaturesCache::supplementName()
{
return "ContextFeaturesCache";
}
ContextFeaturesCache& ContextFeaturesCache::from(Document& document)
{
ContextFeaturesCache* cache = static_cast<ContextFeaturesCache*>(DocumentSupplement::from(document, supplementName()));
if (!cache) {
cache = new ContextFeaturesCache();
DocumentSupplement::provideTo(document, supplementName(), adoptPtr(cache));
}
return *cache;
}
void ContextFeaturesCache::validateAgainst(Document* document)
{
String currentDomain = document->securityOrigin()->domain();
if (currentDomain == m_domain)
return;
m_domain = currentDomain;
for (size_t i = 0; i < ContextFeatures::FeatureTypeSize; ++i)
m_entries[i] = Entry();
}
bool ContextFeaturesClientImpl::isEnabled(Document* document, ContextFeatures::FeatureType type, bool defaultValue)
{
ASSERT(document);
ContextFeaturesCache::Entry& cache = ContextFeaturesCache::from(*document).entryFor(type);
if (cache.needsRefresh(defaultValue))
cache.set(askIfIsEnabled(document, type, defaultValue), defaultValue);
return cache.isEnabled();
}
void ContextFeaturesClientImpl::urlDidChange(Document* document)
{
ASSERT(document);
ContextFeaturesCache::from(*document).validateAgainst(document);
}
bool ContextFeaturesClientImpl::askIfIsEnabled(Document* document, ContextFeatures::FeatureType type, bool defaultValue)
{
WebFrameImpl* frame = WebFrameImpl::fromFrame(document->frame());
if (!frame || !frame->permissionClient())
return defaultValue;
switch (type) {
case ContextFeatures::StyleScoped:
return frame->permissionClient()->allowWebComponents(frame, defaultValue);
case ContextFeatures::MutationEvents:
return frame->permissionClient()->allowMutationEvents(frame, defaultValue);
case ContextFeatures::PushState:
return frame->permissionClient()->allowPushState(frame);
default:
return defaultValue;
}
}
}