OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/renderer/pepper/pepper_plugin_instance_throttler.h" | |
6 | |
7 #include "base/metrics/histogram.h" | |
8 #include "base/metrics/sparse_histogram.h" | |
9 #include "base/time/time.h" | |
10 #include "content/public/common/content_constants.h" | |
11 #include "content/public/renderer/render_thread.h" | |
12 #include "third_party/WebKit/public/web/WebInputEvent.h" | |
13 #include "ui/gfx/color_utils.h" | |
14 #include "url/gurl.h" | |
15 | |
16 namespace content { | |
17 | |
18 namespace { | |
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"; | |
41 | |
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( | |
58 PepperPluginInstanceThrottler::PowerSaverUnthrottleMethod method) { | |
59 UMA_HISTOGRAM_ENUMERATION( | |
60 kPowerSaverUnthrottleHistogram, method, | |
61 PepperPluginInstanceThrottler::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 } | |
85 | |
86 // When we give up waiting for a suitable preview frame, and simply suspend | |
87 // the plugin where it's at. In milliseconds. | |
88 const int kThrottleTimeout = 5000; | |
89 | |
90 // 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 | |
92 // the most common luma bin. The same threshold is used for history thumbnails. | |
93 const double kAcceptableFrameMaximumBoringness = 0.94; | |
94 | |
95 const int kMinimumConsecutiveInterestingFrames = 4; | |
96 | |
97 } // namespace | |
98 | |
99 PepperPluginInstanceThrottler::PepperPluginInstanceThrottler( | |
100 RenderFrame* frame, | |
101 const blink::WebRect& bounds, | |
102 bool is_flash_plugin, | |
103 const GURL& plugin_url, | |
104 RenderFrame::PluginPowerSaverMode power_saver_mode, | |
105 const base::Closure& throttle_change_callback) | |
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), | |
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) { | |
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. | |
130 if (frame) { | |
131 frame->RegisterPeripheralPlugin( | |
132 content_origin, | |
133 base::Bind(&PepperPluginInstanceThrottler::DisablePowerSaver, | |
134 weak_factory_.GetWeakPtr(), UNTHROTTLE_METHOD_BY_WHITELIST)); | |
135 } | |
136 | |
137 if (power_saver_enabled_) { | |
138 needs_representative_keyframe_ = true; | |
139 base::MessageLoop::current()->PostDelayedTask( | |
140 FROM_HERE, | |
141 base::Bind(&PepperPluginInstanceThrottler::SetPluginThrottled, | |
142 weak_factory_.GetWeakPtr(), true /* throttled */), | |
143 base::TimeDelta::FromMilliseconds(kThrottleTimeout)); | |
144 } | |
145 } | |
146 | |
147 PepperPluginInstanceThrottler::~PepperPluginInstanceThrottler() { | |
148 } | |
149 | |
150 void PepperPluginInstanceThrottler::OnImageFlush(const SkBitmap* bitmap) { | |
151 if (!needs_representative_keyframe_ || !bitmap) | |
152 return; | |
153 | |
154 double boring_score = color_utils::CalculateBoringScore(*bitmap); | |
155 if (boring_score <= kAcceptableFrameMaximumBoringness) | |
156 ++consecutive_interesting_frames_; | |
157 else | |
158 consecutive_interesting_frames_ = 0; | |
159 | |
160 if (consecutive_interesting_frames_ >= kMinimumConsecutiveInterestingFrames) | |
161 SetPluginThrottled(true); | |
162 } | |
163 | |
164 bool PepperPluginInstanceThrottler::ConsumeInputEvent( | |
165 const blink::WebInputEvent& event) { | |
166 // 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 | |
168 // using PluginPlaceholder) so users aren't confused and try to click the | |
169 // Flash-internal 'Play' menu item. This is a stopgap solution. | |
170 if (event.modifiers & blink::WebInputEvent::Modifiers::RightButtonDown) | |
171 return false; | |
172 | |
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 && | |
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 } | |
196 | |
197 void PepperPluginInstanceThrottler::DisablePowerSaver( | |
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. | |
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 } | |
220 | |
221 } // namespace content | |
OLD | NEW |