Skip to content

Commit

Permalink
Plugin Power Saver: Remove Size Recheck hack in plugin placeholders.
Browse files Browse the repository at this point in the history
See https://code.google.com/p/chromium/issues/detail?id=343769.

The Blink faulty geometry is _almost_ fixed, and it seems fixed-enough
to remove the (awful) size recheck hack for plugin placeholders.

As a nice side effect, removing this size recheck hack will also make
essential plugins with posters no longer have an ugly delay.

BUG=497429,560590

Review URL: https://codereview.chromium.org/1491893004

Cr-Commit-Position: refs/heads/master@{#372674}
  • Loading branch information
tommycli authored and Commit bot committed Feb 1, 2016
1 parent b8f2530 commit f74fd9e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 81 deletions.
115 changes: 38 additions & 77 deletions components/plugins/renderer/loadable_plugin_placeholder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include "components/plugins/renderer/loadable_plugin_placeholder.h"

#include "base/auto_reset.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/json/string_escape.h"
Expand Down Expand Up @@ -35,11 +34,6 @@ using content::RenderThread;

namespace plugins {

// TODO(tommycli): After a size update, re-check the size after this delay, as
// Blink can report incorrect sizes to plugins while the compositing state is
// dirty. Chosen because it seems to work.
const int kSizeChangeRecheckDelayMilliseconds = 100;

void LoadablePluginPlaceholder::BlockForPowerSaverPoster() {
DCHECK(!is_blocked_for_power_saver_poster_);
is_blocked_for_power_saver_poster_ = true;
Expand Down Expand Up @@ -71,7 +65,6 @@ LoadablePluginPlaceholder::LoadablePluginPlaceholder(
premade_throttler_(nullptr),
allow_loading_(false),
finished_loading_(false),
in_size_recheck_(false),
heuristic_run_before_(premade_throttler_ != nullptr),
weak_factory_(this) {}

Expand Down Expand Up @@ -190,25 +183,48 @@ v8::Local<v8::Object> LoadablePluginPlaceholder::GetV8ScriptableObject(
void LoadablePluginPlaceholder::OnUnobscuredRectUpdate(
const gfx::Rect& unobscured_rect) {
DCHECK(content::RenderThread::Get());
if (!power_saver_enabled_ || !finished_loading_)
if (!plugin() || !power_saver_enabled_ || !finished_loading_)
return;

// Only update the unobscured rect during the recheck phase. Also early exit
// to prevent reentrancy issues.
if (in_size_recheck_) {
unobscured_rect_ = unobscured_rect;
if (unobscured_rect_ == unobscured_rect)
return;
}

if (!size_update_timer_.IsRunning()) {
// TODO(tommycli): We have to post a delayed task to recheck the size, as
// Blink can report wrong sizes for partially obscured plugins while the
// compositing state is dirty. https://crbug.com/343769
size_update_timer_.Start(
FROM_HERE,
base::TimeDelta::FromMilliseconds(kSizeChangeRecheckDelayMilliseconds),
base::Bind(&LoadablePluginPlaceholder::RecheckSizeAndMaybeUnthrottle,
weak_factory_.GetWeakPtr()));
unobscured_rect_ = unobscured_rect;

float zoom_factor = plugin()->container()->pageZoomFactor();
int width = roundf(unobscured_rect_.width() / zoom_factor);
int height = roundf(unobscured_rect_.height() / zoom_factor);

if (is_blocked_for_power_saver_poster_) {
// Adjust poster container padding and dimensions to center play button for
// plugins and plugin posters that have their top or left portions obscured.
int x = roundf(unobscured_rect_.x() / zoom_factor);
int y = roundf(unobscured_rect_.y() / zoom_factor);
std::string script = base::StringPrintf(
"window.resizePoster('%dpx', '%dpx', '%dpx', '%dpx')", x, y, width,
height);
plugin()->web_view()->mainFrame()->executeScript(
blink::WebScriptSource(base::UTF8ToUTF16(script)));

// On a size update check if we now qualify as a essential plugin.
url::Origin content_origin = url::Origin(GetPluginParams().url);
auto status = render_frame()->GetPeripheralContentStatus(
render_frame()->GetWebFrame()->top()->securityOrigin(), content_origin,
gfx::Size(width, height));
if (status != content::RenderFrame::CONTENT_STATUS_PERIPHERAL) {
MarkPluginEssential(
heuristic_run_before_
? PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_SIZE_CHANGE
: PluginInstanceThrottler::UNTHROTTLE_METHOD_DO_NOT_RECORD);

if (!heuristic_run_before_ &&
status ==
content::RenderFrame::CONTENT_STATUS_ESSENTIAL_CROSS_ORIGIN_BIG) {
render_frame()->WhitelistContentOrigin(content_origin);
}
}

heuristic_run_before_ = true;
}
}

Expand Down Expand Up @@ -327,59 +343,4 @@ bool LoadablePluginPlaceholder::LoadingBlocked() const {
is_blocked_for_prerendering_;
}

void LoadablePluginPlaceholder::RecheckSizeAndMaybeUnthrottle() {
DCHECK(content::RenderThread::Get());
DCHECK(!in_size_recheck_);
DCHECK(finished_loading_);

base::AutoReset<bool> recheck_scope(&in_size_recheck_, true);

if (!plugin())
return;

gfx::Rect old_rect = unobscured_rect_;

// Re-check the size in case the reported size was incorrect.
plugin()->container()->reportGeometry();

if (old_rect == unobscured_rect_)
return;

float zoom_factor = plugin()->container()->pageZoomFactor();
int width = roundf(unobscured_rect_.width() / zoom_factor);
int height = roundf(unobscured_rect_.height() / zoom_factor);

if (is_blocked_for_power_saver_poster_) {
// Adjust poster container padding and dimensions to center play button for
// plugins and plugin posters that have their top or left portions obscured.
int x = roundf(unobscured_rect_.x() / zoom_factor);
int y = roundf(unobscured_rect_.y() / zoom_factor);
std::string script = base::StringPrintf(
"window.resizePoster('%dpx', '%dpx', '%dpx', '%dpx')", x, y, width,
height);
plugin()->web_view()->mainFrame()->executeScript(
blink::WebScriptSource(base::UTF8ToUTF16(script)));

// On a size update check if we now qualify as a essential plugin.
url::Origin content_origin = url::Origin(GetPluginParams().url);
auto status = render_frame()->GetPeripheralContentStatus(
render_frame()->GetWebFrame()->top()->securityOrigin(), content_origin,
gfx::Size(width, height));
if (status != content::RenderFrame::CONTENT_STATUS_PERIPHERAL) {
MarkPluginEssential(
heuristic_run_before_
? PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_SIZE_CHANGE
: PluginInstanceThrottler::UNTHROTTLE_METHOD_DO_NOT_RECORD);

if (!heuristic_run_before_ &&
status ==
content::RenderFrame::CONTENT_STATUS_ESSENTIAL_CROSS_ORIGIN_BIG) {
render_frame()->WhitelistContentOrigin(content_origin);
}
}

heuristic_run_before_ = true;
}
}

} // namespace plugins
4 changes: 0 additions & 4 deletions components/plugins/renderer/loadable_plugin_placeholder.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ class LoadablePluginPlaceholder : public PluginPlaceholderBase {
void UpdateMessage();

bool LoadingBlocked() const;
void RecheckSizeAndMaybeUnthrottle();

// Plugin creation is embedder-specific.
virtual blink::WebPlugin* CreatePlugin() = 0;
Expand Down Expand Up @@ -120,10 +119,7 @@ class LoadablePluginPlaceholder : public PluginPlaceholderBase {
bool finished_loading_;
std::string identifier_;

// Used to prevent re-entrancy during the size recheck for throttled plugins.
bool in_size_recheck_;
gfx::Rect unobscured_rect_;
base::OneShotTimer size_update_timer_;

// True if the power saver heuristic has already been run on this content.
bool heuristic_run_before_;
Expand Down

0 comments on commit f74fd9e

Please sign in to comment.