Skip to content

Commit 96cfadb

Browse files
jakub-gocol-redspenap
authored andcommitted
Add configuration option to allow moving window to background when window.close is called
When allow_scripts_to_close_windows property is set to true, scripts can close windows that are not opened by them. But after window.close API is called, page is in "is closing" and browser expects window to be closed. When integrator wants to hide browser window/suspend browser, instead of closing it, page will be stuck in "is closing" state, where some APIs don't work anymore. This change introduces new property allow_move_to_suspend_on_window_close (disabled by default) which removes setting "is closing" state and just sends notification to browser integration, without any expectations on how it's handled.
1 parent 52fd1d8 commit 96cfadb

File tree

4 files changed

+87
-9
lines changed

4 files changed

+87
-9
lines changed

Source/WTF/Scripts/Preferences/UnifiedWebPreferences.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,20 @@ AllowMediaContentTypesRequiringHardwareSupportAsFallback:
258258
WebCore:
259259
default: false
260260

261+
AllowMoveToSuspendOnWindowClose:
262+
type: bool
263+
status: embedder
264+
humanReadableName: "Allow move to suspend on window.close()"
265+
humanReadableDescription: "Allow to suspend browser instead of closing window on window.close()"
266+
condition: PLATFORM(WPE)
267+
defaultValue:
268+
WebKitLegacy:
269+
default: false
270+
WebKit:
271+
default: false
272+
WebCore:
273+
default: false
274+
261275
AllowMultiElementImplicitSubmission:
262276
type: bool
263277
status: embedder

Source/WebCore/page/DOMWindow.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,20 @@ void DOMWindow::close()
113113
if (!frame->isMainFrame())
114114
return;
115115

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

121-
RefPtr localFrame = dynamicDowncast<LocalFrame>(frame);
122-
if (localFrame && !localFrame->protectedLoader()->shouldClose())
123-
return;
122+
RefPtr localFrame = dynamicDowncast<LocalFrame>(frame);
123+
if (localFrame && !localFrame->protectedLoader()->shouldClose())
124+
return;
124125

125-
ResourceLoadObserver::shared().updateCentralStatisticsStore([] { });
126+
ResourceLoadObserver::shared().updateCentralStatisticsStore([] { });
126127

127-
page->setIsClosing();
128+
page->setIsClosing();
129+
}
128130
closePage();
129131
}
130132

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ enum {
190190
PROP_DISABLE_WEB_SECURITY,
191191
PROP_WEBRTC_UDP_PORTS_RANGE,
192192
PROP_ALLOW_SCRIPTS_TO_CLOSE_WINDOWS,
193+
PROP_ALLOW_MOVE_TO_SUSPEND_ON_WINDOW_CLOSE,
193194
PROP_ENABLE_DIRECTORY_UPLOAD,
194195
PROP_ALLOW_RUNNING_OF_INSECURE_CONTENT,
195196
PROP_ALLOW_DISPLAY_OF_INSECURE_CONTENT,
@@ -426,6 +427,9 @@ ALLOW_DEPRECATED_DECLARATIONS_BEGIN
426427
case PROP_ALLOW_SCRIPTS_TO_CLOSE_WINDOWS:
427428
webkit_settings_set_allow_scripts_to_close_windows(settings, g_value_get_boolean(value));
428429
break;
430+
case PROP_ALLOW_MOVE_TO_SUSPEND_ON_WINDOW_CLOSE:
431+
webkit_settings_set_allow_move_to_suspend_on_window_close(settings, g_value_get_boolean(value));
432+
break;
429433
case PROP_ENABLE_DIRECTORY_UPLOAD:
430434
webkit_settings_set_enable_directory_upload(settings, g_value_get_boolean(value));
431435
break;
@@ -652,6 +656,9 @@ ALLOW_DEPRECATED_DECLARATIONS_BEGIN
652656
case PROP_ALLOW_SCRIPTS_TO_CLOSE_WINDOWS:
653657
g_value_set_boolean(value, webkit_settings_get_allow_scripts_to_close_windows(settings));
654658
break;
659+
case PROP_ALLOW_MOVE_TO_SUSPEND_ON_WINDOW_CLOSE:
660+
g_value_set_boolean(value, webkit_settings_get_allow_move_to_suspend_on_window_close(settings));
661+
break;
655662
case PROP_ENABLE_DIRECTORY_UPLOAD:
656663
g_value_set_boolean(value, webkit_settings_get_enable_directory_upload(settings));
657664
break;
@@ -1756,6 +1763,19 @@ static void webkit_settings_class_init(WebKitSettingsClass* klass)
17561763
FALSE,
17571764
readWriteConstructParamFlags);
17581765

1766+
/**
1767+
* WebKitSettings:allow-move-to-suspend-on-window-close:
1768+
*
1769+
* Allow browser to move to suspend on window close.
1770+
*
1771+
*/
1772+
sObjProperties[PROP_ALLOW_MOVE_TO_SUSPEND_ON_WINDOW_CLOSE] = g_param_spec_boolean(
1773+
"allow-move-to-suspend-on-window-close",
1774+
_("Allow move to suspend on window.close()"),
1775+
_("Allow to suspend browser instead of closing window on window.close()"),
1776+
FALSE,
1777+
readWriteConstructParamFlags);
1778+
17591779
/**
17601780
* WebKitSettings:enable-directory-upload:
17611781
*
@@ -4553,6 +4573,42 @@ webkit_settings_set_allow_scripts_to_close_windows(WebKitSettings *settings, gbo
45534573
g_object_notify(G_OBJECT(settings), "allow-scripts-to-close-windows");
45544574
}
45554575

4576+
/**
4577+
* webkit_settings_get_allow_move_to_suspend_on_window_close:
4578+
* @settings: a #WebKitSettings
4579+
*
4580+
* Get the #WebKitSettings:allow-move-to-suspend-on-window-close property.
4581+
*
4582+
* Returns: %TRUE If browser can be suspended on window close.
4583+
*/
4584+
gboolean webkit_settings_get_allow_move_to_suspend_on_window_close (WebKitSettings *settings)
4585+
{
4586+
g_return_val_if_fail(WEBKIT_IS_SETTINGS(settings), FALSE);
4587+
4588+
return settings->priv->preferences->allowMoveToSuspendOnWindowClose();
4589+
}
4590+
4591+
/**
4592+
* webkit_settings_set_allow_move_to_suspend_on_window_close
4593+
* @settings: a #WebKitSettings
4594+
* @allowed: Value to be set
4595+
*
4596+
* Set the #WebKitSettings:allow-move-to-suspend-on-window-close property.
4597+
*/
4598+
WEBKIT_API void
4599+
webkit_settings_set_allow_move_to_suspend_on_window_close(WebKitSettings *settings, gboolean allowed)
4600+
{
4601+
g_return_if_fail(WEBKIT_IS_SETTINGS(settings));
4602+
4603+
WebKitSettingsPrivate* priv = settings->priv;
4604+
bool currentValue = priv->preferences->allowMoveToSuspendOnWindowClose();
4605+
if (currentValue == allowed)
4606+
return;
4607+
4608+
priv->preferences->setAllowMoveToSuspendOnWindowClose(allowed);
4609+
g_object_notify(G_OBJECT(settings), "allow-move-to-suspend-on-window-close");
4610+
}
4611+
45564612
/**
45574613
* webkit_settings_get_enable_directory_upload:
45584614
* @settings: a #WebKitSettings

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,12 @@ webkit_settings_get_allow_scripts_to_close_windows (WebKitSettings
602602
WEBKIT_API void
603603
webkit_settings_set_allow_scripts_to_close_windows (WebKitSettings *settings,
604604
gboolean allowed);
605+
WEBKIT_API gboolean
606+
webkit_settings_get_allow_move_to_suspend_on_window_close (WebKitSettings *settings);
607+
608+
WEBKIT_API void
609+
webkit_settings_set_allow_move_to_suspend_on_window_close (WebKitSettings *settings,
610+
gboolean allowed);
605611

606612
WEBKIT_API gboolean
607613
webkit_settings_get_enable_directory_upload (WebKitSettings *settings);

0 commit comments

Comments
 (0)