2 * Copyright (C) 2012 Adobe Systems Incorporated. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
25 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include "NamedFlowCollection.h"
33 #include "DOMNamedFlowCollection.h"
35 #include "InspectorInstrumentation.h"
36 #include "WebKitNamedFlow.h"
38 #include <wtf/text/StringHash.h>
39 #include <wtf/text/WTFString.h>
43 NamedFlowCollection::NamedFlowCollection(Document* doc)
48 Vector<RefPtr<WebKitNamedFlow> > NamedFlowCollection::namedFlows()
50 Vector<RefPtr<WebKitNamedFlow> > namedFlows;
52 for (NamedFlowSet::iterator it = m_namedFlows.begin(); it != m_namedFlows.end(); ++it) {
53 if ((*it)->flowState() == WebKitNamedFlow::FlowStateNull)
56 namedFlows.append(RefPtr<WebKitNamedFlow>(*it));
62 WebKitNamedFlow* NamedFlowCollection::flowByName(const String& flowName)
64 NamedFlowSet::iterator it = m_namedFlows.find<String, NamedFlowHashTranslator>(flowName);
65 if (it == m_namedFlows.end() || (*it)->flowState() == WebKitNamedFlow::FlowStateNull)
71 PassRefPtr<WebKitNamedFlow> NamedFlowCollection::ensureFlowWithName(const String& flowName)
73 NamedFlowSet::iterator it = m_namedFlows.find<String, NamedFlowHashTranslator>(flowName);
74 if (it != m_namedFlows.end()) {
75 WebKitNamedFlow* namedFlow = *it;
76 ASSERT(namedFlow->flowState() == WebKitNamedFlow::FlowStateNull);
81 RefPtr<WebKitNamedFlow> newFlow = WebKitNamedFlow::create(this, flowName);
82 m_namedFlows.add(newFlow.get());
84 InspectorInstrumentation::didCreateNamedFlow(m_document, newFlow.get());
86 return newFlow.release();
89 void NamedFlowCollection::discardNamedFlow(WebKitNamedFlow* namedFlow)
91 // The document is not valid anymore so the collection will be destroyed anyway.
95 ASSERT(namedFlow->flowState() == WebKitNamedFlow::FlowStateNull);
96 ASSERT(m_namedFlows.contains(namedFlow));
98 InspectorInstrumentation::willRemoveNamedFlow(m_document, namedFlow);
100 m_namedFlows.remove(namedFlow);
103 void NamedFlowCollection::documentDestroyed()
107 PassRefPtr<DOMNamedFlowCollection> NamedFlowCollection::createCSSOMSnapshot()
109 Vector<WebKitNamedFlow*> createdFlows;
110 for (NamedFlowSet::iterator it = m_namedFlows.begin(); it != m_namedFlows.end(); ++it)
111 if ((*it)->flowState() == WebKitNamedFlow::FlowStateCreated)
112 createdFlows.append(*it);
113 return DOMNamedFlowCollection::create(createdFlows);
116 // The HashFunctions object used by the HashSet to compare between NamedFlows.
117 // It is safe to set safeToCompareToEmptyOrDeleted because the HashSet will never contain null pointers or deleted values.
118 struct NamedFlowCollection::NamedFlowHashFunctions {
119 static unsigned hash(WebKitNamedFlow* key) { return DefaultHash<String>::Hash::hash(key->name()); }
120 static bool equal(WebKitNamedFlow* a, WebKitNamedFlow* b) { return a->name() == b->name(); }
121 static const bool safeToCompareToEmptyOrDeleted = true;
124 // The HashTranslator is used to lookup a NamedFlow in the set using a name.
125 struct NamedFlowCollection::NamedFlowHashTranslator {
126 static unsigned hash(const String& key) { return DefaultHash<String>::Hash::hash(key); }
127 static bool equal(WebKitNamedFlow* a, const String& b) { return a->name() == b; }
130 } // namespace WebCore