2015-07-16 23:40:01 +02:00
|
|
|
// Copyright 2014 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/extensions/extensions_renderer_client.h"
|
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
#include "libcef/renderer/alloy/alloy_render_thread_observer.h"
|
2015-11-10 21:18:16 +01:00
|
|
|
#include "libcef/renderer/extensions/extensions_dispatcher_delegate.h"
|
2015-07-16 23:40:01 +02:00
|
|
|
|
2015-11-10 21:18:16 +01:00
|
|
|
#include "base/command_line.h"
|
2021-07-23 18:40:13 +02:00
|
|
|
#include "base/stl_util.h"
|
2015-11-10 21:18:16 +01:00
|
|
|
#include "chrome/common/url_constants.h"
|
2018-02-15 01:12:09 +01:00
|
|
|
#include "chrome/renderer/extensions/extension_process_policy.h"
|
2015-11-10 21:18:16 +01:00
|
|
|
#include "chrome/renderer/extensions/resource_request_policy.h"
|
2021-09-20 11:06:23 +02:00
|
|
|
#include "components/guest_view/renderer/guest_view_container_dispatcher.h"
|
2015-11-10 21:18:16 +01:00
|
|
|
#include "content/public/common/content_constants.h"
|
|
|
|
#include "content/public/common/content_switches.h"
|
|
|
|
#include "content/public/renderer/render_frame.h"
|
|
|
|
#include "content/public/renderer/render_thread.h"
|
|
|
|
#include "extensions/common/constants.h"
|
2018-04-19 17:44:42 +02:00
|
|
|
#include "extensions/common/permissions/permissions_data.h"
|
2015-11-10 21:18:16 +01:00
|
|
|
#include "extensions/common/switches.h"
|
|
|
|
#include "extensions/renderer/dispatcher.h"
|
|
|
|
#include "extensions/renderer/extension_frame_helper.h"
|
2017-09-06 23:40:58 +02:00
|
|
|
#include "extensions/renderer/extensions_render_frame_observer.h"
|
2015-11-10 21:18:16 +01:00
|
|
|
#include "extensions/renderer/renderer_extension_registry.h"
|
|
|
|
#include "extensions/renderer/script_context.h"
|
2018-04-19 17:44:42 +02:00
|
|
|
#include "third_party/blink/public/web/web_document.h"
|
|
|
|
#include "third_party/blink/public/web/web_local_frame.h"
|
|
|
|
#include "third_party/blink/public/web/web_plugin_params.h"
|
2015-11-10 21:18:16 +01:00
|
|
|
|
2015-07-16 23:40:01 +02:00
|
|
|
namespace extensions {
|
|
|
|
|
2015-11-10 21:18:16 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
void IsGuestViewApiAvailableToScriptContext(
|
|
|
|
bool* api_is_available,
|
|
|
|
extensions::ScriptContext* context) {
|
|
|
|
if (context->GetAvailability("guestViewInternal").is_available()) {
|
|
|
|
*api_is_available = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefExtensionsRendererClient::CefExtensionsRendererClient() {}
|
2015-07-16 23:40:01 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefExtensionsRendererClient::~CefExtensionsRendererClient() {}
|
2015-07-16 23:40:01 +02:00
|
|
|
|
|
|
|
bool CefExtensionsRendererClient::IsIncognitoProcess() const {
|
2020-09-25 03:40:47 +02:00
|
|
|
return AlloyRenderThreadObserver::is_incognito_process();
|
2015-07-16 23:40:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int CefExtensionsRendererClient::GetLowestIsolatedWorldId() const {
|
|
|
|
// CEF doesn't need to reserve world IDs for anything other than extensions,
|
|
|
|
// so we always return 1. Note that 0 is reserved for the global world.
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-07-27 01:19:27 +02:00
|
|
|
extensions::Dispatcher* CefExtensionsRendererClient::GetDispatcher() {
|
|
|
|
return extension_dispatcher_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefExtensionsRendererClient::OnExtensionLoaded(
|
|
|
|
const extensions::Extension& extension) {
|
|
|
|
resource_request_policy_->OnExtensionLoaded(extension);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefExtensionsRendererClient::OnExtensionUnloaded(
|
|
|
|
const extensions::ExtensionId& extension_id) {
|
|
|
|
resource_request_policy_->OnExtensionUnloaded(extension_id);
|
|
|
|
}
|
|
|
|
|
2019-09-04 17:13:32 +02:00
|
|
|
bool CefExtensionsRendererClient::ExtensionAPIEnabledForServiceWorkerScript(
|
|
|
|
const GURL& scope,
|
|
|
|
const GURL& script_url) const {
|
|
|
|
// TODO(extensions): Implement to support background sevice worker scripts
|
|
|
|
// in extensions
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-10 21:18:16 +01:00
|
|
|
void CefExtensionsRendererClient::RenderThreadStarted() {
|
|
|
|
content::RenderThread* thread = content::RenderThread::Get();
|
|
|
|
|
2021-09-20 11:06:23 +02:00
|
|
|
extension_dispatcher_ = std::make_unique<extensions::Dispatcher>(
|
|
|
|
std::make_unique<extensions::CefExtensionsDispatcherDelegate>());
|
2018-11-03 02:15:09 +01:00
|
|
|
extension_dispatcher_->OnRenderThreadStarted(thread);
|
2021-09-20 11:06:23 +02:00
|
|
|
resource_request_policy_ =
|
|
|
|
std::make_unique<extensions::ResourceRequestPolicy>(
|
|
|
|
extension_dispatcher_.get());
|
|
|
|
guest_view_container_dispatcher_ =
|
|
|
|
std::make_unique<guest_view::GuestViewContainerDispatcher>();
|
2015-11-10 21:18:16 +01:00
|
|
|
|
|
|
|
thread->AddObserver(extension_dispatcher_.get());
|
|
|
|
thread->AddObserver(guest_view_container_dispatcher_.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefExtensionsRendererClient::RenderFrameCreated(
|
2017-09-06 23:40:58 +02:00
|
|
|
content::RenderFrame* render_frame,
|
|
|
|
service_manager::BinderRegistry* registry) {
|
|
|
|
new extensions::ExtensionsRenderFrameObserver(render_frame, registry);
|
2015-11-10 21:18:16 +01:00
|
|
|
new extensions::ExtensionFrameHelper(render_frame,
|
|
|
|
extension_dispatcher_.get());
|
|
|
|
extension_dispatcher_->OnRenderFrameCreated(render_frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefExtensionsRendererClient::OverrideCreatePlugin(
|
|
|
|
content::RenderFrame* render_frame,
|
|
|
|
const blink::WebPluginParams& params) {
|
2017-04-20 21:28:17 +02:00
|
|
|
if (params.mime_type.Utf8() != content::kBrowserPluginMimeType)
|
2015-11-10 21:18:16 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
bool guest_view_api_available = false;
|
2019-03-13 22:27:37 +01:00
|
|
|
extension_dispatcher_->script_context_set_iterator()->ForEach(
|
2021-06-04 03:34:56 +02:00
|
|
|
render_frame, base::BindRepeating(&IsGuestViewApiAvailableToScriptContext,
|
|
|
|
&guest_view_api_available));
|
2015-11-10 21:18:16 +01:00
|
|
|
return !guest_view_api_available;
|
|
|
|
}
|
|
|
|
|
2018-04-19 17:44:42 +02:00
|
|
|
void CefExtensionsRendererClient::WillSendRequest(
|
2017-01-23 18:36:54 +01:00
|
|
|
blink::WebLocalFrame* frame,
|
2015-11-10 21:18:16 +01:00
|
|
|
ui::PageTransition transition_type,
|
2016-11-23 21:54:29 +01:00
|
|
|
const blink::WebURL& url,
|
2020-03-04 01:29:39 +01:00
|
|
|
const net::SiteForCookies& site_for_cookies,
|
2018-04-19 17:44:42 +02:00
|
|
|
const url::Origin* initiator_origin,
|
2021-01-28 00:13:12 +01:00
|
|
|
GURL* new_url) {
|
2015-11-10 21:18:16 +01:00
|
|
|
// Check whether the request should be allowed. If not allowed, we reset the
|
|
|
|
// URL to something invalid to prevent the request and cause an error.
|
2017-04-20 21:28:17 +02:00
|
|
|
if (url.ProtocolIs(extensions::kExtensionScheme) &&
|
2021-01-28 00:13:12 +01:00
|
|
|
!resource_request_policy_->CanRequestResource(
|
|
|
|
GURL(url), frame, transition_type,
|
|
|
|
base::OptionalFromPtr(initiator_origin))) {
|
2015-11-10 21:18:16 +01:00
|
|
|
*new_url = GURL(chrome::kExtensionInvalidRequestURL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-27 22:38:52 +02:00
|
|
|
void CefExtensionsRendererClient::RunScriptsAtDocumentStart(
|
|
|
|
content::RenderFrame* render_frame) {
|
|
|
|
extension_dispatcher_->RunScriptsAtDocumentStart(render_frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefExtensionsRendererClient::RunScriptsAtDocumentEnd(
|
|
|
|
content::RenderFrame* render_frame) {
|
|
|
|
extension_dispatcher_->RunScriptsAtDocumentEnd(render_frame);
|
|
|
|
}
|
|
|
|
|
2017-04-20 21:28:17 +02:00
|
|
|
void CefExtensionsRendererClient::RunScriptsAtDocumentIdle(
|
|
|
|
content::RenderFrame* render_frame) {
|
|
|
|
extension_dispatcher_->RunScriptsAtDocumentIdle(render_frame);
|
|
|
|
}
|
|
|
|
|
2017-09-06 23:40:58 +02:00
|
|
|
// static
|
|
|
|
bool CefExtensionsRendererClient::IsStandaloneExtensionProcess() {
|
|
|
|
return base::CommandLine::ForCurrentProcess()->HasSwitch(
|
|
|
|
extensions::switches::kExtensionProcess);
|
|
|
|
}
|
|
|
|
|
2015-07-16 23:40:01 +02:00
|
|
|
} // namespace extensions
|