Skip to content

Commit f4aae52

Browse files
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 14527e9 commit f4aae52

4 files changed

Lines changed: 87 additions & 9 deletions

File tree

Source/WTF/Scripts/Preferences/UnifiedWebPreferences.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,20 @@ AllowMediaContentTypesRequiringHardwareSupportAsFallback:
268268
WebCore:
269269
default: false
270270

271+
AllowMoveToSuspendOnWindowClose:
272+
type: bool
273+
status: embedder
274+
humanReadableName: "Allow move to suspend on window.close()"
275+
humanReadableDescription: "Allow to suspend browser instead of closing window on window.close()"
276+
condition: PLATFORM(WPE)
277+
defaultValue:
278+
WebKitLegacy:
279+
default: false
280+
WebKit:
281+
default: false
282+
WebCore:
283+
default: false
284+
271285
AllowMultiElementImplicitSubmission:
272286
type: bool
273287
status: embedder

Source/WebCore/page/DOMWindow.cpp

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

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

120-
RefPtr localFrame = dynamicDowncast<LocalFrame>(frame);
121-
if (localFrame && !localFrame->checkedLoader()->shouldClose())
122-
return;
121+
RefPtr localFrame = dynamicDowncast<LocalFrame>(frame);
122+
if (localFrame && !localFrame->checkedLoader()->shouldClose())
123+
return;
123124

124-
ResourceLoadObserver::shared().updateCentralStatisticsStore([] { });
125+
ResourceLoadObserver::shared().updateCentralStatisticsStore([] { });
125126

126-
page->setIsClosing();
127+
page->setIsClosing();
128+
}
127129
closePage();
128130
}
129131

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ enum {
177177
PROP_ALLOW_RUNNING_OF_INSECURE_CONTENT,
178178
PROP_ALLOW_DISPLAY_OF_INSECURE_CONTENT,
179179
PROP_ALLOW_SCRIPTS_TO_CLOSE_WINDOWS,
180+
PROP_ALLOW_MOVE_TO_SUSPEND_ON_WINDOW_CLOSE,
180181
PROP_ENABLE_DIRECTORY_UPLOAD,
181182
N_PROPERTIES,
182183
};
@@ -422,6 +423,9 @@ ALLOW_DEPRECATED_DECLARATIONS_BEGIN
422423
case PROP_ALLOW_SCRIPTS_TO_CLOSE_WINDOWS:
423424
webkit_settings_set_allow_scripts_to_close_windows(settings, g_value_get_boolean(value));
424425
break;
426+
case PROP_ALLOW_MOVE_TO_SUSPEND_ON_WINDOW_CLOSE:
427+
webkit_settings_set_allow_move_to_suspend_on_window_close(settings, g_value_get_boolean(value));
428+
break;
425429
case PROP_ENABLE_DIRECTORY_UPLOAD:
426430
webkit_settings_set_enable_directory_upload(settings, g_value_get_boolean(value));
427431
break;
@@ -642,6 +646,9 @@ ALLOW_DEPRECATED_DECLARATIONS_BEGIN
642646
case PROP_ALLOW_SCRIPTS_TO_CLOSE_WINDOWS:
643647
g_value_set_boolean(value, webkit_settings_get_allow_scripts_to_close_windows(settings));
644648
break;
649+
case PROP_ALLOW_MOVE_TO_SUSPEND_ON_WINDOW_CLOSE:
650+
g_value_set_boolean(value, webkit_settings_get_allow_move_to_suspend_on_window_close(settings));
651+
break;
645652
case PROP_ENABLE_DIRECTORY_UPLOAD:
646653
g_value_set_boolean(value, webkit_settings_get_enable_directory_upload(settings));
647654
break;
@@ -1683,6 +1690,19 @@ static void webkit_settings_class_init(WebKitSettingsClass* klass)
16831690
FALSE,
16841691
readWriteConstructParamFlags);
16851692

1693+
/**
1694+
* WebKitSettings:allow-move-to-suspend-on-window-close:
1695+
*
1696+
* Allow browser to move to suspend on window close.
1697+
*
1698+
*/
1699+
sObjProperties[PROP_ALLOW_MOVE_TO_SUSPEND_ON_WINDOW_CLOSE] = g_param_spec_boolean(
1700+
"allow-move-to-suspend-on-window-close",
1701+
_("Allow move to suspend on window.close()"),
1702+
_("Allow to suspend browser instead of closing window on window.close()"),
1703+
FALSE,
1704+
readWriteConstructParamFlags);
1705+
16861706
/**
16871707
* WebKitSettings:enable-directory-upload:
16881708
*
@@ -4316,6 +4336,42 @@ webkit_settings_set_allow_scripts_to_close_windows(WebKitSettings *settings, gbo
43164336
g_object_notify(G_OBJECT(settings), "allow-scripts-to-close-windows");
43174337
}
43184338

4339+
/**
4340+
* webkit_settings_get_allow_move_to_suspend_on_window_close:
4341+
* @settings: a #WebKitSettings
4342+
*
4343+
* Get the #WebKitSettings:allow-move-to-suspend-on-window-close property.
4344+
*
4345+
* Returns: %TRUE If browser can be suspended on window close.
4346+
*/
4347+
gboolean webkit_settings_get_allow_move_to_suspend_on_window_close (WebKitSettings *settings)
4348+
{
4349+
g_return_val_if_fail(WEBKIT_IS_SETTINGS(settings), FALSE);
4350+
4351+
return settings->priv->preferences->allowMoveToSuspendOnWindowClose();
4352+
}
4353+
4354+
/**
4355+
* webkit_settings_set_allow_move_to_suspend_on_window_close
4356+
* @settings: a #WebKitSettings
4357+
* @allowed: Value to be set
4358+
*
4359+
* Set the #WebKitSettings:allow-move-to-suspend-on-window-close property.
4360+
*/
4361+
WEBKIT_API void
4362+
webkit_settings_set_allow_move_to_suspend_on_window_close(WebKitSettings *settings, gboolean allowed)
4363+
{
4364+
g_return_if_fail(WEBKIT_IS_SETTINGS(settings));
4365+
4366+
WebKitSettingsPrivate* priv = settings->priv;
4367+
bool currentValue = priv->preferences->allowMoveToSuspendOnWindowClose();
4368+
if (currentValue == allowed)
4369+
return;
4370+
4371+
priv->preferences->setAllowMoveToSuspendOnWindowClose(allowed);
4372+
g_object_notify(G_OBJECT(settings), "allow-move-to-suspend-on-window-close");
4373+
}
4374+
43194375
/**
43204376
* webkit_settings_get_enable_directory_upload:
43214377
* @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
@@ -582,6 +582,12 @@ webkit_settings_get_allow_scripts_to_close_windows (WebKitSettings
582582
WEBKIT_API void
583583
webkit_settings_set_allow_scripts_to_close_windows (WebKitSettings *settings,
584584
gboolean allowed);
585+
WEBKIT_API gboolean
586+
webkit_settings_get_allow_move_to_suspend_on_window_close (WebKitSettings *settings);
587+
588+
WEBKIT_API void
589+
webkit_settings_set_allow_move_to_suspend_on_window_close (WebKitSettings *settings,
590+
gboolean allowed);
585591

586592
WEBKIT_API gboolean
587593
webkit_settings_get_enable_directory_upload (WebKitSettings *settings);

0 commit comments

Comments
 (0)