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

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

Issue 1124173008: Plugin Power Saver: Unthrottle dynamically sized plugins correctly. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 6 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
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/command_line.h" 9 #include "base/command_line.h"
10 #include "base/json/string_escape.h" 10 #include "base/json/string_escape.h"
11 #include "base/strings/string_piece.h" 11 #include "base/strings/string_piece.h"
12 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
14 #include "base/thread_task_runner_handle.h"
14 #include "base/values.h" 15 #include "base/values.h"
15 #include "content/public/child/v8_value_converter.h" 16 #include "content/public/child/v8_value_converter.h"
16 #include "content/public/common/content_switches.h" 17 #include "content/public/common/content_switches.h"
17 #include "content/public/renderer/render_frame.h" 18 #include "content/public/renderer/render_frame.h"
18 #include "content/public/renderer/render_thread.h" 19 #include "content/public/renderer/render_thread.h"
19 #include "gin/handle.h" 20 #include "gin/handle.h"
20 #include "gin/object_template_builder.h" 21 #include "gin/object_template_builder.h"
21 #include "third_party/WebKit/public/web/WebDOMMessageEvent.h" 22 #include "third_party/WebKit/public/web/WebDOMMessageEvent.h"
22 #include "third_party/WebKit/public/web/WebDocument.h" 23 #include "third_party/WebKit/public/web/WebDocument.h"
23 #include "third_party/WebKit/public/web/WebElement.h" 24 #include "third_party/WebKit/public/web/WebElement.h"
(...skipping 14 matching lines...) Expand all
38 using blink::WebPlugin; 39 using blink::WebPlugin;
39 using blink::WebPluginContainer; 40 using blink::WebPluginContainer;
40 using blink::WebPluginParams; 41 using blink::WebPluginParams;
41 using blink::WebScriptSource; 42 using blink::WebScriptSource;
42 using blink::WebURLRequest; 43 using blink::WebURLRequest;
43 using content::PluginInstanceThrottler; 44 using content::PluginInstanceThrottler;
44 using content::RenderThread; 45 using content::RenderThread;
45 46
46 namespace plugins { 47 namespace plugins {
47 48
49 namespace {
Bernhard Bauer 2015/06/10 11:13:24 Nit: Anonymous namespace isn't necessary for const
tommycli 2015/06/10 18:55:52 Done.
50
51 #if defined(ENABLE_PLUGINS)
52 // TODO(tommycli): After an unthrottling size update, re-check the size after
53 // this delay, as Blink can report incorrect sizes to plugins while the
54 // compositing state is dirty. Chosen because it seems to work.
55 const int kSizeChangeRecheckDelayMilliseconds = 100;
56 #endif
57
58 } // namespace
59
48 #if defined(ENABLE_PLUGINS) 60 #if defined(ENABLE_PLUGINS)
49 void LoadablePluginPlaceholder::BlockForPowerSaverPoster() { 61 void LoadablePluginPlaceholder::BlockForPowerSaverPoster() {
50 DCHECK(!is_blocked_for_power_saver_poster_); 62 DCHECK(!is_blocked_for_power_saver_poster_);
51 is_blocked_for_power_saver_poster_ = true; 63 is_blocked_for_power_saver_poster_ = true;
52 64
53 render_frame()->RegisterPeripheralPlugin( 65 render_frame()->RegisterPeripheralPlugin(
54 GURL(GetPluginParams().url).GetOrigin(), 66 GURL(GetPluginParams().url).GetOrigin(),
55 base::Bind(&LoadablePluginPlaceholder::MarkPluginEssential, 67 base::Bind(&LoadablePluginPlaceholder::MarkPluginEssential,
56 weak_factory_.GetWeakPtr(), 68 weak_factory_.GetWeakPtr(),
57 PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_WHITELIST)); 69 PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_WHITELIST));
(...skipping 19 matching lines...) Expand all
77 html_data, 89 html_data,
78 placeholderDataUrl), 90 placeholderDataUrl),
79 is_blocked_for_background_tab_(false), 91 is_blocked_for_background_tab_(false),
80 is_blocked_for_prerendering_(false), 92 is_blocked_for_prerendering_(false),
81 is_blocked_for_power_saver_poster_(false), 93 is_blocked_for_power_saver_poster_(false),
82 power_saver_enabled_(false), 94 power_saver_enabled_(false),
83 premade_throttler_(nullptr), 95 premade_throttler_(nullptr),
84 allow_loading_(false), 96 allow_loading_(false),
85 hidden_(false), 97 hidden_(false),
86 finished_loading_(false), 98 finished_loading_(false),
99 in_size_recheck_(false),
100 size_update_weak_factory_(this),
87 weak_factory_(this) { 101 weak_factory_(this) {
88 } 102 }
89 103
90 LoadablePluginPlaceholder::~LoadablePluginPlaceholder() { 104 LoadablePluginPlaceholder::~LoadablePluginPlaceholder() {
91 } 105 }
92 106
93 #if defined(ENABLE_PLUGINS) 107 #if defined(ENABLE_PLUGINS)
94 void LoadablePluginPlaceholder::MarkPluginEssential( 108 void LoadablePluginPlaceholder::MarkPluginEssential(
95 PluginInstanceThrottler::PowerSaverUnthrottleMethod method) { 109 PluginInstanceThrottler::PowerSaverUnthrottleMethod method) {
96 if (!power_saver_enabled_) 110 if (!power_saver_enabled_)
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 v8::Isolate* isolate) const { 272 v8::Isolate* isolate) const {
259 #if defined(ENABLE_PLUGINS) 273 #if defined(ENABLE_PLUGINS)
260 // Pass through JavaScript access to the underlying throttled plugin. 274 // Pass through JavaScript access to the underlying throttled plugin.
261 if (premade_throttler_ && premade_throttler_->GetWebPlugin()) { 275 if (premade_throttler_ && premade_throttler_->GetWebPlugin()) {
262 return premade_throttler_->GetWebPlugin()->v8ScriptableObject(isolate); 276 return premade_throttler_->GetWebPlugin()->v8ScriptableObject(isolate);
263 } 277 }
264 #endif 278 #endif
265 return v8::Local<v8::Object>(); 279 return v8::Local<v8::Object>();
266 } 280 }
267 281
282 #if defined(ENABLE_PLUGINS)
283 void LoadablePluginPlaceholder::OnUnobscuredSizeUpdate(
284 const gfx::Size& unobscured_size) {
285 DCHECK(
286 content::RenderThread::Get()->GetTaskRunner()->BelongsToCurrentThread());
287 if (!power_saver_enabled_ || !premade_throttler_ || !finished_loading_)
288 return;
289
290 unobscured_size_ = unobscured_size;
291
292 // During a size recheck, we will get another notification into this method.
293 // Use this flag to early exit to prevent reentrancy issues.
294 if (in_size_recheck_)
295 return;
296
297 if (PluginInstanceThrottler::IsLargeContent(unobscured_size.width(),
298 unobscured_size.height())) {
299 if (!size_update_weak_factory_.HasWeakPtrs()) {
Bernhard Bauer 2015/06/10 11:13:24 You could use a base::Timer for this instead of th
tommycli 2015/06/10 18:55:52 Done.
300 // TODO(tommycli): We have to post a delayed task to recheck the size, as
301 // Blink can report wrong sizes for partially obscured plugins while the
302 // compositing state is dirty. https://crbug.com/343769
303 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
304 FROM_HERE,
305 base::Bind(&LoadablePluginPlaceholder::RecheckSizeAndMaybeUnthrottle,
306 size_update_weak_factory_.GetWeakPtr()),
307 base::TimeDelta::FromMilliseconds(
308 kSizeChangeRecheckDelayMilliseconds));
309 }
310 } else {
311 // Cancel any pending unthrottle due to resize calls.
312 size_update_weak_factory_.InvalidateWeakPtrs();
313 }
314 }
315 #endif // defined(ENABLE_PLUGINS)
316
268 void LoadablePluginPlaceholder::WasShown() { 317 void LoadablePluginPlaceholder::WasShown() {
269 if (is_blocked_for_background_tab_) { 318 if (is_blocked_for_background_tab_) {
270 is_blocked_for_background_tab_ = false; 319 is_blocked_for_background_tab_ = false;
271 if (!LoadingBlocked()) 320 if (!LoadingBlocked())
272 LoadPlugin(); 321 LoadPlugin();
273 } 322 }
274 } 323 }
275 324
276 void LoadablePluginPlaceholder::OnLoadBlockedPlugins( 325 void LoadablePluginPlaceholder::OnLoadBlockedPlugins(
277 const std::string& identifier) { 326 const std::string& identifier) {
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 const std::string& LoadablePluginPlaceholder::GetIdentifier() const { 431 const std::string& LoadablePluginPlaceholder::GetIdentifier() const {
383 return identifier_; 432 return identifier_;
384 } 433 }
385 434
386 bool LoadablePluginPlaceholder::LoadingBlocked() const { 435 bool LoadablePluginPlaceholder::LoadingBlocked() const {
387 DCHECK(allow_loading_); 436 DCHECK(allow_loading_);
388 return is_blocked_for_background_tab_ || is_blocked_for_power_saver_poster_ || 437 return is_blocked_for_background_tab_ || is_blocked_for_power_saver_poster_ ||
389 is_blocked_for_prerendering_; 438 is_blocked_for_prerendering_;
390 } 439 }
391 440
441 #if defined(ENABLE_PLUGINS)
442 void LoadablePluginPlaceholder::RecheckSizeAndMaybeUnthrottle() {
443 DCHECK(
444 content::RenderThread::Get()->GetTaskRunner()->BelongsToCurrentThread());
445 DCHECK(!in_size_recheck_);
446 in_size_recheck_ = true;
447
448 // Re-check the size in case the reported size was incorrect.
449 plugin()->container()->reportGeometry();
450
451 if (PluginInstanceThrottler::IsLargeContent(unobscured_size_.width(),
452 unobscured_size_.height())) {
453 MarkPluginEssential(
454 PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_SIZE_CHANGE);
455 }
456
457 in_size_recheck_ = false;
458 }
459 #endif // defined(ENABLE_PLUGINS)
460
392 } // namespace plugins 461 } // namespace plugins
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698