Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: content/renderer/pepper/plugin_instance_throttler_impl.cc

Issue 904913003: Plugin Power Saver: Fix implicitly sized and below the fold plugins. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/plugin_instance_throttler_impl.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/time/time.h" 8 #include "base/time/time.h"
9 #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" 10 #include "content/public/renderer/render_thread.h"
11 #include "content/renderer/render_frame_impl.h"
12 #include "third_party/WebKit/public/platform/WebRect.h"
12 #include "third_party/WebKit/public/web/WebInputEvent.h" 13 #include "third_party/WebKit/public/web/WebInputEvent.h"
14 #include "third_party/WebKit/public/web/WebPluginParams.h"
13 #include "ui/gfx/color_utils.h" 15 #include "ui/gfx/color_utils.h"
14 #include "url/gurl.h" 16 #include "url/gurl.h"
15 17
16 namespace content { 18 namespace content {
17 19
18 namespace { 20 namespace {
19 21
20 // When we give up waiting for a suitable preview frame, and simply suspend
21 // the plugin where it's at. In milliseconds.
22 const int kThrottleTimeout = 5000;
23
24 // Threshold for 'boring' score to accept a frame as good enough to be a 22 // Threshold for 'boring' score to accept a frame as good enough to be a
25 // representative keyframe. Units are the ratio of all pixels that are within 23 // representative keyframe. Units are the ratio of all pixels that are within
26 // the most common luma bin. The same threshold is used for history thumbnails. 24 // the most common luma bin. The same threshold is used for history thumbnails.
27 const double kAcceptableFrameMaximumBoringness = 0.94; 25 const double kAcceptableFrameMaximumBoringness = 0.94;
28 26
29 const int kMinimumConsecutiveInterestingFrames = 4; 27 const int kMinimumConsecutiveInterestingFrames = 4;
30 28
31 } // namespace 29 } // namespace
32 30
33 // static 31 // static
34 scoped_ptr<PluginInstanceThrottler> PluginInstanceThrottler::Get( 32 const int PluginInstanceThrottlerImpl::kMaximumFramesToExamine = 150;
35 RenderFrame* frame,
36 const GURL& plugin_url,
37 PluginPowerSaverMode power_saver_mode) {
38 if (power_saver_mode == PluginPowerSaverMode::POWER_SAVER_MODE_ESSENTIAL)
39 return nullptr;
40 33
41 bool power_saver_enabled = 34 // static
42 power_saver_mode == 35 scoped_ptr<PluginInstanceThrottler> PluginInstanceThrottler::Create(
43 PluginPowerSaverMode::POWER_SAVER_MODE_PERIPHERAL_THROTTLED; 36 bool power_saver_enabled) {
44 return make_scoped_ptr( 37 return make_scoped_ptr(new PluginInstanceThrottlerImpl(power_saver_enabled));
45 new PluginInstanceThrottlerImpl(frame, plugin_url, power_saver_enabled));
46 } 38 }
47 39
48 // static 40 // static
49 void PluginInstanceThrottler::RecordUnthrottleMethodMetric( 41 void PluginInstanceThrottler::RecordUnthrottleMethodMetric(
50 PluginInstanceThrottlerImpl::PowerSaverUnthrottleMethod method) { 42 PluginInstanceThrottlerImpl::PowerSaverUnthrottleMethod method) {
51 UMA_HISTOGRAM_ENUMERATION( 43 UMA_HISTOGRAM_ENUMERATION(
52 "Plugin.PowerSaver.Unthrottle", method, 44 "Plugin.PowerSaver.Unthrottle", method,
53 PluginInstanceThrottler::UNTHROTTLE_METHOD_NUM_ITEMS); 45 PluginInstanceThrottler::UNTHROTTLE_METHOD_NUM_ITEMS);
54 } 46 }
55 47
56 PluginInstanceThrottlerImpl::PluginInstanceThrottlerImpl( 48 PluginInstanceThrottlerImpl::PluginInstanceThrottlerImpl(
57 RenderFrame* frame,
58 const GURL& plugin_url,
59 bool power_saver_enabled) 49 bool power_saver_enabled)
60 : state_(power_saver_enabled ? POWER_SAVER_ENABLED_AWAITING_KEYFRAME 50 : state_(power_saver_enabled ? THROTTLER_STATE_AWAITING_KEYFRAME
61 : POWER_SAVER_DISABLED), 51 : THROTTLER_STATE_POWER_SAVER_DISABLED),
62 is_hidden_for_placeholder_(false), 52 is_hidden_for_placeholder_(false),
63 consecutive_interesting_frames_(0), 53 consecutive_interesting_frames_(0),
64 keyframe_extraction_timed_out_(false), 54 frames_examined_(0),
65 weak_factory_(this) { 55 weak_factory_(this) {
66 // To collect UMAs, register peripheral content even if power saver disabled.
67 if (frame) {
68 frame->RegisterPeripheralPlugin(
69 plugin_url.GetOrigin(),
70 base::Bind(&PluginInstanceThrottlerImpl::MarkPluginEssential,
71 weak_factory_.GetWeakPtr(), UNTHROTTLE_METHOD_BY_WHITELIST));
72 }
73
74 if (power_saver_enabled) {
75 base::MessageLoop::current()->PostDelayedTask(
76 FROM_HERE,
77 base::Bind(&PluginInstanceThrottlerImpl::TimeoutKeyframeExtraction,
78 weak_factory_.GetWeakPtr()),
79 base::TimeDelta::FromMilliseconds(kThrottleTimeout));
80 }
81 } 56 }
82 57
83 PluginInstanceThrottlerImpl::~PluginInstanceThrottlerImpl() { 58 PluginInstanceThrottlerImpl::~PluginInstanceThrottlerImpl() {
84 FOR_EACH_OBSERVER(Observer, observer_list_, OnThrottlerDestroyed()); 59 FOR_EACH_OBSERVER(Observer, observer_list_, OnThrottlerDestroyed());
85 if (state_ != PLUGIN_INSTANCE_MARKED_ESSENTIAL) 60 if (state_ != THROTTLER_STATE_MARKED_ESSENTIAL)
86 RecordUnthrottleMethodMetric(UNTHROTTLE_METHOD_NEVER); 61 RecordUnthrottleMethodMetric(UNTHROTTLE_METHOD_NEVER);
87 } 62 }
88 63
89 void PluginInstanceThrottlerImpl::AddObserver(Observer* observer) { 64 void PluginInstanceThrottlerImpl::AddObserver(Observer* observer) {
90 observer_list_.AddObserver(observer); 65 observer_list_.AddObserver(observer);
91 } 66 }
92 67
93 void PluginInstanceThrottlerImpl::RemoveObserver(Observer* observer) { 68 void PluginInstanceThrottlerImpl::RemoveObserver(Observer* observer) {
94 observer_list_.RemoveObserver(observer); 69 observer_list_.RemoveObserver(observer);
95 } 70 }
96 71
97 bool PluginInstanceThrottlerImpl::IsThrottled() const { 72 bool PluginInstanceThrottlerImpl::IsThrottled() const {
98 return state_ == POWER_SAVER_ENABLED_PLUGIN_THROTTLED; 73 return state_ == THROTTLER_STATE_PLUGIN_THROTTLED;
99 } 74 }
100 75
101 bool PluginInstanceThrottlerImpl::IsHiddenForPlaceholder() const { 76 bool PluginInstanceThrottlerImpl::IsHiddenForPlaceholder() const {
102 return is_hidden_for_placeholder_; 77 return is_hidden_for_placeholder_;
103 } 78 }
104 79
105 void PluginInstanceThrottlerImpl::MarkPluginEssential( 80 void PluginInstanceThrottlerImpl::MarkPluginEssential(
106 PowerSaverUnthrottleMethod method) { 81 PowerSaverUnthrottleMethod method) {
107 if (state_ == PLUGIN_INSTANCE_MARKED_ESSENTIAL) 82 if (state_ == THROTTLER_STATE_MARKED_ESSENTIAL)
108 return; 83 return;
109 84
110 bool was_throttled = IsThrottled(); 85 bool was_throttled = IsThrottled();
111 state_ = PLUGIN_INSTANCE_MARKED_ESSENTIAL; 86 state_ = THROTTLER_STATE_MARKED_ESSENTIAL;
112 RecordUnthrottleMethodMetric(method); 87 RecordUnthrottleMethodMetric(method);
113 88
114 if (was_throttled) 89 if (was_throttled)
115 FOR_EACH_OBSERVER(Observer, observer_list_, OnThrottleStateChange()); 90 FOR_EACH_OBSERVER(Observer, observer_list_, OnThrottleStateChange());
116 } 91 }
117 92
118 void PluginInstanceThrottlerImpl::SetHiddenForPlaceholder(bool hidden) { 93 void PluginInstanceThrottlerImpl::SetHiddenForPlaceholder(bool hidden) {
119 is_hidden_for_placeholder_ = hidden; 94 is_hidden_for_placeholder_ = hidden;
120 FOR_EACH_OBSERVER(Observer, observer_list_, OnHiddenForPlaceholder(hidden)); 95 FOR_EACH_OBSERVER(Observer, observer_list_, OnHiddenForPlaceholder(hidden));
121 } 96 }
122 97
98 void PluginInstanceThrottlerImpl::Initialize(
99 RenderFrameImpl* frame,
100 const GURL& content_origin,
101 const std::string& plugin_module_name,
102 const blink::WebRect& bounds) {
103 // |frame| may be nullptr in tests.
104 if (frame) {
105 PluginPowerSaverHelper* helper = frame->plugin_power_saver_helper();
106 bool cross_origin_main_content = false;
107 if (!helper->ShouldThrottleContent(content_origin, plugin_module_name,
108 bounds.width, bounds.height,
109 &cross_origin_main_content)) {
110 state_ = THROTTLER_STATE_MARKED_ESSENTIAL;
111
112 if (cross_origin_main_content)
113 helper->WhitelistContentOrigin(content_origin);
114
115 return;
116 }
117
118 // To collect UMAs, register peripheral content even if power saver mode
119 // is disabled.
120 helper->RegisterPeripheralPlugin(
121 content_origin,
122 base::Bind(&PluginInstanceThrottlerImpl::MarkPluginEssential,
123 weak_factory_.GetWeakPtr(), UNTHROTTLE_METHOD_BY_WHITELIST));
124 }
125 }
126
123 void PluginInstanceThrottlerImpl::OnImageFlush(const SkBitmap* bitmap) { 127 void PluginInstanceThrottlerImpl::OnImageFlush(const SkBitmap* bitmap) {
124 DCHECK(needs_representative_keyframe()); 128 DCHECK(needs_representative_keyframe());
125 if (!bitmap) 129 if (!bitmap)
126 return; 130 return;
127 131
132 ++frames_examined_;
133
128 double boring_score = color_utils::CalculateBoringScore(*bitmap); 134 double boring_score = color_utils::CalculateBoringScore(*bitmap);
129 if (boring_score <= kAcceptableFrameMaximumBoringness) 135 if (boring_score <= kAcceptableFrameMaximumBoringness)
130 ++consecutive_interesting_frames_; 136 ++consecutive_interesting_frames_;
131 else 137 else
132 consecutive_interesting_frames_ = 0; 138 consecutive_interesting_frames_ = 0;
133 139
134 if (keyframe_extraction_timed_out_ || 140 if (frames_examined_ >= kMaximumFramesToExamine ||
135 consecutive_interesting_frames_ >= kMinimumConsecutiveInterestingFrames) { 141 consecutive_interesting_frames_ >= kMinimumConsecutiveInterestingFrames) {
136 FOR_EACH_OBSERVER(Observer, observer_list_, OnKeyframeExtracted(bitmap)); 142 FOR_EACH_OBSERVER(Observer, observer_list_, OnKeyframeExtracted(bitmap));
137 EngageThrottle(); 143 EngageThrottle();
138 } 144 }
139 } 145 }
140 146
141 bool PluginInstanceThrottlerImpl::ConsumeInputEvent( 147 bool PluginInstanceThrottlerImpl::ConsumeInputEvent(
142 const blink::WebInputEvent& event) { 148 const blink::WebInputEvent& event) {
143 // Always allow right-clicks through so users may verify it's a plug-in. 149 // Always allow right-clicks through so users may verify it's a plug-in.
144 // TODO(tommycli): We should instead show a custom context menu (probably 150 // TODO(tommycli): We should instead show a custom context menu (probably
145 // using PluginPlaceholder) so users aren't confused and try to click the 151 // using PluginPlaceholder) so users aren't confused and try to click the
146 // Flash-internal 'Play' menu item. This is a stopgap solution. 152 // Flash-internal 'Play' menu item. This is a stopgap solution.
147 if (event.modifiers & blink::WebInputEvent::Modifiers::RightButtonDown) 153 if (event.modifiers & blink::WebInputEvent::Modifiers::RightButtonDown)
148 return false; 154 return false;
149 155
150 if (state_ != PLUGIN_INSTANCE_MARKED_ESSENTIAL && 156 if (state_ != THROTTLER_STATE_MARKED_ESSENTIAL &&
151 event.type == blink::WebInputEvent::MouseUp && 157 event.type == blink::WebInputEvent::MouseUp &&
152 (event.modifiers & blink::WebInputEvent::LeftButtonDown)) { 158 (event.modifiers & blink::WebInputEvent::LeftButtonDown)) {
153 bool was_throttled = IsThrottled(); 159 bool was_throttled = IsThrottled();
154 MarkPluginEssential(UNTHROTTLE_METHOD_BY_CLICK); 160 MarkPluginEssential(UNTHROTTLE_METHOD_BY_CLICK);
155 return was_throttled; 161 return was_throttled;
156 } 162 }
157 163
158 return IsThrottled(); 164 return IsThrottled();
159 } 165 }
160 166
161 void PluginInstanceThrottlerImpl::EngageThrottle() { 167 void PluginInstanceThrottlerImpl::EngageThrottle() {
162 if (state_ != POWER_SAVER_ENABLED_AWAITING_KEYFRAME) 168 if (state_ != THROTTLER_STATE_AWAITING_KEYFRAME)
163 return; 169 return;
164 170
165 state_ = POWER_SAVER_ENABLED_PLUGIN_THROTTLED; 171 state_ = THROTTLER_STATE_PLUGIN_THROTTLED;
166 FOR_EACH_OBSERVER(Observer, observer_list_, OnThrottleStateChange()); 172 FOR_EACH_OBSERVER(Observer, observer_list_, OnThrottleStateChange());
167 } 173 }
168 174
169 void PluginInstanceThrottlerImpl::TimeoutKeyframeExtraction() {
170 keyframe_extraction_timed_out_ = true;
171 }
172
173 } // namespace content 175 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698