Skip to content

CefBrowser_N.doClose() unexpectedly closes the host window #364

Description

@magreenblatt

Original report by Anton Tarasov (Bitbucket: Anton Tarasov).


CefClient.doClose() javadoc says that:

If CEF created an OS window for the browser returning false will send an OS close
* notification to the browser window's top-level owner (e.g. WM_CLOSE on Windows, performClose:
* on OS-X and "delete_event" on Linux). If no OS window exists (window rendering disabled)
* returning false will cause the browser object to be destroyed immediately. Return true if the
* browser is parented to another window and that other window needs to receive close
* notification via some non-standard technique
.

‌

CefClient.doClose() may delegate to CefBrowser.doClose(). Here’s the impl from CefBrowser_N.doClose():

    @Override
    public synchronized boolean doClose() {
        if (closeAllowed_) {
            // Allow the close to proceed.
            return false;
        }

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                // Trigger close of the parent window.
                Component parent = SwingUtilities.getRoot(getUIComponent());
                if (parent != null) {
                    parent.dispatchEvent(
                            new WindowEvent((Window) parent, WindowEvent.WINDOW_CLOSING));
                }
            }
        });

        // Cancel the close.
        return true;
    }

‌

  1. On canceling the close the method dispatches WINDOW_CLOSING. This contradicts the spec. Moreover, closeAllowed_ is false by default. This notification may cause the app to actually start application exit steps (when the event is dispatched to the app’s main frame, I experience this on MS Windows).
  2. It’s not quite clear why JCEF needs to dispatch WINDOW_CLOSING even when closeAllowed_ is true. As the spec says, OS will deliver native closing message to the hosting window, that will make AWT generate all corresponding Java events.

‌

Activity

  1. magreenblatt commented on Jul 31, 2020

    @magreenblatt
    CollaboratorAuthor

    This was likely an attempt to match the logic flow of CefLifeSpanHandler::DoClose. Perhaps a different approach would be better. Suggestions (including demo PR) are welcome.

  2. KlemenDEV commented on Nov 3, 2025

    @KlemenDEV

    I can confirm this. Is there a way to prevent window closing currently as this is quite problematic in app with multiple cef tabs as closing single tab in OSR just closes the whole thing

  3. KlemenDEV commented on Nov 3, 2025

    @KlemenDEV

    It also seems that returning true for doClose does not close the browser in OSR mode

  4. KlemenDEV commented on Nov 3, 2025

    @KlemenDEV

    Seems calling setCloseAllowed on browser object works as a workaround as then the parent window is not closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugBug report

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions