| 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/plugin_instance_throttler_impl.h" |
| 6 | 6 |
| 7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
| 8 #include "base/metrics/sparse_histogram.h" | |
| 9 #include "base/time/time.h" | 8 #include "base/time/time.h" |
| 10 #include "content/public/common/content_constants.h" | 9 #include "content/public/common/content_constants.h" |
| 10 #include "content/public/renderer/render_frame.h" |
| 11 #include "content/public/renderer/render_thread.h" | 11 #include "content/public/renderer/render_thread.h" |
| 12 #include "third_party/WebKit/public/web/WebInputEvent.h" | 12 #include "third_party/WebKit/public/web/WebInputEvent.h" |
| 13 #include "ui/gfx/color_utils.h" | 13 #include "ui/gfx/color_utils.h" |
| 14 #include "url/gurl.h" | 14 #include "url/gurl.h" |
| 15 | 15 |
| 16 namespace content { | 16 namespace content { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 static const int kInfiniteRatio = 99999; | |
| 21 | |
| 22 #define UMA_HISTOGRAM_ASPECT_RATIO(name, width, height) \ | |
| 23 UMA_HISTOGRAM_SPARSE_SLOWLY( \ | |
| 24 name, (height) ? ((width)*100) / (height) : kInfiniteRatio); | |
| 25 | |
| 26 // Histogram tracking prevalence of tiny Flash instances. Units in pixels. | |
| 27 enum PluginFlashTinyContentSize { | |
| 28 TINY_CONTENT_SIZE_1_1 = 0, | |
| 29 TINY_CONTENT_SIZE_5_5 = 1, | |
| 30 TINY_CONTENT_SIZE_10_10 = 2, | |
| 31 TINY_CONTENT_SIZE_LARGE = 3, | |
| 32 TINY_CONTENT_SIZE_NUM_ITEMS | |
| 33 }; | |
| 34 | |
| 35 const char kFlashClickSizeAspectRatioHistogram[] = | |
| 36 "Plugin.Flash.ClickSize.AspectRatio"; | |
| 37 const char kFlashClickSizeHeightHistogram[] = "Plugin.Flash.ClickSize.Height"; | |
| 38 const char kFlashClickSizeWidthHistogram[] = "Plugin.Flash.ClickSize.Width"; | |
| 39 const char kFlashTinyContentSizeHistogram[] = "Plugin.Flash.TinyContentSize"; | |
| 40 const char kPowerSaverUnthrottleHistogram[] = "Plugin.PowerSaver.Unthrottle"; | 20 const char kPowerSaverUnthrottleHistogram[] = "Plugin.PowerSaver.Unthrottle"; |
| 41 | 21 |
| 42 // Record size metrics for all Flash instances. | |
| 43 void RecordFlashSizeMetric(int width, int height) { | |
| 44 PluginFlashTinyContentSize size = TINY_CONTENT_SIZE_LARGE; | |
| 45 | |
| 46 if (width <= 1 && height <= 1) | |
| 47 size = TINY_CONTENT_SIZE_1_1; | |
| 48 else if (width <= 5 && height <= 5) | |
| 49 size = TINY_CONTENT_SIZE_5_5; | |
| 50 else if (width <= 10 && height <= 10) | |
| 51 size = TINY_CONTENT_SIZE_10_10; | |
| 52 | |
| 53 UMA_HISTOGRAM_ENUMERATION(kFlashTinyContentSizeHistogram, size, | |
| 54 TINY_CONTENT_SIZE_NUM_ITEMS); | |
| 55 } | |
| 56 | |
| 57 void RecordUnthrottleMethodMetric( | 22 void RecordUnthrottleMethodMetric( |
| 58 PepperPluginInstanceThrottler::PowerSaverUnthrottleMethod method) { | 23 PluginInstanceThrottlerImpl::PowerSaverUnthrottleMethod method) { |
| 59 UMA_HISTOGRAM_ENUMERATION( | 24 UMA_HISTOGRAM_ENUMERATION( |
| 60 kPowerSaverUnthrottleHistogram, method, | 25 kPowerSaverUnthrottleHistogram, method, |
| 61 PepperPluginInstanceThrottler::UNTHROTTLE_METHOD_NUM_ITEMS); | 26 PluginInstanceThrottler::UNTHROTTLE_METHOD_NUM_ITEMS); |
| 62 } | |
| 63 | |
| 64 // Records size metrics for Flash instances that are clicked. | |
| 65 void RecordFlashClickSizeMetric(int width, int height) { | |
| 66 base::HistogramBase* width_histogram = base::LinearHistogram::FactoryGet( | |
| 67 kFlashClickSizeWidthHistogram, | |
| 68 0, // minimum width | |
| 69 500, // maximum width | |
| 70 100, // number of buckets. | |
| 71 base::HistogramBase::kUmaTargetedHistogramFlag); | |
| 72 width_histogram->Add(width); | |
| 73 | |
| 74 base::HistogramBase* height_histogram = base::LinearHistogram::FactoryGet( | |
| 75 kFlashClickSizeHeightHistogram, | |
| 76 0, // minimum height | |
| 77 400, // maximum height | |
| 78 100, // number of buckets. | |
| 79 base::HistogramBase::kUmaTargetedHistogramFlag); | |
| 80 height_histogram->Add(height); | |
| 81 | |
| 82 UMA_HISTOGRAM_ASPECT_RATIO(kFlashClickSizeAspectRatioHistogram, width, | |
| 83 height); | |
| 84 } | 27 } |
| 85 | 28 |
| 86 // When we give up waiting for a suitable preview frame, and simply suspend | 29 // When we give up waiting for a suitable preview frame, and simply suspend |
| 87 // the plugin where it's at. In milliseconds. | 30 // the plugin where it's at. In milliseconds. |
| 88 const int kThrottleTimeout = 5000; | 31 const int kThrottleTimeout = 5000; |
| 89 | 32 |
| 90 // Threshold for 'boring' score to accept a frame as good enough to be a | 33 // Threshold for 'boring' score to accept a frame as good enough to be a |
| 91 // representative keyframe. Units are the ratio of all pixels that are within | 34 // representative keyframe. Units are the ratio of all pixels that are within |
| 92 // the most common luma bin. The same threshold is used for history thumbnails. | 35 // the most common luma bin. The same threshold is used for history thumbnails. |
| 93 const double kAcceptableFrameMaximumBoringness = 0.94; | 36 const double kAcceptableFrameMaximumBoringness = 0.94; |
| 94 | 37 |
| 95 const int kMinimumConsecutiveInterestingFrames = 4; | 38 const int kMinimumConsecutiveInterestingFrames = 4; |
| 96 | 39 |
| 97 } // namespace | 40 } // namespace |
| 98 | 41 |
| 99 PepperPluginInstanceThrottler::PepperPluginInstanceThrottler( | 42 PluginInstanceThrottlerImpl::PluginInstanceThrottlerImpl( |
| 100 RenderFrame* frame, | 43 RenderFrame* frame, |
| 101 const blink::WebRect& bounds, | |
| 102 bool is_flash_plugin, | |
| 103 const GURL& plugin_url, | 44 const GURL& plugin_url, |
| 104 RenderFrame::PluginPowerSaverMode power_saver_mode, | 45 PluginPowerSaverMode power_saver_mode) |
| 105 const base::Closure& throttle_change_callback) | 46 : needs_representative_keyframe_(false), |
| 106 : bounds_(bounds), | |
| 107 throttle_change_callback_(throttle_change_callback), | |
| 108 is_flash_plugin_(is_flash_plugin), | |
| 109 needs_representative_keyframe_(false), | |
| 110 consecutive_interesting_frames_(0), | 47 consecutive_interesting_frames_(0), |
| 111 has_been_clicked_(false), | 48 power_saver_enabled_(power_saver_mode == |
| 112 power_saver_enabled_(false), | 49 POWER_SAVER_MODE_PERIPHERAL_THROTTLED), |
| 113 is_peripheral_content_(power_saver_mode != | 50 is_peripheral_content_(power_saver_mode != POWER_SAVER_MODE_ESSENTIAL), |
| 114 RenderFrame::POWER_SAVER_MODE_ESSENTIAL), | |
| 115 plugin_throttled_(false), | 51 plugin_throttled_(false), |
| 116 weak_factory_(this) { | 52 weak_factory_(this) { |
| 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 power_saver_enabled_ = | |
| 124 is_flash_plugin_ && | |
| 125 power_saver_mode == RenderFrame::POWER_SAVER_MODE_PERIPHERAL_THROTTLED; | |
| 126 | |
| 127 GURL content_origin = plugin_url.GetOrigin(); | |
| 128 | |
| 129 // To collect UMAs, register peripheral content even if power saver disabled. | 53 // To collect UMAs, register peripheral content even if power saver disabled. |
| 130 if (frame) { | 54 if (frame) { |
| 131 frame->RegisterPeripheralPlugin( | 55 frame->RegisterPeripheralPlugin( |
| 132 content_origin, | 56 plugin_url.GetOrigin(), |
| 133 base::Bind(&PepperPluginInstanceThrottler::DisablePowerSaver, | 57 base::Bind(&PluginInstanceThrottlerImpl::DisablePowerSaver, |
| 134 weak_factory_.GetWeakPtr(), UNTHROTTLE_METHOD_BY_WHITELIST)); | 58 weak_factory_.GetWeakPtr(), UNTHROTTLE_METHOD_BY_WHITELIST)); |
| 135 } | 59 } |
| 136 | 60 |
| 137 if (power_saver_enabled_) { | 61 if (power_saver_enabled_) { |
| 138 needs_representative_keyframe_ = true; | 62 needs_representative_keyframe_ = true; |
| 139 base::MessageLoop::current()->PostDelayedTask( | 63 base::MessageLoop::current()->PostDelayedTask( |
| 140 FROM_HERE, | 64 FROM_HERE, base::Bind(&PluginInstanceThrottlerImpl::SetPluginThrottled, |
| 141 base::Bind(&PepperPluginInstanceThrottler::SetPluginThrottled, | 65 weak_factory_.GetWeakPtr(), true /* throttled */), |
| 142 weak_factory_.GetWeakPtr(), true /* throttled */), | |
| 143 base::TimeDelta::FromMilliseconds(kThrottleTimeout)); | 66 base::TimeDelta::FromMilliseconds(kThrottleTimeout)); |
| 144 } | 67 } |
| 145 } | 68 } |
| 146 | 69 |
| 147 PepperPluginInstanceThrottler::~PepperPluginInstanceThrottler() { | 70 PluginInstanceThrottlerImpl::~PluginInstanceThrottlerImpl() { |
| 148 } | 71 } |
| 149 | 72 |
| 150 void PepperPluginInstanceThrottler::OnImageFlush(const SkBitmap* bitmap) { | 73 void PluginInstanceThrottlerImpl::AddObserver(Observer* observer) { |
| 74 observer_list_.AddObserver(observer); |
| 75 } |
| 76 |
| 77 void PluginInstanceThrottlerImpl::RemoveObserver(Observer* observer) { |
| 78 observer_list_.RemoveObserver(observer); |
| 79 } |
| 80 |
| 81 bool PluginInstanceThrottlerImpl::IsThrottled() const { |
| 82 return plugin_throttled_; |
| 83 } |
| 84 |
| 85 void PluginInstanceThrottlerImpl::DisablePowerSaver( |
| 86 PowerSaverUnthrottleMethod method) { |
| 87 if (!is_peripheral_content_) |
| 88 return; |
| 89 |
| 90 is_peripheral_content_ = false; |
| 91 power_saver_enabled_ = false; |
| 92 SetPluginThrottled(false); |
| 93 |
| 94 RecordUnthrottleMethodMetric(method); |
| 95 } |
| 96 |
| 97 void PluginInstanceThrottlerImpl::OnImageFlush(const SkBitmap* bitmap) { |
| 151 if (!needs_representative_keyframe_ || !bitmap) | 98 if (!needs_representative_keyframe_ || !bitmap) |
| 152 return; | 99 return; |
| 153 | 100 |
| 154 double boring_score = color_utils::CalculateBoringScore(*bitmap); | 101 double boring_score = color_utils::CalculateBoringScore(*bitmap); |
| 155 if (boring_score <= kAcceptableFrameMaximumBoringness) | 102 if (boring_score <= kAcceptableFrameMaximumBoringness) |
| 156 ++consecutive_interesting_frames_; | 103 ++consecutive_interesting_frames_; |
| 157 else | 104 else |
| 158 consecutive_interesting_frames_ = 0; | 105 consecutive_interesting_frames_ = 0; |
| 159 | 106 |
| 160 if (consecutive_interesting_frames_ >= kMinimumConsecutiveInterestingFrames) | 107 if (consecutive_interesting_frames_ >= kMinimumConsecutiveInterestingFrames) |
| 161 SetPluginThrottled(true); | 108 SetPluginThrottled(true); |
| 162 } | 109 } |
| 163 | 110 |
| 164 bool PepperPluginInstanceThrottler::ConsumeInputEvent( | 111 bool PluginInstanceThrottlerImpl::ConsumeInputEvent( |
| 165 const blink::WebInputEvent& event) { | 112 const blink::WebInputEvent& event) { |
| 166 // Always allow right-clicks through so users may verify it's a plug-in. | 113 // Always allow right-clicks through so users may verify it's a plug-in. |
| 167 // TODO(tommycli): We should instead show a custom context menu (probably | 114 // TODO(tommycli): We should instead show a custom context menu (probably |
| 168 // using PluginPlaceholder) so users aren't confused and try to click the | 115 // using PluginPlaceholder) so users aren't confused and try to click the |
| 169 // Flash-internal 'Play' menu item. This is a stopgap solution. | 116 // Flash-internal 'Play' menu item. This is a stopgap solution. |
| 170 if (event.modifiers & blink::WebInputEvent::Modifiers::RightButtonDown) | 117 if (event.modifiers & blink::WebInputEvent::Modifiers::RightButtonDown) |
| 171 return false; | 118 return false; |
| 172 | 119 |
| 173 if (!has_been_clicked_ && is_flash_plugin_ && | |
| 174 event.type == blink::WebInputEvent::MouseDown && | |
| 175 (event.modifiers & blink::WebInputEvent::LeftButtonDown)) { | |
| 176 has_been_clicked_ = true; | |
| 177 RecordFlashClickSizeMetric(bounds_.width, bounds_.height); | |
| 178 } | |
| 179 | |
| 180 if (is_peripheral_content_ && event.type == blink::WebInputEvent::MouseUp && | 120 if (is_peripheral_content_ && event.type == blink::WebInputEvent::MouseUp && |
| 181 (event.modifiers & blink::WebInputEvent::LeftButtonDown)) { | 121 (event.modifiers & blink::WebInputEvent::LeftButtonDown)) { |
| 182 is_peripheral_content_ = false; | 122 is_peripheral_content_ = false; |
| 183 power_saver_enabled_ = false; | 123 power_saver_enabled_ = false; |
| 184 needs_representative_keyframe_ = false; | 124 needs_representative_keyframe_ = false; |
| 185 | 125 |
| 186 RecordUnthrottleMethodMetric(UNTHROTTLE_METHOD_BY_CLICK); | 126 RecordUnthrottleMethodMetric(UNTHROTTLE_METHOD_BY_CLICK); |
| 187 | 127 |
| 188 if (plugin_throttled_) { | 128 if (plugin_throttled_) { |
| 189 SetPluginThrottled(false /* throttled */); | 129 SetPluginThrottled(false /* throttled */); |
| 190 return true; | 130 return true; |
| 191 } | 131 } |
| 192 } | 132 } |
| 193 | 133 |
| 194 return plugin_throttled_; | 134 return plugin_throttled_; |
| 195 } | 135 } |
| 196 | 136 |
| 197 void PepperPluginInstanceThrottler::DisablePowerSaver( | 137 void PluginInstanceThrottlerImpl::SetPluginThrottled(bool throttled) { |
| 198 PowerSaverUnthrottleMethod method) { | |
| 199 if (!is_peripheral_content_) | |
| 200 return; | |
| 201 | |
| 202 is_peripheral_content_ = false; | |
| 203 power_saver_enabled_ = false; | |
| 204 SetPluginThrottled(false); | |
| 205 | |
| 206 RecordUnthrottleMethodMetric(method); | |
| 207 } | |
| 208 | |
| 209 void PepperPluginInstanceThrottler::SetPluginThrottled(bool throttled) { | |
| 210 // Do not throttle if we've already disabled power saver. | 138 // Do not throttle if we've already disabled power saver. |
| 211 if (!power_saver_enabled_ && throttled) | 139 if (!power_saver_enabled_ && throttled) |
| 212 return; | 140 return; |
| 213 | 141 |
| 214 // Once we change the throttle state, we will never need the snapshot again. | 142 // Once we change the throttle state, we will never need the snapshot again. |
| 215 needs_representative_keyframe_ = false; | 143 needs_representative_keyframe_ = false; |
| 216 | 144 |
| 217 plugin_throttled_ = throttled; | 145 plugin_throttled_ = throttled; |
| 218 throttle_change_callback_.Run(); | 146 FOR_EACH_OBSERVER(Observer, observer_list_, OnThrottleStateChange()); |
| 219 } | 147 } |
| 220 | 148 |
| 221 } // namespace content | 149 } // namespace content |
| OLD | NEW |