Unreviewed, rolling out r126836.
[WebKit-https.git] / Source / WebCore / rendering / FlowThreadController.cpp
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
32 #include "FlowThreadController.h"
33
34 #include "NamedFlowCollection.h"
35 #include "RenderFlowThread.h"
36 #include "RenderFlowThreadContainer.h"
37 #include "RenderNamedFlowThread.h"
38 #include "StyleInheritedData.h"
39 #include "WebKitNamedFlow.h"
40 #include <wtf/text/AtomicString.h>
41
42 namespace WebCore {
43
44 PassOwnPtr<FlowThreadController> FlowThreadController::create(RenderView* view)
45 {
46     return adoptPtr(new FlowThreadController(view));
47 }
48
49 FlowThreadController::FlowThreadController(RenderView* view)
50     : m_view(view)
51     , m_currentRenderFlowThread(0)
52     , m_flowThreadContainer(0)
53     , m_isRenderNamedFlowThreadOrderDirty(false)
54 {
55 }
56
57 FlowThreadController::~FlowThreadController()
58 {
59 }
60
61 RenderNamedFlowThread* FlowThreadController::ensureRenderFlowThreadWithName(const AtomicString& name)
62 {
63     if (!m_flowThreadContainer) {
64         m_flowThreadContainer = new (m_view->renderArena()) RenderFlowThreadContainer(m_view->document());
65         m_flowThreadContainer->setStyle(RenderFlowThread::createFlowThreadStyle(m_view->style()));
66         m_view->addChild(m_flowThreadContainer);
67     }
68     if (!m_renderNamedFlowThreadList)
69         m_renderNamedFlowThreadList = adoptPtr(new RenderNamedFlowThreadList());
70     else {
71         for (RenderNamedFlowThreadList::iterator iter = m_renderNamedFlowThreadList->begin(); iter != m_renderNamedFlowThreadList->end(); ++iter) {
72             RenderNamedFlowThread* flowRenderer = *iter;
73             if (flowRenderer->flowThreadName() == name)
74                 return flowRenderer;
75         }
76     }
77
78     NamedFlowCollection* namedFlows = m_view->document()->namedFlows();
79
80     // Sanity check for the absence of a named flow in the "CREATED" state with the same name.
81     ASSERT(!namedFlows->flowByName(name));
82
83     RenderNamedFlowThread* flowRenderer = new (m_view->renderArena()) RenderNamedFlowThread(m_view->document(), namedFlows->ensureFlowWithName(name));
84     flowRenderer->setStyle(RenderFlowThread::createFlowThreadStyle(m_view->style()));
85     m_renderNamedFlowThreadList->add(flowRenderer);
86
87     // Keep the flow renderer as a child of RenderFlowThreadContainer.
88     m_flowThreadContainer->addChild(flowRenderer);
89
90     setIsRenderNamedFlowThreadOrderDirty(true);
91
92     return flowRenderer;
93 }
94
95 void FlowThreadController::styleDidChange()
96 {
97     RenderStyle* viewStyle = m_view->style();
98     for (RenderNamedFlowThreadList::iterator iter = m_renderNamedFlowThreadList->begin(); iter != m_renderNamedFlowThreadList->end(); ++iter) {
99         RenderNamedFlowThread* flowRenderer = *iter;
100         flowRenderer->setStyle(RenderFlowThread::createFlowThreadStyle(viewStyle));
101     }
102 }
103
104 void FlowThreadController::layoutRenderNamedFlowThreads()
105 {
106     ASSERT(m_renderNamedFlowThreadList);
107
108     if (isRenderNamedFlowThreadOrderDirty()) {
109         // Arrange the thread list according to dependencies.
110         RenderNamedFlowThreadList sortedList;
111         for (RenderNamedFlowThreadList::iterator iter = m_renderNamedFlowThreadList->begin(); iter != m_renderNamedFlowThreadList->end(); ++iter) {
112             RenderNamedFlowThread* flowRenderer = *iter;
113             if (sortedList.contains(flowRenderer))
114                 continue;
115             flowRenderer->pushDependencies(sortedList);
116             sortedList.add(flowRenderer);
117         }
118         m_renderNamedFlowThreadList->swap(sortedList);
119         setIsRenderNamedFlowThreadOrderDirty(false);
120     }
121
122     for (RenderNamedFlowThreadList::iterator iter = m_renderNamedFlowThreadList->begin(); iter != m_renderNamedFlowThreadList->end(); ++iter) {
123         RenderNamedFlowThread* flowRenderer = *iter;
124         flowRenderer->layoutIfNeeded();
125     }
126 }
127
128 void FlowThreadController::registerNamedFlowContentNode(Node* contentNode, RenderNamedFlowThread* namedFlow)
129 {
130     ASSERT(contentNode && contentNode->isElementNode());
131     ASSERT(namedFlow);
132     ASSERT(!m_mapNamedFlowContentNodes.contains(contentNode));
133     ASSERT(!namedFlow->hasContentNode(contentNode));
134     m_mapNamedFlowContentNodes.add(contentNode, namedFlow);
135     namedFlow->registerNamedFlowContentNode(contentNode);
136 }
137
138 void FlowThreadController::unregisterNamedFlowContentNode(Node* contentNode)
139 {
140     ASSERT(contentNode && contentNode->isElementNode());
141     HashMap<Node*, RenderNamedFlowThread*>::iterator it = m_mapNamedFlowContentNodes.find(contentNode);
142     ASSERT(it != m_mapNamedFlowContentNodes.end());
143     ASSERT(it->second);
144     ASSERT(it->second->hasContentNode(contentNode));
145     it->second->unregisterNamedFlowContentNode(contentNode);
146     m_mapNamedFlowContentNodes.remove(contentNode);
147 }
148
149 void FlowThreadController::removeFlowThread(RenderNamedFlowThread* flowThread)
150 {
151     m_renderNamedFlowThreadList->remove(flowThread);
152     setIsRenderNamedFlowThreadOrderDirty(true);
153 }
154
155 } // namespace WebCore