Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execute js initialization jobs during html page initialization #430

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/main/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@
import com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine;
import com.gargoylesoftware.htmlunit.javascript.PostponedAction;
import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable;
import com.gargoylesoftware.htmlunit.javascript.TimeoutError;
import com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob;
import com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManager;
import com.gargoylesoftware.htmlunit.javascript.host.Window;
import com.gargoylesoftware.htmlunit.javascript.host.css.CSS2Properties;
import com.gargoylesoftware.htmlunit.javascript.host.event.BeforeUnloadEvent;
Expand Down Expand Up @@ -343,6 +346,7 @@ && hasFeature(EVENT_FOCUS_ON_LOAD)) {
catch (final Exception e) {
throw new RuntimeException(e);
}
executeInitializationJobs(enclosingWindow);
executeRefreshIfNeeded();
}

Expand Down Expand Up @@ -2908,4 +2912,36 @@ private void readObject(final ObjectInputStream in) throws IOException, ClassNot
computedStyles_ = new WeakHashMap<>();
}
}

/**
* Executes js jobs usually needed to initialize the page.
* @param webWindow The enclosing window of the page.
*/
private void executeInitializationJobs(WebWindow webWindow) {
if (!getWebClient().isJavaScriptEnabled()) {
return;
}
final JavaScriptJobManager jobManager = webWindow.getJobManager();
if (jobManager != null) {
int jobsDone = 0;
JavaScriptJob earliestJob = jobManager.getEarliestJob();
try {
while(earliestJob != null && (earliestJob.isExecuteAsap() || earliestJob.getTargetExecutionTime() <= System.currentTimeMillis())) {
jobManager.runSingleJob(earliestJob);
jobsDone++;
if(jobsDone > 1000) {
LOG.warn("Too many init jobs executed for window " + webWindow + ". Return for now.");
break;
}
earliestJob = jobManager.getEarliestJob();
}
} catch(TimeoutError e) {
getWebClient().getJavaScriptErrorListener().timeoutError(this, e.getAllowedTime(), e.getExecutionTime());
if (getWebClient().getOptions().isThrowExceptionOnScriptError()) {
throw new RuntimeException(e);
}
LOG.info("Caught script timeout error", e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ public class TimeoutError extends Error {
* Returns the allowed time.
* @return the allowed time
*/
long getAllowedTime() {
public long getAllowedTime() {
return allowedTime_;
}

/**
* Returns the execution time.
* @return the execution time
*/
long getExecutionTime() {
public long getExecutionTime() {
return executionTime_;
}
}