CachedPage.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #include "config.h"
  26. #include "CachedPage.h"
  27. #include "Document.h"
  28. #include "Element.h"
  29. #include "FocusController.h"
  30. #include "Frame.h"
  31. #include "FrameView.h"
  32. #include "Node.h"
  33. #include "Page.h"
  34. #include "Settings.h"
  35. #include "VisitedLinkState.h"
  36. #include <wtf/CurrentTime.h>
  37. #include <wtf/RefCountedLeakCounter.h>
  38. #include <wtf/StdLibExtras.h>
  39. using namespace JSC;
  40. namespace WebCore {
  41. DEFINE_DEBUG_ONLY_GLOBAL(WTF::RefCountedLeakCounter, cachedPageCounter, ("CachedPage"));
  42. PassRefPtr<CachedPage> CachedPage::create(Page* page)
  43. {
  44. return adoptRef(new CachedPage(page));
  45. }
  46. CachedPage::CachedPage(Page* page)
  47. : m_timeStamp(currentTime())
  48. , m_expirationTime(m_timeStamp + page->settings()->backForwardCacheExpirationInterval())
  49. , m_cachedMainFrame(CachedFrame::create(page->mainFrame()))
  50. , m_needStyleRecalcForVisitedLinks(false)
  51. , m_needsFullStyleRecalc(false)
  52. , m_needsCaptionPreferencesChanged(false)
  53. , m_needsDeviceScaleChanged(false)
  54. {
  55. #ifndef NDEBUG
  56. cachedPageCounter.increment();
  57. #endif
  58. }
  59. CachedPage::~CachedPage()
  60. {
  61. #ifndef NDEBUG
  62. cachedPageCounter.decrement();
  63. #endif
  64. destroy();
  65. ASSERT(!m_cachedMainFrame);
  66. }
  67. void CachedPage::restore(Page* page)
  68. {
  69. ASSERT(m_cachedMainFrame);
  70. ASSERT(page && page->mainFrame() && page->mainFrame() == m_cachedMainFrame->view()->frame());
  71. ASSERT(!page->subframeCount());
  72. m_cachedMainFrame->open();
  73. // Restore the focus appearance for the focused element.
  74. // 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.
  75. Document* focusedDocument = page->focusController()->focusedOrMainFrame()->document();
  76. if (Element* element = focusedDocument->focusedElement())
  77. element->updateFocusAppearance(true);
  78. if (m_needStyleRecalcForVisitedLinks) {
  79. for (Frame* frame = page->mainFrame(); frame; frame = frame->tree()->traverseNext())
  80. frame->document()->visitedLinkState()->invalidateStyleForAllLinks();
  81. }
  82. #if USE(ACCELERATED_COMPOSITING)
  83. if (m_needsDeviceScaleChanged) {
  84. if (Frame* frame = page->mainFrame())
  85. frame->deviceOrPageScaleFactorChanged();
  86. }
  87. #endif
  88. if (m_needsFullStyleRecalc)
  89. page->setNeedsRecalcStyleInAllFrames();
  90. #if ENABLE(VIDEO_TRACK)
  91. if (m_needsCaptionPreferencesChanged)
  92. page->captionPreferencesChanged();
  93. #endif
  94. clear();
  95. }
  96. void CachedPage::clear()
  97. {
  98. ASSERT(m_cachedMainFrame);
  99. m_cachedMainFrame->clear();
  100. m_cachedMainFrame = 0;
  101. m_needStyleRecalcForVisitedLinks = false;
  102. m_needsFullStyleRecalc = false;
  103. }
  104. void CachedPage::destroy()
  105. {
  106. if (m_cachedMainFrame)
  107. m_cachedMainFrame->destroy();
  108. m_cachedMainFrame = 0;
  109. }
  110. bool CachedPage::hasExpired() const
  111. {
  112. return currentTime() > m_expirationTime;
  113. }
  114. } // namespace WebCore