• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
CALLBACK_FUNC_DECL(WorkerConstructor)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     if (!args.Length())
57         return throwError("Not enough arguments", V8Proxy::SyntaxError);
58 
59     v8::TryCatch tryCatch;
60     v8::Handle<v8::String> scriptUrl = args[0]->ToString();
61     if (tryCatch.HasCaught())
62         return throwError(tryCatch.Exception());
63 
64     if (scriptUrl.IsEmpty())
65         return v8::Undefined();
66 
67     // Get the script execution context.
68     ScriptExecutionContext* context = 0;
69     WorkerContextExecutionProxy* proxy = WorkerContextExecutionProxy::retrieve();
70     if (proxy)
71         context = proxy->workerContext();
72     else {
73         Frame* frame = V8Proxy::retrieveFrameForCurrentContext();
74         if (!frame)
75             return v8::Undefined();
76         context = frame->document();
77     }
78 
79     // Create the worker object.
80     // Note: it's OK to let this RefPtr go out of scope because we also call setDOMWrapper(), which effectively holds a reference to obj.
81     ExceptionCode ec = 0;
82     RefPtr<Worker> obj = Worker::create(toWebCoreString(scriptUrl), context, ec);
83     if (ec)
84         return throwError(ec);
85 
86     // Setup the standard wrapper object internal fields.
87     v8::Handle<v8::Object> wrapperObject = args.Holder();
88     V8DOMWrapper::setDOMWrapper(wrapperObject, V8ClassIndex::WORKER, obj.get());
89 
90     obj->ref();
91     V8DOMWrapper::setJSWrapperForActiveDOMObject(obj.get(), v8::Persistent<v8::Object>::New(wrapperObject));
92 
93     return wrapperObject;
94 }
95 
getEventListener(Worker * worker,v8::Local<v8::Value> value,bool isAttribute,bool findOnly)96 PassRefPtr<EventListener> getEventListener(Worker* worker, v8::Local<v8::Value> value, bool isAttribute, bool findOnly)
97 {
98     if (worker->scriptExecutionContext()->isWorkerContext()) {
99         WorkerContextExecutionProxy* workerContextProxy = WorkerContextExecutionProxy::retrieve();
100         ASSERT(workerContextProxy);
101         return workerContextProxy->findOrCreateObjectEventListener(value, isAttribute, findOnly);
102     }
103 
104     V8Proxy* proxy = V8Proxy::retrieve(worker->scriptExecutionContext());
105     if (proxy) {
106         V8EventListenerList* list = proxy->objectListeners();
107         return findOnly ? list->findWrapper(value, isAttribute) : list->findOrCreateWrapper<V8ObjectEventListener>(proxy->frame(), value, isAttribute);
108     }
109 
110     return 0;
111 }
112 
ACCESSOR_GETTER(WorkerOnmessage)113 ACCESSOR_GETTER(WorkerOnmessage)
114 {
115     INC_STATS(L"DOM.Worker.onmessage._get");
116     Worker* worker = V8DOMWrapper::convertToNativeObject<Worker>(V8ClassIndex::WORKER, info.Holder());
117     if (worker->onmessage()) {
118         V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(worker->onmessage());
119         v8::Local<v8::Object> v8Listener = listener->getListenerObject();
120         return v8Listener;
121     }
122     return v8::Undefined();
123 }
124 
ACCESSOR_SETTER(WorkerOnmessage)125 ACCESSOR_SETTER(WorkerOnmessage)
126 {
127     INC_STATS(L"DOM.Worker.onmessage._set");
128     Worker* worker = V8DOMWrapper::convertToNativeObject<Worker>(V8ClassIndex::WORKER, info.Holder());
129     V8ObjectEventListener* oldListener = static_cast<V8ObjectEventListener*>(worker->onmessage());
130     if (value->IsNull()) {
131         if (oldListener) {
132             v8::Local<v8::Object> oldV8Listener = oldListener->getListenerObject();
133             removeHiddenDependency(info.Holder(), oldV8Listener, V8Custom::kWorkerRequestCacheIndex);
134         }
135 
136         // Clear the listener.
137         worker->setOnmessage(0);
138     } else {
139         RefPtr<EventListener> listener = getEventListener(worker, value, true, false);
140         if (listener) {
141             if (oldListener) {
142                 v8::Local<v8::Object> oldV8Listener = oldListener->getListenerObject();
143                 removeHiddenDependency(info.Holder(), oldV8Listener, V8Custom::kWorkerRequestCacheIndex);
144             }
145 
146             worker->setOnmessage(listener);
147             createHiddenDependency(info.Holder(), value, V8Custom::kWorkerRequestCacheIndex);
148         }
149     }
150 }
151 
152 } // namespace WebCore
153 
154 #endif // ENABLE(WORKERS)
155