View Javadoc
1   /*
2    * Copyright (c) 2002-2026 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   * @author Ronald Brill
23   */
24  public abstract class BasicJavaScriptJob implements JavaScriptJob {
25  
26      /** The job ID. */
27      private Integer id_;
28  
29      /** The initial amount of time to wait before executing this job. */
30      private final int initialDelay_;
31  
32      /** The amount of time to wait between executions of this job (maybe {@code null}). */
33      private final Integer period_;
34  
35      private final boolean executeAsap_;
36  
37      /**
38       * The time at which this job should be executed.
39       * Note: the browser will make its best effort to execute the job at the target
40       * time, as specified by the timeout/interval.  However, depending on other
41       * scheduled jobs, this target time may not be the actual time at which the job
42       * is executed.
43       */
44      private long targetExecutionTime_;
45  
46      /** Creates a new job instance that executes once, immediately. */
47      public BasicJavaScriptJob() {
48          this(0, null);
49      }
50  
51      /**
52       * Creates a new job instance.
53       * @param initialDelay the initial amount of time to wait before executing this job
54       * @param period the amount of time to wait between executions of this job (maybe {@code null})
55       */
56      BasicJavaScriptJob(final int initialDelay, final Integer period) {
57          initialDelay_ = initialDelay;
58          period_ = period;
59          targetExecutionTime_ = initialDelay + System.currentTimeMillis();
60          executeAsap_ = initialDelay == 0; // XHR are currently run as jobs and should have priority
61      }
62  
63      /**
64       * Sets the job ID.
65       * @param id the job ID
66       */
67      @Override
68      public void setId(final Integer id) {
69          id_ = id;
70      }
71  
72      /**
73       * Returns the job ID.
74       * @return the job ID
75       */
76      @Override
77      public Integer getId() {
78          return id_;
79      }
80  
81      /**
82       * Returns the initial amount of time to wait before executing this job.
83       * @return the initial amount of time to wait before executing this job
84       */
85      public int getInitialDelay() {
86          return initialDelay_;
87      }
88  
89      /**
90       * Returns the amount of time to wait between executions of this job (maybe {@code null}).
91       * @return the amount of time to wait between executions of this job (maybe {@code null})
92       */
93      @Override
94      public Integer getPeriod() {
95          return period_;
96      }
97  
98      /**
99       * Returns {@code true} if this job executes periodically.
100      * @return {@code true} if this job executes periodically
101      */
102     @Override
103     public boolean isPeriodic() {
104         return period_ != null;
105     }
106 
107     /**
108      * Returns {@code true} if this job has to be executed asap.
109      * @return {@code true} if this job has to be executed asap
110      */
111     @Override
112     public boolean isExecuteAsap() {
113         return executeAsap_;
114     }
115 
116     /** {@inheritDoc} */
117     @Override
118     public String toString() {
119         return "JavaScript Job " + id_;
120     }
121 
122     /** {@inheritDoc} */
123     @Override
124     public int compareTo(final JavaScriptJob other) {
125         final boolean xhr1 = executeAsap_;
126         final boolean xhr2 = other.isExecuteAsap();
127 
128         if (xhr1 && xhr2) {
129             return getId().intValue() - other.getId().intValue();
130         }
131 
132         if (xhr1) {
133             return -1;
134         }
135 
136         if (xhr2) {
137             return 1;
138         }
139 
140         final long span = targetExecutionTime_ - other.getTargetExecutionTime();
141 
142         if (span == 0L) {
143             return id_ - other.getId();
144         }
145 
146         return (int) span;
147     }
148 
149     /**
150      * Returns the target execution time of the job.
151      * @return the target execution time in ms
152      */
153     @Override
154     public long getTargetExecutionTime() {
155         return targetExecutionTime_;
156     }
157 
158     /**
159      * Sets the target execution time of the job.
160      * @param targetExecutionTime the new target execution time.
161      */
162     @Override
163     public void setTargetExecutionTime(final long targetExecutionTime) {
164         targetExecutionTime_ = targetExecutionTime;
165     }
166 
167 }