1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.background;
16
17 import java.lang.ref.WeakReference;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.htmlunit.Page;
22 import org.htmlunit.WebWindow;
23 import org.htmlunit.html.HtmlPage;
24
25
26
27
28
29
30
31
32 abstract class JavaScriptExecutionJob extends BasicJavaScriptJob {
33
34
35 private static final Log LOG = LogFactory.getLog(JavaScriptExecutionJob.class);
36
37
38 private final String label_;
39
40
41 private final WeakReference<WebWindow> window_;
42
43
44
45
46
47
48
49
50 JavaScriptExecutionJob(final int initialDelay, final Integer period, final String label,
51 final WebWindow window) {
52 super(initialDelay, period);
53 label_ = label;
54 window_ = new WeakReference<>(window);
55 }
56
57
58 @Override
59 public void run() {
60 final WebWindow w = window_.get();
61 if (w == null) {
62
63 return;
64 }
65
66 if (LOG.isDebugEnabled()) {
67 LOG.debug("Executing " + this + ".");
68 }
69
70 try {
71
72 if (w.isClosed()) {
73 LOG.debug("Enclosing window is now closed. Execution cancelled.");
74 return;
75 }
76 if (!w.getWebClient().containsWebWindow(w)) {
77 LOG.debug("Enclosing window is now closed. Execution cancelled.");
78 return;
79 }
80
81
82 final Page enclosedPage = w.getEnclosedPage();
83 if (enclosedPage == null || !enclosedPage.isHtmlPage()) {
84 if (enclosedPage == null) {
85 LOG.debug("The page that originated this job doesn't exist anymore. Execution cancelled.");
86 return;
87 }
88 if (LOG.isDebugEnabled()) {
89 LOG.debug("The page that originated this job is no html page ("
90 + enclosedPage.getClass().getName() + "). Execution cancelled.");
91 }
92 return;
93 }
94
95 runJavaScript((HtmlPage) enclosedPage);
96 }
97 finally {
98 if (LOG.isDebugEnabled()) {
99 LOG.debug("Finished executing " + this + ".");
100 }
101 }
102 }
103
104
105 @Override
106 public String toString() {
107 return "JavaScript Execution Job " + getId() + ": " + label_;
108 }
109
110
111
112
113
114 protected abstract void runJavaScript(HtmlPage page);
115 }