blob: 622797ceb17e2d4b325602ab30d723ea2ec96445 [file] [log] [blame]
commit-queue@webkit.org5323f762012-07-13 10:01:38 +00001/*
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"
rniwa@webkit.org3fc452e2012-08-24 21:46:24 +000031#include "NamedFlowCollection.h"
commit-queue@webkit.org5323f762012-07-13 10:01:38 +000032
rniwa@webkit.org3fc452e2012-08-24 21:46:24 +000033#include "DOMNamedFlowCollection.h"
commit-queue@webkit.org5323f762012-07-13 10:01:38 +000034#include "Document.h"
commit-queue@webkit.org5c25fc42012-08-06 16:39:56 +000035#include "InspectorInstrumentation.h"
commit-queue@webkit.org5323f762012-07-13 10:01:38 +000036
37#include <wtf/text/StringHash.h>
commit-queue@webkit.org5323f762012-07-13 10:01:38 +000038
39namespace WebCore {
40
commit-queue@webkit.org9aa692e2013-02-08 04:59:23 +000041NamedFlowCollection::NamedFlowCollection(Document* document)
42 : ContextDestructionObserver(document)
commit-queue@webkit.org5323f762012-07-13 10:01:38 +000043{
44}
45
andersca@apple.comc3523f82013-10-18 23:41:24 +000046Vector<RefPtr<WebKitNamedFlow>> NamedFlowCollection::namedFlows()
commit-queue@webkit.org6f859512012-07-20 12:34:58 +000047{
andersca@apple.comc3523f82013-10-18 23:41:24 +000048 Vector<RefPtr<WebKitNamedFlow>> namedFlows;
commit-queue@webkit.org6f859512012-07-20 12:34:58 +000049
50 for (NamedFlowSet::iterator it = m_namedFlows.begin(); it != m_namedFlows.end(); ++it) {
51 if ((*it)->flowState() == WebKitNamedFlow::FlowStateNull)
52 continue;
53
commit-queue@webkit.orge2bb32c2012-08-10 09:58:35 +000054 namedFlows.append(RefPtr<WebKitNamedFlow>(*it));
commit-queue@webkit.org6f859512012-07-20 12:34:58 +000055 }
56
57 return namedFlows;
58}
59
rniwa@webkit.org3fc452e2012-08-24 21:46:24 +000060WebKitNamedFlow* NamedFlowCollection::flowByName(const String& flowName)
commit-queue@webkit.org5323f762012-07-13 10:01:38 +000061{
62 NamedFlowSet::iterator it = m_namedFlows.find<String, NamedFlowHashTranslator>(flowName);
63 if (it == m_namedFlows.end() || (*it)->flowState() == WebKitNamedFlow::FlowStateNull)
mihnea@adobe.com7efa55e2014-09-22 14:47:53 +000064 return nullptr;
commit-queue@webkit.org5323f762012-07-13 10:01:38 +000065
66 return *it;
67}
68
akling@apple.com689f7612014-12-14 08:21:05 +000069Ref<WebKitNamedFlow> NamedFlowCollection::ensureFlowWithName(const String& flowName)
commit-queue@webkit.org5323f762012-07-13 10:01:38 +000070{
71 NamedFlowSet::iterator it = m_namedFlows.find<String, NamedFlowHashTranslator>(flowName);
72 if (it != m_namedFlows.end()) {
73 WebKitNamedFlow* namedFlow = *it;
74 ASSERT(namedFlow->flowState() == WebKitNamedFlow::FlowStateNull);
75
akling@apple.coma3c59842014-02-12 00:08:16 +000076 return *namedFlow;
commit-queue@webkit.org5323f762012-07-13 10:01:38 +000077 }
78
79 RefPtr<WebKitNamedFlow> newFlow = WebKitNamedFlow::create(this, flowName);
80 m_namedFlows.add(newFlow.get());
81
burg@cs.washington.edudbacfc12015-01-05 21:30:33 +000082 InspectorInstrumentation::didCreateNamedFlow(document(), *newFlow);
commit-queue@webkit.org5c25fc42012-08-06 16:39:56 +000083
akling@apple.coma3c59842014-02-12 00:08:16 +000084 return newFlow.releaseNonNull();
commit-queue@webkit.org5323f762012-07-13 10:01:38 +000085}
86
rniwa@webkit.org3fc452e2012-08-24 21:46:24 +000087void NamedFlowCollection::discardNamedFlow(WebKitNamedFlow* namedFlow)
commit-queue@webkit.org5323f762012-07-13 10:01:38 +000088{
89 // The document is not valid anymore so the collection will be destroyed anyway.
commit-queue@webkit.org9aa692e2013-02-08 04:59:23 +000090 if (!document())
commit-queue@webkit.org5323f762012-07-13 10:01:38 +000091 return;
92
93 ASSERT(namedFlow->flowState() == WebKitNamedFlow::FlowStateNull);
94 ASSERT(m_namedFlows.contains(namedFlow));
95
burg@cs.washington.edudbacfc12015-01-05 21:30:33 +000096 InspectorInstrumentation::willRemoveNamedFlow(document(), *namedFlow);
apavlov@chromium.org5aecfe92012-09-07 14:00:11 +000097
commit-queue@webkit.org65563d42012-09-12 12:07:24 +000098 m_namedFlows.remove(namedFlow);
commit-queue@webkit.org5323f762012-07-13 10:01:38 +000099}
100
commit-queue@webkit.org9aa692e2013-02-08 04:59:23 +0000101Document* NamedFlowCollection::document() const
commit-queue@webkit.org5323f762012-07-13 10:01:38 +0000102{
commit-queue@webkit.org9aa692e2013-02-08 04:59:23 +0000103 ScriptExecutionContext* context = ContextDestructionObserver::scriptExecutionContext();
cdumez@apple.comb69c7942014-09-29 22:23:20 +0000104 return downcast<Document>(context);
commit-queue@webkit.org5323f762012-07-13 10:01:38 +0000105}
commit-queue@webkit.org9aa692e2013-02-08 04:59:23 +0000106
rniwa@webkit.org3fc452e2012-08-24 21:46:24 +0000107PassRefPtr<DOMNamedFlowCollection> NamedFlowCollection::createCSSOMSnapshot()
108{
commit-queue@webkit.orgd62fc832012-09-12 16:30:04 +0000109 Vector<WebKitNamedFlow*> createdFlows;
rniwa@webkit.org3fc452e2012-08-24 21:46:24 +0000110 for (NamedFlowSet::iterator it = m_namedFlows.begin(); it != m_namedFlows.end(); ++it)
111 if ((*it)->flowState() == WebKitNamedFlow::FlowStateCreated)
commit-queue@webkit.orgd62fc832012-09-12 16:30:04 +0000112 createdFlows.append(*it);
rniwa@webkit.org3fc452e2012-08-24 21:46:24 +0000113 return DOMNamedFlowCollection::create(createdFlows);
114}
commit-queue@webkit.org5323f762012-07-13 10:01:38 +0000115
commit-queue@webkit.orgd62fc832012-09-12 16:30:04 +0000116// 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.
118struct 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;
122};
123
124// The HashTranslator is used to lookup a NamedFlow in the set using a name.
125struct 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; }
128};
129
commit-queue@webkit.org5323f762012-07-13 10:01:38 +0000130} // namespace WebCore