29b2d43feb47fc67723636785cd437f38ac4c844
[WebKit-https.git] / WebCore / bindings / v8 / custom / V8WorkerCustom.cpp
1 /*
2  * Copyright (C) 2009 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32
33 #if ENABLE(WORKERS)
34
35 #include "Worker.h"
36
37 #include "ExceptionCode.h"
38 #include "Frame.h"
39 #include "V8Binding.h"
40 #include "V8CustomBinding.h"
41 #include "V8ObjectEventListener.h"
42 #include "V8Proxy.h"
43 #include "V8Utilities.h"
44 #include "WorkerContext.h"
45 #include "WorkerContextExecutionProxy.h"
46
47 namespace WebCore {
48
49 CALLBACK_FUNC_DECL(WorkerConstructor)
50 {
51     INC_STATS(L"DOM.Worker.Constructor");
52
53     if (!args.IsConstructCall()) {
54         return throwError("DOM object constructor cannot be called as a function.");
55     }
56
57     if (args.Length() == 0) {
58         return throwError("Not enough arguments", V8Proxy::SyntaxError);
59     }
60
61     v8::TryCatch tryCatch;
62     v8::Handle<v8::String> scriptUrl = args[0]->ToString();
63     if (tryCatch.HasCaught()) {
64         return throwError(tryCatch.Exception());
65     }
66     if (scriptUrl.IsEmpty())
67         return v8::Undefined();
68
69     // Get the script execution context.
70     ScriptExecutionContext* context = 0;
71     WorkerContextExecutionProxy* proxy = WorkerContextExecutionProxy::retrieve();
72     if (proxy)
73         context = proxy->workerContext();
74     else {
75         Frame* frame = V8Proxy::retrieveFrame();
76         if (!frame)
77             return v8::Undefined();
78         context = frame->document();
79     }
80
81     // Create the worker object.
82     // Note: it's OK to let this RefPtr go out of scope because we also call setDOMWrapper(), which effectively holds a reference to obj.
83     ExceptionCode ec = 0;
84     RefPtr<Worker> obj = Worker::create(toWebCoreString(scriptUrl), context, ec);
85
86     if (ec)
87         return throwError(ec);
88
89     // Setup the standard wrapper object internal fields.
90     v8::Handle<v8::Object> wrapperObject = args.Holder();
91     V8DOMWrapper::setDOMWrapper(wrapperObject, V8ClassIndex::WORKER, obj.get());
92
93     obj->ref();
94     V8DOMWrapper::setJSWrapperForActiveDOMObject(obj.get(), v8::Persistent<v8::Object>::New(wrapperObject));
95
96     return wrapperObject;
97 }
98
99 PassRefPtr<EventListener> getEventListener(Worker* worker, v8::Local<v8::Value> value, bool findOnly)
100 {
101     if (worker->scriptExecutionContext()->isWorkerContext()) {
102         WorkerContextExecutionProxy* workerContextProxy = WorkerContextExecutionProxy::retrieve();
103         ASSERT(workerContextProxy);
104         return workerContextProxy->findOrCreateObjectEventListener(value, false, findOnly);
105     }
106
107     V8Proxy* proxy = V8Proxy::retrieve(worker->scriptExecutionContext());
108     if (proxy)
109         return findOnly ? proxy->findObjectEventListener(value, false) : proxy->findOrCreateObjectEventListener(value, false);
110
111     return 0;
112 }
113
114 ACCESSOR_GETTER(WorkerOnmessage)
115 {
116     INC_STATS(L"DOM.Worker.onmessage._get");
117     Worker* worker = V8DOMWrapper::convertToNativeObject<Worker>(V8ClassIndex::WORKER, info.Holder());
118     if (worker->onmessage()) {
119         V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(worker->onmessage());
120         v8::Local<v8::Object> v8Listener = listener->getListenerObject();
121         return v8Listener;
122     }
123     return v8::Undefined();
124 }
125
126 ACCESSOR_SETTER(WorkerOnmessage)
127 {
128     INC_STATS(L"DOM.Worker.onmessage._set");
129     Worker* worker = V8DOMWrapper::convertToNativeObject<Worker>(V8ClassIndex::WORKER, info.Holder());
130     V8ObjectEventListener* oldListener = static_cast<V8ObjectEventListener*>(worker->onmessage());
131     if (value->IsNull()) {
132         if (oldListener) {
133             v8::Local<v8::Object> oldV8Listener = oldListener->getListenerObject();
134             removeHiddenDependency(info.Holder(), oldV8Listener, V8Custom::kWorkerRequestCacheIndex);
135         }
136
137         // Clear the listener.
138         worker->setOnmessage(0);
139     } else {
140         RefPtr<EventListener> listener = getEventListener(worker, value, false);
141         if (listener) {
142             if (oldListener) {
143                 v8::Local<v8::Object> oldV8Listener = oldListener->getListenerObject();
144                 removeHiddenDependency(info.Holder(), oldV8Listener, V8Custom::kWorkerRequestCacheIndex);
145             }
146
147             worker->setOnmessage(listener);
148             createHiddenDependency(info.Holder(), value, V8Custom::kWorkerRequestCacheIndex);
149         }
150     }
151 }
152
153 ACCESSOR_GETTER(WorkerOnerror)
154 {
155     INC_STATS(L"DOM.Worker.onerror._get");
156     Worker* worker = V8DOMWrapper::convertToNativeObject<Worker>(V8ClassIndex::WORKER, info.Holder());
157     if (worker->onerror()) {
158         V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(worker->onerror());
159         v8::Local<v8::Object> v8Listener = listener->getListenerObject();
160         return v8Listener;
161     }
162     return v8::Undefined();
163 }
164
165 ACCESSOR_SETTER(WorkerOnerror)
166 {
167     INC_STATS(L"DOM.Worker.onerror._set");
168     Worker* worker = V8DOMWrapper::convertToNativeObject<Worker>(V8ClassIndex::WORKER, info.Holder());
169     V8ObjectEventListener* oldListener = static_cast<V8ObjectEventListener*>(worker->onerror());
170     if (value->IsNull()) {
171         if (oldListener) {
172             v8::Local<v8::Object> oldV8Listener = oldListener->getListenerObject();
173             removeHiddenDependency(info.Holder(), oldV8Listener, V8Custom::kWorkerRequestCacheIndex);
174         }
175
176         // Clear the listener.
177         worker->setOnerror(0);
178     } else {
179         RefPtr<EventListener> listener = getEventListener(worker, value, false);
180         if (listener) {
181             if (oldListener) {
182                 v8::Local<v8::Object> oldV8Listener = oldListener->getListenerObject();
183                 removeHiddenDependency(info.Holder(), oldV8Listener, V8Custom::kWorkerRequestCacheIndex);
184             }
185
186             worker->setOnerror(listener);
187             createHiddenDependency(info.Holder(), value, V8Custom::kWorkerRequestCacheIndex);
188         }
189     }
190 }
191
192 CALLBACK_FUNC_DECL(WorkerAddEventListener)
193 {
194     INC_STATS(L"DOM.Worker.addEventListener()");
195     Worker* worker = V8DOMWrapper::convertToNativeObject<Worker>(V8ClassIndex::WORKER, args.Holder());
196
197     RefPtr<EventListener> listener = getEventListener(worker, args[1], false);
198     if (listener) {
199         String type = toWebCoreString(args[0]);
200         bool useCapture = args[2]->BooleanValue();
201         worker->addEventListener(type, listener, useCapture);
202
203         createHiddenDependency(args.Holder(), args[1], V8Custom::kWorkerRequestCacheIndex);
204     }
205     return v8::Undefined();
206 }
207
208 CALLBACK_FUNC_DECL(WorkerRemoveEventListener)
209 {
210     INC_STATS(L"DOM.Worker.removeEventListener()");
211     Worker* worker = V8DOMWrapper::convertToNativeObject<Worker>(V8ClassIndex::WORKER, args.Holder());
212
213     RefPtr<EventListener> listener = getEventListener(worker, args[1], true);
214     if (listener) {
215         String type = toWebCoreString(args[0]);
216         bool useCapture = args[2]->BooleanValue();
217         worker->removeEventListener(type, listener.get(), useCapture);
218
219         removeHiddenDependency(args.Holder(), args[1], V8Custom::kWorkerRequestCacheIndex);
220     }
221
222     return v8::Undefined();
223 }
224
225 } // namespace WebCore
226
227 #endif // ENABLE(WORKERS)