mirror of
https://bitbucket.org/chromiumembedded/cef
synced 2025-06-05 21:39:12 +02:00
Add plugin placeholder and policy support (issue #1708)
- Default plugin loading policy can be specified using the new `--plugin-policy=[allow|block|detect]` command-line flag. - Move CefRequestHandler::OnBeforePluginLoad to CefRequestContextHandler and add a new policy argument that supports different actions (allow, block, detect, disable) on a per-plugin-instance basis. - Add CefContextMenuHandler::RunContextMenu for providing a custom context menu implementation. - Add CefResourceBundleHandler::GetDataResourceForScale for returning scaled resources (issue #1272). - Add CefResourceBundle for retrieving resources from the resource bundle (*.pak) files loaded by CEF during startup or via the CefResourceBundleHandler. - Linux: Fix Debug build IO access warning with CefGetMimeType. - cef_unittests: Move the refcounting implementation from TestHandler to subclasses in order to support interface inheritance from subclasses.
This commit is contained in:
@@ -19,6 +19,8 @@
|
||||
#include "libcef/renderer/extensions/print_web_view_helper_delegate.h"
|
||||
#include "libcef/renderer/media/cef_key_systems.h"
|
||||
#include "libcef/renderer/pepper/pepper_helper.h"
|
||||
#include "libcef/renderer/plugins/cef_plugin_placeholder.h"
|
||||
#include "libcef/renderer/plugins/plugin_preroller.h"
|
||||
#include "libcef/renderer/render_frame_observer.h"
|
||||
#include "libcef/renderer/render_message_filter.h"
|
||||
#include "libcef/renderer/render_process_observer.h"
|
||||
@@ -27,15 +29,20 @@
|
||||
#include "libcef/renderer/webkit_glue.h"
|
||||
|
||||
#include "base/command_line.h"
|
||||
#include "base/metrics/user_metrics_action.h"
|
||||
#include "base/path_service.h"
|
||||
#include "base/strings/string_number_conversions.h"
|
||||
#include "base/strings/utf_string_conversions.h"
|
||||
#include "chrome/common/chrome_switches.h"
|
||||
#include "chrome/common/pepper_permission_util.h"
|
||||
#include "chrome/grit/generated_resources.h"
|
||||
#include "chrome/renderer/content_settings_observer.h"
|
||||
#include "chrome/renderer/loadtimes_extension_bindings.h"
|
||||
#include "chrome/renderer/pepper/chrome_pdf_print_client.h"
|
||||
#include "chrome/renderer/spellchecker/spellcheck.h"
|
||||
#include "chrome/renderer/spellchecker/spellcheck_provider.h"
|
||||
#include "components/content_settings/core/common/content_settings_types.h"
|
||||
#include "components/nacl/common/nacl_constants.h"
|
||||
#include "components/printing/renderer/print_web_view_helper.h"
|
||||
#include "components/web_cache/renderer/web_cache_render_process_observer.h"
|
||||
#include "content/child/worker_task_runner.h"
|
||||
@@ -62,6 +69,7 @@
|
||||
#include "third_party/WebKit/public/platform/WebPrerenderingSupport.h"
|
||||
#include "third_party/WebKit/public/platform/WebString.h"
|
||||
#include "third_party/WebKit/public/platform/WebURL.h"
|
||||
#include "third_party/WebKit/public/web/WebConsoleMessage.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"
|
||||
@@ -71,6 +79,7 @@
|
||||
#include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
|
||||
#include "third_party/WebKit/public/web/WebSecurityPolicy.h"
|
||||
#include "third_party/WebKit/public/web/WebView.h"
|
||||
#include "ui/base/l10n/l10n_util.h"
|
||||
|
||||
#if defined(OS_MACOSX)
|
||||
#include "base/mac/mac_util.h"
|
||||
@@ -186,6 +195,19 @@ void AppendParams(const std::vector<base::string16>& additional_names,
|
||||
existing_values->swap(values);
|
||||
}
|
||||
|
||||
std::string GetPluginInstancePosterAttribute(
|
||||
const blink::WebPluginParams& params) {
|
||||
DCHECK_EQ(params.attributeNames.size(), params.attributeValues.size());
|
||||
|
||||
for (size_t i = 0; i < params.attributeNames.size(); ++i) {
|
||||
if (params.attributeNames[i].utf8() == "poster" &&
|
||||
!params.attributeValues[i].isEmpty()) {
|
||||
return params.attributeValues[i].utf8();
|
||||
}
|
||||
}
|
||||
return std::string();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
CefContentRendererClient::CefContentRendererClient()
|
||||
@@ -660,6 +682,7 @@ void CefContentRendererClient::WillDestroyCurrentMessageLoop() {
|
||||
single_process_cleanup_complete_ = true;
|
||||
}
|
||||
|
||||
// static
|
||||
bool CefContentRendererClient::IsExtensionOrSharedModuleWhitelisted(
|
||||
const GURL& url, const std::set<std::string>& whitelist) {
|
||||
DCHECK(extensions::ExtensionsEnabled());
|
||||
@@ -669,6 +692,167 @@ bool CefContentRendererClient::IsExtensionOrSharedModuleWhitelisted(
|
||||
whitelist);
|
||||
}
|
||||
|
||||
// static
|
||||
blink::WebPlugin* CefContentRendererClient::CreatePlugin(
|
||||
content::RenderFrame* render_frame,
|
||||
blink::WebLocalFrame* frame,
|
||||
const blink::WebPluginParams& original_params,
|
||||
const CefViewHostMsg_GetPluginInfo_Output& output) {
|
||||
const content::WebPluginInfo& info = output.plugin;
|
||||
const std::string& actual_mime_type = output.actual_mime_type;
|
||||
const base::string16& group_name = output.group_name;
|
||||
const std::string& identifier = output.group_identifier;
|
||||
CefViewHostMsg_GetPluginInfo_Status status = output.status;
|
||||
GURL url(original_params.url);
|
||||
std::string orig_mime_type = original_params.mimeType.utf8();
|
||||
CefPluginPlaceholder* placeholder = NULL;
|
||||
|
||||
// If the browser plugin is to be enabled, this should be handled by the
|
||||
// renderer, so the code won't reach here due to the early exit in
|
||||
// OverrideCreatePlugin.
|
||||
if (status == CefViewHostMsg_GetPluginInfo_Status::kNotFound ||
|
||||
orig_mime_type == content::kBrowserPluginMimeType) {
|
||||
placeholder = CefPluginPlaceholder::CreateLoadableMissingPlugin(
|
||||
render_frame, frame, original_params);
|
||||
} else {
|
||||
// TODO(bauerb): This should be in content/.
|
||||
blink::WebPluginParams params(original_params);
|
||||
for (size_t i = 0; i < info.mime_types.size(); ++i) {
|
||||
if (info.mime_types[i].mime_type == actual_mime_type) {
|
||||
AppendParams(info.mime_types[i].additional_param_names,
|
||||
info.mime_types[i].additional_param_values,
|
||||
¶ms.attributeNames, ¶ms.attributeValues);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (params.mimeType.isNull() && (actual_mime_type.size() > 0)) {
|
||||
// Webkit might say that mime type is null while we already know the
|
||||
// actual mime type via CefViewHostMsg_GetPluginInfo. In that case
|
||||
// we should use what we know since WebpluginDelegateProxy does some
|
||||
// specific initializations based on this information.
|
||||
params.mimeType = blink::WebString::fromUTF8(actual_mime_type.c_str());
|
||||
}
|
||||
|
||||
auto create_blocked_plugin =
|
||||
[&render_frame, &frame, ¶ms, &info, &identifier, &group_name](
|
||||
int template_id, const base::string16& message) {
|
||||
return CefPluginPlaceholder::CreateBlockedPlugin(
|
||||
render_frame, frame, params, info, identifier, group_name,
|
||||
template_id, message, PlaceholderPosterInfo());
|
||||
};
|
||||
switch (status) {
|
||||
case CefViewHostMsg_GetPluginInfo_Status::kNotFound: {
|
||||
NOTREACHED();
|
||||
break;
|
||||
}
|
||||
case CefViewHostMsg_GetPluginInfo_Status::kAllowed:
|
||||
case CefViewHostMsg_GetPluginInfo_Status::kPlayImportantContent: {
|
||||
// Delay loading plugins if prerendering.
|
||||
// TODO(mmenke): In the case of prerendering, feed into
|
||||
// CefContentRendererClient::CreatePlugin instead, to
|
||||
// reduce the chance of future regressions.
|
||||
bool is_prerendering = false;
|
||||
bool power_saver_enabled =
|
||||
status ==
|
||||
CefViewHostMsg_GetPluginInfo_Status::kPlayImportantContent;
|
||||
bool blocked_for_background_tab =
|
||||
render_frame->IsHidden() && power_saver_enabled;
|
||||
|
||||
PlaceholderPosterInfo poster_info;
|
||||
if (power_saver_enabled) {
|
||||
poster_info.poster_attribute =
|
||||
GetPluginInstancePosterAttribute(params);
|
||||
poster_info.base_url = frame->document().url();
|
||||
}
|
||||
|
||||
if (blocked_for_background_tab || is_prerendering ||
|
||||
!poster_info.poster_attribute.empty()) {
|
||||
placeholder = CefPluginPlaceholder::CreateBlockedPlugin(
|
||||
render_frame, frame, params, info, identifier, group_name,
|
||||
poster_info.poster_attribute.empty() ? IDR_BLOCKED_PLUGIN_HTML
|
||||
: IDR_PLUGIN_POSTER_HTML,
|
||||
l10n_util::GetStringFUTF16(IDS_PLUGIN_BLOCKED, group_name),
|
||||
poster_info);
|
||||
placeholder->set_blocked_for_background_tab(
|
||||
blocked_for_background_tab);
|
||||
placeholder->set_blocked_for_prerendering(is_prerendering);
|
||||
placeholder->set_power_saver_enabled(power_saver_enabled);
|
||||
placeholder->AllowLoading();
|
||||
break;
|
||||
}
|
||||
|
||||
scoped_ptr<content::PluginInstanceThrottler> throttler;
|
||||
if (power_saver_enabled) {
|
||||
throttler = content::PluginInstanceThrottler::Create();
|
||||
// PluginPreroller manages its own lifetime.
|
||||
new CefPluginPreroller(
|
||||
render_frame, frame, params, info, identifier, group_name,
|
||||
l10n_util::GetStringFUTF16(IDS_PLUGIN_BLOCKED, group_name),
|
||||
throttler.get());
|
||||
}
|
||||
|
||||
return render_frame->CreatePlugin(frame, info, params,
|
||||
throttler.Pass());
|
||||
}
|
||||
case CefViewHostMsg_GetPluginInfo_Status::kNPAPINotSupported: {
|
||||
content::RenderThread::Get()->RecordAction(
|
||||
base::UserMetricsAction("Plugin_NPAPINotSupported"));
|
||||
placeholder = create_blocked_plugin(
|
||||
IDR_BLOCKED_PLUGIN_HTML,
|
||||
l10n_util::GetStringUTF16(IDS_PLUGIN_NOT_SUPPORTED_METRO));
|
||||
break;
|
||||
}
|
||||
case CefViewHostMsg_GetPluginInfo_Status::kDisabled: {
|
||||
// Intentionally using the blocked plugin resources instead of the
|
||||
// disabled plugin resources. This provides better messaging (no link to
|
||||
// chrome://plugins) and adds testing support.
|
||||
placeholder = create_blocked_plugin(
|
||||
IDR_BLOCKED_PLUGIN_HTML,
|
||||
l10n_util::GetStringFUTF16(IDS_PLUGIN_BLOCKED_BY_POLICY,
|
||||
group_name));
|
||||
break;
|
||||
}
|
||||
case CefViewHostMsg_GetPluginInfo_Status::kOutdatedBlocked: {
|
||||
NOTREACHED() << "Plugin installation is not supported.";
|
||||
break;
|
||||
}
|
||||
case CefViewHostMsg_GetPluginInfo_Status::kOutdatedDisallowed: {
|
||||
placeholder = create_blocked_plugin(
|
||||
IDR_BLOCKED_PLUGIN_HTML,
|
||||
l10n_util::GetStringFUTF16(IDS_PLUGIN_OUTDATED, group_name));
|
||||
break;
|
||||
}
|
||||
case CefViewHostMsg_GetPluginInfo_Status::kUnauthorized: {
|
||||
placeholder = create_blocked_plugin(
|
||||
IDR_BLOCKED_PLUGIN_HTML,
|
||||
l10n_util::GetStringFUTF16(IDS_PLUGIN_NOT_AUTHORIZED, group_name));
|
||||
placeholder->AllowLoading();
|
||||
break;
|
||||
}
|
||||
case CefViewHostMsg_GetPluginInfo_Status::kBlocked: {
|
||||
placeholder = create_blocked_plugin(
|
||||
IDR_BLOCKED_PLUGIN_HTML,
|
||||
l10n_util::GetStringFUTF16(IDS_PLUGIN_BLOCKED, group_name));
|
||||
placeholder->AllowLoading();
|
||||
content::RenderThread::Get()->RecordAction(
|
||||
base::UserMetricsAction("Plugin_Blocked"));
|
||||
break;
|
||||
}
|
||||
case CefViewHostMsg_GetPluginInfo_Status::kBlockedByPolicy: {
|
||||
placeholder = create_blocked_plugin(
|
||||
IDR_BLOCKED_PLUGIN_HTML,
|
||||
l10n_util::GetStringFUTF16(IDS_PLUGIN_BLOCKED_BY_POLICY,
|
||||
group_name));
|
||||
content::RenderThread::Get()->RecordAction(
|
||||
base::UserMetricsAction("Plugin_BlockedByPolicy"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
placeholder->SetStatus(status);
|
||||
return placeholder->plugin();
|
||||
}
|
||||
|
||||
void CefContentRendererClient::BrowserCreated(
|
||||
content::RenderView* render_view,
|
||||
content::RenderFrame* render_frame) {
|
||||
@@ -743,57 +927,3 @@ void CefContentRendererClient::RunSingleProcessCleanupOnUIThread() {
|
||||
if (!CefContext::Get()->settings().multi_threaded_message_loop)
|
||||
delete host;
|
||||
}
|
||||
|
||||
blink::WebPlugin* CefContentRendererClient::CreatePlugin(
|
||||
content::RenderFrame* render_frame,
|
||||
blink::WebLocalFrame* frame,
|
||||
const blink::WebPluginParams& original_params,
|
||||
const CefViewHostMsg_GetPluginInfo_Output& output) {
|
||||
const content::WebPluginInfo& info = output.plugin;
|
||||
const std::string& actual_mime_type = output.actual_mime_type;
|
||||
CefViewHostMsg_GetPluginInfo_Status status = output.status;
|
||||
GURL url(original_params.url);
|
||||
std::string orig_mime_type = original_params.mimeType.utf8();
|
||||
|
||||
// If the browser plugin is to be enabled, this should be handled by the
|
||||
// renderer, so the code won't reach here due to the early exit in
|
||||
// OverrideCreatePlugin.
|
||||
if (status == CefViewHostMsg_GetPluginInfo_Status::kNotFound ||
|
||||
orig_mime_type == content::kBrowserPluginMimeType) {
|
||||
return NULL;
|
||||
} else {
|
||||
// TODO(bauerb): This should be in content/.
|
||||
blink::WebPluginParams params(original_params);
|
||||
for (size_t i = 0; i < info.mime_types.size(); ++i) {
|
||||
if (info.mime_types[i].mime_type == actual_mime_type) {
|
||||
AppendParams(info.mime_types[i].additional_param_names,
|
||||
info.mime_types[i].additional_param_values,
|
||||
¶ms.attributeNames, ¶ms.attributeValues);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (params.mimeType.isNull() && (actual_mime_type.size() > 0)) {
|
||||
// Webkit might say that mime type is null while we already know the
|
||||
// actual mime type via CefViewHostMsg_GetPluginInfo. In that case
|
||||
// we should use what we know since WebpluginDelegateProxy does some
|
||||
// specific initializations based on this information.
|
||||
params.mimeType = blink::WebString::fromUTF8(actual_mime_type.c_str());
|
||||
}
|
||||
|
||||
switch (status) {
|
||||
case CefViewHostMsg_GetPluginInfo_Status::kNotFound: {
|
||||
NOTREACHED();
|
||||
break;
|
||||
}
|
||||
case CefViewHostMsg_GetPluginInfo_Status::kAllowed:
|
||||
case CefViewHostMsg_GetPluginInfo_Status::kPlayImportantContent: {
|
||||
// TODO(cef): Maybe supply a throttler based on power settings.
|
||||
return render_frame->CreatePlugin(frame, info, params, nullptr);
|
||||
}
|
||||
default:
|
||||
// TODO(cef): Provide a placeholder for the various failure conditions.
|
||||
break;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
@@ -116,6 +116,12 @@ class CefContentRendererClient : public content::ContentRendererClient,
|
||||
static bool IsExtensionOrSharedModuleWhitelisted(
|
||||
const GURL& url, const std::set<std::string>& whitelist);
|
||||
|
||||
static blink::WebPlugin* CreatePlugin(
|
||||
content::RenderFrame* render_frame,
|
||||
blink::WebLocalFrame* frame,
|
||||
const blink::WebPluginParams& params,
|
||||
const CefViewHostMsg_GetPluginInfo_Output& output);
|
||||
|
||||
private:
|
||||
void BrowserCreated(content::RenderView* render_view,
|
||||
content::RenderFrame* render_frame);
|
||||
@@ -123,12 +129,6 @@ class CefContentRendererClient : public content::ContentRendererClient,
|
||||
// Perform cleanup work for single-process mode.
|
||||
void RunSingleProcessCleanupOnUIThread();
|
||||
|
||||
static blink::WebPlugin* CreatePlugin(
|
||||
content::RenderFrame* render_frame,
|
||||
blink::WebLocalFrame* frame,
|
||||
const blink::WebPluginParams& params,
|
||||
const CefViewHostMsg_GetPluginInfo_Output& output);
|
||||
|
||||
scoped_refptr<base::SequencedTaskRunner> render_task_runner_;
|
||||
scoped_ptr<CefRenderProcessObserver> observer_;
|
||||
scoped_ptr<web_cache::WebCacheRenderProcessObserver> web_cache_observer_;
|
||||
|
285
libcef/renderer/plugins/cef_plugin_placeholder.cc
Normal file
285
libcef/renderer/plugins/cef_plugin_placeholder.cc
Normal file
@@ -0,0 +1,285 @@
|
||||
// Copyright 2013 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "libcef/renderer/plugins/cef_plugin_placeholder.h"
|
||||
|
||||
#include "libcef/common/cef_messages.h"
|
||||
#include "libcef/renderer/content_renderer_client.h"
|
||||
#include "libcef/renderer/plugins/plugin_preroller.h"
|
||||
|
||||
#include "base/strings/string_number_conversions.h"
|
||||
#include "base/strings/utf_string_conversions.h"
|
||||
#include "base/values.h"
|
||||
#include "chrome/grit/generated_resources.h"
|
||||
#include "chrome/renderer/custom_menu_commands.h"
|
||||
#include "components/content_settings/content/common/content_settings_messages.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/WebInputEvent.h"
|
||||
#include "third_party/WebKit/public/web/WebLocalFrame.h"
|
||||
#include "third_party/WebKit/public/web/WebScriptSource.h"
|
||||
#include "third_party/WebKit/public/web/WebView.h"
|
||||
#include "ui/base/l10n/l10n_util.h"
|
||||
#include "ui/base/resource/resource_bundle.h"
|
||||
#include "ui/base/webui/jstemplate_builder.h"
|
||||
#include "ui/gfx/geometry/size.h"
|
||||
#include "url/url_util.h"
|
||||
|
||||
using base::UserMetricsAction;
|
||||
using content::RenderThread;
|
||||
using content::RenderView;
|
||||
|
||||
namespace {
|
||||
const CefPluginPlaceholder* g_last_active_menu = NULL;
|
||||
} // namespace
|
||||
|
||||
gin::WrapperInfo CefPluginPlaceholder::kWrapperInfo = {
|
||||
gin::kEmbedderNativeGin};
|
||||
|
||||
CefPluginPlaceholder::CefPluginPlaceholder(
|
||||
content::RenderFrame* render_frame,
|
||||
blink::WebLocalFrame* frame,
|
||||
const blink::WebPluginParams& params,
|
||||
const std::string& html_data,
|
||||
const base::string16& title)
|
||||
: plugins::LoadablePluginPlaceholder(render_frame,
|
||||
frame,
|
||||
params,
|
||||
html_data),
|
||||
status_(CefViewHostMsg_GetPluginInfo_Status::kAllowed),
|
||||
title_(title),
|
||||
has_host_(false),
|
||||
context_menu_request_id_(0) {
|
||||
RenderThread::Get()->AddObserver(this);
|
||||
}
|
||||
|
||||
CefPluginPlaceholder::~CefPluginPlaceholder() {
|
||||
RenderThread::Get()->RemoveObserver(this);
|
||||
if (context_menu_request_id_ && render_frame())
|
||||
render_frame()->CancelContextMenu(context_menu_request_id_);
|
||||
}
|
||||
|
||||
// static
|
||||
CefPluginPlaceholder* CefPluginPlaceholder::CreateLoadableMissingPlugin(
|
||||
content::RenderFrame* render_frame,
|
||||
blink::WebLocalFrame* frame,
|
||||
const blink::WebPluginParams& params) {
|
||||
const base::StringPiece template_html(
|
||||
ResourceBundle::GetSharedInstance().GetRawDataResource(
|
||||
IDR_BLOCKED_PLUGIN_HTML));
|
||||
|
||||
base::DictionaryValue values;
|
||||
values.SetString("message",
|
||||
l10n_util::GetStringUTF8(IDS_PLUGIN_NOT_SUPPORTED));
|
||||
|
||||
std::string html_data = webui::GetI18nTemplateHtml(template_html, &values);
|
||||
|
||||
// Will destroy itself when its WebViewPlugin is going away.
|
||||
return new CefPluginPlaceholder(render_frame, frame, params, html_data,
|
||||
params.mimeType);
|
||||
}
|
||||
|
||||
// static
|
||||
CefPluginPlaceholder* CefPluginPlaceholder::CreateBlockedPlugin(
|
||||
content::RenderFrame* render_frame,
|
||||
blink::WebLocalFrame* frame,
|
||||
const blink::WebPluginParams& params,
|
||||
const content::WebPluginInfo& info,
|
||||
const std::string& identifier,
|
||||
const base::string16& name,
|
||||
int template_id,
|
||||
const base::string16& message,
|
||||
const PlaceholderPosterInfo& poster_info) {
|
||||
base::DictionaryValue values;
|
||||
values.SetString("message", message);
|
||||
values.SetString("name", name);
|
||||
values.SetString("hide", l10n_util::GetStringUTF8(IDS_PLUGIN_HIDE));
|
||||
values.SetString("pluginType",
|
||||
frame->view()->mainFrame()->document().isPluginDocument()
|
||||
? "document"
|
||||
: "embedded");
|
||||
|
||||
if (!poster_info.poster_attribute.empty()) {
|
||||
values.SetString("poster", poster_info.poster_attribute);
|
||||
values.SetString("baseurl", poster_info.base_url.spec());
|
||||
|
||||
if (!poster_info.custom_poster_size.IsEmpty()) {
|
||||
float zoom_factor =
|
||||
blink::WebView::zoomLevelToZoomFactor(frame->view()->zoomLevel());
|
||||
int width = roundf(poster_info.custom_poster_size.width() / zoom_factor);
|
||||
int height =
|
||||
roundf(poster_info.custom_poster_size.height() / zoom_factor);
|
||||
values.SetString("visibleWidth", base::IntToString(width) + "px");
|
||||
values.SetString("visibleHeight", base::IntToString(height) + "px");
|
||||
}
|
||||
}
|
||||
|
||||
const base::StringPiece template_html(
|
||||
ResourceBundle::GetSharedInstance().GetRawDataResource(template_id));
|
||||
|
||||
DCHECK(!template_html.empty()) << "unable to load template. ID: "
|
||||
<< template_id;
|
||||
std::string html_data = webui::GetI18nTemplateHtml(template_html, &values);
|
||||
|
||||
// |blocked_plugin| will destroy itself when its WebViewPlugin is going away.
|
||||
CefPluginPlaceholder* blocked_plugin = new CefPluginPlaceholder(
|
||||
render_frame, frame, params, html_data, name);
|
||||
|
||||
if (!poster_info.poster_attribute.empty())
|
||||
blocked_plugin->BlockForPowerSaverPoster();
|
||||
blocked_plugin->SetPluginInfo(info);
|
||||
blocked_plugin->SetIdentifier(identifier);
|
||||
return blocked_plugin;
|
||||
}
|
||||
|
||||
void CefPluginPlaceholder::SetStatus(
|
||||
CefViewHostMsg_GetPluginInfo_Status status) {
|
||||
status_ = status;
|
||||
}
|
||||
|
||||
bool CefPluginPlaceholder::OnMessageReceived(const IPC::Message& message) {
|
||||
// We don't swallow these messages because multiple blocked plugins and other
|
||||
// objects have an interest in them.
|
||||
IPC_BEGIN_MESSAGE_MAP(CefPluginPlaceholder, message)
|
||||
IPC_MESSAGE_HANDLER(CefViewMsg_LoadBlockedPlugins, OnLoadBlockedPlugins)
|
||||
IPC_END_MESSAGE_MAP()
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void CefPluginPlaceholder::OpenAboutPluginsCallback() {
|
||||
// CEF does not use IDR_DISABLED_PLUGIN_HTML which would originate this
|
||||
// callback for the chrome://plugins link.
|
||||
NOTREACHED();
|
||||
}
|
||||
|
||||
void CefPluginPlaceholder::PluginListChanged() {
|
||||
if (!GetFrame() || !plugin())
|
||||
return;
|
||||
blink::WebDocument document = GetFrame()->top()->document();
|
||||
if (document.isNull())
|
||||
return;
|
||||
|
||||
CefViewHostMsg_GetPluginInfo_Output output;
|
||||
std::string mime_type(GetPluginParams().mimeType.utf8());
|
||||
blink::WebString top_origin = GetFrame()->top()->securityOrigin().toString();
|
||||
render_frame()->Send(
|
||||
new CefViewHostMsg_GetPluginInfo(routing_id(),
|
||||
GURL(GetPluginParams().url),
|
||||
GURL(top_origin),
|
||||
mime_type,
|
||||
&output));
|
||||
if (output.status == status_)
|
||||
return;
|
||||
blink::WebPlugin* new_plugin = CefContentRendererClient::CreatePlugin(
|
||||
render_frame(), GetFrame(), GetPluginParams(), output);
|
||||
ReplacePlugin(new_plugin);
|
||||
}
|
||||
|
||||
void CefPluginPlaceholder::OnMenuAction(int request_id, unsigned action) {
|
||||
DCHECK_EQ(context_menu_request_id_, request_id);
|
||||
if (g_last_active_menu != this)
|
||||
return;
|
||||
switch (action) {
|
||||
case chrome::MENU_COMMAND_PLUGIN_RUN: {
|
||||
RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Load_Menu"));
|
||||
MarkPluginEssential(
|
||||
content::PluginInstanceThrottler::UNTHROTTLE_METHOD_BY_CLICK);
|
||||
LoadPlugin();
|
||||
break;
|
||||
}
|
||||
case chrome::MENU_COMMAND_PLUGIN_HIDE: {
|
||||
RenderThread::Get()->RecordAction(UserMetricsAction("Plugin_Hide_Menu"));
|
||||
HidePlugin();
|
||||
break;
|
||||
}
|
||||
default:
|
||||
NOTREACHED();
|
||||
}
|
||||
}
|
||||
|
||||
void CefPluginPlaceholder::OnMenuClosed(int request_id) {
|
||||
DCHECK_EQ(context_menu_request_id_, request_id);
|
||||
context_menu_request_id_ = 0;
|
||||
}
|
||||
|
||||
v8::Local<v8::Value> CefPluginPlaceholder::GetV8Handle(
|
||||
v8::Isolate* isolate) {
|
||||
return gin::CreateHandle(isolate, this).ToV8();
|
||||
}
|
||||
|
||||
void CefPluginPlaceholder::ShowContextMenu(
|
||||
const blink::WebMouseEvent& event) {
|
||||
if (context_menu_request_id_)
|
||||
return; // Don't allow nested context menu requests.
|
||||
|
||||
content::ContextMenuParams params;
|
||||
|
||||
if (!title_.empty()) {
|
||||
content::MenuItem name_item;
|
||||
name_item.label = title_;
|
||||
params.custom_items.push_back(name_item);
|
||||
|
||||
content::MenuItem separator_item;
|
||||
separator_item.type = content::MenuItem::SEPARATOR;
|
||||
params.custom_items.push_back(separator_item);
|
||||
}
|
||||
|
||||
if (!GetPluginInfo().path.value().empty()) {
|
||||
content::MenuItem run_item;
|
||||
run_item.action = chrome::MENU_COMMAND_PLUGIN_RUN;
|
||||
// Disable this menu item if the plugin is blocked by policy.
|
||||
run_item.enabled = LoadingAllowed();
|
||||
run_item.label = l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_PLUGIN_RUN);
|
||||
params.custom_items.push_back(run_item);
|
||||
}
|
||||
|
||||
content::MenuItem hide_item;
|
||||
hide_item.action = chrome::MENU_COMMAND_PLUGIN_HIDE;
|
||||
hide_item.enabled =
|
||||
!GetFrame()->view()->mainFrame()->document().isPluginDocument();
|
||||
hide_item.label = l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_PLUGIN_HIDE);
|
||||
params.custom_items.push_back(hide_item);
|
||||
|
||||
params.x = event.windowX;
|
||||
params.y = event.windowY;
|
||||
|
||||
context_menu_request_id_ = render_frame()->ShowContextMenu(this, params);
|
||||
g_last_active_menu = this;
|
||||
}
|
||||
|
||||
blink::WebPlugin* CefPluginPlaceholder::CreatePlugin() {
|
||||
scoped_ptr<content::PluginInstanceThrottler> throttler;
|
||||
// If the plugin has already been marked essential in its placeholder form,
|
||||
// we shouldn't create a new throttler and start the process all over again.
|
||||
if (power_saver_enabled()) {
|
||||
throttler = content::PluginInstanceThrottler::Create();
|
||||
// PluginPreroller manages its own lifetime.
|
||||
new CefPluginPreroller(
|
||||
render_frame(), GetFrame(), GetPluginParams(),
|
||||
GetPluginInfo(), GetIdentifier(), title_,
|
||||
l10n_util::GetStringFUTF16(IDS_PLUGIN_BLOCKED, title_),
|
||||
throttler.get());
|
||||
}
|
||||
return render_frame()->CreatePlugin(GetFrame(), GetPluginInfo(),
|
||||
GetPluginParams(), throttler.Pass());
|
||||
}
|
||||
|
||||
gin::ObjectTemplateBuilder CefPluginPlaceholder::GetObjectTemplateBuilder(
|
||||
v8::Isolate* isolate) {
|
||||
return gin::Wrappable<CefPluginPlaceholder>::GetObjectTemplateBuilder(
|
||||
isolate)
|
||||
.SetMethod<void (CefPluginPlaceholder::*)()>(
|
||||
"hide", &CefPluginPlaceholder::HideCallback)
|
||||
.SetMethod<void (CefPluginPlaceholder::*)()>(
|
||||
"load", &CefPluginPlaceholder::LoadCallback)
|
||||
.SetMethod<void (CefPluginPlaceholder::*)()>(
|
||||
"didFinishLoading",
|
||||
&CefPluginPlaceholder::DidFinishLoadingCallback)
|
||||
.SetMethod("openAboutPlugins",
|
||||
&CefPluginPlaceholder::OpenAboutPluginsCallback);
|
||||
}
|
102
libcef/renderer/plugins/cef_plugin_placeholder.h
Normal file
102
libcef/renderer/plugins/cef_plugin_placeholder.h
Normal file
@@ -0,0 +1,102 @@
|
||||
// Copyright 2013 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef CEF_LIBCEF_RENDERER_PLUGINS_CEF_PLUGIN_PLACEHOLDER_H_
|
||||
#define CEF_LIBCEF_RENDERER_PLUGINS_CEF_PLUGIN_PLACEHOLDER_H_
|
||||
|
||||
#include "components/plugins/renderer/loadable_plugin_placeholder.h"
|
||||
#include "content/public/renderer/context_menu_client.h"
|
||||
#include "content/public/renderer/render_process_observer.h"
|
||||
|
||||
namespace gfx {
|
||||
class Size;
|
||||
}
|
||||
|
||||
enum class CefViewHostMsg_GetPluginInfo_Status;
|
||||
|
||||
// This contains information specifying the poster image of plugin placeholders.
|
||||
// The default constructor specifies no poster image.
|
||||
struct PlaceholderPosterInfo {
|
||||
// The poster image specified in image 'srcset' attribute format.
|
||||
std::string poster_attribute;
|
||||
|
||||
// Used to resolve relative paths in |poster_attribute|.
|
||||
GURL base_url;
|
||||
|
||||
// Specify this to provide partially obscured plugins a centered poster image.
|
||||
gfx::Size custom_poster_size;
|
||||
};
|
||||
|
||||
class CefPluginPlaceholder final
|
||||
: public plugins::LoadablePluginPlaceholder,
|
||||
public content::RenderProcessObserver,
|
||||
public content::ContextMenuClient,
|
||||
public gin::Wrappable<CefPluginPlaceholder> {
|
||||
public:
|
||||
static gin::WrapperInfo kWrapperInfo;
|
||||
|
||||
static CefPluginPlaceholder* CreateBlockedPlugin(
|
||||
content::RenderFrame* render_frame,
|
||||
blink::WebLocalFrame* frame,
|
||||
const blink::WebPluginParams& params,
|
||||
const content::WebPluginInfo& info,
|
||||
const std::string& identifier,
|
||||
const base::string16& name,
|
||||
int resource_id,
|
||||
const base::string16& message,
|
||||
const PlaceholderPosterInfo& poster_info);
|
||||
|
||||
// Creates a new WebViewPlugin with a MissingPlugin as a delegate.
|
||||
static CefPluginPlaceholder* CreateLoadableMissingPlugin(
|
||||
content::RenderFrame* render_frame,
|
||||
blink::WebLocalFrame* frame,
|
||||
const blink::WebPluginParams& params);
|
||||
|
||||
void SetStatus(CefViewHostMsg_GetPluginInfo_Status status);
|
||||
|
||||
private:
|
||||
CefPluginPlaceholder(content::RenderFrame* render_frame,
|
||||
blink::WebLocalFrame* frame,
|
||||
const blink::WebPluginParams& params,
|
||||
const std::string& html_data,
|
||||
const base::string16& title);
|
||||
~CefPluginPlaceholder() override;
|
||||
|
||||
// content::LoadablePluginPlaceholder method
|
||||
blink::WebPlugin* CreatePlugin() override;
|
||||
|
||||
// gin::Wrappable (via PluginPlaceholder) method
|
||||
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
|
||||
v8::Isolate* isolate) final;
|
||||
|
||||
// content::RenderViewObserver (via PluginPlaceholder) override:
|
||||
bool OnMessageReceived(const IPC::Message& message) override;
|
||||
|
||||
// WebViewPlugin::Delegate (via PluginPlaceholder) methods:
|
||||
v8::Local<v8::Value> GetV8Handle(v8::Isolate* isolate) override;
|
||||
void ShowContextMenu(const blink::WebMouseEvent&) override;
|
||||
|
||||
// content::RenderProcessObserver methods:
|
||||
void PluginListChanged() override;
|
||||
|
||||
// content::ContextMenuClient methods:
|
||||
void OnMenuAction(int request_id, unsigned action) override;
|
||||
void OnMenuClosed(int request_id) override;
|
||||
|
||||
// Javascript callbacks:
|
||||
// Open chrome://plugins in a new tab.
|
||||
void OpenAboutPluginsCallback();
|
||||
|
||||
CefViewHostMsg_GetPluginInfo_Status status_;
|
||||
|
||||
base::string16 title_;
|
||||
|
||||
bool has_host_;
|
||||
int context_menu_request_id_; // Nonzero when request pending.
|
||||
base::string16 plugin_name_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(CefPluginPlaceholder);
|
||||
};
|
||||
|
||||
#endif // CEF_LIBCEF_RENDERER_PLUGINS_CEF_PLUGIN_PLACEHOLDER_H_
|
91
libcef/renderer/plugins/plugin_preroller.cc
Normal file
91
libcef/renderer/plugins/plugin_preroller.cc
Normal file
@@ -0,0 +1,91 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "libcef/renderer/plugins/plugin_preroller.h"
|
||||
|
||||
#include "libcef/renderer/plugins/cef_plugin_placeholder.h"
|
||||
|
||||
#include "base/base64.h"
|
||||
#include "chrome/grit/generated_resources.h"
|
||||
#include "third_party/WebKit/public/platform/WebRect.h"
|
||||
#include "third_party/WebKit/public/web/WebElement.h"
|
||||
#include "third_party/WebKit/public/web/WebPlugin.h"
|
||||
#include "third_party/WebKit/public/web/WebPluginContainer.h"
|
||||
#include "ui/gfx/codec/png_codec.h"
|
||||
|
||||
CefPluginPreroller::CefPluginPreroller(
|
||||
content::RenderFrame* render_frame,
|
||||
blink::WebLocalFrame* frame,
|
||||
const blink::WebPluginParams& params,
|
||||
const content::WebPluginInfo& info,
|
||||
const std::string& identifier,
|
||||
const base::string16& name,
|
||||
const base::string16& message,
|
||||
content::PluginInstanceThrottler* throttler)
|
||||
: RenderFrameObserver(render_frame),
|
||||
frame_(frame),
|
||||
params_(params),
|
||||
info_(info),
|
||||
identifier_(identifier),
|
||||
name_(name),
|
||||
message_(message),
|
||||
throttler_(throttler) {
|
||||
DCHECK(throttler);
|
||||
throttler_->AddObserver(this);
|
||||
}
|
||||
|
||||
CefPluginPreroller::~CefPluginPreroller() {
|
||||
if (throttler_)
|
||||
throttler_->RemoveObserver(this);
|
||||
}
|
||||
|
||||
void CefPluginPreroller::OnKeyframeExtracted(const SkBitmap* bitmap) {
|
||||
std::vector<unsigned char> png_data;
|
||||
if (!gfx::PNGCodec::EncodeBGRASkBitmap(*bitmap, false, &png_data)) {
|
||||
DLOG(ERROR) << "Provided keyframe could not be encoded as PNG.";
|
||||
return;
|
||||
}
|
||||
|
||||
base::StringPiece png_as_string(reinterpret_cast<char*>(&png_data[0]),
|
||||
png_data.size());
|
||||
|
||||
std::string data_url_header = "data:image/png;base64,";
|
||||
std::string data_url_body;
|
||||
base::Base64Encode(png_as_string, &data_url_body);
|
||||
keyframe_data_url_ = GURL(data_url_header + data_url_body);
|
||||
}
|
||||
|
||||
void CefPluginPreroller::OnThrottleStateChange() {
|
||||
if (!throttler_->IsThrottled())
|
||||
return;
|
||||
|
||||
PlaceholderPosterInfo poster_info;
|
||||
poster_info.poster_attribute = keyframe_data_url_.spec();
|
||||
poster_info.custom_poster_size = throttler_->GetSize();
|
||||
|
||||
CefPluginPlaceholder* placeholder =
|
||||
CefPluginPlaceholder::CreateBlockedPlugin(
|
||||
render_frame(), frame_, params_, info_, identifier_, name_,
|
||||
IDR_PLUGIN_POSTER_HTML, message_, poster_info);
|
||||
placeholder->SetPremadePlugin(throttler_);
|
||||
placeholder->set_power_saver_enabled(true);
|
||||
placeholder->AllowLoading();
|
||||
|
||||
blink::WebPluginContainer* container =
|
||||
throttler_->GetWebPlugin()->container();
|
||||
container->setPlugin(placeholder->plugin());
|
||||
|
||||
bool success = placeholder->plugin()->initialize(container);
|
||||
DCHECK(success);
|
||||
|
||||
container->invalidate();
|
||||
container->reportGeometry();
|
||||
|
||||
delete this;
|
||||
}
|
||||
|
||||
void CefPluginPreroller::OnThrottlerDestroyed() {
|
||||
throttler_ = nullptr;
|
||||
delete this;
|
||||
}
|
61
libcef/renderer/plugins/plugin_preroller.h
Normal file
61
libcef/renderer/plugins/plugin_preroller.h
Normal file
@@ -0,0 +1,61 @@
|
||||
// Copyright 2015 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef CEF_LIBCEF_RENDERER_PLUGINS_PLUGIN_PREROLLER_H_
|
||||
#define CEF_LIBCEF_RENDERER_PLUGINS_PLUGIN_PREROLLER_H_
|
||||
|
||||
#include "base/macros.h"
|
||||
#include "content/public/common/webplugininfo.h"
|
||||
#include "content/public/renderer/plugin_instance_throttler.h"
|
||||
#include "content/public/renderer/render_frame_observer.h"
|
||||
#include "third_party/WebKit/public/web/WebPluginParams.h"
|
||||
#include "url/gurl.h"
|
||||
|
||||
namespace blink {
|
||||
class WebLocalFrame;
|
||||
class WebPlugin;
|
||||
}
|
||||
|
||||
class SkBitmap;
|
||||
|
||||
// This class manages a plugin briefly for the purposes of keyframe extraction.
|
||||
// Once a keyframe has been extracted, this class will replace the plugin with
|
||||
// a ChromePluginPlaceholder. The actual plugin will continue to live in a
|
||||
// throttled state. This class manages its own lifetime.
|
||||
class CefPluginPreroller : public content::PluginInstanceThrottler::Observer,
|
||||
public content::RenderFrameObserver {
|
||||
public:
|
||||
// Does not take ownership of either |plugin| or |throttler|.
|
||||
CefPluginPreroller(content::RenderFrame* render_frame,
|
||||
blink::WebLocalFrame* frame,
|
||||
const blink::WebPluginParams& params,
|
||||
const content::WebPluginInfo& info,
|
||||
const std::string& identifier,
|
||||
const base::string16& name,
|
||||
const base::string16& message,
|
||||
content::PluginInstanceThrottler* throttler);
|
||||
|
||||
~CefPluginPreroller() override;
|
||||
|
||||
private:
|
||||
// content::PluginInstanceThrottler::Observer methods:
|
||||
void OnKeyframeExtracted(const SkBitmap* bitmap) override;
|
||||
void OnThrottleStateChange() override;
|
||||
void OnThrottlerDestroyed() override;
|
||||
|
||||
blink::WebLocalFrame* frame_;
|
||||
blink::WebPluginParams params_;
|
||||
content::WebPluginInfo info_;
|
||||
std::string identifier_;
|
||||
base::string16 name_;
|
||||
base::string16 message_;
|
||||
|
||||
content::PluginInstanceThrottler* throttler_;
|
||||
|
||||
GURL keyframe_data_url_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(CefPluginPreroller);
|
||||
};
|
||||
|
||||
#endif // CEF_LIBCEF_RENDERER_PLUGINS_PLUGIN_PREROLLER_H_
|
Reference in New Issue
Block a user