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 bool power_saver_enabled) |
105 const base::Closure& throttle_change_callback) | 46 : state_(power_saver_enabled ? POWER_SAVER_ENABLED_AWAITING_KEYFRAME |
106 : bounds_(bounds), | 47 : POWER_SAVER_DISABLED), |
107 throttle_change_callback_(throttle_change_callback), | |
108 is_flash_plugin_(is_flash_plugin), | |
109 needs_representative_keyframe_(false), | |
110 consecutive_interesting_frames_(0), | 48 consecutive_interesting_frames_(0), |
111 has_been_clicked_(false), | |
112 power_saver_enabled_(false), | |
113 is_peripheral_content_(power_saver_mode != | |
114 RenderFrame::POWER_SAVER_MODE_ESSENTIAL), | |
115 plugin_throttled_(false), | |
116 weak_factory_(this) { | 49 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. | 50 // To collect UMAs, register peripheral content even if power saver disabled. |
130 if (frame) { | 51 if (frame) { |
131 frame->RegisterPeripheralPlugin( | 52 frame->RegisterPeripheralPlugin( |
132 content_origin, | 53 plugin_url.GetOrigin(), |
133 base::Bind(&PepperPluginInstanceThrottler::DisablePowerSaver, | 54 base::Bind(&PluginInstanceThrottlerImpl::MarkPluginEssential, |
134 weak_factory_.GetWeakPtr(), UNTHROTTLE_METHOD_BY_WHITELIST)); | 55 weak_factory_.GetWeakPtr(), UNTHROTTLE_METHOD_BY_WHITELIST)); |
135 } | 56 } |
136 | 57 |
137 if (power_saver_enabled_) { | 58 if (power_saver_enabled) { |
138 needs_representative_keyframe_ = true; | |
139 base::MessageLoop::current()->PostDelayedTask( | 59 base::MessageLoop::current()->PostDelayedTask( |
140 FROM_HERE, | 60 FROM_HERE, base::Bind(&PluginInstanceThrottlerImpl::EngageThrottle, |
141 base::Bind(&PepperPluginInstanceThrottler::SetPluginThrottled, | 61 weak_factory_.GetWeakPtr()), |
142 weak_factory_.GetWeakPtr(), true /* throttled */), | |
143 base::TimeDelta::FromMilliseconds(kThrottleTimeout)); | 62 base::TimeDelta::FromMilliseconds(kThrottleTimeout)); |
144 } | 63 } |
145 } | 64 } |
146 | 65 |
147 PepperPluginInstanceThrottler::~PepperPluginInstanceThrottler() { | 66 PluginInstanceThrottlerImpl::~PluginInstanceThrottlerImpl() { |
148 } | 67 } |
149 | 68 |
150 void PepperPluginInstanceThrottler::OnImageFlush(const SkBitmap* bitmap) { | 69 void PluginInstanceThrottlerImpl::AddObserver(Observer* observer) { |
151 if (!needs_representative_keyframe_ || !bitmap) | 70 observer_list_.AddObserver(observer); |
| 71 } |
| 72 |
| 73 void PluginInstanceThrottlerImpl::RemoveObserver(Observer* observer) { |
| 74 observer_list_.RemoveObserver(observer); |
| 75 } |
| 76 |
| 77 bool PluginInstanceThrottlerImpl::IsThrottled() const { |
| 78 return state_ == POWER_SAVER_ENABLED_PLUGIN_THROTTLED; |
| 79 } |
| 80 |
| 81 void PluginInstanceThrottlerImpl::MarkPluginEssential( |
| 82 PowerSaverUnthrottleMethod method) { |
| 83 if (state_ == PLUGIN_INSTANCE_MARKED_ESSENTIAL) |
| 84 return; |
| 85 |
| 86 bool was_throttled = IsThrottled(); |
| 87 state_ = PLUGIN_INSTANCE_MARKED_ESSENTIAL; |
| 88 RecordUnthrottleMethodMetric(method); |
| 89 |
| 90 if (was_throttled) |
| 91 FOR_EACH_OBSERVER(Observer, observer_list_, OnThrottleStateChange()); |
| 92 } |
| 93 |
| 94 void PluginInstanceThrottlerImpl::OnImageFlush(const SkBitmap* bitmap) { |
| 95 DCHECK(needs_representative_keyframe()); |
| 96 if (!bitmap) |
152 return; | 97 return; |
153 | 98 |
154 double boring_score = color_utils::CalculateBoringScore(*bitmap); | 99 double boring_score = color_utils::CalculateBoringScore(*bitmap); |
155 if (boring_score <= kAcceptableFrameMaximumBoringness) | 100 if (boring_score <= kAcceptableFrameMaximumBoringness) |
156 ++consecutive_interesting_frames_; | 101 ++consecutive_interesting_frames_; |
157 else | 102 else |
158 consecutive_interesting_frames_ = 0; | 103 consecutive_interesting_frames_ = 0; |
159 | 104 |
160 if (consecutive_interesting_frames_ >= kMinimumConsecutiveInterestingFrames) | 105 if (consecutive_interesting_frames_ >= kMinimumConsecutiveInterestingFrames) |
161 SetPluginThrottled(true); | 106 EngageThrottle(); |
162 } | 107 } |
163 | 108 |
164 bool PepperPluginInstanceThrottler::ConsumeInputEvent( | 109 bool PluginInstanceThrottlerImpl::ConsumeInputEvent( |
165 const blink::WebInputEvent& event) { | 110 const blink::WebInputEvent& event) { |
166 // Always allow right-clicks through so users may verify it's a plug-in. | 111 // 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 | 112 // TODO(tommycli): We should instead show a custom context menu (probably |
168 // using PluginPlaceholder) so users aren't confused and try to click the | 113 // using PluginPlaceholder) so users aren't confused and try to click the |
169 // Flash-internal 'Play' menu item. This is a stopgap solution. | 114 // Flash-internal 'Play' menu item. This is a stopgap solution. |
170 if (event.modifiers & blink::WebInputEvent::Modifiers::RightButtonDown) | 115 if (event.modifiers & blink::WebInputEvent::Modifiers::RightButtonDown) |
171 return false; | 116 return false; |
172 | 117 |
173 if (!has_been_clicked_ && is_flash_plugin_ && | 118 if (state_ != PLUGIN_INSTANCE_MARKED_ESSENTIAL && |
174 event.type == blink::WebInputEvent::MouseDown && | 119 event.type == blink::WebInputEvent::MouseUp && |
175 (event.modifiers & blink::WebInputEvent::LeftButtonDown)) { | 120 (event.modifiers & blink::WebInputEvent::LeftButtonDown)) { |
176 has_been_clicked_ = true; | 121 bool was_throttled = IsThrottled(); |
177 RecordFlashClickSizeMetric(bounds_.width, bounds_.height); | 122 MarkPluginEssential(UNTHROTTLE_METHOD_BY_CLICK); |
| 123 return was_throttled; |
178 } | 124 } |
179 | 125 |
180 if (is_peripheral_content_ && event.type == blink::WebInputEvent::MouseUp && | 126 return IsThrottled(); |
181 (event.modifiers & blink::WebInputEvent::LeftButtonDown)) { | |
182 is_peripheral_content_ = false; | |
183 power_saver_enabled_ = false; | |
184 needs_representative_keyframe_ = false; | |
185 | |
186 RecordUnthrottleMethodMetric(UNTHROTTLE_METHOD_BY_CLICK); | |
187 | |
188 if (plugin_throttled_) { | |
189 SetPluginThrottled(false /* throttled */); | |
190 return true; | |
191 } | |
192 } | |
193 | |
194 return plugin_throttled_; | |
195 } | 127 } |
196 | 128 |
197 void PepperPluginInstanceThrottler::DisablePowerSaver( | 129 void PluginInstanceThrottlerImpl::EngageThrottle() { |
198 PowerSaverUnthrottleMethod method) { | 130 if (state_ != POWER_SAVER_ENABLED_AWAITING_KEYFRAME) |
199 if (!is_peripheral_content_) | |
200 return; | 131 return; |
201 | 132 |
202 is_peripheral_content_ = false; | 133 state_ = POWER_SAVER_ENABLED_PLUGIN_THROTTLED; |
203 power_saver_enabled_ = false; | 134 FOR_EACH_OBSERVER(Observer, observer_list_, OnThrottleStateChange()); |
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. | |
211 if (!power_saver_enabled_ && throttled) | |
212 return; | |
213 | |
214 // Once we change the throttle state, we will never need the snapshot again. | |
215 needs_representative_keyframe_ = false; | |
216 | |
217 plugin_throttled_ = throttled; | |
218 throttle_change_callback_.Run(); | |
219 } | 135 } |
220 | 136 |
221 } // namespace content | 137 } // namespace content |
OLD | NEW |