Skip to content

Commit

Permalink
fix: make preload calculation identical between sandbox & non-sandbox…
Browse files Browse the repository at this point in the history
…ed (#34585)

* fix: make preload calculation identical between sandbox & non-sandboxed

* fix test

* split IPCs

* fix ipc constant

* fix unneeded async function

* remove unused preloads blink preferences

* fix typings

Co-authored-by: Jeremy Rose <japthorp@slack-corp.com>
  • Loading branch information
trop[bot] and nornagon committed Jun 20, 2022
1 parent 10062c9 commit ff7f2a5
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 59 deletions.
4 changes: 4 additions & 0 deletions lib/browser/rpc-server.ts
Expand Up @@ -68,6 +68,10 @@ ipcMainUtils.handleSync(IPC_MESSAGES.BROWSER_SANDBOX_LOAD, async function (event
};
});

ipcMainUtils.handleSync(IPC_MESSAGES.BROWSER_NONSANDBOX_LOAD, function (event) {
return { preloadPaths: event.sender._getPreloadPaths() };
});

ipcMainInternal.on(IPC_MESSAGES.BROWSER_PRELOAD_ERROR, function (event, preloadPath: string, error: Error) {
event.sender.emit('preload-error', event, preloadPath, error);
});
1 change: 1 addition & 0 deletions lib/common/ipc-messages.ts
Expand Up @@ -3,6 +3,7 @@ export const enum IPC_MESSAGES {
BROWSER_GET_LAST_WEB_PREFERENCES = 'BROWSER_GET_LAST_WEB_PREFERENCES',
BROWSER_PRELOAD_ERROR = 'BROWSER_PRELOAD_ERROR',
BROWSER_SANDBOX_LOAD = 'BROWSER_SANDBOX_LOAD',
BROWSER_NONSANDBOX_LOAD = 'BROWSER_NONSANDBOX_LOAD',
BROWSER_WINDOW_CLOSE = 'BROWSER_WINDOW_CLOSE',
BROWSER_GET_PROCESS_MEMORY_INFO = 'BROWSER_GET_PROCESS_MEMORY_INFO',

Expand Down
12 changes: 4 additions & 8 deletions lib/renderer/init.ts
Expand Up @@ -2,6 +2,7 @@ import * as path from 'path';
import { IPC_MESSAGES } from '@electron/internal/common/ipc-messages';

import type * as ipcRendererInternalModule from '@electron/internal/renderer/ipc-renderer-internal';
import type * as ipcRendererUtilsModule from '@electron/internal/renderer/ipc-renderer-internal-utils';

const Module = require('module');

Expand Down Expand Up @@ -38,6 +39,7 @@ require('../common/reset-search-paths');
require('@electron/internal/common/init');

const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal') as typeof ipcRendererInternalModule;
const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils') as typeof ipcRendererUtilsModule;

process.getProcessMemoryInfo = () => {
return ipcRendererInternal.invoke<Electron.ProcessMemoryInfo>(IPC_MESSAGES.BROWSER_GET_PROCESS_MEMORY_INFO);
Expand All @@ -48,15 +50,8 @@ const { hasSwitch, getSwitchValue } = process._linkedBinding('electron_common_co
const { mainFrame } = process._linkedBinding('electron_renderer_web_frame');

const nodeIntegration = mainFrame.getWebPreference('nodeIntegration');
const preloadScript = mainFrame.getWebPreference('preload');
const preloadScripts = mainFrame.getWebPreference('preloadScripts');
const appPath = hasSwitch('app-path') ? getSwitchValue('app-path') : null;

// The webContents preload script is loaded after the session preload scripts.
if (preloadScript) {
preloadScripts.push(preloadScript);
}

// Common renderer initialization
require('@electron/internal/renderer/common-init');

Expand Down Expand Up @@ -127,8 +122,9 @@ if (nodeIntegration) {
}
}

const { preloadPaths } = ipcRendererUtils.invokeSync(IPC_MESSAGES.BROWSER_NONSANDBOX_LOAD);
// Load the preload scripts.
for (const preloadScript of preloadScripts) {
for (const preloadScript of preloadPaths) {
try {
Module._load(preloadScript);
} catch (error) {
Expand Down
Expand Up @@ -8,10 +8,10 @@ WebPreferences of in-process child windows, rather than relying on
process-level command line switches, as before.

diff --git a/third_party/blink/common/web_preferences/web_preferences.cc b/third_party/blink/common/web_preferences/web_preferences.cc
index 6d51f6a77c21b60ab756dbf8d4961b351a2e2b07..c6dc3cebfb32489a5e50b850ed02a066bf3f258e 100644
index 6d51f6a77c21b60ab756dbf8d4961b351a2e2b07..26b820b3ce387e01900cec4227ff290113a833c3 100644
--- a/third_party/blink/common/web_preferences/web_preferences.cc
+++ b/third_party/blink/common/web_preferences/web_preferences.cc
@@ -142,6 +142,20 @@ WebPreferences::WebPreferences()
@@ -142,6 +142,19 @@ WebPreferences::WebPreferences()
fake_no_alloc_direct_call_for_testing_enabled(false),
v8_cache_options(blink::mojom::V8CacheOptions::kDefault),
record_whole_document(false),
Expand All @@ -20,7 +20,6 @@ index 6d51f6a77c21b60ab756dbf8d4961b351a2e2b07..c6dc3cebfb32489a5e50b850ed02a066
+ is_webview(false),
+ hidden_page(false),
+ offscreen(false),
+ preload(base::FilePath::StringType()),
+ node_integration(false),
+ node_integration_in_worker(false),
+ node_integration_in_sub_frames(false),
Expand All @@ -33,21 +32,10 @@ index 6d51f6a77c21b60ab756dbf8d4961b351a2e2b07..c6dc3cebfb32489a5e50b850ed02a066
accelerated_video_decode_enabled(false),
animation_policy(
diff --git a/third_party/blink/common/web_preferences/web_preferences_mojom_traits.cc b/third_party/blink/common/web_preferences/web_preferences_mojom_traits.cc
index 187fd8d9818693262256d5cbddd5e02659ba903d..f166517f4e70ced310253539fb6197f6b3af559c 100644
index 187fd8d9818693262256d5cbddd5e02659ba903d..28e5178361e03d1ac851fa74214931b2332dd9c2 100644
--- a/third_party/blink/common/web_preferences/web_preferences_mojom_traits.cc
+++ b/third_party/blink/common/web_preferences/web_preferences_mojom_traits.cc
@@ -23,6 +23,10 @@ bool StructTraits<blink::mojom::WebPreferencesDataView,
!data.ReadCursiveFontFamilyMap(&out->cursive_font_family_map) ||
!data.ReadFantasyFontFamilyMap(&out->fantasy_font_family_map) ||
!data.ReadMathFontFamilyMap(&out->math_font_family_map) ||
+ // Begin Electron-specific WebPreferences.
+ !data.ReadPreloads(&out->preloads) ||
+ !data.ReadPreload(&out->preload) ||
+ // End Electron-specific WebPreferences.
!data.ReadLazyFrameLoadingDistanceThresholdsPx(
&out->lazy_frame_loading_distance_thresholds_px) ||
!data.ReadLazyImageLoadingDistanceThresholdsPx(
@@ -148,6 +152,19 @@ bool StructTraits<blink::mojom::WebPreferencesDataView,
@@ -148,6 +148,19 @@ bool StructTraits<blink::mojom::WebPreferencesDataView,
data.fake_no_alloc_direct_call_for_testing_enabled();
out->v8_cache_options = data.v8_cache_options();
out->record_whole_document = data.record_whole_document();
Expand All @@ -68,7 +56,7 @@ index 187fd8d9818693262256d5cbddd5e02659ba903d..f166517f4e70ced310253539fb6197f6
out->accelerated_video_decode_enabled =
data.accelerated_video_decode_enabled();
diff --git a/third_party/blink/public/common/web_preferences/web_preferences.h b/third_party/blink/public/common/web_preferences/web_preferences.h
index 59947bfd3c042e5f7d3993967fece9b519f93472..368ec7fb398409cd6386269934eaffcce356793f 100644
index 59947bfd3c042e5f7d3993967fece9b519f93472..cb2a53f6147767394585ed371744d8a140aace71 100644
--- a/third_party/blink/public/common/web_preferences/web_preferences.h
+++ b/third_party/blink/public/common/web_preferences/web_preferences.h
@@ -10,6 +10,7 @@
Expand All @@ -79,17 +67,15 @@ index 59947bfd3c042e5f7d3993967fece9b519f93472..368ec7fb398409cd6386269934eaffcc
#include "net/nqe/effective_connection_type.h"
#include "third_party/blink/public/common/common_export.h"
#include "third_party/blink/public/mojom/css/preferred_color_scheme.mojom-shared.h"
@@ -154,6 +155,22 @@ struct BLINK_COMMON_EXPORT WebPreferences {
@@ -154,6 +155,20 @@ struct BLINK_COMMON_EXPORT WebPreferences {
blink::mojom::V8CacheOptions v8_cache_options;
bool record_whole_document;

+ // Begin Electron-specific WebPreferences.
+ std::vector<base::FilePath> preloads;
+ bool context_isolation;
+ bool is_webview;
+ bool hidden_page;
+ bool offscreen;
+ base::FilePath preload;
+ bool node_integration;
+ bool node_integration_in_worker;
+ bool node_integration_in_sub_frames;
Expand All @@ -103,7 +89,7 @@ index 59947bfd3c042e5f7d3993967fece9b519f93472..368ec7fb398409cd6386269934eaffcc
// only controls whether or not the "document.cookie" field is properly
// connected to the backing store, for instance if you wanted to be able to
diff --git a/third_party/blink/public/common/web_preferences/web_preferences_mojom_traits.h b/third_party/blink/public/common/web_preferences/web_preferences_mojom_traits.h
index bd425a5869477de9095c6a41c683609d065b08c0..3a5d7450aa40e8a7614e83f316d3d0fb59583381 100644
index bd425a5869477de9095c6a41c683609d065b08c0..c7ba3ff52c9de01028c9e2be214e20cd91cbf309 100644
--- a/third_party/blink/public/common/web_preferences/web_preferences_mojom_traits.h
+++ b/third_party/blink/public/common/web_preferences/web_preferences_mojom_traits.h
@@ -6,6 +6,7 @@
Expand All @@ -114,15 +100,11 @@ index bd425a5869477de9095c6a41c683609d065b08c0..3a5d7450aa40e8a7614e83f316d3d0fb
#include "mojo/public/cpp/bindings/struct_traits.h"
#include "net/nqe/effective_connection_type.h"
#include "third_party/blink/public/common/common_export.h"
@@ -428,6 +429,60 @@ struct BLINK_COMMON_EXPORT StructTraits<blink::mojom::WebPreferencesDataView,
@@ -428,6 +429,52 @@ struct BLINK_COMMON_EXPORT StructTraits<blink::mojom::WebPreferencesDataView,
return r.record_whole_document;
}

+ // Begin Electron-specific WebPreferences.
+ static const std::vector<base::FilePath>& preloads(const blink::web_pref::WebPreferences& r) {
+ return r.preloads;
+ }
+
+ static bool context_isolation(const blink::web_pref::WebPreferences& r) {
+ return r.context_isolation;
+ }
Expand All @@ -139,10 +121,6 @@ index bd425a5869477de9095c6a41c683609d065b08c0..3a5d7450aa40e8a7614e83f316d3d0fb
+ return r.offscreen;
+ }
+
+ static const base::FilePath& preload(const blink::web_pref::WebPreferences& r) {
+ return r.preload;
+ }
+
+ static bool node_integration(const blink::web_pref::WebPreferences& r) {
+ return r.node_integration;
+ }
Expand Down Expand Up @@ -176,7 +154,7 @@ index bd425a5869477de9095c6a41c683609d065b08c0..3a5d7450aa40e8a7614e83f316d3d0fb
return r.cookie_enabled;
}
diff --git a/third_party/blink/public/mojom/webpreferences/web_preferences.mojom b/third_party/blink/public/mojom/webpreferences/web_preferences.mojom
index 738ac646e075f7a37ba2a263c7799a755e63d3fb..80d6ae17d34ef5c7fd14e21c4e1ee3ed4ef9c5fe 100644
index 738ac646e075f7a37ba2a263c7799a755e63d3fb..40ab2a4b9a1f9de5f201502adebc166a48fccd35 100644
--- a/third_party/blink/public/mojom/webpreferences/web_preferences.mojom
+++ b/third_party/blink/public/mojom/webpreferences/web_preferences.mojom
@@ -10,6 +10,7 @@ import "third_party/blink/public/mojom/v8_cache_options.mojom";
Expand All @@ -187,17 +165,15 @@ index 738ac646e075f7a37ba2a263c7799a755e63d3fb..80d6ae17d34ef5c7fd14e21c4e1ee3ed

enum PointerType {
kPointerNone = 1, // 1 << 0
@@ -206,6 +207,22 @@ struct WebPreferences {
@@ -206,6 +207,20 @@ struct WebPreferences {
V8CacheOptions v8_cache_options;
bool record_whole_document;

+ // Begin Electron-specific WebPreferences.
+ array<mojo_base.mojom.FilePath> preloads;
+ bool context_isolation;
+ bool is_webview;
+ bool hidden_page;
+ bool offscreen;
+ mojo_base.mojom.FilePath preload;
+ bool node_integration;
+ bool node_integration_in_worker;
+ bool node_integration_in_sub_frames;
Expand Down
5 changes: 0 additions & 5 deletions shell/browser/electron_browser_client.cc
Expand Up @@ -497,11 +497,6 @@ void ElectronBrowserClient::OverrideWebkitPrefs(
? blink::mojom::PreferredColorScheme::kDark
: blink::mojom::PreferredColorScheme::kLight;

auto preloads =
SessionPreferences::GetValidPreloads(web_contents->GetBrowserContext());
if (!preloads.empty())
prefs->preloads = preloads;

SetFontDefaults(prefs);

// Custom preferences of guest page.
Expand Down
4 changes: 0 additions & 4 deletions shell/browser/web_contents_preferences.cc
Expand Up @@ -486,10 +486,6 @@ void WebContentsPreferences::OverrideWebkitPrefs(

prefs->offscreen = offscreen_;

// The preload script.
if (preload_path_)
prefs->preload = *preload_path_;

prefs->node_integration = node_integration_;
prefs->node_integration_in_worker = node_integration_in_worker_;
prefs->node_integration_in_sub_frames = node_integration_in_sub_frames_;
Expand Down
6 changes: 1 addition & 5 deletions shell/renderer/api/electron_api_web_frame.cc
Expand Up @@ -496,9 +496,7 @@ class WebFrameRenderer : public gin::Wrappable<WebFrameRenderer>,

const auto& prefs = render_frame->GetBlinkPreferences();

if (pref_name == options::kPreloadScripts) {
return gin::ConvertToV8(isolate, prefs.preloads);
} else if (pref_name == "isWebView") {
if (pref_name == "isWebView") {
// FIXME(zcbenz): For child windows opened with window.open('') from
// webview, the WebPreferences is inherited from webview and the value
// of |is_webview| is wrong.
Expand All @@ -513,8 +511,6 @@ class WebFrameRenderer : public gin::Wrappable<WebFrameRenderer>,
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::kNodeIntegration) {
return gin::ConvertToV8(isolate, prefs.node_integration);
} else if (pref_name == options::kNodeIntegrationInWorker) {
Expand Down
10 changes: 9 additions & 1 deletion spec-main/api-browser-window-spec.ts
Expand Up @@ -3263,7 +3263,15 @@ describe('BrowserWindow module', () => {
});
w.webContents.setWindowOpenHandler(() => ({
action: 'allow',
overrideBrowserWindowOptions: { show: false, webPreferences: { contextIsolation: false, webviewTag: true, nodeIntegrationInSubFrames: true } }
overrideBrowserWindowOptions: {
show: false,
webPreferences: {
contextIsolation: false,
webviewTag: true,
nodeIntegrationInSubFrames: true,
preload
}
}
}));
w.webContents.once('new-window', (event, url, frameName, disposition, options) => {
options.show = false;
Expand Down
2 changes: 0 additions & 2 deletions typings/internal-ambient.d.ts
Expand Up @@ -108,8 +108,6 @@ declare namespace NodeJS {
isWebView: boolean;
hiddenPage: boolean;
nodeIntegration: boolean;
preload: string
preloadScripts: string[];
webviewTag: boolean;
}

Expand Down

0 comments on commit ff7f2a5

Please sign in to comment.