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 import org.htmlunit.WebWindow;
18 import org.htmlunit.html.HtmlPage;
19 import org.htmlunit.html.csp.ContentSecurityPolicy;
20
21 /**
22 * A {@link JavaScriptJob} created from a string of code.
23 *
24 * @author Brad Clarke
25 * @author Ronald Brill
26 */
27 class JavaScriptStringJob extends JavaScriptExecutionJob {
28
29 /** The JavaScript code to execute. */
30 private final String script_;
31
32 /**
33 * Creates a new JavaScript execution job, where the JavaScript code to execute is a string.
34 * @param initialDelay the initial amount of time to wait before executing this job
35 * @param period the amount of time to wait between executions of this job (perhaps {@code null})
36 * @param label the label for the job
37 * @param window the window to which the job belongs
38 * @param script the JavaScript code to execute
39 */
40 JavaScriptStringJob(final int initialDelay, final Integer period, final String label,
41 final WebWindow window, final String script) {
42 super(initialDelay, period, label, window);
43 script_ = script;
44 }
45
46 /** {@inheritDoc} */
47 @Override
48 protected void runJavaScript(final HtmlPage page) {
49 if (script_ == null) {
50 return;
51 }
52 if (ContentSecurityPolicy.allowsEval(page, script_, 0)) {
53 page.executeJavaScript(script_, "JavaScriptStringJob", 1);
54 }
55 }
56
57 }