Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: components/plugins/renderer/loadable_plugin_placeholder.cc

Issue 1111893003: Plugin Power Saver: Fix double-counting NEVER case in LoadablePluginPlaceholder. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add a correction Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/plugins/renderer/loadable_plugin_placeholder.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 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 #include "components/plugins/renderer/loadable_plugin_placeholder.h" 5 #include "components/plugins/renderer/loadable_plugin_placeholder.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/json/string_escape.h" 9 #include "base/json/string_escape.h"
10 #include "base/strings/string_piece.h" 10 #include "base/strings/string_piece.h"
(...skipping 29 matching lines...) Expand all
40 void LoadablePluginPlaceholder::BlockForPowerSaverPoster() { 40 void LoadablePluginPlaceholder::BlockForPowerSaverPoster() {
41 DCHECK(!is_blocked_for_power_saver_poster_); 41 DCHECK(!is_blocked_for_power_saver_poster_);
42 is_blocked_for_power_saver_poster_ = true; 42 is_blocked_for_power_saver_poster_ = true;
43 43
44 render_frame()->RegisterPeripheralPlugin( 44 render_frame()->RegisterPeripheralPlugin(
45 GURL(GetPluginParams().url).GetOrigin(), 45 GURL(GetPluginParams().url).GetOrigin(),
46 base::Bind(&LoadablePluginPlaceholder::MarkPluginEssential, 46 base::Bind(&LoadablePluginPlaceholder::MarkPluginEssential,
47 weak_factory_.GetWeakPtr(), 47 weak_factory_.GetWeakPtr(),
48 PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_WHITELIST)); 48 PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_WHITELIST));
49 } 49 }
50 #endif
51 50
52 void LoadablePluginPlaceholder::SetPremadePlugin( 51 void LoadablePluginPlaceholder::SetPremadePlugin(
53 content::PluginInstanceThrottler* throttler) { 52 content::PluginInstanceThrottler* throttler) {
54 DCHECK(throttler); 53 DCHECK(throttler);
55 DCHECK(!premade_throttler_); 54 DCHECK(!premade_throttler_);
56 premade_throttler_ = throttler; 55 premade_throttler_ = throttler;
57
58 premade_throttler_->AddObserver(this);
59 } 56 }
57 #endif
60 58
61 LoadablePluginPlaceholder::LoadablePluginPlaceholder( 59 LoadablePluginPlaceholder::LoadablePluginPlaceholder(
62 content::RenderFrame* render_frame, 60 content::RenderFrame* render_frame,
63 WebLocalFrame* frame, 61 WebLocalFrame* frame,
64 const WebPluginParams& params, 62 const WebPluginParams& params,
65 const std::string& html_data, 63 const std::string& html_data,
66 GURL placeholderDataUrl) 64 GURL placeholderDataUrl)
67 : PluginPlaceholder(render_frame, 65 : PluginPlaceholder(render_frame,
68 frame, 66 frame,
69 params, 67 params,
70 html_data, 68 html_data,
71 placeholderDataUrl), 69 placeholderDataUrl),
72 is_blocked_for_background_tab_(false), 70 is_blocked_for_background_tab_(false),
73 is_blocked_for_prerendering_(false), 71 is_blocked_for_prerendering_(false),
74 is_blocked_for_power_saver_poster_(false), 72 is_blocked_for_power_saver_poster_(false),
75 power_saver_enabled_(false), 73 power_saver_enabled_(false),
76 plugin_marked_essential_(false),
77 premade_throttler_(nullptr), 74 premade_throttler_(nullptr),
78 allow_loading_(false), 75 allow_loading_(false),
79 placeholder_was_replaced_(false),
80 hidden_(false), 76 hidden_(false),
81 finished_loading_(false), 77 finished_loading_(false),
82 weak_factory_(this) { 78 weak_factory_(this) {
83 } 79 }
84 80
85 LoadablePluginPlaceholder::~LoadablePluginPlaceholder() { 81 LoadablePluginPlaceholder::~LoadablePluginPlaceholder() {
86 #if defined(ENABLE_PLUGINS)
87 DCHECK(!premade_throttler_);
88
89 if (!plugin_marked_essential_ && !placeholder_was_replaced_ &&
90 !is_blocked_for_prerendering_ && is_blocked_for_power_saver_poster_) {
91 PluginInstanceThrottler::RecordUnthrottleMethodMetric(
92 PluginInstanceThrottler::UNTHROTTLE_METHOD_NEVER);
93 }
94 #endif
95 } 82 }
96 83
97 #if defined(ENABLE_PLUGINS) 84 #if defined(ENABLE_PLUGINS)
98 void LoadablePluginPlaceholder::MarkPluginEssential( 85 void LoadablePluginPlaceholder::MarkPluginEssential(
99 PluginInstanceThrottler::PowerSaverUnthrottleMethod method) { 86 PluginInstanceThrottler::PowerSaverUnthrottleMethod method) {
100 if (plugin_marked_essential_) 87 if (!power_saver_enabled_)
101 return; 88 return;
102 89
103 plugin_marked_essential_ = true; 90 power_saver_enabled_ = false;
91
104 if (premade_throttler_) 92 if (premade_throttler_)
105 premade_throttler_->MarkPluginEssential(method); 93 premade_throttler_->MarkPluginEssential(method);
106 else 94 else
107 PluginInstanceThrottler::RecordUnthrottleMethodMetric(method); 95 PluginInstanceThrottler::RecordUnthrottleMethodMetric(method);
108 96
109 if (is_blocked_for_power_saver_poster_) { 97 if (is_blocked_for_power_saver_poster_) {
110 is_blocked_for_power_saver_poster_ = false; 98 is_blocked_for_power_saver_poster_ = false;
111 if (!LoadingBlocked()) 99 if (!LoadingBlocked())
112 LoadPlugin(); 100 LoadPlugin();
113 } 101 }
(...skipping 27 matching lines...) Expand all
141 return; 129 return;
142 } 130 }
143 131
144 // The plugin has been removed from the page. Destroy the old plugin. We 132 // The plugin has been removed from the page. Destroy the old plugin. We
145 // will be destroyed as soon as V8 garbage collects us. 133 // will be destroyed as soon as V8 garbage collects us.
146 if (!element.pluginContainer()) { 134 if (!element.pluginContainer()) {
147 plugin()->destroy(); 135 plugin()->destroy();
148 return; 136 return;
149 } 137 }
150 138
151 placeholder_was_replaced_ = true;
152
153 // During initialization, the new plugin might have replaced itself in turn 139 // During initialization, the new plugin might have replaced itself in turn
154 // with another plugin. Make sure not to use the passed in |new_plugin| after 140 // with another plugin. Make sure not to use the passed in |new_plugin| after
155 // this point. 141 // this point.
156 new_plugin = container->plugin(); 142 new_plugin = container->plugin();
157 143
158 plugin()->RestoreTitleText(); 144 plugin()->RestoreTitleText();
159 container->invalidate(); 145 container->invalidate();
160 container->reportGeometry(); 146 container->reportGeometry();
161 plugin()->ReplayReceivedData(new_plugin); 147 plugin()->ReplayReceivedData(new_plugin);
162 plugin()->destroy(); 148 plugin()->destroy();
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 void LoadablePluginPlaceholder::UpdateMessage() { 205 void LoadablePluginPlaceholder::UpdateMessage() {
220 if (!plugin()) 206 if (!plugin())
221 return; 207 return;
222 std::string script = 208 std::string script =
223 "window.setMessage(" + base::GetQuotedJSONString(message_) + ")"; 209 "window.setMessage(" + base::GetQuotedJSONString(message_) + ")";
224 plugin()->web_view()->mainFrame()->executeScript( 210 plugin()->web_view()->mainFrame()->executeScript(
225 WebScriptSource(base::UTF8ToUTF16(script))); 211 WebScriptSource(base::UTF8ToUTF16(script)));
226 } 212 }
227 213
228 void LoadablePluginPlaceholder::PluginDestroyed() { 214 void LoadablePluginPlaceholder::PluginDestroyed() {
229 // Since the premade plugin has been detached from the container, it will not 215 #if defined(ENABLE_PLUGINS)
230 // be automatically destroyed along with the page. 216 if (power_saver_enabled_) {
Bernhard Bauer 2015/04/29 10:46:00 Early-return if |power_saver_enabled_| is false?
tommycli 2015/04/29 18:17:28 Err well we still want to call PluginPlaceholder::
Bernhard Bauer 2015/04/29 18:53:26 Oh, durr.
231 if (!placeholder_was_replaced_ && premade_throttler_) { 217 if (premade_throttler_) {
232 premade_throttler_->RemoveObserver(this); 218 // Since the premade plugin has been detached from the container, it will
233 premade_throttler_->GetWebPlugin()->destroy(); 219 // not be automatically destroyed along with the page.
234 premade_throttler_ = nullptr; 220 premade_throttler_->GetWebPlugin()->destroy();
221 premade_throttler_ = nullptr;
222 } else if (is_blocked_for_power_saver_poster_) {
223 // Record the NEVER unthrottle count only if there is no throttler.
224 PluginInstanceThrottler::RecordUnthrottleMethodMetric(
225 PluginInstanceThrottler::UNTHROTTLE_METHOD_NEVER);
226 }
227
228 // Prevent processing subsequent calls to MarkPluginEssential.
229 power_saver_enabled_ = false;
235 } 230 }
231 #endif
236 232
237 PluginPlaceholder::PluginDestroyed(); 233 PluginPlaceholder::PluginDestroyed();
238 } 234 }
239 235
240 void LoadablePluginPlaceholder::WasShown() { 236 void LoadablePluginPlaceholder::WasShown() {
241 if (is_blocked_for_background_tab_) { 237 if (is_blocked_for_background_tab_) {
242 is_blocked_for_background_tab_ = false; 238 is_blocked_for_background_tab_ = false;
243 if (!LoadingBlocked()) 239 if (!LoadingBlocked())
244 LoadPlugin(); 240 LoadPlugin();
245 } 241 }
246 } 242 }
247 243
248 void LoadablePluginPlaceholder::OnThrottleStateChange() {
249 DCHECK(premade_throttler_);
250 if (!premade_throttler_->IsThrottled()) {
251 // Premade plugin has been unthrottled externally (by audio playback, etc.).
252 LoadPlugin();
253 }
254 }
255
256 void LoadablePluginPlaceholder::OnLoadBlockedPlugins( 244 void LoadablePluginPlaceholder::OnLoadBlockedPlugins(
257 const std::string& identifier) { 245 const std::string& identifier) {
258 if (!identifier.empty() && identifier != identifier_) 246 if (!identifier.empty() && identifier != identifier_)
259 return; 247 return;
260 248
261 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Load_UI")); 249 RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Load_UI"));
262 LoadPlugin(); 250 LoadPlugin();
263 } 251 }
264 252
265 void LoadablePluginPlaceholder::OnSetIsPrerendering(bool is_prerendering) { 253 void LoadablePluginPlaceholder::OnSetIsPrerendering(bool is_prerendering) {
(...skipping 13 matching lines...) Expand all
279 if (hidden_) 267 if (hidden_)
280 return; 268 return;
281 if (!plugin()) 269 if (!plugin())
282 return; 270 return;
283 if (!allow_loading_) { 271 if (!allow_loading_) {
284 NOTREACHED(); 272 NOTREACHED();
285 return; 273 return;
286 } 274 }
287 275
288 if (premade_throttler_) { 276 if (premade_throttler_) {
289 premade_throttler_->RemoveObserver(this);
290 premade_throttler_->SetHiddenForPlaceholder(false /* hidden */); 277 premade_throttler_->SetHiddenForPlaceholder(false /* hidden */);
291
292 ReplacePlugin(premade_throttler_->GetWebPlugin()); 278 ReplacePlugin(premade_throttler_->GetWebPlugin());
293 premade_throttler_ = nullptr; 279 premade_throttler_ = nullptr;
294 } else { 280 } else {
295 // TODO(mmenke): In the case of prerendering, feed into 281 // TODO(mmenke): In the case of prerendering, feed into
296 // ChromeContentRendererClient::CreatePlugin instead, to 282 // ChromeContentRendererClient::CreatePlugin instead, to
297 // reduce the chance of future regressions. 283 // reduce the chance of future regressions.
298 scoped_ptr<PluginInstanceThrottler> throttler; 284 scoped_ptr<PluginInstanceThrottler> throttler;
299 #if defined(ENABLE_PLUGINS) 285 #if defined(ENABLE_PLUGINS)
300 // If the plugin has already been marked essential in its placeholder form, 286 // If the plugin has already been marked essential in its placeholder form,
301 // we shouldn't create a new throttler and start the process all over again. 287 // we shouldn't create a new throttler and start the process all over again.
302 if (!plugin_marked_essential_ && power_saver_enabled_) 288 if (power_saver_enabled_)
303 throttler = PluginInstanceThrottler::Create(); 289 throttler = PluginInstanceThrottler::Create();
304 #endif 290 #endif
305 WebPlugin* plugin = render_frame()->CreatePlugin( 291 WebPlugin* plugin = render_frame()->CreatePlugin(
306 GetFrame(), plugin_info_, GetPluginParams(), throttler.Pass()); 292 GetFrame(), plugin_info_, GetPluginParams(), throttler.Pass());
307 293
308 ReplacePlugin(plugin); 294 ReplacePlugin(plugin);
309 } 295 }
310 } 296 }
311 297
312 void LoadablePluginPlaceholder::LoadCallback() { 298 void LoadablePluginPlaceholder::LoadCallback() {
(...skipping 11 matching lines...) Expand all
324 HidePlugin(); 310 HidePlugin();
325 } 311 }
326 312
327 void LoadablePluginPlaceholder::DidFinishLoadingCallback() { 313 void LoadablePluginPlaceholder::DidFinishLoadingCallback() {
328 finished_loading_ = true; 314 finished_loading_ = true;
329 if (message_.length() > 0) 315 if (message_.length() > 0)
330 UpdateMessage(); 316 UpdateMessage();
331 317
332 // Wait for the placeholder to finish loading to hide the premade plugin. 318 // Wait for the placeholder to finish loading to hide the premade plugin.
333 // This is necessary to prevent a flicker. 319 // This is necessary to prevent a flicker.
334 if (premade_throttler_ && !placeholder_was_replaced_) 320 if (premade_throttler_ && power_saver_enabled_)
335 premade_throttler_->SetHiddenForPlaceholder(true /* hidden */); 321 premade_throttler_->SetHiddenForPlaceholder(true /* hidden */);
336 } 322 }
337 323
338 void LoadablePluginPlaceholder::SetPluginInfo( 324 void LoadablePluginPlaceholder::SetPluginInfo(
339 const content::WebPluginInfo& plugin_info) { 325 const content::WebPluginInfo& plugin_info) {
340 plugin_info_ = plugin_info; 326 plugin_info_ = plugin_info;
341 } 327 }
342 328
343 const content::WebPluginInfo& LoadablePluginPlaceholder::GetPluginInfo() const { 329 const content::WebPluginInfo& LoadablePluginPlaceholder::GetPluginInfo() const {
344 return plugin_info_; 330 return plugin_info_;
345 } 331 }
346 332
347 void LoadablePluginPlaceholder::SetIdentifier(const std::string& identifier) { 333 void LoadablePluginPlaceholder::SetIdentifier(const std::string& identifier) {
348 identifier_ = identifier; 334 identifier_ = identifier;
349 } 335 }
350 336
351 bool LoadablePluginPlaceholder::LoadingBlocked() const { 337 bool LoadablePluginPlaceholder::LoadingBlocked() const {
352 DCHECK(allow_loading_); 338 DCHECK(allow_loading_);
353 return is_blocked_for_background_tab_ || is_blocked_for_power_saver_poster_ || 339 return is_blocked_for_background_tab_ || is_blocked_for_power_saver_poster_ ||
354 is_blocked_for_prerendering_; 340 is_blocked_for_prerendering_;
355 } 341 }
356 342
357 } // namespace plugins 343 } // namespace plugins
OLDNEW
« no previous file with comments | « components/plugins/renderer/loadable_plugin_placeholder.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698