Skip to content

Commit eb9188e

Browse files
magomezspenap
authored andcommitted
Add setting to allow closing windows from scripts
1 parent 1a64310 commit eb9188e

4 files changed

Lines changed: 76 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: 54 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,8 @@ 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));
422425
default:
423426
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
424427
break;
@@ -633,6 +636,8 @@ ALLOW_DEPRECATED_DECLARATIONS_BEGIN
633636
case PROP_WEBRTC_UDP_PORTS_RANGE:
634637
g_value_set_string(value, webkit_settings_get_webrtc_udp_ports_range(settings));
635638
break;
639+
case PROP_ALLOW_SCRIPTS_TO_CLOSE_WINDOWS:
640+
g_value_set_boolean(value, webkit_settings_get_allow_scripts_to_close_windows(settings));
636641
default:
637642
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
638643
break;
@@ -1715,6 +1720,19 @@ static void webkit_settings_class_init(WebKitSettingsClass* klass)
17151720
nullptr, // A null string forces the default value.
17161721
readWriteConstructParamFlags);
17171722

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

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

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)