• 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 "AbstractWorker.h"
36 
37 #include "ExceptionCode.h"
38 #include "ScriptExecutionContext.h"
39 #include "V8Binding.h"
40 #include "V8CustomBinding.h"
41 #include "V8ObjectEventListener.h"
42 #include "V8Proxy.h"
43 #include "V8Utilities.h"
44 #include "WorkerContextExecutionProxy.h"
45 
46 namespace WebCore {
47 
getEventListener(AbstractWorker * worker,v8::Local<v8::Value> value,bool isAttribute,bool findOnly)48 PassRefPtr<EventListener> getEventListener(AbstractWorker* worker, v8::Local<v8::Value> value, bool isAttribute, bool findOnly)
49 {
50     if (worker->scriptExecutionContext()->isWorkerContext()) {
51         WorkerContextExecutionProxy* workerContextProxy = WorkerContextExecutionProxy::retrieve();
52         ASSERT(workerContextProxy);
53         return workerContextProxy->findOrCreateObjectEventListener(value, isAttribute, findOnly);
54     }
55 
56     V8Proxy* proxy = V8Proxy::retrieve(worker->scriptExecutionContext());
57     if (proxy) {
58         V8EventListenerList* list = proxy->objectListeners();
59         return findOnly ? list->findWrapper(value, isAttribute) : list->findOrCreateWrapper<V8ObjectEventListener>(proxy->frame(), value, isAttribute);
60     }
61 
62     return 0;
63 }
64 
ACCESSOR_GETTER(AbstractWorkerOnerror)65 ACCESSOR_GETTER(AbstractWorkerOnerror)
66 {
67     INC_STATS(L"DOM.AbstractWorker.onerror._get");
68     AbstractWorker* worker = V8DOMWrapper::convertToNativeObject<AbstractWorker>(V8ClassIndex::ABSTRACTWORKER, info.Holder());
69     if (worker->onerror()) {
70         V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(worker->onerror());
71         v8::Local<v8::Object> v8Listener = listener->getListenerObject();
72         return v8Listener;
73     }
74     return v8::Undefined();
75 }
76 
ACCESSOR_SETTER(AbstractWorkerOnerror)77 ACCESSOR_SETTER(AbstractWorkerOnerror)
78 {
79     INC_STATS(L"DOM.AbstractWorker.onerror._set");
80     AbstractWorker* worker = V8DOMWrapper::convertToNativeObject<AbstractWorker>(V8ClassIndex::ABSTRACTWORKER, info.Holder());
81     V8ObjectEventListener* oldListener = static_cast<V8ObjectEventListener*>(worker->onerror());
82     if (value->IsNull()) {
83         if (oldListener) {
84             v8::Local<v8::Object> oldV8Listener = oldListener->getListenerObject();
85             removeHiddenDependency(info.Holder(), oldV8Listener, V8Custom::kAbstractWorkerRequestCacheIndex);
86         }
87 
88         // Clear the listener.
89         worker->setOnerror(0);
90     } else {
91         RefPtr<EventListener> listener = getEventListener(worker, value, true, false);
92         if (listener) {
93             if (oldListener) {
94                 v8::Local<v8::Object> oldV8Listener = oldListener->getListenerObject();
95                 removeHiddenDependency(info.Holder(), oldV8Listener, V8Custom::kAbstractWorkerRequestCacheIndex);
96             }
97 
98             worker->setOnerror(listener);
99             createHiddenDependency(info.Holder(), value, V8Custom::kAbstractWorkerRequestCacheIndex);
100         }
101     }
102 }
103 
CALLBACK_FUNC_DECL(AbstractWorkerAddEventListener)104 CALLBACK_FUNC_DECL(AbstractWorkerAddEventListener)
105 {
106     INC_STATS(L"DOM.AbstractWorker.addEventListener()");
107     AbstractWorker* worker = V8DOMWrapper::convertToNativeObject<AbstractWorker>(V8ClassIndex::ABSTRACTWORKER, args.Holder());
108 
109     RefPtr<EventListener> listener = getEventListener(worker, args[1], false, false);
110     if (listener) {
111         String type = toWebCoreString(args[0]);
112         bool useCapture = args[2]->BooleanValue();
113         worker->addEventListener(type, listener, useCapture);
114 
115         createHiddenDependency(args.Holder(), args[1], V8Custom::kAbstractWorkerRequestCacheIndex);
116     }
117     return v8::Undefined();
118 }
119 
CALLBACK_FUNC_DECL(AbstractWorkerRemoveEventListener)120 CALLBACK_FUNC_DECL(AbstractWorkerRemoveEventListener)
121 {
122     INC_STATS(L"DOM.AbstractWorker.removeEventListener()");
123     AbstractWorker* worker = V8DOMWrapper::convertToNativeObject<AbstractWorker>(V8ClassIndex::ABSTRACTWORKER, args.Holder());
124 
125     RefPtr<EventListener> listener = getEventListener(worker, args[1], false, true);
126     if (listener) {
127         String type = toWebCoreString(args[0]);
128         bool useCapture = args[2]->BooleanValue();
129         worker->removeEventListener(type, listener.get(), useCapture);
130 
131         removeHiddenDependency(args.Holder(), args[1], V8Custom::kAbstractWorkerRequestCacheIndex);
132     }
133 
134     return v8::Undefined();
135 }
136 
137 } // namespace WebCore
138 
139 #endif // ENABLE(WORKERS)
140