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/public/renderer/plugin_instance_throttler.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 PluginInstanceThrottler::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 PluginInstanceThrottler::PluginInstanceThrottler( |
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(&PluginInstanceThrottler::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(&PluginInstanceThrottler::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 PluginInstanceThrottler::~PluginInstanceThrottler() { |
148 } | 71 } |
149 | 72 |
150 void PepperPluginInstanceThrottler::OnImageFlush(const SkBitmap* bitmap) { | 73 void PluginInstanceThrottler::AddObserver(Observer* observer) { |
| 74 observer_list_.AddObserver(observer); |
| 75 } |
| 76 |
| 77 void PluginInstanceThrottler::RemoveObserver(Observer* observer) { |
| 78 observer_list_.RemoveObserver(observer); |
| 79 } |
| 80 |
| 81 void PluginInstanceThrottler::OnImageFlush(const SkBitmap* bitmap) { |
151 if (!needs_representative_keyframe_ || !bitmap) | 82 if (!needs_representative_keyframe_ || !bitmap) |
152 return; | 83 return; |
153 | 84 |
154 double boring_score = color_utils::CalculateBoringScore(*bitmap); | 85 double boring_score = color_utils::CalculateBoringScore(*bitmap); |
155 if (boring_score <= kAcceptableFrameMaximumBoringness) | 86 if (boring_score <= kAcceptableFrameMaximumBoringness) |
156 ++consecutive_interesting_frames_; | 87 ++consecutive_interesting_frames_; |
157 else | 88 else |
158 consecutive_interesting_frames_ = 0; | 89 consecutive_interesting_frames_ = 0; |
159 | 90 |
160 if (consecutive_interesting_frames_ >= kMinimumConsecutiveInterestingFrames) | 91 if (consecutive_interesting_frames_ >= kMinimumConsecutiveInterestingFrames) |
161 SetPluginThrottled(true); | 92 SetPluginThrottled(true); |
162 } | 93 } |
163 | 94 |
164 bool PepperPluginInstanceThrottler::ConsumeInputEvent( | 95 bool PluginInstanceThrottler::ConsumeInputEvent( |
165 const blink::WebInputEvent& event) { | 96 const blink::WebInputEvent& event) { |
166 // Always allow right-clicks through so users may verify it's a plug-in. | 97 // 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 | 98 // TODO(tommycli): We should instead show a custom context menu (probably |
168 // using PluginPlaceholder) so users aren't confused and try to click the | 99 // using PluginPlaceholder) so users aren't confused and try to click the |
169 // Flash-internal 'Play' menu item. This is a stopgap solution. | 100 // Flash-internal 'Play' menu item. This is a stopgap solution. |
170 if (event.modifiers & blink::WebInputEvent::Modifiers::RightButtonDown) | 101 if (event.modifiers & blink::WebInputEvent::Modifiers::RightButtonDown) |
171 return false; | 102 return false; |
172 | 103 |
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 && | 104 if (is_peripheral_content_ && event.type == blink::WebInputEvent::MouseUp && |
181 (event.modifiers & blink::WebInputEvent::LeftButtonDown)) { | 105 (event.modifiers & blink::WebInputEvent::LeftButtonDown)) { |
182 is_peripheral_content_ = false; | 106 is_peripheral_content_ = false; |
183 power_saver_enabled_ = false; | 107 power_saver_enabled_ = false; |
184 needs_representative_keyframe_ = false; | 108 needs_representative_keyframe_ = false; |
185 | 109 |
186 RecordUnthrottleMethodMetric(UNTHROTTLE_METHOD_BY_CLICK); | 110 RecordUnthrottleMethodMetric(UNTHROTTLE_METHOD_BY_CLICK); |
187 | 111 |
188 if (plugin_throttled_) { | 112 if (plugin_throttled_) { |
189 SetPluginThrottled(false /* throttled */); | 113 SetPluginThrottled(false /* throttled */); |
190 return true; | 114 return true; |
191 } | 115 } |
192 } | 116 } |
193 | 117 |
194 return plugin_throttled_; | 118 return plugin_throttled_; |
195 } | 119 } |
196 | 120 |
197 void PepperPluginInstanceThrottler::DisablePowerSaver( | 121 void PluginInstanceThrottler::DisablePowerSaver( |
198 PowerSaverUnthrottleMethod method) { | 122 PowerSaverUnthrottleMethod method) { |
199 if (!is_peripheral_content_) | 123 if (!is_peripheral_content_) |
200 return; | 124 return; |
201 | 125 |
202 is_peripheral_content_ = false; | 126 is_peripheral_content_ = false; |
203 power_saver_enabled_ = false; | 127 power_saver_enabled_ = false; |
204 SetPluginThrottled(false); | 128 SetPluginThrottled(false); |
205 | 129 |
206 RecordUnthrottleMethodMetric(method); | 130 RecordUnthrottleMethodMetric(method); |
207 } | 131 } |
208 | 132 |
209 void PepperPluginInstanceThrottler::SetPluginThrottled(bool throttled) { | 133 void PluginInstanceThrottler::SetPluginThrottled(bool throttled) { |
210 // Do not throttle if we've already disabled power saver. | 134 // Do not throttle if we've already disabled power saver. |
211 if (!power_saver_enabled_ && throttled) | 135 if (!power_saver_enabled_ && throttled) |
212 return; | 136 return; |
213 | 137 |
214 // Once we change the throttle state, we will never need the snapshot again. | 138 // Once we change the throttle state, we will never need the snapshot again. |
215 needs_representative_keyframe_ = false; | 139 needs_representative_keyframe_ = false; |
216 | 140 |
217 plugin_throttled_ = throttled; | 141 plugin_throttled_ = throttled; |
218 throttle_change_callback_.Run(); | 142 FOR_EACH_OBSERVER(Observer, observer_list_, OnThrottleStateChange()); |
219 } | 143 } |
220 | 144 |
221 } // namespace content | 145 } // namespace content |
OLD | NEW |