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