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/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" | 10 #include "content/public/renderer/render_frame.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 "Plugin.PowerSaver.Unthrottle", method, | 52 "Plugin.PowerSaver.Unthrottle", method, |
53 PluginInstanceThrottler::UNTHROTTLE_METHOD_NUM_ITEMS); | 53 PluginInstanceThrottler::UNTHROTTLE_METHOD_NUM_ITEMS); |
54 } | 54 } |
55 | 55 |
56 PluginInstanceThrottlerImpl::PluginInstanceThrottlerImpl( | 56 PluginInstanceThrottlerImpl::PluginInstanceThrottlerImpl( |
57 RenderFrame* frame, | 57 RenderFrame* frame, |
58 const GURL& plugin_url, | 58 const GURL& plugin_url, |
59 bool power_saver_enabled) | 59 bool power_saver_enabled) |
60 : state_(power_saver_enabled ? POWER_SAVER_ENABLED_AWAITING_KEYFRAME | 60 : state_(power_saver_enabled ? POWER_SAVER_ENABLED_AWAITING_KEYFRAME |
61 : POWER_SAVER_DISABLED), | 61 : POWER_SAVER_DISABLED), |
| 62 is_hidden_for_placeholder_(false), |
62 consecutive_interesting_frames_(0), | 63 consecutive_interesting_frames_(0), |
| 64 keyframe_extraction_timed_out_(false), |
63 weak_factory_(this) { | 65 weak_factory_(this) { |
64 // To collect UMAs, register peripheral content even if power saver disabled. | 66 // To collect UMAs, register peripheral content even if power saver disabled. |
65 if (frame) { | 67 if (frame) { |
66 frame->RegisterPeripheralPlugin( | 68 frame->RegisterPeripheralPlugin( |
67 plugin_url.GetOrigin(), | 69 plugin_url.GetOrigin(), |
68 base::Bind(&PluginInstanceThrottlerImpl::MarkPluginEssential, | 70 base::Bind(&PluginInstanceThrottlerImpl::MarkPluginEssential, |
69 weak_factory_.GetWeakPtr(), UNTHROTTLE_METHOD_BY_WHITELIST)); | 71 weak_factory_.GetWeakPtr(), UNTHROTTLE_METHOD_BY_WHITELIST)); |
70 } | 72 } |
71 | 73 |
72 if (power_saver_enabled) { | 74 if (power_saver_enabled) { |
73 base::MessageLoop::current()->PostDelayedTask( | 75 base::MessageLoop::current()->PostDelayedTask( |
74 FROM_HERE, base::Bind(&PluginInstanceThrottlerImpl::EngageThrottle, | 76 FROM_HERE, |
75 weak_factory_.GetWeakPtr()), | 77 base::Bind(&PluginInstanceThrottlerImpl::TimeoutKeyframeExtraction, |
| 78 weak_factory_.GetWeakPtr()), |
76 base::TimeDelta::FromMilliseconds(kThrottleTimeout)); | 79 base::TimeDelta::FromMilliseconds(kThrottleTimeout)); |
77 } | 80 } |
78 } | 81 } |
79 | 82 |
80 PluginInstanceThrottlerImpl::~PluginInstanceThrottlerImpl() { | 83 PluginInstanceThrottlerImpl::~PluginInstanceThrottlerImpl() { |
| 84 FOR_EACH_OBSERVER(Observer, observer_list_, OnThrottlerDestroyed()); |
81 if (state_ != PLUGIN_INSTANCE_MARKED_ESSENTIAL) | 85 if (state_ != PLUGIN_INSTANCE_MARKED_ESSENTIAL) |
82 RecordUnthrottleMethodMetric(UNTHROTTLE_METHOD_NEVER); | 86 RecordUnthrottleMethodMetric(UNTHROTTLE_METHOD_NEVER); |
83 } | 87 } |
84 | 88 |
85 void PluginInstanceThrottlerImpl::AddObserver(Observer* observer) { | 89 void PluginInstanceThrottlerImpl::AddObserver(Observer* observer) { |
86 observer_list_.AddObserver(observer); | 90 observer_list_.AddObserver(observer); |
87 } | 91 } |
88 | 92 |
89 void PluginInstanceThrottlerImpl::RemoveObserver(Observer* observer) { | 93 void PluginInstanceThrottlerImpl::RemoveObserver(Observer* observer) { |
90 observer_list_.RemoveObserver(observer); | 94 observer_list_.RemoveObserver(observer); |
91 } | 95 } |
92 | 96 |
93 bool PluginInstanceThrottlerImpl::IsThrottled() const { | 97 bool PluginInstanceThrottlerImpl::IsThrottled() const { |
94 return state_ == POWER_SAVER_ENABLED_PLUGIN_THROTTLED; | 98 return state_ == POWER_SAVER_ENABLED_PLUGIN_THROTTLED; |
95 } | 99 } |
96 | 100 |
| 101 bool PluginInstanceThrottlerImpl::IsHiddenForPlaceholder() const { |
| 102 return is_hidden_for_placeholder_; |
| 103 } |
| 104 |
97 void PluginInstanceThrottlerImpl::MarkPluginEssential( | 105 void PluginInstanceThrottlerImpl::MarkPluginEssential( |
98 PowerSaverUnthrottleMethod method) { | 106 PowerSaverUnthrottleMethod method) { |
99 if (state_ == PLUGIN_INSTANCE_MARKED_ESSENTIAL) | 107 if (state_ == PLUGIN_INSTANCE_MARKED_ESSENTIAL) |
100 return; | 108 return; |
101 | 109 |
102 bool was_throttled = IsThrottled(); | 110 bool was_throttled = IsThrottled(); |
103 state_ = PLUGIN_INSTANCE_MARKED_ESSENTIAL; | 111 state_ = PLUGIN_INSTANCE_MARKED_ESSENTIAL; |
104 RecordUnthrottleMethodMetric(method); | 112 RecordUnthrottleMethodMetric(method); |
105 | 113 |
106 if (was_throttled) | 114 if (was_throttled) |
107 FOR_EACH_OBSERVER(Observer, observer_list_, OnThrottleStateChange()); | 115 FOR_EACH_OBSERVER(Observer, observer_list_, OnThrottleStateChange()); |
108 } | 116 } |
109 | 117 |
| 118 void PluginInstanceThrottlerImpl::SetHiddenForPlaceholder(bool hidden) { |
| 119 is_hidden_for_placeholder_ = hidden; |
| 120 FOR_EACH_OBSERVER(Observer, observer_list_, OnHiddenForPlaceholder(hidden)); |
| 121 } |
| 122 |
110 void PluginInstanceThrottlerImpl::OnImageFlush(const SkBitmap* bitmap) { | 123 void PluginInstanceThrottlerImpl::OnImageFlush(const SkBitmap* bitmap) { |
111 DCHECK(needs_representative_keyframe()); | 124 DCHECK(needs_representative_keyframe()); |
112 if (!bitmap) | 125 if (!bitmap) |
113 return; | 126 return; |
114 | 127 |
115 double boring_score = color_utils::CalculateBoringScore(*bitmap); | 128 double boring_score = color_utils::CalculateBoringScore(*bitmap); |
116 if (boring_score <= kAcceptableFrameMaximumBoringness) | 129 if (boring_score <= kAcceptableFrameMaximumBoringness) |
117 ++consecutive_interesting_frames_; | 130 ++consecutive_interesting_frames_; |
118 else | 131 else |
119 consecutive_interesting_frames_ = 0; | 132 consecutive_interesting_frames_ = 0; |
120 | 133 |
121 if (consecutive_interesting_frames_ >= kMinimumConsecutiveInterestingFrames) | 134 if (keyframe_extraction_timed_out_ || |
| 135 consecutive_interesting_frames_ >= kMinimumConsecutiveInterestingFrames) { |
| 136 FOR_EACH_OBSERVER(Observer, observer_list_, OnKeyframeExtracted(bitmap)); |
122 EngageThrottle(); | 137 EngageThrottle(); |
| 138 } |
123 } | 139 } |
124 | 140 |
125 bool PluginInstanceThrottlerImpl::ConsumeInputEvent( | 141 bool PluginInstanceThrottlerImpl::ConsumeInputEvent( |
126 const blink::WebInputEvent& event) { | 142 const blink::WebInputEvent& event) { |
127 // Always allow right-clicks through so users may verify it's a plug-in. | 143 // Always allow right-clicks through so users may verify it's a plug-in. |
128 // TODO(tommycli): We should instead show a custom context menu (probably | 144 // TODO(tommycli): We should instead show a custom context menu (probably |
129 // using PluginPlaceholder) so users aren't confused and try to click the | 145 // using PluginPlaceholder) so users aren't confused and try to click the |
130 // Flash-internal 'Play' menu item. This is a stopgap solution. | 146 // Flash-internal 'Play' menu item. This is a stopgap solution. |
131 if (event.modifiers & blink::WebInputEvent::Modifiers::RightButtonDown) | 147 if (event.modifiers & blink::WebInputEvent::Modifiers::RightButtonDown) |
132 return false; | 148 return false; |
(...skipping 10 matching lines...) Expand all Loading... |
143 } | 159 } |
144 | 160 |
145 void PluginInstanceThrottlerImpl::EngageThrottle() { | 161 void PluginInstanceThrottlerImpl::EngageThrottle() { |
146 if (state_ != POWER_SAVER_ENABLED_AWAITING_KEYFRAME) | 162 if (state_ != POWER_SAVER_ENABLED_AWAITING_KEYFRAME) |
147 return; | 163 return; |
148 | 164 |
149 state_ = POWER_SAVER_ENABLED_PLUGIN_THROTTLED; | 165 state_ = POWER_SAVER_ENABLED_PLUGIN_THROTTLED; |
150 FOR_EACH_OBSERVER(Observer, observer_list_, OnThrottleStateChange()); | 166 FOR_EACH_OBSERVER(Observer, observer_list_, OnThrottleStateChange()); |
151 } | 167 } |
152 | 168 |
| 169 void PluginInstanceThrottlerImpl::TimeoutKeyframeExtraction() { |
| 170 keyframe_extraction_timed_out_ = true; |
| 171 } |
| 172 |
153 } // namespace content | 173 } // namespace content |
OLD | NEW |