1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.javascript.host.worker;
16
17 import org.htmlunit.WebClient;
18 import org.htmlunit.corejs.javascript.Context;
19 import org.htmlunit.corejs.javascript.Function;
20 import org.htmlunit.corejs.javascript.Scriptable;
21 import org.htmlunit.corejs.javascript.VarScope;
22 import org.htmlunit.javascript.JavaScriptEngine;
23 import org.htmlunit.javascript.configuration.JsxClass;
24 import org.htmlunit.javascript.configuration.JsxConstructor;
25 import org.htmlunit.javascript.configuration.JsxFunction;
26 import org.htmlunit.javascript.configuration.JsxGetter;
27 import org.htmlunit.javascript.configuration.JsxSetter;
28 import org.htmlunit.javascript.host.Window;
29 import org.htmlunit.javascript.host.event.Event;
30 import org.htmlunit.javascript.host.event.EventTarget;
31
32
33
34
35
36
37
38
39
40
41 @JsxClass
42 public class Worker extends EventTarget {
43
44 private final DedicatedWorkerGlobalScope workerScope_;
45
46
47
48
49 public Worker() {
50
51 super();
52 workerScope_ = null;
53 }
54
55 private Worker(final Context cx, final VarScope scope,
56 final Window owningWindow, final String url,
57 final Scriptable options) throws Exception {
58 super();
59 setParentScope(scope);
60 setPrototype(getPrototype(getClass()));
61
62 final WebClient webClient = getWindow().getWebWindow().getWebClient();
63 String name = null;
64 if (options != null && options.has("name", options)) {
65 name = JavaScriptEngine.toString(options.get("name", options));
66 }
67 workerScope_ = new DedicatedWorkerGlobalScope(owningWindow, cx, webClient, name, this);
68
69 workerScope_.loadAndExecute(webClient, url, null, false);
70 }
71
72
73
74
75
76
77
78
79
80
81
82
83 @JsxConstructor
84 public static Worker jsConstructor(final Context cx, final VarScope scope,
85 final Object[] args, final Function ctorObj, final boolean inNewExpr) throws Exception {
86 if (args.length < 1 || args.length > 2) {
87 throw JavaScriptEngine.reportRuntimeError(
88 "Worker Error: constructor must have one or two parameters.");
89 }
90
91 final String url = JavaScriptEngine.toString(args[0]);
92 Scriptable options = null;
93 if (args.length > 1 && args[1] instanceof Scriptable scriptable) {
94 options = scriptable;
95 }
96 return new Worker(cx, scope, getWindow(ctorObj), url, options);
97 }
98
99
100
101
102
103
104 @JsxFunction
105 public void postMessage(final Object message) {
106 workerScope_.messagePosted(message);
107 }
108
109
110
111
112
113 @JsxFunction
114 public void terminate() {
115
116 }
117
118
119
120
121
122
123 @JsxSetter
124 public void setOnmessage(final Object onmessage) {
125 getEventListenersContainer().setEventHandler(Event.TYPE_MESSAGE, onmessage);
126 }
127
128
129
130
131
132
133 @JsxGetter
134 public Function getOnmessage() {
135 return getEventListenersContainer().getEventHandler(Event.TYPE_MESSAGE);
136 }
137 }