Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / pepper / plugin_instance_throttler_impl.h
blobb33db8e8558d3d3460f3edd84ecebdd541ba7200
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
3 // found in the LICENSE file.
5 #ifndef CONTENT_RENDERER_PEPPER_PLUGIN_INSTANCE_THROTTLER_IMPL_H_
6 #define CONTENT_RENDERER_PEPPER_PLUGIN_INSTANCE_THROTTLER_IMPL_H_
8 #include "base/macros.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/observer_list.h"
11 #include "base/timer/timer.h"
12 #include "content/common/content_export.h"
13 #include "content/public/renderer/plugin_instance_throttler.h"
14 #include "ppapi/shared_impl/ppb_view_shared.h"
15 #include "third_party/skia/include/core/SkBitmap.h"
16 #include "ui/gfx/geometry/size.h"
18 namespace blink {
19 class WebInputEvent;
20 struct WebRect;
23 namespace content {
25 class RenderFrameImpl;
27 class CONTENT_EXPORT PluginInstanceThrottlerImpl
28 : public PluginInstanceThrottler {
29 public:
30 PluginInstanceThrottlerImpl();
32 ~PluginInstanceThrottlerImpl() override;
34 // PluginInstanceThrottler implementation:
35 void AddObserver(Observer* observer) override;
36 void RemoveObserver(Observer* observer) override;
37 bool IsThrottled() const override;
38 bool IsHiddenForPlaceholder() const override;
39 void MarkPluginEssential(PowerSaverUnthrottleMethod method) override;
40 void SetHiddenForPlaceholder(bool hidden) override;
41 blink::WebPlugin* GetWebPlugin() const override;
42 const gfx::Size& GetSize() const override;
43 void NotifyAudioThrottled() override;
45 void SetWebPlugin(blink::WebPlugin* web_plugin);
47 bool needs_representative_keyframe() const {
48 return state_ == THROTTLER_STATE_AWAITING_KEYFRAME;
51 bool power_saver_enabled() const {
52 return state_ != THROTTLER_STATE_MARKED_ESSENTIAL;
55 void Initialize(RenderFrameImpl* frame,
56 const GURL& content_origin,
57 const std::string& plugin_module_name,
58 const gfx::Size& unobscured_size);
60 // Called when the plugin flushes it's graphics context. Supplies the
61 // throttler with a candidate to use as the representative keyframe.
62 void OnImageFlush(const SkBitmap* bitmap);
64 // Returns true if |event| was handled and shouldn't be further processed.
65 bool ConsumeInputEvent(const blink::WebInputEvent& event);
67 private:
68 friend class PluginInstanceThrottlerImplTest;
70 enum ThrottlerState {
71 // Plugin has been found to be peripheral, Plugin Power Saver is enabled,
72 // and throttler is awaiting a representative keyframe.
73 THROTTLER_STATE_AWAITING_KEYFRAME,
74 // A representative keyframe has been chosen and the plugin is throttled.
75 THROTTLER_STATE_PLUGIN_THROTTLED,
76 // Plugin instance has been marked essential.
77 THROTTLER_STATE_MARKED_ESSENTIAL,
80 // Maximum number of frames to examine for a suitable keyframe. After that, we
81 // simply suspend the plugin where it's at. Chosen arbitrarily.
82 static const int kMaximumFramesToExamine;
84 void AudioThrottledFrameTimeout();
85 void EngageThrottle();
87 ThrottlerState state_;
89 bool is_hidden_for_placeholder_;
91 blink::WebPlugin* web_plugin_;
93 // Holds a reference to the last received frame. This doesn't actually copy
94 // the pixel data, but rather increments the reference count to the pixels.
95 SkBitmap last_received_frame_;
97 // Number of consecutive interesting frames we've encountered.
98 int consecutive_interesting_frames_;
100 // Number of frames we've examined to find a keyframe.
101 int frames_examined_;
103 // Plugin's unobscured dimensions as of initialization.
104 gfx::Size unobscured_size_;
106 // Video plugins with throttled audio often stop generating frames.
107 // This timer is so we don't wait forever for candidate poster frames.
108 bool audio_throttled_;
109 base::DelayTimer<PluginInstanceThrottlerImpl> audio_throttled_frame_timeout_;
111 ObserverList<Observer> observer_list_;
113 base::WeakPtrFactory<PluginInstanceThrottlerImpl> weak_factory_;
115 DISALLOW_COPY_AND_ASSIGN(PluginInstanceThrottlerImpl);
119 #endif // CONTENT_RENDERER_PEPPER_PLUGIN_INSTANCE_THROTTLER_IMPL_H_