Call OnBeforePluginLoad when building the plugin list (issue #1708)

This commit is contained in:
Marshall Greenblatt
2015-09-25 14:59:30 +03:00
parent a33720558e
commit 4149192d81
23 changed files with 421 additions and 161 deletions

View File

@ -6,9 +6,9 @@
#include "libcef/browser/plugins/plugin_info_message_filter.h"
#include "libcef/browser/browser_context.h"
#include "libcef/browser/plugins/plugin_service_filter.h"
#include "libcef/browser/web_plugin_impl.h"
#include "libcef/common/cef_messages.h"
#include "libcef/common/content_client.h"
#include "base/bind.h"
#include "base/memory/scoped_ptr.h"
@ -243,83 +243,15 @@ void CefPluginInfoMessageFilter::PluginsLoaded(
IPC::Message* reply_msg,
const std::vector<WebPluginInfo>& plugins) {
CefViewHostMsg_GetPluginInfo_Output output;
CefRefPtr<CefRequestContextHandler> handler =
browser_context_->GetHandler();
// This also fills in |actual_mime_type|.
scoped_ptr<PluginMetadata> plugin_metadata;
if (context_.FindEnabledPlugin(params.render_frame_id, params.url,
params.top_origin_url, params.mime_type,
&output.status, &output.plugin,
&output.actual_mime_type,
&plugin_metadata)) {
context_.DecidePluginStatus(params, output.plugin, plugin_metadata.get(),
&output.status);
const bool is_found =
output.status != CefViewHostMsg_GetPluginInfo_Status::kNotFound &&
output.status != CefViewHostMsg_GetPluginInfo_Status::kNPAPINotSupported;
const bool is_internal_pdf_plugin =
is_found &&
(output.plugin.path == CefString(CefContentClient::kPDFPluginPath));
if (is_internal_pdf_plugin &&
output.status != CefViewHostMsg_GetPluginInfo_Status::kAllowed) {
// Always allow the internal PDF plugin to load if it exists.
output.status = CefViewHostMsg_GetPluginInfo_Status::kAllowed;
}
// Give the handler an opportunity to modify the policy.
CefRefPtr<CefRequestContextHandler> handler =
browser_context_->GetHandler();
if (handler.get() &&
is_found &&
!is_internal_pdf_plugin) {
cef_plugin_policy_t plugin_policy = PLUGIN_POLICY_DISABLE;
switch (output.status) {
case CefViewHostMsg_GetPluginInfo_Status::kAllowed:
plugin_policy = PLUGIN_POLICY_ALLOW;
break;
case CefViewHostMsg_GetPluginInfo_Status::kBlocked:
case CefViewHostMsg_GetPluginInfo_Status::kBlockedByPolicy:
case CefViewHostMsg_GetPluginInfo_Status::kOutdatedBlocked:
case CefViewHostMsg_GetPluginInfo_Status::kOutdatedDisallowed:
case CefViewHostMsg_GetPluginInfo_Status::kUnauthorized:
plugin_policy = PLUGIN_POLICY_BLOCK;
break;
case CefViewHostMsg_GetPluginInfo_Status::kDisabled:
plugin_policy = PLUGIN_POLICY_DISABLE;
break;
case CefViewHostMsg_GetPluginInfo_Status::kPlayImportantContent:
plugin_policy = PLUGIN_POLICY_DETECT_IMPORTANT;
break;
default:
NOTREACHED();
break;
}
CefRefPtr<CefWebPluginInfoImpl> pluginInfo(
new CefWebPluginInfoImpl(output.plugin));
if (handler->OnBeforePluginLoad(output.actual_mime_type,
params.url.spec(),
params.top_origin_url.spec(),
pluginInfo.get(),
&plugin_policy)) {
switch (plugin_policy) {
case PLUGIN_POLICY_ALLOW:
output.status = CefViewHostMsg_GetPluginInfo_Status::kAllowed;
break;
case PLUGIN_POLICY_DETECT_IMPORTANT:
output.status =
CefViewHostMsg_GetPluginInfo_Status::kPlayImportantContent;
break;
case PLUGIN_POLICY_BLOCK:
output.status = CefViewHostMsg_GetPluginInfo_Status::kBlocked;
break;
case PLUGIN_POLICY_DISABLE:
output.status = CefViewHostMsg_GetPluginInfo_Status::kDisabled;
break;
}
}
}
}
context_.FindEnabledPlugin(params, handler.get(),
&output.status, &output.plugin,
&output.actual_mime_type,
&plugin_metadata);
if (plugin_metadata) {
output.group_identifier = plugin_metadata->identifier();
@ -488,10 +420,8 @@ void CefPluginInfoMessageFilter::Context::DecidePluginStatus(
}
bool CefPluginInfoMessageFilter::Context::FindEnabledPlugin(
int render_frame_id,
const GURL& url,
const GURL& top_origin_url,
const std::string& mime_type,
const GetPluginInfo_Params& params,
CefRequestContextHandler* handler,
CefViewHostMsg_GetPluginInfo_Status* status,
WebPluginInfo* plugin,
std::string* actual_mime_type,
@ -502,7 +432,8 @@ bool CefPluginInfoMessageFilter::Context::FindEnabledPlugin(
std::vector<WebPluginInfo> matching_plugins;
std::vector<std::string> mime_types;
PluginService::GetInstance()->GetPluginInfoArray(
url, mime_type, allow_wildcard, &matching_plugins, &mime_types);
params.url, params.mime_type, allow_wildcard, &matching_plugins,
&mime_types);
if (matching_plugins.empty()) {
*status = CefViewHostMsg_GetPluginInfo_Status::kNotFound;
#if defined(OS_WIN) || defined(OS_MACOSX)
@ -511,39 +442,48 @@ bool CefPluginInfoMessageFilter::Context::FindEnabledPlugin(
// could be a not-yet-installed Pepper plugin. To avoid notifying on
// these types, bail early based on a blacklist of pepper mime types.
for (auto pepper_mime_type : kPepperPluginMimeTypes)
if (pepper_mime_type == mime_type)
if (pepper_mime_type == params.mime_type)
return false;
}
#endif
return false;
}
content::PluginServiceFilter* filter =
PluginService::GetInstance()->GetFilter();
CefPluginServiceFilter* filter = static_cast<CefPluginServiceFilter*>(
PluginService::GetInstance()->GetFilter());
DCHECK(filter);
CefViewHostMsg_GetPluginInfo_Status first_status = *status;
size_t i = 0;
for (; i < matching_plugins.size(); ++i) {
if (!filter || filter->IsPluginAvailable(render_process_id_,
render_frame_id,
resource_context_,
url,
top_origin_url,
&matching_plugins[i])) {
*plugin = matching_plugins[i];
*actual_mime_type = mime_types[i];
*plugin_metadata = PluginFinder::GetInstance()->GetPluginMetadata(*plugin);
DecidePluginStatus(params, *plugin, (*plugin_metadata).get(), status);
if (filter->IsPluginAvailable(handler,
params.url,
params.top_origin_url,
plugin,
status)) {
break;
}
if (i == 0)
first_status = *status;
}
// If we broke out of the loop, we have found an enabled plugin.
bool enabled = i < matching_plugins.size();
if (!enabled) {
// Otherwise, we only found disabled plugins, so we take the first one.
i = 0;
*status = CefViewHostMsg_GetPluginInfo_Status::kDisabled;
}
*plugin = matching_plugins[i];
*actual_mime_type = mime_types[i];
if (plugin_metadata)
// Otherwise, we only found disabled/not-found plugins, so we take the first
// one.
*plugin = matching_plugins[0];
*actual_mime_type = mime_types[0];
*plugin_metadata = PluginFinder::GetInstance()->GetPluginMetadata(*plugin);
*status = first_status;
}
return enabled;
}

View File

@ -20,6 +20,7 @@
#include "content/public/browser/browser_message_filter.h"
class CefBrowserContext;
class CefRequestContextHandler;
struct CefViewHostMsg_GetPluginInfo_Output;
enum class CefViewHostMsg_GetPluginInfo_Status;
class GURL;
@ -50,10 +51,8 @@ class CefPluginInfoMessageFilter : public content::BrowserMessageFilter {
const content::WebPluginInfo& plugin,
const PluginMetadata* plugin_metadata,
CefViewHostMsg_GetPluginInfo_Status* status) const;
bool FindEnabledPlugin(int render_frame_id,
const GURL& url,
const GURL& top_origin_url,
const std::string& mime_type,
bool FindEnabledPlugin(const GetPluginInfo_Params& params,
CefRequestContextHandler* handler,
CefViewHostMsg_GetPluginInfo_Status* status,
content::WebPluginInfo* plugin,
std::string* actual_mime_type,

View File

@ -0,0 +1,106 @@
// Copyright (c) 2015 The Chromium Embedded Framework 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/browser/plugins/plugin_service_filter.h"
#include "include/cef_request_context_handler.h"
#include "libcef/browser/browser_host_impl.h"
#include "libcef/browser/resource_context.h"
#include "libcef/browser/thread_util.h"
#include "libcef/browser/web_plugin_impl.h"
#include "libcef/common/cef_messages.h"
#include "libcef/common/content_client.h"
CefPluginServiceFilter::CefPluginServiceFilter() {
}
bool CefPluginServiceFilter::IsPluginAvailable(
int render_process_id,
int render_frame_id,
const void* context,
const GURL& url,
const GURL& policy_url,
content::WebPluginInfo* plugin) {
CefRefPtr<CefRequestContextHandler> handler =
reinterpret_cast<const CefResourceContext*>(context)->GetHandler();
CefViewHostMsg_GetPluginInfo_Status status =
CefViewHostMsg_GetPluginInfo_Status::kAllowed;
return IsPluginAvailable(handler.get(), url, policy_url, plugin, &status);
}
bool CefPluginServiceFilter::CanLoadPlugin(int render_process_id,
const base::FilePath& path) {
return true;
}
bool CefPluginServiceFilter::IsPluginAvailable(
CefRequestContextHandler* handler,
const GURL& url,
const GURL& policy_url,
content::WebPluginInfo* plugin,
CefViewHostMsg_GetPluginInfo_Status* status) {
if (*status == CefViewHostMsg_GetPluginInfo_Status::kNotFound ||
*status == CefViewHostMsg_GetPluginInfo_Status::kNPAPINotSupported) {
// The plugin does not exist so no need to query the handler.
return false;
}
if (plugin->path == CefString(CefContentClient::kPDFPluginPath)) {
// Always allow the internal PDF plugin to load.
*status = CefViewHostMsg_GetPluginInfo_Status::kAllowed;
return true;
}
if (handler) {
CefRefPtr<CefWebPluginInfoImpl> pluginInfo(
new CefWebPluginInfoImpl(*plugin));
cef_plugin_policy_t plugin_policy = PLUGIN_POLICY_ALLOW;
switch (*status) {
case CefViewHostMsg_GetPluginInfo_Status::kAllowed:
plugin_policy = PLUGIN_POLICY_ALLOW;
break;
case CefViewHostMsg_GetPluginInfo_Status::kBlocked:
case CefViewHostMsg_GetPluginInfo_Status::kBlockedByPolicy:
case CefViewHostMsg_GetPluginInfo_Status::kOutdatedBlocked:
case CefViewHostMsg_GetPluginInfo_Status::kOutdatedDisallowed:
case CefViewHostMsg_GetPluginInfo_Status::kUnauthorized:
plugin_policy = PLUGIN_POLICY_BLOCK;
break;
case CefViewHostMsg_GetPluginInfo_Status::kDisabled:
plugin_policy = PLUGIN_POLICY_DISABLE;
break;
case CefViewHostMsg_GetPluginInfo_Status::kPlayImportantContent:
plugin_policy = PLUGIN_POLICY_DETECT_IMPORTANT;
break;
default:
NOTREACHED();
break;
}
if (handler->OnBeforePluginLoad(plugin->mime_types[0].mime_type,
url.possibly_invalid_spec(),
policy_url.possibly_invalid_spec(),
pluginInfo.get(),
&plugin_policy)) {
switch (plugin_policy) {
case PLUGIN_POLICY_ALLOW:
*status = CefViewHostMsg_GetPluginInfo_Status::kAllowed;
break;
case PLUGIN_POLICY_DETECT_IMPORTANT:
*status = CefViewHostMsg_GetPluginInfo_Status::kPlayImportantContent;
break;
case PLUGIN_POLICY_BLOCK:
*status = CefViewHostMsg_GetPluginInfo_Status::kBlocked;
break;
case PLUGIN_POLICY_DISABLE:
*status = CefViewHostMsg_GetPluginInfo_Status::kDisabled;
break;
}
}
}
return (*status != CefViewHostMsg_GetPluginInfo_Status::kDisabled);
}

View File

@ -0,0 +1,46 @@
// Copyright (c) 2015 The Chromium Embedded Framework 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_BROWSER_PLUGINS_PLUGIN_SERVICE_FILTER_H_
#define CEF_LIBCEF_BROWSER_PLUGINS_PLUGIN_SERVICE_FILTER_H_
#include "content/public/browser/plugin_service_filter.h"
#include "include/internal/cef_types.h"
#include "base/macros.h"
class CefRequestContextHandler;
enum class CefViewHostMsg_GetPluginInfo_Status;
class CefPluginServiceFilter : public content::PluginServiceFilter {
public:
CefPluginServiceFilter();
// Called whenever the plugin list is queried. For example, when choosing the
// plugin to handle a mime type or when determining the plugins that will be
// exposed to JavaScript via 'navigator.plugins'.
bool IsPluginAvailable(int render_process_id,
int render_frame_id,
const void* context,
const GURL& url,
const GURL& policy_url,
content::WebPluginInfo* plugin) override;
bool CanLoadPlugin(int render_process_id,
const base::FilePath& path) override;
// Returns false if the plugin is not found or disabled. May call
// CefRequestContextHandler::OnBeforePluginLoad if possible/necessary.
bool IsPluginAvailable(CefRequestContextHandler* handler,
const GURL& url,
const GURL& policy_url,
content::WebPluginInfo* plugin,
CefViewHostMsg_GetPluginInfo_Status* status);
private:
DISALLOW_COPY_AND_ASSIGN(CefPluginServiceFilter);
};
#endif // CEF_LIBCEF_BROWSER_PLUGINS_PLUGIN_SERVICE_FILTER_H_