NamedFlowCollection.cpp [plain text]
#include "config.h"
#include "NamedFlowCollection.h"
#include "DOMNamedFlowCollection.h"
#include "Document.h"
#include "InspectorInstrumentation.h"
#include <wtf/text/StringHash.h>
namespace WebCore {
NamedFlowCollection::NamedFlowCollection(Document* document)
: ContextDestructionObserver(document)
{
}
Vector<RefPtr<WebKitNamedFlow>> NamedFlowCollection::namedFlows()
{
Vector<RefPtr<WebKitNamedFlow>> namedFlows;
for (NamedFlowSet::iterator it = m_namedFlows.begin(); it != m_namedFlows.end(); ++it) {
if ((*it)->flowState() == WebKitNamedFlow::FlowStateNull)
continue;
namedFlows.append(RefPtr<WebKitNamedFlow>(*it));
}
return namedFlows;
}
WebKitNamedFlow* NamedFlowCollection::flowByName(const String& flowName)
{
NamedFlowSet::iterator it = m_namedFlows.find<String, NamedFlowHashTranslator>(flowName);
if (it == m_namedFlows.end() || (*it)->flowState() == WebKitNamedFlow::FlowStateNull)
return 0;
return *it;
}
PassRef<WebKitNamedFlow> NamedFlowCollection::ensureFlowWithName(const String& flowName)
{
NamedFlowSet::iterator it = m_namedFlows.find<String, NamedFlowHashTranslator>(flowName);
if (it != m_namedFlows.end()) {
WebKitNamedFlow* namedFlow = *it;
ASSERT(namedFlow->flowState() == WebKitNamedFlow::FlowStateNull);
return *namedFlow;
}
RefPtr<WebKitNamedFlow> newFlow = WebKitNamedFlow::create(this, flowName);
m_namedFlows.add(newFlow.get());
InspectorInstrumentation::didCreateNamedFlow(document(), newFlow.get());
return newFlow.releaseNonNull();
}
void NamedFlowCollection::discardNamedFlow(WebKitNamedFlow* namedFlow)
{
if (!document())
return;
ASSERT(namedFlow->flowState() == WebKitNamedFlow::FlowStateNull);
ASSERT(m_namedFlows.contains(namedFlow));
InspectorInstrumentation::willRemoveNamedFlow(document(), namedFlow);
m_namedFlows.remove(namedFlow);
}
Document* NamedFlowCollection::document() const
{
ScriptExecutionContext* context = ContextDestructionObserver::scriptExecutionContext();
return toDocument(context);
}
PassRefPtr<DOMNamedFlowCollection> NamedFlowCollection::createCSSOMSnapshot()
{
Vector<WebKitNamedFlow*> createdFlows;
for (NamedFlowSet::iterator it = m_namedFlows.begin(); it != m_namedFlows.end(); ++it)
if ((*it)->flowState() == WebKitNamedFlow::FlowStateCreated)
createdFlows.append(*it);
return DOMNamedFlowCollection::create(createdFlows);
}
struct NamedFlowCollection::NamedFlowHashFunctions {
static unsigned hash(WebKitNamedFlow* key) { return DefaultHash<String>::Hash::hash(key->name()); }
static bool equal(WebKitNamedFlow* a, WebKitNamedFlow* b) { return a->name() == b->name(); }
static const bool safeToCompareToEmptyOrDeleted = true;
};
struct NamedFlowCollection::NamedFlowHashTranslator {
static unsigned hash(const String& key) { return DefaultHash<String>::Hash::hash(key); }
static bool equal(WebKitNamedFlow* a, const String& b) { return a->name() == b; }
};
}