feat: add webContents.setWindowOpenHandler API (#24517)

Co-authored-by: Jeremy Rose <jeremya@chromium.org>
This commit is contained in:
loc 2020-11-10 09:06:03 -08:00 committed by GitHub
parent 6b222a2d8a
commit 0b85fdf26c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
56 changed files with 2087 additions and 885 deletions

View file

@ -20,6 +20,7 @@
#include "shell/common/api/api.mojom.h"
#include "shell/common/gin_converters/blink_converter.h"
#include "shell/common/gin_converters/callback_converter.h"
#include "shell/common/gin_converters/file_path_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/error_thrower.h"
#include "shell/common/gin_helper/promise.h"
@ -29,6 +30,7 @@
#include "shell/renderer/electron_renderer_client.h"
#include "third_party/blink/public/common/page/page_zoom.h"
#include "third_party/blink/public/common/web_cache/web_cache_resource_type_stats.h"
#include "third_party/blink/public/common/web_preferences/web_preferences.h"
#include "third_party/blink/public/platform/web_cache.h"
#include "third_party/blink/public/platform/web_isolated_world_info.h"
#include "third_party/blink/public/web/web_custom_element.h"
@ -395,6 +397,65 @@ double GetZoomFactor(gin_helper::ErrorThrower thrower,
return blink::PageZoomLevelToZoomFactor(zoom_level);
}
v8::Local<v8::Value> GetWebPreference(v8::Isolate* isolate,
v8::Local<v8::Value> window,
std::string pref_name) {
content::RenderFrame* render_frame = GetRenderFrame(window);
const auto& prefs = render_frame->GetBlinkPreferences();
if (pref_name == options::kPreloadScripts) {
return gin::ConvertToV8(isolate, prefs.preloads);
} else if (pref_name == options::kDisableElectronSiteInstanceOverrides) {
return gin::ConvertToV8(isolate,
prefs.disable_electron_site_instance_overrides);
} else if (pref_name == options::kBackgroundColor) {
return gin::ConvertToV8(isolate, prefs.background_color);
} else if (pref_name == options::kOpenerID) {
// NOTE: openerId is internal-only.
return gin::ConvertToV8(isolate, prefs.opener_id);
} else if (pref_name == options::kContextIsolation) {
return gin::ConvertToV8(isolate, prefs.context_isolation);
#if BUILDFLAG(ENABLE_REMOTE_MODULE)
} else if (pref_name == options::kEnableRemoteModule) {
return gin::ConvertToV8(isolate, prefs.enable_remote_module);
#endif
} else if (pref_name == options::kWorldSafeExecuteJavaScript) {
return gin::ConvertToV8(isolate, prefs.world_safe_execute_javascript);
} else if (pref_name == options::kGuestInstanceID) {
// NOTE: guestInstanceId is internal-only.
return gin::ConvertToV8(isolate, prefs.guest_instance_id);
} else if (pref_name == options::kHiddenPage) {
// NOTE: hiddenPage is internal-only.
return gin::ConvertToV8(isolate, prefs.hidden_page);
} else if (pref_name == options::kOffscreen) {
return gin::ConvertToV8(isolate, prefs.offscreen);
} else if (pref_name == options::kPreloadScript) {
return gin::ConvertToV8(isolate, prefs.preload.value());
} else if (pref_name == options::kNativeWindowOpen) {
return gin::ConvertToV8(isolate, prefs.native_window_open);
} else if (pref_name == options::kNodeIntegration) {
return gin::ConvertToV8(isolate, prefs.node_integration);
} else if (pref_name == options::kNodeIntegrationInWorker) {
return gin::ConvertToV8(isolate, prefs.node_integration_in_worker);
} else if (pref_name == options::kEnableNodeLeakageInRenderers) {
// NOTE: enableNodeLeakageInRenderers is internal-only.
return gin::ConvertToV8(isolate, prefs.node_leakage_in_renderers);
} else if (pref_name == options::kNodeIntegrationInSubFrames) {
return gin::ConvertToV8(isolate, true);
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
} else if (pref_name == options::kSpellcheck) {
return gin::ConvertToV8(isolate, prefs.enable_spellcheck);
#endif
} else if (pref_name == options::kPlugins) {
return gin::ConvertToV8(isolate, prefs.enable_plugins);
} else if (pref_name == options::kEnableWebSQL) {
return gin::ConvertToV8(isolate, prefs.enable_websql);
} else if (pref_name == options::kWebviewTag) {
return gin::ConvertToV8(isolate, prefs.webview_tag);
}
return v8::Null(isolate);
}
void SetVisualZoomLevelLimits(gin_helper::ErrorThrower thrower,
v8::Local<v8::Value> window,
double min_level,
@ -574,13 +635,13 @@ v8::Local<v8::Promise> ExecuteJavaScript(gin_helper::Arguments* args,
ScriptExecutionCallback::CompletionCallback completion_callback;
args->GetNext(&completion_callback);
bool world_safe_exec_js = base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kWorldSafeExecuteJavaScript);
auto& prefs = render_frame->GetBlinkPreferences();
render_frame->GetWebFrame()->RequestExecuteScriptAndReturnValue(
blink::WebScriptSource(blink::WebString::FromUTF16(code)),
has_user_gesture,
new ScriptExecutionCallback(std::move(promise), world_safe_exec_js,
new ScriptExecutionCallback(std::move(promise),
prefs.world_safe_execute_javascript,
std::move(completion_callback)));
return handle;
@ -640,8 +701,7 @@ v8::Local<v8::Promise> ExecuteJavaScriptInIsolatedWorld(
blink::WebURL(GURL(url)), start_line));
}
bool world_safe_exec_js = base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kWorldSafeExecuteJavaScript);
auto& prefs = render_frame->GetBlinkPreferences();
// Debugging tip: if you see a crash stack trace beginning from this call,
// then it is very likely that some exception happened when executing the
@ -649,7 +709,8 @@ v8::Local<v8::Promise> ExecuteJavaScriptInIsolatedWorld(
render_frame->GetWebFrame()->RequestExecuteScriptInIsolatedWorld(
world_id, &sources.front(), sources.size(), has_user_gesture,
scriptExecutionType,
new ScriptExecutionCallback(std::move(promise), world_safe_exec_js,
new ScriptExecutionCallback(std::move(promise),
prefs.world_safe_execute_javascript,
std::move(completion_callback)));
return handle;
@ -852,6 +913,7 @@ void Initialize(v8::Local<v8::Object> exports,
dict.SetMethod("allowGuestViewElementDefinition",
&AllowGuestViewElementDefinition);
dict.SetMethod("getWebFrameId", &GetWebFrameId);
dict.SetMethod("getWebPreference", &GetWebPreference);
dict.SetMethod("setSpellCheckProvider", &SetSpellCheckProvider);
dict.SetMethod("insertText", &InsertText);
dict.SetMethod("insertCSS", &InsertCSS);

View file

@ -7,6 +7,7 @@
#include "base/command_line.h"
#include "content/public/renderer/render_frame.h"
#include "shell/common/options_switches.h"
#include "third_party/blink/public/common/web_preferences/web_preferences.h"
#include "third_party/blink/public/platform/url_conversion.h"
#include "third_party/blink/public/platform/web_security_origin.h"
#include "third_party/blink/public/web/web_local_frame.h"
@ -23,8 +24,10 @@ ContentSettingsObserver::~ContentSettingsObserver() = default;
bool ContentSettingsObserver::AllowStorageAccessSync(StorageType storage_type) {
if (storage_type == StorageType::kDatabase &&
!base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnableWebSQL)) {
// Command line support is still relevant for extensions.
!(base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kEnableWebSQL) ||
render_frame()->GetBlinkPreferences().enable_websql)) {
return false;
}

View file

@ -21,6 +21,7 @@
#include "services/service_manager/public/cpp/interface_provider.h"
#include "shell/common/options_switches.h"
#include "shell/common/world_ids.h"
#include "third_party/blink/public/common/web_preferences/web_preferences.h"
#include "third_party/blink/public/platform/web_isolated_world_info.h"
#include "third_party/blink/public/web/blink.h"
#include "third_party/blink/public/web/web_document.h"
@ -64,21 +65,18 @@ void ElectronRenderFrameObserver::DidInstallConditionalFeatures(
if (ShouldNotifyClient(world_id))
renderer_client_->DidCreateScriptContext(context, render_frame_);
auto* command_line = base::CommandLine::ForCurrentProcess();
bool use_context_isolation = renderer_client_->isolated_world();
auto prefs = render_frame_->GetBlinkPreferences();
bool use_context_isolation = prefs.context_isolation;
// This logic matches the EXPLAINED logic in electron_renderer_client.cc
// to avoid explaining it twice go check that implementation in
// DidCreateScriptContext();
bool is_main_world = IsMainWorld(world_id);
bool is_main_frame = render_frame_->IsMainFrame();
bool reuse_renderer_processes_enabled =
command_line->HasSwitch(switches::kDisableElectronSiteInstanceOverrides);
bool is_not_opened =
!render_frame_->GetWebFrame()->Opener() ||
command_line->HasSwitch(switches::kEnableNodeLeakageInRenderers);
bool allow_node_in_sub_frames =
command_line->HasSwitch(switches::kNodeIntegrationInSubFrames);
prefs.disable_electron_site_instance_overrides;
bool is_not_opened = !render_frame_->GetWebFrame()->Opener() ||
prefs.node_leakage_in_renderers;
bool allow_node_in_sub_frames = prefs.node_integration_in_sub_frames;
bool should_create_isolated_context =
use_context_isolation && is_main_world &&
(is_main_frame || allow_node_in_sub_frames) &&
@ -163,10 +161,9 @@ bool ElectronRenderFrameObserver::IsIsolatedWorld(int world_id) {
}
bool ElectronRenderFrameObserver::ShouldNotifyClient(int world_id) {
bool allow_node_in_sub_frames =
base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kNodeIntegrationInSubFrames);
if (renderer_client_->isolated_world() &&
auto prefs = render_frame_->GetBlinkPreferences();
bool allow_node_in_sub_frames = prefs.node_integration_in_sub_frames;
if (prefs.context_isolation &&
(render_frame_->IsMainFrame() || allow_node_in_sub_frames))
return IsIsolatedWorld(world_id);
else

View file

@ -20,6 +20,7 @@
#include "shell/common/options_switches.h"
#include "shell/renderer/electron_render_frame_observer.h"
#include "shell/renderer/web_worker_observer.h"
#include "third_party/blink/public/common/web_preferences/web_preferences.h"
#include "third_party/blink/public/web/web_document.h"
#include "third_party/blink/public/web/web_local_frame.h"
@ -91,17 +92,15 @@ void ElectronRendererClient::DidCreateScriptContext(
// TODO(zcbenz): Do not create Node environment if node integration is not
// enabled.
auto* command_line = base::CommandLine::ForCurrentProcess();
// Only load node if we are a main frame or a devtools extension
// unless node support has been explicitly enabled for sub frames
auto prefs = render_frame->GetBlinkPreferences();
bool reuse_renderer_processes_enabled =
command_line->HasSwitch(switches::kDisableElectronSiteInstanceOverrides);
prefs.disable_electron_site_instance_overrides;
// Consider the window not "opened" if it does not have an Opener, or if a
// user has manually opted in to leaking node in the renderer
bool is_not_opened =
!render_frame->GetWebFrame()->Opener() ||
command_line->HasSwitch(switches::kEnableNodeLeakageInRenderers);
!render_frame->GetWebFrame()->Opener() || prefs.node_leakage_in_renderers;
// Consider this the main frame if it is both a Main Frame and it wasn't
// opened. We allow an opened main frame to have node if renderer process
// reuse is enabled as that will correctly free node environments prevent a
@ -109,8 +108,7 @@ void ElectronRendererClient::DidCreateScriptContext(
bool is_main_frame = render_frame->IsMainFrame() &&
(is_not_opened || reuse_renderer_processes_enabled);
bool is_devtools = IsDevToolsExtension(render_frame);
bool allow_node_in_subframes =
command_line->HasSwitch(switches::kNodeIntegrationInSubFrames);
bool allow_node_in_subframes = prefs.node_integration_in_sub_frames;
bool should_load_node =
(is_main_frame || is_devtools || allow_node_in_subframes) &&
!IsWebViewFrame(renderer_context, render_frame);
@ -186,10 +184,9 @@ void ElectronRendererClient::WillReleaseScriptContext(
// for existing users.
// We also do this if we have disable electron site instance overrides to
// avoid memory leaks
auto* command_line = base::CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(switches::kNodeIntegrationInSubFrames) ||
command_line->HasSwitch(
switches::kDisableElectronSiteInstanceOverrides)) {
auto prefs = render_frame->GetBlinkPreferences();
if (prefs.node_integration_in_sub_frames ||
prefs.disable_electron_site_instance_overrides) {
node::FreeEnvironment(env);
if (env == node_bindings_->uv_env())
node::FreeIsolateData(node_bindings_->isolate_data());
@ -213,6 +210,8 @@ bool ElectronRendererClient::ShouldFork(blink::WebLocalFrame* frame,
void ElectronRendererClient::WorkerScriptReadyForEvaluationOnWorkerThread(
v8::Local<v8::Context> context) {
// TODO(loc): Note that this will not be correct for in-process child windows
// with webPreferences that have a different value for nodeIntegrationInWorker
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kNodeIntegrationInWorker)) {
WebWorkerObserver::GetCurrent()->WorkerScriptReadyForEvaluation(context);
@ -221,6 +220,8 @@ void ElectronRendererClient::WorkerScriptReadyForEvaluationOnWorkerThread(
void ElectronRendererClient::WillDestroyWorkerContextOnWorkerThread(
v8::Local<v8::Context> context) {
// TODO(loc): Note that this will not be correct for in-process child windows
// with webPreferences that have a different value for nodeIntegrationInWorker
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kNodeIntegrationInWorker)) {
WebWorkerObserver::GetCurrent()->ContextWillDestroy(context);
@ -230,8 +231,9 @@ void ElectronRendererClient::WillDestroyWorkerContextOnWorkerThread(
void ElectronRendererClient::SetupMainWorldOverrides(
v8::Handle<v8::Context> context,
content::RenderFrame* render_frame) {
auto prefs = render_frame->GetBlinkPreferences();
// We only need to run the isolated bundle if webview is enabled
if (!base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kWebviewTag))
if (!prefs.webview_tag)
return;
// Setup window overrides in the main world context
// Wrap the bundle into a function that receives the isolatedWorld as

View file

@ -19,6 +19,7 @@
#include "shell/common/node_util.h"
#include "shell/common/options_switches.h"
#include "shell/renderer/electron_render_frame_observer.h"
#include "third_party/blink/public/common/web_preferences/web_preferences.h"
#include "third_party/blink/public/web/blink.h"
#include "third_party/blink/public/web/web_document.h"
#include "third_party/electron_node/src/node_binding.h"
@ -198,8 +199,7 @@ void ElectronSandboxedRendererClient::DidCreateScriptContext(
bool is_devtools =
IsDevTools(render_frame) || IsDevToolsExtension(render_frame);
bool allow_node_in_sub_frames =
base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kNodeIntegrationInSubFrames);
render_frame->GetBlinkPreferences().node_integration_in_sub_frames;
bool should_load_preload =
(is_main_frame || is_devtools || allow_node_in_sub_frames) &&
!IsWebViewFrame(context, render_frame);
@ -232,8 +232,9 @@ void ElectronSandboxedRendererClient::DidCreateScriptContext(
void ElectronSandboxedRendererClient::SetupMainWorldOverrides(
v8::Handle<v8::Context> context,
content::RenderFrame* render_frame) {
auto prefs = render_frame->GetBlinkPreferences();
// We only need to run the isolated bundle if webview is enabled
if (!base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kWebviewTag))
if (!prefs.webview_tag)
return;
// Setup window overrides in the main world context

View file

@ -31,6 +31,7 @@
#include "shell/renderer/electron_api_service_impl.h"
#include "shell/renderer/electron_autofill_agent.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_registry.h"
#include "third_party/blink/public/common/web_preferences/web_preferences.h"
#include "third_party/blink/public/web/blink.h"
#include "third_party/blink/public/web/web_custom_element.h" // NOLINT(build/include_alpha)
#include "third_party/blink/public/web/web_frame_widget.h"
@ -112,8 +113,6 @@ RendererClientBase::RendererClientBase() {
ParseSchemesCLISwitch(command_line, switches::kStreamingSchemes);
for (const std::string& scheme : streaming_schemes_list)
media::AddStreamingScheme(scheme.c_str());
isolated_world_ = base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kContextIsolation);
// We rely on the unique process host id which is notified to the
// renderer process via command line switch from the content layer,
// if this switch is removed from the content layer for some reason,
@ -135,9 +134,8 @@ void RendererClientBase::DidCreateScriptContext(
global.SetHidden("contextId", context_id);
#if BUILDFLAG(ENABLE_REMOTE_MODULE)
auto* command_line = base::CommandLine::ForCurrentProcess();
bool enableRemoteModule =
command_line->HasSwitch(switches::kEnableRemoteModule);
render_frame->GetBlinkPreferences().enable_remote_module;
global.SetHidden("enableRemoteModule", enableRemoteModule);
#endif
}
@ -153,6 +151,8 @@ void RendererClientBase::RenderThreadStarted() {
// On macOS, popup menus are rendered by the main process by default.
// This causes problems in OSR, since when the popup is rendered separately,
// it won't be captured in the rendered image.
// TODO(loc): This will be wrong for in-process child windows, as this
// function won't run again for them.
if (command_line->HasSwitch(options::kOffscreen)) {
blink::WebView::SetUseExternalPopupMenus(false);
}
@ -177,8 +177,7 @@ void RendererClientBase::RenderThreadStarted() {
#endif
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
if (command_line->HasSwitch(switches::kEnableSpellcheck))
spellcheck_ = std::make_unique<SpellCheck>(this);
spellcheck_ = std::make_unique<SpellCheck>(this);
#endif
blink::WebCustomElement::AddEmbedderCustomElementName("webview");
@ -274,11 +273,11 @@ void RendererClientBase::RenderFrameCreated(
if (render_frame->IsMainFrame() && render_view) {
blink::WebView* webview = render_view->GetWebView();
if (webview) {
base::CommandLine* cmd = base::CommandLine::ForCurrentProcess();
if (cmd->HasSwitch(switches::kGuestInstanceID)) { // webview.
auto prefs = render_frame->GetBlinkPreferences();
if (prefs.guest_instance_id) { // webview.
webview->SetBaseBackgroundColor(SK_ColorTRANSPARENT);
} else { // normal window.
std::string name = cmd->GetSwitchValueASCII(switches::kBackgroundColor);
std::string name = prefs.background_color;
SkColor color =
name.empty() ? SK_ColorTRANSPARENT : ParseHexColor(name);
webview->SetBaseBackgroundColor(color);
@ -300,8 +299,7 @@ void RendererClientBase::RenderFrameCreated(
#endif
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
auto* command_line = base::CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(switches::kEnableSpellcheck))
if (render_frame->GetBlinkPreferences().enable_spellcheck)
new SpellCheckProvider(render_frame, spellcheck_.get(), this);
#endif
}
@ -330,12 +328,11 @@ bool RendererClientBase::OverrideCreatePlugin(
content::RenderFrame* render_frame,
const blink::WebPluginParams& params,
blink::WebPlugin** plugin) {
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
if (params.mime_type.Utf8() == content::kBrowserPluginMimeType ||
#if BUILDFLAG(ENABLE_PDF_VIEWER)
params.mime_type.Utf8() == kPdfPluginMimeType ||
#endif // BUILDFLAG(ENABLE_PDF_VIEWER)
command_line->HasSwitch(switches::kEnablePlugins))
render_frame->GetBlinkPreferences().enable_plugins)
return false;
*plugin = nullptr;
@ -444,7 +441,9 @@ void RendererClientBase::RunScriptsAtDocumentEnd(
v8::Local<v8::Context> RendererClientBase::GetContext(
blink::WebLocalFrame* frame,
v8::Isolate* isolate) const {
if (isolated_world())
auto* render_frame = content::RenderFrame::FromWebFrame(frame);
DCHECK(render_frame);
if (render_frame && render_frame->GetBlinkPreferences().context_isolation)
return frame->WorldScriptContext(isolate, WorldIDs::ISOLATED_WORLD_ID);
else
return frame->MainWorldScriptContext();

View file

@ -79,7 +79,6 @@ class RendererClientBase : public content::ContentRendererClient
std::unique_ptr<blink::WebPrescientNetworking> CreatePrescientNetworking(
content::RenderFrame* render_frame) override;
bool isolated_world() const { return isolated_world_; }
// Get the context that the Electron API is running in.
v8::Local<v8::Context> GetContext(blink::WebLocalFrame* frame,
@ -143,7 +142,6 @@ class RendererClientBase : public content::ContentRendererClient
#if defined(WIDEVINE_CDM_AVAILABLE)
ChromeKeySystemsProvider key_systems_provider_;
#endif
bool isolated_world_;
std::string renderer_client_id_;
// An increasing ID used for identifying an V8 context in this process.
int64_t next_context_id_ = 0;