Title: [175112] branches/safari-600.3-branch/Source

Diff

Modified: branches/safari-600.3-branch/Source/WebCore/ChangeLog (175111 => 175112)


--- branches/safari-600.3-branch/Source/WebCore/ChangeLog	2014-10-23 09:29:10 UTC (rev 175111)
+++ branches/safari-600.3-branch/Source/WebCore/ChangeLog	2014-10-23 09:30:47 UTC (rev 175112)
@@ -1,5 +1,54 @@
 2014-10-23  Babak Shafiei  <[email protected]>
 
+        Merge r173694.
+
+    2014-09-17  Gavin Barraclough  <[email protected]>
+
+            DOM timer throttling for hidden plugins
+            https://bugs.webkit.org/show_bug.cgi?id=136197
+
+            Reviewed by Geoff Garen & Andreas Kling.
+
+            For non-visible web pages we slow timers (since they can't be driving animations, etc).
+            We should do the same for plugins.
+
+            There are a few steps to this:
+                - JSPluginElementFunctions notifies DOMTimer when script interacts with a plugin.
+                - DOMTimerFireState keeps a record of events that occur while a timer is firing.
+                - DOMTimer::fired detects timers that interact with invisible/inaudible plugins, and flags itself for throtting.
+                - DOMTimer::intervalClampedToMinimum sets timer intervals appropriately.
+
+            * bindings/js/JSPluginElementFunctions.cpp:
+            (WebCore::pluginScriptObject):
+                - notify DOMTimer when script interacts with a plugin.
+            * html/HTMLPlugInElement.cpp:
+            (WebCore::HTMLPlugInElement::isDetectable):
+            * html/HTMLPlugInElement.h:
+                - added isDetectable, to check for visible / audible plugins.
+            * page/DOMTimer.cpp:
+            (WebCore::DOMTimerFireState::DOMTimerFireState):
+            (WebCore::DOMTimerFireState::~DOMTimerFireState):
+                - track current DOMTimerFireState.
+            (WebCore::DOMTimer::DOMTimer):
+                - initialize m_throttleState.
+            (WebCore::DOMTimer::scriptDidInteractWithPlugin):
+                - if interaction with a plugin occurs while a timer is firing, record on the DOMTimerFireState.
+            (WebCore::DOMTimer::fired):
+                - set DOMTimerFireState, and update m_throttleState accordingly.
+            (WebCore::DOMTimer::updateTimerIntervalIfNecessary):
+                - remove redundant check of maxTimerNestingLevel (covered by intervalClampedToMinimum).
+            (WebCore::DOMTimer::intervalClampedToMinimum):
+                - Also take m_throttleState into account when throttling.
+            * page/DOMTimer.h:
+                - added scriptDidInteractWithPlugin, m_throttleState.
+            * platform/audio/AudioHardwareListener.h:
+                - enum -> enum class.
+            * plugins/PluginViewBase.h:
+            (WebCore::PluginViewBase::audioHardwareActivity):
+                - expose audioHardwareActivity on PluginViewBase (previously available in subclass).
+
+2014-10-23  Babak Shafiei  <[email protected]>
+
         Merge r173208.
 
     2014-08-29  Gavin Barraclough  <[email protected]>

Modified: branches/safari-600.3-branch/Source/WebCore/bindings/js/JSPluginElementFunctions.cpp (175111 => 175112)


--- branches/safari-600.3-branch/Source/WebCore/bindings/js/JSPluginElementFunctions.cpp	2014-10-23 09:29:10 UTC (rev 175111)
+++ branches/safari-600.3-branch/Source/WebCore/bindings/js/JSPluginElementFunctions.cpp	2014-10-23 09:30:47 UTC (rev 175112)
@@ -82,6 +82,9 @@
 
     HTMLPlugInElement& pluginElement = toHTMLPlugInElement(element);
 
+    // Choke point for script/plugin interaction; notify DOMTimer of the event.
+    DOMTimer::scriptDidInteractWithPlugin(pluginElement);
+
     // First, see if the element has a plug-in replacement with a script.
     if (JSObject* scriptObject = pluginElement.scriptObjectForPluginReplacement())
         return scriptObject;

Modified: branches/safari-600.3-branch/Source/WebCore/html/HTMLPlugInElement.cpp (175111 => 175112)


--- branches/safari-600.3-branch/Source/WebCore/html/HTMLPlugInElement.cpp	2014-10-23 09:29:10 UTC (rev 175111)
+++ branches/safari-600.3-branch/Source/WebCore/html/HTMLPlugInElement.cpp	2014-10-23 09:30:47 UTC (rev 175112)
@@ -253,6 +253,23 @@
     return true;
 }
 
+bool HTMLPlugInElement::isUserObservable() const
+{
+    // No widget - can't be anything to see or hear here.
+    Widget* widget = pluginWidget();
+    if (!widget || !widget->isPluginViewBase())
+        return false;
+
+    PluginViewBase* pluginView = toPluginViewBase(widget);
+
+    // If audio is playing (or might be) then the plugin is detectable.
+    if (pluginView->audioHardwareActivity() != AudioHardwareActivityType::IsInactive)
+        return true;
+
+    // If the plugin is visible and not vanishingly small in either dimension it is detectable.
+    return pluginView->isVisible() && widget->width() > 2 && widget->height() > 2;
+}
+
 bool HTMLPlugInElement::supportsFocus() const
 {
     if (HTMLFrameOwnerElement::supportsFocus())

Modified: branches/safari-600.3-branch/Source/WebCore/html/HTMLPlugInElement.h (175111 => 175112)


--- branches/safari-600.3-branch/Source/WebCore/html/HTMLPlugInElement.h	2014-10-23 09:29:10 UTC (rev 175111)
+++ branches/safari-600.3-branch/Source/WebCore/html/HTMLPlugInElement.h	2014-10-23 09:30:47 UTC (rev 175112)
@@ -88,6 +88,8 @@
 
     virtual bool isPlugInImageElement() const { return false; }
 
+    bool isUserObservable() const;
+    
 protected:
     HTMLPlugInElement(const QualifiedName& tagName, Document&);
 

Modified: branches/safari-600.3-branch/Source/WebCore/page/DOMTimer.cpp (175111 => 175112)


--- branches/safari-600.3-branch/Source/WebCore/page/DOMTimer.cpp	2014-10-23 09:29:10 UTC (rev 175111)
+++ branches/safari-600.3-branch/Source/WebCore/page/DOMTimer.cpp	2014-10-23 09:30:47 UTC (rev 175112)
@@ -27,7 +27,9 @@
 #include "config.h"
 #include "DOMTimer.h"
 
+#include "HTMLPlugInElement.h"
 #include "InspectorInstrumentation.h"
+#include "PluginViewBase.h"
 #include "ScheduledAction.h"
 #include "ScriptExecutionContext.h"
 #include "UserGestureIndicator.h"
@@ -46,9 +48,42 @@
 namespace WebCore {
 
 static const int maxIntervalForUserGestureForwarding = 1000; // One second matches Gecko.
+static const int minIntervalForNonUserObservablePluginScriptTimers = 1000; // Empirically determined to maximize battery life.
 static const int maxTimerNestingLevel = 5;
 static const double _oneMillisecond_ = 0.001;
 
+struct DOMTimerFireState {
+    DOMTimerFireState(ScriptExecutionContext* context)
+        : scriptDidInteractWithNonUserObservablePlugin(false)
+        , scriptDidInteractWithUserObservablePlugin(false)
+        , shouldSetCurrent(context->isDocument())
+    {
+        // For worker threads, don't update the current DOMTimerFireState.
+        // Setting this from workers would not be thread-safe, and its not relevant to current uses.
+        if (shouldSetCurrent) {
+            previous = current;
+            current = this;
+        }
+    }
+
+    ~DOMTimerFireState()
+    {
+        if (shouldSetCurrent)
+            current = previous;
+    }
+
+    static DOMTimerFireState* current;
+
+    bool scriptDidInteractWithNonUserObservablePlugin;
+    bool scriptDidInteractWithUserObservablePlugin;
+
+private:
+    bool shouldSetCurrent;
+    DOMTimerFireState* previous;
+};
+
+DOMTimerFireState* DOMTimerFireState::current = nullptr;
+
 static inline bool shouldForwardUserGesture(int interval, int nestingLevel)
 {
     return UserGestureIndicator::processingUserGesture()
@@ -61,6 +96,7 @@
     , m_nestingLevel(context->timerNestingLevel())
     , m_action(WTF::move(action))
     , m_originalInterval(interval)
+    , m_throttleState(Undetermined)
     , m_currentTimerInterval(intervalClampedToMinimum())
     , m_shouldForwardUserGesture(shouldForwardUserGesture(interval, m_nestingLevel))
 {
@@ -112,6 +148,17 @@
     context->removeTimeout(timeoutId);
 }
 
+void DOMTimer::scriptDidInteractWithPlugin(HTMLPlugInElement& pluginElement)
+{
+    if (!DOMTimerFireState::current)
+        return;
+
+    if (pluginElement.isUserObservable())
+        DOMTimerFireState::current->scriptDidInteractWithUserObservablePlugin = true;
+    else
+        DOMTimerFireState::current->scriptDidInteractWithNonUserObservablePlugin = true;
+}
+
 void DOMTimer::fired()
 {
     // Retain this - if the timer is cancelled while this function is on the stack (implicitly and always
@@ -121,6 +168,9 @@
 
     ScriptExecutionContext* context = scriptExecutionContext();
     ASSERT(context);
+
+    DOMTimerFireState fireState(context);
+
 #if PLATFORM(IOS)
     Document* document = nullptr;
     if (context->isDocument()) {
@@ -149,6 +199,14 @@
 
         InspectorInstrumentation::didFireTimer(cookie);
 
+        if (fireState.scriptDidInteractWithUserObservablePlugin && m_throttleState != ShouldNotThrottle) {
+            m_throttleState = ShouldNotThrottle;
+            updateTimerIntervalIfNecessary();
+        } else if (fireState.scriptDidInteractWithNonUserObservablePlugin && m_throttleState == Undetermined) {
+            m_throttleState = ShouldThrottle;
+            updateTimerIntervalIfNecessary();
+        }
+
         return;
     }
 
@@ -199,8 +257,6 @@
 void DOMTimer::updateTimerIntervalIfNecessary()
 {
     ASSERT(m_nestingLevel <= maxTimerNestingLevel);
-    if (m_nestingLevel < maxTimerNestingLevel)
-        return;
 
     double previousInterval = m_currentTimerInterval;
     m_currentTimerInterval = intervalClampedToMinimum();
@@ -220,12 +276,17 @@
     ASSERT(scriptExecutionContext());
     ASSERT(m_nestingLevel <= maxTimerNestingLevel);
 
-    double minimumTimerInterval = scriptExecutionContext()->minimumTimerInterval();
-    double intervalMilliseconds = std::max(oneMillisecond, m_originalInterval * oneMillisecond);
+    double intervalInSeconds = std::max(oneMillisecond, m_originalInterval * oneMillisecond);
 
-    if (intervalMilliseconds < minimumTimerInterval && m_nestingLevel == maxTimerNestingLevel)
-        intervalMilliseconds = minimumTimerInterval;
-    return intervalMilliseconds;
+    // Only apply throttling to repeating timers.
+    if (m_nestingLevel < maxTimerNestingLevel)
+        return intervalInSeconds;
+
+    // Apply two throttles - the global (per Page) minimum, and also a per-timer throttle.
+    intervalInSeconds = std::max(intervalInSeconds, scriptExecutionContext()->minimumTimerInterval());
+    if (m_throttleState == ShouldThrottle)
+        intervalInSeconds = std::max(intervalInSeconds, minIntervalForNonUserObservablePluginScriptTimers * oneMillisecond);
+    return intervalInSeconds;
 }
 
 double DOMTimer::alignedFireTime(double fireTime) const

Modified: branches/safari-600.3-branch/Source/WebCore/page/DOMTimer.h (175111 => 175112)


--- branches/safari-600.3-branch/Source/WebCore/page/DOMTimer.h	2014-10-23 09:29:10 UTC (rev 175111)
+++ branches/safari-600.3-branch/Source/WebCore/page/DOMTimer.h	2014-10-23 09:30:47 UTC (rev 175112)
@@ -33,6 +33,7 @@
 
 namespace WebCore {
 
+    class HTMLPlugInElement;
     class ScheduledAction;
 
     class DOMTimer final : public RefCounted<DOMTimer>, public SuspendableTimer {
@@ -48,6 +49,8 @@
         // setting for the context has changed).
         void updateTimerIntervalIfNecessary();
 
+        static void scriptDidInteractWithPlugin(HTMLPlugInElement&);
+
     private:
         DOMTimer(ScriptExecutionContext*, std::unique_ptr<ScheduledAction>, int interval, bool singleShot);
         double intervalClampedToMinimum() const;
@@ -57,10 +60,17 @@
         virtual void didStop() override;
         virtual double alignedFireTime(double) const override;
 
+        enum TimerThrottleState {
+            Undetermined,
+            ShouldThrottle,
+            ShouldNotThrottle
+        };
+
         int m_timeoutId;
         int m_nestingLevel;
         std::unique_ptr<ScheduledAction> m_action;
         int m_originalInterval;
+        TimerThrottleState m_throttleState;
         double m_currentTimerInterval;
         bool m_shouldForwardUserGesture;
     };

Modified: branches/safari-600.3-branch/Source/WebCore/plugins/PluginViewBase.h (175111 => 175112)


--- branches/safari-600.3-branch/Source/WebCore/plugins/PluginViewBase.h	2014-10-23 09:29:10 UTC (rev 175111)
+++ branches/safari-600.3-branch/Source/WebCore/plugins/PluginViewBase.h	2014-10-23 09:30:47 UTC (rev 175112)
@@ -25,6 +25,7 @@
 #ifndef PluginWidget_h
 #define PluginWidget_h
 
+#include "AudioHardwareListener.h"
 #include "PlatformLayer.h"
 #include "ScrollTypes.h"
 #include "Widget.h"
@@ -74,6 +75,8 @@
     virtual bool isPluginViewBase() const { return true; }
     virtual bool shouldNotAddLayer() const { return false; }
 
+    virtual AudioHardwareActivityType audioHardwareActivity() const { return AudioHardwareActivityType::Unknown; }
+
 protected:
     explicit PluginViewBase(PlatformWidget widget = 0) : Widget(widget) { }
 };

Modified: branches/safari-600.3-branch/Source/WebKit2/ChangeLog (175111 => 175112)


--- branches/safari-600.3-branch/Source/WebKit2/ChangeLog	2014-10-23 09:29:10 UTC (rev 175111)
+++ branches/safari-600.3-branch/Source/WebKit2/ChangeLog	2014-10-23 09:30:47 UTC (rev 175112)
@@ -1,5 +1,25 @@
 2014-10-23  Babak Shafiei  <[email protected]>
 
+        Merge r173694.
+
+    2014-09-17  Gavin Barraclough  <[email protected]>
+
+            DOM timer throttling for hidden plugins
+            https://bugs.webkit.org/show_bug.cgi?id=136197
+
+            Reviewed by Geoff Garen & Andreas Kling.
+
+            For non-visible web pages we slow timers (since they can't be driving animations, etc).
+            We should do the same for plugins.
+
+            * PluginProcess/WebProcessConnection.cpp:
+            * WebProcess/Plugins/Netscape/NPRuntimeObjectMap.cpp:
+                - remove now redundant includes.
+            * WebProcess/Plugins/PluginView.h:
+                - audioHardwareActivity is now virtual, override.
+
+2014-10-23  Babak Shafiei  <[email protected]>
+
         Merge r172653.
 
     2014-08-15  Gavin Barraclough  <[email protected]>

Modified: branches/safari-600.3-branch/Source/WebKit2/PluginProcess/WebProcessConnection.cpp (175111 => 175112)


--- branches/safari-600.3-branch/Source/WebKit2/PluginProcess/WebProcessConnection.cpp	2014-10-23 09:29:10 UTC (rev 175111)
+++ branches/safari-600.3-branch/Source/WebKit2/PluginProcess/WebProcessConnection.cpp	2014-10-23 09:30:47 UTC (rev 175112)
@@ -39,7 +39,6 @@
 #include "PluginProcessConnectionMessages.h"
 #include "PluginProxyMessages.h"
 #include "WebProcessConnectionMessages.h"
-#include <WebCore/AudioHardwareListener.h>
 #include <unistd.h>
 #include <wtf/RunLoop.h>
 

Modified: branches/safari-600.3-branch/Source/WebKit2/WebProcess/Plugins/Netscape/NPRuntimeObjectMap.cpp (175111 => 175112)


--- branches/safari-600.3-branch/Source/WebKit2/WebProcess/Plugins/Netscape/NPRuntimeObjectMap.cpp	2014-10-23 09:29:10 UTC (rev 175111)
+++ branches/safari-600.3-branch/Source/WebKit2/WebProcess/Plugins/Netscape/NPRuntimeObjectMap.cpp	2014-10-23 09:30:47 UTC (rev 175112)
@@ -39,7 +39,6 @@
 #include <_javascript_Core/SourceCode.h>
 #include <_javascript_Core/Strong.h>
 #include <_javascript_Core/StrongInlines.h>
-#include <WebCore/AudioHardwareListener.h>
 #include <WebCore/DOMWrapperWorld.h>
 #include <WebCore/Frame.h>
 #include <WebCore/Page.h>

Modified: branches/safari-600.3-branch/Source/WebKit2/WebProcess/Plugins/PluginView.cpp (175111 => 175112)


--- branches/safari-600.3-branch/Source/WebKit2/WebProcess/Plugins/PluginView.cpp	2014-10-23 09:29:10 UTC (rev 175111)
+++ branches/safari-600.3-branch/Source/WebKit2/WebProcess/Plugins/PluginView.cpp	2014-10-23 09:30:47 UTC (rev 175112)
@@ -514,7 +514,19 @@
         m_plugin->windowFocusChanged(m_webPage->windowIsFocused());
 }
 
+WebCore::AudioHardwareActivityType PluginView::audioHardwareActivity() const
+{
+    if (!m_isInitialized || !m_plugin)
+        return AudioHardwareActivityType::IsInactive;
+    
 #if PLATFORM(COCOA)
+    return m_plugin->audioHardwareActivity();
+#else
+    return AudioHardwareActivityType::Unknown;
+#endif
+}
+    
+#if PLATFORM(COCOA)
 void PluginView::setDeviceScaleFactor(float scaleFactor)
 {
     if (!m_isInitialized || !m_plugin)
@@ -543,11 +555,6 @@
     return true;
 }
     
-WebCore::AudioHardwareActivityType PluginView::audioHardwareActivity() const
-{
-    return m_plugin->audioHardwareActivity();
-}
-    
 NSObject *PluginView::accessibilityObject() const
 {
     if (!m_isInitialized || !m_plugin)

Modified: branches/safari-600.3-branch/Source/WebKit2/WebProcess/Plugins/PluginView.h (175111 => 175112)


--- branches/safari-600.3-branch/Source/WebKit2/WebProcess/Plugins/PluginView.h	2014-10-23 09:29:10 UTC (rev 175111)
+++ branches/safari-600.3-branch/Source/WebKit2/WebProcess/Plugins/PluginView.h	2014-10-23 09:30:47 UTC (rev 175112)
@@ -109,7 +109,7 @@
 
     PassRefPtr<WebCore::SharedBuffer> liveResourceData() const;
     bool performDictionaryLookupAtLocation(const WebCore::FloatPoint&);
-    WebCore::AudioHardwareActivityType audioHardwareActivity() const;
+    virtual WebCore::AudioHardwareActivityType audioHardwareActivity() const override;
 
 private:
     PluginView(PassRefPtr<WebCore::HTMLPlugInElement>, PassRefPtr<Plugin>, const Plugin::Parameters& parameters);
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to