1 /*
2 * Copyright (C) 2009 Apple Inc. 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 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "CachedPage.h"
28
29 #include "CachedFramePlatformData.h"
30 #include "DocumentLoader.h"
31 #include "ExceptionCode.h"
32 #include "EventNames.h"
33 #include "FocusController.h"
34 #include "Frame.h"
35 #include "FrameLoaderClient.h"
36 #include "FrameView.h"
37 #include "HistoryItem.h"
38 #include "Logging.h"
39 #include "PageTransitionEvent.h"
40 #include <wtf/text/CString.h>
41 #include <wtf/RefCountedLeakCounter.h>
42
43 #if ENABLE(SVG)
44 #include "SVGDocumentExtensions.h"
45 #endif
46
47 #if ENABLE(TOUCH_EVENTS)
48 #include "Chrome.h"
49 #include "ChromeClient.h"
50 #include "Page.h"
51 #endif
52
53 namespace WebCore {
54
55 #ifndef NDEBUG
cachedFrameCounter()56 static WTF::RefCountedLeakCounter& cachedFrameCounter()
57 {
58 DEFINE_STATIC_LOCAL(WTF::RefCountedLeakCounter, counter, ("CachedFrame"));
59 return counter;
60 }
61 #endif
62
CachedFrameBase(Frame * frame)63 CachedFrameBase::CachedFrameBase(Frame* frame)
64 : m_document(frame->document())
65 , m_documentLoader(frame->loader()->documentLoader())
66 , m_view(frame->view())
67 , m_mousePressNode(frame->eventHandler()->mousePressNode())
68 , m_url(frame->document()->url())
69 , m_isMainFrame(!frame->tree()->parent())
70 {
71 }
72
~CachedFrameBase()73 CachedFrameBase::~CachedFrameBase()
74 {
75 #ifndef NDEBUG
76 cachedFrameCounter().decrement();
77 #endif
78 // CachedFrames should always have had destroy() called by their parent CachedPage
79 ASSERT(!m_document);
80 }
81
restore()82 void CachedFrameBase::restore()
83 {
84 ASSERT(m_document->view() == m_view);
85
86 if (m_isMainFrame)
87 m_view->setParentVisible(true);
88
89 Frame* frame = m_view->frame();
90 m_cachedFrameScriptData->restore(frame);
91
92 #if ENABLE(SVG)
93 if (m_document->svgExtensions())
94 m_document->accessSVGExtensions()->unpauseAnimations();
95 #endif
96
97 frame->animation()->resumeAnimationsForDocument(m_document.get());
98 frame->eventHandler()->setMousePressNode(m_mousePressNode.get());
99 m_document->resumeActiveDOMObjects();
100 m_document->resumeScriptedAnimationControllerCallbacks();
101
102 // It is necessary to update any platform script objects after restoring the
103 // cached page.
104 frame->script()->updatePlatformScriptObjects();
105
106 frame->loader()->client()->didRestoreFromPageCache();
107
108 // Reconstruct the FrameTree
109 for (unsigned i = 0; i < m_childFrames.size(); ++i)
110 frame->tree()->appendChild(m_childFrames[i]->view()->frame());
111
112 // Open the child CachedFrames in their respective FrameLoaders.
113 for (unsigned i = 0; i < m_childFrames.size(); ++i)
114 m_childFrames[i]->open();
115
116 m_document->enqueuePageshowEvent(PageshowEventPersisted);
117
118 HistoryItem* historyItem = frame->loader()->history()->currentItem();
119 m_document->enqueuePopstateEvent(historyItem && historyItem->stateObject() ? historyItem->stateObject() : SerializedScriptValue::nullValue());
120
121 #if ENABLE(TOUCH_EVENTS)
122 if (m_document->hasListenerType(Document::TOUCH_LISTENER))
123 m_document->page()->chrome()->client()->needTouchEvents(true);
124 #endif
125
126 m_document->documentDidBecomeActive();
127 }
128
CachedFrame(Frame * frame)129 CachedFrame::CachedFrame(Frame* frame)
130 : CachedFrameBase(frame)
131 {
132 #ifndef NDEBUG
133 cachedFrameCounter().increment();
134 #endif
135 ASSERT(m_document);
136 ASSERT(m_documentLoader);
137 ASSERT(m_view);
138
139 if (frame->page()->focusController()->focusedFrame() == frame)
140 frame->page()->focusController()->setFocusedFrame(frame->page()->mainFrame());
141
142 // Custom scrollbar renderers will get reattached when the document comes out of the page cache
143 m_view->detachCustomScrollbars();
144
145 m_document->documentWillBecomeInactive();
146 frame->clearTimers();
147 m_document->setInPageCache(true);
148 frame->loader()->stopLoading(UnloadEventPolicyUnloadAndPageHide);
149
150 // Create the CachedFrames for all Frames in the FrameTree.
151 for (Frame* child = frame->tree()->firstChild(); child; child = child->tree()->nextSibling())
152 m_childFrames.append(CachedFrame::create(child));
153
154 // Active DOM objects must be suspended before we cache the frame script data,
155 // but after we've fired the pagehide event, in case that creates more objects.
156 // Suspending must also happen after we've recursed over child frames, in case
157 // those create more objects.
158 // FIXME: It's still possible to have objects created after suspending in some cases, see http://webkit.org/b/53733 for more details.
159 m_document->suspendScriptedAnimationControllerCallbacks();
160 m_document->suspendActiveDOMObjects(ActiveDOMObject::DocumentWillBecomeInactive);
161 m_cachedFrameScriptData = adoptPtr(new ScriptCachedFrameData(frame));
162
163 frame->loader()->client()->savePlatformDataToCachedFrame(this);
164
165 // Deconstruct the FrameTree, to restore it later.
166 // We do this for two reasons:
167 // 1 - We reuse the main frame, so when it navigates to a new page load it needs to start with a blank FrameTree.
168 // 2 - It's much easier to destroy a CachedFrame while it resides in the PageCache if it is disconnected from its parent.
169 for (unsigned i = 0; i < m_childFrames.size(); ++i)
170 frame->tree()->removeChild(m_childFrames[i]->view()->frame());
171
172 if (!m_isMainFrame)
173 frame->page()->decrementFrameCount();
174
175 frame->loader()->client()->didSaveToPageCache();
176
177 #ifndef NDEBUG
178 if (m_isMainFrame)
179 LOG(PageCache, "Finished creating CachedFrame for main frame url '%s' and DocumentLoader %p\n", m_url.string().utf8().data(), m_documentLoader.get());
180 else
181 LOG(PageCache, "Finished creating CachedFrame for child frame with url '%s' and DocumentLoader %p\n", m_url.string().utf8().data(), m_documentLoader.get());
182 #endif
183
184 #if ENABLE(TOUCH_EVENTS)
185 if (m_document->hasListenerType(Document::TOUCH_LISTENER))
186 m_document->page()->chrome()->client()->needTouchEvents(false);
187 #endif
188 }
189
open()190 void CachedFrame::open()
191 {
192 ASSERT(m_view);
193 m_view->frame()->loader()->open(*this);
194
195 if (!m_isMainFrame)
196 m_view->frame()->page()->incrementFrameCount();
197 }
198
clear()199 void CachedFrame::clear()
200 {
201 if (!m_document)
202 return;
203
204 // clear() should only be called for Frames representing documents that are no longer in the page cache.
205 // This means the CachedFrame has been:
206 // 1 - Successfully restore()'d by going back/forward.
207 // 2 - destroy()'ed because the PageCache is pruning or the WebView was closed.
208 ASSERT(!m_document->inPageCache());
209 ASSERT(m_view);
210 ASSERT(m_document->frame() == m_view->frame());
211
212 for (int i = m_childFrames.size() - 1; i >= 0; --i)
213 m_childFrames[i]->clear();
214
215 m_document = 0;
216 m_view = 0;
217 m_mousePressNode = 0;
218 m_url = KURL();
219
220 m_cachedFramePlatformData.clear();
221 m_cachedFrameScriptData.clear();
222 }
223
destroy()224 void CachedFrame::destroy()
225 {
226 if (!m_document)
227 return;
228
229 // Only CachedFrames that are still in the PageCache should be destroyed in this manner
230 ASSERT(m_document->inPageCache());
231 ASSERT(m_view);
232 ASSERT(m_document->frame() == m_view->frame());
233
234 if (!m_isMainFrame) {
235 m_view->frame()->detachFromPage();
236 m_view->frame()->loader()->detachViewsAndDocumentLoader();
237 }
238
239 for (int i = m_childFrames.size() - 1; i >= 0; --i)
240 m_childFrames[i]->destroy();
241
242 if (m_cachedFramePlatformData)
243 m_cachedFramePlatformData->clear();
244
245 Frame::clearTimers(m_view.get(), m_document.get());
246
247 // FIXME: Why do we need to call removeAllEventListeners here? When the document is in page cache, this method won't work
248 // fully anyway, because the document won't be able to access its DOMWindow object (due to being frameless).
249 m_document->removeAllEventListeners();
250
251 m_document->setInPageCache(false);
252 // FIXME: We don't call willRemove here. Why is that OK?
253 m_document->detach();
254 m_view->clearFrame();
255
256 clear();
257 }
258
setCachedFramePlatformData(PassOwnPtr<CachedFramePlatformData> data)259 void CachedFrame::setCachedFramePlatformData(PassOwnPtr<CachedFramePlatformData> data)
260 {
261 m_cachedFramePlatformData = data;
262 }
263
cachedFramePlatformData()264 CachedFramePlatformData* CachedFrame::cachedFramePlatformData()
265 {
266 return m_cachedFramePlatformData.get();
267 }
268
descendantFrameCount() const269 int CachedFrame::descendantFrameCount() const
270 {
271 int count = m_childFrames.size();
272 for (size_t i = 0; i < m_childFrames.size(); ++i)
273 count += m_childFrames[i]->descendantFrameCount();
274
275 return count;
276 }
277
278 } // namespace WebCore
279