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 "DOMTimer.h"
36 #include "ExceptionCode.h"
37 #include "ScheduledAction.h"
38 #include "V8Binding.h"
39 #include "V8CustomBinding.h"
40 #include "V8Proxy.h"
41 #include "V8Utilities.h"
42 #include "V8WorkerContextEventListener.h"
43 #include "WorkerContext.h"
44 #include "WorkerContextExecutionProxy.h"
45
46 namespace WebCore {
47
ACCESSOR_GETTER(WorkerContextSelf)48 ACCESSOR_GETTER(WorkerContextSelf)
49 {
50 INC_STATS(L"DOM.WorkerContext.self._get");
51 WorkerContext* workerContext = V8DOMWrapper::convertDOMWrapperToNative<WorkerContext>(info.Holder());
52 return WorkerContextExecutionProxy::convertWorkerContextToV8Object(workerContext);
53 }
54
ACCESSOR_GETTER(WorkerContextOnerror)55 ACCESSOR_GETTER(WorkerContextOnerror)
56 {
57 INC_STATS(L"DOM.WorkerContext.onerror._get");
58 WorkerContext* workerContext = V8DOMWrapper::convertToNativeObject<WorkerContext>(V8ClassIndex::WORKERCONTEXT, info.Holder());
59 if (workerContext->onerror()) {
60 V8WorkerContextEventListener* listener = static_cast<V8WorkerContextEventListener*>(workerContext->onerror());
61 v8::Local<v8::Object> v8Listener = listener->getListenerObject();
62 return v8Listener;
63 }
64 return v8::Undefined();
65 }
66
ACCESSOR_SETTER(WorkerContextOnerror)67 ACCESSOR_SETTER(WorkerContextOnerror)
68 {
69 INC_STATS(L"DOM.WorkerContext.onerror._set");
70 WorkerContext* workerContext = V8DOMWrapper::convertToNativeObject<WorkerContext>(V8ClassIndex::WORKERCONTEXT, info.Holder());
71 V8WorkerContextEventListener* oldListener = static_cast<V8WorkerContextEventListener*>(workerContext->onerror());
72 if (value->IsNull()) {
73 if (workerContext->onerror()) {
74 v8::Local<v8::Object> oldV8Listener = oldListener->getListenerObject();
75 removeHiddenDependency(info.Holder(), oldV8Listener, V8Custom::kWorkerContextRequestCacheIndex);
76 }
77
78 // Clear the listener.
79 workerContext->setOnerror(0);
80 } else {
81 RefPtr<V8EventListener> listener = workerContext->script()->proxy()->findOrCreateEventListener(v8::Local<v8::Object>::Cast(value), true, false);
82 if (listener) {
83 if (oldListener) {
84 v8::Local<v8::Object> oldV8Listener = oldListener->getListenerObject();
85 removeHiddenDependency(info.Holder(), oldV8Listener, V8Custom::kWorkerContextRequestCacheIndex);
86 }
87
88 workerContext->setOnerror(listener);
89 createHiddenDependency(info.Holder(), value, V8Custom::kWorkerContextRequestCacheIndex);
90 }
91 }
92 }
93
SetTimeoutOrInterval(const v8::Arguments & args,bool singleShot)94 v8::Handle<v8::Value> SetTimeoutOrInterval(const v8::Arguments& args, bool singleShot)
95 {
96 WorkerContext* workerContext = V8DOMWrapper::convertDOMWrapperToNative<WorkerContext>(args.Holder());
97
98 int argumentCount = args.Length();
99 if (argumentCount < 1)
100 return v8::Undefined();
101
102 v8::Handle<v8::Value> function = args[0];
103 int32_t timeout = argumentCount >= 2 ? args[1]->Int32Value() : 0;
104 int timerId;
105
106 if (function->IsString()) {
107 WebCore::String stringFunction = toWebCoreString(function);
108 timerId = DOMTimer::install(workerContext, new ScheduledAction(stringFunction, workerContext->url()), timeout, singleShot);
109 } else if (function->IsFunction()) {
110 size_t paramCount = argumentCount >= 2 ? argumentCount - 2 : 0;
111 v8::Local<v8::Value>* params = 0;
112 if (paramCount > 0) {
113 params = new v8::Local<v8::Value>[paramCount];
114 for (size_t i = 0; i < paramCount; ++i)
115 params[i] = args[i+2];
116 }
117 // ScheduledAction takes ownership of actual params and releases them in its destructor.
118 ScheduledAction* action = new ScheduledAction(v8::Handle<v8::Function>::Cast(function), paramCount, params);
119 delete [] params;
120 timerId = DOMTimer::install(workerContext, action, timeout, singleShot);
121 } else
122 return v8::Undefined();
123
124 return v8::Integer::New(timerId);
125 }
126
CALLBACK_FUNC_DECL(WorkerContextImportScripts)127 CALLBACK_FUNC_DECL(WorkerContextImportScripts)
128 {
129 INC_STATS(L"DOM.WorkerContext.importScripts()");
130 if (!args.Length())
131 return v8::Undefined();
132
133 String callerURL = V8Proxy::sourceName();
134 int callerLine = V8Proxy::sourceLineNumber() + 1;
135
136 Vector<String> urls;
137 for (int i = 0; i < args.Length(); i++) {
138 v8::TryCatch tryCatch;
139 v8::Handle<v8::String> scriptUrl = args[i]->ToString();
140 if (tryCatch.HasCaught() || scriptUrl.IsEmpty())
141 return v8::Undefined();
142 urls.append(toWebCoreString(scriptUrl));
143 }
144
145 WorkerContext* workerContext = V8DOMWrapper::convertDOMWrapperToNative<WorkerContext>(args.Holder());
146
147 ExceptionCode ec = 0;
148 workerContext->importScripts(urls, callerURL, callerLine, ec);
149
150 if (ec)
151 return throwError(ec);
152
153 return v8::Undefined();
154 }
155
CALLBACK_FUNC_DECL(WorkerContextSetTimeout)156 CALLBACK_FUNC_DECL(WorkerContextSetTimeout)
157 {
158 INC_STATS(L"DOM.WorkerContext.setTimeout()");
159 return SetTimeoutOrInterval(args, true);
160 }
161
CALLBACK_FUNC_DECL(WorkerContextSetInterval)162 CALLBACK_FUNC_DECL(WorkerContextSetInterval)
163 {
164 INC_STATS(L"DOM.WorkerContext.setInterval()");
165 return SetTimeoutOrInterval(args, false);
166 }
167
CALLBACK_FUNC_DECL(WorkerContextAddEventListener)168 CALLBACK_FUNC_DECL(WorkerContextAddEventListener)
169 {
170 INC_STATS(L"DOM.WorkerContext.addEventListener()");
171 WorkerContext* workerContext = V8DOMWrapper::convertDOMWrapperToNative<WorkerContext>(args.Holder());
172
173 RefPtr<V8EventListener> listener = workerContext->script()->proxy()->findOrCreateEventListener(v8::Local<v8::Object>::Cast(args[1]), false, false);
174
175 if (listener) {
176 String type = toWebCoreString(args[0]);
177 bool useCapture = args[2]->BooleanValue();
178 workerContext->addEventListener(type, listener, useCapture);
179
180 createHiddenDependency(args.Holder(), args[1], V8Custom::kWorkerContextRequestCacheIndex);
181 }
182 return v8::Undefined();
183 }
184
CALLBACK_FUNC_DECL(WorkerContextRemoveEventListener)185 CALLBACK_FUNC_DECL(WorkerContextRemoveEventListener)
186 {
187 INC_STATS(L"DOM.WorkerContext.removeEventListener()");
188 WorkerContext* workerContext = V8DOMWrapper::convertDOMWrapperToNative<WorkerContext>(args.Holder());
189 WorkerContextExecutionProxy* proxy = workerContext->script()->proxy();
190
191 RefPtr<V8EventListener> listener = proxy->findOrCreateEventListener(v8::Local<v8::Object>::Cast(args[1]), false, true);
192
193 if (listener) {
194 String type = toWebCoreString(args[0]);
195 bool useCapture = args[2]->BooleanValue();
196 workerContext->removeEventListener(type, listener.get(), useCapture);
197
198 removeHiddenDependency(args.Holder(), args[1], V8Custom::kWorkerContextRequestCacheIndex);
199 }
200
201 return v8::Undefined();
202 }
203
204 } // namespace WebCore
205
206 #endif // ENABLE(WORKERS)
207