Skip to content

Commit

Permalink
refactor: move duplicate code to RendererClientBase::ShouldLoadPreloa…
Browse files Browse the repository at this point in the history
…d helper (#34520)

Co-authored-by: Milan Burda <milan.burda@gmail.com>
  • Loading branch information
trop[bot] and miniak committed Jun 13, 2022
1 parent e174aac commit 48f4ef6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 39 deletions.
19 changes: 1 addition & 18 deletions shell/renderer/electron_renderer_client.cc
Expand Up @@ -24,15 +24,6 @@

namespace electron {

namespace {

bool IsDevToolsExtension(content::RenderFrame* render_frame) {
return static_cast<GURL>(render_frame->GetWebFrame()->GetDocument().Url())
.SchemeIs("chrome-extension");
}

} // namespace

ElectronRendererClient::ElectronRendererClient()
: node_bindings_(
NodeBindings::Create(NodeBindings::BrowserEnvironment::kRenderer)),
Expand Down Expand Up @@ -77,15 +68,7 @@ void ElectronRendererClient::DidCreateScriptContext(

// Only load Node.js if we are a main frame or a devtools extension
// unless Node.js support has been explicitly enabled for subframes.
auto prefs = render_frame->GetBlinkPreferences();
bool is_main_frame = render_frame->IsMainFrame();
bool is_devtools = IsDevToolsExtension(render_frame);
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);
if (!should_load_node)
if (!ShouldLoadPreload(renderer_context, render_frame))
return;

injected_frames_.insert(render_frame);
Expand Down
22 changes: 1 addition & 21 deletions shell/renderer/electron_sandboxed_renderer_client.cc
Expand Up @@ -36,16 +36,6 @@ namespace {
const char kLifecycleKey[] = "lifecycle";
const char kModuleCacheKey[] = "native-module-cache";

bool IsDevTools(content::RenderFrame* render_frame) {
return render_frame->GetWebFrame()->GetDocument().Url().ProtocolIs(
"devtools");
}

bool IsDevToolsExtension(content::RenderFrame* render_frame) {
return render_frame->GetWebFrame()->GetDocument().Url().ProtocolIs(
"chrome-extension");
}

v8::Local<v8::Object> GetModuleCache(v8::Isolate* isolate) {
auto context = isolate->GetCurrentContext();
gin_helper::Dictionary global(isolate, context->Global());
Expand Down Expand Up @@ -207,17 +197,7 @@ void ElectronSandboxedRendererClient::DidCreateScriptContext(
// Only allow preload for the main frame or
// For devtools we still want to run the preload_bundle script
// Or when nodeSupport is explicitly enabled in sub frames
bool is_main_frame = render_frame->IsMainFrame();
bool is_devtools =
IsDevTools(render_frame) || IsDevToolsExtension(render_frame);

bool allow_node_in_sub_frames =
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);
if (!should_load_preload)
if (!ShouldLoadPreload(context, render_frame))
return;

injected_frames_.insert(render_frame);
Expand Down
23 changes: 23 additions & 0 deletions shell/renderer/renderer_client_base.cc
Expand Up @@ -134,6 +134,16 @@ class ChromePdfInternalPluginDelegate final
// static
RendererClientBase* g_renderer_client_base = nullptr;

bool IsDevTools(content::RenderFrame* render_frame) {
return render_frame->GetWebFrame()->GetDocument().Url().ProtocolIs(
"devtools");
}

bool IsDevToolsExtension(content::RenderFrame* render_frame) {
return render_frame->GetWebFrame()->GetDocument().Url().ProtocolIs(
"chrome-extension");
}

} // namespace

RendererClientBase::RendererClientBase() {
Expand Down Expand Up @@ -196,6 +206,19 @@ void RendererClientBase::BindProcess(v8::Isolate* isolate,
process->SetReadOnly("contextId", context_id);
}

bool RendererClientBase::ShouldLoadPreload(
v8::Handle<v8::Context> context,
content::RenderFrame* render_frame) const {
auto prefs = render_frame->GetBlinkPreferences();
bool is_main_frame = render_frame->IsMainFrame();
bool is_devtools =
IsDevTools(render_frame) || IsDevToolsExtension(render_frame);
bool allow_node_in_sub_frames = prefs.node_integration_in_sub_frames;

return (is_main_frame || is_devtools || allow_node_in_sub_frames) &&
!IsWebViewFrame(context, render_frame);
}

void RendererClientBase::RenderThreadStarted() {
auto* command_line = base::CommandLine::ForCurrentProcess();

Expand Down
3 changes: 3 additions & 0 deletions shell/renderer/renderer_client_base.h
Expand Up @@ -93,6 +93,9 @@ class RendererClientBase : public content::ContentRendererClient
gin_helper::Dictionary* process,
content::RenderFrame* render_frame);

bool ShouldLoadPreload(v8::Handle<v8::Context> context,
content::RenderFrame* render_frame) const;

// content::ContentRendererClient:
void RenderThreadStarted() override;
void ExposeInterfacesToBrowser(mojo::BinderMap* binders) override;
Expand Down

0 comments on commit 48f4ef6

Please sign in to comment.