[CSS Regions] Fix the lifecycle for the flow objects and their renderers
https://bugs.webkit.org/show_bug.cgi?id=89000

Patch by Andrei Bucur <abucur@adobe.com> on 2012-07-13
Reviewed by Eric Seidel.

Source/WebCore:

This patch adds the concept of a NamedFlowCollection, owned by the document, that keeps track of
all the named flows that exist in the Document. This collection contains a ListHashSet of weak references to
all the existing NamedFlows in the document. This is not a managed set because the CREATED flows are referenced from the renderer and
the NULL flows are only cached, they should be destructible.
Two named flows are considered to be equal if they have the same name.
I've changed the NamedFlow state to depend on the existence of its renderer. A flow thread that has a renderer will also have a NamedFlow object.
A flow thread without a renderer can have a NamedFlow object, but only in the NULL state. It's possible for a NamedFlow object to jump from the
NULL state to the CREATED state if it was not destroyed (e.g. it was referenced from JS). Keeping track of the NULL state flows that have listeners will be important
so when they go back to the CREATED state, the listeners would still be there.

Link to spec: http://www.w3.org/TR/2012/WD-css3-regions-20120503/

Tests: The old tests have been modified to take into account the new behavior

* CMakeLists.txt:
* GNUmakefile.list.am:
* Target.pri:
* WebCore.gypi:
* WebCore.vcproj/WebCore.vcproj:
* WebCore.xcodeproj/project.pbxproj:
* dom/DOMAllInOne.cpp:
* dom/Document.cpp:
(WebCore::Document::~Document):
(WebCore):
(WebCore::Document::webkitGetFlowByName):
(WebCore::Document::namedFlows):
* dom/Document.h:
(WebCore):
(Document):
* dom/WebKitNamedFlow.cpp:
(WebCore::WebKitNamedFlow::WebKitNamedFlow):
(WebCore::WebKitNamedFlow::~WebKitNamedFlow):
(WebCore::WebKitNamedFlow::create):
(WebCore):
(WebCore::WebKitNamedFlow::name):
(WebCore::WebKitNamedFlow::overset):
(WebCore::nodeInFlowThread):
(WebCore::WebKitNamedFlow::getRegionsByContentNode):
(WebCore::WebKitNamedFlow::getContent):
(WebCore::WebKitNamedFlow::setRenderer):
* dom/WebKitNamedFlow.h:
(WebCore):
(WebKitNamedFlow):
(WebCore::WebKitNamedFlow::getFlowState):
(WebCore::WebKitNamedFlow::switchFlowState):
* dom/WebKitNamedFlowCollection.cpp: Added.
(WebCore):
(WebCore::WebKitNamedFlowCollection::WebKitNamedFlowCollection):
(WebCore::WebKitNamedFlowCollection::length): An O(1) operation
(WebCore::WebKitNamedFlowCollection::item): An O(N) operation
(WebCore::WebKitNamedFlowCollection::flowByName): An O(1) operation
(WebCore::WebKitNamedFlowCollection::ensureNamedFlowInCreatedState): An O(1) operation
(WebCore::WebKitNamedFlowCollection::moveNamedFlowToNullState): An O(1) operation
(WebCore::WebKitNamedFlowCollection::discardNamedFlow): An O(1) operation
(WebCore::WebKitNamedFlowCollection::documentDestroyed):
(WebCore::WebKitNamedFlowCollection::NamedFlowHashFunctions::hash):
(WebCore::WebKitNamedFlowCollection::NamedFlowHashFunctions::equal):
(WebKitNamedFlowCollection::NamedFlowHashFunctions):
(WebCore::WebKitNamedFlowCollection::NamedFlowHashTranslator::hash):
(WebCore::WebKitNamedFlowCollection::NamedFlowHashTranslator::equal):
* dom/WebKitNamedFlowCollection.h: Copied from Source/WebCore/dom/WebKitNamedFlow.h.
(WebCore):
(WebKitNamedFlowCollection):
(WebCore::WebKitNamedFlowCollection::create):
(WebCore::WebKitNamedFlowCollection::document):
* rendering/FlowThreadController.cpp:
(WebCore::FlowThreadController::ensureRenderFlowThreadWithName):
(WebCore::FlowThreadController::removeFlowThread):
(WebCore):
* rendering/FlowThreadController.h:
(FlowThreadController):
* rendering/RenderNamedFlowThread.cpp:
(WebCore::RenderNamedFlowThread::RenderNamedFlowThread):
(WebCore):
(WebCore::RenderNamedFlowThread::~RenderNamedFlowThread):
(WebCore::RenderNamedFlowThread::removeRegionFromThread):
(WebCore::RenderNamedFlowThread::unregisterNamedFlowContentNode):
(WebCore::RenderNamedFlowThread::flowThreadName):
(WebCore::RenderNamedFlowThread::willBeDestroyed):
* rendering/RenderNamedFlowThread.h:
(RenderNamedFlowThread):
(WebCore::RenderNamedFlowThread::contentNodes):
(WebCore::RenderNamedFlowThread::canBeDestroyed):

LayoutTests:

These tests expected getFlowByName() to return a valid object even for the NULL flows. They've been modified to
reflect the new behavior.

* fast/regions/get-regions-by-content-node2-expected.txt:
* fast/regions/get-regions-by-content-node2.html:
* fast/regions/webkit-named-flow-flow-added.html:
* fast/regions/webkit-named-flow-get-content-expected.txt:
* fast/regions/webkit-named-flow-get-content.html:
* fast/regions/webkit-named-flow-invalid-name.html:
* fast/regions/webkit-named-flow-modified-flow.html:
* fast/regions/webkit-named-flow-non-existing-flow.html:
* fast/regions/webkit-named-flow-removed-flow.html:
* platform/chromium-win/fast/regions/region-style-block-background-color2-expected.txt:
* platform/mac/fast/regions/region-style-block-background-color2-expected.txt:

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@122556 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/dom/WebKitNamedFlowCollection.cpp b/Source/WebCore/dom/WebKitNamedFlowCollection.cpp
new file mode 100644
index 0000000..b0cff11
--- /dev/null
+++ b/Source/WebCore/dom/WebKitNamedFlowCollection.cpp
@@ -0,0 +1,102 @@
+/*
+ * Copyright (C) 2012 Adobe Systems Incorporated. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above
+ *    copyright notice, this list of conditions and the following
+ *    disclaimer.
+ * 2. Redistributions in binary form must reproduce the above
+ *    copyright notice, this list of conditions and the following
+ *    disclaimer in the documentation and/or other materials
+ *    provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
+ * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
+ * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "WebKitNamedFlowCollection.h"
+
+#include "Document.h"
+#include "WebKitNamedFlow.h"
+
+#include <wtf/text/StringHash.h>
+#include <wtf/text/WTFString.h>
+
+namespace WebCore {
+
+WebKitNamedFlowCollection::WebKitNamedFlowCollection(Document* doc)
+    : m_document(doc)
+{
+}
+
+WebKitNamedFlow* WebKitNamedFlowCollection::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;
+}
+
+PassRefPtr<WebKitNamedFlow> WebKitNamedFlowCollection::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());
+
+    return newFlow.release();
+}
+
+void WebKitNamedFlowCollection::discardNamedFlow(WebKitNamedFlow* namedFlow)
+{
+    // The document is not valid anymore so the collection will be destroyed anyway.
+    if (!m_document)
+        return;
+
+    ASSERT(namedFlow->flowState() == WebKitNamedFlow::FlowStateNull);
+    ASSERT(m_namedFlows.contains(namedFlow));
+
+    m_namedFlows.remove(namedFlow);
+}
+
+void WebKitNamedFlowCollection::documentDestroyed()
+{
+    m_document = 0;
+}
+
+// The HashFunctions object used by the HashSet to compare between NamedFlows.
+// It is safe to set safeToCompareToEmptyOrDeleted because the HashSet will never contain null pointers or deleted values.
+struct WebKitNamedFlowCollection::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;
+};
+
+// The HashTranslator is used to lookup a NamedFlow in the set using a name.
+struct WebKitNamedFlowCollection::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; }
+};
+
+} // namespace WebCore