Skip to content

Commit

Permalink
WebWindow handling is hard
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Oct 20, 2023
1 parent 8c45e12 commit 80bd6de
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/main/java/org/htmlunit/WebClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public WebClient(final BrowserVersion browserVersion, final boolean javaScriptEn
loadQueue_ = new ArrayList<>();

// The window must be constructed AFTER the script engine.
currentWindowTracker_ = new CurrentWindowTracker(this);
currentWindowTracker_ = new CurrentWindowTracker(this, true);
currentWindow_ = new TopLevelWindow("", this);

initMSXMLActiveX();
Expand Down Expand Up @@ -2188,9 +2188,11 @@ public void setCache(final Cache cache) {
*/
private static final class CurrentWindowTracker implements WebWindowListener, Serializable {
private final WebClient webClient_;
private final boolean ensureOneTopLevelWindow_;

CurrentWindowTracker(final WebClient webClient) {
CurrentWindowTracker(final WebClient webClient, final boolean ensureOneTopLevelWindow) {
webClient_ = webClient;
ensureOneTopLevelWindow_ = ensureOneTopLevelWindow;
}

/**
Expand Down Expand Up @@ -2225,6 +2227,10 @@ else if (window == webClient_.getCurrentWindow()) {
* Postprocessing to make sure we have always one top level window open.
*/
public void afterWebWindowClosedListenersProcessed(final WebWindowEvent event) {
if (!ensureOneTopLevelWindow_) {
return;
}

if (webClient_.topLevelWindows_.isEmpty()) {
// Must always have at least window, and there are no top-level windows left; must create one.
final TopLevelWindow newWindow = new TopLevelWindow("", webClient_);
Expand Down Expand Up @@ -2299,9 +2305,8 @@ public void close() {
scriptEngine_.prepareShutdown();
}

// stop the CurrentWindowTracker
// the CurrentWindowTracker makes sure there is still one window available
currentWindowTracker_ = null;
// stop the CurrentWindowTracker from making sure there is still one window available
currentWindowTracker_ = new CurrentWindowTracker(this, false);

// Hint: a new TopLevelWindow may be opened by some JS script while we are closing the others
// but the prepareShutdown() call will prevent the new window form getting js support
Expand All @@ -2312,7 +2317,6 @@ public void close() {

try {
topLevelWindow.close(true);
topLevelWindows_.remove(topLevelWindow);
}
catch (final Exception e) {
LOG.error("Exception while closing a TopLevelWindow", e);
Expand All @@ -2339,7 +2343,6 @@ else if (window instanceof DialogWindow) {

try {
topLevelWindow.close(true);
topLevelWindows_.remove(topLevelWindow);
}
catch (final Exception e) {
LOG.error("Exception while closing a TopLevelWindow", e);
Expand Down Expand Up @@ -2432,7 +2435,7 @@ public void reset() {
initMSXMLActiveX();

// The window must be constructed AFTER the script engine.
currentWindowTracker_ = new CurrentWindowTracker(this);
currentWindowTracker_ = new CurrentWindowTracker(this, true);
currentWindow_ = new TopLevelWindow("", this);
}

Expand Down

0 comments on commit 80bd6de

Please sign in to comment.