1 /*
2 * Copyright (c) 2002-2025 Gargoyle Software Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 package org.htmlunit.javascript.background;
16
17 /**
18 * A JavaScript-triggered background job managed by a {@link JavaScriptJobManager}.
19 *
20 * @author Daniel Gredler
21 * @author Amit Manjhi
22 */
23 public abstract class BasicJavaScriptJob implements JavaScriptJob {
24
25 /** The job ID. */
26 private Integer id_;
27
28 /** The initial amount of time to wait before executing this job. */
29 private final int initialDelay_;
30
31 /** The amount of time to wait between executions of this job (maybe {@code null}). */
32 private final Integer period_;
33
34 private final boolean executeAsap_;
35
36 /**
37 * The time at which this job should be executed.
38 * Note: the browser will make its best effort to execute the job at the target
39 * time, as specified by the timeout/interval. However, depending on other
40 * scheduled jobs, this target time may not be the actual time at which the job
41 * is executed.
42 */
43 private long targetExecutionTime_;
44
45 /** Creates a new job instance that executes once, immediately. */
46 public BasicJavaScriptJob() {
47 this(0, null);
48 }
49
50 /**
51 * Creates a new job instance.
52 * @param initialDelay the initial amount of time to wait before executing this job
53 * @param period the amount of time to wait between executions of this job (maybe {@code null})
54 */
55 BasicJavaScriptJob(final int initialDelay, final Integer period) {
56 initialDelay_ = initialDelay;
57 period_ = period;
58 targetExecutionTime_ = initialDelay + System.currentTimeMillis();
59 executeAsap_ = initialDelay == 0; // XHR are currently run as jobs and should have priority
60 }
61
62 /**
63 * Sets the job ID.
64 * @param id the job ID
65 */
66 @Override
67 public void setId(final Integer id) {
68 id_ = id;
69 }
70
71 /**
72 * Returns the job ID.
73 * @return the job ID
74 */
75 @Override
76 public Integer getId() {
77 return id_;
78 }
79
80 /**
81 * Returns the initial amount of time to wait before executing this job.
82 * @return the initial amount of time to wait before executing this job
83 */
84 public int getInitialDelay() {
85 return initialDelay_;
86 }
87
88 /**
89 * Returns the amount of time to wait between executions of this job (maybe {@code null}).
90 * @return the amount of time to wait between executions of this job (maybe {@code null})
91 */
92 @Override
93 public Integer getPeriod() {
94 return period_;
95 }
96
97 /**
98 * Returns {@code true} if this job executes periodically.
99 * @return {@code true} if this job executes periodically
100 */
101 @Override
102 public boolean isPeriodic() {
103 return period_ != null;
104 }
105
106 /**
107 * Returns {@code true} if this job has to be executed asap.
108 * @return {@code true} if this job has to be executed asap
109 */
110 @Override
111 public boolean isExecuteAsap() {
112 return executeAsap_;
113 }
114
115 /** {@inheritDoc} */
116 @Override
117 public String toString() {
118 return "JavaScript Job " + id_;
119 }
120
121 /** {@inheritDoc} */
122 @Override
123 public int compareTo(final JavaScriptJob other) {
124 final boolean xhr1 = executeAsap_;
125 final boolean xhr2 = other.isExecuteAsap();
126
127 if (xhr1 && xhr2) {
128 return getId().intValue() - other.getId().intValue();
129 }
130
131 if (xhr1) {
132 return -1;
133 }
134
135 if (xhr2) {
136 return 1;
137 }
138
139 final long span = targetExecutionTime_ - other.getTargetExecutionTime();
140
141 if (span == 0L) {
142 return id_ - other.getId();
143 }
144
145 return (int) span;
146 }
147
148 /**
149 * Returns the target execution time of the job.
150 * @return the target execution time in ms
151 */
152 @Override
153 public long getTargetExecutionTime() {
154 return targetExecutionTime_;
155 }
156
157 /**
158 * Sets the target execution time of the job.
159 * @param targetExecutionTime the new target execution time.
160 */
161 @Override
162 public void setTargetExecutionTime(final long targetExecutionTime) {
163 targetExecutionTime_ = targetExecutionTime;
164 }
165
166 }