View Javadoc
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  import org.htmlunit.WebWindow;
18  import org.htmlunit.corejs.javascript.Function;
19  import org.htmlunit.corejs.javascript.Scriptable;
20  import org.htmlunit.html.DomElement;
21  import org.htmlunit.html.HtmlPage;
22  
23  /**
24   * A {@link JavaScriptJob} created from a {@link Function} object.
25   * @author Brad Clarke
26   * @author Ronald Brill
27   * @author Atsushi Nakagawa
28   */
29  class JavaScriptFunctionJob extends JavaScriptExecutionJob {
30  
31      /** The JavaScript code to execute. */
32      private final Function function_;
33      private final Object[] args_;
34  
35      /**
36       * Creates a new JavaScript execution job, where the JavaScript code to execute is a function.
37       * @param initialDelay the initial amount of time to wait before executing this job
38       * @param period the amount of time to wait between executions of this job (may be {@code null})
39       * @param label the label for the job
40       * @param window the window to which the job belongs
41       * @param function the JavaScript code to execute
42       * @param args the arguments to pass into the function call
43       */
44      JavaScriptFunctionJob(final int initialDelay, final Integer period, final String label,
45          final WebWindow window, final Function function, final Object[] args) {
46          super(initialDelay, period, label, window);
47          function_ = function;
48          args_ = args;
49      }
50  
51      /** {@inheritDoc} */
52      @Override
53      protected void runJavaScript(final HtmlPage page) {
54          final DomElement doc = page.getDocumentElement();
55          final Scriptable scriptable = page.getEnclosingWindow().getScriptableObject();
56          page.executeJavaScriptFunction(function_, scriptable, args_, doc);
57      }
58  
59  }