Title: [194006] trunk/Source
Revision
194006
Author
[email protected]
Date
2015-12-12 01:26:42 -0800 (Sat, 12 Dec 2015)

Log Message

Safari background tabs should be fully suspended where possible.
https://bugs.webkit.org/show_bug.cgi?id=150515

Patch by Katlyn Graff <[email protected]> on 2015-12-12
Reviewed by Ryosuke Niwa.

Source/WebCore:

Support for tab suspension for Mac, enabled by defaults writing to WebKitTabSuspension.
Page-down suspension consolidated with PageCache suspension code in Document::
suspend and Document::resume. Pages canTabSuspend if cacheable, nonvisible, nonprerender,
and nonactive.

* dom/Document.cpp: moved scrollbar handling from setInPageCache to suspend/resume
(WebCore::Document::suspend): moved scrollbar, dom, animation, timer, and visual update suspending into here
(WebCore::Document::resume): moved scrollbar, dom, animation, timer, and visual update resuming into here
* dom/Document.h: added m_isSuspended to prevent repeat calls from PageCache/Tab Suspension contention
* history/CachedFrame.cpp: moved dom, animation, and timer suspension into Document::suspend
(WebCore::CachedFrame::CachedFrame):
       * history/PageCache.cpp: Added a few nullchecks to prevent crashes if canCacheFrame is called but document is null
(WebCore::PageCache::canCacheFrame):
* page/Page.cpp:
(WebCore::Page::Page): Added timer to fire delayed suspension
(WebCore::Page::setPageActivityState): Added a call to schedule tab suspension
(WebCore::Page::setIsVisibleInternal): Added a call to schedule tab suspension
(WebCore::Page::canTabSuspend): Added support for suspending if cacheable, nonvisible, nonprerender, and nonactive
(WebCore::Page::setIsTabSuspended): Added a function to suspend or resume tabs
(WebCore::Page::setTabSuspensionEnabled): Added support for a defaults write enable
(WebCore::Page::scheduleTabSuspension): Added ability to schedule the suspension timer to fire or resume
(WebCore::Page::timerFired): Added a suspension timer
* page/Page.h:
* page/PageThrottler.h:
(WebCore::PageThrottler::activityState): Added access to m_activityState for canTabSuspend

Source/WebKit2:

Added a runtime flag enabling tab suspension, default off.

* Shared/WebProcessCreationParameters.cpp:
(WebKit::WebProcessCreationParameters::WebProcessCreationParameters):
(WebKit::WebProcessCreationParameters::encode):
(WebKit::WebProcessCreationParameters::decode):
* Shared/WebProcessCreationParameters.h:
* UIProcess/Cocoa/WebProcessPoolCocoa.mm:
(WebKit::registerUserDefaultsIfNeeded):
(WebKit::WebProcessPool::platformInitializeWebProcess):
* WebProcess/cocoa/WebProcessCocoa.mm:
(WebKit::WebProcess::platformInitializeWebProcess):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (194005 => 194006)


--- trunk/Source/WebCore/ChangeLog	2015-12-12 07:44:07 UTC (rev 194005)
+++ trunk/Source/WebCore/ChangeLog	2015-12-12 09:26:42 UTC (rev 194006)
@@ -1,3 +1,36 @@
+2015-12-12  Katlyn Graff  <[email protected]>
+
+        Safari background tabs should be fully suspended where possible.
+        https://bugs.webkit.org/show_bug.cgi?id=150515
+
+        Reviewed by Ryosuke Niwa.
+
+        Support for tab suspension for Mac, enabled by defaults writing to WebKitTabSuspension.
+        Page-down suspension consolidated with PageCache suspension code in Document::
+        suspend and Document::resume. Pages canTabSuspend if cacheable, nonvisible, nonprerender,
+        and nonactive.
+
+        * dom/Document.cpp: moved scrollbar handling from setInPageCache to suspend/resume
+        (WebCore::Document::suspend): moved scrollbar, dom, animation, timer, and visual update suspending into here
+        (WebCore::Document::resume): moved scrollbar, dom, animation, timer, and visual update resuming into here
+        * dom/Document.h: added m_isSuspended to prevent repeat calls from PageCache/Tab Suspension contention
+        * history/CachedFrame.cpp: moved dom, animation, and timer suspension into Document::suspend
+        (WebCore::CachedFrame::CachedFrame):
+       * history/PageCache.cpp: Added a few nullchecks to prevent crashes if canCacheFrame is called but document is null
+        (WebCore::PageCache::canCacheFrame):
+        * page/Page.cpp:
+        (WebCore::Page::Page): Added timer to fire delayed suspension
+        (WebCore::Page::setPageActivityState): Added a call to schedule tab suspension
+        (WebCore::Page::setIsVisibleInternal): Added a call to schedule tab suspension
+        (WebCore::Page::canTabSuspend): Added support for suspending if cacheable, nonvisible, nonprerender, and nonactive
+        (WebCore::Page::setIsTabSuspended): Added a function to suspend or resume tabs
+        (WebCore::Page::setTabSuspensionEnabled): Added support for a defaults write enable
+        (WebCore::Page::scheduleTabSuspension): Added ability to schedule the suspension timer to fire or resume
+        (WebCore::Page::timerFired): Added a suspension timer
+        * page/Page.h:
+        * page/PageThrottler.h:
+        (WebCore::PageThrottler::activityState): Added access to m_activityState for canTabSuspend
+
 2015-12-11  Simon Fraser  <[email protected]>
 
         Mousewheel events don't work in iframes in RTL documents

Modified: trunk/Source/WebCore/dom/Document.cpp (194005 => 194006)


--- trunk/Source/WebCore/dom/Document.cpp	2015-12-12 07:44:07 UTC (rev 194005)
+++ trunk/Source/WebCore/dom/Document.cpp	2015-12-12 09:26:42 UTC (rev 194006)
@@ -4573,9 +4573,6 @@
     FrameView* v = view();
     Page* page = this->page();
 
-    if (page)
-        page->lockAllOverlayScrollbarsToHidden(flag);
-
     if (flag) {
         if (v) {
             // FIXME: There is some scrolling related work that needs to happen whenever a page goes into the
@@ -4608,6 +4605,9 @@
 
 void Document::suspend()
 {
+    if (m_isSuspended)
+        return;
+
     documentWillBecomeInactive();
 
     for (auto* element : m_documentSuspensionCallbackElements)
@@ -4619,14 +4619,31 @@
     m_didDispatchViewportPropertiesChanged = false;
 #endif
 
+    ASSERT(page());
+    page()->lockAllOverlayScrollbarsToHidden(true);
+
     if (RenderView* view = renderView()) {
         if (view->usesCompositing())
             view->compositor().cancelCompositingLayerUpdate();
     }
+
+    suspendScriptedAnimationControllerCallbacks();
+    suspendActiveDOMObjects(ActiveDOMObject::PageCache);
+
+    ASSERT(m_frame);
+    m_frame->clearTimers();
+
+    m_visualUpdatesAllowed = false;
+    m_visualUpdatesSuppressionTimer.stop();
+
+    m_isSuspended = true;
 }
 
 void Document::resume()
 {
+    if (!m_isSuspended)
+        return;
+
     Vector<Element*> elements;
     copyToVector(m_documentSuspensionCallbackElements, elements);
     for (auto* element : elements)
@@ -4640,6 +4657,14 @@
 
     ASSERT(m_frame);
     m_frame->loader().client().dispatchDidBecomeFrameset(isFrameSet());
+    m_frame->animation().resumeAnimationsForDocument(this);
+
+    resumeActiveDOMObjects(ActiveDOMObject::PageWillBeSuspended);
+    resumeScriptedAnimationControllerCallbacks();
+
+    m_visualUpdatesAllowed = true;
+
+    m_isSuspended = false;
 }
 
 void Document::registerForDocumentSuspensionCallbacks(Element* e)

Modified: trunk/Source/WebCore/dom/Document.h (194005 => 194006)


--- trunk/Source/WebCore/dom/Document.h	2015-12-12 07:44:07 UTC (rev 194005)
+++ trunk/Source/WebCore/dom/Document.h	2015-12-12 09:26:42 UTC (rev 194006)
@@ -1764,6 +1764,7 @@
 
     bool m_hasStyleWithViewportUnits;
     bool m_isTimerThrottlingEnabled { false };
+    bool m_isSuspended { false };
 
     HashSet<MediaProducer*> m_audioProducers;
     MediaProducer::MediaStateFlags m_mediaState { MediaProducer::IsNotPlaying };

Modified: trunk/Source/WebCore/history/CachedFrame.cpp (194005 => 194006)


--- trunk/Source/WebCore/history/CachedFrame.cpp	2015-12-12 07:44:07 UTC (rev 194005)
+++ trunk/Source/WebCore/history/CachedFrame.cpp	2015-12-12 09:26:42 UTC (rev 194006)
@@ -165,9 +165,9 @@
     // but after we've fired the pagehide event, in case that creates more objects.
     // Suspending must also happen after we've recursed over child frames, in case
     // those create more objects.
+
     m_document->suspend();
-    m_document->suspendScriptedAnimationControllerCallbacks();
-    m_document->suspendActiveDOMObjects(ActiveDOMObject::PageCache);
+
     m_cachedFrameScriptData = std::make_unique<ScriptCachedFrameData>(frame);
 
     m_document->domWindow()->suspendForDocumentSuspension();
@@ -179,9 +179,6 @@
 
     frame.view()->clearScrollableAreas();
 
-    // suspend() can set up a layout timer on the FrameView, so clear timers after that.
-    frame.clearTimers();
-
     // Deconstruct the FrameTree, to restore it later.
     // We do this for two reasons:
     // 1 - We reuse the main frame, so when it navigates to a new page load it needs to start with a blank FrameTree.

Modified: trunk/Source/WebCore/history/PageCache.cpp (194005 => 194006)


--- trunk/Source/WebCore/history/PageCache.cpp	2015-12-12 07:44:07 UTC (rev 194005)
+++ trunk/Source/WebCore/history/PageCache.cpp	2015-12-12 09:26:42 UTC (rev 194006)
@@ -122,7 +122,7 @@
         logPageCacheFailureDiagnosticMessage(diagnosticLoggingClient, DiagnosticLoggingKeys::hasPluginsKey());
         isCacheable = false;
     }
-    if (frame.isMainFrame() && frame.document()->url().protocolIs("https") && documentLoader->response().cacheControlContainsNoStore()) {
+    if (frame.isMainFrame() && frame.document() && frame.document()->url().protocolIs("https") && documentLoader->response().cacheControlContainsNoStore()) {
         PCLOG("   -Frame is HTTPS, and cache control prohibits storing");
         logPageCacheFailureDiagnosticMessage(diagnosticLoggingClient, DiagnosticLoggingKeys::httpsNoStoreKey());
         isCacheable = false;
@@ -149,7 +149,7 @@
     }
 
     Vector<ActiveDOMObject*> unsuspendableObjects;
-    if (!frame.document()->canSuspendActiveDOMObjectsForDocumentSuspension(&unsuspendableObjects)) {
+    if (frame.document() && !frame.document()->canSuspendActiveDOMObjectsForDocumentSuspension(&unsuspendableObjects)) {
         PCLOG("   -The document cannot suspend its active DOM Objects");
         for (auto* activeDOMObject : unsuspendableObjects) {
             PCLOG("    - Unsuspendable: ", activeDOMObject->activeDOMObjectName());

Modified: trunk/Source/WebCore/page/Page.cpp (194005 => 194006)


--- trunk/Source/WebCore/page/Page.cpp	2015-12-12 07:44:07 UTC (rev 194005)
+++ trunk/Source/WebCore/page/Page.cpp	2015-12-12 09:26:42 UTC (rev 194006)
@@ -143,6 +143,7 @@
 }
 
 static const ViewState::Flags PageInitialViewState = ViewState::IsVisible | ViewState::IsInWindow;
+bool Page::s_tabSuspensionIsEnabled = false;
 
 Page::Page(PageConfiguration& pageConfiguration)
     : m_chrome(std::make_unique<Chrome>(*this, *pageConfiguration.chromeClient))
@@ -223,6 +224,7 @@
     , m_visitedLinkStore(*WTF::move(pageConfiguration.visitedLinkStore))
     , m_sessionID(SessionID::defaultSessionID())
     , m_isClosing(false)
+    , m_tabSuspensionTimer(*this, &Page::tabSuspensionTimerFired)
 {
     setTimerThrottlingEnabled(m_viewState & ViewState::IsVisuallyIdle);
 
@@ -1293,6 +1295,11 @@
 void Page::setPageActivityState(PageActivityState::Flags activityState)
 {
     chrome().client().setPageActivityState(activityState);
+    
+    if (activityState == PageActivityState::NoFlags && !isVisible())
+        scheduleTabSuspension(true);
+    else
+        scheduleTabSuspension(false);
 }
 
 void Page::setIsVisible(bool isVisible)
@@ -1344,6 +1351,8 @@
         if (FrameView* view = mainFrame().view())
             view->hide();
     }
+
+    scheduleTabSuspension(!isVisible);
 }
 
 void Page::setIsPrerender()
@@ -1843,4 +1852,48 @@
 }
 #endif
 
+bool Page::canTabSuspend()
+{
+    return s_tabSuspensionIsEnabled && !m_isPrerender && (m_pageThrottler.activityState() == PageActivityState::NoFlags) && PageCache::singleton().canCache(this);
+}
+
+void Page::setIsTabSuspended(bool shouldSuspend)
+{
+    for (Frame* frame = &mainFrame(); frame; frame = frame->tree().traverseNext()) {
+        if (auto* document = frame->document()) {
+            if (shouldSuspend)
+                document->suspend();
+            else
+                document->resume();
+        }
+    }
+}
+
+void Page::setTabSuspensionEnabled(bool enable)
+{
+    s_tabSuspensionIsEnabled = enable;
+}
+
+void Page::scheduleTabSuspension(bool shouldSuspend)
+{
+    if (m_shouldTabSuspend == shouldSuspend)
+        return;
+    
+    if (shouldSuspend && canTabSuspend()) {
+        m_shouldTabSuspend = shouldSuspend;
+        m_tabSuspensionTimer.startOneShot(0);
+    } else {
+        m_tabSuspensionTimer.stop();
+        if (!shouldSuspend) {
+            m_shouldTabSuspend = shouldSuspend;
+            setIsTabSuspended(false);
+        }
+    }
+}
+
+void Page::tabSuspensionTimerFired()
+{
+    setIsTabSuspended(true);
+}
+
 } // namespace WebCore

Modified: trunk/Source/WebCore/page/Page.h (194005 => 194006)


--- trunk/Source/WebCore/page/Page.h	2015-12-12 07:44:07 UTC (rev 194005)
+++ trunk/Source/WebCore/page/Page.h	2015-12-12 09:26:42 UTC (rev 194006)
@@ -138,6 +138,7 @@
 public:
     WEBCORE_EXPORT static void updateStyleForAllPagesAfterGlobalChangeInEnvironment();
     WEBCORE_EXPORT static void clearPreviousItemFromAllPages(HistoryItem*);
+    WEBCORE_EXPORT static void setTabSuspensionEnabled(bool);
 
     WEBCORE_EXPORT explicit Page(PageConfiguration&);
     WEBCORE_EXPORT ~Page();
@@ -365,6 +366,8 @@
     // with exponential growth in the number of frames.
     static const int maxNumberOfFrames = 1000;
 
+    static bool s_tabSuspensionIsEnabled;
+
     void setEditable(bool isEditable) { m_isEditable = isEditable; }
     bool isEditable() { return m_isEditable; }
 
@@ -491,6 +494,7 @@
 
     void setShowAllPlugins(bool showAll) { m_showAllPlugins = showAll; }
     bool showAllPlugins() const;
+    void setIsTabSuspended(bool);
 
 private:
     WEBCORE_EXPORT void initGroup();
@@ -516,6 +520,9 @@
 
     void hiddenPageDOMTimerThrottlingStateChanged();
     void setTimerThrottlingEnabled(bool);
+    bool canTabSuspend();
+    void scheduleTabSuspension(bool);
+    void tabSuspensionTimerFired();
 
     const std::unique_ptr<Chrome> m_chrome;
     const std::unique_ptr<DragCaretController> m_dragCaretController;
@@ -605,6 +612,7 @@
     bool m_isEditable;
     bool m_isPrerender;
     ViewState::Flags m_viewState;
+    PageActivityState::Flags m_pageActivityState;
 
     LayoutMilestones m_requestedLayoutMilestones;
 
@@ -655,6 +663,8 @@
     SessionID m_sessionID;
 
     bool m_isClosing;
+    bool m_shouldTabSuspend { false };
+    Timer m_tabSuspensionTimer;
 
     MediaProducer::MediaStateFlags m_mediaState { MediaProducer::IsNotPlaying };
     

Modified: trunk/Source/WebCore/page/PageThrottler.h (194005 => 194006)


--- trunk/Source/WebCore/page/PageThrottler.h	2015-12-12 07:44:07 UTC (rev 194005)
+++ trunk/Source/WebCore/page/PageThrottler.h	2015-12-12 09:26:42 UTC (rev 194006)
@@ -59,6 +59,7 @@
     PageThrottler(Page&);
 
     void didReceiveUserInput() { m_userInputHysteresis.impulse(); }
+    PageActivityState::Flags activityState() { return m_activityState; }
     void pluginDidEvaluateWhileAudioIsPlaying() { m_audiblePluginHysteresis.impulse(); }
     PageActivityAssertionToken mediaActivityToken();
     PageActivityAssertionToken pageLoadActivityToken();

Modified: trunk/Source/WebKit2/ChangeLog (194005 => 194006)


--- trunk/Source/WebKit2/ChangeLog	2015-12-12 07:44:07 UTC (rev 194005)
+++ trunk/Source/WebKit2/ChangeLog	2015-12-12 09:26:42 UTC (rev 194006)
@@ -1,3 +1,23 @@
+2015-12-12  Katlyn Graff  <[email protected]>
+
+        Safari background tabs should be fully suspended where possible.
+        https://bugs.webkit.org/show_bug.cgi?id=150515
+
+        Reviewed by Ryosuke Niwa.
+
+        Added a runtime flag enabling tab suspension, default off.
+
+        * Shared/WebProcessCreationParameters.cpp:
+        (WebKit::WebProcessCreationParameters::WebProcessCreationParameters):
+        (WebKit::WebProcessCreationParameters::encode):
+        (WebKit::WebProcessCreationParameters::decode):
+        * Shared/WebProcessCreationParameters.h:
+        * UIProcess/Cocoa/WebProcessPoolCocoa.mm:
+        (WebKit::registerUserDefaultsIfNeeded):
+        (WebKit::WebProcessPool::platformInitializeWebProcess):
+        * WebProcess/cocoa/WebProcessCocoa.mm:
+        (WebKit::WebProcess::platformInitializeWebProcess):
+
 2015-12-11  Eric Carlson  <[email protected]>
 
         [MediaStream] Add a setting to allow the mock media capture devices to be enabled and disabled

Modified: trunk/Source/WebKit2/Shared/WebProcessCreationParameters.cpp (194005 => 194006)


--- trunk/Source/WebKit2/Shared/WebProcessCreationParameters.cpp	2015-12-12 07:44:07 UTC (rev 194005)
+++ trunk/Source/WebKit2/Shared/WebProcessCreationParameters.cpp	2015-12-12 09:26:42 UTC (rev 194006)
@@ -43,6 +43,9 @@
     , shouldEnableJIT(false)
     , shouldEnableFTLJIT(false)
 #endif
+#if PLATFORM(MAC)
+    , shouldEnableTabSuspension(false)
+#endif
     , memoryCacheDisabled(false)
 #if ENABLE(SERVICE_CONTROLS)
     , hasImageServices(false)
@@ -118,6 +121,10 @@
         encoder << bundleParameterData->dataReference();
 #endif
 
+#if PLATFORM(MAC)
+    encoder << shouldEnableTabSuspension;
+#endif
+
 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
     encoder << notificationPermissions;
 #endif
@@ -257,6 +264,11 @@
     }
 #endif
 
+#if PLATFORM(MAC)
+    if (!decoder.decode(parameters.shouldEnableTabSuspension))
+        return false;
+#endif
+
 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
     if (!decoder.decode(parameters.notificationPermissions))
         return false;

Modified: trunk/Source/WebKit2/Shared/WebProcessCreationParameters.h (194005 => 194006)


--- trunk/Source/WebKit2/Shared/WebProcessCreationParameters.h	2015-12-12 07:44:07 UTC (rev 194005)
+++ trunk/Source/WebKit2/Shared/WebProcessCreationParameters.h	2015-12-12 09:26:42 UTC (rev 194006)
@@ -142,6 +142,10 @@
 
 #endif // PLATFORM(COCOA)
 
+#if PLATFORM(MAC)
+    bool shouldEnableTabSuspension;
+#endif
+
 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
     HashMap<String, bool> notificationPermissions;
 #endif

Modified: trunk/Source/WebKit2/UIProcess/Cocoa/WebProcessPoolCocoa.mm (194005 => 194006)


--- trunk/Source/WebKit2/UIProcess/Cocoa/WebProcessPoolCocoa.mm	2015-12-12 07:44:07 UTC (rev 194005)
+++ trunk/Source/WebKit2/UIProcess/Cocoa/WebProcessPoolCocoa.mm	2015-12-12 09:26:42 UTC (rev 194006)
@@ -84,6 +84,10 @@
 #endif
 #endif
 
+#if PLATFORM(MAC)
+NSString *WebKitTabSuspension = @"WebKitTabSuspension";
+#endif
+
 namespace WebKit {
 
 NSString *SchemeForCustomProtocolRegisteredNotificationName = @"WebKitSchemeForCustomProtocolRegisteredNotification";
@@ -101,6 +105,10 @@
     [registrationDictionary setObject:[NSNumber numberWithBool:YES] forKey:WebKitJSCJITEnabledDefaultsKey];
     [registrationDictionary setObject:[NSNumber numberWithBool:YES] forKey:WebKitJSCFTLJITEnabledDefaultsKey];
 
+#if PLATFORM(MAC)
+    [registrationDictionary setObject:[NSNumber numberWithBool:NO] forKey:WebKitTabSuspension];
+#endif
+
 #if ENABLE(NETWORK_CACHE)
     [registrationDictionary setObject:[NSNumber numberWithBool:YES] forKey:WebKitNetworkCacheEnabledDefaultsKey];
     [registrationDictionary setObject:[NSNumber numberWithBool:NO] forKey:WebKitNetworkCacheEfficacyLoggingEnabledDefaultsKey];
@@ -170,6 +178,10 @@
     parameters.accessibilityEnhancedUserInterfaceEnabled = false;
 #endif
 
+#if PLATFORM(MAC)
+    parameters.shouldEnableTabSuspension = [[NSUserDefaults standardUserDefaults] boolForKey:WebKitTabSuspension];
+#endif
+
     parameters.shouldEnableJIT = [[NSUserDefaults standardUserDefaults] boolForKey:WebKitJSCJITEnabledDefaultsKey];
     parameters.shouldEnableFTLJIT = [[NSUserDefaults standardUserDefaults] boolForKey:WebKitJSCFTLJITEnabledDefaultsKey];
     parameters.shouldEnableMemoryPressureReliefLogging = [[NSUserDefaults standardUserDefaults] boolForKey:@"LogMemoryJetsamDetails"];

Modified: trunk/Source/WebKit2/WebProcess/cocoa/WebProcessCocoa.mm (194005 => 194006)


--- trunk/Source/WebKit2/WebProcess/cocoa/WebProcessCocoa.mm	2015-12-12 07:44:07 UTC (rev 194005)
+++ trunk/Source/WebKit2/WebProcess/cocoa/WebProcessCocoa.mm	2015-12-12 09:26:42 UTC (rev 194006)
@@ -145,6 +145,7 @@
 
 #if PLATFORM(MAC)
     WebCore::FontCache::setFontWhitelist(parameters.fontWhitelist);
+    Page::setTabSuspensionEnabled(parameters.shouldEnableTabSuspension);
 #endif
 
     m_compositingRenderServerPort = WTF::move(parameters.acceleratedCompositingPort);
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to