| Index: components/plugins/renderer/loadable_plugin_placeholder.cc
|
| diff --git a/components/plugins/renderer/loadable_plugin_placeholder.cc b/components/plugins/renderer/loadable_plugin_placeholder.cc
|
| index b13ece52396a57c5836c5fccfeb0906e77cbe439..f28c19638b980491014f7c72e410f0150bc2ad3b 100644
|
| --- a/components/plugins/renderer/loadable_plugin_placeholder.cc
|
| +++ b/components/plugins/renderer/loadable_plugin_placeholder.cc
|
| @@ -4,6 +4,7 @@
|
|
|
| #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"
|
| @@ -34,6 +35,11 @@
|
|
|
| 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;
|
| @@ -65,6 +71,7 @@
|
| premade_throttler_(nullptr),
|
| allow_loading_(false),
|
| finished_loading_(false),
|
| + in_size_recheck_(false),
|
| heuristic_run_before_(premade_throttler_ != nullptr),
|
| weak_factory_(this) {}
|
|
|
| @@ -183,13 +190,160 @@
|
| void LoadablePluginPlaceholder::OnUnobscuredRectUpdate(
|
| const gfx::Rect& unobscured_rect) {
|
| DCHECK(content::RenderThread::Get());
|
| - if (!plugin() || !power_saver_enabled_ || !finished_loading_)
|
| - return;
|
| -
|
| - if (unobscured_rect_ == unobscured_rect)
|
| - return;
|
| -
|
| - unobscured_rect_ = unobscured_rect;
|
| + if (!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;
|
| + 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()));
|
| + }
|
| +}
|
| +
|
| +void LoadablePluginPlaceholder::WasShown() {
|
| + if (is_blocked_for_background_tab_) {
|
| + is_blocked_for_background_tab_ = false;
|
| + if (!LoadingBlocked())
|
| + LoadPlugin();
|
| + }
|
| +}
|
| +
|
| +void LoadablePluginPlaceholder::OnLoadBlockedPlugins(
|
| + const std::string& identifier) {
|
| + if (!identifier.empty() && identifier != identifier_)
|
| + return;
|
| +
|
| + RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Load_UI"));
|
| + LoadPlugin();
|
| +}
|
| +
|
| +void LoadablePluginPlaceholder::OnSetIsPrerendering(bool is_prerendering) {
|
| + // Prerendering can only be enabled prior to a RenderView's first navigation,
|
| + // so no BlockedPlugin should see the notification that enables prerendering.
|
| + DCHECK(!is_prerendering);
|
| + if (is_blocked_for_prerendering_) {
|
| + is_blocked_for_prerendering_ = false;
|
| + if (!LoadingBlocked())
|
| + LoadPlugin();
|
| + }
|
| +}
|
| +
|
| +void LoadablePluginPlaceholder::LoadPlugin() {
|
| + // This is not strictly necessary but is an important defense in case the
|
| + // event propagation changes between "close" vs. "click-to-play".
|
| + if (hidden())
|
| + return;
|
| + if (!plugin())
|
| + return;
|
| + if (!allow_loading_) {
|
| + NOTREACHED();
|
| + return;
|
| + }
|
| +
|
| + if (premade_throttler_) {
|
| + premade_throttler_->SetHiddenForPlaceholder(false /* hidden */);
|
| + ReplacePlugin(premade_throttler_->GetWebPlugin());
|
| + premade_throttler_ = nullptr;
|
| + } else {
|
| + ReplacePlugin(CreatePlugin());
|
| + }
|
| +}
|
| +
|
| +void LoadablePluginPlaceholder::LoadCallback() {
|
| + RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Load_Click"));
|
| + // If the user specifically clicks on the plugin content's placeholder,
|
| + // disable power saver throttling for this instance.
|
| + MarkPluginEssential(PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_CLICK);
|
| + LoadPlugin();
|
| +}
|
| +
|
| +void LoadablePluginPlaceholder::DidFinishLoadingCallback() {
|
| + finished_loading_ = true;
|
| + if (message_.length() > 0)
|
| + UpdateMessage();
|
| +
|
| + // Wait for the placeholder to finish loading to hide the premade plugin.
|
| + // This is necessary to prevent a flicker.
|
| + if (premade_throttler_ && power_saver_enabled_)
|
| + premade_throttler_->SetHiddenForPlaceholder(true /* hidden */);
|
| +
|
| + // In case our initial geometry was reported before the placeholder finished
|
| + // loading, request another one. Needed for correct large poster unthrottling.
|
| + if (plugin()) {
|
| + CHECK(plugin()->container());
|
| + plugin()->container()->reportGeometry();
|
| + }
|
| +}
|
| +
|
| +void LoadablePluginPlaceholder::DidFinishIconRepositionForTestingCallback() {
|
| + // Set an attribute and post an event, so browser tests can wait for the
|
| + // placeholder to be ready to receive simulated user input.
|
| + blink::WebElement element = plugin()->container()->element();
|
| + element.setAttribute("placeholderReady", "true");
|
| +
|
| + scoped_ptr<content::V8ValueConverter> converter(
|
| + content::V8ValueConverter::create());
|
| + base::StringValue value("placeholderReady");
|
| + blink::WebSerializedScriptValue message_data =
|
| + blink::WebSerializedScriptValue::serialize(converter->ToV8Value(
|
| + &value, element.document().frame()->mainWorldScriptContext()));
|
| + blink::WebDOMMessageEvent msg_event(message_data);
|
| +
|
| + element.dispatchEvent(msg_event);
|
| +}
|
| +
|
| +void LoadablePluginPlaceholder::SetPluginInfo(
|
| + const content::WebPluginInfo& plugin_info) {
|
| + plugin_info_ = plugin_info;
|
| +}
|
| +
|
| +const content::WebPluginInfo& LoadablePluginPlaceholder::GetPluginInfo() const {
|
| + return plugin_info_;
|
| +}
|
| +
|
| +void LoadablePluginPlaceholder::SetIdentifier(const std::string& identifier) {
|
| + identifier_ = identifier;
|
| +}
|
| +
|
| +const std::string& LoadablePluginPlaceholder::GetIdentifier() const {
|
| + return identifier_;
|
| +}
|
| +
|
| +bool LoadablePluginPlaceholder::LoadingBlocked() const {
|
| + DCHECK(allow_loading_);
|
| + return is_blocked_for_background_tab_ || is_blocked_for_power_saver_poster_ ||
|
| + 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);
|
| @@ -228,119 +382,4 @@
|
| }
|
| }
|
|
|
| -void LoadablePluginPlaceholder::WasShown() {
|
| - if (is_blocked_for_background_tab_) {
|
| - is_blocked_for_background_tab_ = false;
|
| - if (!LoadingBlocked())
|
| - LoadPlugin();
|
| - }
|
| -}
|
| -
|
| -void LoadablePluginPlaceholder::OnLoadBlockedPlugins(
|
| - const std::string& identifier) {
|
| - if (!identifier.empty() && identifier != identifier_)
|
| - return;
|
| -
|
| - RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Load_UI"));
|
| - LoadPlugin();
|
| -}
|
| -
|
| -void LoadablePluginPlaceholder::OnSetIsPrerendering(bool is_prerendering) {
|
| - // Prerendering can only be enabled prior to a RenderView's first navigation,
|
| - // so no BlockedPlugin should see the notification that enables prerendering.
|
| - DCHECK(!is_prerendering);
|
| - if (is_blocked_for_prerendering_) {
|
| - is_blocked_for_prerendering_ = false;
|
| - if (!LoadingBlocked())
|
| - LoadPlugin();
|
| - }
|
| -}
|
| -
|
| -void LoadablePluginPlaceholder::LoadPlugin() {
|
| - // This is not strictly necessary but is an important defense in case the
|
| - // event propagation changes between "close" vs. "click-to-play".
|
| - if (hidden())
|
| - return;
|
| - if (!plugin())
|
| - return;
|
| - if (!allow_loading_) {
|
| - NOTREACHED();
|
| - return;
|
| - }
|
| -
|
| - if (premade_throttler_) {
|
| - premade_throttler_->SetHiddenForPlaceholder(false /* hidden */);
|
| - ReplacePlugin(premade_throttler_->GetWebPlugin());
|
| - premade_throttler_ = nullptr;
|
| - } else {
|
| - ReplacePlugin(CreatePlugin());
|
| - }
|
| -}
|
| -
|
| -void LoadablePluginPlaceholder::LoadCallback() {
|
| - RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Load_Click"));
|
| - // If the user specifically clicks on the plugin content's placeholder,
|
| - // disable power saver throttling for this instance.
|
| - MarkPluginEssential(PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_CLICK);
|
| - LoadPlugin();
|
| -}
|
| -
|
| -void LoadablePluginPlaceholder::DidFinishLoadingCallback() {
|
| - finished_loading_ = true;
|
| - if (message_.length() > 0)
|
| - UpdateMessage();
|
| -
|
| - // Wait for the placeholder to finish loading to hide the premade plugin.
|
| - // This is necessary to prevent a flicker.
|
| - if (premade_throttler_ && power_saver_enabled_)
|
| - premade_throttler_->SetHiddenForPlaceholder(true /* hidden */);
|
| -
|
| - // In case our initial geometry was reported before the placeholder finished
|
| - // loading, request another one. Needed for correct large poster unthrottling.
|
| - if (plugin()) {
|
| - CHECK(plugin()->container());
|
| - plugin()->container()->reportGeometry();
|
| - }
|
| -}
|
| -
|
| -void LoadablePluginPlaceholder::DidFinishIconRepositionForTestingCallback() {
|
| - // Set an attribute and post an event, so browser tests can wait for the
|
| - // placeholder to be ready to receive simulated user input.
|
| - blink::WebElement element = plugin()->container()->element();
|
| - element.setAttribute("placeholderReady", "true");
|
| -
|
| - scoped_ptr<content::V8ValueConverter> converter(
|
| - content::V8ValueConverter::create());
|
| - base::StringValue value("placeholderReady");
|
| - blink::WebSerializedScriptValue message_data =
|
| - blink::WebSerializedScriptValue::serialize(converter->ToV8Value(
|
| - &value, element.document().frame()->mainWorldScriptContext()));
|
| - blink::WebDOMMessageEvent msg_event(message_data);
|
| -
|
| - element.dispatchEvent(msg_event);
|
| -}
|
| -
|
| -void LoadablePluginPlaceholder::SetPluginInfo(
|
| - const content::WebPluginInfo& plugin_info) {
|
| - plugin_info_ = plugin_info;
|
| -}
|
| -
|
| -const content::WebPluginInfo& LoadablePluginPlaceholder::GetPluginInfo() const {
|
| - return plugin_info_;
|
| -}
|
| -
|
| -void LoadablePluginPlaceholder::SetIdentifier(const std::string& identifier) {
|
| - identifier_ = identifier;
|
| -}
|
| -
|
| -const std::string& LoadablePluginPlaceholder::GetIdentifier() const {
|
| - return identifier_;
|
| -}
|
| -
|
| -bool LoadablePluginPlaceholder::LoadingBlocked() const {
|
| - DCHECK(allow_loading_);
|
| - return is_blocked_for_background_tab_ || is_blocked_for_power_saver_poster_ ||
|
| - is_blocked_for_prerendering_;
|
| -}
|
| -
|
| } // namespace plugins
|
|
|