This source file includes following definitions.
- finished_loading_
- GetObjectTemplateBuilder
- ReplacePlugin
- HidePlugin
- SetMessage
- UpdateMessage
- ShowContextMenu
- PluginDestroyed
- OnDestruct
- OnLoadBlockedPlugins
- OnSetIsPrerendering
- LoadPlugin
- LoadCallback
- HideCallback
- DidFinishLoadingCallback
- SetPluginInfo
- GetPluginInfo
- SetIdentifier
- GetFrame
- GetPluginParams
#include "components/plugins/renderer/plugin_placeholder.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/json/string_escape.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "content/public/common/content_constants.h"
#include "content/public/common/context_menu_params.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_thread.h"
#include "gin/object_template_builder.h"
#include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebElement.h"
#include "third_party/WebKit/public/web/WebFrame.h"
#include "third_party/WebKit/public/web/WebInputEvent.h"
#include "third_party/WebKit/public/web/WebPluginContainer.h"
#include "third_party/WebKit/public/web/WebScriptSource.h"
#include "third_party/WebKit/public/web/WebView.h"
#include "third_party/re2/re2/re2.h"
using base::UserMetricsAction;
using blink::WebElement;
using blink::WebFrame;
using blink::WebMouseEvent;
using blink::WebNode;
using blink::WebPlugin;
using blink::WebPluginContainer;
using blink::WebPluginParams;
using blink::WebScriptSource;
using blink::WebURLRequest;
using content::RenderThread;
namespace plugins {
gin::WrapperInfo PluginPlaceholder::kWrapperInfo = {gin::kEmbedderNativeGin};
PluginPlaceholder::PluginPlaceholder(content::RenderFrame* render_frame,
WebFrame* frame,
const WebPluginParams& params,
const std::string& html_data,
GURL placeholderDataUrl)
: content::RenderFrameObserver(render_frame),
frame_(frame),
plugin_params_(params),
plugin_(WebViewPlugin::Create(this,
render_frame->GetWebkitPreferences(),
html_data,
placeholderDataUrl)),
is_blocked_for_prerendering_(false),
allow_loading_(false),
hidden_(false),
finished_loading_(false) {}
PluginPlaceholder::~PluginPlaceholder() {}
gin::ObjectTemplateBuilder PluginPlaceholder::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return gin::Wrappable<PluginPlaceholder>::GetObjectTemplateBuilder(isolate)
.SetMethod("load", &PluginPlaceholder::LoadCallback)
.SetMethod("hide", &PluginPlaceholder::HideCallback)
.SetMethod("didFinishLoading",
&PluginPlaceholder::DidFinishLoadingCallback);
}
void PluginPlaceholder::ReplacePlugin(WebPlugin* new_plugin) {
CHECK(plugin_);
if (!new_plugin) return;
WebPluginContainer* container = plugin_->container();
container->setPlugin(new_plugin);
WebElement element = container->element();
if (!new_plugin->initialize(container)) {
container->setPlugin(plugin_);
return;
}
if (!element.pluginContainer()) {
plugin_->destroy();
return;
}
new_plugin = container->plugin();
plugin_->RestoreTitleText();
container->invalidate();
container->reportGeometry();
plugin_->ReplayReceivedData(new_plugin);
plugin_->destroy();
}
void PluginPlaceholder::HidePlugin() {
hidden_ = true;
if (!plugin_)
return;
WebPluginContainer* container = plugin_->container();
WebElement element = container->element();
element.setAttribute("style", "display: none;");
if (element.hasAttribute("width") && element.hasAttribute("height")) {
std::string width_str("width:[\\s]*");
width_str += element.getAttribute("width").utf8().data();
if (EndsWith(width_str, "px", false)) {
width_str = width_str.substr(0, width_str.length() - 2);
}
base::TrimWhitespace(width_str, base::TRIM_TRAILING, &width_str);
width_str += "[\\s]*px";
std::string height_str("height:[\\s]*");
height_str += element.getAttribute("height").utf8().data();
if (EndsWith(height_str, "px", false)) {
height_str = height_str.substr(0, height_str.length() - 2);
}
base::TrimWhitespace(height_str, base::TRIM_TRAILING, &height_str);
height_str += "[\\s]*px";
WebNode parent = element;
while (!parent.parentNode().isNull()) {
parent = parent.parentNode();
if (!parent.isElementNode())
continue;
element = parent.toConst<WebElement>();
if (element.hasAttribute("style")) {
std::string style_str = element.getAttribute("style").utf8();
if (RE2::PartialMatch(style_str, width_str) &&
RE2::PartialMatch(style_str, height_str))
element.setAttribute("style", "display: none;");
}
}
}
}
void PluginPlaceholder::SetMessage(const base::string16& message) {
message_ = message;
if (finished_loading_)
UpdateMessage();
}
void PluginPlaceholder::UpdateMessage() {
if (!plugin_)
return;
std::string script =
"window.setMessage(" + base::GetQuotedJSONString(message_) + ")";
plugin_->web_view()->mainFrame()->executeScript(
WebScriptSource(base::ASCIIToUTF16(script)));
}
void PluginPlaceholder::ShowContextMenu(const WebMouseEvent& event) {
return;
}
void PluginPlaceholder::PluginDestroyed() {
plugin_ = NULL;
}
void PluginPlaceholder::OnDestruct() {
frame_ = NULL;
}
void PluginPlaceholder::OnLoadBlockedPlugins(const std::string& identifier) {
if (!identifier.empty() && identifier != identifier_)
return;
RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Load_UI"));
LoadPlugin();
}
void PluginPlaceholder::OnSetIsPrerendering(bool is_prerendering) {
DCHECK(!is_prerendering);
if (is_blocked_for_prerendering_ && !is_prerendering)
LoadPlugin();
}
void PluginPlaceholder::LoadPlugin() {
if (hidden_)
return;
if (!plugin_)
return;
if (!allow_loading_) {
NOTREACHED();
return;
}
WebPlugin* plugin =
render_frame()->CreatePlugin(frame_, plugin_info_, plugin_params_);
ReplacePlugin(plugin);
}
void PluginPlaceholder::LoadCallback() {
RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Load_Click"));
LoadPlugin();
}
void PluginPlaceholder::HideCallback() {
RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Hide_Click"));
HidePlugin();
}
void PluginPlaceholder::DidFinishLoadingCallback() {
finished_loading_ = true;
if (message_.length() > 0)
UpdateMessage();
}
void PluginPlaceholder::SetPluginInfo(
const content::WebPluginInfo& plugin_info) {
plugin_info_ = plugin_info;
}
const content::WebPluginInfo& PluginPlaceholder::GetPluginInfo() const {
return plugin_info_;
}
void PluginPlaceholder::SetIdentifier(const std::string& identifier) {
identifier_ = identifier;
}
blink::WebFrame* PluginPlaceholder::GetFrame() { return frame_; }
const blink::WebPluginParams& PluginPlaceholder::GetPluginParams() const {
return plugin_params_;
}
}