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