Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_PEPPER_PLUGIN_INSTANCE_THROTTLER_H_ | 5 #ifndef CONTENT_PUBLIC_RENDERER_PLUGIN_INSTANCE_THROTTLER_H_ |
| 6 #define CONTENT_RENDERER_PEPPER_PEPPER_PLUGIN_INSTANCE_THROTTLER_H_ | 6 #define CONTENT_PUBLIC_RENDERER_PLUGIN_INSTANCE_THROTTLER_H_ |
| 7 | 7 |
| 8 #include "base/callback.h" | |
| 9 #include "base/macros.h" | 8 #include "base/macros.h" |
| 10 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 11 #include "content/common/content_export.h" | 10 #include "content/common/content_export.h" |
| 12 #include "content/public/renderer/render_frame.h" | |
| 13 #include "ppapi/shared_impl/ppb_view_shared.h" | |
| 14 #include "third_party/WebKit/public/platform/WebRect.h" | |
| 15 | |
| 16 namespace blink { | |
| 17 class WebInputEvent; | |
| 18 } | |
| 19 | 11 |
| 20 class GURL; | 12 class GURL; |
| 21 class SkBitmap; | |
| 22 | 13 |
| 23 namespace content { | 14 namespace content { |
| 24 | 15 |
| 25 // Manages the Plugin Power Saver feature for a single Pepper plugin instance. | 16 class RenderFrame; |
| 17 | |
| 18 // Plugin instances are "peripheral" if they are heuristically determined to be | |
| 19 // not "essential" to the web page content. See RenderFrame comments for | |
|
Lei Zhang
2015/01/16 01:09:58
Which "RenderFrame comments" are we referring to?
tommycli
2015/01/16 01:26:10
Done.
| |
| 20 // details on these heuristics. | |
| 21 enum PluginPowerSaverMode { | |
| 22 // Plugin content is main content, and therefore never throttled. | |
| 23 POWER_SAVER_MODE_ESSENTIAL = 0, | |
| 24 // Plugin content is peripheral, but throttling is disabled. | |
| 25 POWER_SAVER_MODE_PERIPHERAL_UNTHROTTLED = 1, | |
| 26 // Plugin content is peripheral, and throttling is enabled. | |
| 27 POWER_SAVER_MODE_PERIPHERAL_THROTTLED = 2 | |
| 28 }; | |
| 29 | |
| 30 // This class manages the metric collection, throttling, and unthrottling of a | |
| 31 // single peripheral plugin instance. If the Power Saver feature is disabled, | |
| 32 // the plugin instance will never actually be throttled, but still collects | |
| 33 // user interaction metrics. | |
| 26 // | 34 // |
| 27 // A plugin must meet certain criteria in order to be throttled (e.g. it must | 35 // The process for throttling a plugin is as follows: |
| 28 // be a Flash plugin, it must meet certain size criteria, etc.). The process | |
| 29 // for throttling a plugin is as follows: | |
| 30 // 1) Attempt to find a representative keyframe to display as a placeholder for | 36 // 1) Attempt to find a representative keyframe to display as a placeholder for |
| 31 // the plugin. | 37 // the plugin. |
| 32 // 2) a) If a representative keyframe is found, throttle the plugin at that | 38 // 2) a) If a representative keyframe is found, throttle the plugin at that |
| 33 // keyframe. | 39 // keyframe. |
| 34 // b) If a representative keyframe is not found, throttle the plugin after a | 40 // b) If a representative keyframe is not found, throttle the plugin after a |
| 35 // certain period of time. | 41 // certain period of time. |
| 36 // | 42 // |
| 37 // The plugin will then be unthrottled by receiving a mouse click from the user. | 43 // The plugin will then be unthrottled by receiving a mouse click from the user. |
| 38 // | 44 // |
| 39 // To choose a representative keyframe, we first wait for a certain number of | 45 // To choose a representative keyframe, we first wait for a certain number of |
| 40 // "interesting" frames to be displayed by the plugin. A frame is called | 46 // "interesting" frames to be displayed by the plugin. A frame is called |
| 41 // interesting if it meets some heuristic. After we have seen a certain number | 47 // interesting if it meets some heuristic. After we have seen a certain number |
| 42 // of interesting frames, we throttle the plugin and use that frame as the | 48 // of interesting frames, we throttle the plugin and use that frame as the |
| 43 // representative keyframe. | 49 // representative keyframe. |
| 44 class CONTENT_EXPORT PepperPluginInstanceThrottler { | 50 class CONTENT_EXPORT PluginInstanceThrottler { |
| 45 public: | 51 public: |
| 46 // How the throttled power saver is unthrottled, if ever. | 52 // How the throttled power saver is unthrottled, if ever. |
| 47 // These numeric values are used in UMA logs; do not change them. | 53 // These numeric values are used in UMA logs; do not change them. |
| 48 enum PowerSaverUnthrottleMethod { | 54 enum PowerSaverUnthrottleMethod { |
| 49 UNTHROTTLE_METHOD_NEVER = 0, | 55 UNTHROTTLE_METHOD_NEVER = 0, |
| 50 UNTHROTTLE_METHOD_BY_CLICK = 1, | 56 UNTHROTTLE_METHOD_BY_CLICK = 1, |
| 51 UNTHROTTLE_METHOD_BY_WHITELIST = 2, | 57 UNTHROTTLE_METHOD_BY_WHITELIST = 2, |
| 52 UNTHROTTLE_METHOD_BY_AUDIO = 3, | 58 UNTHROTTLE_METHOD_BY_AUDIO = 3, |
| 53 UNTHROTTLE_METHOD_NUM_ITEMS | 59 UNTHROTTLE_METHOD_NUM_ITEMS |
| 54 }; | 60 }; |
| 55 | 61 |
| 56 PepperPluginInstanceThrottler( | 62 class Observer { |
| 63 public: | |
| 64 virtual void OnThrottleStateChange() = 0; | |
| 65 }; | |
| 66 | |
| 67 // Returns a nullptr if no throttler needed based on |power_saver_mode|. | |
| 68 static scoped_ptr<PluginInstanceThrottler> Get( | |
| 57 RenderFrame* frame, | 69 RenderFrame* frame, |
| 58 const blink::WebRect& bounds, | |
| 59 bool is_flash_plugin, | |
| 60 const GURL& plugin_url, | 70 const GURL& plugin_url, |
| 61 RenderFrame::PluginPowerSaverMode power_saver_mode, | 71 PluginPowerSaverMode power_saver_mode); |
| 62 const base::Closure& throttle_change_callback); | |
| 63 | 72 |
| 64 virtual ~PepperPluginInstanceThrottler(); | 73 virtual ~PluginInstanceThrottler() {} |
| 65 | 74 |
| 66 bool needs_representative_keyframe() const { | 75 virtual void AddObserver(Observer* observer) = 0; |
| 67 return needs_representative_keyframe_; | 76 virtual void RemoveObserver(Observer* observer) = 0; |
| 68 } | |
| 69 | 77 |
| 70 bool power_saver_enabled() const { | 78 virtual bool IsThrottled() const = 0; |
| 71 return power_saver_enabled_; | |
| 72 } | |
| 73 | 79 |
| 74 // Called when the plugin flushes it's graphics context. Supplies the | 80 // Marks the plugin as essential. Unthrottles the plugin if already throttled. |
| 75 // throttler with a candidate to use as the representative keyframe. | 81 virtual void MarkPluginEssential(PowerSaverUnthrottleMethod method) = 0; |
| 76 void OnImageFlush(const SkBitmap* bitmap); | |
| 77 | 82 |
| 78 bool is_throttled() const { return plugin_throttled_; } | 83 protected: |
| 79 | 84 PluginInstanceThrottler() {} |
| 80 // Returns true if |event| was handled and shouldn't be further processed. | |
| 81 bool ConsumeInputEvent(const blink::WebInputEvent& event); | |
| 82 | |
| 83 // Disables Power Saver and unthrottles the plugin if already throttled. | |
| 84 void DisablePowerSaver(PowerSaverUnthrottleMethod method); | |
| 85 | 85 |
| 86 private: | 86 private: |
| 87 friend class PepperPluginInstanceThrottlerTest; | 87 DISALLOW_COPY_AND_ASSIGN(PluginInstanceThrottler); |
| 88 | |
| 89 void SetPluginThrottled(bool throttled); | |
| 90 | |
| 91 // Plugin's bounds in view space. | |
| 92 blink::WebRect bounds_; | |
| 93 | |
| 94 // Called when the throttle state changes. | |
| 95 base::Closure throttle_change_callback_; | |
| 96 | |
| 97 bool is_flash_plugin_; | |
| 98 | |
| 99 // True if throttler is still waiting to find a representative keyframe. | |
| 100 bool needs_representative_keyframe_; | |
| 101 | |
| 102 // Number of consecutive interesting frames we've encountered. | |
| 103 int consecutive_interesting_frames_; | |
| 104 | |
| 105 // Set to true first time plugin is clicked. Used to collect metrics. | |
| 106 bool has_been_clicked_; | |
| 107 | |
| 108 // Indicates whether this plugin may be throttled to reduce power consumption. | |
| 109 // |power_saver_enabled_| implies |is_peripheral_content_|. | |
| 110 bool power_saver_enabled_; | |
| 111 | |
| 112 // Indicates whether this plugin was found to be peripheral content. | |
| 113 // This is separately tracked from |power_saver_enabled_| to collect UMAs. | |
| 114 // Always true if |power_saver_enabled_| is true. | |
| 115 bool is_peripheral_content_; | |
| 116 | |
| 117 // Indicates if the plugin is currently throttled. | |
| 118 bool plugin_throttled_; | |
| 119 | |
| 120 base::WeakPtrFactory<PepperPluginInstanceThrottler> weak_factory_; | |
| 121 | |
| 122 DISALLOW_COPY_AND_ASSIGN(PepperPluginInstanceThrottler); | |
| 123 }; | 88 }; |
| 124 } | 89 } |
| 125 | 90 |
| 126 #endif // CONTENT_RENDERER_PEPPER_PEPPER_PLUGIN_INSTANCE_THROTTLER_H_ | 91 #endif // CONTENT_PUBLIC_RENDERER_PLUGIN_INSTANCE_THROTTLER_H_ |
| OLD | NEW |