From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: deepak1556 <hop2deep@gmail.com>
Date: Fri, 29 Nov 2019 16:08:14 -0800
Subject: feat: allow disabling blink scheduler throttling per RenderView

This allows us to disable throttling for hidden windows.

diff --git a/content/browser/renderer_host/render_view_host_impl.cc b/content/browser/renderer_host/render_view_host_impl.cc
index 01a4e3dc134b3cf267ec637dde1066db51ac78e7..9178621b483b3790fe41f75f40463001cde4f133 100644
--- a/content/browser/renderer_host/render_view_host_impl.cc
+++ b/content/browser/renderer_host/render_view_host_impl.cc
@@ -458,6 +458,10 @@ void RenderViewHostImpl::SetBackgroundOpaque(bool opaque) {
   GetWidget()->GetAssociatedFrameWidget()->SetBackgroundOpaque(opaque);
 }
 
+void RenderViewHostImpl::SetSchedulerThrottling(bool allowed) {
+  Send(new ViewMsg_SetSchedulerThrottling(GetRoutingID(), allowed));
+}
+
 bool RenderViewHostImpl::IsMainFrameActive() {
   return is_active();
 }
diff --git a/content/browser/renderer_host/render_view_host_impl.h b/content/browser/renderer_host/render_view_host_impl.h
index 70f9ea0f650b9a4b482d823a603871808f8e125c..8bd66fa4058fa5fa2ef0872c9fda2ab1192b2a2f 100644
--- a/content/browser/renderer_host/render_view_host_impl.h
+++ b/content/browser/renderer_host/render_view_host_impl.h
@@ -110,6 +110,7 @@ class CONTENT_EXPORT RenderViewHostImpl
   SiteInstanceImpl* GetSiteInstance() override;
   bool IsRenderViewLive() override;
   void NotifyMoveOrResizeStarted() override;
+  void SetSchedulerThrottling(bool allowed) override;
   WebPreferences GetWebkitPreferences() override;
   void UpdateWebkitPreferences(const WebPreferences& prefs) override;
   void OnWebkitPreferencesChanged() override;
diff --git a/content/common/view_messages.h b/content/common/view_messages.h
index b75aee16237cad6043cd6cfbe22a166e10327a16..7a7e96bcd17e730d2a282e3c7df4a0e3c56b75e0 100644
--- a/content/common/view_messages.h
+++ b/content/common/view_messages.h
@@ -95,6 +95,9 @@ IPC_STRUCT_TRAITS_END()
 
 // Messages sent from the browser to the renderer.
 
+// Whether to enable the Renderer scheduler background throttling.
+IPC_MESSAGE_ROUTED1(ViewMsg_SetSchedulerThrottling, bool /* allowed */)
+
 // This passes a set of webkit preferences down to the renderer.
 IPC_MESSAGE_ROUTED1(ViewMsg_UpdateWebPreferences,
                     content::WebPreferences)
diff --git a/content/public/browser/render_view_host.h b/content/public/browser/render_view_host.h
index 893c45a5c71b4e55ea323ae60e11d12ddb099efd..db9a1ff0fe1a912db83692ab17c620cff7446bcb 100644
--- a/content/public/browser/render_view_host.h
+++ b/content/public/browser/render_view_host.h
@@ -96,6 +96,9 @@ class CONTENT_EXPORT RenderViewHost : public IPC::Sender {
   // started.
   virtual void NotifyMoveOrResizeStarted() = 0;
 
+  // Disable/Enable scheduler throttling.
+  virtual void SetSchedulerThrottling(bool allowed) = 0;
+
   // TODO(mustaq): Replace "Webkit" from the following three method names.
   //
   // Returns the current WebKit preferences. Note: WebPreferences is cached, so
diff --git a/content/renderer/render_view_impl.cc b/content/renderer/render_view_impl.cc
index 5e83b930fa0f32c368ce1d5980436d4c2cc450bc..fdde69954f331cb28d0409d2987a147587685007 100644
--- a/content/renderer/render_view_impl.cc
+++ b/content/renderer/render_view_impl.cc
@@ -1183,6 +1183,8 @@ bool RenderViewImpl::OnMessageReceived(const IPC::Message& message) {
 
   bool handled = true;
   IPC_BEGIN_MESSAGE_MAP(RenderViewImpl, message)
+    IPC_MESSAGE_HANDLER(ViewMsg_SetSchedulerThrottling,
+                        OnSetSchedulerThrottling)
     IPC_MESSAGE_HANDLER(ViewMsg_UpdateTargetURL_ACK, OnUpdateTargetURLAck)
     IPC_MESSAGE_HANDLER(ViewMsg_UpdateWebPreferences, OnUpdateWebPreferences)
     IPC_MESSAGE_HANDLER(ViewMsg_MoveOrResizeStarted, OnMoveOrResizeStarted)
@@ -1698,6 +1700,12 @@ bool RenderViewImpl::GetContentStateImmediately() {
   return send_content_state_immediately_;
 }
 
+void RenderViewImpl::OnSetSchedulerThrottling(bool allowed) {
+  if (!GetWebView())
+    return;
+  GetWebView()->SetSchedulerThrottling(allowed);
+}
+
 void RenderViewImpl::ApplyPageVisibilityState(
     PageVisibilityState visibility_state,
     bool initial_setting) {
diff --git a/content/renderer/render_view_impl.h b/content/renderer/render_view_impl.h
index 701aac4f7865097899b76ee8c37db6eb32672b33..9e4ef233ce1df822d08bef03bfa5bf48774e0c24 100644
--- a/content/renderer/render_view_impl.h
+++ b/content/renderer/render_view_impl.h
@@ -413,6 +413,7 @@ class CONTENT_EXPORT RenderViewImpl : public blink::WebViewClient,
   void OnSetHistoryOffsetAndLength(int history_offset, int history_length);
   void OnSetRendererPrefs(
       const blink::mojom::RendererPreferences& renderer_prefs);
+  void OnSetSchedulerThrottling(bool allowed);
   void OnSuppressDialogsUntilSwapOut();
   void OnUpdateTargetURLAck();
   void OnUpdateWebPreferences(const WebPreferences& prefs);
diff --git a/third_party/blink/public/web/web_view.h b/third_party/blink/public/web/web_view.h
index 406c19b10b2e97ba05b9b3eb5c1ac0831023b3e2..dcbaf84dceee74da60ae8cc5466e08db317e3543 100644
--- a/third_party/blink/public/web/web_view.h
+++ b/third_party/blink/public/web/web_view.h
@@ -402,6 +402,7 @@ class WebView {
   // Scheduling -----------------------------------------------------------
 
   virtual PageScheduler* Scheduler() const = 0;
+  virtual void SetSchedulerThrottling(bool allowed) = 0;
 
   // Visibility -----------------------------------------------------------
 
diff --git a/third_party/blink/renderer/core/exported/web_view_impl.cc b/third_party/blink/renderer/core/exported/web_view_impl.cc
index 43fc2bb2de690486b38061760edec4c64cc19c0b..cd94798b733c1ea12ca60666dbabd7058aa77393 100644
--- a/third_party/blink/renderer/core/exported/web_view_impl.cc
+++ b/third_party/blink/renderer/core/exported/web_view_impl.cc
@@ -3355,12 +3355,20 @@ PageScheduler* WebViewImpl::Scheduler() const {
   return GetPage()->GetPageScheduler();
 }
 
+void WebViewImpl::SetSchedulerThrottling(bool allowed) {
+  DCHECK(GetPage());
+  scheduler_throttling_allowed_ = allowed;
+  GetPage()->GetPageScheduler()->SetPageVisible(allowed ?
+      (GetVisibilityState() == PageVisibilityState::kVisible) : true);
+}
+
 void WebViewImpl::SetVisibilityState(PageVisibilityState visibility_state,
                                      bool is_initial_state) {
   DCHECK(GetPage());
   GetPage()->SetVisibilityState(visibility_state, is_initial_state);
-  GetPage()->GetPageScheduler()->SetPageVisible(visibility_state ==
-                                                PageVisibilityState::kVisible);
+  GetPage()->GetPageScheduler()->SetPageVisible(
+      scheduler_throttling_allowed_ ?
+          (visibility_state == PageVisibilityState::kVisible) : true);
 }
 
 PageVisibilityState WebViewImpl::GetVisibilityState() {
diff --git a/third_party/blink/renderer/core/exported/web_view_impl.h b/third_party/blink/renderer/core/exported/web_view_impl.h
index 60cfa746367a03d308f0f26fef05839ecd6420b2..5c2b7a3aafbd72567c413be42d4bdf9665a97a2e 100644
--- a/third_party/blink/renderer/core/exported/web_view_impl.h
+++ b/third_party/blink/renderer/core/exported/web_view_impl.h
@@ -328,6 +328,7 @@ class CORE_EXPORT WebViewImpl final : public WebView,
   LocalDOMWindow* PagePopupWindow() const;
 
   PageScheduler* Scheduler() const override;
+  void SetSchedulerThrottling(bool allowed) override;
   void SetVisibilityState(PageVisibilityState visibility_state,
                           bool is_initial_state) override;
   PageVisibilityState GetVisibilityState() override;
@@ -696,6 +697,8 @@ class CORE_EXPORT WebViewImpl final : public WebView,
   // WebViewImpl::Close while handling an input event.
   bool debug_inside_input_handling_ = false;
 
+  bool scheduler_throttling_allowed_ = true;
+
   FloatSize elastic_overscroll_;
 
   // If true, we send IPC messages when |preferred_size_| changes.
