Don't preload rarely seen large images
[chromium-blink-merge.git] / components / plugins / renderer / loadable_plugin_placeholder.h
blob13b903df92292b05a8b89888a37542471a0151f7
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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_PLUGINS_RENDERER_LOADABLE_PLUGIN_PLACEHOLDER_H_
6 #define COMPONENTS_PLUGINS_RENDERER_LOADABLE_PLUGIN_PLACEHOLDER_H_
8 #include "base/memory/weak_ptr.h"
9 #include "base/timer/timer.h"
10 #include "components/plugins/renderer/plugin_placeholder.h"
11 #include "content/public/common/webplugininfo.h"
12 #include "content/public/renderer/plugin_instance_throttler.h"
13 #include "content/public/renderer/render_thread.h"
15 namespace plugins {
16 // Placeholders can be used if a plugin is missing or not available
17 // (blocked or disabled).
18 class LoadablePluginPlaceholder : public PluginPlaceholderBase {
19 public:
20 void set_blocked_for_background_tab(bool blocked_for_background_tab) {
21 is_blocked_for_background_tab_ = blocked_for_background_tab;
24 void set_blocked_for_prerendering(bool blocked_for_prerendering) {
25 is_blocked_for_prerendering_ = blocked_for_prerendering;
28 bool power_saver_enabled() const { return power_saver_enabled_; }
30 void set_power_saver_enabled(bool power_saver_enabled) {
31 power_saver_enabled_ = power_saver_enabled;
34 // Defer loading of plugin, and instead show the Power Saver poster image.
35 void BlockForPowerSaverPoster();
37 // When we load the plugin, use this already-created plugin, not a new one.
38 void SetPremadePlugin(content::PluginInstanceThrottler* throttler);
40 void DisallowLoading() { allow_loading_ = false; }
42 protected:
43 LoadablePluginPlaceholder(content::RenderFrame* render_frame,
44 blink::WebLocalFrame* frame,
45 const blink::WebPluginParams& params,
46 const std::string& html_data);
48 ~LoadablePluginPlaceholder() override;
50 void MarkPluginEssential(
51 content::PluginInstanceThrottler::PowerSaverUnthrottleMethod method);
53 void OnLoadBlockedPlugins(const std::string& identifier);
54 void OnSetIsPrerendering(bool is_prerendering);
56 void SetMessage(const base::string16& message);
57 void SetPluginInfo(const content::WebPluginInfo& plugin_info);
58 const content::WebPluginInfo& GetPluginInfo() const;
59 void SetIdentifier(const std::string& identifier);
60 const std::string& GetIdentifier() const;
61 bool LoadingAllowed() const { return allow_loading_; }
63 // Replace this placeholder with a different plugin (which could be
64 // a placeholder again).
65 void ReplacePlugin(blink::WebPlugin* new_plugin);
67 // Load the blocked plugin.
68 void LoadPlugin();
70 // Javascript callbacks:
71 void LoadCallback();
72 void DidFinishLoadingCallback();
74 private:
75 // WebViewPlugin::Delegate methods:
76 void PluginDestroyed() override;
77 v8::Local<v8::Object> GetV8ScriptableObject(
78 v8::Isolate* isolate) const override;
79 void OnUnobscuredSizeUpdate(const gfx::Size& unobscured_size) override;
81 // RenderFrameObserver methods:
82 void WasShown() override;
84 void UpdateMessage();
86 bool LoadingBlocked() const;
87 void RecheckSizeAndMaybeUnthrottle();
89 // Plugin creation is embedder-specific.
90 virtual blink::WebPlugin* CreatePlugin() = 0;
92 content::WebPluginInfo plugin_info_;
94 base::string16 message_;
96 // True if the plugin load was deferred due to page being a background tab.
97 // Plugin may be automatically loaded when the page is foregrounded.
98 bool is_blocked_for_background_tab_;
100 // True if the plugin was blocked because the page was being prerendered.
101 // Plugin may be automatically be loaded when the page is displayed.
102 bool is_blocked_for_prerendering_;
104 // True if the plugin load was deferred due to a Power Saver poster.
105 bool is_blocked_for_power_saver_poster_;
107 // True if power saver is enabled for this plugin and it has not been marked
108 // essential (by a click or retroactive whitelisting).
109 bool power_saver_enabled_;
111 // When we load, uses this premade plugin instead of creating a new one.
112 content::PluginInstanceThrottler* premade_throttler_;
114 bool allow_loading_;
116 bool finished_loading_;
117 std::string identifier_;
119 // Used to prevent re-entrancy during the size recheck for throttled plugins.
120 bool in_size_recheck_;
121 gfx::Size unobscured_size_;
122 base::OneShotTimer<LoadablePluginPlaceholder> size_update_timer_;
124 base::WeakPtrFactory<LoadablePluginPlaceholder> weak_factory_;
126 DISALLOW_COPY_AND_ASSIGN(LoadablePluginPlaceholder);
129 } // namespace plugins
131 #endif // COMPONENTS_PLUGINS_RENDERER_LOADABLE_PLUGIN_PLACEHOLDER_H_