| Index: content/renderer/pepper/pepper_plugin_instance_throttler.cc
|
| diff --git a/content/renderer/pepper/pepper_plugin_instance_throttler.cc b/content/renderer/pepper/pepper_plugin_instance_throttler.cc
|
| index f1d02b9e0682fd014f21c5db009cc90e1a06cbf0..98a68c573c60c134abb04b5a1a0e06c290bbc534 100644
|
| --- a/content/renderer/pepper/pepper_plugin_instance_throttler.cc
|
| +++ b/content/renderer/pepper/pepper_plugin_instance_throttler.cc
|
| @@ -4,27 +4,139 @@
|
|
|
| #include "content/renderer/pepper/pepper_plugin_instance_throttler.h"
|
|
|
| +#include "base/command_line.h"
|
| #include "base/message_loop/message_loop.h"
|
| +#include "base/metrics/histogram.h"
|
| #include "base/time/time.h"
|
| +#include "content/public/common/content_constants.h"
|
| +#include "content/public/common/content_switches.h"
|
| +#include "content/renderer/pepper/plugin_module.h"
|
| +#include "content/renderer/pepper/pepper_plugin_instance_impl.h"
|
| +#include "content/renderer/pepper/plugin_power_saver_helper.h"
|
| +#include "content/renderer/render_frame_impl.h"
|
| +#include "third_party/WebKit/public/platform/WebRect.h"
|
| +#include "third_party/WebKit/public/web/WebElement.h"
|
| +#include "third_party/WebKit/public/web/WebInputEvent.h"
|
| +#include "third_party/WebKit/public/web/WebPluginContainer.h"
|
|
|
| namespace content {
|
|
|
| namespace {
|
|
|
| +// How the throttled power saver is unthrottled, if ever.
|
| +// These numeric values are used in UMA logs; do not change them.
|
| +enum PowerSaverUnthrottleMethod {
|
| + UNTHROTTLE_METHOD_NEVER = 0,
|
| + UNTHROTTLE_METHOD_BY_CLICK = 1,
|
| + UNTHROTTLE_METHOD_BY_WHITELIST = 2,
|
| + UNTHROTTLE_METHOD_NUM_ITEMS
|
| +};
|
| +
|
| // When we give up waiting for a suitable preview frame, and simply suspend
|
| // the plugin where it's at. In milliseconds.
|
| const int kThrottleTimeout = 5000;
|
| +
|
| +const char kPowerSaverUnthrottleHistogram[] = "Plugin.PowerSaverUnthrottle";
|
| +
|
| +void RecordUnthrottleMethodMetric(PowerSaverUnthrottleMethod method) {
|
| + UMA_HISTOGRAM_ENUMERATION(kPowerSaverUnthrottleHistogram, method,
|
| + UNTHROTTLE_METHOD_NUM_ITEMS);
|
| +}
|
| }
|
|
|
| PepperPluginInstanceThrottler::PepperPluginInstanceThrottler(
|
| - const base::Closure& throttle_closure) {
|
| - DCHECK(!throttle_closure.is_null());
|
| - base::MessageLoop::current()->PostDelayedTask(
|
| - FROM_HERE, throttle_closure,
|
| - base::TimeDelta::FromMilliseconds(kThrottleTimeout));
|
| + PepperPluginInstanceImpl* instance,
|
| + const base::Closure& throttle_change_callback)
|
| + : throttle_change_callback_(throttle_change_callback),
|
| + power_saver_enabled_(false),
|
| + is_peripheral_content_(false),
|
| + plugin_throttled_(false),
|
| + weak_factory_(this) {
|
| + DCHECK(instance);
|
| +
|
| + PluginPowerSaverHelper* power_saver_helper =
|
| + instance->render_frame()->plugin_power_saver_helper();
|
| + GURL content_origin = instance->GetPluginURL().GetOrigin();
|
| + blink::WebRect bounds =
|
| + instance->container()->element().boundsInViewportSpace();
|
| +
|
| + bool cross_origin = false;
|
| + is_peripheral_content_ =
|
| + instance->module()->name() == kFlashPluginName &&
|
| + power_saver_helper->ShouldThrottleContent(content_origin, bounds.width,
|
| + bounds.height, &cross_origin);
|
| +
|
| + power_saver_enabled_ = is_peripheral_content_ &&
|
| + base::CommandLine::ForCurrentProcess()->HasSwitch(
|
| + switches::kEnablePluginPowerSaver);
|
| +
|
| + if (is_peripheral_content_) {
|
| + // To collect UMAs, register peripheral content even if we don't throttle.
|
| + power_saver_helper->RegisterPeripheralPlugin(
|
| + content_origin, base::Bind(&PepperPluginInstanceThrottler::
|
| + DisablePowerSaverByRetroactiveWhitelist,
|
| + weak_factory_.GetWeakPtr()));
|
| +
|
| + if (power_saver_enabled_) {
|
| + base::MessageLoop::current()->PostDelayedTask(
|
| + FROM_HERE,
|
| + base::Bind(&PepperPluginInstanceThrottler::SetPluginThrottled,
|
| + weak_factory_.GetWeakPtr(), true /* throttled */),
|
| + base::TimeDelta::FromMilliseconds(kThrottleTimeout));
|
| + }
|
| + } else if (cross_origin) {
|
| + power_saver_helper->WhitelistContentOrigin(content_origin);
|
| + }
|
| }
|
|
|
| +PepperPluginInstanceThrottler::PepperPluginInstanceThrottler(
|
| + const base::Closure& throttle_change_callback,
|
| + bool power_saver_enabled,
|
| + bool is_peripheral_content)
|
| + : throttle_change_callback_(throttle_change_callback),
|
| + power_saver_enabled_(power_saver_enabled),
|
| + is_peripheral_content_(is_peripheral_content),
|
| + plugin_throttled_(false),
|
| + weak_factory_(this) {}
|
| +
|
| PepperPluginInstanceThrottler::~PepperPluginInstanceThrottler() {
|
| }
|
|
|
| +bool PepperPluginInstanceThrottler::ConsumeInputEvent(
|
| + const blink::WebInputEvent& event) {
|
| + if (event.type == blink::WebInputEvent::MouseUp && is_peripheral_content_) {
|
| + is_peripheral_content_ = false;
|
| + power_saver_enabled_ = false;
|
| +
|
| + RecordUnthrottleMethodMetric(UNTHROTTLE_METHOD_BY_CLICK);
|
| +
|
| + if (plugin_throttled_) {
|
| + SetPluginThrottled(false /* throttled */);
|
| + return true;
|
| + }
|
| + }
|
| +
|
| + return false;
|
| +}
|
| +
|
| +void PepperPluginInstanceThrottler::SetPluginThrottled(bool throttled) {
|
| + // Do not throttle if we've already disabled power saver.
|
| + if (!power_saver_enabled_ && throttled)
|
| + return;
|
| +
|
| + plugin_throttled_ = throttled;
|
| + throttle_change_callback_.Run();
|
| +}
|
| +
|
| +void PepperPluginInstanceThrottler::DisablePowerSaverByRetroactiveWhitelist() {
|
| + if (!is_peripheral_content_)
|
| + return;
|
| +
|
| + is_peripheral_content_ = false;
|
| + power_saver_enabled_ = false;
|
| + SetPluginThrottled(false);
|
| +
|
| + RecordUnthrottleMethodMetric(UNTHROTTLE_METHOD_BY_WHITELIST);
|
| +}
|
| +
|
| } // namespace content
|
|
|