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.WebClient; 18 import org.htmlunit.WebWindow; 19 import org.htmlunit.corejs.javascript.ContextAction; 20 import org.htmlunit.corejs.javascript.ContextFactory; 21 import org.htmlunit.corejs.javascript.Function; 22 23 /** 24 * A factory for all the things we have to construct from outside the 25 * JavaScript engine. 26 * 27 * @author Ronald Brill 28 * @author Atsushi Nakagawa 29 */ 30 public class BackgroundJavaScriptFactory { 31 32 private static BackgroundJavaScriptFactory Factory_ = new BackgroundJavaScriptFactory(); 33 34 /** 35 * Returns the current factory. 36 * 37 * @return the active factory 38 */ 39 public static BackgroundJavaScriptFactory theFactory() { 40 return Factory_; 41 } 42 43 /** 44 * Set the factory to a new one. 45 * 46 * @param factory the new factory. 47 */ 48 public static void setFactory(final BackgroundJavaScriptFactory factory) { 49 Factory_ = factory; 50 } 51 52 /** 53 * Creates a new JavaScript execution job, where the JavaScript code to execute is a string. 54 * @param initialDelay the initial amount of time to wait before executing this job 55 * @param period the amount of time to wait between executions of this job (maybe {@code null}) 56 * @param label the label for the job 57 * @param window the window to which the job belongs 58 * @param script the JavaScript code to execute 59 * 60 * @return JavaScriptJob the created job 61 */ 62 public JavaScriptJob createJavaScriptJob(final int initialDelay, final Integer period, final String label, 63 final WebWindow window, final String script) { 64 return new JavaScriptStringJob(initialDelay, period, label, window, script); 65 } 66 67 /** 68 * Creates a new JavaScript execution job, where the JavaScript code to execute is a function. 69 * @param initialDelay the initial amount of time to wait before executing this job 70 * @param period the amount of time to wait between executions of this job (maybe {@code null}) 71 * @param label the label for the job 72 * @param window the window to which the job belongs 73 * @param function the JavaScript code to execute 74 * @param args the arguments to pass into the function call 75 * 76 * @return JavaScriptJob the created job 77 */ 78 public JavaScriptFunctionJob createJavaScriptJob(final int initialDelay, 79 final Integer period, final String label, 80 final WebWindow window, final Function function, final Object[] args) { 81 return new JavaScriptFunctionJob(initialDelay, period, label, window, function, args); 82 } 83 84 /** 85 * Creates a new job for XMLHttpRequestProcessing. 86 * @param contextFactory the ContextFactory 87 * @param action the action 88 * 89 * @return JavaScriptJob the created job 90 */ 91 public JavaScriptJob createJavascriptXMLHttpRequestJob(final ContextFactory contextFactory, 92 final ContextAction<Object> action) { 93 return new JavascriptXMLHttpRequestJob(contextFactory, action); 94 } 95 96 /** 97 * Creates a new job. 98 * @param initialDelay the initial amount of time to wait before executing this job 99 * @param period the amount of time to wait between executions of this job (maybe {@code null}) 100 * @param runnable the runnable to run 101 * 102 * @return JavaScriptJob the created job 103 */ 104 public JavaScriptJob createJavaScriptJob(final int initialDelay, final Integer period, final Runnable runnable) { 105 return new BasicJavaScriptJob(initialDelay, period) { 106 @Override 107 public void run() { 108 runnable.run(); 109 } 110 }; 111 } 112 113 /** 114 * Creates the {@link JavaScriptExecutor} that will be used to handle JS. 115 * @param webClient the WebClient of the executor 116 * @return the executor. 117 */ 118 public JavaScriptExecutor createJavaScriptExecutor(final WebClient webClient) { 119 return new DefaultJavaScriptExecutor(webClient); 120 } 121 122 /** 123 * Creates a new JavaScriptJobManager for the given window. 124 * @param webWindow the window the JavaScriptJobManager will work for 125 * @return the new JavaScriptJobManager 126 */ 127 public JavaScriptJobManager createJavaScriptJobManager(final WebWindow webWindow) { 128 return new JavaScriptJobManagerImpl(webWindow); 129 } 130 131 /** 132 * The constructor. 133 */ 134 protected BackgroundJavaScriptFactory() { 135 super(); 136 } 137 }