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 ASSERT(m_createdFlowsCount >= 0);
59
60 if (index >= m_createdFlowsCount)
61 return 0;
62
63 NamedFlowSet::const_iterator it = m_namedFlows.begin();
64 NamedFlowSet::const_iterator end = m_namedFlows.end();
65
66 while (it != end) {
67 WebKitNamedFlow* namedFlow = *it;
68 if (namedFlow->getFlowState() == WebKitNamedFlow::STATE_CREATED) {
69 if (!index)
70 return namedFlow;
71 --index;
72 }
73
74 ++it;
75 }
76
77 return 0;
78}
79
80WebKitNamedFlow* WebKitNamedFlowCollection::getFlowByName(const String& flowName)
81{
82 ASSERT(m_createdFlowsCount >= 0);
83
84 NamedFlowSet::iterator it = m_namedFlows.find<String, NamedFlowHashTranslator>(flowName);
85 if (it == m_namedFlows.end() || (*it)->getFlowState() == WebKitNamedFlow::STATE_NULL)
86 return 0;
87
88 return *it;
89}
90
91PassRefPtr<WebKitNamedFlow> WebKitNamedFlowCollection::ensureNamedFlowInCreatedState(const String& flowName)
92{
93 ++m_createdFlowsCount;
94
95 NamedFlowSet::iterator it = m_namedFlows.find<String, NamedFlowHashTranslator>(flowName);
96 if (it != m_namedFlows.end()) {
97 WebKitNamedFlow* namedFlow = *it;
98 ASSERT(namedFlow->getFlowState() == WebKitNamedFlow::STATE_NULL);
99 namedFlow->switchFlowState();
100
101 return namedFlow;
102 }
103
104 RefPtr<WebKitNamedFlow> newFlow = WebKitNamedFlow::create(this, flowName);
105 m_namedFlows.add(newFlow.get());
106
107 return newFlow.release();
108}
109
110void WebKitNamedFlowCollection::moveNamedFlowToNullState(WebKitNamedFlow* namedFlow)
111{
112 ASSERT(namedFlow->getFlowState() == WebKitNamedFlow::STATE_CREATED);
113 ASSERT(m_createdFlowsCount > 0);
114
115 namedFlow->switchFlowState();
116 --m_createdFlowsCount;
117}
118
119void WebKitNamedFlowCollection::discardNamedFlow(WebKitNamedFlow* namedFlow)
120{
121 // The document is not valid anymore so the collection will be destroyed anyway.
122 if (!m_document)
123 return;
124
125 ASSERT(namedFlow->getFlowState() == WebKitNamedFlow::STATE_NULL);
126 ASSERT(m_namedFlows.contains(namedFlow));
127
128 m_namedFlows.remove(namedFlow);
129}
130
131void WebKitNamedFlowCollection::documentDestroyed()
132{
133 m_document = 0;
134}
135
136struct WebKitNamedFlowCollection::NamedFlowHashFunctions {
137 static unsigned hash(WebKitNamedFlow* key) { return DefaultHash<String>::Hash::hash(key->name()); }
138 static bool equal(WebKitNamedFlow* a, WebKitNamedFlow* b) { return a->name() == b->name(); }
139 static const bool safeToCompareToEmptyOrDeleted = true;
140};
141
142struct WebKitNamedFlowCollection::NamedFlowHashTranslator {
143 static unsigned hash(const String& key) { return DefaultHash<String>::Hash::hash(key); }
144 static bool equal(WebKitNamedFlow* a, const String& b) { return a->name() == b; }
145};
146
147} // namespace WebCore