Skip to content

Commit 8e73d61

Browse files
committed
Add setting to allow closing windows from scripts
1 parent b0f274c commit 8e73d61

4 files changed

Lines changed: 78 additions & 1 deletion

File tree

Source/WTF/Scripts/Preferences/UnifiedWebPreferences.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,20 @@ AllowRunningOfInsecureContent:
295295
WebCore:
296296
default: false
297297

298+
AllowScriptsToCloseWindows:
299+
type: bool
300+
status: embedder
301+
humanReadableName: "Allow scripts to close windows"
302+
humanReadableDescription: "Allow scripts to close windows"
303+
condition: PLATFORM(WPE)
304+
defaultValue:
305+
WebKitLegacy:
306+
default: false
307+
WebKit:
308+
default: false
309+
WebCore:
310+
default: false
311+
298312
AllowTestOnlyIPC:
299313
type: bool
300314
status: embedder

Source/WebCore/page/DOMWindow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ void DOMWindow::close()
113113
if (!frame->isMainFrame())
114114
return;
115115

116-
if (!(page->openedByDOM() || page->checkedBackForward()->count() <= 1)) {
116+
if (!(page->openedByDOM() || page->checkedBackForward()->count() <= 1 || frame->settings().allowScriptsToCloseWindows())) {
117117
checkedConsole()->addMessage(MessageSource::JS, MessageLevel::Warning, "Can't close the window since it was not opened by JavaScript"_s);
118118
return;
119119
}

Source/WebKit/UIProcess/API/glib/WebKitSettings.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ enum {
189189
PROP_ENABLE_WEBRTC,
190190
PROP_DISABLE_WEB_SECURITY,
191191
PROP_WEBRTC_UDP_PORTS_RANGE,
192+
PROP_ALLOW_SCRIPTS_TO_CLOSE_WINDOWS,
192193
N_PROPERTIES,
193194
};
194195

@@ -419,6 +420,9 @@ ALLOW_DEPRECATED_DECLARATIONS_BEGIN
419420
case PROP_WEBRTC_UDP_PORTS_RANGE:
420421
webkit_settings_set_webrtc_udp_ports_range(settings, g_value_get_string(value));
421422
break;
423+
case PROP_ALLOW_SCRIPTS_TO_CLOSE_WINDOWS:
424+
webkit_settings_set_allow_scripts_to_close_windows(settings, g_value_get_boolean(value));
425+
break;
422426
default:
423427
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
424428
break;
@@ -633,6 +637,9 @@ ALLOW_DEPRECATED_DECLARATIONS_BEGIN
633637
case PROP_WEBRTC_UDP_PORTS_RANGE:
634638
g_value_set_string(value, webkit_settings_get_webrtc_udp_ports_range(settings));
635639
break;
640+
case PROP_ALLOW_SCRIPTS_TO_CLOSE_WINDOWS:
641+
g_value_set_boolean(value, webkit_settings_get_allow_scripts_to_close_windows(settings));
642+
break;
636643
default:
637644
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
638645
break;
@@ -1715,6 +1722,19 @@ static void webkit_settings_class_init(WebKitSettingsClass* klass)
17151722
nullptr, // A null string forces the default value.
17161723
readWriteConstructParamFlags);
17171724

1725+
/**
1726+
* WebKitSettings:allow-scripts-to-close-windows:
1727+
*
1728+
* Allow scripts to close windows they didn't open.
1729+
*
1730+
*/
1731+
sObjProperties[PROP_ALLOW_SCRIPTS_TO_CLOSE_WINDOWS] = g_param_spec_boolean(
1732+
"allow-scripts-to-close-windows",
1733+
_("Allow scripts to close windows"),
1734+
_("Whether scripts can close windows they didn't open."),
1735+
FALSE,
1736+
readWriteConstructParamFlags);
1737+
17181738
g_object_class_install_properties(gObjectClass, N_PROPERTIES, sObjProperties.data());
17191739
}
17201740

@@ -4438,3 +4458,39 @@ webkit_settings_set_webrtc_udp_ports_range(WebKitSettings* settings, const gchar
44384458
UNUSED_PARAM(udpPortsRange);
44394459
#endif
44404460
}
4461+
4462+
/**
4463+
* webkit_settings_get_allow_scripts_to_close_windows:
4464+
* @settings: a #WebKitSettings
4465+
*
4466+
* Get the #WebKitSettings:allow-scripts-to-close-windows property.
4467+
*
4468+
* Returns: %TRUE If script can close windows not opened by them or %FALSE otherwise.
4469+
*/
4470+
gboolean webkit_settings_get_allow_scripts_to_close_windows (WebKitSettings *settings)
4471+
{
4472+
g_return_val_if_fail(WEBKIT_IS_SETTINGS(settings), FALSE);
4473+
4474+
return settings->priv->preferences->allowScriptsToCloseWindows();
4475+
}
4476+
4477+
/**
4478+
* webkit_settings_set_allow_scripts_to_close_windows
4479+
* @settings: a #WebKitSettings
4480+
* @allowed: Value to be set
4481+
*
4482+
* Set the #WebKitSettings:allow-scripts-to-close-windows property.
4483+
*/
4484+
WEBKIT_API void
4485+
webkit_settings_set_allow_scripts_to_close_windows(WebKitSettings *settings, gboolean allowed)
4486+
{
4487+
g_return_if_fail(WEBKIT_IS_SETTINGS(settings));
4488+
4489+
WebKitSettingsPrivate* priv = settings->priv;
4490+
bool currentValue = priv->preferences->allowScriptsToCloseWindows();
4491+
if (currentValue == allowed)
4492+
return;
4493+
4494+
priv->preferences->setAllowScriptsToCloseWindows(allowed);
4495+
g_object_notify(G_OBJECT(settings), "allow-scripts-to-close-windows");
4496+
}

Source/WebKit/UIProcess/API/glib/WebKitSettings.h.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,13 @@ WEBKIT_API void
582582
webkit_settings_set_webrtc_udp_ports_range (WebKitSettings *settings,
583583
const gchar *udp_port_range);
584584

585+
WEBKIT_API gboolean
586+
webkit_settings_get_allow_scripts_to_close_windows (WebKitSettings *settings);
587+
588+
WEBKIT_API void
589+
webkit_settings_set_allow_scripts_to_close_windows (WebKitSettings *settings,
590+
gboolean allowed);
591+
585592
G_END_DECLS
586593

587594
#endif /* WebKitSettings_h */

0 commit comments

Comments
 (0)