From d156846036a3dc3fba67f6d799a2dfbb002d0571 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 8 Apr 2016 15:54:33 +0900 Subject: [PATCH] Add backgroundThrottling option to webPreferences --- atom/browser/api/atom_api_web_contents.cc | 23 ++++++++++++++++++----- atom/browser/api/atom_api_web_contents.h | 3 +++ docs/api/browser-window.md | 2 ++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index c2b04a87aa26..610d958ca1ea 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -19,6 +19,7 @@ #include "atom/browser/web_view_guest_delegate.h" #include "atom/common/api/api_messages.h" #include "atom/common/api/event_emitter_caller.h" +#include "atom/common/mouse_util.h" #include "atom/common/native_mate_converters/blink_converter.h" #include "atom/common/native_mate_converters/callback.h" #include "atom/common/native_mate_converters/content_converter.h" @@ -28,13 +29,14 @@ #include "atom/common/native_mate_converters/image_converter.h" #include "atom/common/native_mate_converters/string16_converter.h" #include "atom/common/native_mate_converters/value_converter.h" -#include "atom/common/mouse_util.h" +#include "atom/common/options_switches.h" #include "base/strings/string_util.h" #include "base/strings/utf_string_conversions.h" #include "brightray/browser/inspectable_web_contents.h" #include "brightray/browser/inspectable_web_contents_view.h" #include "chrome/browser/printing/print_view_manager_basic.h" #include "chrome/browser/printing/print_preview_message_handler.h" +#include "content/browser/renderer_host/render_widget_host_impl.h" #include "content/common/view_messages.h" #include "content/public/browser/favicon_status.h" #include "content/public/browser/native_web_keyboard_event.h" @@ -212,7 +214,9 @@ content::ServiceWorkerContext* GetServiceWorkerContext( WebContents::WebContents(content::WebContents* web_contents) : content::WebContentsObserver(web_contents), - type_(REMOTE) { + type_(REMOTE), + request_id_(0), + background_throttling_(true) { AttachAsUserData(web_contents); web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent()); } @@ -220,7 +224,11 @@ WebContents::WebContents(content::WebContents* web_contents) WebContents::WebContents(v8::Isolate* isolate, const mate::Dictionary& options) : embedder_(nullptr), - request_id_(0) { + request_id_(0), + background_throttling_(true) { + // Read options. + options.Get("backgroundThrottling", &background_throttling_); + // Whether it is a guest WebContents. bool is_guest = false; options.Get("isGuest", &is_guest); @@ -744,8 +752,13 @@ void WebContents::LoadURL(const GURL& url, const mate::Dictionary& options) { // override the background color set by the user. // We have to call it right after LoadURL because the RenderViewHost is only // created after loading a page. - web_contents()->GetRenderViewHost()->GetWidget()->GetView() - ->SetBackgroundColor(SK_ColorTRANSPARENT); + const auto view = web_contents()->GetRenderWidgetHostView(); + view->SetBackgroundColor(SK_ColorTRANSPARENT); + + // For the same reason we can only disable hidden here. + const auto host = static_cast( + view->GetRenderWidgetHost()); + host->disable_hidden_ = !background_throttling_; } void WebContents::DownloadURL(const GURL& url) { diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index 5fb1947f5795..ee5a25b26660 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -296,6 +296,9 @@ class WebContents : public mate::TrackableObject, // Request id used for findInPage request. uint32_t request_id_; + // Whether background throttling is disabled. + bool background_throttling_; + DISALLOW_COPY_AND_ASSIGN(WebContents); }; diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index 0e79659854ae..877a6d0f7457 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -179,6 +179,8 @@ The `webPreferences` option is an object that can have following properties: * `defaultMonospaceFontSize` Integer - Defaults to `13`. * `minimumFontSize` Integer - Defaults to `0`. * `defaultEncoding` String - Defaults to `ISO-8859-1`. +* `backgroundThrottling` Boolean - Whether to throttle animations and timers + when the page becomes background. Defaults to `true`. ## Events