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 "content/renderer/pepper/pepper_webplugin_impl.h"
15 #include "ppapi/shared_impl/ppb_view_shared.h"
16 #include "third_party/skia/include/core/SkBitmap.h"
17 #include "ui/gfx/geometry/size.h"
26 class PepperWebPluginImpl
;
27 class RenderFrameImpl
;
29 class CONTENT_EXPORT PluginInstanceThrottlerImpl
30 : public PluginInstanceThrottler
{
32 PluginInstanceThrottlerImpl();
34 ~PluginInstanceThrottlerImpl() override
;
36 // PluginInstanceThrottler implementation:
37 void AddObserver(Observer
* observer
) override
;
38 void RemoveObserver(Observer
* observer
) override
;
39 bool IsThrottled() const override
;
40 bool IsHiddenForPlaceholder() const override
;
41 void MarkPluginEssential(PowerSaverUnthrottleMethod method
) override
;
42 void SetHiddenForPlaceholder(bool hidden
) override
;
43 PepperWebPluginImpl
* GetWebPlugin() const override
;
44 const gfx::Size
& GetSize() const override
;
45 void NotifyAudioThrottled() override
;
47 void SetWebPlugin(PepperWebPluginImpl
* web_plugin
);
49 bool needs_representative_keyframe() const {
50 return state_
== THROTTLER_STATE_AWAITING_KEYFRAME
;
53 bool power_saver_enabled() const {
54 return state_
!= THROTTLER_STATE_MARKED_ESSENTIAL
;
57 void Initialize(RenderFrameImpl
* frame
,
58 const GURL
& content_origin
,
59 const std::string
& plugin_module_name
,
60 const gfx::Size
& unobscured_size
);
62 // Called when the plugin flushes it's graphics context. Supplies the
63 // throttler with a candidate to use as the representative keyframe.
64 void OnImageFlush(const SkBitmap
* bitmap
);
66 // Returns true if |event| was handled and shouldn't be further processed.
67 bool ConsumeInputEvent(const blink::WebInputEvent
& event
);
70 friend class PluginInstanceThrottlerImplTest
;
73 // Plugin has been found to be peripheral, Plugin Power Saver is enabled,
74 // and throttler is awaiting a representative keyframe.
75 THROTTLER_STATE_AWAITING_KEYFRAME
,
76 // A representative keyframe has been chosen and the plugin is throttled.
77 THROTTLER_STATE_PLUGIN_THROTTLED
,
78 // Plugin instance has been marked essential.
79 THROTTLER_STATE_MARKED_ESSENTIAL
,
82 // Maximum number of frames to examine for a suitable keyframe. After that, we
83 // simply suspend the plugin where it's at. Chosen arbitrarily.
84 static const int kMaximumFramesToExamine
;
86 void AudioThrottledFrameTimeout();
87 void EngageThrottle();
89 ThrottlerState state_
;
91 bool is_hidden_for_placeholder_
;
93 PepperWebPluginImpl
* web_plugin_
;
95 // Holds a reference to the last received frame. This doesn't actually copy
96 // the pixel data, but rather increments the reference count to the pixels.
97 SkBitmap last_received_frame_
;
99 // Number of frames we've examined to find a keyframe.
100 int frames_examined_
;
102 // Plugin's unobscured dimensions as of initialization.
103 gfx::Size unobscured_size_
;
105 // Video plugins with throttled audio often stop generating frames.
106 // This timer is so we don't wait forever for candidate poster frames.
107 bool audio_throttled_
;
108 base::DelayTimer
<PluginInstanceThrottlerImpl
> audio_throttled_frame_timeout_
;
110 base::ObserverList
<Observer
> observer_list_
;
112 base::WeakPtrFactory
<PluginInstanceThrottlerImpl
> weak_factory_
;
114 DISALLOW_COPY_AND_ASSIGN(PluginInstanceThrottlerImpl
);
118 #endif // CONTENT_RENDERER_PEPPER_PLUGIN_INSTANCE_THROTTLER_IMPL_H_