1/*
2 * Copyright (C) 2012 Adobe Systems Incorporated. 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
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer.
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.
15 *
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
27 * SUCH DAMAGE.
28 */
29
30#include "config.h"
31#include "WebKitNamedFlowCollection.h"
32
33#include "Document.h"
34#include "WebKitNamedFlow.h"
35
36#include <wtf/text/StringHash.h>
37#include <wtf/text/WTFString.h>
38
39namespace WebCore {
40
41WebKitNamedFlowCollection::WebKitNamedFlowCollection(Document* doc)
42 : m_document(doc)
43 , m_createdFlowsCount(0)
44{
45}
46
47WebKitNamedFlowCollection::~WebKitNamedFlowCollection()
48{
49}
50
51unsigned WebKitNamedFlowCollection::length() const
52{
53 return m_createdFlowsCount;
54}
55
56WebKitNamedFlow* WebKitNamedFlowCollection::item(unsigned index) const
57{
58 if (index >= m_createdFlowsCount)
59 return 0;
60
61 NamedFlowSet::const_iterator end = m_namedFlows.end();
62 for (NamedFlowSet::const_iterator it = m_namedFlows.begin(); it != end; ++it) {
63 WebKitNamedFlow* namedFlow = *it;
64 if (namedFlow->getFlowState() == WebKitNamedFlow::FlowStateCreated) {
65 if (!index)
66 return namedFlow;
67 --index;
68 }
69 }
70
71 return 0;
72}
73
74WebKitNamedFlow* WebKitNamedFlowCollection::getFlowByName(const String& flowName)
75{
76 NamedFlowSet::iterator it = m_namedFlows.find<String, NamedFlowHashTranslator>(flowName);
77 if (it == m_namedFlows.end() || (*it)->getFlowState() == WebKitNamedFlow::FlowStateNull)
78 return 0;
79
80 return *it;
81}
82
83PassRefPtr<WebKitNamedFlow> WebKitNamedFlowCollection::ensureNamedFlowInCreatedState(const String& flowName)
84{
85 NamedFlowSet::iterator it = m_namedFlows.find<String, NamedFlowHashTranslator>(flowName);
86 if (it != m_namedFlows.end()) {
87 WebKitNamedFlow* namedFlow = *it;
88 ASSERT(namedFlow->getFlowState() == WebKitNamedFlow::FlowStateNull);
89 if (namedFlow->getFlowState() == WebKitNamedFlow::FlowStateNull) {
90 namedFlow->switchFlowState();
91 ++m_createdFlowsCount;
92 }
93
94 return namedFlow;
95 }
96
97 RefPtr<WebKitNamedFlow> newFlow = WebKitNamedFlow::create(this, flowName);
98 m_namedFlows.add(newFlow.get());
99 ++m_createdFlowsCount;
100
101 return newFlow.release();
102}
103
104void WebKitNamedFlowCollection::moveNamedFlowToNullState(WebKitNamedFlow* namedFlow)
105{
106 ASSERT(namedFlow->getFlowState() == WebKitNamedFlow::FlowStateCreated);
107 ASSERT(m_createdFlowsCount > 0);
108
109 namedFlow->switchFlowState();
110 --m_createdFlowsCount;
111}
112
113void WebKitNamedFlowCollection::discardNamedFlow(WebKitNamedFlow* namedFlow)
114{
115 // The document is not valid anymore so the collection will be destroyed anyway.
116 if (!m_document)
117 return;
118
119 ASSERT(namedFlow->getFlowState() == WebKitNamedFlow::FlowStateNull);
120 ASSERT(m_namedFlows.contains(namedFlow));
121
122 m_namedFlows.remove(namedFlow);
123}
124
125void WebKitNamedFlowCollection::documentDestroyed()
126{
127 m_document = 0;
128}
129
130struct WebKitNamedFlowCollection::NamedFlowHashFunctions {
131 static unsigned hash(WebKitNamedFlow* key) { return DefaultHash<String>::Hash::hash(key->name()); }
132 static bool equal(WebKitNamedFlow* a, WebKitNamedFlow* b) { return a->name() == b->name(); }
133 static const bool safeToCompareToEmptyOrDeleted = true;
134};
135
136struct WebKitNamedFlowCollection::NamedFlowHashTranslator {
137 static unsigned hash(const String& key) { return DefaultHash<String>::Hash::hash(key); }
138 static bool equal(WebKitNamedFlow* a, const String& b) { return a->name() == b; }
139};
140
141} // namespace WebCore