WebCore:
[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     RefPtr<Worker> obj = Worker::create(toWebCoreString(scriptUrl), context);
84
85     // Setup the standard wrapper object internal fields.
86     v8::Handle<v8::Object> wrapperObject = args.Holder();
87     V8DOMWrapper::setDOMWrapper(wrapperObject, V8ClassIndex::WORKER, obj.get());
88
89     obj->ref();
90     V8DOMWrapper::setJSWrapperForActiveDOMObject(obj.get(), v8::Persistent<v8::Object>::New(wrapperObject));
91
92     return wrapperObject;
93 }
94
95 PassRefPtr<EventListener> getEventListener(Worker* worker, v8::Local<v8::Value> value, bool findOnly)
96 {
97     if (worker->scriptExecutionContext()->isWorkerContext()) {
98         WorkerContextExecutionProxy* workerContextProxy = WorkerContextExecutionProxy::retrieve();
99         ASSERT(workerContextProxy);
100         return workerContextProxy->findOrCreateObjectEventListener(value, false, findOnly);
101     }
102
103     V8Proxy* proxy = V8Proxy::retrieve(worker->scriptExecutionContext());
104     if (proxy)
105         return findOnly ? proxy->findObjectEventListener(value, false) : proxy->findOrCreateObjectEventListener(value, false);
106
107     return 0;
108 }
109
110 ACCESSOR_GETTER(WorkerOnmessage)
111 {
112     INC_STATS(L"DOM.Worker.onmessage._get");
113     Worker* worker = V8DOMWrapper::convertToNativeObject<Worker>(V8ClassIndex::WORKER, info.Holder());
114     if (worker->onmessage()) {
115         V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(worker->onmessage());
116         v8::Local<v8::Object> v8Listener = listener->getListenerObject();
117         return v8Listener;
118     }
119     return v8::Undefined();
120 }
121
122 ACCESSOR_SETTER(WorkerOnmessage)
123 {
124     INC_STATS(L"DOM.Worker.onmessage._set");
125     Worker* worker = V8DOMWrapper::convertToNativeObject<Worker>(V8ClassIndex::WORKER, info.Holder());
126     V8ObjectEventListener* oldListener = static_cast<V8ObjectEventListener*>(worker->onmessage());
127     if (value->IsNull()) {
128         if (oldListener) {
129             v8::Local<v8::Object> oldV8Listener = oldListener->getListenerObject();
130             removeHiddenDependency(info.Holder(), oldV8Listener, V8Custom::kWorkerRequestCacheIndex);
131         }
132
133         // Clear the listener.
134         worker->setOnmessage(0);
135     } else {
136         RefPtr<EventListener> listener = getEventListener(worker, value, false);
137         if (listener) {
138             if (oldListener) {
139                 v8::Local<v8::Object> oldV8Listener = oldListener->getListenerObject();
140                 removeHiddenDependency(info.Holder(), oldV8Listener, V8Custom::kWorkerRequestCacheIndex);
141             }
142
143             worker->setOnmessage(listener);
144             createHiddenDependency(info.Holder(), value, V8Custom::kWorkerRequestCacheIndex);
145         }
146     }
147 }
148
149 ACCESSOR_GETTER(WorkerOnerror)
150 {
151     INC_STATS(L"DOM.Worker.onerror._get");
152     Worker* worker = V8DOMWrapper::convertToNativeObject<Worker>(V8ClassIndex::WORKER, info.Holder());
153     if (worker->onerror()) {
154         V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(worker->onerror());
155         v8::Local<v8::Object> v8Listener = listener->getListenerObject();
156         return v8Listener;
157     }
158     return v8::Undefined();
159 }
160
161 ACCESSOR_SETTER(WorkerOnerror)
162 {
163     INC_STATS(L"DOM.Worker.onerror._set");
164     Worker* worker = V8DOMWrapper::convertToNativeObject<Worker>(V8ClassIndex::WORKER, info.Holder());
165     V8ObjectEventListener* oldListener = static_cast<V8ObjectEventListener*>(worker->onerror());
166     if (value->IsNull()) {
167         if (oldListener) {
168             v8::Local<v8::Object> oldV8Listener = oldListener->getListenerObject();
169             removeHiddenDependency(info.Holder(), oldV8Listener, V8Custom::kWorkerRequestCacheIndex);
170         }
171
172         // Clear the listener.
173         worker->setOnerror(0);
174     } else {
175         RefPtr<EventListener> listener = getEventListener(worker, value, false);
176         if (listener) {
177             if (oldListener) {
178                 v8::Local<v8::Object> oldV8Listener = oldListener->getListenerObject();
179                 removeHiddenDependency(info.Holder(), oldV8Listener, V8Custom::kWorkerRequestCacheIndex);
180             }
181
182             worker->setOnerror(listener);
183             createHiddenDependency(info.Holder(), value, V8Custom::kWorkerRequestCacheIndex);
184         }
185     }
186 }
187
188 CALLBACK_FUNC_DECL(WorkerAddEventListener)
189 {
190     INC_STATS(L"DOM.Worker.addEventListener()");
191     Worker* worker = V8DOMWrapper::convertToNativeObject<Worker>(V8ClassIndex::WORKER, args.Holder());
192
193     RefPtr<EventListener> listener = getEventListener(worker, args[1], false);
194     if (listener) {
195         String type = toWebCoreString(args[0]);
196         bool useCapture = args[2]->BooleanValue();
197         worker->addEventListener(type, listener, useCapture);
198
199         createHiddenDependency(args.Holder(), args[1], V8Custom::kWorkerRequestCacheIndex);
200     }
201     return v8::Undefined();
202 }
203
204 CALLBACK_FUNC_DECL(WorkerRemoveEventListener)
205 {
206     INC_STATS(L"DOM.Worker.removeEventListener()");
207     Worker* worker = V8DOMWrapper::convertToNativeObject<Worker>(V8ClassIndex::WORKER, args.Holder());
208
209     RefPtr<EventListener> listener = getEventListener(worker, args[1], true);
210     if (listener) {
211         String type = toWebCoreString(args[0]);
212         bool useCapture = args[2]->BooleanValue();
213         worker->removeEventListener(type, listener.get(), useCapture);
214
215         removeHiddenDependency(args.Holder(), args[1], V8Custom::kWorkerRequestCacheIndex);
216     }
217
218     return v8::Undefined();
219 }
220
221 } // namespace WebCore
222
223 #endif // ENABLE(WORKERS)