Skip to content

Commit 7508775

Browse files
magomezpgorszkowski-igalia
authored andcommitted
Add a setting to allow closing windows from scripts
1 parent e846875 commit 7508775

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
@@ -291,6 +291,20 @@ AllowRunningOfInsecureContent:
291291
WebCore:
292292
default: false
293293

294+
AllowScriptsToCloseWindows:
295+
type: bool
296+
status: embedder
297+
humanReadableName: "Allow scripts to close windows"
298+
humanReadableDescription: "Allow scripts to close windows"
299+
condition: PLATFORM(WPE)
300+
defaultValue:
301+
WebKitLegacy:
302+
default: false
303+
WebKit:
304+
default: false
305+
WebCore:
306+
default: false
307+
294308
AllowSettingAnyXHRHeaderFromFileURLs:
295309
type: bool
296310
status: embedder

Source/WebCore/page/DOMWindow.cpp

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

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

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ enum {
174174
PROP_MEDIA_CONTENT_TYPES_REQUIRING_HARDWARE_SUPPORT,
175175
PROP_ENABLE_WEBRTC,
176176
PROP_DISABLE_WEB_SECURITY,
177+
PROP_ALLOW_SCRIPTS_TO_CLOSE_WINDOWS,
177178
N_PROPERTIES,
178179
};
179180

@@ -409,6 +410,9 @@ ALLOW_DEPRECATED_DECLARATIONS_BEGIN
409410
case PROP_DISABLE_WEB_SECURITY:
410411
webkit_settings_set_disable_web_security(settings, g_value_get_boolean(value));
411412
break;
413+
case PROP_ALLOW_SCRIPTS_TO_CLOSE_WINDOWS:
414+
webkit_settings_set_allow_scripts_to_close_windows(settings, g_value_get_boolean(value));
415+
break;
412416
default:
413417
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
414418
break;
@@ -617,6 +621,9 @@ ALLOW_DEPRECATED_DECLARATIONS_BEGIN
617621
case PROP_DISABLE_WEB_SECURITY:
618622
g_value_set_boolean(value, webkit_settings_get_disable_web_security(settings));
619623
break;
624+
case PROP_ALLOW_SCRIPTS_TO_CLOSE_WINDOWS:
625+
g_value_set_boolean(value, webkit_settings_get_allow_scripts_to_close_windows(settings));
626+
break;
620627
default:
621628
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
622629
break;
@@ -1618,6 +1625,19 @@ static void webkit_settings_class_init(WebKitSettingsClass* klass)
16181625
FALSE,
16191626
readWriteConstructParamFlags);
16201627

1628+
/**
1629+
* WebKitSettings:allow-scripts-to-close-windows:
1630+
*
1631+
* Allow scripts to close windows they didn't open.
1632+
*
1633+
*/
1634+
sObjProperties[PROP_ALLOW_SCRIPTS_TO_CLOSE_WINDOWS] = g_param_spec_boolean(
1635+
"allow-scripts-to-close-windows",
1636+
_("Allow scripts to close windows"),
1637+
_("Whether scripts can close windows they didn't open."),
1638+
FALSE,
1639+
readWriteConstructParamFlags);
1640+
16211641
g_object_class_install_properties(gObjectClass, N_PROPERTIES, sObjProperties);
16221642
}
16231643

@@ -4131,3 +4151,39 @@ WebKitFeatureList* webkit_settings_get_development_features(void)
41314151
{
41324152
return webkitFeatureListCreate(WebPreferences::internalDebugFeatures());
41334153
}
4154+
4155+
/**
4156+
* webkit_settings_get_allow_scripts_to_close_windows:
4157+
* @settings: a #WebKitSettings
4158+
*
4159+
* Get the #WebKitSettings:allow-scripts-to-close-windows property.
4160+
*
4161+
* Returns: %TRUE If script can close windows not opened by them or %FALSE otherwise.
4162+
*/
4163+
gboolean webkit_settings_get_allow_scripts_to_close_windows (WebKitSettings *settings)
4164+
{
4165+
g_return_val_if_fail(WEBKIT_IS_SETTINGS(settings), FALSE);
4166+
4167+
return settings->priv->preferences->allowScriptsToCloseWindows();
4168+
}
4169+
4170+
/**
4171+
* webkit_settings_set_allow_scripts_to_close_windows
4172+
* @settings: a #WebKitSettings
4173+
* @allowed: Value to be set
4174+
*
4175+
* Set the #WebKitSettings:allow-scripts-to-close-windows property.
4176+
*/
4177+
WEBKIT_API void
4178+
webkit_settings_set_allow_scripts_to_close_windows(WebKitSettings *settings, gboolean allowed)
4179+
{
4180+
g_return_if_fail(WEBKIT_IS_SETTINGS(settings));
4181+
4182+
WebKitSettingsPrivate* priv = settings->priv;
4183+
bool currentValue = priv->preferences->allowScriptsToCloseWindows();
4184+
if (currentValue == allowed)
4185+
return;
4186+
4187+
priv->preferences->setAllowScriptsToCloseWindows(allowed);
4188+
g_object_notify(G_OBJECT(settings), "allow-scripts-to-close-windows");
4189+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,13 @@ webkit_settings_get_experimental_features (void);
562562
WEBKIT_API WebKitFeatureList *
563563
webkit_settings_get_development_features (void);
564564

565+
WEBKIT_API gboolean
566+
webkit_settings_get_allow_scripts_to_close_windows (WebKitSettings *settings);
567+
568+
WEBKIT_API void
569+
webkit_settings_set_allow_scripts_to_close_windows (WebKitSettings *settings,
570+
gboolean allowed);
571+
565572
G_END_DECLS
566573

567574
#endif /* WebKitSettings_h */

0 commit comments

Comments
 (0)