initial import
[vuplus_webkit] / Source / WebCore / history / CachedPage.cpp
1 /*
2  * Copyright (C) 2006, 2007, 2008 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 "CSSStyleSelector.h"
30 #include "Document.h"
31 #include "Element.h"
32 #include "FocusController.h"
33 #include "Frame.h"
34 #include "FrameView.h"
35 #include "Node.h"
36 #include "Page.h"
37 #include <wtf/CurrentTime.h>
38 #include <wtf/RefCountedLeakCounter.h>
39
40 using namespace JSC;
41
42 namespace WebCore {
43
44 #ifndef NDEBUG
45 static WTF::RefCountedLeakCounter cachedPageCounter("CachedPage");
46 #endif
47
48 PassRefPtr<CachedPage> CachedPage::create(Page* page)
49 {
50     return adoptRef(new CachedPage(page));
51 }
52
53 CachedPage::CachedPage(Page* page)
54     : m_timeStamp(currentTime())
55     , m_cachedMainFrame(CachedFrame::create(page->mainFrame()))
56     , m_needStyleRecalcForVisitedLinks(false)
57     , m_needsFullStyleRecalc(false)
58 {
59 #ifndef NDEBUG
60     cachedPageCounter.increment();
61 #endif
62 }
63
64 CachedPage::~CachedPage()
65 {
66 #ifndef NDEBUG
67     cachedPageCounter.decrement();
68 #endif
69
70     destroy();
71     ASSERT(!m_cachedMainFrame);
72 }
73
74 void CachedPage::restore(Page* page)
75 {
76     ASSERT(m_cachedMainFrame);
77     ASSERT(page && page->mainFrame() && page->mainFrame() == m_cachedMainFrame->view()->frame());
78     ASSERT(!page->frameCount());
79
80     m_cachedMainFrame->open();
81     
82     // Restore the focus appearance for the focused element.
83     // FIXME: Right now we don't support pages w/ frames in the b/f cache.  This may need to be tweaked when we add support for that.
84     Document* focusedDocument = page->focusController()->focusedOrMainFrame()->document();
85     if (Node* node = focusedDocument->focusedNode()) {
86         if (node->isElementNode())
87             static_cast<Element*>(node)->updateFocusAppearance(true);
88     }
89
90     if (m_needStyleRecalcForVisitedLinks) {
91         for (Frame* frame = page->mainFrame(); frame; frame = frame->tree()->traverseNext()) {
92             if (CSSStyleSelector* styleSelector = frame->document()->styleSelector())
93                 styleSelector->allVisitedStateChanged();
94         }
95     }
96
97     if (m_needsFullStyleRecalc)
98         page->setNeedsRecalcStyleInAllFrames();
99
100     clear();
101 }
102
103 void CachedPage::clear()
104 {
105     ASSERT(m_cachedMainFrame);
106     m_cachedMainFrame->clear();
107     m_cachedMainFrame = 0;
108     m_needStyleRecalcForVisitedLinks = false;
109     m_needsFullStyleRecalc = false;
110 }
111
112 void CachedPage::destroy()
113 {
114     if (m_cachedMainFrame)
115         m_cachedMainFrame->destroy();
116
117     m_cachedMainFrame = 0;
118 }
119
120 } // namespace WebCore