Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / page / HistoryController.cpp
1 /*
2  * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3  * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
4  * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1.  Redistributions of source code must retain the above copyright
11  *     notice, this list of conditions and the following disclaimer.
12  * 2.  Redistributions in binary form must reproduce the above copyright
13  *     notice, this list of conditions and the following disclaimer in the
14  *     documentation and/or other materials provided with the distribution.
15  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
16  *     its contributors may be used to endorse or promote products derived
17  *     from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
20  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "core/page/HistoryController.h"
33
34 #include "core/frame/Frame.h"
35 #include "core/loader/FrameLoader.h"
36 #include "core/page/FrameTree.h"
37 #include "core/page/Page.h"
38 #include "wtf/Deque.h"
39 #include "wtf/text/StringHash.h"
40
41 namespace WebCore {
42
43 PassOwnPtr<HistoryNode> HistoryNode::create(HistoryEntry* entry, HistoryItem* value)
44 {
45     return adoptPtr(new HistoryNode(entry, value));
46 }
47
48 HistoryNode* HistoryNode::addChild(PassRefPtr<HistoryItem> item)
49 {
50     m_children.append(HistoryNode::create(m_entry, item.get()));
51     return m_children.last().get();
52 }
53
54 PassOwnPtr<HistoryNode> HistoryNode::cloneAndReplace(HistoryEntry* newEntry, HistoryItem* newItem, bool clipAtTarget, Frame* targetFrame, Frame* currentFrame)
55 {
56     bool isNodeBeingNavigated = targetFrame == currentFrame;
57     HistoryItem* itemForCreate = isNodeBeingNavigated ? newItem : m_value.get();
58     OwnPtr<HistoryNode> newHistoryNode = create(newEntry, itemForCreate);
59
60     if (!clipAtTarget || !isNodeBeingNavigated) {
61         for (Frame* child = currentFrame->tree().firstChild(); child; child = child->tree().nextSibling()) {
62             HistoryNode* childHistoryNode = m_entry->historyNodeForFrame(child);
63             if (!childHistoryNode)
64                 continue;
65             newHistoryNode->m_children.append(childHistoryNode->cloneAndReplace(newEntry, newItem, clipAtTarget, targetFrame, child));
66         }
67     }
68     return newHistoryNode.release();
69 }
70
71 HistoryNode::HistoryNode(HistoryEntry* entry, HistoryItem* value)
72     : m_entry(entry)
73     , m_value(value)
74 {
75     m_entry->m_framesToItems.add(value->targetFrameID(), this);
76     String target = value->target();
77     if (target.isNull())
78         target = emptyString();
79     m_entry->m_uniqueNamesToItems.add(target, this);
80 }
81
82 void HistoryNode::removeChildren()
83 {
84     // FIXME: This is inefficient. Figure out a cleaner way to ensure this HistoryNode isn't cached anywhere.
85     for (unsigned i = 0; i < m_children.size(); i++) {
86         m_children[i]->removeChildren();
87
88         HashMap<uint64_t, HistoryNode*>::iterator framesEnd = m_entry->m_framesToItems.end();
89         HashMap<String, HistoryNode*>::iterator uniqueNamesEnd = m_entry->m_uniqueNamesToItems.end();
90         for (HashMap<uint64_t, HistoryNode*>::iterator it = m_entry->m_framesToItems.begin(); it != framesEnd; ++it) {
91             if (it->value == m_children[i])
92                 m_entry->m_framesToItems.remove(it);
93         }
94         for (HashMap<String, HistoryNode*>::iterator it = m_entry->m_uniqueNamesToItems.begin(); it != uniqueNamesEnd; ++it) {
95             if (it->value == m_children[i])
96                 m_entry->m_uniqueNamesToItems.remove(it);
97         }
98     }
99     m_children.clear();
100 }
101
102 HistoryEntry::HistoryEntry(HistoryItem* root)
103 {
104     m_root = HistoryNode::create(this, root);
105 }
106
107 PassOwnPtr<HistoryEntry> HistoryEntry::create(HistoryItem* root)
108 {
109     return adoptPtr(new HistoryEntry(root));
110 }
111
112 PassOwnPtr<HistoryEntry> HistoryEntry::cloneAndReplace(HistoryItem* newItem, bool clipAtTarget, Frame* targetFrame, Page* page)
113 {
114     OwnPtr<HistoryEntry> newEntry = adoptPtr(new HistoryEntry());
115     newEntry->m_root = m_root->cloneAndReplace(newEntry.get(), newItem, clipAtTarget, targetFrame, page->mainFrame());
116     return newEntry.release();
117 }
118
119 HistoryNode* HistoryEntry::historyNodeForFrame(Frame* frame)
120 {
121     if (HistoryNode* historyNode = m_framesToItems.get(frame->frameID()))
122         return historyNode;
123     String target = frame->tree().uniqueName();
124     if (target.isNull())
125         target = emptyString();
126     return m_uniqueNamesToItems.get(target);
127 }
128
129 HistoryItem* HistoryEntry::itemForFrame(Frame* frame)
130 {
131     if (HistoryNode* historyNode = historyNodeForFrame(frame))
132         return historyNode->value();
133     return 0;
134 }
135
136 HistoryController::HistoryController(Page* page)
137     : m_page(page)
138     , m_defersLoading(false)
139     , m_deferredCachePolicy(UseProtocolCachePolicy)
140 {
141 }
142
143 HistoryController::~HistoryController()
144 {
145 }
146
147 void HistoryController::updateBackForwardListForFragmentScroll(Frame* frame, HistoryItem* item)
148 {
149     createNewBackForwardItem(frame, item, false);
150 }
151
152 void HistoryController::goToEntry(PassOwnPtr<HistoryEntry> targetEntry, ResourceRequestCachePolicy cachePolicy)
153 {
154     HistoryFrameLoadSet sameDocumentLoads;
155     HistoryFrameLoadSet differentDocumentLoads;
156
157     m_provisionalEntry = targetEntry;
158     if (m_currentEntry)
159         recursiveGoToEntry(m_page->mainFrame(), sameDocumentLoads, differentDocumentLoads);
160     else
161         differentDocumentLoads.set(m_page->mainFrame(), m_provisionalEntry->root());
162
163     if (sameDocumentLoads.isEmpty() && differentDocumentLoads.isEmpty())
164         sameDocumentLoads.set(m_page->mainFrame(), m_provisionalEntry->root());
165
166     if (differentDocumentLoads.isEmpty()) {
167         m_previousEntry = m_currentEntry.release();
168         m_currentEntry = m_provisionalEntry.release();
169     }
170
171     for (HistoryFrameLoadSet::iterator it = sameDocumentLoads.begin(); it != sameDocumentLoads.end(); ++it) {
172         if (it->key->host())
173             it->key->loader().loadHistoryItem(it->value.get(), HistorySameDocumentLoad, cachePolicy);
174     }
175     for (HistoryFrameLoadSet::iterator it = differentDocumentLoads.begin(); it != differentDocumentLoads.end(); ++it) {
176         if (it->key->host())
177             it->key->loader().loadHistoryItem(it->value.get(), HistoryDifferentDocumentLoad, cachePolicy);
178     }
179 }
180
181 void HistoryController::recursiveGoToEntry(Frame* frame, HistoryFrameLoadSet& sameDocumentLoads, HistoryFrameLoadSet& differentDocumentLoads)
182 {
183     ASSERT(m_provisionalEntry);
184     ASSERT(m_currentEntry);
185     HistoryItem* newItem = m_provisionalEntry->itemForFrame(frame);
186     HistoryItem* oldItem = m_currentEntry->itemForFrame(frame);
187     if (!newItem)
188         return;
189
190     if (!oldItem || (newItem != oldItem && newItem->itemSequenceNumber() != oldItem->itemSequenceNumber())) {
191         if (oldItem && newItem->documentSequenceNumber() == oldItem->documentSequenceNumber())
192             sameDocumentLoads.set(frame, newItem);
193         else
194             differentDocumentLoads.set(frame, newItem);
195         return;
196     }
197
198     for (Frame* child = frame->tree().firstChild(); child; child = child->tree().nextSibling())
199         recursiveGoToEntry(child, sameDocumentLoads, differentDocumentLoads);
200 }
201
202 void HistoryController::goToItem(HistoryItem* targetItem, ResourceRequestCachePolicy cachePolicy)
203 {
204     if (m_defersLoading) {
205         m_deferredItem = targetItem;
206         m_deferredCachePolicy = cachePolicy;
207         return;
208     }
209
210     OwnPtr<HistoryEntry> newEntry = HistoryEntry::create(targetItem);
211     Deque<HistoryNode*> historyNodes;
212     historyNodes.append(newEntry->rootHistoryNode());
213     while (!historyNodes.isEmpty()) {
214         // For each item, read the children (if any) off the HistoryItem,
215         // create a new HistoryNode for each child and attach it,
216         // then clear the children on the HistoryItem.
217         HistoryNode* historyNode = historyNodes.takeFirst();
218         const HistoryItemVector& children = historyNode->value()->children();
219         for (size_t i = 0; i < children.size(); i++) {
220             HistoryNode* childHistoryNode = historyNode->addChild(children[i].get());
221             historyNodes.append(childHistoryNode);
222         }
223         historyNode->value()->clearChildren();
224     }
225     goToEntry(newEntry.release(), cachePolicy);
226 }
227
228 void HistoryController::setDefersLoading(bool defer)
229 {
230     m_defersLoading = defer;
231     if (!defer && m_deferredItem) {
232         goToItem(m_deferredItem.get(), m_deferredCachePolicy);
233         m_deferredItem = 0;
234         m_deferredCachePolicy = UseProtocolCachePolicy;
235     }
236 }
237
238 void HistoryController::updateForInitialLoadInChildFrame(Frame* frame, HistoryItem* item)
239 {
240     ASSERT(frame->tree().parent());
241     if (!m_currentEntry)
242         return;
243     if (HistoryNode* existingNode = m_currentEntry->historyNodeForFrame(frame))
244         existingNode->updateValue(item);
245     else if (HistoryNode* parentHistoryNode = m_currentEntry->historyNodeForFrame(frame->tree().parent()))
246         parentHistoryNode->addChild(item);
247 }
248
249 void HistoryController::updateForCommit(Frame* frame, HistoryItem* item, HistoryCommitType commitType)
250 {
251     if (commitType == BackForwardCommit) {
252         if (!m_provisionalEntry)
253             return;
254         // Once committed, we want to use current item for saving DocState, and
255         // the provisional item for restoring state.
256         // Note previousItem must be set before we close the URL, which will
257         // happen when the data source is made non-provisional below
258         m_previousEntry = m_currentEntry.release();
259         ASSERT(m_provisionalEntry);
260         m_currentEntry = m_provisionalEntry.release();
261     } else if (commitType == StandardCommit) {
262         createNewBackForwardItem(frame, item, true);
263     } else if (commitType == InitialCommitInChildFrame) {
264         updateForInitialLoadInChildFrame(frame, item);
265     }
266 }
267
268 static PassRefPtr<HistoryItem> itemForExport(HistoryNode* historyNode)
269 {
270     ASSERT(historyNode);
271     RefPtr<HistoryItem> item = historyNode->value()->copy();
272     const Vector<OwnPtr<HistoryNode> >& childEntries = historyNode->children();
273     for (size_t i = 0; i < childEntries.size(); i++)
274         item->addChildItem(itemForExport(childEntries[i].get()));
275     return item;
276 }
277
278 PassRefPtr<HistoryItem> HistoryController::currentItemForExport()
279 {
280     if (!m_currentEntry)
281         return 0;
282     return itemForExport(m_currentEntry->rootHistoryNode());
283 }
284
285 PassRefPtr<HistoryItem> HistoryController::previousItemForExport()
286 {
287     if (!m_previousEntry)
288         return 0;
289     return itemForExport(m_previousEntry->rootHistoryNode());
290 }
291
292 HistoryItem* HistoryController::itemForNewChildFrame(Frame* frame) const
293 {
294     return m_currentEntry ? m_currentEntry->itemForFrame(frame) : 0;
295 }
296
297 void HistoryController::removeChildrenForRedirect(Frame* frame)
298 {
299     if (!m_provisionalEntry)
300         return;
301     if (HistoryNode* node = m_provisionalEntry->historyNodeForFrame(frame))
302         node->removeChildren();
303 }
304
305 void HistoryController::createNewBackForwardItem(Frame* targetFrame, HistoryItem* item, bool clipAtTarget)
306 {
307     RefPtr<HistoryItem> newItem = item;
308     if (!m_currentEntry) {
309         m_currentEntry = HistoryEntry::create(newItem.get());
310     } else {
311         HistoryItem* oldItem = m_currentEntry->itemForFrame(targetFrame);
312         if (!clipAtTarget && oldItem)
313             newItem->setDocumentSequenceNumber(oldItem->documentSequenceNumber());
314         m_previousEntry = m_currentEntry.release();
315         m_currentEntry = m_previousEntry->cloneAndReplace(newItem.get(), clipAtTarget, targetFrame, m_page);
316     }
317 }
318
319 } // namespace WebCore