Add backgroundThrottling option to webPreferences

This commit is contained in:
Cheng Zhao 2016-04-08 15:54:33 +09:00
parent d703a87317
commit d156846036
3 changed files with 23 additions and 5 deletions

View file

@ -19,6 +19,7 @@
#include "atom/browser/web_view_guest_delegate.h" #include "atom/browser/web_view_guest_delegate.h"
#include "atom/common/api/api_messages.h" #include "atom/common/api/api_messages.h"
#include "atom/common/api/event_emitter_caller.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/blink_converter.h"
#include "atom/common/native_mate_converters/callback.h" #include "atom/common/native_mate_converters/callback.h"
#include "atom/common/native_mate_converters/content_converter.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/image_converter.h"
#include "atom/common/native_mate_converters/string16_converter.h" #include "atom/common/native_mate_converters/string16_converter.h"
#include "atom/common/native_mate_converters/value_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/string_util.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "brightray/browser/inspectable_web_contents.h" #include "brightray/browser/inspectable_web_contents.h"
#include "brightray/browser/inspectable_web_contents_view.h" #include "brightray/browser/inspectable_web_contents_view.h"
#include "chrome/browser/printing/print_view_manager_basic.h" #include "chrome/browser/printing/print_view_manager_basic.h"
#include "chrome/browser/printing/print_preview_message_handler.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/common/view_messages.h"
#include "content/public/browser/favicon_status.h" #include "content/public/browser/favicon_status.h"
#include "content/public/browser/native_web_keyboard_event.h" #include "content/public/browser/native_web_keyboard_event.h"
@ -212,7 +214,9 @@ content::ServiceWorkerContext* GetServiceWorkerContext(
WebContents::WebContents(content::WebContents* web_contents) WebContents::WebContents(content::WebContents* web_contents)
: content::WebContentsObserver(web_contents), : content::WebContentsObserver(web_contents),
type_(REMOTE) { type_(REMOTE),
request_id_(0),
background_throttling_(true) {
AttachAsUserData(web_contents); AttachAsUserData(web_contents);
web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent()); web_contents->SetUserAgentOverride(GetBrowserContext()->GetUserAgent());
} }
@ -220,7 +224,11 @@ WebContents::WebContents(content::WebContents* web_contents)
WebContents::WebContents(v8::Isolate* isolate, WebContents::WebContents(v8::Isolate* isolate,
const mate::Dictionary& options) const mate::Dictionary& options)
: embedder_(nullptr), : embedder_(nullptr),
request_id_(0) { request_id_(0),
background_throttling_(true) {
// Read options.
options.Get("backgroundThrottling", &background_throttling_);
// Whether it is a guest WebContents. // Whether it is a guest WebContents.
bool is_guest = false; bool is_guest = false;
options.Get("isGuest", &is_guest); 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. // override the background color set by the user.
// We have to call it right after LoadURL because the RenderViewHost is only // We have to call it right after LoadURL because the RenderViewHost is only
// created after loading a page. // created after loading a page.
web_contents()->GetRenderViewHost()->GetWidget()->GetView() const auto view = web_contents()->GetRenderWidgetHostView();
->SetBackgroundColor(SK_ColorTRANSPARENT); view->SetBackgroundColor(SK_ColorTRANSPARENT);
// For the same reason we can only disable hidden here.
const auto host = static_cast<content::RenderWidgetHostImpl*>(
view->GetRenderWidgetHost());
host->disable_hidden_ = !background_throttling_;
} }
void WebContents::DownloadURL(const GURL& url) { void WebContents::DownloadURL(const GURL& url) {

View file

@ -296,6 +296,9 @@ class WebContents : public mate::TrackableObject<WebContents>,
// Request id used for findInPage request. // Request id used for findInPage request.
uint32_t request_id_; uint32_t request_id_;
// Whether background throttling is disabled.
bool background_throttling_;
DISALLOW_COPY_AND_ASSIGN(WebContents); DISALLOW_COPY_AND_ASSIGN(WebContents);
}; };

View file

@ -179,6 +179,8 @@ The `webPreferences` option is an object that can have following properties:
* `defaultMonospaceFontSize` Integer - Defaults to `13`. * `defaultMonospaceFontSize` Integer - Defaults to `13`.
* `minimumFontSize` Integer - Defaults to `0`. * `minimumFontSize` Integer - Defaults to `0`.
* `defaultEncoding` String - Defaults to `ISO-8859-1`. * `defaultEncoding` String - Defaults to `ISO-8859-1`.
* `backgroundThrottling` Boolean - Whether to throttle animations and timers
when the page becomes background. Defaults to `true`.
## Events ## Events