cef/libcef/browser/plugins/plugin_service_filter.cc

163 lines
6.0 KiB
C++
Raw Normal View History

// 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_context.h"
#include "libcef/browser/thread_util.h"
#include "libcef/browser/web_plugin_impl.h"
#include "libcef/common/content_client.h"
#include "extensions/common/constants.h"
CefPluginServiceFilter::CefPluginServiceFilter() {}
bool CefPluginServiceFilter::IsPluginAvailable(
int render_process_id,
int render_frame_id,
const GURL& url,
bool is_main_frame,
const url::Origin& main_frame_origin,
content::WebPluginInfo* plugin) {
CEF_REQUIRE_UIT();
DCHECK_GT(render_process_id, 0);
chrome::mojom::PluginStatus status = chrome::mojom::PluginStatus::kAllowed;
// Perform origin check here because we're passing an empty origin value to
// IsPluginAvailable() below.
const GURL& policy_url = main_frame_origin.GetURL();
if (!policy_url.is_empty() &&
policy_url.scheme() == extensions::kExtensionScheme) {
// Always allow extension origins to load plugins.
// TODO(extensions): Revisit this decision once CEF supports more than just
// the PDF extension.
return true;
}
// Blink requires this method to return a consistent value during renderer
// process initialization and page load, so we always call IsPluginAvailable()
// with an empty origin. If we return false then the plugin will not be listed
// in navigator.plugins and navigating to the plugin mime type will trigger
// the download code path. If we return true then individual plugin instance
// loads will be evaluated in CefContentRendererClient::OverrideCreatePlugin,
// which will result in a call to CefPluginInfoMessageFilter::PluginsLoaded to
// retrieve the actual load decision with a non-empty origin. That will
// determine whether the plugin load is allowed or the plugin placeholder is
// displayed.
return IsPluginAvailable(render_process_id, render_frame_id, url,
is_main_frame, url::Origin(), plugin, &status);
}
bool CefPluginServiceFilter::CanLoadPlugin(int render_process_id,
const base::FilePath& path) {
return true;
}
bool CefPluginServiceFilter::IsPluginAvailable(
int render_process_id,
int render_frame_id,
const GURL& url,
bool is_main_frame,
const url::Origin& main_frame_origin,
content::WebPluginInfo* plugin,
chrome::mojom::PluginStatus* status) {
CEF_REQUIRE_UIT();
DCHECK_GT(render_process_id, 0);
if (*status == chrome::mojom::PluginStatus::kNotFound) {
// 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 = chrome::mojom::PluginStatus::kAllowed;
return true;
}
const GURL& policy_url = main_frame_origin.GetURL();
if (!policy_url.is_empty() &&
policy_url.scheme() == extensions::kExtensionScheme) {
// Always allow extension origins to load plugins.
// TODO(extensions): Revisit this decision once CEF supports more than just
// the PDF extension.
*status = chrome::mojom::PluginStatus::kAllowed;
return true;
}
auto browser_context = CefBrowserContext::GetForIDs(
Implement NetworkService request interception/handling (see issue #2622). Implementation notes: - Chromium change: CookieMonster::SetCookieableSchemes needs to be called immediately after the CookieMonster is created in NetworkContext:: ApplyContextParamsToBuilder. Add a Profile::GetCookieableSchemes method and NetworkContextParams.cookieable_schemes member (set from ProfileNetworkContextService::CreateNetworkContextParams) to support that. - Chromium change: Add a ContentBrowserClient::HandleExternalProtocol variant that exposes additional NetworkService request information. - GetResourceResponseFilter is not yet implemented. API changes: - Resource-related callbacks have been moved from CefRequestHandler to a new CefResourceRequestHandler interface which is returned via the GetResourceRequestHandler method. If the CefRequestHandler declines to handle a resource it can optionally be handled by the CefRequestContextHandler, if any, associated with the loading context. - The OnProtocolExecution callback has been moved from CefRequestHandler to CefResourceRequestHandler and will be called if a custom scheme request is unhandled. - Cookie send/save permission callbacks have been moved from CefRequestHandler and CefResourceHandler to CefResourceRequestHandler. - New methods added to CefResourceHandler that better match NetworkService execution sequence expectations. The old methods are now deprecated. - New methods added to CefRequest and CefResponse. Known behavior changes with the NetworkService implementation: - Modifying the |new_url| parameter in OnResourceRedirect will no longer result in the method being called an additional time (likely a bug in the old implementation). - Modifying the request URL in OnResourceResponse would previously cause a redirect. This behavior is now deprecated because the NetworkService does not support this functionality when using default network loaders. Temporary support has been added in combination with CefResourceHandler usage only. - Other changes to the request object in OnResourceResponse will now cause the request to be restarted. This means that OnBeforeResourceLoad, etc, will be called an additional time with the new request information. - CefResponse::GetMimeType will now be empty for non-200 responses. - Requests using custom schemes can now be handled via CefResourceRequestHandler with the same callback behavior as builtin schemes. - Redirects of custom scheme requests will now be followed as expected. - Default handling of builtin schemes can now be disabled by setting |disable_default_handling| to true in GetResourceRequestHandler. - Unhandled requests (custom scheme or builtin scheme with default handling disabled) will fail with an CefResponse::GetError value of ERR_UNKNOWN_URL_SCHEME. - The CefSchemeHandlerFactory::Create callback will now include cookie headers. To test: - Run `cefclient --enable-network-service`. All resources should load successfully (this tests the transparent proxy capability). - All tests pass with NetworkService disabled. - The following tests pass with NetworkService enabled: - CookieTest.* - FrameTest.* (excluding .*Nav) - NavigationTest.* (excluding .Redirect*) - RequestHandlerTest.* - RequestContextTest.Basic* - RequestContextTest.Popup* - RequestTest.* - ResourceManagerTest.* - ResourceRequestHandlerTest.* (excluding .Filter*) - SchemeHandlerTest.* - StreamResourceHandlerTest.*
2019-04-24 04:50:25 +02:00
render_process_id, render_frame_id, -1, false);
CefRefPtr<CefRequestContextHandler> handler;
if (browser_context) {
handler = browser_context->GetHandler(render_process_id, render_frame_id,
-1, false);
}
if (!handler) {
// No handler so go with the default plugin load decision.
return *status != chrome::mojom::PluginStatus::kDisabled;
}
// Check for a cached plugin load decision.
if (browser_context->HasPluginLoadDecision(render_process_id, plugin->path,
is_main_frame, main_frame_origin,
status)) {
return *status != chrome::mojom::PluginStatus::kDisabled;
}
CefRefPtr<CefWebPluginInfoImpl> pluginInfo(new CefWebPluginInfoImpl(*plugin));
cef_plugin_policy_t plugin_policy = PLUGIN_POLICY_ALLOW;
switch (*status) {
case chrome::mojom::PluginStatus::kAllowed:
plugin_policy = PLUGIN_POLICY_ALLOW;
break;
case chrome::mojom::PluginStatus::kBlocked:
case chrome::mojom::PluginStatus::kBlockedByPolicy:
case chrome::mojom::PluginStatus::kOutdatedBlocked:
case chrome::mojom::PluginStatus::kOutdatedDisallowed:
case chrome::mojom::PluginStatus::kUnauthorized:
plugin_policy = PLUGIN_POLICY_BLOCK;
break;
case chrome::mojom::PluginStatus::kDisabled:
plugin_policy = PLUGIN_POLICY_DISABLE;
break;
case chrome::mojom::PluginStatus::kPlayImportantContent:
plugin_policy = PLUGIN_POLICY_DETECT_IMPORTANT;
break;
default:
NOTREACHED();
break;
}
if (handler->OnBeforePluginLoad(plugin->mime_types[0].mime_type,
url.possibly_invalid_spec(), is_main_frame,
policy_url.possibly_invalid_spec(),
pluginInfo.get(), &plugin_policy)) {
switch (plugin_policy) {
case PLUGIN_POLICY_ALLOW:
*status = chrome::mojom::PluginStatus::kAllowed;
break;
case PLUGIN_POLICY_DETECT_IMPORTANT:
*status = chrome::mojom::PluginStatus::kPlayImportantContent;
break;
case PLUGIN_POLICY_BLOCK:
*status = chrome::mojom::PluginStatus::kBlocked;
break;
case PLUGIN_POLICY_DISABLE:
*status = chrome::mojom::PluginStatus::kDisabled;
break;
}
}
// Cache the plugin load decision.
browser_context->AddPluginLoadDecision(render_process_id, plugin->path,
is_main_frame, main_frame_origin,
*status);
return *status != chrome::mojom::PluginStatus::kDisabled;
}