| 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 #ifndef CONTENT_RENDERER_PEPPER_PLUGIN_INSTANCE_THROTTLER_IMPL_H_ | 5 #ifndef CONTENT_RENDERER_PEPPER_PLUGIN_INSTANCE_THROTTLER_IMPL_H_ |
| 6 #define CONTENT_RENDERER_PEPPER_PLUGIN_INSTANCE_THROTTLER_IMPL_H_ | 6 #define CONTENT_RENDERER_PEPPER_PLUGIN_INSTANCE_THROTTLER_IMPL_H_ |
| 7 | 7 |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
| 10 #include "base/observer_list.h" | 10 #include "base/observer_list.h" |
| 11 #include "content/common/content_export.h" | 11 #include "content/common/content_export.h" |
| 12 #include "content/public/renderer/plugin_instance_throttler.h" | 12 #include "content/public/renderer/plugin_instance_throttler.h" |
| 13 #include "ppapi/shared_impl/ppb_view_shared.h" | 13 #include "ppapi/shared_impl/ppb_view_shared.h" |
| 14 #include "third_party/WebKit/public/platform/WebRect.h" | |
| 15 | 14 |
| 16 namespace blink { | 15 namespace blink { |
| 17 class WebInputEvent; | 16 class WebInputEvent; |
| 17 struct WebRect; |
| 18 } | 18 } |
| 19 | 19 |
| 20 class SkBitmap; | 20 namespace content { |
| 21 | 21 |
| 22 namespace content { | 22 class RenderFrameImpl; |
| 23 | 23 |
| 24 class CONTENT_EXPORT PluginInstanceThrottlerImpl | 24 class CONTENT_EXPORT PluginInstanceThrottlerImpl |
| 25 : public PluginInstanceThrottler { | 25 : public PluginInstanceThrottler { |
| 26 public: | 26 public: |
| 27 PluginInstanceThrottlerImpl(RenderFrame* frame, | 27 PluginInstanceThrottlerImpl(bool power_saver_enabled); |
| 28 const GURL& plugin_url, | |
| 29 bool power_saver_enabled); | |
| 30 | 28 |
| 31 ~PluginInstanceThrottlerImpl() override; | 29 ~PluginInstanceThrottlerImpl() override; |
| 32 | 30 |
| 33 // PluginInstanceThrottler implementation: | 31 // PluginInstanceThrottler implementation: |
| 34 void AddObserver(Observer* observer) override; | 32 void AddObserver(Observer* observer) override; |
| 35 void RemoveObserver(Observer* observer) override; | 33 void RemoveObserver(Observer* observer) override; |
| 36 bool IsThrottled() const override; | 34 bool IsThrottled() const override; |
| 37 bool IsHiddenForPlaceholder() const override; | 35 bool IsHiddenForPlaceholder() const override; |
| 38 void MarkPluginEssential(PowerSaverUnthrottleMethod method) override; | 36 void MarkPluginEssential(PowerSaverUnthrottleMethod method) override; |
| 39 void SetHiddenForPlaceholder(bool hidden) override; | 37 void SetHiddenForPlaceholder(bool hidden) override; |
| 40 | 38 |
| 41 bool needs_representative_keyframe() const { | 39 bool needs_representative_keyframe() const { |
| 42 return state_ == POWER_SAVER_ENABLED_AWAITING_KEYFRAME; | 40 return state_ == THROTTLER_STATE_AWAITING_KEYFRAME; |
| 43 } | 41 } |
| 44 | 42 |
| 45 bool power_saver_enabled() const { | 43 bool power_saver_enabled() const { |
| 46 return state_ == POWER_SAVER_ENABLED_AWAITING_KEYFRAME || | 44 return state_ == THROTTLER_STATE_AWAITING_KEYFRAME || |
| 47 state_ == POWER_SAVER_ENABLED_PLUGIN_THROTTLED; | 45 state_ == THROTTLER_STATE_PLUGIN_THROTTLED; |
| 48 } | 46 } |
| 49 | 47 |
| 48 // Throttler needs to be initialized with the real plugin's view bounds. |
| 49 void Initialize(RenderFrameImpl* frame, |
| 50 const GURL& content_origin, |
| 51 const std::string& plugin_module_name, |
| 52 const blink::WebRect& bounds); |
| 53 |
| 50 // Called when the plugin flushes it's graphics context. Supplies the | 54 // Called when the plugin flushes it's graphics context. Supplies the |
| 51 // throttler with a candidate to use as the representative keyframe. | 55 // throttler with a candidate to use as the representative keyframe. |
| 52 void OnImageFlush(const SkBitmap* bitmap); | 56 void OnImageFlush(const SkBitmap* bitmap); |
| 53 | 57 |
| 54 // Returns true if |event| was handled and shouldn't be further processed. | 58 // Returns true if |event| was handled and shouldn't be further processed. |
| 55 bool ConsumeInputEvent(const blink::WebInputEvent& event); | 59 bool ConsumeInputEvent(const blink::WebInputEvent& event); |
| 56 | 60 |
| 57 private: | 61 private: |
| 58 friend class PluginInstanceThrottlerImplTest; | 62 friend class PluginInstanceThrottlerImplTest; |
| 59 | 63 |
| 60 enum State { | 64 enum ThrottlerState { |
| 61 // Initial state if Power Saver is disabled. We are just collecting metrics. | 65 // Power saver is disabled, but the plugin instance is still peripheral. |
| 62 POWER_SAVER_DISABLED, | 66 THROTTLER_STATE_POWER_SAVER_DISABLED, |
| 63 // Initial state if Power Saver is enabled. Waiting for a keyframe. | 67 // Plugin has been found to be peripheral, Plugin Power Saver is enabled, |
| 64 POWER_SAVER_ENABLED_AWAITING_KEYFRAME, | 68 // and throttler is awaiting a representative keyframe. |
| 65 // We've chosen a keyframe and the plug-in is throttled. | 69 THROTTLER_STATE_AWAITING_KEYFRAME, |
| 66 POWER_SAVER_ENABLED_PLUGIN_THROTTLED, | 70 // A representative keyframe has been chosen and the plugin is throttled. |
| 67 // Plugin instance is no longer considered peripheral. This can happen from | 71 THROTTLER_STATE_PLUGIN_THROTTLED, |
| 68 // a user click, whitelisting, or some other reason. We can end up in this | 72 // Plugin instance has been marked essential. |
| 69 // state regardless of whether power saver is enabled. | 73 THROTTLER_STATE_MARKED_ESSENTIAL, |
| 70 PLUGIN_INSTANCE_MARKED_ESSENTIAL | |
| 71 }; | 74 }; |
| 72 | 75 |
| 76 // Maximum number of frames to examine for a suitable keyframe. After that, we |
| 77 // simply suspend the plugin where it's at. Chosen arbitrarily. |
| 78 static const int kMaximumFramesToExamine; |
| 79 |
| 73 void EngageThrottle(); | 80 void EngageThrottle(); |
| 74 | 81 |
| 75 void TimeoutKeyframeExtraction(); | 82 ThrottlerState state_; |
| 76 | |
| 77 State state_; | |
| 78 | 83 |
| 79 bool is_hidden_for_placeholder_; | 84 bool is_hidden_for_placeholder_; |
| 80 | 85 |
| 81 // Number of consecutive interesting frames we've encountered. | 86 // Number of consecutive interesting frames we've encountered. |
| 82 int consecutive_interesting_frames_; | 87 int consecutive_interesting_frames_; |
| 83 | 88 |
| 84 // If true, take the next frame as the keyframe regardless of interestingness. | 89 // Number of frames we've examined to find a keyframe. |
| 85 bool keyframe_extraction_timed_out_; | 90 int frames_examined_; |
| 86 | 91 |
| 87 ObserverList<Observer> observer_list_; | 92 ObserverList<Observer> observer_list_; |
| 88 | 93 |
| 89 base::WeakPtrFactory<PluginInstanceThrottlerImpl> weak_factory_; | 94 base::WeakPtrFactory<PluginInstanceThrottlerImpl> weak_factory_; |
| 90 | 95 |
| 91 DISALLOW_COPY_AND_ASSIGN(PluginInstanceThrottlerImpl); | 96 DISALLOW_COPY_AND_ASSIGN(PluginInstanceThrottlerImpl); |
| 92 }; | 97 }; |
| 93 } | 98 } |
| 94 | 99 |
| 95 #endif // CONTENT_RENDERER_PEPPER_PLUGIN_INSTANCE_THROTTLER_IMPL_H_ | 100 #endif // CONTENT_RENDERER_PEPPER_PLUGIN_INSTANCE_THROTTLER_IMPL_H_ |
| OLD | NEW |