OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/pepper/pepper_plugin_instance_throttler.h" | 5 #include "content/renderer/pepper/pepper_plugin_instance_throttler.h" |
6 | 6 |
| 7 #include "base/command_line.h" |
7 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/metrics/histogram.h" |
| 10 #include "base/metrics/sparse_histogram.h" |
8 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 12 #include "content/public/common/content_constants.h" |
| 13 #include "content/public/common/content_switches.h" |
| 14 #include "content/renderer/pepper/plugin_power_saver_helper.h" |
| 15 #include "content/renderer/render_thread_impl.h" |
| 16 #include "third_party/WebKit/public/web/WebInputEvent.h" |
9 | 17 |
10 namespace content { | 18 namespace content { |
11 | 19 |
12 namespace { | 20 namespace { |
13 | 21 |
| 22 static const int kInfiniteRatio = 99999; |
| 23 |
| 24 #define UMA_HISTOGRAM_ASPECT_RATIO(name, width, height) \ |
| 25 UMA_HISTOGRAM_SPARSE_SLOWLY( \ |
| 26 name, (height) ? ((width)*100) / (height) : kInfiniteRatio); |
| 27 |
| 28 // Histogram tracking prevalence of tiny Flash instances. Units in pixels. |
| 29 enum PluginFlashTinyContentSize { |
| 30 TINY_CONTENT_SIZE_1_1 = 0, |
| 31 TINY_CONTENT_SIZE_5_5 = 1, |
| 32 TINY_CONTENT_SIZE_10_10 = 2, |
| 33 TINY_CONTENT_SIZE_LARGE = 3, |
| 34 TINY_CONTENT_SIZE_NUM_ITEMS |
| 35 }; |
| 36 |
| 37 // How the throttled power saver is unthrottled, if ever. |
| 38 // These numeric values are used in UMA logs; do not change them. |
| 39 enum PowerSaverUnthrottleMethod { |
| 40 UNTHROTTLE_METHOD_NEVER = 0, |
| 41 UNTHROTTLE_METHOD_BY_CLICK = 1, |
| 42 UNTHROTTLE_METHOD_BY_WHITELIST = 2, |
| 43 UNTHROTTLE_METHOD_NUM_ITEMS |
| 44 }; |
| 45 |
| 46 const char kFlashClickSizeAspectRatioHistogram[] = |
| 47 "Plugin.Flash.ClickSize.AspectRatio"; |
| 48 const char kFlashClickSizeHeightHistogram[] = "Plugin.Flash.ClickSize.Height"; |
| 49 const char kFlashClickSizeWidthHistogram[] = "Plugin.Flash.ClickSize.Width"; |
| 50 const char kFlashTinyContentSizeHistogram[] = "Plugin.Flash.TinyContentSize"; |
| 51 const char kPowerSaverUnthrottleHistogram[] = "Plugin.PowerSaver.Unthrottle"; |
| 52 |
| 53 // Record size metrics for all Flash instances. |
| 54 void RecordFlashSizeMetric(int width, int height) { |
| 55 PluginFlashTinyContentSize size = TINY_CONTENT_SIZE_LARGE; |
| 56 |
| 57 if (width <= 1 && height <= 1) |
| 58 size = TINY_CONTENT_SIZE_1_1; |
| 59 else if (width <= 5 && height <= 5) |
| 60 size = TINY_CONTENT_SIZE_5_5; |
| 61 else if (width <= 10 && height <= 10) |
| 62 size = TINY_CONTENT_SIZE_10_10; |
| 63 |
| 64 UMA_HISTOGRAM_ENUMERATION(kFlashTinyContentSizeHistogram, size, |
| 65 TINY_CONTENT_SIZE_NUM_ITEMS); |
| 66 } |
| 67 |
| 68 void RecordUnthrottleMethodMetric(PowerSaverUnthrottleMethod method) { |
| 69 UMA_HISTOGRAM_ENUMERATION(kPowerSaverUnthrottleHistogram, method, |
| 70 UNTHROTTLE_METHOD_NUM_ITEMS); |
| 71 } |
| 72 |
| 73 // Records size metrics for Flash instances that are clicked. |
| 74 void RecordFlashClickSizeMetric(int width, int height) { |
| 75 base::HistogramBase* width_histogram = base::LinearHistogram::FactoryGet( |
| 76 kFlashClickSizeWidthHistogram, |
| 77 0, // minimum width |
| 78 500, // maximum width |
| 79 100, // number of buckets. |
| 80 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 81 width_histogram->Add(width); |
| 82 |
| 83 base::HistogramBase* height_histogram = base::LinearHistogram::FactoryGet( |
| 84 kFlashClickSizeHeightHistogram, |
| 85 0, // minimum height |
| 86 400, // maximum height |
| 87 100, // number of buckets. |
| 88 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 89 height_histogram->Add(height); |
| 90 |
| 91 UMA_HISTOGRAM_ASPECT_RATIO(kFlashClickSizeAspectRatioHistogram, width, |
| 92 height); |
| 93 } |
| 94 |
14 // When we give up waiting for a suitable preview frame, and simply suspend | 95 // When we give up waiting for a suitable preview frame, and simply suspend |
15 // the plugin where it's at. In milliseconds. | 96 // the plugin where it's at. In milliseconds. |
16 const int kThrottleTimeout = 5000; | 97 const int kThrottleTimeout = 5000; |
17 } | 98 |
| 99 } // namespace |
18 | 100 |
19 PepperPluginInstanceThrottler::PepperPluginInstanceThrottler( | 101 PepperPluginInstanceThrottler::PepperPluginInstanceThrottler( |
20 const base::Closure& throttle_closure) { | 102 PluginPowerSaverHelper* power_saver_helper, |
21 DCHECK(!throttle_closure.is_null()); | 103 const blink::WebRect& bounds, |
22 base::MessageLoop::current()->PostDelayedTask( | 104 const std::string& module_name, |
23 FROM_HERE, throttle_closure, | 105 const GURL& plugin_url, |
24 base::TimeDelta::FromMilliseconds(kThrottleTimeout)); | 106 const base::Closure& throttle_change_callback) |
| 107 : bounds_(bounds), |
| 108 throttle_change_callback_(throttle_change_callback), |
| 109 is_flash_plugin_(module_name == kFlashPluginName), |
| 110 has_been_clicked_(false), |
| 111 power_saver_enabled_(false), |
| 112 is_peripheral_content_(false), |
| 113 plugin_throttled_(false), |
| 114 weak_factory_(this) { |
| 115 GURL content_origin = plugin_url.GetOrigin(); |
| 116 |
| 117 if (is_flash_plugin_ && RenderThread::Get()) { |
| 118 RenderThread::Get()->RecordAction( |
| 119 base::UserMetricsAction("Flash.PluginInstanceCreated")); |
| 120 RecordFlashSizeMetric(bounds.width, bounds.height); |
| 121 } |
| 122 |
| 123 bool cross_origin = false; |
| 124 is_peripheral_content_ = |
| 125 is_flash_plugin_ && |
| 126 power_saver_helper->ShouldThrottleContent(content_origin, bounds.width, |
| 127 bounds.height, &cross_origin); |
| 128 |
| 129 power_saver_enabled_ = is_peripheral_content_ && |
| 130 base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 131 switches::kEnablePluginPowerSaver); |
| 132 |
| 133 if (is_peripheral_content_) { |
| 134 // To collect UMAs, register peripheral content even if we don't throttle. |
| 135 power_saver_helper->RegisterPeripheralPlugin( |
| 136 content_origin, base::Bind(&PepperPluginInstanceThrottler:: |
| 137 DisablePowerSaverByRetroactiveWhitelist, |
| 138 weak_factory_.GetWeakPtr())); |
| 139 |
| 140 if (power_saver_enabled_) { |
| 141 base::MessageLoop::current()->PostDelayedTask( |
| 142 FROM_HERE, |
| 143 base::Bind(&PepperPluginInstanceThrottler::SetPluginThrottled, |
| 144 weak_factory_.GetWeakPtr(), true /* throttled */), |
| 145 base::TimeDelta::FromMilliseconds(kThrottleTimeout)); |
| 146 } |
| 147 } else if (cross_origin) { |
| 148 power_saver_helper->WhitelistContentOrigin(content_origin); |
| 149 } |
25 } | 150 } |
26 | 151 |
27 PepperPluginInstanceThrottler::~PepperPluginInstanceThrottler() { | 152 PepperPluginInstanceThrottler::~PepperPluginInstanceThrottler() { |
28 } | 153 } |
29 | 154 |
| 155 bool PepperPluginInstanceThrottler::ConsumeInputEvent( |
| 156 const blink::WebInputEvent& event) { |
| 157 if (!has_been_clicked_ && is_flash_plugin_ && |
| 158 event.type == blink::WebInputEvent::MouseDown) { |
| 159 has_been_clicked_ = true; |
| 160 RecordFlashClickSizeMetric(bounds_.width, bounds_.height); |
| 161 } |
| 162 |
| 163 if (event.type == blink::WebInputEvent::MouseUp && is_peripheral_content_) { |
| 164 is_peripheral_content_ = false; |
| 165 power_saver_enabled_ = false; |
| 166 |
| 167 RecordUnthrottleMethodMetric(UNTHROTTLE_METHOD_BY_CLICK); |
| 168 |
| 169 if (plugin_throttled_) { |
| 170 SetPluginThrottled(false /* throttled */); |
| 171 return true; |
| 172 } |
| 173 } |
| 174 |
| 175 return plugin_throttled_; |
| 176 } |
| 177 |
| 178 void PepperPluginInstanceThrottler::SetPluginThrottled(bool throttled) { |
| 179 // Do not throttle if we've already disabled power saver. |
| 180 if (!power_saver_enabled_ && throttled) |
| 181 return; |
| 182 |
| 183 plugin_throttled_ = throttled; |
| 184 throttle_change_callback_.Run(); |
| 185 } |
| 186 |
| 187 void PepperPluginInstanceThrottler::DisablePowerSaverByRetroactiveWhitelist() { |
| 188 if (!is_peripheral_content_) |
| 189 return; |
| 190 |
| 191 is_peripheral_content_ = false; |
| 192 power_saver_enabled_ = false; |
| 193 SetPluginThrottled(false); |
| 194 |
| 195 RecordUnthrottleMethodMetric(UNTHROTTLE_METHOD_BY_WHITELIST); |
| 196 } |
| 197 |
30 } // namespace content | 198 } // namespace content |
OLD | NEW |